C Programming Notes for Professionals

GoalKicker.com (CC BY-SA)
Page 1 sur 342Lecteur de document UniversityLib

C Programming Notes for Professionals

Programming, C Language, Education · notes

Voir tous les documents en programmation

C

Notes for ProfessionalsC

Notes for Professionals

300+ pages

of professional hints and tricks

GoalKicker.com

Free Programming Books

Disclaimer

This is an unocial free book created for educational purposes and is

not aliated with ocial C group(s) or company(s).

All trademarks and registered trademarks are

the property of their respective owners

Contents

About

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

1

Chapter 1: Getting started with C Language

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

2

Section 1.1: Hello World

Section 1.2: Original "Hello, World!" in K&R C

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

2

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

4

Chapter 2: Comments

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

6

Section 2.1: Commenting using the preprocessor

Section 2.2: / / delimited comments

Section 2.3: // delimited comments

Section 2.4: Possible pitfall due to trigraphs

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

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

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

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

6

Chapter 3: Data Types

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

9

Section 3.1: Interpreting Declarations

Section 3.2: Fixed Width Integer Types (since C99)

Section 3.3: Integer types and constants

Section 3.4: Floating Point Constants

Section 3.5: String Literals

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

9

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

11

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

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

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

Chapter 4: Operators

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

14

Section 4.1: Relational Operators

Section 4.2: Conditional Operator/Ternary Operator

Section 4.3: Bitwise Operators

Section 4.4: Short circuit behavior of logical operators

Section 4.5: Comma Operator

Section 4.6: Arithmetic Operators

Section 4.7: Access Operators

Section 4.8: sizeof Operator

Section 4.9: Cast Operator

Section 4.10: Function Call Operator

Section 4.11: Increment / Decrement

Section 4.12: Assignment Operators

Section 4.13: Logical Operators

Section 4.14: Pointer Arithmetic

Section 4.15: _Alignof

Chapter 5: Boolean

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

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

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

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

18

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Section 5.1: Using stdbool.h

Section 5.2: Using #define

Section 5.3: Using the Intrinsic (built-in) Type _Bool

Section 5.4: Integers and pointers in Boolean expressions

Section 5.5: Defining a bool type using typedef

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

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

31

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

32

Chapter 6: Strings

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

33

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

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

Section 6.1: Tokenisation: strtok(), strtok_r() and strtok_s()

Section 6.2: String literals

Section 6.3: Calculate the Length: strlen()

Section 6.4: Basic introduction to strings

Section 6.5: Copying strings

Section 6.6: Iterating Over the Characters in a String

Section 6.7: Creating Arrays of Strings

