c-intro-and-ref
c-intro-and-ref
Edition 0.1
Richard Stallman
and
Trevis Rothwell
plus Nelson Beebe
on floating point
This is Edition 0.1.
Copyright c 2022, 2023, 2025 Richard Stallman and Free Software Foundation, Inc.
(The work of Trevis Rothwell and Nelson Beebe has been assigned to the FSF.)
Permission is granted to copy, distribute and/or modify this document under
the terms of the GNU Free Documentation License, Version 1.3 or any later
version published by the Free Software Foundation; with the Invariant Sections
being “GNU General Public License,” with the Front-Cover Texts being “A
GNU Manual,” and with the Back-Cover Texts as in (a) below. A copy of the
license is included in the section entitled “GNU Free Documentation License.”
(a) The FSF’s Back-Cover Text is: “You have the freedom to copy and modify
this GNU manual.”
i
Short Contents
1 The First Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 A Complete Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Storage and Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4 Beyond Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5 Lexical Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
6 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
7 Assignment Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
8 Execution Control Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 34
9 Binary Operator Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
10 Order of Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
11 Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
12 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
13 Type Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
14 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
15 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
16 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
17 Enumeration Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
18 Defining Typedef Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
19 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
20 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
21 Type Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
22 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
23 Compatible Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
24 Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
25 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
26 Preprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
27 Integers in Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
28 Floating Point in Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
29 Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
30 Directing Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
A Type Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
B Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
C Digraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
ii
Table of Contents
2 A Complete Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1 Complete Program Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Complete Program Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Complete Program, Line by Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4 Compiling the Example Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4 Beyond Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.1 An Example with Non-Integer Numbers. . . . . . . . . . . . . . . . . . . . . . . . 11
4.2 An Example with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.3 Calling the Array Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.4 Variations for Array Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5 Lexical Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.1 Write Programs in English! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.2 Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.3 Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.4 Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.5 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.6 Operators and Punctuation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.7 Line Continuation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.1 Basic Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.2 Integer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.3 Integer Overflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
6.3.1 Overflow with Unsigned Integers . . . . . . . . . . . . . . . . . . . . . . . . . . 21
6.3.2 Overflow with Signed Integers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6.4 Mixed-Mode Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6.5 Division and Remainder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6.6 Numeric Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
6.7 Shift Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
6.7.1 Shifting Makes New Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
iv
7 Assignment Expressions . . . . . . . . . . . . . . . . . . . . . . . 28
7.1 Simple Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
7.2 Lvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
7.3 Modifying Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
7.4 Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
7.5 Postincrement and Postdecrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
7.6 Pitfall: Assignment in Subexpressions . . . . . . . . . . . . . . . . . . . . . . . . . . 32
7.7 Write Assignments in Separate Statements . . . . . . . . . . . . . . . . . . . . . 33
10 Order of Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
10.1 Reordering of Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
10.2 Associativity and Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
10.3 Sequence Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
10.4 Postincrement and Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
10.5 Ordering of Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
10.6 Optimization and Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
12 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
12.1 Integer Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
12.2 Integer Constant Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
12.3 Floating-Point Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
12.4 Imaginary Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
12.5 Invalid Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
12.6 Character Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
12.7 String Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
12.8 UTF-8 String Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
12.9 Unicode Character Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
12.10 Wide Character Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
12.11 Wide String Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
13 Type Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
14 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
14.1 Address of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
14.2 Pointer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
14.3 Pointer-Variable Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
14.4 Pointer-Type Designators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
14.5 Dereferencing Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
14.6 Null Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
14.7 Dereferencing Null or Invalid Pointers . . . . . . . . . . . . . . . . . . . . . . . . 63
14.8 Void Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
14.9 Pointer Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
14.10 Pointer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
14.11 Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
14.12 Pointer Arithmetic at Low-Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
14.13 Pointer Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
14.14 Drawbacks of Pointer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
14.15 Pointer-Integer Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
14.16 Printing Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
15 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
15.1 Referencing Structure Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
15.2 Arrays as Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
15.3 Dynamic Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
15.4 Field Offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
15.5 Structure Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
15.6 Packed Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
15.7 Bit Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
15.8 Bit Field Packing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
15.9 const Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
vi
16 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
16.1 Accessing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
16.2 Declaring an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
16.3 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
16.4 Array Type Designators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
16.5 Incomplete Array Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
16.6 Limitations of C Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
16.7 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
16.8 Constructing Array Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
16.9 Arrays of Variable Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
17 Enumeration Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
19 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
19.1 Expression Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
19.2 if Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
19.3 if-else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
19.4 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
19.5 return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
19.6 Loop Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
19.6.1 while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
19.6.2 do-while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
19.6.3 break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
19.6.4 for Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
19.6.5 Example of for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
19.6.6 Omitted for-Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
19.6.7 for-Index Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
19.6.8 continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
19.7 switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
19.8 Example of switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
19.9 Duff’s Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
vii
20 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
20.1 Variable Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
20.1.1 Declaring Arrays and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . 118
20.1.2 Combining Variable Declarations . . . . . . . . . . . . . . . . . . . . . . . 119
20.2 Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
20.3 Designated Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
20.4 Referring to a Type with __auto_type . . . . . . . . . . . . . . . . . . . . . . 122
20.5 Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
20.6 File-Scope Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
20.7 Static Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
20.8 extern Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
20.9 Allocating File-Scope Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
20.10 auto and register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
20.11 Omitting Types in Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
22 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
22.1 Function Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
22.1.1 Function Parameter Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 130
22.1.2 Forward Function Declarations . . . . . . . . . . . . . . . . . . . . . . . . . 131
22.1.3 Static Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
22.1.4 Arrays as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
22.1.4.1 Array parameters are pointers . . . . . . . . . . . . . . . . . . . . . 132
22.1.4.2 Passing array arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
22.1.4.3 Type qualifiers on array parameters . . . . . . . . . . . . . . . . 134
22.1.5 Functions That Accept Structure Arguments . . . . . . . . . . . . 135
22.2 Function Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
22.3 Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
22.4 Function Call Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
22.5 Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
22.5.1 Declaring Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
22.5.2 Assigning Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
22.5.3 Calling Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
viii
25 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
26 Preprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
26.1 Preprocessing Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
26.2 Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
26.3 Preprocessing Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
26.4 Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
26.4.1 #include Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
26.4.2 #include Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
26.4.3 Search Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
26.4.4 Once-Only Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
26.4.5 Computed Includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
26.5 Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
26.5.1 Object-like Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
26.5.2 Function-like Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
26.5.3 Macro Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
26.5.4 Stringification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
26.5.5 Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
26.5.6 Variadic Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
26.5.7 Predefined Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
26.5.8 Undefining and Redefining Macros . . . . . . . . . . . . . . . . . . . . . . 176
26.5.9 Directives Within Macro Arguments . . . . . . . . . . . . . . . . . . . . 176
26.5.10 Macro Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
26.5.10.1 Misnesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
ix
29 Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
x
• Two kinds of statements, the return statement and the if. . . else statement. See
Chapter 19 [Statements], page 101.
• Recursion. The function fib calls itself; that is called a recursive call. These are valid
in C, and quite common.
The fib function would not be useful if it didn’t return. Thus, recursive definitions,
to be of any use, must avoid infinite recursion.
This function definition prevents infinite recursion by specially handling the case where
n is two or less. Thus the maximum depth of recursive calls is less than n.
if-true-statement
else
if-false-statement
Its meaning is to compute the expression condition and, if it’s “true,” execute if-
true-statement. Otherwise, execute if-false-statement. See Section 19.3 [if-else
Statement], page 102.
Inside the if. . . else statement, condition is simply an expression. It’s consid-
ered “true” if its value is nonzero. (A comparison operation, such as n <= 2,
produces the value 1 if it’s “true” and 0 if it’s “false.” See Section 6.6 [Numeric
Comparisons], page 24.) Thus,
if (n <= 2)
return 1;
else
return fib (n - 1) + fib (n - 2);
first tests whether the value of n is less than or equal to 2. If so, the expression
n <= 2 has the value 1. So execution continues with the statement
return 1;
Otherwise, execution continues with this statement:
return fib (n - 1) + fib (n - 2);
Each of these statements ends the execution of the function and provides a
value for it to return. See Section 19.5 [return Statement], page 103.
Calculating fib using ordinary integers in C works only for n < 47 because the value of
fib (47) is too large to fit in type int. In GNU C, type int holds 32 bits (see Section 11.1
[Integer Types], page 44), so the addition operation that tries to add fib (46) and fib
(45) cannot deliver the correct result. This occurrence is called integer overflow.
Overflow can manifest itself in various ways, but one thing that can’t possibly happen is
to produce the correct value, since that can’t fit in the space for the value. See Section 6.3
[Integer Overflow], page 21, for more details about this situation.
See Chapter 22 [Functions], page 130, for a full explanation about functions.
return last;
}
This definition computes fib (n) in a time proportional to n. The comments in the
definition explain how it works: it advances through the series, always keeps the last two
values in last and prev, and adds them to get the next value.
Here are the additional C features that this definition uses:
Internal blocks
Within a function, wherever a statement is called for, you can write a block. It
looks like { . . . } and contains zero or more statements and declarations. (You
can also use additional blocks as statements in a block.)
The function body also counts as a block, which is why it can contain statements
and declarations.
See Section 19.4 [Blocks], page 102.
Declarations of local variables
This function body contains declarations as well as statements. There are three
declarations directly in the function body, as well as a fourth declaration in an
internal block. Each starts with int because it declares a variable whose type
is integer. One declaration can declare several variables, but each of these
declarations is simple and declares just one variable.
Variables declared inside a block (either a function body or an internal block)
are local variables. These variables exist only within that block; their names are
not defined outside the block, and exiting the block deallocates their storage.
This example declares four local variables: last, prev, i, and next.
The most basic local variable declaration looks like this:
type variablename;
For instance,
int i;
declares the local variable i as an integer. See Section 20.1 [Variable Declara-
tions], page 118.
Initializers When you declare a variable, you can also specify its initial value, like this:
type variablename = value;
For instance,
int last = 1;
declares the local variable last as an integer (type int) and starts it off with
the value 1. See Section 20.2 [Initializers], page 119.
Assignment
Assignment: a specific kind of expression, written with the ‘=’ operator, that
stores a new value in a variable or other place. Thus,
variable = value
Chapter 1: The First Example 6
is an expression that computes value and stores the value in variable. See
Chapter 7 [Assignment Expressions], page 28.
Expression statements
An expression statement is an expression followed by a semicolon. That com-
putes the value of the expression, then ignores the value.
An expression statement is useful when the expression changes some data or
has other side effects—for instance, with function calls, or with assignments as
in this example. See Section 19.1 [Expression Statement], page 101.
Using an expression with no side effects in an expression statement is pointless;
for instance, the expression statement x; would examine the value of x and
ignore it. That is not useful.1
Increment operator
The increment operator is ‘++’. ++i is an expression that is short for i = i + 1.
See Section 7.4 [Increment/Decrement], page 30.
for statements
A for statement is a clean way of executing a statement repeatedly—a loop
(see Section 19.6 [Loop Statements], page 103). Specifically,
for (i = 1; i < n; ++i)
body
means to start by doing i = 1 (set i to one) to prepare for the loop. The loop
itself consists of
• Testing i < n and exiting the loop if that’s false.
• Executing body.
• Advancing the loop (executing ++i, which increments i).
The net result is to execute body with 1 in i, then with 2 in i, and so on,
stopping just before the repetition where i would equal n. If n is less than 1,
the loop will execute the body zero times.
The body of the for statement must be one and only one statement. You can’t
write two statements in a row there; if you try to, only the first of them will be
treated as part of the loop.
The way to put multiple statements in such a place is to group them with a
block, and that’s what we do in this example.
1
Computing an expression and ignoring the result can be useful in peculiar cases. For instance, dereferenc-
ing a pointer and ignoring the value is a way to cause a fault if a pointer value is invalid. See Appendix E
[Signals], page 222. But you may need to declare the pointer target volatile or the dereference may be
optimized away. See Section 21.2 [volatile], page 127.
7
2 A Complete Program
It’s all very well to write a Fibonacci function, but you cannot run it by itself. It is a useful
program, but it is not a complete program.
In this chapter we present a complete program that contains the fib function. This ex-
ample shows how to make the program start, how to make it finish, how to do computation,
and how to print a result.
int
fib (int n)
{
if (n <= 2) /* This avoids infinite recursion. */
return 1;
else
return fib (n - 1) + fib (n - 2);
}
int
main (void)
{
printf ("Fibonacci series item %d is %d\n",
20, fib (20));
return 0;
}
This program prints a message that shows the value of fib (20).
Now for an explanation of what that code means.
(void as a function’s parameter list normally means “call with no arguments,” but main is
a special case.)
The function main returns 0 because that is the conventional way for main to indicate
successful execution. It could instead return a positive integer to indicate failure, and some
utility programs have specific conventions for the meaning of certain numeric failure codes.
See Section 22.6.1 [Values from main], page 140.
The simplest way to print text in C is by calling the printf function, so here we explain
very briefly what that function does. For a full explanation of printf and the other standard
I/O functions, see Section “Input/Output on Streams” in The GNU C Library Reference
Manual.
The first argument to printf is a string constant (see Section 12.7 [String Constants],
page 54) that is a template for output. The function printf copies most of that string
directly as output, including the newline character at the end of the string, which is written
as ‘\n’. The output goes to the program’s standard output destination, which in the usual
case is the terminal.
‘%’ in the template introduces a code that substitutes other text into the output. Specif-
ically, ‘%d’ means to take the next argument to printf and substitute it into the text as
a decimal number. (The argument for ‘%d’ must be of type int; if it isn’t, printf will
malfunction.) So the output is a line that looks like this:
Fibonacci series item 20 is 6765
This program does not contain a definition for printf because it is defined by the C
library, which makes it available in all C programs. However, each program does need to
declare printf so it will be called correctly. The #include line takes care of that; it includes
a header file called stdio.h into the program’s code. That file is provided by the operating
system and it contains declarations for the many standard input/output functions in the C
library, one of which is printf.
Don’t worry about header files for now; we’ll explain them later in Section 26.4 [Header
Files], page 160.
The first argument of printf does not have to be a string constant; it can be any string
(see Section 16.3 [Strings], page 90). However, using a constant is the most common case.
4 Beyond Integers
So far we’ve presented programs that operate on integers. In this chapter we’ll present
examples of handling non-integral numbers and arrays of numbers.
int
main (void)
{
printf ("Average is %f\n",
average_of_three (1.1, 9.8, 3.62));
return 0;
}
From now on we will not present examples of calls to main. Instead we encourage you
to write them for yourself when you want to test executing some code.
Chapter 4: Beyond Integers 12
nums_to_average[0] = 58.7;
nums_to_average[1] = 5.1;
nums_to_average[2] = 7.7;
nums_to_average[3] = 105.2;
nums_to_average[4] = -3.14159;
However, while you can combine them, that doesn’t mean you should. If it is useful
to write comments about the variables, and usually it is, then it’s clearer to keep the
declarations separate so you can put a comment on each one. That also helps with using
textual tools to find occurrences of a variable in source files.
We set all of the elements of the array nums_to_average with assignments, but it is
more convenient to use an initializer in the declaration:
{
/* The array of values to average. */
double nums_to_average[]
= { 58.7, 5.1, 7.7, 105.2, -3.14159 };
5 Lexical Syntax
To start the full description of the C language, we explain the lexical syntax and lexical units
of C code. The lexical units of a programming language are known as tokens. This chapter
covers all the tokens of C except for constants, which are covered in a later chapter (see
Chapter 12 [Constants], page 50). One vital kind of token is the identifier (see Section 5.5
[Identifiers], page 17), which is used for names of any kind.
5.2 Characters
GNU C source files are usually written in the ASCII character set, which was defined in the
1960s for English. However, they can also include Unicode characters represented in the
UTF-8 multibyte encoding. This makes it possible to represent accented letters such as ‘á’,
as well as other scripts such as Arabic, Chinese, Cyrillic, Hebrew, Japanese, and Korean.1
In C source code, non-ASCII characters are valid in comments, in wide character con-
stants (see Section 12.10 [Wide Character Constants], page 56), and in string constants (see
Section 12.7 [String Constants], page 54).
Another way to specify non-ASCII characters in constants (character or string) and iden-
tifiers is with an escape sequence starting with backslash, specifying the intended Unicode
1
On some obscure systems, GNU C uses UTF-EBCDIC instead of UTF-8, but that is not worth describing
in this manual.
Chapter 5: Lexical Syntax 16
character. (See Section 12.9 [Unicode Character Codes], page 55.) This specifies non-ASCII
characters without putting a real non-ASCII character in the source file itself.
C accepts two-character aliases called digraphs for certain characters. See Appendix C
[Digraphs], page 219.
5.3 Whitespace
Whitespace means characters that exist in a file but appear blank in a printed listing of
a file (or traditionally did appear blank, several decades ago). The C language requires
whitespace in order to separate two consecutive identifiers, or to separate an identifier
from a numeric constant. Other than that, and a few special situations described later,
whitespace is optional; you can put it in when you wish, to make the code easier to read.
Space and tab in C code are treated as whitespace characters. So are line breaks. You
can represent a line break with the newline character (also called linefeed or LF), CR
(carriage return), or the CRLF sequence (two characters: carriage return followed by a
newline character).
The formfeed character, Control-L, was traditionally used to divide a file into pages. It
is still used this way in source code, and the tools that generate nice printouts of source code
still start a new page after each “formfeed” character. Dividing code into pages separated
by formfeed characters is a good way to break it up into comprehensible pieces and show
other programmers where they start and end.
The vertical tab character, Control-K, was traditionally used to make printing advance
down to the next section of a page. We know of no particular reason to use it in source
code, but it is still accepted as whitespace in C.
Comments are also syntactically equivalent to whitespace.
5.4 Comments
A comment encapsulates text that has no effect on the program’s execution or meaning.
The purpose of comments is to explain the code to people that read it. Writing good
comments for your code is tremendously important—they should provide background in-
formation that helps programmers understand the reasons why the code is written the way
it is. You, returning to the code six months from now, will need the help of these comments
to remember why you wrote it this way.
Outdated comments that become incorrect are counterproductive, so part of the software
developer’s responsibility is to update comments as needed to correspond with changes to
the program code.
C allows two kinds of comment syntax, the traditional style and the C++ style. A
traditional C comment starts with ‘/*’ and ends with ‘*/’. For instance,
/* This is a comment in traditional C syntax. */
A traditional comment can contain ‘/*’, but these delimiters do not nest as pairs. The
first ‘*/’ ends the comment regardless of whether it contains ‘/*’ sequences.
/* This /* is a comment */ But this is not! */
A line comment starts with ‘//’ and ends at the end of the line. For instance,
// This is a comment in C++ style.
Chapter 5: Lexical Syntax 17
Line comments do nest, in effect, because ‘//’ inside a line comment is part of that
comment:
// this whole line is // one comment
This is code, not comment.
It is safe to put line comments inside block comments, or vice versa.
/* traditional comment
// contains line comment
more traditional comment
*/ text here is not a comment
5.5 Identifiers
An identifier (name) in C is a sequence of letters and digits, as well as ‘_’, that does not
start with a digit. Most C compilers also allow ‘$’; GNU C allows it. An identifier can be
as long as you like; for example,
int anti_dis_establishment_arian_ism;
Letters in identifiers are case-sensitive in C; thus, a and A are two different identifiers.
Identifiers in C are used as variable names, function names, typedef names, enumeration
constants, type tags, field names, and labels. Certain identifiers in C are keywords, which
means they have specific syntactic meanings. Keywords in C are reserved words, meaning
you cannot use them in any other way. For instance, you can’t define a variable or function
named return or if.
You can also include other characters, even non-ASCII characters, in identifiers by writ-
ing their Unicode character names, which start with ‘\u’ or ‘\U’, in the identifier name. See
Section 12.9 [Unicode Character Codes], page 55. However, it is usually a bad idea to use
non-ASCII characters in identifiers, and when the names are written in English, they never
need non-ASCII characters. See Section 5.1 [English], page 15.
Chapter 5: Lexical Syntax 18
6 Arithmetic
Arithmetic operators in C attempt to be as similar as possible to the abstract arithmetic
operations, but it is impossible to do this perfectly. Numbers in a computer have a finite
range of possible values, and non-integer values have a limit on their possible accuracy.
Nonetheless, except when results are out of range, you will encounter no surprises in using
‘+’ for addition, ‘-’ for subtraction, and ‘*’ for multiplication.
Each C operator has a precedence, which is its rank in the grammatical order of the
various operators. The operators with the highest precedence grab adjoining operands first;
these expressions then become operands for operators of lower precedence. We give some
information about precedence of operators in this chapter where we describe the operators;
for the full explanation, see Chapter 9 [Binary Operator Grammar], page 39.
The arithmetic operators always promote their operands before operating on them. This
means converting narrow integer data types to a wider data type (see Section 24.4 [Operand
Promotions], page 155). If you are just learning C, don’t worry about this yet.
Given two operands that have different types, most arithmetic operations convert them
both to their common type. For instance, if one is int and the other is double, the common
type is double. (That’s because double can represent all the values that an int can hold,
but not vice versa.) For the full details, see Section 24.5 [Common Type], page 155.
Each integer data type in C is either signed or unsigned. A signed type can hold a range
of positive and negative numbers, with zero near the middle of the range. An unsigned type
can hold only nonnegative numbers; its range starts with zero and runs upward.
The most basic integer types are int, which normally can hold numbers from
−2,147,483,648 to 2,147,483,647, and unsigned int, which normally can hold numbers
from 0 to 4,294,967,295. (This assumes int is 32 bits wide, always true for GNU C on real
computers but not always on embedded controllers.) See Section 11.1 [Integer Types],
page 44, for full information about integer types.
When a basic arithmetic operation is given two signed operands, it does signed arith-
metic. Given two unsigned operands, it does unsigned arithmetic.
If one operand is unsigned int and the other is int, the operator treats them both
as unsigned. More generally, the common type of the operands determines whether the
operation is signed or not. See Section 24.5 [Common Type], page 155.
Printing the results of unsigned arithmetic with printf using ‘%d’ can produce surprising
results for values far away from zero. Even though the rules above say that the computation
was done with unsigned arithmetic, the printed result may appear to be signed!
The explanation is that the bit pattern resulting from addition, subtraction or multipli-
cation is actually the same for signed and unsigned operations. The difference is only in
the data type of the result, which affects the interpretation of the result bit pattern, and
whether the arithmetic operation can overflow (see the next section).
But ‘%d’ doesn’t know its argument’s data type. It sees only the value’s bit pattern, and
it is defined to interpret that as signed int. To print it as unsigned requires using ‘%u’
instead of ‘%d’. See Section “Formatted Output” in The GNU C Library Reference Manual.
Arithmetic in C never operates directly on narrow integer types (those with fewer bits
than int; Section 11.1.3 [Narrow Integers], page 45). Instead it “promotes” them to int.
See Section 24.4 [Operand Promotions], page 155.
unsigned int x = 1;
unsigned int y;
y = -x;
causes overflow because the negative number −1 can’t be stored in an unsigned type. The
actual result, which is −1 modulo the nth power of 2, is one less than the nth power of 2.
That is the largest value that the unsigned data type can store. For a 32-bit unsigned int,
the value is 4,294,967,295. See Section 27.2 [Maximum and Minimum Values], page 190.
Adding that number to itself, as here,
unsigned int z;
z = y + y;
ought to yield 8,489,934,590; however, that is again too large to fit, so overflow truncates
the value to 4,294,967,294. If that were a signed integer, it would mean −2, which (not by
coincidence) equals −1 + −1.
16 / 3.0 ⇒ 5.333333333333333
16.0 / 3.0 ⇒ 5.333333333333333
16 / 3 ⇒ 5
The remainder operator ‘%’ is not allowed for floating-point operands, because it is not
needed. The concept of remainder makes sense for integers because the result of division
of integers has to be an integer. For floating point, the result of division is a floating-point
number, in other words a fraction, which will differ from the exact result only by a very
small amount.
There are functions in the standard C library to calculate remainders from integral-
values division of floating-point numbers. See Section “Remainder Functions” in The GNU
C Library Reference Manual.
Integer division overflows in one specific case: dividing the smallest negative value for
the data type (see Section 27.2 [Maximum and Minimum Values], page 190) by −1. That’s
because the correct result, which is the corresponding positive number, does not fit (see
Section 6.3 [Integer Overflow], page 21) in the same number of bits. On some computers
now in use, this always causes a signal SIGFPE (see Appendix E [Signals], page 222), the same
behavior that the option -ftrapv specifies (see Section 6.3.2 [Signed Overflow], page 22).
Division by zero leads to unpredictable results—depending on the type of computer, it
might cause a signal SIGFPE, or it might produce a numeric result.
Watch out: Make sure the program does not divide by zero. If you can’t prove that the
divisor is not zero, test whether it is zero, and skip the division if so.
5 << 2 ⇒ 20
/* Right shift. */
5 >> 2 ⇒ 1
The left operand is the value to be shifted, and the right operand says how many bits to shift
it (the shift count). The left operand is promoted (see Section 24.4 [Operand Promotions],
page 155), so shifting never operates on a narrow integer type; it’s always either int or
wider. The result of the shift operation has the same type as the promoted left operand.
The examples in this section use binary constants, starting with ‘0b’ (see Section 12.1
[Integer Constants], page 50). They stand for 32-bit integers of type int.
Note: according to the C standard, shifting of signed values isn’t guaranteed to work
properly when the value shifted is negative, or becomes negative during shifting. However,
only pedants have a reason to be concerned about this; only computers with strange shift
instructions could plausibly do this wrong. In GNU C, the operation always works as
expected.
~ (-1) ⇒ 0
It is useful to remember that ~x + 1 equals -x, for integers, and ~x equals -x -
1. The last example above shows this with −1 as x.
a&b Binary operator for bitwise “and” or “conjunction.” Each bit in the result is 1
if that bit is 1 in both a and b.
0b10101010 & 0b11001100 ⇒ 0b10001000
a|b Binary operator for bitwise “or” (“inclusive or” or “disjunction”). Each bit in
the result is 1 if that bit is 1 in either a or b.
0b10101010 | 0b11001100 ⇒ 0b11101110
a^b Binary operator for bitwise “xor” (“exclusive or”). Each bit in the result is 1
if that bit is 1 in exactly one of a and b.
0b10101010 ^ 0b11001100 ⇒ 0b01100110
To understand the effect of these operators on signed integers, keep in mind that all
modern computers use two’s-complement representation (see Section 27.1 [Integer Repre-
sentations], page 189) for negative integers. This means that the highest bit of the number
indicates the sign; it is 1 for a negative number and 0 for a positive number. In a negative
number, the value in the other bits increases as the number gets closer to zero, so that
0b111. . . 111 is −1 and 0b100. . . 000 is the most negative possible integer.
Warning: C defines a precedence ordering for the bitwise binary operators, but you
should never rely on it. Likewise, you should never rely on how bitwise binary operators
relate in precedence to the arithmetic and shift binary operators. Other programmers don’t
remember these aspects of C’s precedence ordering; to make your programs clear, always
use parentheses to explicitly specify the nesting among these operators.
For example, suppose offset is an integer that specifies the offset within shared memory
of a table, except that its bottom few bits (LOWBITS says how many) are special flags. Here’s
how to get just that offset and add it to the base address.
shared_mem_base + (offset & (-1 << LOWBITS))
Thanks to the outer set of parentheses, we don’t need to know whether ‘&’ has higher
precedence than ‘+’. Thanks to the inner set, we don’t need to know whether ‘&’ has higher
precedence than ‘<<’. But we can rely on all unary operators to have higher precedence
than any binary operator, so we don’t need parentheses around the left operand of ‘<<’.
28
7 Assignment Expressions
As a general concept in programming, an assignment is a construct that stores a new value
into a place where values can be stored—for instance, in a variable. Such places are called
lvalues (see Section 7.2 [Lvalues], page 29) because they are locations that hold a value.
In C, an assignment is an expression because it has a value; we call it an assignment
expression. A simple assignment looks like
lvalue = value-to-store
We say it assigns the value of the expression value-to-store to the location lvalue, or that it
stores value-to-store there. You can think of the “l” in “lvalue” as standing for “left,” since
that’s what you put on the left side of the assignment operator.
However, that’s not the only way to use an lvalue, and not all lvalues can be assigned
to. To use the lvalue in the left side of an assignment, it has to be modifiable. In C, that
means it was not declared with the type qualifier const (see Section 21.1 [const], page 126).
The value of the assignment expression is that of lvalue after the new value is stored in
it. This means you can use an assignment inside other expressions. Assignment operators
are right-associative so that
x = y = z = 0;
is equivalent to
x = (y = (z = 0));
This is the only useful way for them to associate; the other way,
((x = y) = z) = 0;
would be invalid since an assignment expression such as x = y is not a valid lvalue.
Warning: Write parentheses around an assignment if you nest it inside another expres-
sion, unless that containing expression is a comma-separated series or another assignment.
For example, see Section 8.3 [Logicals and Assignments], page 35, and Section 8.5.1 [Uses
of Comma], page 37.
Simple assignment is also allowed on some non-numeric types: pointers (see Chapter 14
[Pointers], page 60), structures (see Section 15.13 [Structure Assignment], page 80), and
unions (see Section 15.14 [Unions], page 81).
Warning: Assignment is not allowed on arrays because there are no array values in C; C
variables can be arrays, but these arrays cannot be manipulated as wholes. See Section 16.6
[Limitations of C Arrays], page 92.
See Section 24.2 [Assignment Type Conversions], page 153, for the complete rules about
data types used in assignments.
7.2 Lvalues
An expression that identifies a memory space that holds a value is called an lvalue, because
it is a location that can hold a value.
The standard kinds of lvalues are:
• A variable.
• A pointer-dereference expression (see Section 14.5 [Pointer Dereference], page 61) using
unary ‘*’, if its type is not a function type.
• A structure field reference (see Chapter 15 [Structures], page 72) using ‘.’, if the struc-
ture value is an lvalue.
• A structure field reference using ‘->’. This is always an lvalue since ‘->’ implies pointer
dereference.
• A union alternative reference (see Section 15.14 [Unions], page 81), on the same con-
ditions as for structure fields.
• An array-element reference using ‘[. . . ]’, if the array is an lvalue.
• A string constant (see Section 12.7 [String Constants], page 54).
• An array constructor (see Section 16.8 [Constructing Array Values], page 94).
• A structure or union constructor (see Section 15.17 [Structure Constructors], page 84).
If an expression’s outermost operation is any other operator, that expression is not an
lvalue. Thus, the variable x is an lvalue, but x + 0 is not, even though these two expressions
compute the same value (assuming x is a number).
It is rare that a structure value or an array value is not an lvalue, but that does happen—
for instance, the result of a function call or a conditional operator can have a structure or
array type, but is never an lvalue.
If an array is an lvalue, using the array in an expression still converts it automatically
to a pointer to the zeroth element. The result of this conversion is not an lvalue. Thus, if
the variable a is an array, you can’t use a by itself as the left operand of an assignment.
But you can assign to an element of a, such as a[0]. That is an lvalue since a is an lvalue.
int
main (void)
{
int i = 5;
Chapter 7: Assignment Expressions 31
int
main (void)
{
int i = 5;
printf ("%d\n", i);
printf ("%d\n", --i);
printf ("%d\n", i);
return 0;
}
prints three lines that contain (respectively) ‘5’, ‘4’, and again ‘4’.
int
main (void)
{
int i = 5;
printf ("%d\n", i);
printf ("%d\n", i++);
printf ("%d\n", i);
return 0;
}
prints lines containing ‘5’, again ‘5’, and ‘6’. The expression i++ has the value 5, which is
the value of i at the time, but it increments i from 5 to 6 just a little later.
How much later is “just a little later”? The compiler has some flexibility in deciding
that. The rule is that the increment has to happen by the next sequence point; in simple
cases, that means by the end of the statement. See Section 10.3 [Sequence Points], page 42.
Regardless of precisely where the compiled code increments the value of i, the crucial
thing is that the value of i++ is the value that i has before incrementing it.
Chapter 7: Assignment Expressions 32
This is equivalent:
if (r && x % r == 0)
A truth value is simply a number, so using r as a truth value tests whether it is nonzero.
But r’s meaning as an expression is not a truth value—it is a number to divide by. So it is
clearer style to write the explicit != 0.
Here’s another equivalent way to write it:
if (!(r == 0) && x % r == 0)
This illustrates the unary ‘!’ operator, as well as the need to write parentheses around its
operand.
value becomes the value of the conditional expression. Otherwise the conditional expres-
sion computes iffalse and its value becomes the value of the conditional expression. The
conditional expression always computes just one of iftrue and iffalse, never both of them.
Here’s an example: the absolute value of a number x can be written as (x >= 0 ? x :
-x).
Warning: The conditional expression has rather low syntactic precedence. Except when
the conditional expression is used as an argument in a function call, write parentheses
around it. For clarity, always write parentheses around it if it extends across more than one
line.
Warning: Assignment operators and the comma operator (see Section 8.5 [Comma Op-
erator], page 37) have lower precedence than conditional expressions, so write parentheses
around those when they appear inside a conditional expression. See Chapter 10 [Order of
Execution], page 41.
Warning: When nesting a conditional expression within another conditional expression,
unless a pair of matching delimiters surrounds the inner conditional expression for some
other reason, write parentheses around it:
((foo > 0 ? test1 : test2) ? (ifodd (foo) ? 5 : 10)
: (ifodd (whatever) ? 5 : 10));
In the first operand, those parentheses are necessary to prevent incorrect parsing. In the
second and third operands, the computer may not need the parentheses, but they will help
human beings.
once. For example, if we suppose that the function next_element advances a pointer
variable to point to the next element in a list and returns the new pointer,
next_element () ? : default_pointer
is a way to advance the pointer and use its new value if it isn’t null, but use default_
pointer if that is null. We cannot do it this way,
next_element () ? next_element () : default_pointer
because that would advance the pointer a second time.
Warning: Don’t use the comma operator within an argument of a function unless it
makes the code more readable. When you do so, don’t put part of another argument on the
same line. Instead, add a line break to make the parentheses around the comma operator
easier to see, like this.
foo ((mumble (x, y), frob (z)),
*p)
The caveat for bitwise and shift operations is like that for logical operators: you can
let multiple uses of one bitwise operation associate, but always use parentheses to control
nesting of dissimilar operations.
These lists do not specify any precedence ordering between the bitwise and shift opera-
tions of the second list and the binary operations above conditional expressions in the first
list. When they come together, parenthesize them. See Section 6.8 [Bitwise Operations],
page 26.
41
10 Order of Execution
The order of execution of a C program is not always obvious, and not necessarily predictable.
This chapter describes what you can count on.
Write signed or unsigned before the type keyword to specify a signed or an unsigned
type. However, the integer types other than char are signed by default; with them, signed
is a no-op.
Plain char may be signed or unsigned; this depends on the compiler, the machine in
use, and its operating system. It is not the same type as either signed char or unsigned
char, but it is always equivalent to one of those two.
In many programs, it makes no difference whether the type char is signed. When
signedness does matter for a certain value, don’t leave it to chance; declare it as signed
char or unsigned char instead.1
because loss of significant bits is a normal consequence of truncation. Likewise for conversion
between signed and unsigned types of the same width.
More information about conversion for assignment is in Section 24.2 [Assignment Type
Conversions], page 153. For conversion for arithmetic, see Section 24.3 [Argument Promo-
tions], page 154.
as the fraction and 1 as the exponent. The value 0.75 would use 0.75 as the fraction and 0
as the exponent. The value 0.375 would use 0.75 as the fraction and −1 as the exponent.
These binary exponents are used by machine instructions. You can write a floating-point
constant this way if you wish, using hexadecimal; but normally we write floating-point
numbers in decimal (base 10). See Section 12.3 [Floating Constants], page 51.
C has three floating-point data types:
double “Double-precision” floating point, which uses 64 bits. This is the normal
floating-point type, and modern computers normally do their floating-point
computations in this type, or some wider type. Except when there is a special
reason to do otherwise, this is the type to use for floating-point values.
float “Single-precision” floating point, which uses 32 bits. It is useful for floating-
point values stored in structures and arrays, to save space when the full precision
of double is not needed. In addition, single-precision arithmetic is faster on
some computers, and occasionally that is useful. But not often—most programs
don’t use the type float.
C would be cleaner if float were the name of the type we use for most floating-
point values; however, for historical reasons, that’s not so.
long double
“Extended-precision” floating point is either 80-bit or 128-bit precision, depend-
ing on the machine in use. On some machines, which have no floating-point
format wider than double, this is equivalent to double.
Floating-point arithmetic raises many subtle issues. See Chapter 28 [Floating Point in
Depth], page 191, for more information.
2
For compatibility with older versions of GNU C, the keyword __complex__ is also allowed. Going forward,
however, use the new _Complex keyword as defined in ISO C11.
Chapter 11: Primitive Data Types 48
To pull the real and imaginary parts of the number back out, GNU C provides the
keywords __real__ and __imag__:
_Complex double foo = 4.0 + 3.0i;
12 Constants
A constant is an expression that stands for a specific value by explicitly representing the
desired value. C allows constants for numbers, characters, and strings. We have already
seen numeric and string constants in the examples.
4.0 + 3.0i
That gives the value 4 + 3i, with type _Complex double.
Such a sum can include multiple real constants, or none. Likewise, it can include multiple
imaginary constants, or none. For example:
_Complex double foo, bar, quux;
This is useful for writing a string containing multiple lines, like this:
"This message is so long that it needs more than\n"
"a single line of text. C does not allow a newline\n"
"to represent itself in a string constant, so we have to\n"
"write \\n to put it in the string. For readability of\n"
"the source code, it is advisable to put line breaks in\n"
"the source where they occur in the contents of the\n"
"constant.\n"
The sequence of a backslash and a newline is ignored anywhere in a C program, and that
includes inside a string constant. Thus, you can write multi-line string constants this way:
"This is another way to put newlines in a string constant\n\
and break the line after them in the source code."
However, concatenation is the recommended way to do this.
You can also write perverse string constants like this,
"Fo\
o!"
but don’t do that—write it like this instead:
"Foo!"
Be careful to avoid passing a string constant to a function that modifies the string
it receives. The memory where the string constant is stored may be read-only, which
would cause a fatal SIGSEGV signal that normally terminates the function (see Appendix E
[Signals], page 222). Even worse, the memory may not be read-only. Then the function
might modify the string constant, thus spoiling the contents of other string constants that
are supposed to contain the same value and are unified by the compiler.
Use the ‘\u’ escape sequence with a 16-bit hexadecimal Unicode character code. If the
character’s numeric code is too big for 16 bits, use the ‘\U’ escape sequence with a 32-bit
hexadecimal Unicode character code. Here are some examples.
\u6C34 /* 16-bit code (Chinese for ‘‘water”), UTF-16 */
\U0010ABCD /* 32-bit code, UTF-32 */
One way to use these is in UTF-8 string constants (see Section 12.8 [UTF-8 String Con-
stants], page 55). For instance, here we use two of them, each preceded by a space.
u8"fóó \u6C34 \U0010ABCD"
You can also use them in wide character constants (see Section 12.10 [Wide Character
Constants], page 56), like this:
u'\u6C34' /* 16-bit code (water) */
U'\U0010ABCD' /* 32-bit code */
and in wide string constants (see Section 12.11 [Wide String Constants], page 57), like this:
u"\u6C34\u706B" /* 16-bit codes (water, fire) */
U"\U0010ABCD" /* 32-bit code */
And in an identifier:
int foo\u6C34bar = 0;
Codes in the range of D800 through DFFF are invalid in universal character names.
Trying to write them using ‘\u’ causes an error. Unicode calls them “surrogate code points”
and uses them in UTF-16 for purposes too specialized to explain here.
Codes less than 00A0 are likewise invalid in universal character names, and likewise
cause errors, except for 0024 (‘$’), 0040 (‘@’), and 0060 (‘`’). Characters which can’t be
represented with universal character names can be specified with octal or hexadecimal
escape sequences (see Section 12.6 [Character Constants], page 53).
In all three kinds of wide character constants, you can write a non-ASCII Unicode
character in the constant itself; the constant’s value is the character’s Unicode character
code. Or you can specify the Unicode character with an escape sequence (see Section 12.9
[Unicode Character Codes], page 55).
13 Type Size
Each data type has a size, which is the number of bytes (see Chapter 3 [Storage], page 10)
that it occupies in memory. To refer to the size in a C program, use sizeof. There are two
ways to use it:
sizeof expression
This gives the size of expression, based on its data type. It does not calculate
the value of expression, only its size, so if expression includes side effects or
function calls, they do not happen. Therefore, sizeof with an expression as
argument is always a compile-time operation that has zero run-time cost, unless
it applies to a variable-size array.
A value that is a bit field (see Section 15.7 [Bit Fields], page 77) is not allowed
as an operand of sizeof.
For example,
double a;
i = sizeof a + 10;
sets i to 18 on most computers because a occupies 8 bytes.
Here’s how to determine the number of elements in an array arr:
(sizeof arr / sizeof arr[0])
The expression sizeof arr gives the size of the array, not the size of a pointer
to an element. However, if expression is a function parameter that was declared
as an array, that variable really has a pointer type (see Section 22.1.4.1 [Array
Params are Ptrs], page 132), so the result is the size of that pointer.
sizeof (type)
This gives the size of type. For example,
i = sizeof (double) + 10;
is equivalent to the previous example.
Warning: If type contains expressions which have side effects, those expressions
are actually computed and any side effects in them do occur.
You can’t apply sizeof to an incomplete type (see Section 15.19 [Incomplete
Types], page 85). Using it on a function type or void gives 1 in GNU C, which
makes adding an integer to these pointer types work as desired (see Section 14.10
[Pointer Arithmetic], page 65).
Warning: When you use sizeof with a type instead of an expression, you must write
parentheses around the type.
Warning: When applying sizeof to the result of a cast (see Section 24.1 [Explicit Type
Conversion], page 153), you must write parentheses around the cast expression to avoid an
ambiguity in the grammar of C. Specifically,
sizeof (int) -x
parses as
(sizeof (int)) - x
Chapter 13: Type Size 59
14 Pointers
Among high-level languages, C is rather low-level, close to the machine. This is mainly
because it has explicit pointers. A pointer value is the numeric address of data in memory.
The type of data to be found at that address is specified by the data type of the pointer
itself. Nothing in C can determine the “correct” data type of data in memory; it can only
blindly follow the data type of the pointer you use to access the data.
The unary operator ‘*’ gets the data that a pointer points to—this is called dereferencing
the pointer. Its value always has the type that the pointer points to.
C also allows pointers to functions, but since there are some differences in how they
work, we treat them later. See Section 22.5 [Function Pointers], page 138.
ptr = &i;
i = 5;
...
This shows how to declare the variable ptr as type int * (pointer to int), store a pointer
value into it (pointing at i), and use it later to get the value of the object it points at (the
value in i).
Here is another example of using a pointer to a variable.
/* Define global variable i. */
int i = 2;
int
foo (void)
{
/* Save global variable i’s address. */
int *global_i = &i;
Since testing a pointer for not being null is basic and frequent, all but beginners in C
will understand the conditional without need for != NULL:
if (p)
/* p is not null. */
operate (p);
p = numbered_slot_pointer (5);
...
}
Passing an argument of type void * for a parameter that has a pointer type also con-
verts. For example, supposing the function hack is declared to require type float * for its
parameter, this call to hack will convert the argument to that type.
/* Declare hack that way.
We assume it is defined somewhere else. */
void hack (float *);
...
/* Now call hack. */
{
/* Converts return value of numbered_slot_pointer
to float * to pass it to hack. */
hack (numbered_slot_pointer (5));
...
}
You can also convert to another pointer type with an explicit cast (see Section 24.1
[Explicit Type Conversion], page 153), like this:
(int *) numbered_slot_pointer (5)
Here is an example which decides at run time which pointer type to convert to:
void
extract_int_or_double (void *ptr, bool its_an_int)
{
if (its_an_int)
handle_an_int (*(int *)ptr);
else
handle_a_double (*(double *)ptr);
}
The expression *(int *)ptr means to convert ptr to type int *, then dereference it.
p = &i;
q = &i;
if (p == q)
printf ("This will be printed.\n");
if (p != q)
printf ("This won't be printed.\n");
Chapter 14: Pointers 65
}
Ordering comparisons such as > and >= operate on pointers by converting them to
unsigned integers. The C standard says the two pointers must point within the same object
in memory, but on GNU/Linux systems these operations simply compare the numeric values
of the pointers.
The pointer values to be compared should in principle have the same type, but they are
allowed to differ in limited cases. First of all, if the two pointers’ target types are nearly
compatible (see Chapter 23 [Compatible Types], page 152), the comparison is allowed.
If one of the operands is void * (see Section 14.8 [Void Pointers], page 63) and the other
is another pointer type, the comparison operator converts the void * pointer to the other
type so as to compare them. (In standard C, this is not allowed if the other type is a
function pointer type, but it works in GNU C.)
Comparison operators also allow comparing the integer 0 with a pointer value. This
works by converting 0 to a null pointer of the same type as the other operand.
int *p = &array[0];
/* Now p points at element 0. Fetch it. */
elt0 = *p;
++p;
/* Now p points at element 1. Fetch it. */
elt1 = *p;
p += 3;
/* Now p points at element 4 (the last). Fetch it. */
elt4 = *p;
decrementing_pointers ()
{
int array[5] = { 45, 29, 104, -3, 123456 };
int elt0, elt3, elt4;
int *p = &array[4];
/* Now p points at element 4 (the last). Fetch it. */
elt4 = *p;
--p;
/* Now p points at element 3. Fetch it. */
elt3 = *p;
p -= 3;
/* Now p points at element 0. Fetch it. */
elt0 = *p;
If one pointer value was made by adding an integer to another pointer value, it should
be possible to subtract the pointer values and recover that integer. That works too in C.
void
subtract_pointers ()
{
int array[5] = { 45, 29, 104, -3, 123456 };
int *p0, *p3, *p4;
int *p = &array[4];
/* Now p points at element 4 (the last). Save the value. */
p4 = p;
--p;
/* Now p points at element 3. Save the value. */
p3 = p;
p -= 3;
/* Now p points at element 0. Save the value. */
p0 = p;
The addition operation does not know where arrays begin or end in memory. All it does
is add the integer (multiplied by target object size) to the numeric value of the pointer.
When the initial pointer and the result point into the same array, the result is well-defined.
Warning: Only experts should do pointer arithmetic involving pointers into different
memory objects.
The difference between two pointers has type int, or long if necessary (see Section 11.1
[Integer Types], page 44). The clean way to declare it is to use the typedef name ptrdiff_t
defined in the file stddef.h.
C defines pointer subtraction to be consistent with pointer-integer addition, so that (p3
- p1) + p1 equals p3, as in ordinary algebra. Pointer subtraction works by subtracting p1’s
numeric value from p3’s, and dividing by target object size. The two pointer arguments
should point into the same array.
In standard C, addition and subtraction are not allowed on void *, since the target type’s
size is not defined in that case. Likewise, they are not allowed on pointers to function types.
However, these operations work in GNU C, and the “size of the target type” is taken as 1
byte.
void *
ptr_add (void *p, int i, int objsize)
{
intptr_t p_address = (long) p;
intptr_t totalsize = i * objsize;
intptr_t new_address = p_address + totalsize;
return (void *) new_address;
}
This does the same job as p + i with the proper pointer type for p. It uses the type intptr_
t, which is defined in the header file stdint.h. (In practice, long long would always work,
but it is cleaner to use intptr_t.)
for (;;)
{
/* Fetch the next integer. */
int next = *p++;
/* Exit the loop if it’s 0. */
if (next == 0)
break;
/* Add it into running total. */
sum += next;
}
Chapter 14: Pointers 69
return sum;
}
The statement ‘break;’ will be explained further on (see Section 19.6.3 [break Statement],
page 104). Used in this way, it immediately exits the surrounding for statement.
*p++ uses postincrement (++; see Section 7.5 [Postincrement/Postdecrement], page 31)
on the pointer p. That expression parses as *(p++), because a postfix operator always takes
precedence over a prefix operator. Therefore, it dereferences the entering value of p, then
increments p afterwards.
Incrementing a variable means adding 1 to it, as in p = p + 1. Since p is a pointer,
adding 1 to it advances it by the width of the datum it points to—in this case, sizeof
(int). Therefore, each iteration of the loop picks up the next integer from the series and
puts it into next.
This for-loop has no initialization expression since p and sum are already initialized, has
no end-test since the ‘break;’ statement will exit it, and needs no expression to advance it
since that’s done within the loop by incrementing p and sum. Thus, those three expressions
after for are left empty.
Another way to write this function is by keeping the parameter value unchanged and
using indexing to access the integers in the table.
int
sum_array_till_0_indexing (int *p)
{
int i;
int sum = 0;
for (i = 0; ; i++)
{
/* Fetch the next integer. */
int next = p[i];
/* Exit the loop if it’s 0. */
if (next == 0)
break;
/* Add it into running total. */
sum += next;
}
return sum;
}
In this program, instead of advancing p, we advance i and add it to p. (Recall that p[i]
means *(p + i).) Either way, it uses the same address to get the next integer.
It makes no difference in this program whether we write i++ or ++i, because the value
of that expression is not used. We use it for its effect, to increment i.
The ‘--’ operator also works on pointers; it can be used to step backwards through an
array, like this:
int
after_last_nonzero (int *p, int len)
Chapter 14: Pointers 70
{
/* Set up q to point just after the last array element. */
int *q = p + len;
while (q != p)
/* Step q back until it reaches a nonzero element. */
if (*--q != 0)
/* Return the index of the element after that nonzero. */
return q - p + 1;
return 0;
}
That function returns the length of the nonzero part of the array specified by its argu-
ments; that is, the index of the first zero of the run of zeros at the end.
void
print_pointer (void *ptr)
{
uintptr_t converted = (uintptr_t) ptr;
15 Structures
A structure is a user-defined data type that holds various fields of data. Each field has a
name and a data type specified in the structure’s definition. Because a structure combines
various fields, each of its own type, we call a structure type a compound type. (Union types
are also compound types; see Section 15.14 [Unions], page 81.)
Here we define a structure suitable for storing a linked list of integers. Each list item
will hold one integer, plus a pointer to the next item.
struct intlistlink
{
int datum;
struct intlistlink *next;
};
The structure definition has a type tag so that the code can refer to this structure.
The type tag here is intlistlink. The definition refers recursively to the same structure
through that tag.
You can define a structure without a type tag, but then you can’t refer to it again. That
is useful only in some special contexts, such as inside a typedef or a union.
The contents of the structure are specified by the field declarations inside the braces.
Each field in the structure needs a declaration there. The fields in one structure definition
must have distinct names, but these names do not conflict with any other names in the
program.
A field declaration looks just like a variable declaration. You can combine field declara-
tions with the same beginning, just as you can combine variable declarations.
This structure has two fields. One, named datum, has type int and will hold one integer
in the list. The other, named next, is a pointer to another struct intlistlink which
would be the rest of the list. In the last list item, it would be NULL.
This structure definition is recursive, since the type of the next field refers to the
structure type. Such recursion is not a problem; in fact, you can use the type struct
intlistlink * before the definition of the type struct intlistlink itself. That works
because pointers to all kinds of structures really look the same at the machine level.
After defining the structure, you can declare a variable of type struct intlistlink like
this:
struct intlistlink foo;
The structure definition itself can serve as the beginning of a variable declaration, so
you can declare variables immediately after, like this:
struct intlistlink
{
int datum;
struct intlistlink *next;
} foo;
But that is ugly. It is almost always clearer to separate the definition of the structure from
its uses.
Chapter 15: Structures 73
Declaring a structure type inside a block (see Section 19.4 [Blocks], page 102) limits the
scope of the structure type name to that block. That means the structure type is recognized
only within that block. Declaring it in a function parameter list, as here,
int f (struct foo {int a, b} parm);
(assuming that struct foo is not already defined) limits the scope of the structure type
struct foo to that parameter list; that is basically useless, so it triggers a warning.
Standard C requires at least one field in a structure. GNU C does not require this.
. . . foo.l.next->next->datum. . .
to a struct record which contains those things; you can access the second integer in that
record with recptr->data[1].
If you have two objects of type struct record, each one contains an array. With this
declaration,
struct record r1, r2;
r1.data holds space for 4 ints, and r2.data holds space for another 4 ints,
...
struct intlistlink *
alloc_intlistlink ()
{
struct intlistlink *p;
if (p == NULL)
fatal ("Ran out of storage");
return p;
}
malloc returns void *, so the assignment to p will automatically convert it to type struct
intlistlink *. The return value of malloc is always sufficiently aligned (see Appendix A
[Type Alignment], page 215) that it is valid for any data type.
The test for p == NULL is necessary because malloc returns a null pointer if it cannot
get any storage. We assume that the program defines the function fatal to report a fatal
error to the user.
Here’s how to add one more integer to the front of such a list:
struct intlistlink *my_list = NULL;
void
add_to_mylist (int my_int)
{
Chapter 15: Structures 75
p->datum = my_int;
p->next = mylist;
mylist = p;
}
The way to free the objects is by calling free. Here’s a function to free all the links in
one of these lists:
void
free_intlist (struct intlistlink *p)
{
while (p)
{
struct intlistlink *q = p;
p = p->next;
free (q);
}
}
We must extract the next pointer from the object before freeing it, because free can
clobber the data that was in the object. For the same reason, the program must not use
the list any more after freeing its elements. To make sure it won’t, it is best to clear out
the variable where the list was stored, like this:
free_intlist (mylist);
mylist = NULL;
The precise layout of a struct type is crucial when using it to overlay hardware registers,
to access data structures in shared memory, or to assemble and disassemble packets for
network communication. It is also important for avoiding memory waste when the program
makes many objects of that type. However, the layout depends on the target platform.
Each platform has conventions for structure layout, which compilers need to follow.
Here are the conventions used on most platforms.
The structure’s fields appear in the structure layout in the order they are declared.
When possible, consecutive fields occupy consecutive bytes within the structure. However,
if a field’s type demands more alignment than it would get that way, C gives it the alignment
it requires by leaving a gap after the previous field.
Once all the fields have been laid out, it is possible to determine the structure’s alignment
and size. The structure’s alignment is the maximum alignment of any of the fields in it.
Then the structure’s size is rounded up to a multiple of its alignment. That may require
leaving a gap at the end of the structure.
Here are some examples, where we assume that char has size and alignment 1 (always
true), and int has size and alignment 4 (true on most kinds of computers):
struct foo
{
char a, b;
int c;
};
This structure occupies 8 bytes, with an alignment of 4. a is at offset 0, b is at offset 1, and
c is at offset 4. There is a gap of 2 bytes before c.
Contrast that with this structure:
struct foo
{
char a;
int c;
char b;
};
This structure has size 12 and alignment 4. a is at offset 0, c is at offset 4, and b is at
offset 8. There are two gaps: three bytes before c, and three bytes at the end.
These two structures have the same contents at the C level, but one takes 8 bytes and
the other takes 12 bytes due to the ordering of the fields. A reliable way to avoid this sort
of wastage is to order the fields by size, biggest fields first.
char b;
};
Without __attribute__((packed)), this structure occupies 12 bytes (as described
in the previous section), assuming 4-byte alignment for int. With __attribute__
((packed)), it is only 6 bytes long—the sum of the lengths of its fields.
Use of __attribute__((packed)) often results in fields that don’t have the normal
alignment for their types. Taking the address of such a field can result in an invalid pointer
because of its improper alignment. Dereferencing such a pointer can cause a SIGSEGV signal
on a machine that doesn’t, in general, allow unaligned pointers.
See Appendix D [Attributes], page 220.
all five fields fit consecutively into one two-byte short. They need 15 bits, and one short
provides 16. By contrast,
unsigned char a : 3, b : 3, c : 3, d : 3, e : 3;
needs three bytes. It fits a and b into one char, but c won’t fit in that char (they would
add up to 9 bits). So c and d go into a second char, leaving a gap of two bits between b
and c. Then e needs a third char. By contrast,
unsigned char a : 3, b : 3;
unsigned int c : 3;
unsigned char d : 3, e : 3;
needs only two bytes: the type unsigned int allows c to straddle bytes that are in the
same word.
You can leave a gap of a specified number of bits by defining a nameless bit field. This
looks like type : nbits;. It is allocated space in the structure just as a named bit field
would be allocated.
You can force the following bit field to advance to the following aligned memory object
with type : 0;.
Both of these constructs can syntactically share type with ordinary bit fields. This
example illustrates both:
unsigned int a : 5, : 3, b : 5, : 0, c : 5, : 3, d : 5;
It puts a and b into one int, with a 3-bit gap between them. Then : 0 advances to the
next int, so c and d fit into that one.
These rules for packing bit fields apply to most target platforms, including all the usual
real computers. A few embedded controllers have special layout rules.
q->datum = 5; /* Error! */
p->datum = 5; /* Valid since *p is
not a struct intlistlink_ro. */
A const field can get a value in two ways: by initialization of the whole structure, and
by making a pointer-to-structure point to an object in which that field already has a value.
Chapter 15: Structures 79
Any const field in a structure type makes assignment impossible for structures of that
type (see Section 15.13 [Structure Assignment], page 80). That is because structure assign-
ment works by assigning the structure’s fields, one by one.
struct point *
copy_point (struct point point)
{
struct point *p
= (struct point *) malloc (sizeof (struct point));
if (p == NULL)
fatal ("Out of memory");
Chapter 15: Structures 81
*p = point;
return p;
}
Notionally, assignment on a structure type works by copying each of the fields. Thus, if
any of the fields has the const qualifier, that structure type does not allow assignment:
struct point { const double x, y; };
struct point a, b;
a = b; /* Error! */
See Chapter 7 [Assignment Expressions], page 28.
When a structure type has a field which is an array, as here,
struct record
{
char *name;
int data[4];
};
15.14 Unions
A union type defines alternative ways of looking at the same piece of memory. Each
alternative view is defined with a data type, and identified by a name. Because a union
combines various types, it is considered a compound type, like structures (see Chapter 15
[Structures], page 72). A union definition looks like this:
union name
{
alternative declarations. . .
};
Each alternative declaration looks like a structure field declaration, except that it can’t
be a bit field. For instance,
union number
{
long int integer;
Chapter 15: Structures 82
double float;
}
lets you store either an integer (type long int) or a floating point number (type double) in
the same place in memory. The length and alignment of the union type are the maximum
of all the alternatives—they do not have to be the same. In this union example, double
probably takes more space than long int, but that doesn’t cause a problem in programs
that use the union in the normal way.
The members don’t have to be different in data type. Sometimes each member pertains
to a way the data will be used. For instance,
union datum
{
double latitude;
double longitude;
double height;
double weight;
int continent;
}
This union holds one of several kinds of data; most kinds are floating points, but the
value can also be a code for a continent which is an integer. You could use one member of
type double to access all the values which have that type, but the different member names
will make the program clearer.
The alignment of a union type is the maximum of the alignments of the alternatives.
The size of the union type is the maximum of the sizes of the alternatives, rounded up to a
multiple of the alignment (because every type’s size must be a multiple of its alignment).
All the union alternatives start at the address of the union itself. If an alternative is
shorter than the union as a whole, it occupies the first part of the union’s storage, leaving
the last part unused for that alternative.
Warning: If the code stores data using one union alternative and accesses it with another,
the results depend on the kind of computer in use. Only wizards should try to do this.
However, when you need to do this, a union is a clean way to do it.
Assignment works on any union type by copying the entire value.
This union makes it possible to look at 8 bytes of data that p points to as a single
8-byte integer (p->big_int_elt), as a single floating-point number (p->double_elt), as a
pair of integers (p->two_ints.first and p->two_ints.second), or as a pair of pointers
(p->two_ptrs.first and p->two_ptrs.second).
To pack storage with such a union makes assumptions about the sizes of all the types
involved. This particular union was written expecting a pointer to have the same size as int.
On a machine where one pointer takes 8 bytes, the code using this union probably won’t work
as expected. The union, as such, will function correctly—if you store two values through
two_ints and extract them through two_ints, you will get the same integers back—but
the part of the program that expects the union to be 8 bytes long could malfunction, or at
least use too much space.
The above example shows one case where a struct type with no tag can be useful.
Another way to get effectively the same result is with arrays as members of the union:
union eight_bytes
{
long long big_int_elt;
double double_elt;
int two_ints[2];
void *two_ptrs[2];
};
int x; double y;
int b;
float c;
};
int d;
} foo;
You can access the fields of the unnamed union within foo as if they were individual fields
at the same level as the union definition:
foo.a = 42;
foo.b = 47;
foo.c = 5.25; // Overwrites the value in foo.b.
foo.d = 314;
Avoid using field names that could cause ambiguity. For example, with this definition:
struct
{
int a;
struct
{
int a;
float b;
};
} foo;
it is impossible to tell what foo.a refers to. GNU C reports an error when a definition is
ambiguous in this way.
void
foo (struct mysterious_value *arg)
{
bar (arg);
}
...
Chapter 15: Structures 86
{
struct mysterious_value *p, **q;
p = *q;
foo (p);
}
These examples are valid because the code doesn’t try to understand what p points to; it
just passes the pointer around. (Presumably bar is defined in some other file that really
does have a definition for struct mysterious_value.) However, dereferencing the pointer
would get an error; that requires a definition for the structure type.
called a type tag. In C, a type tag never conflicts with a variable name or function name;
the type tags have a separate name space. Thus, there is no name conflict in this code:
struct pair { int a, b; };
int pair = 1;
nor in this one:
struct pair { int a, b; } pair;
where pair is both a structure type tag and a variable name.
However, struct, union, and enum share the same name space of tags, so this is a
conflict:
struct pair { int a, b; };
enum pair { c, d };
and so is this:
struct pair { int a, b; };
struct pair { int c, d; };
When the code defines a type tag inside a block, the tag’s scope is limited to that block
(as for local variables). Two definitions for one type tag do not conflict if they are in
different scopes; rather, each is valid in its scope. For example,
struct pair { int a, b; };
void
pair_up_doubles (int len, double array[])
{
struct pair { double a, b; };
...
}
has two definitions for struct pair which do not conflict. The one inside the function
applies only within the definition of pair_up_doubles. Within its scope, that definition
shadows the outer definition.
If struct pair appears inside the function body, before the inner definition, it refers to
the outer definition—the only one that has been seen at that point. Thus, in this code,
struct pair { int a, b; };
void
pair_up_doubles (int len, double array[])
{
struct two_pairs { struct pair *p, *q; };
struct pair { double a, b; };
...
}
the structure two_pairs has pointers to the outer definition of struct pair, which is
probably not desirable.
To prevent that, you can write struct pair; inside the function body as a variable
declaration with no variables. This is a forward declaration of the type tag pair: it makes
Chapter 15: Structures 88
the type tag local to the current block, with the details of the type to come later. Here’s
an example:
void
pair_up_doubles (int len, double array[])
{
/* Forward declaration for pair. */
struct pair;
struct two_pairs { struct pair *p, *q; };
/* Give the details. */
struct pair { double a, b; };
...
}
However, the cleanest practice is to avoid shadowing type tags.
89
16 Arrays
An array is a data object that holds a series of elements, all of the same data type. Each
element is identified by its numeric index within the array.
We presented arrays of numbers in the sample programs early in this manual (see
Section 4.2 [Array Example], page 12). However, arrays can have elements of any data
type, including pointers, structures, unions, and other arrays.
If you know another programming language, you may suppose that you know all about
arrays, but C arrays have special quirks, so in this chapter we collect all the information
about arrays in C.
The elements of a C array are allocated consecutively in memory, with no gaps be-
tween them. Each element is aligned as required for its data type (see Appendix A [Type
Alignment], page 215).
The surrounding declaration specifies the element type of the array; that can be any
type of data, but not void or a function type. For instance,
double a[5];
declares a as an array of 5 doubles.
struct foo bstruct[length];
declares bstruct as an array of length objects of type struct foo. A variable array size
like this is allowed when the array is not file-scope.
Other declaration constructs can nest within the array declaration construct. For in-
stance:
struct foo *b[length];
declares b as an array of length pointers to struct foo. This shows that the length need
not be a constant (see Section 16.9 [Arrays of Variable Length], page 95).
double (*c)[5];
declares c as a pointer to an array of 5 doubles, and
char *(*f (int))[5];
declares f as a function taking an int argument and returning a pointer to an array of 5
strings (pointers to chars).
double aa[5][10];
declares aa as an array of 5 elements, each of which is an array of 10 doubles. This shows
how to declare a multidimensional array in C (see Section 16.7 [Multidimensional Arrays],
page 93).
All these declarations specify the array’s length, which is needed in these cases in order
to allocate storage for the array.
16.3 Strings
A string in C is a sequence of elements of type char, terminated with the null character,
the character with code zero. However, the C code that operates on strings normally uses
the pointer type char * to do it.
Programs often need to use strings with specific, fixed contents. To write one in a
C program, use a string constant such as "Take me to your leader!". The data type
of a string constant is char *. For the full syntactic details of writing string constants,
Section 12.7 [String Constants], page 54.
To declare a place to store a non-constant string, declare an array of char. Keep in mind
that it must include one extra char for the terminating null. For instance,
char text[] = { 'H', 'e', 'l', 'l', 'o', 0 };
declares an array named ‘text’ with six elements—five letters and the terminating null
character. An equivalent way to get the same result is this,
char text[] = "Hello";
which copies the elements of the string constant, including its terminating null character.
char message[200];
declares an array long enough to hold a string of 199 ASCII characters plus the terminating
null character.
Chapter 16: Arrays 91
When you store a string into message be sure to check or prove that the length does not
exceed its size. For example,
void
set_message (char *text)
{
int i;
/* Recall that message is declared above. */
for (i = 0; i < sizeof (message); i++)
{
message[i] = text[i];
if (text[i] == 0)
return;
}
fatal_error ("Message is too long for `message'\n");
}
It’s easy to do this with the standard library function strncpy, which fills out the whole
destination array (up to a specified length) with null characters. Thus, if the last character
of the destination is not null, the string did not fit. Many system libraries, including the
GNU C library, hand-optimize strncpy to run faster than an explicit for-loop.
Here’s what the code looks like:
void
set_message (char *text)
{
strncpy (message, text, sizeof (message));
if (message[sizeof (message) - 1] != 0)
fatal_error ("Message is too long for `message');
}
See Section “String and Array Utilities” in The GNU C Library Reference Manual, for
more information about the standard library functions for operating on strings.
You can avoid putting a fixed length limit on strings you construct or operate on by
allocating the space for them dynamically. See Section 15.3 [Dynamic Memory Allocation],
page 74.
space for the array, or for sizeof and typeof (see Section 20.4 [Auto Type], page 122).
Thus, in some contexts C allows
• An extern declaration says how to refer to a variable allocated elsewhere. It does not
need to allocate space for the variable, so if it is an array, you can omit the length. For
example,
extern int foo[];
• When declaring a function parameter as an array, the argument value passed to the
function is really a pointer to the array’s zeroth element. This value does not say how
long the array really is, there is no need to declare it. For example,
int
func (int foo[])
These declarations are examples of incomplete array types, types that are not fully
specified. The incompleteness makes no difference for accessing elements of the array, but it
matters for some other things. For instance, sizeof is not allowed on an incomplete type.
With multidimensional arrays, only the first dimension can be omitted. For example,
suppose we want to represent the positions of pieces on a chessboard which has the usual 8
files (columns), but more (or fewer) ranks (rows) than the usual 8. This declaration could
hold a pointer to a two-dimensional array that can hold that data. Each element of the
array holds one row.
struct chesspiece *funnyboard[][8];
Since it is just a pointer to the start of an array, its type can be incomplete, but it must
state how big each array element is—the number of elements in each row.
Third, a string constant used as an initializer for an array is not converted to a pointer—
rather, the declaration copies the contents of that string in that one special case.
You can copy the contents of an array, just not with an assignment operator. You
can do it by calling the library function memcpy or memmove (see Section “Copying and
Concatenation” in The GNU C Library Reference Manual). Also, when a structure contains
just an array, you can copy that structure.
An array itself is an lvalue if it is a declared variable, or part of a structure or union that
is an lvalue. When you construct an array from elements (see Section 16.8 [Constructing
Array Values], page 94), that array is not an lvalue.
The subarrays within the multidimensional array are allocated consecutively in memory,
and within each subarray, its elements are allocated consecutively in memory. The most
efficient way to process all the elements in the array is to scan the last subscript in the
innermost loop. This means consecutive accesses go to consecutive memory locations, which
optimizes use of the processor’s memory cache. For example:
int total = 0;
float average;
FILE *
concat_fopen (char *s1, char *s2, char *mode)
{
char str[strlen (s1) + strlen (s2) + 1];
strcpy (str, s1);
strcat (str, s2);
return fopen (str, mode);
}
(This uses some standard library functions; see Section “String and Array Utilities” in The
GNU C Library Reference Manual.)
The length of an array is computed once when the storage is allocated and is remembered
for the scope of the array in case it is used in sizeof.
Warning: Don’t allocate a variable-length array if the size might be very large (more
than 100,000), or in a recursive function, because that is likely to cause stack overflow.
Allocate the array dynamically instead (see Section 15.3 [Dynamic Memory Allocation],
page 74).
Jumping or breaking out of the scope of the array name deallocates the storage. Jumping
into the scope is not allowed; that gives an error message.
You can also use variable-length arrays as arguments to functions:
struct entry
tester (int len, char data[len][len])
{
...
}
As usual, a function argument declared with an array type is really a pointer to an array
that already exists. Calling the function does not allocate the array, so there’s no particular
danger of stack overflow in using this construct.
To pass the array first and the length afterward, use a forward declaration in the func-
tion’s parameter list (another GNU extension). For example,
struct entry
tester (int len; char data[len][len], int len)
{
...
}
The int len before the semicolon is a parameter forward declaration, and it serves the
purpose of making the name len known when the declaration of data is parsed.
Chapter 16: Arrays 96
You can write any number of such parameter forward declarations in the parameter
list. They can be separated by commas or semicolons, but the last one must end with a
semicolon, which is followed by the “real” parameter declarations. Each forward declaration
must match a “real” declaration in parameter name and data type. ISO C11 does not
support parameter forward declarations.
97
17 Enumeration Types
An enumeration type represents a limited set of integer values, each with a name. It is
effectively equivalent to a primitive integer type.
Suppose we have a list of possible emotional states to store in an integer variable. We
can give names to these alternative values with an enumeration:
enum emotion_state { neutral, happy, sad, worried,
calm, nervous };
(Never mind that this is a simplistic way to classify emotional states; it’s just a code
example.)
The names inside the enumeration are called enumerators. The enumeration type defines
them as constants, and their values are consecutive integers; neutral is 0, happy is 1, sad
is 2, and so on. Alternatively, you can specify values for the enumerators explicitly like this:
enum emotion_state { neutral = 2, happy = 5,
sad = 20, worried = 10,
calm = -5, nervous = -300 };
Each enumerator which does not specify a value gets value zero (if it is at the beginning)
or the next consecutive integer.
/* neutral is 0 by default,
and worried is 21 by default. */
enum emotion_state { neutral,
happy = 5, sad = 20, worried,
calm = -5, nervous = -300 };
If an enumerator is obsolete, you can specify that using it should cause a warning, by
including an attribute in the enumerator’s declaration. Here is how happy would look with
this attribute:
happy __attribute__
((deprecated
("impossible under plutocratic rule")))
= 5,
See Appendix D [Attributes], page 220.
You can declare variables with the enumeration type:
enum emotion_state feelings_now;
In the C code itself, this is equivalent to declaring the variable int. (If all the enumer-
ation values are positive, it is equivalent to unsigned int.) However, declaring it with the
enumeration type has an advantage in debugging, because GDB knows it should display the
current value of the variable using the corresponding name. If the variable’s type is int,
GDB can only show the value as a number.
The identifier that follows enum is called a type tag since it distinguishes different enu-
meration types. Type tags are in a separate name space and belong to scopes like most
other names in C. See Section 15.21 [Type Tags], page 86, for explanation.
You can predeclare an enum type tag like a structure or union type tag, like this:
enum foo;
Chapter 17: Enumeration Types 98
fooptr x, y;
That declaration is equivalent to the following one:
struct foo *x, *y;
You can define a typedef alias for any type. For instance, this makes frobcount an alias
for type int:
typedef int frobcount;
This doesn’t define a new type distinct from int. Rather, frobcount is another name
for the type int. Once the variable is declared, it makes no difference which name the
declaration used.
There is a syntactic difference, however, between frobcount and int: A typedef name
cannot be used with signed, unsigned, long or short. It has to specify the type all by
itself. So you can’t write this:
unsigned frobcount f1; /* Error! */
But you can write this:
typedef unsigned int unsigned_frobcount;
unsigned_frobcount f1;
In other words, a typedef name is not an alias for a keyword such as int. It stands for
a type, and that could be the type int.
Typedef names are in the same namespace as functions and variables, so you can’t use
the same name for a typedef and a function, or a typedef and a variable. When a typedef
is declared inside a code block, it is in scope only in that block.
Warning: Avoid defining typedef names that end in ‘_t’, because many of these have
standard meanings.
You can redefine a typedef name to the exact same type as its first definition, but you
cannot redefine a typedef name to a different type, even if the two types are compatible.
For example, this is valid:
typedef int frobcount;
typedef int frotzcount;
typedef frotzcount frobcount;
typedef frobcount frotzcount;
Chapter 18: Defining Typedef Names 100
because each typedef name is always defined with the same type (int), but this is not valid:
enum foo {f1, f2, f3};
typedef enum foo frobcount;
typedef int frobcount;
Even though the type enum foo is compatible with int, they are not the same type.
101
19 Statements
A statement specifies computations to be done for effect; it does not produce a value, as
an expression would. In general a statement ends with a semicolon (‘;’), but blocks (which
are statements, more or less) are an exception to that rule.
The places to use statements are inside a block, and inside a complex statement. A
complex statement contains one or two components that are nested statements. Each such
component must consist of one and only one statement. The way to put multiple statements
in such a component is to group them into a block (see Section 19.4 [Blocks], page 102),
which counts as one statement.
The following sections describe the various kinds of statement.
If the target of p is not declared volatile, the compiler might optimize away the memory
access, since it knows that the value isn’t really used. See Section 21.2 [volatile], page 127.
19.2 if Statement
An if statement computes an expression to decide whether to execute the following state-
ment or not. It looks like this:
if (condition)
execute-if-true
The first thing this does is compute the value of condition. If that is true (nonzero),
then it executes the statement execute-if-true. If the value of condition is false (zero), it
doesn’t execute execute-if-true; instead, it does nothing.
This is a complex statement because it contains a component execute-if-true that is
a nested statement. It must be one and only one statement. The way to put multiple
statements there is to group them into a block (see Section 19.4 [Blocks], page 102).
Chapter 19: Statements 102
19.4 Blocks
A block is a construct that contains multiple statements of any kind. It begins with ‘{’ and
ends with ‘}’, and has a series of statements and declarations in between. Another name
for blocks is compound statements.
Is a block a statement? Yes and no. It doesn’t look like a normal statement—it does
not end with a semicolon. But you can use it like a statement; anywhere that a statement
is required or allowed, you can write a block and consider that block a statement.
So far it seems that a block is a kind of statement with an unusual syntax. But that
is not entirely true: a function body is also a block, and that block is definitely not a
statement. The text after a function header is not treated as a statement; only a function
body is allowed there, and nothing else would be meaningful there.
In a formal grammar we would have to choose—either a block is a kind of statement
or it is not. But this manual is meant for humans, not for parser generators. The clearest
answer for humans is, “a block is a statement, in some ways.”
A block that isn’t a function body is called an internal block or a nested block. You can
put a nested block directly inside another block, but more often the nested block is inside
some complex statement, such as a for statement or an if statement.
There are two uses for nested blocks in C:
• To specify the scope for local declarations. For instance, a local variable’s scope is the
rest of the innermost containing block.
• To write a series of statements where, syntactically, one statement is called for. For
instance, the execute-if-true of an if statement is one statement. To put multiple
statements there, they have to be wrapped in a block, like this:
if (x < 0)
{
printf ("x was negative\n");
x = -x;
}
Chapter 19: Statements 103
This example (repeated from above) shows a nested block which serves both purposes:
it includes two statements (plus a declaration) in the body of a while statement, and it
provides the scope for the declaration of q.
void
free_intlist (struct intlistlink *p)
{
while (p)
{
struct intlistlink *q = p;
p = p->next;
free (q);
}
}
};
void
process_all_elements (struct list_if_tuples *list)
{
while (list)
{
/* Process all the elements in this node’s vector,
stopping when we reach one that is null. */
for (i = 0; i < list->length; i++)
{
/* Null element terminates this node’s vector. */
if (list->contents[i] == NULL)
/* Exit the for loop. */
break;
/* Operate on the next element. */
process_element (list->contents[i]);
}
list = list->next;
}
}
The only way in C to exit from an outer loop is with goto (see Section 19.12 [goto
Statement], page 112).
++i;
}
instead of this:
for (i = 0; i < n; ++i)
{
...
}
The choice is mainly a matter of what is more readable for programmers. However, there
is also a syntactic difference: advance is an expression, not a statement. It can’t include
loops, blocks, declarations, etc.
The values in case labels must reduce to integer constants. They can use arithmetic,
and enum constants, but they cannot refer to data in memory, because they have to be
computed at compile time. It is an error if two case labels specify the same value, or
ranges that overlap, or if one is a range and the other is a value in that range.
You can also define a default case to handle “any other value,” like this:
default:
statements
break;
If the switch statement has no default: label, then it does nothing when the value
matches none of the cases.
The brace-group inside the switch statement is a block, and you can declare variables
with that scope just as in any other block (see Section 19.4 [Blocks], page 102). How-
ever, initializers in these declarations won’t necessarily be executed every time the switch
statement runs, so it is best to avoid giving them initializers.
break; inside a switch statement exits immediately from the switch statement. See
Section 19.6.3 [break Statement], page 104.
If there is no break; at the end of the code for a case, execution continues into the code
for the following case. This happens more often by mistake than intentionally, but since
this feature is used in real code, we cannot eliminate it.
Warning: When one case is intended to fall through to the next, write a comment like
‘falls through’ to say it’s intentional. That way, other programmers won’t assume it was
an error and “fix” it erroneously.
Consecutive case statements could, pedantically, be considered an instance of falling
through, but we don’t consider or treat them that way because they won’t confuse anyone.
struct vp
count_vowels_and_punct (char *string)
{
int c;
int vowels = 0;
int punct = 0;
/* Don’t change the parameter itself. */
/* That helps in debugging. */
char *p = string;
struct vp value;
while (c = *p++)
switch (c)
{
case 'y':
Chapter 19: Statements 110
case 'Y':
/* We assume y_is_consonant will check surrounding
letters to determine whether this y is a vowel. */
if (y_is_consonant (p - 1))
break;
/* Falls through */
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowels++;
break;
case '.':
case ',':
case ':':
case ';':
case '?':
case '!':
case '\"':
case '\'':
punct++;
break;
}
value.vowels = vowels;
value.punct = punct;
return value;
}
in the for-header itself, leaving no work for the body. Here is an example that searches for
the first newline in array:
for (p = array; *p != '\n'; p++)
;
If the goto jumps into the scope of a variable, it does not initialize the variable. For
example, if x is negative,
if (x < 0)
goto negative;
if (y < 0)
{
int i = 5;
negative:
printf ("Negative, and i is %d\n", i);
return;
}
prints junk because i was not initialized.
If the block declares a variable-length automatic array, jumping into it gives a compi-
lation error. However, jumping out of the scope of a variable-length array works fine, and
deallocates its storage.
A label can’t come directly before a declaration, so the code can’t jump directly to one.
For example, this is not allowed:
{
goto foo;
foo:
int x = 5;
bar(&x);
}
The workaround is to add a statement, even an empty statement, directly after the label.
For example:
{
goto foo;
foo:
;
int x = 5;
bar(&x);
}
Likewise, a label can’t be the last thing in a block. The workaround solution is the same:
add a semicolon after the label.
These unnecessary restrictions on labels make no sense, and ought in principle to be
removed; but they do only a little harm since labels and goto are rarely the best way to
write a program.
These examples are all artificial; it would be more natural to write them in other ways,
without goto. For instance, the clean way to write the example that prints ‘Negative’ is
this:
if (x < 0 || y < 0)
{
printf ("Negative\n");
return;
Chapter 19: Statements 114
}
It is hard to construct simple examples where goto is actually the best way to write a
program. Its rare good uses tend to be in complex code, thus not apt for the purpose of
explaining the meaning of goto.
The only good time to use goto is when it makes the code simpler than any alternative.
Jumping backward is rarely desirable, because usually the other looping and control con-
structs give simpler code. Using goto to jump forward is more often desirable, for instance
when a function needs to do some processing in an error case and errors can occur at various
different places within the function.
({ \
__label__ found; \
__auto_type _SEARCH_target = (target); \
__auto_type _SEARCH_array = (array); \
int i, j; \
int value; \
for (i = 0; i < max; i++) \
for (j = 0; j < max; j++) \
if (_SEARCH_array[i][j] == _SEARCH_target) \
{ value = i; goto found; } \
value = -1; \
found: \
value; \
})
Ordinary labels are visible throughout the function where they are defined, and only
in that function. However, explicitly declared local labels of a block are visible in nested
function definitions inside that block. See Section 22.7.3 [Nested Functions], page 145, for
details.
See Section 19.12 [goto Statement], page 112.
You can make the table entries offsets instead of addresses by subtracting one label from
the others. Here is an example:
static const int array[] = { &&foo - &&foo, &&bar - &&foo,
&&hack - &&foo };
goto *(&&foo + array[i]);
Using offsets is preferable in shared libraries, as it avoids the need for dynamic relocation
of the array elements; therefore, the array can be read-only.
An array of label values or offsets serves a purpose much like that of the switch state-
ment. The switch statement is cleaner, so use switch by preference when feasible.
Another use of label values is in an interpreter for threaded code. The labels within the
interpreter function can be stored in the threaded code for super-fast dispatching.
The last statement in the block should be an expression statement; an expression followed
by a semicolon, that is. The value of this expression serves as the value of statement
expression. If the last statement is anything else, the statement expression’s value is void.
This feature is mainly useful in making macro definitions compute each operand exactly
once. See Section 26.5.10.5 [Macros and Auto Type], page 180.
Statement expressions are not allowed in expressions that must be constant, such as the
value for an enumerator, the width of a bit-field, or the initial value of a static variable.
Jumping into a statement expression—with goto, or using a switch statement outside
the statement expression—is an error. With a computed goto (see Section 19.14 [Labels as
Values], page 115), the compiler can’t detect the error, but it still won’t work.
Jumping out of a statement expression is permitted, but since subexpressions in C are
not computed in a strict order, it is unpredictable which other subexpressions will have
been computed by then. For example,
foo (), (({ bar1 (); goto a; 0; }) + bar2 ()), baz();
calls foo and bar1 before it jumps, and never calls baz, but may or may not call bar2. If
bar2 does get called, that occurs after foo and before bar1.
118
20 Variables
Every variable used in a C program needs to be made known by a declaration. It can be
used only after it has been declared. It is an error to declare a variable name more than
once in the same scope; an exception is that extern declarations and tentative definitions
can coexist with another declaration of the same variable.
Variables can be declared anywhere within a block or file. (Older versions of C required
that all variable declarations within a block occur before any statements.)
Variables declared within a function or block are local to it. This means that the
variable name is visible only until the end of that function or block, and the memory space
is allocated only while control is within it.
Variables declared at the top level in a file are called file-scope. They are assigned
fixed, distinct memory locations, so they retain their values for the whole execution of the
program.
20.2 Initializers
A variable’s declaration, unless it is extern, should also specify its initial value. For numeric
and pointer-type variables, the initializer is an expression for the value. If necessary, it is
converted to the variable’s type, just as in an assignment.
You can also initialize a local structure-type (see Chapter 15 [Structures], page 72) or
local union-type (see Section 15.14 [Unions], page 81) variable this way, from an expression
whose value has the same type. But you can’t initialize an array this way (see Chapter 16
Chapter 20: Variables 120
[Arrays], page 89), since arrays are not first-class objects in C (see Section 16.6 [Limitations
of C Arrays], page 92) and there is no array assignment.
You can initialize arrays and structures componentwise, with a list of the elements or
components. You can initialize a union with any one of its alternatives.
• A component-wise initializer for an array consists of element values surrounded by
‘{. . . }’. If the values in the initializer don’t cover all the elements in the array, the
remaining elements are initialized to zero.
You can omit the size of the array when you declare it, and let the initializer specify
the size:
int array[] = { 3, 9, 12 };
• A component-wise initializer for a structure consists of field values surrounded by
‘{. . . }’. Write the field values in the same order as the fields are declared in the
structure. If the values in the initializer don’t cover all the fields in the structure, the
remaining fields are initialized to zero.
• The initializer for a union-type variable has the form { value }, where value initializes
the first alternative in the union definition.
For an array of arrays, a structure containing arrays, an array of structures, etc., you
can nest these constructs. For example,
struct point { double x, y; };
To designate specific array elements during initialization, include the array index in
brackets, and an assignment operator, for each element:
int foo[10] = { [3] = 42, [7] = 58 };
This does the same thing as:
int foo[10] = { 0, 0, 0, 42, 0, 0, 0, 58, 0, 0 };
The array initialization can include non-designated element values alongside designated
indices; these follow the expected ordering of the array initialization, so that
int foo[10] = { [3] = 42, 43, 44, [7] = 58 };
does the same thing as:
int foo[10] = { 0, 0, 0, 42, 43, 44, 0, 58, 0, 0 };
Note that you can only use constant expressions as array index values, not variables.
If you need to initialize a subsequence of sequential array elements to the same value,
you can specify a range:
int foo[100] = { [0 ... 19] = 42, [20 ... 99] = 43 };
Using a range this way is a GNU C extension.
When subsequence ranges overlap, each element is initialized by the last specification
that applies to it. Thus, this initialization is equivalent to the previous one.
int foo[100] = { [0 ... 99] = 43, [0 ... 19] = 42 };
as the second overrides the first for elements 0 through 19.
The value used to initialize a range of elements is evaluated only once, for the first
element in the range. So for example, this code
int random_values[100]
= { [0 ... 99] = get_random_number() };
would initialize all 100 elements of the array random_values to the same value—probably
not what is intended.
Similarly, you can initialize specific fields of a structure variable by specifying the field
name prefixed with a dot:
struct point { int x; int y; };
In a simple test program, that statement is likely to print 0, simply because every process
starts with memory zeroed. But don’t rely on it to be zero—that is erroneous.
Note: Make sure to store a value into each local variable (by assignment, or by initial-
ization) before referring to its value.
The initial value of a static local variable has the same limitations as for file-scope
variables: it can’t depend on the contents of storage or call any functions. It can use
the address of a file-scope variable or a static local variable, because those addresses are
determined before the program runs.
variable is used anywhere in the program, the use will be reported as an error, saying that
the variable is not defined.
A file-scope declaration without an initial value is called a tentative definition. This is
a strange hybrid: it can allocate space for the variable, but does not insist. So it causes no
conflict, no error, if the variable has another declaration that allocates space for it, perhaps
in another compilation module. But if nothing else allocates space for the variable, the
tentative definition will do it. Any number of compilation modules can declare the same
variable in this way, and that is sufficient for all of them to use the variable.
In programs that are very large or have many contributors, it may be wise to adopt
the convention of never using tentative definitions. You can use the compilation option
-fno-common to make them an error, or -fcommon to enable them. The default depends on
the version of GCC and its target.
If a file-scope variable gets its space through a tentative definition, it starts out containing
all zeros.
21 Type Qualifiers
A declaration can include type qualifiers to advise the compiler about how the variable will
be used. There are three different qualifiers, const, volatile and restrict. They pertain
to different issues, so you can use more than one together. For instance, const volatile
describes a value that the program is not allowed to change, but might have a different value
each time the program examines it. (This might perhaps be a special hardware register, or
part of shared memory.)
If you are just learning C, you can skip this chapter.
and calling foo is not specified, so they may be done in either order; the fact that lock is
volatile has no effect on that.
22 Functions
We have already presented many examples of functions, so if you’ve read this far, you
basically understand the concept of a function. It is vital, nonetheless, to have a chapter in
the manual that collects all the information about functions.
• The scope of the parameter variables is the entire function body, notwithstanding the
fact that they are written in the function header, which is just outside the function
body.
If a function has no parameters, it would be most natural for the list of parameters in
its definition to be empty. But that, in C, has a special meaning for historical reasons: “Do
not check that calls to this function have the right number of arguments.” Thus,
int
foo ()
{
return 5;
}
int
bar (int x)
{
return foo (x);
}
would not report a compilation error in passing x as an argument to foo. By contrast,
int
foo (void)
{
return 5;
}
int
bar (int x)
{
return foo (x);
}
would report an error because foo is supposed to receive no arguments.
{
array[4] = 0;
}
or write the parameter declaration explicitly as a pointer:
void
clobber4 (int *array)
{
array[4] = 0;
}
They are all equivalent.
int
main (void)
{
int data[] = {1, 2, 3, 4, 5, 6};
int i;
printf ("\n");
clobber4 (data);
printf ("\n");
return EXIT_SUCCESS;
}
shows that data[4] has become zero after the call to clobber4.
The array data has 6 elements, but passing it to a function whose argument type is
written as int [20] is not an error, because that really stands for int *. The pointer that
Chapter 22: Functions 134
is the real argument carries no indication of the length of the array it points into. It is not
required to point to the beginning of the array, either. For instance,
clobber4 (data+1);
passes an “array” that starts at element 1 of data, and the effect is to zero data[5] instead
of data[4].
If all calls to the function will provide an array of a particular size, you can specify the
size of the array to be static:
void
clobber4 (int array[static 20])
...
This is a promise to the compiler that the function will always be called with an array of
20 elements, so that the compiler can optimize code accordingly. If the code breaks this
promise and calls the function with, for example, a shorter array, unpredictable things may
happen.
struct foo x;
struct foo
swapfoo (struct foo inval)
{
struct foo outval;
outval.a = inval.b;
outval.b = inval.a;
return outval;
}
This simpler definition of swapfoo avoids using a local variable to hold the result about
to be return, by using a structure constructor (see Section 15.17 [Structure Constructors],
page 84), like this:
struct foo
swapfoo (struct foo inval)
{
return (struct foo) { inval.b, inval.a };
}
It is valid to define a structure type in a function’s parameter list, as in
int
frob_bar (struct bar { int a, b; } inval)
{
body
}
and body can access the fields of inval since the structure type struct bar is defined for
the whole function body. However, there is no way to create a struct bar argument to pass
to frob_bar, except with kludges. As a result, defining a structure type in a parameter list
is useless in practice.
A declaration that specifies argument types is called a function prototype. You can
include the argument names or omit them. The names, if included in the declaration, have
no effect, but they may serve as documentation.
This form of prototype specifies fixed argument types:
rettype function (argtypes. . . );
This form says the function takes no arguments:
rettype function (void);
This form declares types for some arguments, and allows additional arguments whose types
are not specified:
rettype function (argtypes. . . , ...);
For a parameter that’s an array of variable length, you can write its declaration with ‘*’
where the “length” of the array would normally go; for example, these are all equivalent.
double maximum (int n, int m, double a[n][m]);
double maximum (int n, int m, double a[*][*]);
double maximum (int n, int m, double a[ ][*]);
double maximum (int n, int m, double a[ ][m]);
The old-fashioned form of declaration, which is not a prototype, says nothing about the
types of arguments or how many they should be:
rettype function ();
Warning: Arguments passed to a function declared without a prototype are converted
with the default argument promotions (see Section 24.3 [Argument Promotions], page 154.
Likewise for additional arguments whose types are unspecified.
Function declarations are usually written at the top level in a source file, but you can
also put them inside code blocks. Then the function name is visible for the rest of the
containing scope. For example:
void
foo (char *file_name)
{
void save_file (char *);
save_file (file_name);
}
If another part of the code tries to call the function save_file, this declaration won’t
be in effect there. So the function will get an implicit declaration of the form extern
int save_file ();. That conflicts with the explicit declaration here, and the discrepancy
generates a warning.
The syntax of C traditionally allows omitting the data type in a function declaration if
it specifies a storage class or a qualifier. Then the type defaults to int. For example:
static foo (double x);
defaults the return type to int. This is bad practice; if you see it, fix it.
Calling a function that is undeclared has the effect of creating an implicit declaration in
the innermost containing scope, equivalent to this:
extern int function ();
Chapter 22: Functions 137
This declaration says that the function returns int but leaves its argument types unspec-
ified. If that does not accurately fit the function, then the program needs an explicit
declaration of the function with argument types in order to call it correctly.
Implicit declarations are deprecated, and a function call that creates one causes a warn-
ing.
void
subroutine (int x)
{
x = 5;
}
Chapter 22: Functions 138
void
main (void)
{
int y = 20;
subroutine (y);
printf ("y is %d\n", y);
return EXIT_SUCCESS;
}
prints ‘y is 20’. Calling subroutine initializes x from the value of y, but this does not
establish any other relationship between the two variables. Thus, the assignment to x,
inside subroutine, changes only that x.
If an argument’s type is specified by the function’s declaration, the function call converts
the argument expression to that type if possible. If the conversion is impossible, that is an
error.
If the function’s declaration doesn’t specify the type of that argument, then the default
argument promotions apply. See Section 24.3 [Argument Promotions], page 154.
The call conceptually dereferences the pointer binary_op to “get” the function it points
to, and calls that function. If you wish, you can explicitly represent the dereference by
writing the * operator:
(*binary_op) (x, 5)
The ‘*’ reminds people reading the code that binary_op is a function pointer rather
than the name of a specific function.
int
main (void)
{
...
if (foo)
return EXIT_SUCCESS;
else
Chapter 22: Functions 141
return EXIT_FAILURE;
}
Some types of programs maintain special conventions for various return values; for ex-
ample, comparison programs including cmp and diff return 1 to indicate a mismatch, and
2 to indicate that the comparison couldn’t be performed.
int
main (int argc, char *argv[])
{
char *program_name = argv[0];
int
Chapter 22: Functions 142
int
main (void)
{
char *home_directory = getenv ("HOME");
if (home_directory)
printf ("My home directory is: %s\n", home_directory);
else
printf ("My home directory is not defined!\n");
}
int
add_multiple_values (int argcount, ...)
{
int counter, total = 0;
return total;
}
With GNU C, va_end is superfluous, but some other compilers might make va_start
allocate memory so that calling va_end is necessary to avoid a memory leak. Before doing
va_start again with the same variable, do va_end first.
Because of this possible memory allocation, it is risky (in principle) to copy one va_list
variable to another with assignment. Instead, use va_copy, which copies the substance
but allocates separate memory in the variable you copy to. The call looks like va_copy
(to, from), where both to and from should be variables of type va_list. In principle, do
va_end on each of these variables before its scope ends.
Chapter 22: Functions 145
Since the additional arguments’ types are not specified in the function’s definition, the
default argument promotions (see Section 24.3 [Argument Promotions], page 154) apply
to them in function calls. The function definition must take account of this; thus, if an
argument was passed as short, the function should get it as int. If an argument was
passed as float, the function should get it as double.
C has no mechanism to tell the variadic function how many arguments were passed to it,
so its calling convention must give it a way to determine this. That’s why add_multiple_
values takes a fixed argument that says how many more arguments follow. Thus, you can
call the function like this:
sum = add_multiple_values (3, 12, 34, 190);
/* Value is 12+34+190. */
In GNU C, there is no actual need to use the va_end function. In fact, it does nothing.
It’s used for compatibility with other compilers, when that matters.
It is a mistake to access variables declared as va_list except in the specific ways de-
scribed here. Just what that type consists of is an implementation detail, which could vary
from one platform to another.
function’s name is the same as the parent function’s name, there will be no way to refer to
the parent function inside the scope of the name of the nested function.
Using extern or static on a nested function definition is an error.
It is possible to call the nested function from outside the scope of its name by storing its
address or passing the address to another function. You can do this safely, but you must
be careful:
hack (int *array, int size, int addition)
{
void store (int index, int value)
{ array[index] = value + addition; }
int i;
...
for (i = 0; i < size; i++)
. . . access (array, i) . . .
...
return 0;
return p->second;
}
Optimized compilation can substitute the inline function’s body for any call to it. This is
called inlining the function. It makes the code that contains the call run faster, significantly
so if the inline function is small.
Here’s a function that uses list_second:
int
pairlist_length (struct list *l)
{
int length = 0;
while (l)
{
length++;
l = list_second (l);
}
return length;
}
Substituting the code of list_second into the definition of pairlist_length results in
this code, in effect:
int
pairlist_length (struct list *l)
{
int length = 0;
while (l)
{
length++;
l = l->second;
}
return length;
}
Since the definition of list_second does not say extern or static, that definition is
used only for inlining. It doesn’t generate code that can be called at run time. If not all
the calls to the function are inlined, there must be a definition of the same function name
in another module for them to call.
Adding static to an inline function definition means the function definition is limited
to this compilation module. Also, it generates run-time code if necessary for the sake of
any calls that were not inlined. If all calls are inlined then the function definition does
not generate run-time code, but you can force generation of run-time code with the option
-fkeep-inline-functions.
Specifying extern along with inline means the function is external and generates run-
time code to be called from other separately compiled modules, as well as inlined. You can
define the function as inline without extern in other modules so as to inline calls to the
same function in those modules.
Why are some calls not inlined? First of all, inlining is an optimization, so non-optimized
compilation does not inline.
Chapter 22: Functions 149
Some calls cannot be inlined for technical reasons. Also, certain usages in a function
definition can make it unsuitable for inline substitution. Among these usages are: vari-
adic functions, use of alloca, use of computed goto (see Section 19.14 [Labels as Values],
page 115), and use of nonlocal goto. The option -Winline requests a warning when a func-
tion marked inline is unsuitable to be inlined. The warning explains what obstacle makes
it unsuitable.
Just because a call can be inlined does not mean it should be inlined. The GNU C com-
piler weighs costs and benefits to decide whether inlining a particular call is advantageous.
You can force inlining of all calls to a given function that can be inlined, even in a
non-optimized compilation. by specifying the ‘always_inline’ attribute for the function,
like this:
/* Prototype. */
inline void foo (const char) __attribute__((always_inline));
This is a GNU C extension. See Appendix D [Attributes], page 220.
A function call may be inlined even if not declared inline in special cases where the
compiler can determine this is correct and desirable. For instance, when a static function
is called only once, it will very likely be inlined. With -flto, link-time optimization, any
function might be inlined. To absolutely prevent inlining of a specific function, specify
__attribute__((__noinline__)) in the function’s definition.
defaults the return type to int. This is bad practice; if you see it, fix it.
An old-style (or “K&R”) function definition is the way function definitions were written
in the 1980s. It looks like this:
rettype
function (parmnames)
parm_declarations
{
body
}
In parmnames, only the parameter names are listed, separated by commas. Then
parm declarations declares their data types; these declarations look just like variable dec-
larations. If a parameter is listed in parmnames but has no declaration, it is implicitly
declared int.
There is no reason to write a definition this way nowadays, but they can still be seen in
older GNU programs.
An old-style variadic function definition looks like this:
#include <varargs.h>
int
add_multiple_values (va_alist)
va_dcl
{
int argcount;
int counter, total = 0;
return total;
}
Chapter 22: Functions 151
Note that the old-style variadic function definition has no fixed parameter variables; all
arguments must be obtained with va_arg.
152
23 Compatible Types
Declaring a function or variable twice is valid in C only if the two declarations specify com-
patible types. In addition, some operations on pointers require operands to have compatible
target types.
In C, two different primitive types are never compatible. Likewise for the defined types
struct, union and enum: two separately defined types are incompatible unless they are
defined exactly the same way.
However, there are a few cases where different types can be compatible:
• Every enumeration type is compatible with some integer type. In GNU C, the choice
of integer type depends on the largest enumeration value.
• Array types are compatible if the element types are compatible and the sizes (when
specified) match.
• Pointer types are compatible if the pointer target types are compatible.
• Function types that specify argument types are compatible if the return types are com-
patible and the argument types are compatible, argument by argument. In addition,
they must all agree in whether they use ... to allow additional arguments.
• Function types that don’t specify argument types are compatible if the return types
are.
• Function types that specify the argument types are compatible with function types
that omit them, if the return types are compatible and the specified argument types
are unaltered by the argument promotions (see Section 24.3 [Argument Promotions],
page 154).
In order for types to be compatible, they must agree in their type qualifiers. Thus, const
int and int are incompatible. It follows that const int * and int * are incompatible too
(they are pointers to types that are not compatible).
If two types are compatible ignoring the qualifiers, we call them nearly compatible. (If
they are array types, we ignore qualifiers on the element types.1 ) Comparison of pointers
is valid if the pointers’ target types are nearly compatible. Likewise, the two branches of a
conditional expression may be pointers to nearly compatible target types.
If two types are compatible ignoring the qualifiers, and the first type has all the qualifiers
of the second type, we say the first is upward compatible with the second. Assignment of
pointers requires the assigned pointer’s target type to be upward compatible with the right
operand (the new value)’s target type.
1
This is a GNU C extension.
153
24 Type Conversions
C converts between data types automatically when that seems clearly necessary. In addition,
you can convert explicitly with a cast.
• Converting between pointer types when the left-hand target type is upward compatible
with the right-hand target type. See Chapter 23 [Compatible Types], page 152.
1
On an embedded controller where char or short is the same width as int, unsigned char or unsigned
short promotes to unsigned int, but that never occurs in GNU C on real computers.
Chapter 24: Type Conversions 155
25 Scope
void
foo (void)
{
int x = 17;
printf ("%d\n", x);
}
This prints 17, the value of the variable x declared in the function body block, rather than
the value of the variable x at file scope. We say that the inner declaration of x shadows the
outer declaration, for the extent of the inner declaration’s scope.
A declaration with block scope can be shadowed by another declaration with the same
name in a subblock.
Chapter 25: Scope 157
void
foo (void)
{
char *x = "foo";
{
int x = 42;
...
exit (x / 6);
}
}
A function parameter’s scope is the entire function body, but it can be shadowed. For
example:
int x = 42;
void
foo (int x)
{
printf ("%d\n", x);
}
This prints the value of x the function parameter, rather than the value of the file-scope
variable x.
Labels (see Section 19.12 [goto Statement], page 112) have function scope: each label
is visible for the whole of the containing function body, both before and after the label
declaration:
void
foo (void)
{
...
goto bar;
...
{ // Subblock does not affect labels.
bar:
...
}
goto bar;
}
Except for labels, a declared identifier is not visible to code before its declaration. For
example:
int x = 5;
int y = x + 10;
will work, but:
int x = y + 10;
int y = 5;
cannot refer to the variable y before its declaration.
158
26 Preprocessing
As the first stage of compiling a C source module, GCC transforms the text with text
substitutions and file inclusions. This is called preprocessing.
26.2 Directives
Preprocessing directives are lines in the program that start with ‘#’. Whitespace is allowed
before and after the ‘#’. The ‘#’ is followed by an identifier, the directive name. It specifies
the operation to perform. Here are a couple of examples:
#define LIMIT 51
# undef LIMIT
# error You screwed up!
We usually refer to a directive as #name where name is the directive name. For example,
#define means the directive that defines a macro.
The ‘#’ that begins a directive cannot come from a macro expansion. Also, the directive
name is not macro expanded. Thus, if foo is defined as a macro expanding to define, that
does not make #foo a valid preprocessing directive.
Chapter 26: Preprocessing 159
The set of valid directive names is fixed. Programs cannot define new preprocessing
directives.
Some directives require arguments; these make up the rest of the directive line and
must be separated from the directive name by whitespace. For example, #define must be
followed by a macro name and the intended expansion of the macro.
A preprocessing directive cannot cover more than one line. The line can, however, be
continued with backslash-newline, or by a ‘/*. . . */’-style comment that extends past the
end of the line. These will be replaced (by nothing, or by whitespace) before the directive
is processed.
• Program-specific header files contain declarations for interfaces between the source files
of a particular program. It is a good idea to create a header file for related declarations
and macro definitions if all or most of them are needed in several different source files.
Including a header file produces the same results as copying the header file into each
source file that needs it. Such copying would be time-consuming and error-prone. With a
header file, the related declarations appear in only one place. If they need to be changed,
you can change them in one place, and programs that include the header file will then
automatically use the new version when next recompiled. The header file eliminates the
labor of finding and changing all the copies as well as the risk that a failure to change one
copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with .h. It is most
portable to use only letters, digits, dashes, and underscores in header file names, and at
most one dot.
The operation of including another source file isn’t actually limited to the sort of code
we put into header files. You can put any sort of C code into a separate file, then use
#include to copy it virtually into other C source files. But that is a strange thing to do.
int
main (void)
{
puts (test ());
}
the result is equivalent to putting this text in program.c:
int x;
char *test (void);
int
main (void)
{
puts (test ());
}
Included files are not limited to declarations and macro definitions; those are merely the
typical uses. Any fragment of a C program can be included from another file. The include
file could even contain the beginning of a statement that is concluded in the containing file,
or the end of a statement that was started in the including file. However, an included file
must consist of complete tokens. Comments and string literals that have not been closed
by the end of an included file are invalid. For error recovery, the compiler terminates them
at the end of the file.
To avoid confusion, it is best if header files contain only complete syntactic units—
function declarations or definitions, type declarations, etc.
The line following the #include directive is always treated as a separate line, even if the
included file lacks a final newline. There is no problem putting a preprocessing directive
there.
libdir/target/include
/usr/include/target
/usr/include
The list may be different in some operating systems. Other directories are added for C++.
In the above, target is the canonical name of the system GCC was configured to compile
code for; often but not always the same as the canonical name of the system it runs on.
version is the version of GCC in use.
You can add to this list with the -Idir command-line option. All the directories named
by -I are searched, in left-to-right order, before the default directories. The only exception
is when dir is already searched by default. In this case, the option is ignored and the search
order for system directories remains unchanged.
Duplicate directories are removed from the quote and bracket search chains before the
two chains are merged to make the final search chain. Thus, it is possible for a directory to
occur twice in the final search chain if it was specified in both the quote and bracket chains.
You can prevent GCC from searching any of the default directories with the -nostdinc
option. This is useful when you are compiling an operating system kernel or some other
program that does not use the standard C library facilities, or the standard C library itself.
-I options are not ignored as described above when -nostdinc is in effect.
GCC looks for headers requested with #include "file" first in the directory containing
the current file, then in the quote directories specified by -iquote options, then in the same
places it looks for a system header. For example, if /usr/include/sys/stat.h contains
#include "types.h", GCC looks for types.h first in /usr/include/sys, then in the quote
directories and then in its usual search path.
#line (see Section 26.8 [Line Control], page 187) does not change GCC’s idea of the
directory containing the current file.
The -I- is an old-fashioned, deprecated way to specify the quote directories. To look for
headers in a directory named -, specify -I./-. There are several more ways to adjust the
header search path. See Section “Invoking GCC” in Using the GNU Compiler Collection.
#endif /* !FILE_FOO_SEEN */
This construct is commonly known as a wrapper #ifndef. When the header is included
again, the conditional will be false, because FILE_FOO_SEEN is defined. Preprocessing skips
Chapter 26: Preprocessing 164
over the entire contents of the file, so that compilation will never “see” the file contents
twice in one module.
GCC optimizes this case even further. It remembers when a header file has a wrapper
#ifndef. If a subsequent #include specifies that header, and the macro in the #ifndef is
still defined, it does not bother to rescan the file at all.
You can put comments in the header file outside the wrapper. They do not interfere
with this optimization.
The macro FILE_FOO_SEEN is called the controlling macro or guard macro. In a user
header file, the macro name should not begin with ‘_’. In a system header file, it should
begin with ‘__’ (or ‘_’ followed by an upper-case letter) to avoid conflicts with user programs.
In any kind of header file, the macro name should contain the name of the file and some
additional text, to avoid conflicts with other header files.
#include HEADER
looks for a file named a\"b. Preprocessing searches for the file according to the rules for
double-quoted includes.
If the line expands to a token stream beginning with a ‘<’ token and including a ‘>’ token,
then the tokens between the ‘<’ and the first ‘>’ are combined to form the filename to be
included. Any whitespace between tokens is reduced to a single space; then any space after
the initial ‘<’ is retained, but a trailing space before the closing ‘>’ is ignored. Preprocessing
searches for the file according to the rules for angle-bracket includes.
In either case, if there are any tokens on the line after the file name, an error occurs and
the directive is not processed. It is also an error if the result of expansion does not match
either of the two expected forms.
These rules are implementation-defined behavior according to the C standard. To min-
imize the risk of different compilers interpreting your computed includes differently, we
recommend you use only a single object-like macro that expands to a string constant. That
also makes it clear to people reading your program.
26.5 Macros
A macro is a fragment of code that has been given a name. Whenever the name is used, it
is replaced by the contents of the macro. There are two kinds of macros. They differ mostly
in what they look like when they are used. Object-like macros resemble data objects when
used, function-like macros resemble function calls.
You may define any valid identifier as a macro, even if it is a C keyword. In the
preprocessing stage, GCC does not know anything about keywords. This can be useful if
you wish to hide a keyword such as const from an older compiler that does not understand
it. However, the preprocessing operator defined (see Section 26.6.2.3 [defined], page 185)
can never be defined as a macro.
The preprocessing operator # is used in macros for stringification of an argument (see
Section 26.5.4 [Stringification], page 169), and ## is used for concatenation of arguments
into larger tokens (see Section 26.5.5 [Concatenation], page 171)
The first two pairs of parentheses in this expansion come from the macro. The third is
the pair that was originally after the macro invocation. Since lang_init is an object-like
macro, it does not consume those parentheses.
Any name can have at most one macro definition at a time. Thus, you can’t define the
same name as an object-like macro and a function-like macro at once.
and then to
((((a) < (b) ? (a) : (b))) < (c)
? (((a) < (b) ? (a) : (b)))
: (c))
(The line breaks shown here for clarity are not actually generated.)
You can leave macro arguments empty without error, but many macros will then expand
to invalid code. You cannot leave out arguments entirely; if a macro takes two arguments,
there must be exactly one comma at the top level of its argument list. Here are some silly
examples using min:
min(, b) 7→ (( ) < (b) ? ( ) : (b))
min(a, ) 7 → ((a ) < ( ) ? (a ) : ( ))
min(,) 7 → (( ) < ( ) ? ( ) : ( ))
min((,),) 7 → (((,)) < ( ) ? ((,)) : ( ))
Whitespace is not a preprocessing token, so if a macro foo takes one argument, foo ()
and foo ( ) both supply it an empty argument.
Macro parameters appearing inside string literals are not replaced by their corresponding
actual arguments.
#define foo(x) x, "x"
foo(bar) 7→ bar, "x"
See the next subsection for how to insert macro arguments into a string literal.
The token following the macro call and the last token of the macro expansion do not
become one token even if it looks like they could:
#define foo() abc
foo()def 7→ abc def
26.5.4 Stringification
Sometimes you may want to convert a macro argument into a string constant. Parameters
are not replaced inside string constants, but you can use the # preprocessing operator
instead. When a macro parameter is used with a leading #, preprocessing replaces it with the
literal text of the actual argument, converted to a string constant. Unlike normal parameter
replacement, the argument is not macro-expanded first. This is called stringification.
There is no way to combine an argument with surrounding text and stringify it all
together. But you can write a series of string constants and stringified arguments. Af-
ter preprocessing replaces the stringified arguments with string constants, the consecutive
string constants will be concatenated into one long string constant (see Section 12.7 [String
Constants], page 54).
Here is an example that uses stringification and concatenation of string constants:
Chapter 26: Preprocessing 170
#define WARN_IF(EXP) \
do { if (EXP) \
fprintf (stderr, "Warning: " #EXP "\n"); } \
while (0)
WARN_IF (x == 0);
7→
do { if (x == 0)
fprintf (stderr, "Warning: " "x == 0" "\n"); }
while (0);
The argument for EXP is substituted once, as is, into the if statement, and once, stringified,
into the argument to fprintf. If x were a macro, it would be expanded in the if statement
but not in the string.
The do and while (0) are a kludge to make it possible to write WARN_IF (arg);.
The resemblance of WARN_IF to a function makes that a natural way to write it. See
Section 26.5.10.3 [Swallowing the Semicolon], page 178.
Stringification in C involves more than putting double-quote characters around the frag-
ment. It also backslash-escapes the quotes surrounding embedded string constants, and all
backslashes within string and character constants, in order to get a valid C string constant
with the proper contents. Thus, stringifying p = "foo\n"; results in "p = \"foo\\n\";".
However, backslashes that are not inside string or character constants are not duplicated:
‘\n’ by itself stringifies to "\n".
All leading and trailing whitespace in text being stringified is ignored. Any sequence of
whitespace in the middle of the text is converted to a single space in the stringified result.
Comments are replaced by whitespace long before stringification happens, so they never
appear in stringified text.
There is no way to convert a macro argument into a character constant.
To stringify the result of expansion of a macro argument, you have to use two levels of
macros, like this:
#define xstr(S) str(S)
#define str(s) #s
#define foo 4
str (foo)
7→ "foo"
xstr (foo)
7→ xstr (4)
7→ str (4)
7→ "4"
s is stringified when it is used in str, so it is not macro-expanded first. But S is
an ordinary argument to xstr, so it is completely macro-expanded before xstr itself is
expanded (see Section 26.5.10.7 [Argument Prescan], page 181). Therefore, by the time str
gets to its argument text, that text already been macro-expanded.
Chapter 26: Preprocessing 171
26.5.5 Concatenation
It is often useful to merge two tokens into one while expanding macros. This is called token
pasting or token concatenation. The ## preprocessing operator performs token pasting.
When a macro is expanded, the two tokens on either side of each ## operator are combined
into a single token, which then replaces the ## and the two original tokens in the macro
expansion. Usually both will be identifiers, or one will be an identifier and the other a
preprocessing number. When pasted, they make a longer identifier.
Concatenation into an identifier isn’t the only valid case. It is also possible to concatenate
two numbers (or a number and a name, such as 1.5 and e3) into a number. Also, multi-
character operators such as += can be formed by token pasting.
However, two tokens that don’t together form a valid token cannot be pasted together.
For example, you cannot concatenate x with +, not in either order. Trying this issues a
warning and keeps the two tokens separate. Whether it puts white space between the tokens
is undefined. It is common to find unnecessary uses of ## in complex macros. If you get
this warning, it is likely that you can simply remove the ##.
The tokens combined by ## could both come from the macro body, but then you could
just as well write them as one token in the first place. Token pasting is useful when one
or both of the tokens comes from a macro argument. If either of the tokens next to an
## is a parameter name, it is replaced by its actual argument before ## executes. As with
stringification, the actual argument is not macro-expanded first. If the argument is empty,
that ## has no effect.
Keep in mind that preprocessing converts comments to whitespace before it looks for
uses of macros. Therefore, you cannot create a comment by concatenating ‘/’ and ‘*’. You
can put as much whitespace between ## and its operands as you like, including comments,
and you can put comments in arguments that will be concatenated.
It is an error to use ## at the beginning or end of a macro body.
Multiple ## preprocessing operators are handled left-to-right, so that ‘1 ## e ## -2’
pastes into ‘1e-2’. (Right-to-left processing would first generate ‘e-2’, which is an invalid
token.) When # and ## are used together, they are all handled left-to-right.
Consider a C program that interprets named commands. There probably needs to be a
table of commands, perhaps an array of structures declared as follows:
struct command
{
char *name;
void (*function) (void);
};
argument can make this unnecessary. It can create the string constant with stringification,
and the function name by concatenating the argument with ‘_command’. Here is how it is
done:
#define COMMAND(NAME) { #NAME, NAME ## _command }
To get rid of that comma, the ## token paste operator has a special meaning when placed
between a comma and a variable argument.2 If you write
#define eprintf(format, ...) \
fprintf (stderr, format, ##__VA_ARGS__)
then use the macro eprintf with empty variable arguments, ## deletes the preceding
comma.
eprintf ("success!\n")
7→ fprintf(stderr, "success!\n")
This does not happen if you pass an empty argument, nor does it happen if the token
preceding ## is anything other than a comma.
When the only macro parameter is a variable arguments parameter, and the macro call has
no argument at all, it is not obvious whether that means an empty argument or a missing
argument. Should the comma be kept, or deleted? The C standard says to keep the comma,
but the preexisting GNU C extension deleted the comma. Nowadays, GNU C retains the
comma when implementing a specific C standard, and deletes it otherwise.
C99 mandates that the only place the identifier __VA_ARGS__ can appear is in the re-
placement list of a variadic macro. It may not be used as a macro name, macro parameter
name, or within a different type of macro. It may also be forbidden in open text; the
standard is ambiguous. We recommend you avoid using that name except for its special
purpose.
Variadic macros where you specify the parameter name is a GNU C feature that has
been supported for a long time. Standard C, as of C99, supports only the form where the
parameter is called __VA_ARGS__. For portability to previous versions of GNU C you should
use only named variable argument parameters. On the other hand, for portability to other
C99 compilers, you should use only __VA_ARGS__.
__FUNCTION__ is the name that has been defined in GNU C since time immemo-
rial; __func__ is defined by the C standard. With the following conditionals,
you can use whichever one is defined.
#if __STDC_VERSION__ < 199901L
# if __GNUC__ >= 2
# define __func__ __FUNCTION__
# else
# define __func__ "<unknown>"
# endif
#endif
__PRETTY_FUNCTION__
This is equivalent to __FUNCTION__ in C, but in C++ the string includes argu-
ment type information as well. It is a GNU C extension.
Those features are useful in generating an error message to report an inconsistency
detected by the program; the message can state the source line where the inconsistency was
detected. For example,
fprintf (stderr, "Internal error: "
"negative string length "
"in function %s "
"%d at %s, line %d.",
__func__, length, __FILE__, __LINE__);
A #line directive changes __LINE__, and may change __FILE__ as well. See Section 26.8
[Line Control], page 187.
__DATE__ This macro expands to a string constant that describes the date of compilation.
The string constant contains eleven characters and looks like "Feb 12 1996".
If the day of the month is just one digit, an extra space precedes it so that the
date is always eleven characters.
If the compiler cannot determine the current date, it emits a warning messages
(once per compilation) and __DATE__ expands to "??? ?? ????".
We deprecate the use of __DATE__ for the sake of reproducible compilation.
__TIME__ This macro expands to a string constant that describes the time of compilation.
The string constant contains eight characters and looks like "23:59:01".
If the compiler cannot determine the current time, it emits a warning message
(once per compilation) and __TIME__ expands to "??:??:??".
We deprecate the use of __TIME__ for the sake of reproducible compilation.
__STDC__ In normal operation, this macro expands to the constant 1, to signify that this
compiler implements ISO Standard C.
__STDC_VERSION__
This macro expands to the C Standard’s version number, a long integer con-
stant of the form yyyymmL where yyyy and mm are the year and month of the
Standard version. This states which version of the C Standard the compiler
implements.
The current default value is 201112L, which signifies the C 2011 standard.
Chapter 26: Preprocessing 175
__STDC_HOSTED__
This macro is defined, with value 1, if the compiler’s target is a hosted envi-
ronment. A hosted environment provides the full facilities of the standard C
library.
The rest of the predefined macros are GNU C extensions.
__COUNTER__
This macro expands to sequential integral values starting from 0. In other
words, each time the program uses this macro, it generates the next successive
integer. This, with the ## preprocessing operator, provides a convenient means
for macros to generate unique identifiers.
__GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__
These macros expand to the major version, minor version, and patch level of
the compiler, as integer constants. For example, GCC 3.2.1 expands __GNUC__
to 3, __GNUC_MINOR__ to 2, and __GNUC_PATCHLEVEL__ to 1.
If all you need to know is whether or not your program is being compiled by
GCC, or a non-GCC compiler that claims to accept the GNU C extensions,
you can simply test __GNUC__. If you need to write code that depends on
a specific version, you must check more carefully. Each change in the minor
version resets the patch level to zero; each change in the major version (which
happens rarely) resets the minor version and the patch level to zero. To use
the predefined macros directly in the conditional, write it like this:
/* Test for version 3.2.0 or later. */
#if __GNUC__ > 3 || \
(__GNUC__ == 3 && (__GNUC_MINOR__ > 2 || \
(__GNUC_MINOR__ == 2 && \
__GNUC_PATCHLEVEL__ > 0))
Another approach is to use the predefined macros to calculate a single number,
then compare that against a threshold:
#define GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
/* . . . */
/* Test for GCC > 3.2.0 */
#if GCC_VERSION > 30200
Many people find this form easier to understand.
__VERSION__
This macro expands to a string constant that describes the version of the com-
piler in use. You should not rely on its contents’ having any particular form,
but you can count on it to contain at least the release number.
__TIMESTAMP__
This macro expands to a string constant that describes the date and time of
the last modification of the current source file. The string constant contains
Chapter 26: Preprocessing 176
abbreviated day of the week, month, day of the month, time in hh:mm:ss form,
and the year, in the format "Sun Sep 16 01:03:52 1973". If the day of the
month is less than 10, it is padded with a space on the left.
If GCC cannot determine that information date, it emits a warn-
ing message (once per compilation) and __TIMESTAMP__ expands to
"??? ??? ?? ??:??:?? ????".
We deprecate the use of this macro for the sake of reproducible compilation.
A paradoxical case is to redefine a macro within the call to that same macro. What
happens is, the new definition takes effect in time for pre-expansion of all the arguments,
then the original definition is expanded to replace the call. Here is a pathological example:
#define f(x) x x
f (first f second
#undef f
#define f 2
f)
which expands to
first 2 second 2 first 2 second 2
with the semantics described above. We suggest you avoid writing code which does this
sort of thing.
26.5.10.1 Misnesting
When a macro is called with arguments, the arguments are substituted into the macro body
and the result is checked, together with the rest of the input file, for more macro calls. It is
possible to piece together a macro call coming partially from the macro body and partially
from the arguments. For example,
#define twice(x) (2*(x))
#define call_with_1(x) x(1)
call_with_1 (twice)
7→ twice(1)
7→ (2*(1))
Macro definitions do not have to have balanced parentheses. By writing an unbalanced
open parenthesis in a macro body, it is possible to create a macro call that begins inside
the macro body but ends outside of it. For example,
#define strange(file) fprintf (file, "%s %d",
/* . . . */
strange(stderr) p, 35)
7→ fprintf (stderr, "%s %d", p, 35)
The ability to piece together a macro call can be useful, but the use of unbalanced open
parentheses in a macro body is just confusing, and should be avoided.
whose purpose is to divide, rounding up. (One use for this operation is to compute how
many int objects are needed to hold a certain number of char objects.) Then suppose it
is used as follows:
a = ceil_div (b & c, sizeof (int));
7→ a = (b & c + sizeof (int) - 1) / sizeof (int);
This does not do what is intended. The operator-precedence rules of C make it equivalent
to this:
a = (b & (c + sizeof (int) - 1)) / sizeof (int);
What we want is this:
a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
Defining the macro as
#define ceil_div(x, y) ((x) + (y) - 1) / (y)
provides the desired result.
Unintended grouping can result in another way. Consider sizeof ceil_div(1, 2). That
has the appearance of a C expression that would compute the size of the type of ceil_div
(1, 2), but in fact it means something very different. Here is what it expands to:
sizeof ((1) + (2) - 1) / (2)
This would take the size of an integer and divide it by two. The precedence rules have put
the division outside the sizeof when it was intended to be inside.
Parentheses around the entire macro definition prevent such problems. Here, then, is
the recommended way to define ceil_div:
#define ceil_div(x, y) (((x) + (y) - 1) / (y))
else /* . . . */
The presence of two statements—the compound statement and a null statement—in between
the if condition and the else makes invalid C code.
The definition of the macro SKIP_SPACES can be altered to solve this problem, using a
do . . . while statement. Here is how:
#define SKIP_SPACES(p, limit) \
do { char *lim = (limit); \
while (p < lim) { \
if (*p++ != ' ') { \
p--; break; }}} \
while (0)
Now SKIP_SPACES (p, lim); expands into
do { /* . . . */ } while (0);
which is one statement. The loop executes exactly once; most compilers generate no extra
code for it.
When the repeated value appears as the condition of the ?: operator and again as its
iftrue expression, you can avoid repeated execution by omitting the iftrue expression, like
this:
#define x_or_y(X, Y) ((X) ? : (Y))
In GNU C, this expands to use the first macro argument’s value if that isn’t zero. If that’s
zero, it compiles the second argument and uses that value. See Section 8.4 [Conditional
Expression], page 35.
reader will come across the identifier foo in the program and think its value should be that
of the variable foo, whereas in fact the value is four greater.
It is useful to make a macro definition that expands to the macro name itself. If you
write
#define EPERM EPERM
then the macro EPERM expands to EPERM. Effectively, preprocessing leaves it unchanged in
the source code. You can tell that it’s a macro with #ifdef. You might do this if you want
to define numeric constants with an enum, but have #ifdef be true for each constant.
If a macro x expands to use a macro y, and the expansion of y refers to the macro x,
that is an indirect self-reference of x. x is not expanded in this case either. Thus, if we have
#define x (4 + y)
#define y (2 * x)
then x and y expand as follows:
x 7→ (4 + y)
7→ (4 + (2 * x))
y 7→ (2 * x)
7 → (2 * (4 + y))
Each macro is expanded when it appears in the definition of the other macro, but not when
it indirectly appears in its own definition.
happen. Without the prescan, f (1) itself would be substituted as an argument, and
the inner use of f would appear during the main scan as an indirect self-reference and
would not be expanded.
• Macros that call other macros that stringify or concatenate.
If an argument is stringified or concatenated, the prescan does not occur. If you want
to expand a macro, then stringify or concatenate its expansion, you can do that by
causing one macro to call another macro that does the stringification or concatenation.
For instance, if you have
#define AFTERX(x) X_ ## x
#define XAFTERX(x) AFTERX(x)
#define TABLESIZE 1024
#define BUFSIZE TABLESIZE
then AFTERX(BUFSIZE) expands to X_BUFSIZE, and XAFTERX(BUFSIZE) expands to X_
1024. (Not to X_TABLESIZE. Prescan always does a complete expansion.)
• Macros used in arguments, whose expansions contain unshielded commas.
This can cause a macro expanded on the second scan to be called with the wrong
number of arguments. Here is an example:
#define foo a,b
#define bar(x) lose(x)
#define lose(x) (1 + (x))
We would like bar(foo) to turn into (1 + (foo)), which would then turn into (1 +
(a,b)). Instead, bar(foo) expands into lose(a,b), which gives an error because
lose requires a single argument. In this case, the problem is easily solved by the same
parentheses that ought to be used to prevent misnesting of arithmetic operations:
#define foo (a,b)
or
#define bar(x) lose((x))
The extra pair of parentheses prevents the comma in foo’s definition from being inter-
preted as an argument separator.
26.6 Conditionals
A conditional is a preprocessing directive that controls whether or not to include a chunk
of code in the final token stream that is compiled. Preprocessing conditionals can test
arithmetic expressions, or whether a name is defined as a macro, or both together using the
special defined preprocessing operator.
A preprocessing conditional in C resembles in some ways an if statement in C, but it is
important to understand the difference between them. The condition in an if statement is
tested during the execution of your program. Its purpose is to allow your program to behave
differently from run to run, depending on the data it is operating on. The condition in a
preprocessing conditional directive is tested when your program is compiled. Its purpose
is to allow different code to be included in the program depending on the situation at the
time of compilation.
Sometimes this distinction makes no practical difference. GCC and other modern com-
pilers often do test if statements when a program is compiled, if their conditions are known
Chapter 26: Preprocessing 183
not to vary at run time, and eliminate code that can never be executed. If you can count
on your compiler to do this, you may find that your program is more readable if you use if
statements with constant conditions (perhaps determined by macros). Of course, you can
only use this to exclude code, not type definitions or other preprocessing directives, and
you can only do it if the file remains syntactically valid when that code is not used.
controlled text
#endif /* MACRO */
This block is called a conditional group. The body, controlled text, will be included
in compilation if and only if MACRO is defined. We say that the conditional succeeds if
MACRO is defined, fails if it is not.
The controlled text inside a conditional can include preprocessing directives. They are
executed only if the conditional succeeds. You can nest conditional groups inside other
conditional groups, but they must be completely nested. In other words, #endif always
matches the nearest #ifdef (or #ifndef, or #if). Also, you cannot start a conditional
group in one file and end it in another.
Even if a conditional fails, the controlled text inside it is still run through initial trans-
formations and tokenization. Therefore, it must all be lexically valid C. Normally the only
way this matters is that all comments and string literals inside a failing conditional group
must still be properly ended.
Chapter 26: Preprocessing 184
The comment following the #endif is not required, but it is a good practice if there
is a lot of controlled text, because it helps people match the #endif to the corresponding
#ifdef.
Older programs sometimes put macro directly after the #endif without enclosing it in
a comment. This is invalid code according to the C standard, but it only causes a warning
in GNU C. It never affects which #ifndef the #endif matches.
Sometimes you wish to use some code if a macro is not defined. You can do this by
writing #ifndef instead of #ifdef. One common use of #ifndef is to include code only
the first time a header file is included. See Section 26.4.4 [Once-Only Headers], page 163.
Macro definitions can vary between compilations for several reasons. Here are some
samples.
• Some macros are predefined on each kind of machine (see Section “System-specific
Predefined Macros” in The C Preprocessor). This allows you to provide code specially
tuned for a particular machine.
• System header files define more macros, associated with the features they implement.
You can test these macros with conditionals to avoid using a system feature on a
machine where it is not implemented.
• Macros can be defined or undefined with the -D and -U command-line options when
you compile the program. You can arrange to compile the same source file into two
different programs by choosing a macro name to specify which program you want,
writing conditionals to test whether or how this macro is defined, and then controlling
the state of the macro with command-line options, perhaps set in the file Makefile.
See Section “Invoking GCC” in Using the GNU Compiler Collection.
• Your program might have a special header file (often called config.h) that is adjusted
when the program is compiled. It can define or not define macros depending on the
features of the system and the desired capabilities of the program. The adjustment can
be automated by a tool such as autoconf, or done by hand.
controlled text
#endif /* expression */
expression is a C expression of integer type, subject to stringent restrictions so its value
can be computed at compile time. It may contain
• Integer constants.
• Character constants, which are interpreted as they would be in normal code.
• Arithmetic operators for addition, subtraction, multiplication, division, bitwise opera-
tions, shifts, comparisons, and logical operations (&& and ||). The latter two obey the
usual short-circuiting rules of standard C.
Chapter 26: Preprocessing 185
• Macros. All macros in the expression are expanded before actual computation of the
expression’s value begins.
• Uses of the defined preprocessing operator, which lets you check whether macros are
defined in the middle of an #if.
• Identifiers that are not macros, which are all considered to be the number zero. This
allows you to write #if MACRO instead of #ifdef MACRO, if you know that MACRO,
when defined, will always have a nonzero value. Function-like macros used without
their function call parentheses are also treated as zero.
In some contexts this shortcut is undesirable. The -Wundef requests warnings for any
identifier in an #if that is not defined as a macro.
Preprocessing does not know anything about the data types of C. Therefore, sizeof
operators are not recognized in #if; sizeof is simply an identifier, and if it is not a macro,
it stands for zero. This is likely to make the expression invalid. Preprocessing does not
recognize enum constants; they too are simply identifiers, so if they are not macros, they
stand for zero.
Preprocessing calculates the value of expression, and carries out all calculations in the
widest integer type known to the compiler; on most machines supported by GNU C this is
64 bits. This is not the same rule as the compiler uses to calculate the value of a constant
expression, and may give different results in some cases. If the value comes out to be
nonzero, the #if succeeds and the controlled text is compiled; otherwise it is skipped.
#if MACRO_DEFINED(BUFSIZE)
which would expand the #if expression to:
#if defined BUFSIZE
Generating defined in this way is a GNU C extension.
Chapter 26: Preprocessing 186
nest, so the first comment inside the old code will end the commenting-out. The probable
result is a flood of syntax errors.
One way to avoid this problem is to use an always-false conditional instead. For instance,
put #if 0 before the deleted code and #endif after it. This works even if the code being
turned off contains conditionals, but they must be entire conditionals (balanced #if and
#endif).
Some people use #ifdef notdef instead. This is risky, because notdef might be acci-
dentally defined as a macro, and then the conditional would succeed. #if 0 can be counted
on to fail.
Do not use #if 0 around text that is not C code. Use a real comment, instead. The
interior of #if 0 must consist of complete tokens; in particular, single-quote characters must
balance. Comments often contain unbalanced single-quote characters (known in English as
apostrophes). These confuse #if 0. They don’t confuse ‘/*’.
26.7 Diagnostics
The directive #error reports a fatal error. The tokens forming the rest of the line following
#error are used as the error message.
The usual place to use #error is inside a conditional that detects a combination of
parameters that you know the program does not properly support. For example,
#if !defined(UNALIGNED_INT_ASM_OP) && defined(DWARF2_DEBUGGING_INFO)
#error "DWARF2_DEBUGGING_INFO requires UNALIGNED_INT_ASM_OP."
#endif
The directive #warning is like #error, but it reports a warning instead of an error. The
tokens following #warning are used as the warning message.
You might use #warning in obsolete header files, with a message saying which header
file to use instead.
Neither #error nor #warning macro-expands its argument. Internal whitespace se-
quences are each replaced with a single space. The line must consist of complete tokens. It
is wisest to make the argument of these directives be a single string constant; this avoids
problems with apostrophes and the like.
that file, which is the real source code. To make that happen, Bison generates line-control
directives that the C compiler understands.
#line is a directive that specifies the original line number and source file name for
subsequent code. #line has three variants:
#line linenum
linenum is a non-negative decimal integer constant. It specifies the line number
that should be reported for the following line of input. Subsequent lines are
counted from linenum.
#line linenum filename
linenum is the same as for the first form, and has the same effect. In addition,
filename is a string constant that specifies the source file name. Subsequent
source lines are recorded as coming from that file, until something else happens
to change that. filename is interpreted according to the normal rules for a string
constant. Backslash escapes are interpreted, in contrast to #include.
#line anything else
anything else is checked for macro calls, which are expanded. The result should
match one of the above two forms.
#line directives alter the results of the __FILE__ and __LINE__ symbols from that point
on. See Section 26.5.7 [Predefined Macros], page 173.
27 Integers in Depth
This chapter explains the machine-level details of integer types: how they are represented
as bits in memory, and the range of possible values for each integer type.
1
In theory, any of these types could have some other size, bit it’s not worth even a minute to cater to
that possibility. It never happens on GNU/Linux.
Chapter 27: Integers in Depth 190
FLT_MAX
DBL_MAX
LDBL_MAX Defines the largest values that can be represented with the type.
FLT_DECIMAL_DIG
DBL_DECIMAL_DIG
LDBL_DECIMAL_DIG
Defines the number of decimal digits n such that any floating-point number that
can be represented in the type can be rounded to a floating-point number with
n decimal digits, and back again, without losing any precision of the value.
zero as the result, and possibly to report the underflow in some sort of program
output.
The IEEE 754 Standard is vague about whether rounding happens before de-
tection of floating underflow and overflow, or after, and CPU designers may
choose either.
However, the Standard does something unusual compared to earlier designs,
and that is that when the result is smaller than the smallest normalized repre-
sentable value (i.e., one in which the leading significand bit is 1), the normal-
ization requirement is relaxed, leading zero bits are permitted, and precision is
gradually lost until there are no more bits in the significand. That phenomenon
is called gradual underflow, and it serves important numerical purposes, al-
though it does reduce the precision of the final result. Some floating-point
designs allow you to choose at compile time, or even at run time, whether
underflows are gradual, or are flushed abruptly to zero. Numbers that have
entered the region of gradual underflow are called subnormal.
You can use the library functions fesetround and fegetround to set and get
the rounding mode. Rounding modes are defined (if supported by the platform)
in fenv.h as: FE_UPWARD to round toward positive infinity; FE_DOWNWARD to
round toward negative infinity; FE_TOWARDZERO to round toward zero; and FE_
TONEAREST to round to the nearest representable value, the default mode. It is
best to use FE_TONEAREST except when there is a special need for some other
mode.
The conditions include invalid operand, division by zero, inexact result (i.e., one that
required rounding), underflow, and overflow. Some extended floating-point designs of-
fer several additional exception flags. The functions feclearexcept, feraiseexcept,
fetestexcept, fegetexceptflags, and fesetexceptflags provide a standardized inter-
face to those flags. See Section “Status bit operations” in The GNU C Library Reference
Manual.
One important use of those flags is to do a computation that is normally expected to be
exact in floating-point arithmetic, but occasionally might not be, in which case, corrective
action is needed. You can clear the inexact result flag with a call to feclearexcept (FE_
INEXACT), do the computation, and then test the flag with fetestexcept (FE_INEXACT);
the result of that call is 0 if the flag is not set (there was no rounding), and 1 when there
was rounding (which, we presume, implies the program has to correct for that).
28.7 Rounding
When floating-point arithmetic produces a result that can’t fit exactly in the significand
of the type that’s in use, it has to round the value. The basic arithmetic operations—
addition, subtraction, multiplication, division, and square root—always produce a result
that is equivalent to the exact, possibly infinite-precision result rounded to storage precision
according to the current rounding rule.
Rounding sets the FE_INEXACT exception flag (see Section 28.5 [Exception Flags],
page 193). This enables programs to determine that rounding has occurred.
Rounding consists of adjusting the exponent to bring the significand back to the required
base-point alignment, then applying the current rounding rule to squeeze the significand
into the fixed available size.
The current rule is selected at run time from four options. Here they are:
* round-to-nearest, with ties rounded to an even integer;
* round-up, towards +Infinity;
* round-down, towards -Infinity;
* round-towards-zero.
Under those four rounding rules, a decimal value -1.2345 that is to be rounded to a
four-digit result would become -1.234, -1.234, -1.235, and -1.234, respectively.
Chapter 28: Floating Point in Depth 195
The default rounding rule is round-to-nearest, because that has the least bias, and pro-
duces the lowest average error. When the true result lies exactly halfway between two
representable machine numbers, the result is rounded to the one that ends with an even
digit.
The round-towards-zero rule was common on many early computer designs, because it
is the easiest to implement: it just requires silent truncation of all extra bits.
The two other rules, round-up and round-down, are essential for implementing interval
arithmetic, whereby each arithmetic operation produces lower and upper bounds that are
guaranteed to enclose the exact result.
See Section 28.17 [Rounding Control], page 202, for details on getting and setting the
current rounding mode.
After 50 iterations, y has barely one correct digit, and soon after, there are no correct
digits.
x = 20 k = 81
s (x) = 0.912_945_250_749_573
sin (x) = 0.912_945_250_727_628
x = 30 k = 109
s (x) = -0.987_813_746_058_855
sin (x) = -0.988_031_624_092_862
x = 40 k = 137
s (x) = 0.617_400_430_980_474
sin (x) = 0.745_113_160_479_349
x = 50 k = 159
s (x) = 57_105.187_673_745_720_532
sin (x) = -0.262_374_853_703_929
x = 10 k = 47
t (x) = 1.101_323_287_470_340e+04
sinh (x) = 1.101_323_287_470_339e+04
x = 20 k = 69
t (x) = 2.425_825_977_048_951e+08
sinh (x) = 2.425_825_977_048_951e+08
Chapter 28: Floating Point in Depth 197
x = 30 k = 87
t (x) = 5.343_237_290_762_229e+12
sinh (x) = 5.343_237_290_762_231e+12
x = 40 k = 105
t (x) = 1.176_926_334_185_100e+17
sinh (x) = 1.176_926_334_185_100e+17
x = 50 k = 121
t (x) = 2.592_352_764_293_534e+21
sinh (x) = 2.592_352_764_293_536e+21
We have added underscores to the numbers to enhance readability.
The sinh (x) series with positive terms can be summed to high accuracy. By con-
trast, the series for sin (x) suffers increasing significance loss, so that when x = 30 only
two correct digits remain. Soon after, all digits are wrong, and the answers are complete
nonsense.
An important skill in numerical programming is to recognize when significance loss is
likely to contaminate a computation, and revise the algorithm to reduce this problem.
Sometimes, the only practical way to do so is to compute in higher intermediate precision,
which is why the extended types like long double are important.
x = -1.0 / 0.0;
Chapter 28: Floating Point in Depth 200
In general, when function gets a NaN argument, it usually returns a NaN. However, there
are some exceptions in the math-library functions that you need to be aware of, because
they violate the NaNs-always-propagate rule:
• pow (x, 0.0) always returns 1.0, even if x is 0.0, Infinity, or a NaN.
• pow (1, y) always returns 1, even if y is a NaN.
• hypot (INFINITY, y) and hypot (-INFINITY, y) both always return INFINITY, even
if y is a Nan.
• If just one of the arguments to fmax (x, y) or fmin (x, y) is a NaN, it returns the other
argument. If both arguments are NaNs, it returns a NaN, but there is no requirement
about where it comes from: it could be x, or y, or some other quiet NaN.
NaNs are also used for the return values of math-library functions where the result is
not representable in real arithmetic, or is mathematically undefined or uncertain, such as
sqrt (-1.0) and sin (Infinity). However, note that a result that is merely too big to
represent should always produce an Infinity, such as with exp (1000.0) (too big) and exp
(Infinity) (truly infinite).
if (fesetround (FE_UPWARD) == 0)
{
v.hi = x + y;
v.lo = -(-x - y);
}
else
fatal ("ERROR: failed to change rounding rule");
if (fesetround (rule) != 0)
fatal ("ERROR: failed to restore rounding rule");
}
The volatile qualifier (see Section 21.2 [volatile], page 127) is essential on x86 platforms
to prevent an optimizing compiler from producing the same value for both bounds.
A measure of the precision is the answer to the question: what is the smallest number
that can be added to 1.0 such that the sum differs from 1.0? That number is called the
machine epsilon.
We could define the needed machine-epsilon constants for float, double, and long
double like this:
static const float epsf = 0x1p-23; /* about 1.192e-07 */
static const double eps = 0x1p-52; /* about 2.220e-16 */
static const long double epsl = 0x1p-63; /* about 1.084e-19 */
Instead of the hexadecimal constants, we could also have used the Standard C macros,
FLT_EPSILON, DBL_EPSILON, and LDBL_EPSILON.
It is useful to be able to compute the machine epsilons at run time, and we can easily
generalize the operation by replacing the constant 1.0 with a user-supplied value:
double
macheps (double x)
{ /* Return machine epsilon for x, */
/* such that x + macheps (x) > x. */
static const double base = 2.0;
double eps;
if (isnan (x))
eps = x;
else
{
eps = (x == 0.0) ? 1.0 : x;
return (eps);
}
If we call that function with arguments from 0 to 10, as well as Infinity and NaN, and print
the returned values in hexadecimal, we get output like this:
macheps ( 0) = 0x1.0000000000000p-1074
macheps ( 1) = 0x1.0000000000000p-52
macheps ( 2) = 0x1.0000000000000p-51
macheps ( 3) = 0x1.8000000000000p-52
macheps ( 4) = 0x1.0000000000000p-50
macheps ( 5) = 0x1.4000000000000p-51
macheps ( 6) = 0x1.8000000000000p-51
macheps ( 7) = 0x1.c000000000000p-51
macheps ( 8) = 0x1.0000000000000p-49
macheps ( 9) = 0x1.2000000000000p-50
macheps ( 10) = 0x1.4000000000000p-50
macheps (Inf) = infinity
Chapter 28: Floating Point in Depth 204
The first important point is that, unlike real arithmetic, in complex arithmetic, the
danger of significance loss is pervasive, and affects every one of the basic operations, and
almost all of the math-library functions. To understand why, recall the rules for complex
multiplication and division:
a = u + I*v /* First operand. */
b = x + I*y /* Second operand. */
prod = a * b
= (u + I*v) * (x + I*y)
= (u * x - v * y) + I*(v * x + u * y)
quo = a / b
= (u + I*v) / (x + I*y)
= [(u + I*v) * (x - I*y)] / [(x + I*y) * (x - I*y)]
= [(u * x + v * y) + I*(v * x - u * y)] / (x**2 + y**2)
There are four critical observations about those formulas:
• the multiplications on the right-hand side introduce the possibility of premature un-
derflow or overflow;
• the products must be accurate to twice working precision;
• there is always one subtraction on the right-hand sides that is subject to catastrophic
significance loss; and
• complex multiplication has up to six rounding errors, and complex division has ten
rounding errors.
Another point that needs careful study is the fact that many functions in complex
arithmetic have branch cuts. You can view a function with a complex argument, f (z), as
f (x + I*y), and thus, it defines a relation between a point (x, y) on the complex plane
with an elevation value on a surface. A branch cut looks like a tear in that surface, so
approaching the cut from one side produces a particular value, and from the other side,
a quite different value. Great care is needed to handle branch cuts properly, and even
small numerical errors can push a result from one side to the other, radically changing the
returned value. As we reported earlier, correct handling of the sign of zero is critically
important for computing near branch cuts.
The best advice that we can give to programmers who need complex arithmetic is to
always use the highest precision available, and then to carefully check the results of test
calculations to gauge the likely accuracy of the computed results. It is easy to supply test
values of real and imaginary parts where all five basic operations in complex arithmetic,
and almost all of the complex math functions, lose all significance, and fail to produce even
a single correct digit.
Even though complex arithmetic makes some programming tasks easier, it may be nu-
merically preferable to rework the algorithm so that it can be carried out in real arithmetic.
That is commonly possible in matrix algebra.
GNU C can perform code optimization on complex number multiplication and division
if certain boundary checks will not be needed. The command-line option -fcx-limited-
range tells the compiler that a range reduction step is not needed when performing complex
Chapter 28: Floating Point in Depth 206
division, and that there is no need to check if a complex multiplication or division results
in the value Nan + I*NaN. By default these checks are enabled. You can explicitly enable
them with the -fno-cx-limited-range option.
int
goldberg(int ndec)
{ /* Return output bits needed for ndec-digits input. */
return ((int)ceil((double)ndec / log10(2.0) + 1.0));
}
One significant observation from those numbers is that we cannot achieve correct round-
trip conversion between the decimal and binary formats in the same storage size! For
example, we need 25 bits to represent a 7-digit value from the 32-bit decimal format, but
the binary format only has 24 available. Similar observations hold for each of the other
conversion pairs.
The general input/output base-conversion problem is astonishingly complicated, and
solutions were not generally known until the publication of two papers in 1990 that are
listed later near the end of this chapter. For the 128-bit formats, the worst case needs more
than 11,500 decimal digits of precision to guarantee correct rounding in a binary-to-decimal
conversion!
For further details see the references for Bennett Goldberg and David Matula.
references that we recommend for further reading, and for finding other important material
about computer arithmetic.
We include URLs for these references when we were given them, when they are morally
legitimate to recommend; we have omitted the URLs that are paywalled or that require run-
ning nonfree JavaScript code in order to function. We hope you can find morally legitimate
sites where you can access these works.
• Paul H. Abbott and 15 others, Architecture and software support in IBM S/390 Parallel
Enterprise Servers for IEEE Floating-Point arithmetic, IBM Journal of Research and
Development 43(5/6) 723–760 (1999), This article gives a good description of IBM’s al-
gorithm for exact decimal-to-binary conversion, complementing earlier ones by Clinger
and others.
• Nelson H. F. Beebe, The Mathematical-Function Computation Handbook: Program-
ming Using the MathCW Portable Software Library, Springer (2017). This book de-
scribes portable implementations of a large superset of the mathematical functions
available in many programming languages, extended to a future 256-bit format (70
decimal digits), for both binary and decimal floating point. It includes a substan-
tial portion of the functions described in the famous NIST Handbook of Mathemati-
cal Functions, Cambridge (2018), ISBN 0-521-19225-0. See https://www.math.utah.
edu/pub/mathcw/ for compilers and libraries.
• William D. Clinger, How to Read Floating Point Numbers Accurately, ACM SIGPLAN
Notices 25(6) 92–101 (June 1990), https://doi.org/10.1145/93548.93557. See also
the papers by Steele & White.
• I. Bennett Goldberg, 27 Bits Are Not Enough For 8-Digit Accuracy, Communications
of the ACM 10(2) 105–106 (February 1967), https://doi.acm.org/10.1145/363067.
363112. This paper, and its companions by David Matula, address the base-conversion
problem, and show that the naive formulas are wrong by one or two digits.
• David Goldberg, What Every Computer Scientist Should Know About Floating-Point
Arithmetic, ACM Computing Surveys 23(1) 5–58 (March 1991), corrigendum 23(3) 413
(September 1991), https://doi.org/10.1145/103162.103163. This paper has been
widely distributed, and reissued in vendor programming-language documentation. It
is well worth reading, and then rereading from time to time.
• Norbert Juffa and Nelson H. F. Beebe, A Bibliography of Publications on Floating-
Point Arithmetic, https://www.math.utah.edu/pub/tex/bib/fparith.bib. This is
the largest known bibliography of publications about floating-point, and also integer,
arithmetic. It is actively maintained, and in mid 2019, contains more than 6400 refer-
ences to original research papers, reports, theses, books, and Web sites on the subject
matter. It can be used to locate the latest research in the field, and the historical
coverage dates back to a 1726 paper on signed-digit arithmetic, and an 1837 paper by
Charles Babbage, the intellectual father of mechanical computers. The entries for the
Abbott, Clinger, and Steele & White papers cited earlier contain pointers to several
other important related papers on the base-conversion problem.
• William Kahan, Branch Cuts for Complex Elementary Functions, or Much Ado About
Nothing’s Sign Bit, (1987), https://people.freebsd.org/~das/kahan86branch.
pdf. This Web document about the fine points of complex arithmetic also appears
in the volume edited by A. Iserles and M. J. D. Powell, The State of the Art in Nu-
Chapter 28: Floating Point in Depth 208
merical Analysis: Proceedings of the Joint IMA/SIAM Conference on the State of the
Art in Numerical Analysis held at the University of Birmingham, 14–18 April 1986,
Oxford University Press (1987), ISBN 0-19-853614-3 (xiv + 719 pages). Its author is
the famous chief architect of the IEEE 754 arithmetic system, and one of the world’s
greatest experts in the field of floating-point arithmetic. An entire generation of his
students at the University of California, Berkeley, have gone on to careers in academic
and industry, spreading the knowledge of how to do floating-point arithmetic right.
• Donald E. Knuth, A Simple Program Whose Proof Isn’t, in Beauty is our business: a
birthday salute to Edsger W. Dijkstra, W. H. J. Feijen, A. J. M. van Gasteren, D. Gries,
and J. Misra (eds.), Springer (1990), ISBN 1-4612-8792-8, This book chapter supplies
a correctness proof of the decimal to binary, and binary to decimal, conversions in
fixed-point arithmetic in the TeX typesetting system. The proof evaded its author for
a dozen years.
• David W. Matula, In-and-out conversions, Communications of the ACM 11(1) 57–50
(January 1968), https://doi.org/10.1145/362851.362887.
• David W. Matula, The Base Conversion Theorem, Proceedings of the American Math-
ematical Society 19(3) 716–723 (June 1968). See also other papers here by this author,
and by I. Bennett Goldberg.
• David W. Matula, A Formalization of Floating-Point Numeric Base Conversion, IEEE
Transactions on Computers C-19(8) 681–692 (August 1970),
• Jean-Michel Muller and eight others, Handbook of Floating-Point Arithmetic,
Birkhäuser-Boston (2010), ISBN 0-8176-4704-X (xxiii + 572 pages). This is a
comprehensive treatise from a French team who are among the world’s greatest
experts in floating-point arithmetic, and among the most prolific writers of research
papers in that field. They have much to teach, and their book deserves a place on the
shelves of every serious numerical programmer.
• Jean-Michel Muller and eight others, Handbook of Floating-Point Arithmetic, Second
edition, Birkhäuser-Boston (2018), ISBN 3-319-76525-6 (xxv + 627 pages). This is a
new edition of the preceding entry.
• Michael Overton, Numerical Computing with IEEE Floating Point Arithmetic, Includ-
ing One Theorem, One Rule of Thumb, and One Hundred and One Exercises, SIAM
(2001), ISBN 0-89871-482-6 (xiv + 104 pages), This is a small volume that can be
covered in a few hours.
• Guy L. Steele Jr. and Jon L. White, How to Print Floating-Point Numbers Accu-
rately, ACM SIGPLAN Notices 25(6) 112–126 (June 1990), https://doi.org/10.
1145/93548.93559. See also the papers by Clinger.
• Guy L. Steele Jr. and Jon L. White, Retrospective: How to Print Floating-Point
Numbers Accurately, ACM SIGPLAN Notices 39(4) 372–389 (April 2004), Reprint of
1990 paper, with additional commentary.
• Pat H. Sterbenz, Floating Point Computation, Prentice-Hall (1974), ISBN 0-13-322495-
3 (xiv + 316 pages). This often-cited book provides solid coverage of what floating-point
arithmetic was like before the introduction of IEEE 754 arithmetic.
209
29 Compilation
Early in the manual we explained how to compile a simple C program that consists of a
single source file (see Section 2.4 [Compile Example], page 9). However, we handle only
short programs that way. A typical C program consists of many source files, each of which
is usually a separate compilation module—meaning that it has to be compiled separately.
(The source files that are not separate compilation modules are those that are used via
#include; see Section 26.4 [Header Files], page 160.)
To compile a multi-module program, you compile each of the program’s compilation
modules, making an object file for that module. The last step is to link the many object
files together into a single executable for the whole program.
For the full details of how to compile C programs (and other languages’ programs)
with GCC, see Using the GNU Compiler Collection. On the Web, all is available through
https://gcc.gnu.org/onlinedocs/. Here we give only a simple introduction.
These commands compile two compilation modules, foo.c and bar.c, running the com-
piler for each module:
gcc -c -O -g foo.c
gcc -c -O -g bar.c
In these commands, -g says to generate debugging information, -O says to do some opti-
mization, and -c says to put the compiled code for that module into a corresponding object
file and go no further. The object file for foo.c is automatically called foo.o, and so on.
If you wish, you can specify the additional compilation options. For instance, -Wformat
-Wparenthesis -Wstrict-prototypes request additional warnings.
After you compile all the program’s modules, you link the object files into a combined
executable, like this:
gcc -o foo foo.o bar.o
In this command, -o foo species the file name for the executable file, and the other argu-
ments are the object files to link. Always specify the executable file name in a command
that generates one.
One reason to divide a large program into multiple compilation modules is to control
how each module can access the internals of the others. When a module declares a function
or variable extern, other modules can access it. The other functions and variables defined
in a module can’t be accessed from outside that module.
The other reason for using multiple modules is so that changing one source file does
not require recompiling all of them in order to try the modified program. It is sufficient
to recompile the source file that you changed, then link them all again. Dividing a large
program into many substantial modules in this way typically makes recompilation much
faster.
Normally we don’t run any of these commands directly. Instead we write a set of make
rules for the program, then use the make program to recompile only the source files that
need to be recompiled, by following those rules. See The GNU Make Manual.
210
30 Directing Compilation
This chapter describes C constructs that don’t alter the program’s meaning as such, but
rather direct the compiler how to treat some aspects of the program.
30.1 Pragmas
A pragma is an annotation in a program that gives direction to the compiler.
/* Functions to compile
with the forward-propagate optimization. */
Appendix B Aliasing
We have already presented examples of casting a void * pointer to another pointer type,
and casting another pointer type to void *.
One common kind of pointer cast is guaranteed safe: casting the value returned by
malloc and related functions (see Section 15.3 [Dynamic Memory Allocation], page 74).
It is safe because these functions do not save the pointer anywhere else; the only way the
program will access the newly allocated memory is via the pointer just returned.
In fact, C allows casting any pointer type to any other pointer type. Using this to access
the same place in memory using two different data types is called aliasing.
Aliasing is necessary in some programs that do sophisticated memory management, such
as GNU Emacs, but most C programs don’t need to do aliasing. When it isn’t needed, stay
away from it! To do aliasing correctly requires following the rules stated below. Otherwise,
the aliasing may result in malfunctions when the program runs.
The rest of this appendix explains the pitfalls and rules of aliasing.
struct
{
double d, e, f;
} foo;
q->f = 5.14159;
the value q->f will run past the end of the int that p points to. If p was initialized to
the start of an array of type int[6], the object is long enough for three doubles. But if
p points to something shorter, q->f will run on beyond the end of that, overlaying some
other data. Storing that will garble that other data. Or it could extend past the end of
memory space and cause a SIGSEGV signal (see Appendix E [Signals], page 222).
int main(void)
{
struct a foo;
struct a *p = &foo;
struct b *q = (struct b *) &foo;
From this assumption, the compiler can deduce (falsely, here) that the assignment into
q->size has no effect on the value of p->size, which must therefore still be 0. Thus, x will
be set to 0.
GNU C, following the C standard, defines this optimization as legitimate. Code that
misbehaves when optimized following these rules is, by definition, incorrect C code.
The rules for storage aliasing in C are based on the two data types: the type of the
object, and the type it is accessed through. The rules permit accessing part of a storage
object of type t using only these types:
• t.
• A type compatible with t. See Chapter 23 [Compatible Types], page 152.
• A signed or unsigned version of one of the above.
• A qualified version of one of the above. See Chapter 21 [Type Qualifiers], page 126.
• An array, structure (see Chapter 15 [Structures], page 72), or union type (Unions) that
contains one of the above, either directly as a field or through multiple levels of fields.
If t is double, this would include struct s { union { double d[2]; int i[4]; } u;
int i; }; because there’s a double inside it somewhere.
• A character type.
What do these rules say about the example in this subsection?
For foo.size (equivalently, a->size), t is int. The type float is not allowed as an
aliasing type by those rules, so b->size is not supposed to alias with elements of a. Based
on that assumption, GNU C makes a permitted optimization that was not, in this case,
consistent with what the programmer intended the program to do.
Whether GCC actually performs type-based aliasing analysis depends on the details of
the code. GCC has other ways to determine (in some cases) whether objects alias, and if it
gets a reliable answer that way, it won’t fall back on type-based heuristics.
The importance of knowing the type-based aliasing rules is not so as to ensure that the
optimization is done where it would be safe, but so as to ensure it is not done in a way that
would break the program. You can turn off type-based aliasing analysis by giving GCC the
option -fno-strict-aliasing.
219
Appendix C Digraphs
C accepts aliases for certain characters. Apparently in the 1990s some computer systems
had trouble inputting these characters, or trouble displaying them. These digraphs almost
never appear in C programs nowadays, but we mention them for completeness.
‘<:’ An alias for ‘[’.
‘:>’ An alias for ‘]’.
‘<%’ An alias for ‘{’.
‘%>’ An alias for ‘}’.
‘%:’ An alias for ‘#’, used for preprocessing directives (see Section 26.2 [Directives],
page 158) and macros (see Section 26.5 [Macros], page 165).
220
gnu_inline
The gnu_inline attribute, in a function’s declaration or definition, specifies to
handle the inline keyword the way GNU C originally implemented it, many
years before ISO C said anything about inlining. See Section 22.7.4 [Inline
Function Definitions], page 147.
For full documentation of attributes, see the GCC manual. See Section “System Head-
ers” in Using the GNU Compiler Collection.
222
Appendix E Signals
Some program operations bring about an error condition called a signal. These signals
terminate the program, by default.
There are various different kinds of signals, each with a name. We have seen several such
error conditions through this manual:
SIGSEGV This signal is generated when a program tries to read or write outside the
memory that is allocated for it, or to write memory that can only be read. The
name is an abbreviation for “segmentation violation”.
SIGFPE This signal indicates a fatal arithmetic error. The name is an abbreviation for
“floating-point exception”, but covers all types of arithmetic errors, including
division by zero and overflow.
SIGBUS This signal is generated when an invalid pointer is dereferenced, typically the
result of dereferencing an uninitialized pointer. It is similar to SIGSEGV, except
that SIGSEGV indicates invalid access to valid memory, while SIGBUS indicates
an attempt to access an invalid address.
These kinds of signal allow the program to specify a function as a signal handler. When
a signal has a handler, it doesn’t terminate the program; instead it calls the handler.
There are many other kinds of signal; here we list only those that come from run-time
errors in C operations. The rest have to do with the functioning of the operating system.
The GNU C Library Reference Manual gives more explanation about signals (see Section
“Program Signal Handling” in The GNU C Library Reference Manual).
223
under this License. If a section does not fit the above definition of Secondary then it is
not allowed to be designated as Invariant. The Document may contain zero Invariant
Sections. If the Document does not identify any Invariant Sections then there are none.
The “Cover Texts” are certain short passages of text that are listed, as Front-Cover
Texts or Back-Cover Texts, in the notice that says that the Document is released under
this License. A Front-Cover Text may be at most 5 words, and a Back-Cover Text may
be at most 25 words.
A “Transparent” copy of the Document means a machine-readable copy, represented
in a format whose specification is available to the general public, that is suitable for
revising the document straightforwardly with generic text editors or (for images com-
posed of pixels) generic paint programs or (for drawings) some widely available drawing
editor, and that is suitable for input to text formatters or for automatic translation to
a variety of formats suitable for input to text formatters. A copy made in an otherwise
Transparent file format whose markup, or absence of markup, has been arranged to
thwart or discourage subsequent modification by readers is not Transparent. An image
format is not Transparent if used for any substantial amount of text. A copy that is
not “Transparent” is called “Opaque”.
Examples of suitable formats for Transparent copies include plain ASCII without
markup, Texinfo input format, LaTEX input format, SGML or XML using a publicly
available DTD, and standard-conforming simple HTML, PostScript or PDF designed
for human modification. Examples of transparent image formats include PNG, XCF
and JPG. Opaque formats include proprietary formats that can be read and edited
only by proprietary word processors, SGML or XML for which the DTD and/or pro-
cessing tools are not generally available, and the machine-generated HTML, PostScript
or PDF produced by some word processors for output purposes only.
The “Title Page” means, for a printed book, the title page itself, plus such following
pages as are needed to hold, legibly, the material this License requires to appear in the
title page. For works in formats which do not have any title page as such, “Title Page”
means the text near the most prominent appearance of the work’s title, preceding the
beginning of the body of the text.
The “publisher” means any person or entity that distributes copies of the Document
to the public.
A section “Entitled XYZ” means a named subunit of the Document whose title either
is precisely XYZ or contains XYZ in parentheses following text that translates XYZ in
another language. (Here XYZ stands for a specific section name mentioned below, such
as “Acknowledgements”, “Dedications”, “Endorsements”, or “History”.) To “Preserve
the Title” of such a section when you modify the Document means that it remains a
section “Entitled XYZ” according to this definition.
The Document may include Warranty Disclaimers next to the notice which states that
this License applies to the Document. These Warranty Disclaimers are considered to
be included by reference in this License, but only as regards disclaiming warranties:
any other implication that these Warranty Disclaimers may have is void and has no
effect on the meaning of this License.
2. VERBATIM COPYING
Appendix F: GNU Free Documentation License 225
You may copy and distribute the Document in any medium, either commercially or
noncommercially, provided that this License, the copyright notices, and the license
notice saying this License applies to the Document are reproduced in all copies, and
that you add no other conditions whatsoever to those of this License. You may not use
technical measures to obstruct or control the reading or further copying of the copies
you make or distribute. However, you may accept compensation in exchange for copies.
If you distribute a large enough number of copies you must also follow the conditions
in section 3.
You may also lend copies, under the same conditions stated above, and you may publicly
display copies.
3. COPYING IN QUANTITY
If you publish printed copies (or copies in media that commonly have printed covers) of
the Document, numbering more than 100, and the Document’s license notice requires
Cover Texts, you must enclose the copies in covers that carry, clearly and legibly, all
these Cover Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
the back cover. Both covers must also clearly and legibly identify you as the publisher
of these copies. The front cover must present the full title with all words of the title
equally prominent and visible. You may add other material on the covers in addition.
Copying with changes limited to the covers, as long as they preserve the title of the
Document and satisfy these conditions, can be treated as verbatim copying in other
respects.
If the required texts for either cover are too voluminous to fit legibly, you should put
the first ones listed (as many as fit reasonably) on the actual cover, and continue the
rest onto adjacent pages.
If you publish or distribute Opaque copies of the Document numbering more than 100,
you must either include a machine-readable Transparent copy along with each Opaque
copy, or state in or with each Opaque copy a computer-network location from which
the general network-using public has access to download using public-standard network
protocols a complete Transparent copy of the Document, free of added material. If
you use the latter option, you must take reasonably prudent steps, when you begin
distribution of Opaque copies in quantity, to ensure that this Transparent copy will
remain thus accessible at the stated location until at least one year after the last time
you distribute an Opaque copy (directly or through your agents or retailers) of that
edition to the public.
It is requested, but not required, that you contact the authors of the Document well
before redistributing any large number of copies, to give them a chance to provide you
with an updated version of the Document.
4. MODIFICATIONS
You may copy and distribute a Modified Version of the Document under the conditions
of sections 2 and 3 above, provided that you release the Modified Version under precisely
this License, with the Modified Version filling the role of the Document, thus licensing
distribution and modification of the Modified Version to whoever possesses a copy of
it. In addition, you must do these things in the Modified Version:
A. Use in the Title Page (and on the covers, if any) a title distinct from that of the
Document, and from those of previous versions (which should, if there were any,
Appendix F: GNU Free Documentation License 226
be listed in the History section of the Document). You may use the same title as
a previous version if the original publisher of that version gives permission.
B. List on the Title Page, as authors, one or more persons or entities responsible for
authorship of the modifications in the Modified Version, together with at least five
of the principal authors of the Document (all of its principal authors, if it has fewer
than five), unless they release you from this requirement.
C. State on the Title page the name of the publisher of the Modified Version, as the
publisher.
D. Preserve all the copyright notices of the Document.
E. Add an appropriate copyright notice for your modifications adjacent to the other
copyright notices.
F. Include, immediately after the copyright notices, a license notice giving the public
permission to use the Modified Version under the terms of this License, in the form
shown in the Addendum below.
G. Preserve in that license notice the full lists of Invariant Sections and required Cover
Texts given in the Document’s license notice.
H. Include an unaltered copy of this License.
I. Preserve the section Entitled “History”, Preserve its Title, and add to it an item
stating at least the title, year, new authors, and publisher of the Modified Version
as given on the Title Page. If there is no section Entitled “History” in the Docu-
ment, create one stating the title, year, authors, and publisher of the Document
as given on its Title Page, then add an item describing the Modified Version as
stated in the previous sentence.
J. Preserve the network location, if any, given in the Document for public access to
a Transparent copy of the Document, and likewise the network locations given in
the Document for previous versions it was based on. These may be placed in the
“History” section. You may omit a network location for a work that was published
at least four years before the Document itself, or if the original publisher of the
version it refers to gives permission.
K. For any section Entitled “Acknowledgements” or “Dedications”, Preserve the Title
of the section, and preserve in the section all the substance and tone of each of the
contributor acknowledgements and/or dedications given therein.
L. Preserve all the Invariant Sections of the Document, unaltered in their text and
in their titles. Section numbers or the equivalent are not considered part of the
section titles.
M. Delete any section Entitled “Endorsements”. Such a section may not be included
in the Modified Version.
N. Do not retitle any existing section to be Entitled “Endorsements” or to conflict in
title with any Invariant Section.
O. Preserve any Warranty Disclaimers.
If the Modified Version includes new front-matter sections or appendices that qualify
as Secondary Sections and contain no material copied from the Document, you may at
your option designate some or all of these sections as invariant. To do this, add their
Appendix F: GNU Free Documentation License 227
titles to the list of Invariant Sections in the Modified Version’s license notice. These
titles must be distinct from any other section titles.
You may add a section Entitled “Endorsements”, provided it contains nothing but
endorsements of your Modified Version by various parties—for example, statements of
peer review or that the text has been approved by an organization as the authoritative
definition of a standard.
You may add a passage of up to five words as a Front-Cover Text, and a passage of up
to 25 words as a Back-Cover Text, to the end of the list of Cover Texts in the Modified
Version. Only one passage of Front-Cover Text and one of Back-Cover Text may be
added by (or through arrangements made by) any one entity. If the Document already
includes a cover text for the same cover, previously added by you or by arrangement
made by the same entity you are acting on behalf of, you may not add another; but
you may replace the old one, on explicit permission from the previous publisher that
added the old one.
The author(s) and publisher(s) of the Document do not by this License give permission
to use their names for publicity for or to assert or imply endorsement of any Modified
Version.
5. COMBINING DOCUMENTS
You may combine the Document with other documents released under this License,
under the terms defined in section 4 above for modified versions, provided that you
include in the combination all of the Invariant Sections of all of the original documents,
unmodified, and list them all as Invariant Sections of your combined work in its license
notice, and that you preserve all their Warranty Disclaimers.
The combined work need only contain one copy of this License, and multiple identical
Invariant Sections may be replaced with a single copy. If there are multiple Invariant
Sections with the same name but different contents, make the title of each such section
unique by adding at the end of it, in parentheses, the name of the original author or
publisher of that section if known, or else a unique number. Make the same adjustment
to the section titles in the list of Invariant Sections in the license notice of the combined
work.
In the combination, you must combine any sections Entitled “History” in the vari-
ous original documents, forming one section Entitled “History”; likewise combine any
sections Entitled “Acknowledgements”, and any sections Entitled “Dedications”. You
must delete all sections Entitled “Endorsements.”
6. COLLECTIONS OF DOCUMENTS
You may make a collection consisting of the Document and other documents released
under this License, and replace the individual copies of this License in the various
documents with a single copy that is included in the collection, provided that you
follow the rules of this License for verbatim copying of each of the documents in all
other respects.
You may extract a single document from such a collection, and distribute it individu-
ally under this License, provided you insert a copy of this License into the extracted
document, and follow this License in all other respects regarding verbatim copying of
that document.
Appendix F: GNU Free Documentation License 228
Preamble
The GNU General Public License is a free, copyleft license for software and other kinds of
works.
The licenses for most software and other practical works are designed to take away your
freedom to share and change the works. By contrast, the GNU General Public License is
intended to guarantee your freedom to share and change all versions of a program—to make
sure it remains free software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to any other work
released this way by its authors. You can apply it to your programs, too.
When we speak of free software, we are referring to freedom, not price. Our General
Public Licenses are designed to make sure that you have the freedom to distribute copies
of free software (and charge for them if you wish), that you receive source code or can get
it if you want it, that you can change the software or use pieces of it in new free programs,
and that you know you can do these things.
To protect your rights, we need to prevent others from denying you these rights or asking
you to surrender the rights. Therefore, you have certain responsibilities if you distribute
copies of the software, or if you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether gratis or for a fee, you
must pass on to the recipients the same freedoms that you received. You must make sure
that they, too, receive or can get the source code. And you must show them these terms so
they know their rights.
Developers that use the GNU GPL protect your rights with two steps: (1) assert copy-
right on the software, and (2) offer you this License giving you legal permission to copy,
distribute and/or modify it.
For the developers’ and authors’ protection, the GPL clearly explains that there is no
warranty for this free software. For both users’ and authors’ sake, the GPL requires that
modified versions be marked as changed, so that their problems will not be attributed
erroneously to authors of previous versions.
Some devices are designed to deny users access to install or run modified versions of the
software inside them, although the manufacturer can do so. This is fundamentally incom-
patible with the aim of protecting users’ freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to use, which is pre-
cisely where it is most unacceptable. Therefore, we have designed this version of the GPL
to prohibit the practice for those products. If such problems arise substantially in other
domains, we stand ready to extend this provision to those domains in future versions of the
GPL, as needed to protect the freedom of users.
Appendix G: GNU General Public License 232
Finally, every program is threatened constantly by software patents. States should not
allow patents to restrict development and use of software on general-purpose computers, but
in those that do, we wish to avoid the special danger that patents applied to a free program
could make it effectively proprietary. To prevent this, the GPL assures that patents cannot
be used to render the program non-free.
The precise terms and conditions for copying, distribution and modification follow.
The “System Libraries” of an executable work include anything, other than the work as
a whole, that (a) is included in the normal form of packaging a Major Component, but
which is not part of that Major Component, and (b) serves only to enable use of the
work with that Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A “Major Component”,
in this context, means a major essential component (kernel, window system, and so
on) of the specific operating system (if any) on which the executable work runs, or a
compiler used to produce the work, or an object code interpreter used to run it.
The “Corresponding Source” for a work in object code form means all the source code
needed to generate, install, and (for an executable work) run the object code and to
modify the work, including scripts to control those activities. However, it does not
include the work’s System Libraries, or general-purpose tools or generally available
free programs which are used unmodified in performing those activities but which are
not part of the work. For example, Corresponding Source includes interface definition
files associated with source files for the work, and the source code for shared libraries
and dynamically linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those subprograms
and other parts of the work.
The Corresponding Source need not include anything that users can regenerate auto-
matically from other parts of the Corresponding Source.
The Corresponding Source for a work in source code form is that same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of copyright on the
Program, and are irrevocable provided the stated conditions are met. This License ex-
plicitly affirms your unlimited permission to run the unmodified Program. The output
from running a covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your rights of fair use
or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not convey, without
conditions so long as your license otherwise remains in force. You may convey covered
works to others for the sole purpose of having them make modifications exclusively
for you, or provide you with facilities for running those works, provided that you
comply with the terms of this License in conveying all material for which you do not
control copyright. Those thus making or running the covered works for you must do
so exclusively on your behalf, under your direction and control, on terms that prohibit
them from making any copies of your copyrighted material outside their relationship
with you.
Conveying under any other circumstances is permitted solely under the conditions
stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
3. Protecting Users’ Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological measure under
any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty
adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention
of such measures.
Appendix G: GNU General Public License 234
When you convey a covered work, you waive any legal power to forbid circumvention of
technological measures to the extent such circumvention is effected by exercising rights
under this License with respect to the covered work, and you disclaim any intention
to limit operation or modification of the work as a means of enforcing, against the
work’s users, your or third parties’ legal rights to forbid circumvention of technological
measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program’s source code as you receive it, in any
medium, provided that you conspicuously and appropriately publish on each copy an
appropriate copyright notice; keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code; keep intact all
notices of the absence of any warranty; and give all recipients a copy of this License
along with the Program.
You may charge any price or no price for each copy that you convey, and you may offer
support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to produce it from
the Program, in the form of source code under the terms of section 4, provided that
you also meet all of these conditions:
a. The work must carry prominent notices stating that you modified it, and giving a
relevant date.
b. The work must carry prominent notices stating that it is released under this Li-
cense and any conditions added under section 7. This requirement modifies the
requirement in section 4 to “keep intact all notices”.
c. You must license the entire work, as a whole, under this License to anyone who
comes into possession of a copy. This License will therefore apply, along with any
applicable section 7 additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no permission to license
the work in any other way, but it does not invalidate such permission if you have
separately received it.
d. If the work has interactive user interfaces, each must display Appropriate Legal
Notices; however, if the Program has interactive interfaces that do not display
Appropriate Legal Notices, your work need not make them do so.
A compilation of a covered work with other separate and independent works, which
are not by their nature extensions of the covered work, and which are not combined
with it such as to form a larger program, in or on a volume of a storage or distribution
medium, is called an “aggregate” if the compilation and its resulting copyright are
not used to limit the access or legal rights of the compilation’s users beyond what the
individual works permit. Inclusion of a covered work in an aggregate does not cause
this License to apply to the other parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms of sections 4 and
5, provided that you also convey the machine-readable Corresponding Source under
the terms of this License, in one of these ways:
Appendix G: GNU General Public License 235
a. Convey the object code in, or embodied in, a physical product (including a phys-
ical distribution medium), accompanied by the Corresponding Source fixed on a
durable physical medium customarily used for software interchange.
b. Convey the object code in, or embodied in, a physical product (including a physi-
cal distribution medium), accompanied by a written offer, valid for at least three
years and valid for as long as you offer spare parts or customer support for that
product model, to give anyone who possesses the object code either (1) a copy of
the Corresponding Source for all the software in the product that is covered by this
License, on a durable physical medium customarily used for software interchange,
for a price no more than your reasonable cost of physically performing this con-
veying of source, or (2) access to copy the Corresponding Source from a network
server at no charge.
c. Convey individual copies of the object code with a copy of the written offer to
provide the Corresponding Source. This alternative is allowed only occasionally
and noncommercially, and only if you received the object code with such an offer,
in accord with subsection 6b.
d. Convey the object code by offering access from a designated place (gratis or for
a charge), and offer equivalent access to the Corresponding Source in the same
way through the same place at no further charge. You need not require recipients
to copy the Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source may be on
a different server (operated by you or a third party) that supports equivalent
copying facilities, provided you maintain clear directions next to the object code
saying where to find the Corresponding Source. Regardless of what server hosts
the Corresponding Source, you remain obligated to ensure that it is available for
as long as needed to satisfy these requirements.
e. Convey the object code using peer-to-peer transmission, provided you inform other
peers where the object code and Corresponding Source of the work are being offered
to the general public at no charge under subsection 6d.
A separable portion of the object code, whose source code is excluded from the Cor-
responding Source as a System Library, need not be included in conveying the object
code work.
A “User Product” is either (1) a “consumer product”, which means any tangible per-
sonal property which is normally used for personal, family, or household purposes, or
(2) anything designed or sold for incorporation into a dwelling. In determining whether
a product is a consumer product, doubtful cases shall be resolved in favor of coverage.
For a particular product received by a particular user, “normally used” refers to a
typical or common use of that class of product, regardless of the status of the par-
ticular user or of the way in which the particular user actually uses, or expects or is
expected to use, the product. A product is a consumer product regardless of whether
the product has substantial commercial, industrial or non-consumer uses, unless such
uses represent the only significant mode of use of the product.
“Installation Information” for a User Product means any methods, procedures, autho-
rization keys, or other information required to install and execute modified versions of a
covered work in that User Product from a modified version of its Corresponding Source.
Appendix G: GNU General Public License 236
The information must suffice to ensure that the continued functioning of the modified
object code is in no case prevented or interfered with solely because modification has
been made.
If you convey an object code work under this section in, or with, or specifically for
use in, a User Product, and the conveying occurs as part of a transaction in which
the right of possession and use of the User Product is transferred to the recipient in
perpetuity or for a fixed term (regardless of how the transaction is characterized),
the Corresponding Source conveyed under this section must be accompanied by the
Installation Information. But this requirement does not apply if neither you nor any
third party retains the ability to install modified object code on the User Product (for
example, the work has been installed in ROM).
The requirement to provide Installation Information does not include a requirement
to continue to provide support service, warranty, or updates for a work that has been
modified or installed by the recipient, or for the User Product in which it has been
modified or installed. Access to a network may be denied when the modification itself
materially and adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided, in accord with
this section must be in a format that is publicly documented (and with an implementa-
tion available to the public in source code form), and must require no special password
or key for unpacking, reading or copying.
7. Additional Terms.
“Additional permissions” are terms that supplement the terms of this License by mak-
ing exceptions from one or more of its conditions. Additional permissions that are
applicable to the entire Program shall be treated as though they were included in this
License, to the extent that they are valid under applicable law. If additional permis-
sions apply only to part of the Program, that part may be used separately under those
permissions, but the entire Program remains governed by this License without regard
to the additional permissions.
When you convey a copy of a covered work, you may at your option remove any
additional permissions from that copy, or from any part of it. (Additional permissions
may be written to require their own removal in certain cases when you modify the
work.) You may place additional permissions on material, added by you to a covered
work, for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you add to a covered
work, you may (if authorized by the copyright holders of that material) supplement
the terms of this License with terms:
a. Disclaiming warranty or limiting liability differently from the terms of sections 15
and 16 of this License; or
b. Requiring preservation of specified reasonable legal notices or author attributions
in that material or in the Appropriate Legal Notices displayed by works containing
it; or
c. Prohibiting misrepresentation of the origin of that material, or requiring that mod-
ified versions of such material be marked in reasonable ways as different from the
original version; or
Appendix G: GNU General Public License 237
d. Limiting the use for publicity purposes of names of licensors or authors of the
material; or
e. Declining to grant rights under trademark law for use of some trade names, trade-
marks, or service marks; or
f. Requiring indemnification of licensors and authors of that material by anyone who
conveys the material (or modified versions of it) with contractual assumptions
of liability to the recipient, for any liability that these contractual assumptions
directly impose on those licensors and authors.
All other non-permissive additional terms are considered “further restrictions” within
the meaning of section 10. If the Program as you received it, or any part of it, con-
tains a notice stating that it is governed by this License along with a term that is a
further restriction, you may remove that term. If a license document contains a further
restriction but permits relicensing or conveying under this License, you may add to a
covered work material governed by the terms of that license document, provided that
the further restriction does not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you must place, in the
relevant source files, a statement of the additional terms that apply to those files, or a
notice indicating where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the form of a sep-
arately written license, or stated as exceptions; the above requirements apply either
way.
8. Termination.
You may not propagate or modify a covered work except as expressly provided un-
der this License. Any attempt otherwise to propagate or modify it is void, and will
automatically terminate your rights under this License (including any patent licenses
granted under the third paragraph of section 11).
However, if you cease all violation of this License, then your license from a particular
copyright holder is reinstated (a) provisionally, unless and until the copyright holder
explicitly and finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means prior to 60 days
after the cessation.
Moreover, your license from a particular copyright holder is reinstated permanently if
the copyright holder notifies you of the violation by some reasonable means, this is the
first time you have received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after your receipt of the
notice.
Termination of your rights under this section does not terminate the licenses of parties
who have received copies or rights from you under this License. If your rights have
been terminated and not permanently reinstated, you do not qualify to receive new
licenses for the same material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or run a copy of the
Program. Ancillary propagation of a covered work occurring solely as a consequence of
using peer-to-peer transmission to receive a copy likewise does not require acceptance.
Appendix G: GNU General Public License 238
However, nothing other than this License grants you permission to propagate or modify
any covered work. These actions infringe copyright if you do not accept this License.
Therefore, by modifying or propagating a covered work, you indicate your acceptance
of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically receives a license
from the original licensors, to run, modify and propagate that work, subject to this
License. You are not responsible for enforcing compliance by third parties with this
License.
An “entity transaction” is a transaction transferring control of an organization, or
substantially all assets of one, or subdividing an organization, or merging organizations.
If propagation of a covered work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever licenses to the work
the party’s predecessor in interest had or could give under the previous paragraph, plus
a right to possession of the Corresponding Source of the work from the predecessor in
interest, if the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the rights granted or
affirmed under this License. For example, you may not impose a license fee, royalty, or
other charge for exercise of rights granted under this License, and you may not initiate
litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent
claim is infringed by making, using, selling, offering for sale, or importing the Program
or any portion of it.
11. Patents.
A “contributor” is a copyright holder who authorizes use under this License of the
Program or a work on which the Program is based. The work thus licensed is called
the contributor’s “contributor version”.
A contributor’s “essential patent claims” are all patent claims owned or controlled by
the contributor, whether already acquired or hereafter acquired, that would be infringed
by some manner, permitted by this License, of making, using, or selling its contributor
version, but do not include claims that would be infringed only as a consequence of
further modification of the contributor version. For purposes of this definition, “con-
trol” includes the right to grant patent sublicenses in a manner consistent with the
requirements of this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free patent license
under the contributor’s essential patent claims, to make, use, sell, offer for sale, import
and otherwise run, modify and propagate the contents of its contributor version.
In the following three paragraphs, a “patent license” is any express agreement or com-
mitment, however denominated, not to enforce a patent (such as an express permission
to practice a patent or covenant not to sue for patent infringement). To “grant” such
a patent license to a party means to make such an agreement or commitment not to
enforce a patent against the party.
If you convey a covered work, knowingly relying on a patent license, and the Corre-
sponding Source of the work is not available for anyone to copy, free of charge and under
the terms of this License, through a publicly available network server or other readily
accessible means, then you must either (1) cause the Corresponding Source to be so
Appendix G: GNU General Public License 239
available, or (2) arrange to deprive yourself of the benefit of the patent license for this
particular work, or (3) arrange, in a manner consistent with the requirements of this
License, to extend the patent license to downstream recipients. “Knowingly relying”
means you have actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient’s use of the covered work in a country,
would infringe one or more identifiable patents in that country that you have reason
to believe are valid.
If, pursuant to or in connection with a single transaction or arrangement, you convey,
or propagate by procuring conveyance of, a covered work, and grant a patent license
to some of the parties receiving the covered work authorizing them to use, propagate,
modify or convey a specific copy of the covered work, then the patent license you grant
is automatically extended to all recipients of the covered work and works based on it.
A patent license is “discriminatory” if it does not include within the scope of its cover-
age, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the
rights that are specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is in the business of
distributing software, under which you make payment to the third party based on the
extent of your activity of conveying the work, and under which the third party grants,
to any of the parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work conveyed by you (or
copies made from those copies), or (b) primarily for and in connection with specific
products or compilations that contain the covered work, unless you entered into that
arrangement, or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting any implied license or
other defenses to infringement that may otherwise be available to you under applicable
patent law.
12. No Surrender of Others’ Freedom.
If conditions are imposed on you (whether by court order, agreement or otherwise) that
contradict the conditions of this License, they do not excuse you from the conditions
of this License. If you cannot convey a covered work so as to satisfy simultaneously
your obligations under this License and any other pertinent obligations, then as a
consequence you may not convey it at all. For example, if you agree to terms that
obligate you to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this License would
be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have permission to link or
combine any covered work with a work licensed under version 3 of the GNU Affero
General Public License into a single combined work, and to convey the resulting work.
The terms of this License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License, section 13,
concerning interaction through a network will apply to the combination as such.
14. Revised Versions of this License.
Appendix G: GNU General Public License 240
The Free Software Foundation may publish revised and/or new versions of the GNU
General Public License from time to time. Such new versions will be similar in spirit
to the present version, but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Program specifies that
a certain numbered version of the GNU General Public License “or any later version”
applies to it, you have the option of following the terms and conditions either of that
numbered version or of any later version published by the Free Software Foundation.
If the Program does not specify a version number of the GNU General Public License,
you may choose any version ever published by the Free Software Foundation.
If the Program specifies that a proxy can decide which future versions of the GNU
General Public License can be used, that proxy’s public statement of acceptance of a
version permanently authorizes you to choose that version for the Program.
Later license versions may give you additional or different permissions. However, no
additional obligations are imposed on any author or copyright holder as a result of your
choosing to follow a later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PER-
MITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN
WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE
THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EX-
PRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFEC-
TIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO
MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, IN-
CIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO
LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUS-
TAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM
TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR
OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAM-
AGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided above cannot be given
local legal effect according to their terms, reviewing courts shall apply local law that
most closely approximates an absolute waiver of all civil liability in connection with
the Program, unless a warranty or assumption of liability accompanies a copy of the
Program in return for a fee.
Appendix G: GNU General Public License 241
You should have received a copy of the GNU General Public License
along with this program. If not, see https://www.gnu.org/licenses/.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short notice like this when it
starts in an interactive mode:
program Copyright (C) year name of author
This program comes with ABSOLUTELY NO WARRANTY; for details type ‘show w’.
This is free software, and you are welcome to redistribute it
under certain conditions; type ‘show c’ for details.
The hypothetical commands ‘show w’ and ‘show c’ should show the appropriate parts of
the General Public License. Of course, your program’s commands might be different; for a
GUI interface, you would use an “about box”.
You should also get your employer (if you work as a programmer) or school, if any, to
sign a “copyright disclaimer” for the program, if necessary. For more information on this,
and how to apply and follow the GNU GPL, see https://www.gnu.org/licenses/.
The GNU General Public License does not permit incorporating your program into pro-
prietary programs. If your program is a subroutine library, you may consider it more useful
to permit linking proprietary applications with the library. If this is what you want to do,
use the GNU Lesser General Public License instead of this License. But first, please read
https://www.gnu.org/licenses/why-not-lgpl.html.
242
# E
#define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
#elif. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 extern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
#else. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
#endif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
#error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 F
#if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
#ifdef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
#ifndef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 FLT_DECIMAL_DIG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
#include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 FLT_HAS_SUBNORM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
#line. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 FLT_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
#undef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 FLT_MIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
#warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 FLT_TRUE_MIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
__aligned__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 G
__alignof__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
__attribute__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
__auto_type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
__complex__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
__label__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 I
_Alignas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
_Alignof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 inline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
_Complex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
_Static_assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 INT_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
INT_MIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
A
auto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 L
LDBL_DECIMAL_DIG . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
B LDBL_HAS_SUBNORM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
bool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 LDBL_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
break. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 LDBL_MIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
LDBL_TRUE_MIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
LLONG_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
C long double . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 long int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
char . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 long long int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
CHAR_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 LONG_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
const. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
M
D main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
DBL_DECIMAL_DIG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
DBL_HAS_SUBNORM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
DBL_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
R
DBL_MIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
DBL_TRUE_MIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 restrict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
double. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Index of Symbols and Keywords 243
S U
SCHAR_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 UCHAR_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
short int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 UINT_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
ULLONG_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
SHRT_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
ULONG_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
signed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
sizeof. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 unsigned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123, 132 USHRT_MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
struct. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
V
void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
T
typedef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 W
typeof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 while. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
244
Concept Index
# array, multidimensional . . . . . . . . . . . . . . . . . . . . . . . . 93
# preprocessing operator . . . . . . . . . . . . . . . . . . . . . . 169 arrays and pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
## preprocessing operator . . . . . . . . . . . . . . . . . . . . . 171 arrays as parameters . . . . . . . . . . . . . . . . . . . . . . . . . . 132
assigning function pointers . . . . . . . . . . . . . . . . . . . . 139
assigning structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
? assignment expressions . . . . . . . . . . . . . . . . . . . . . . . . . 28
assignment in subexpressions . . . . . . . . . . . . . . . . . . . 32
?: side effect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
assignment type conversions. . . . . . . . . . . . . . . . . . . 153
assignment, modifying . . . . . . . . . . . . . . . . . . . . . . . . . 29
assignment, simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
associativity and ordering . . . . . . . . . . . . . . . . . . . . . . 41
‘_’ in variables in macros . . . . . . . . . . . . . . . . . . . . . . 180
attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
__attribute__((packed)) . . . . . . . . . . . . . . . . . . . . . 76
auto declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
__complex__ keyword . . . . . . . . . . . . . . . . . . . . . . . . . . 47
_Complex keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Complex I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 B
backspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
\ base conversion (floating point) . . . . . . . . . . . . . . . 206
bell character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
‘\a’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
binary integer constants . . . . . . . . . . . . . . . . . . . . . . . . 50
‘\b’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
binary operator grammar . . . . . . . . . . . . . . . . . . . . . . 39
‘\e’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
‘\f’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
bitwise operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
‘\n’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
‘\r’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
block scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
‘\t’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
boolean type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
‘\v’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
branch cuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
branches of conditional expression . . . . . . . . . . . . . . 36
A break statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
accessing array elements . . . . . . . . . . . . . . . . . . . . . . . 89
addition operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
address of a label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
address-of operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
C
aliasing (of storage) . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 call-by-value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
alignment of structures . . . . . . . . . . . . . . . . . . . . . . . . . 76 calling function pointers . . . . . . . . . . . . . . . . . . . . . . 139
alignment of type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 calling functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
allocating memory dynamically. . . . . . . . . . . . . . . . . 74 carriage return in source . . . . . . . . . . . . . . . . . . . . . . . 16
allocation file-scope variables . . . . . . . . . . . . . . . . . . 124 case labels in initializers . . . . . . . . . . . . . . . . . . . . . . 120
argument promotions . . . . . . . . . . . . . . . . . . . . . . . . . 154 case of letters in identifiers . . . . . . . . . . . . . . . . . . . . . 17
arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 case ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
arguments in macro definitions . . . . . . . . . . . . . . . . 168 cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
arithmetic operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 cast to a union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
arithmetic, pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 CHAR_BIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 character constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
array elements, accessing . . . . . . . . . . . . . . . . . . . . . . . 89 character set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
array example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 cloning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
array fields, flexible . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 code point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
array of length zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 combining variable declarations . . . . . . . . . . . . . . . 119
array of variable length . . . . . . . . . . . . . . . . . . . . . . . . 95 comma operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
array parameters, variable-length. . . . . . . . . . . . . . 142 command-line parameters . . . . . . . . . . . . . . . . . . . . . 141
array types, incomplete . . . . . . . . . . . . . . . . . . . . . . . . 91 commenting out code . . . . . . . . . . . . . . . . . . . . . . . . . 186
array values, constructing . . . . . . . . . . . . . . . . . . . . . . 94 comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
array, declaring . . . . . . . . . . . . . . . . . . . . . . . . . . . 89, 118 common type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
array, layout in memory . . . . . . . . . . . . . . . . . . . . . . . . 93 comparison, pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Concept Index 245
L N
label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 NaN in floating-point arithmetic . . . . . . . . . . . . . . 200
labeled elements in initializers . . . . . . . . . . . . . . . . . 120 NaNs-always-propagate rule . . . . . . . . . . . . . . . . . . . 200
labels as values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 negation operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
layout of structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 negation operator, logical . . . . . . . . . . . . . . . . . . . . . . 34
negation, bitwise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
left-associative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
nested block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
length-zero arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
nested functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
less-or-equal operator . . . . . . . . . . . . . . . . . . . . . . . . . . 24
newline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
less-than operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 newline in source. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
lexical syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 not a number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
limitations of C arrays . . . . . . . . . . . . . . . . . . . . . . . . . 92 not-equal operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
line continuation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 null directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
line control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 null pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
linefeed in source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 null statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
link . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 numbers, preprocessing . . . . . . . . . . . . . . . . . . . . . . . 159
linking object files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 numeric comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
local labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
local variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
local variables in macros . . . . . . . . . . . . . . . . . . . . . . 180 O
logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 object file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
loop statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 object-like macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
low level pointer arithmetic . . . . . . . . . . . . . . . . . . . . 67 offset of structure fields . . . . . . . . . . . . . . . . . . . . . . . . 75
lvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 old-style function definitions . . . . . . . . . . . . . . . . . . 149
omitting types in declarations . . . . . . . . . . . . . . . . . 125
operand execution ordering. . . . . . . . . . . . . . . . . . . . . 41
operand ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
M operand promotions. . . . . . . . . . . . . . . . . . . . . . . . . . . 155
operator precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
machine epsilon (floating point) . . . . . . . . . . . . . . . 202 operator, addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
macro argument expansion . . . . . . . . . . . . . . . . . . . . 181 operator, comma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
macro arguments and directives . . . . . . . . . . . . . . . 176 operator, decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 operator, division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
macros in include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 operator, equal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
macros with arguments . . . . . . . . . . . . . . . . . . . . . . . 168 operator, greater-or-equal . . . . . . . . . . . . . . . . . . . . . . 24
macros with variable arguments . . . . . . . . . . . . . . . 172 operator, greater-than. . . . . . . . . . . . . . . . . . . . . . . . . . 24
macros, local labels . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 operator, increment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
macros, local variables in . . . . . . . . . . . . . . . . . . . . . 180 operator, less-or-equal . . . . . . . . . . . . . . . . . . . . . . . . . . 24
macros, types of arguments . . . . . . . . . . . . . . . . . . . 122 operator, less-than . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
main function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 operator, multiplication . . . . . . . . . . . . . . . . . . . . . . . . 20
operator, negation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
make rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
operator, not-equal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
manifest constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
operator, postdecrement . . . . . . . . . . . . . . . . . . . . . . . 31
maximum integer values . . . . . . . . . . . . . . . . . . . . . . 190 operator, postincrement . . . . . . . . . . . . . . . . . . . . . . . . 31
memory allocation, dynamic . . . . . . . . . . . . . . . . . . . 74 operator, remainder . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
memory organization . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 operator, subtraction. . . . . . . . . . . . . . . . . . . . . . . . . . . 20
minimum integer values . . . . . . . . . . . . . . . . . . . . . . . 190 operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
modifying assignment . . . . . . . . . . . . . . . . . . . . . . . . . . 29 operators, arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
modulus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 operators, assignment . . . . . . . . . . . . . . . . . . . . . . . . . . 28
multidimensional arrays . . . . . . . . . . . . . . . . . . . . . . . . 93 operators, bitwise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
multiplication operator . . . . . . . . . . . . . . . . . . . . . . . . . 20 operators, comparison . . . . . . . . . . . . . . . . . . . . . . . . . . 24
operators, logical. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
operators, shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
optimization and ordering . . . . . . . . . . . . . . . . . . . . . . 43
order of execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
ordering and optimization . . . . . . . . . . . . . . . . . . . . . . 43
ordering and postincrement . . . . . . . . . . . . . . . . . . . . 42
ordering of operands . . . . . . . . . . . . . . . . . . . . . . . 41, 43
Concept Index 248
W
while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Z
whitespace characters in source files . . . . . . . . . . . . 16 zero, division by . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
wide character constants . . . . . . . . . . . . . . . . . . . . . . . 56 zero-length arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
wide string constants. . . . . . . . . . . . . . . . . . . . . . . . . . . 57 zero-origin indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12