The Basics of C Programming - University of Connecticut

The Basics of C Programming

Marshall Brain Last updated: October 30, 2013

Contents

1 C programming

1

What is C? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

The simplest C program, I . . . . . . . . . . . . . . . . . . . . . . . . 2

Spacing and indentation . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Compilation and run . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

The simplest C program, II . . . . . . . . . . . . . . . . . . . . . . . . 4

Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2 Input and output

7

printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Programming exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

3 Branching and looping

13

if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Boolean expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Boolean: = vs == . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

do-while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

i

CONTENTS

CONTENTS

Looping: an example . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Programming exercise . . . . . . . . . . . . . . . . . . . . . . . . . . 22

4 Arrays

23

Programming exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

5 Variable Types

29

Typecasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Typedef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

6 Operators

33

Operator precedence, I . . . . . . . . . . . . . . . . . . . . . . . . . . 34

Incrementing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

Programming exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

7 Functions

35

Programming exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

Function prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

8 Structures

41

9 Libraries

43

Making a library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

Compiling and running . . . . . . . . . . . . . . . . . . . . . . . . . . 49

10 Makefiles

51

11 Pointers

53

Pointers: why? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

Pointer Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

ii

CONTENTS

CONTENTS

Pointers: RAM Adresses . . . . . . . . . . . . . . . . . . . . . . . . . 57 Pointing to the Same Addres . . . . . . . . . . . . . . . . . . . . . . . 62 Pointers: Common Bugs . . . . . . . . . . . . . . . . . . . . . . . . . 62

Bug #1 - Uninitialized pointers . . . . . . . . . . . . . . . . . . . 62 Bug #2 - Invalid Pointer References . . . . . . . . . . . . . . . . 63 Bug #3 - Zero Pointer Reference . . . . . . . . . . . . . . . . . . 63 Pointers: Function Parameters . . . . . . . . . . . . . . . . . . . . . . 64

12 Dynamic Data Structures

67

The Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

Malloc and Free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

13 Advanced Pointers

77

Pointer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

Pointers to Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

Pointers to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

Arrays of Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

Structures Containing Pointers . . . . . . . . . . . . . . . . . . . . . . 81

Pointers to Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

Pointers to Structures Containing Pointers . . . . . . . . . . . . . . . . 82

Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

A Linked Stack Example . . . . . . . . . . . . . . . . . . . . . . . . . 84

Programming exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

Using Pointers with Arrays . . . . . . . . . . . . . . . . . . . . . . . . 87

14 Strings

91

Special Note on String Constants . . . . . . . . . . . . . . . . . . . . . 96

Special Note on Using Strings with malloc . . . . . . . . . . . . . . . . 97

Programming exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

iii

CONTENTS

CONTENTS

15 Operator Precedence, II

99

16 Command Line Arguments

101

17 Text files

103

Text files: opening . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

Text files: reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

Main function return values . . . . . . . . . . . . . . . . . . . . . . . . 107

18 Binary Files

109

19 Further reading

115

Index

117

iv

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download