Section 6.8: Convert Strings to Number: atoi(), atof() (dangerous, don't use them)

Section 6.9: string formatted data read/write

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

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

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

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

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

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

42

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

41

6

7

7

11

12

13

14

15

16

19

Publicité

19

22

24

24

24

25

25

26

27

28

30

30

30

31

33

35

36

37

37

40

41

Section 6.10: Find first/last occurrence of a specific character: strchr(), strrchr()

Section 6.11: Copy and Concatenation: strcpy(), strcat()

Section 6.12: Comparsion: strcmp(), strncmp(), strcasecmp(), strncasecmp()

Section 6.13: Safely convert Strings to Number: strtoX functions

Section 6.14: strspn and strcspn

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

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

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

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

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

43

Chapter 7: Literals for numbers, characters and strings

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

50

Section 7.1: Floating point literals

Section 7.2: String literals

Section 7.3: Character literals

Section 7.4: Integer literals

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

50

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

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

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

Chapter 8: Compound Literals

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

53

Section 8.1: Definition/Initialisation of Compound Literals

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

53

Chapter 9: Bit-fields

Section 9.1: Bit-fields

Section 9.2: Using bit-fields as small integers

Section 9.3: Bit-field alignment

Section 9.4: Don'ts for bit-fields

Section 9.5: When are bit-fields useful?

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

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

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

56

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

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

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

Chapter 10: Arrays

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

60

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

60

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

Section 10.1: Declaring and initializing an array

Section 10.2: Iterating through an array eciently and row-major order

Section 10.3: Array length

Section 10.4: Passing multidimensional arrays to a function

Section 10.5: Multi-dimensional arrays

Section 10.6: Define array and access array element

Section 10.7: Clearing array contents (zeroing)

Section 10.8: Setting values in arrays

Section 10.9: Allocate and zero-initialize an array with user defined size

Section 10.10: Iterating through an array using pointers

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

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

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

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

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

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

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

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

61

Chapter 11: Linked lists

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

71

Section 11.1: A doubly linked list

Section 11.2: Reversing a linked list

Section 11.3: Inserting a node at the nth position

Section 11.4: Inserting a node at the beginning of a singly linked list

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

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

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

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

76

Chapter 12: Enumerations

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

79

Section 12.1: Simple Enumeration

Section 12.2: enumeration constant without typename

Section 12.3: Enumeration with duplicate value

Section 12.4: Typedef enum

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

79

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

80

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

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

Chapter 13: Structs

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

83

Section 13.1: Flexible Array Members

Section 13.2: Typedef Structs

Section 13.3: Pointers to structs

Section 13.4: Passing structs to functions

Section 13.5: Object-based programming using structs

Section 13.6: Simple data structures

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

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

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

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

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

91

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

89

Chapter 14: Standard Math

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

93

Publicité

Section 14.1: Power functions - pow(), powf(), powl()

Section 14.2: Double precision floating-point remainder: fmod()

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

93

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

94

44

45

47

48

50

50

51

55

55

56

57

58

62

63

64

67

67

68

68

69

71

73

75

80

81

83

85

86

88

Section 14.3: Single precision and long double precision floating-point remainder: fmodf(), fmodl()

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

94

Chapter 15: Iteration Statements/Loops: for, while, do-while

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

96

Section 15.1: For loop

Section 15.2: Loop Unrolling and Du's Device

Section 15.3: While loop

Section 15.4: Do-While loop

Section 15.5: Structure and flow of control in a for loop

Section 15.6: Infinite Loops

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

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

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

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

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

99

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

98

Chapter 16: Selection Statements

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

100

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

Section 16.1: if () Statements

Section 16.2: Nested if()...else VS if()..else Ladder

Section 16.3: switch () Statements

Section 16.4: if () ... else statements and syntax

Section 16.5: if()...else Ladder Chaining two or more if () ... else statements

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

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

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

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

104

Chapter 17: Initialization

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

105

Section 17.1: Initialization of Variables in C

Section 17.2: Using designated initializers

Section 17.3: Initializing structures and arrays of structures

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

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

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

108

Chapter 18: Declaration vs Definition

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

110

Section 18.1: Understanding Declaration and Definition

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

110

Chapter 19: Command-line arguments

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

111

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

111

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

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

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

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

Section 19.1: Print the arguments to a program and convert to integer values

Section 19.2: Printing the command line arguments

Section 19.3: Using GNU getopt tools

Chapter 20: Files and I/O streams

Section 20.1: Open and write to file

Section 20.2: Run process

Section 20.3: fprintf

Section 20.4: Get lines from a file using getline()

Section 20.5: fscanf()

Section 20.6: Read lines from a file

Section 20.7: Open and write to a binary file

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

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

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

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

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

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

Chapter 21: Formatted Input/Output

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

124

Section 21.1: Conversion Specifiers for printing

Section 21.2: The printf() Function

Section 21.3: Printing format flags

Section 21.4: Printing the Value of a Pointer to an Object

Section 21.5: Printing the Dierence of the Values of two Pointers to an Object

Section 21.6: Length modifiers

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

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

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

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

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

128

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

127

Chapter 22: Pointers

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

129

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

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

Publicité

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

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

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

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

Section 22.1: Introduction

Section 22.2: Common errors

Section 22.3: Dereferencing a Pointer

Section 22.4: Dereferencing a Pointer to a struct

Section 22.5: Const Pointers

Section 22.6: Function pointers

Section 22.7: Polymorphic behaviour with void pointers

Section 22.8: Address-of Operator ( & )

Section 22.9: Initializing Pointers

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

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

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

139

96

96

97

97

100

100

102

104

105

106

111

112

115

115

116

116

116

120

121

122

124

125

125

126

129

131

134

134

135

138

140

140

144

144

147

149

149

150

152

152

153

153

156

162

Section 22.10: Pointer to Pointer

Section 22.11: void* pointers as arguments and return values to standard functions

Section 22.12: Same Asterisk, Dierent Meanings

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

141

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

142

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

141

Chapter 23: Sequence points

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

144

Section 23.1: Unsequenced expressions

Section 23.2: Sequenced expressions

Section 23.3: Indeterminately sequenced expressions

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

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

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

145

Chapter 24: Function Pointers

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

146

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

146

Section 24.1: Introduction

Section 24.2: Returning Function Pointers from a Function

Section 24.3: Best Practices

Section 24.4: Assigning a Function Pointer

Section 24.5: Mnemonic for writing function pointers

Section 24.6: Basics

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

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

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

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

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

146

Chapter 25: Function Parameters

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

152

Section 25.1: Parameters are passed by value

Section 25.2: Passing in Arrays to Functions

Section 25.3: Order of function parameter execution

Section 25.4: Using pointer parameters to return multiple values

Section 25.5: Example of function returning struct containing values with error codes

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

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

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

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

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

154

Chapter 26: Pass 2D-arrays to functions

Section 26.1: Pass a 2D-array to a function

Section 26.2: Using flat arrays as 2D arrays

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

156

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

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

Chapter 27: Error handling

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

163

Section 27.1: errno

Section 27.2: strerror

Section 27.3: perror

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

163

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

163

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

163

Chapter 28: Undefined behavior

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

165

Publicité

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

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

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

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

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

Section 28.1: Dereferencing a pointer to variable beyond its lifetime

Section 28.2: Copying overlapping memory

Section 28.3: Signed integer overflow

Section 28.4: Use of an uninitialized variable

Section 28.5: Data race

Section 28.6: Read value of pointer that was freed

Section 28.7: Using incorrect format specifier in printf

Section 28.8: Modify string literal

Section 28.9: Passing a null pointer to printf %s conversion

Section 28.10: Modifying any object more than once between two sequence points

Section 28.11: Freeing memory twice

Section 28.12: Bit shifting using negative counts or beyond the width of the type

Section 28.13: Returning from a function that's declared with _Noreturn or noreturn function specifier

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

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

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

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

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

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

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

165

165

166

167

168

169

170

170

170

171

172

172

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

173

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

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

Section 28.14: Accessing memory beyond allocated chunk

Section 28.15: Modifying a const variable using a pointer

Section 28.16: Reading an uninitialized object that is not backed by memory

Section 28.17: Addition or subtraction of pointer not properly bounded

Section 28.18: Dereferencing a null pointer

Section 28.19: Using ush on an input stream

Section 28.20: Inconsistent linkage of identifiers

Section 28.21: Missing return statement in value returning function

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

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

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

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

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

175

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

174

174

175

175

176

176

177

Section 28.22: Division by zero

Section 28.23: Conversion between pointer types produces incorrectly aligned result

Section 28.24: Modifying the string returned by getenv, strerror, and setlocale functions

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

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

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

179

177

178

Chapter 29: Random Number Generation

Section 29.1: Basic Random Number Generation

Section 29.2: Permuted Congruential Generator

Section 29.3: Xorshift Generation

Section 29.4: Restrict generation to a given range

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

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

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

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

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

182

Chapter 30: Preprocessor and Macros

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

183

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

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

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

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

Section 30.1: Header Include Guards

Section 30.2: #if 0 to block out code sections

Section 30.3: Function-like macros

Section 30.4: Source file inclusion

Section 30.5: Conditional inclusion and conditional function signature modification

Section 30.6: __cplusplus for using C externals in C++ code compiled with C++ - name mangling

Section 30.7: Token pasting

Section 30.8: Predefined Macros

Section 30.9: Variadic arguments macro

Section 30.10: Macro Replacement

Section 30.11: Error directive

Section 30.12: FOREACH implementation

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

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

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

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

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

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

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

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

190

Chapter 31: Signal handling

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

199

Section 31.1: Signal Handling with “signal()”

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

199

Chapter 32: Variable arguments

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

201

Section 32.1: Using an explicit count argument to determine the length of the va_list

Section 32.2: Using terminator values to determine the end of va_list

Section 32.3: Implementing functions with a printf()-like interface

Section 32.4: Using a format string

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

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

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

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

201

Chapter 33: Assertion

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

207

Section...