GNU C Language Intro and Reference

This is a plain-text render of the unfinished GNU C Language Intro and Reference by Richard Stallman, Trevis Rothwell and Nelson Beebe.

See announcement on the info-gnu mailing list.


  1. GNU C Manual
  2. 1 The First Example
  3. 2 A Complete Program
  4. 3 Storage and Data
  5. 4 Beyond Integers
  6. 5 Lexical Syntax
  7. 6 Arithmetic
  8. 7 Assignment Expressions
  9. 8 Execution Control Expressions
  10. 9 Binary Operator Grammar
  11. 10 Order of Execution
  12. 11 Primitive Data Types
  13. 12 Constants
  14. 13 Type Size
  15. 14 Pointers
  16. 15 Structures
  17. 16 Arrays
  18. 17 Enumeration Types
  19. 18 Defining Typedef Names
  20. 19 Statements
  21. 20 Variables
  22. 21 Type Qualifiers
  23. 22 Functions
  24. 23 Compatible Types
  25. 24 Type Conversions
  26. 25 Scope
  27. 26 Preprocessing
  28. 27 Integers in Depth
  29. 28 Floating Point in Depth
  30. 29 Compilation
  31. 30 Directing Compilation
  32. Appendix A Type Alignment
  33. Appendix B Aliasing
  34. Appendix C Digraphs
  35. Appendix D Attributes in Declarations
  36. Appendix E Signals
  37. Appendix F GNU Free Documentation License
  38. Index of Symbols and Keywords
  39. Concept Index
  40. GNU C Manual
  41. 1 The First Example
  42. 1.1 Example: Recursive Fibonacci
  43. 1.1.1 Function Header
  44. 1.1.2 Function Body
  45. 1.2 The Stack, And Stack Overflow
  46. 1.3 Example: Iterative Fibonacci
  47. 2 A Complete Program
  48. 2.1 Complete Program Example
  49. 2.2 Complete Program Explanation
  50. 2.3 Complete Program, Line by Line
  51. 2.4 Compiling the Example Program
  52. 3 Storage and Data
  53. 4 Beyond Integers
  54. 4.1 An Example with Non-Integer Numbers
  55. 4.2 An Example with Arrays
  56. 4.3 Calling the Array Example
  57. 4.4 Variations for Array Example
  58. 5 Lexical Syntax
  59. 5.1 Write Programs in English!
  60. 5.2 Characters
  61. 5.3 Whitespace
  62. 5.4 Comments
  63. 5.5 Identifiers
  64. 5.6 Operators and Punctuation
  65. 5.7 Line Continuation
  66. 6 Arithmetic
  67. 6.1 Basic Arithmetic
  68. 6.2 Integer Arithmetic
  69. 6.3 Integer Overflow
  70. 6.3.1 Overflow with Unsigned Integers
  71. 6.3.2 Overflow with Signed Integers
  72. 6.4 Mixed-Mode Arithmetic
  73. 6.5 Division and Remainder
  74. 6.6 Numeric Comparisons
  75. 6.7 Shift Operations
  76. 6.7.1 Shifting Makes New Bits
  77. 6.7.2 Caveats for Shift Operations
  78. 6.7.3 Shift Hacks
  79. 6.8 Bitwise Operations
  80. 7 Assignment Expressions
  81. 7.1 Simple Assignment
  82. 7.2 Lvalues
  83. 7.3 Modifying Assignment
  84. 7.4 Increment and Decrement Operators
  85. 7.5 Postincrement and Postdecrement
  86. 7.6 Pitfall: Assignment in Subexpressions
  87. 7.7 Write Assignments in Separate Statements
  88. 8 Execution Control Expressions
  89. 8.1 Logical Operators
  90. 8.2 Logical Operators and Comparisons
  91. 8.3 Logical Operators and Assignments
  92. 8.4 Conditional Expression
  93. 8.4.1 Rules for Conditional Operator
  94. 8.4.2 Conditional Operator Branches
  95. 8.5 Comma Operator
  96. 8.5.1 The Uses of the Comma Operator
  97. 8.5.2 Clean Use of the Comma Operator
  98. 8.5.3 When Not to Use the Comma Operator
  99. 9 Binary Operator Grammar
  100. 10 Order of Execution
  101. 10.1 Reordering of Operands
  102. 10.2 Associativity and Ordering
  103. 10.3 Sequence Points
  104. 10.4 Postincrement and Ordering
  105. 10.5 Ordering of Operands
  106. 10.6 Optimization and Ordering
  107. 11 Primitive Data Types
  108. 11.1 Integer Data Types
  109. 11.1.1 Basic Integers
  110. 11.1.2 Signed and Unsigned Types
  111. 11.1.3 Narrow Integers
  112. 11.1.4 Conversion among Integer Types
  113. 11.1.5 Boolean Type
  114. 11.1.6 Integer Variations
  115. 11.2 Floating-Point Data Types
  116. 11.3 Complex Data Types
  117. 11.4 The Void Type
  118. 11.5 Other Data Types
  119. 11.6 Type Designators
  120. 12 Constants
  121. 12.1 Integer Constants
  122. 12.2 Integer Constant Data Types
  123. 12.3 Floating-Point Constants
  124. 12.4 Imaginary Constants
  125. 12.5 Invalid Numbers
  126. 12.6 Character Constants
  127. 12.7 String Constants
  128. 12.8 UTF-8 String Constants
  129. 12.9 Unicode Character Codes
  130. 12.10 Wide Character Constants
  131. 12.11 Wide String Constants
  132. 13 Type Size
  133. 14 Pointers
  134. 14.1 Address of Data
  135. 14.2 Pointer Types
  136. 14.3 Pointer-Variable Declarations
  137. 14.4 Pointer-Type Designators
  138. 14.5 Dereferencing Pointers
  139. 14.6 Null Pointers
  140. 14.7 Dereferencing Null or Invalid Pointers
  141. 14.8 Void Pointers
  142. 14.9 Pointer Comparison
  143. 14.10 Pointer Arithmetic
  144. 14.11 Pointers and Arrays
  145. 14.12 Pointer Arithmetic at Low Level
  146. 14.13 Pointer Increment and Decrement
  147. 14.14 Drawbacks of Pointer Arithmetic
  148. 14.15 Pointer-Integer Conversion
  149. 14.16 Printing Pointers
  150. 15 Structures
  151. 15.1 Referencing Structure Fields
  152. 15.2 Dynamic Memory Allocation
  153. 15.3 Field Offset
  154. 15.4 Structure Layout
  155. 15.5 Packed Structures
  156. 15.6 Bit Fields
  157. 15.7 Bit Field Packing
  158. 15.8 ‘const’ Fields
  159. 15.9 Arrays of Length Zero
  160. 15.10 Flexible Array Fields
  161. 15.11 Overlaying Different Structures
  162. 15.12 Structure Assignment
  163. 15.13 Unions
  164. 15.14 Packing With Unions
  165. 15.15 Cast to a Union Type
  166. 15.16 Structure Constructors
  167. 15.17 Unnamed Types as Fields
  168. 15.18 Incomplete Types
  169. 15.19 Intertwined Incomplete Types
  170. 15.20 Type Tags
  171. 16 Arrays
  172. 16.1 Accessing Array Elements
  173. 16.2 Declaring an Array
  174. 16.3 Strings
  175. 16.4 Array Type Designators
  176. 16.5 Incomplete Array Types
  177. 16.6 Limitations of C Arrays
  178. 16.7 Multidimensional Arrays
  179. 16.8 Constructing Array Values
  180. 16.9 Arrays of Variable Length
  181. 17 Enumeration Types
  182. 18 Defining Typedef Names
  183. 19 Statements
  184. 19.1 Expression Statement
  185. 19.2 ‘if’ Statement
  186. 19.3 ‘if-else’ Statement
  187. 19.4 Blocks
  188. 19.5 ‘return’ Statement
  189. 19.6 Loop Statements
  190. 19.6.1 ‘while’ Statement
  191. 19.6.2 ‘do-while’ Statement
  192. 19.6.3 ‘break’ Statement
  193. 19.6.4 ‘for’ Statement
  194. 19.6.5 Example of ‘for’
  195. 19.6.6 Omitted ‘for’-Expressions
  196. 19.6.7 ‘for’-Index Declarations
  197. 19.6.8 ‘continue’ Statement
  198. 19.7 ‘switch’ Statement
  199. 19.8 Example of ‘switch’
  200. 19.9 Duff’s Device
  201. 19.10 Case Ranges
  202. 19.11 Null Statement
  203. 19.12 ‘goto’ Statement and Labels
  204. 19.13 Locally Declared Labels
  205. 19.14 Labels as Values
  206. 19.14.1 Label Value Uses
  207. 19.14.2 Label Value Caveats
  208. 19.15 Statements and Declarations in Expressions
  209. 20 Variables
  210. 20.1 Variable Declarations
  211. 20.1.1 Declaring Arrays and Pointers
  212. 20.1.2 Combining Variable Declarations
  213. 20.2 Initializers
  214. 20.3 Designated Initializers
  215. 20.4 Referring to a Type with ‘__auto_type’
  216. 20.5 Local Variables
  217. 20.6 File-Scope Variables
  218. 20.7 Static Local Variables
  219. 20.8 ‘extern’ Declarations
  220. 20.9 Allocating File-Scope Variables
  221. 20.10 ‘auto’ and ‘register’
  222. 20.11 Omitting Types in Declarations
  223. 21 Type Qualifiers
  224. 21.1 ‘const’ Variables and Fields
  225. 21.2 ‘volatile’ Variables and Fields
  226. 21.3 ‘restrict’-Qualified Pointers
  227. 21.4 ‘restrict’ Pointer Example
  228. 22 Functions
  229. 22.1 Function Definitions
  230. 22.1.1 Function Parameter Variables
  231. 22.1.2 Forward Function Declarations
  232. 22.1.3 Static Functions
  233. 22.1.4 Arrays as Parameters
  234. 22.1.4.1 Array parameters are pointers
  235. 22.1.4.2 Passing array arguments
  236. 22.1.4.3 Type qualifiers on array parameters
  237. 22.1.5 Functions That Accept Structure Arguments
  238. 22.2 Function Declarations
  239. 22.3 Function Calls
  240. 22.4 Function Call Semantics
  241. 22.5 Function Pointers
  242. 22.5.1 Declaring Function Pointers
  243. 22.5.2 Assigning Function Pointers
  244. 22.5.3 Calling Function Pointers
  245. 22.6 The ‘main’ Function
  246. 22.6.1 Returning Values from ‘main’
  247. 22.6.2 Accessing Command-line Parameters
  248. 22.6.3 Accessing Environment Variables
  249. 22.7 Advanced Function Features
  250. 22.7.1 Variable-Length Array Parameters
  251. 22.7.2 Variable-Length Parameter Lists
  252. 22.7.3 Nested Functions
  253. 22.7.4 Inline Function Definitions
  254. 22.8 Obsolete Function Features
  255. 22.8.1 Older GNU C Inlining
  256. 22.8.2 Old-Style Function Definitions
  257. 23 Compatible Types
  258. 24 Type Conversions
  259. 24.1 Explicit Type Conversion
  260. 24.2 Assignment Type Conversions
  261. 24.3 Argument Promotions
  262. 24.4 Operand Promotions
  263. 24.5 Common Type
  264. 25 Scope
  265. 26 Preprocessing
  266. 26.1 Preprocessing Overview
  267. 26.2 Directives
  268. 26.3 Preprocessing Tokens
  269. 26.4 Header Files
  270. 26.4.1 ‘#include’ Syntax
  271. 26.4.2 ‘#include’ Operation
  272. 26.4.3 Search Path
  273. 26.4.4 Once-Only Headers
  274. 26.4.5 Computed Includes
  275. 26.5 Macros
  276. 26.5.1 Object-like Macros
  277. 26.5.2 Function-like Macros
  278. 26.5.3 Macro Arguments
  279. 26.5.4 Stringification
  280. 26.5.5 Concatenation
  281. 26.5.6 Variadic Macros
  282. 26.5.7 Predefined Macros
  283. 26.5.8 Undefining and Redefining Macros
  284. 26.5.9 Directives Within Macro Arguments
  285. 26.5.10 Macro Pitfalls
  286. 26.5.10.1 Misnesting
  287. 26.5.10.2 Operator Precedence Problems
  288. 26.5.10.3 Swallowing the Semicolon
  289. 26.5.10.4 Duplication of Side Effects
  290. 26.5.10.5 Using ‘__auto_type’ for Local Variables
  291. 26.5.10.6 Self-Referential Macros
  292. 26.5.10.7 Argument Prescan
  293. 26.6 Conditionals
  294. 26.6.1 Uses of Conditional Directives
  295. 26.6.2 Syntax of Preprocessing Conditionals
  296. 26.6.2.1 The ‘#ifdef’ directive
  297. 26.6.2.2 The ‘#if’ directive
  298. 26.6.2.3 The ‘defined’ test
  299. 26.6.2.4 The ‘#else’ directive
  300. 26.6.2.5 The ‘#elif’ directive
  301. 26.6.3 Deleted Code
  302. 26.7 Diagnostics
  303. 26.8 Line Control
  304. 26.9 Null Directive
  305. 27 Integers in Depth
  306. 27.1 Integer Representations
  307. 27.2 Maximum and Minimum Values
  308. 28 Floating Point in Depth
  309. 28.1 Floating-Point Representations
  310. 28.2 Floating-Point Type Specifications
  311. 28.3 Special Floating-Point Values
  312. 28.4 Invalid Optimizations
  313. 28.5 Floating Arithmetic Exception Flags
  314. 28.6 Exact Floating-Point Arithmetic
  315. 28.7 Rounding
  316. 28.8 Rounding Issues
  317. 28.9 Significance Loss
  318. 28.10 Fused Multiply-Add
  319. 28.11 Error Recovery
  320. 28.12 Exact Floating-Point Constants
  321. 28.13 Handling Infinity
  322. 28.14 Handling NaN
  323. 28.15 Signed Zeros
  324. 28.16 Scaling by Powers of the Base
  325. 28.17 Rounding Control
  326. 28.18 Machine Epsilon
  327. 28.19 Complex Arithmetic
  328. 28.20 Round-Trip Base Conversion
  329. 28.21 Further Reading
  330. 29 Compilation
  331. 30 Directing Compilation
  332. 30.1 Pragmas
  333. 30.1.1 Pragma Basics
  334. 30.1.2 Severity Pragmas
  335. 30.1.3 Optimization Pragmas
  336. 30.2 Static Assertions
  337. Appendix A Type Alignment
  338. Appendix B Aliasing
  339. B.1 Aliasing and Alignment
  340. B.2 Aliasing and Length
  341. B.3 Type Rules for Aliasing
  342. Appendix C Digraphs
  343. Appendix D Attributes in Declarations
  344. Appendix E Signals
  345. Appendix F GNU Free Documentation License
  346. Index of Symbols and Keywords
  347. Concept Index
  348. GNU C Manual
  349. ************
  350. This manual explains the C language for use with the GNU Compiler
  351. Collection (GCC) on the GNU/Linux system and other systems. We refer to
  352. this dialect as GNU C. If you already know C, you can use this as a
  353. reference manual.
  354. If you understand basic concepts of programming but know nothing
  355. about C, you can read this manual sequentially from the beginning to
  356. learn the C language.
  357. If you are a beginner to programming, we recommend you first learn a
  358. language with automatic garbage collection and no explicit pointers,
  359. rather than starting with C. Good choices include Lisp, Scheme, Python
  360. and Java. C’s explicit pointers mean that programmers must be careful
  361. to avoid certain kinds of errors.
  362. C is a venerable language; it was first used in 1973. The GNU C
  363. Compiler, which was subsequently extended into the GNU Compiler
  364. Collection, was first released in 1987. Other important languages were
  365. designed based on C: once you know C, it gives you a useful base for
  366. learning C++, C#, Java, Scala, D, Go, and more.
  367. The special advantage of C is that it is fairly simple while allowing
  368. close access to the computer’s hardware, which previously required
  369. writing in assembler language to describe the individual machine
  370. instructions. Some have called C a “high-level assembler language”
  371. because of its explicit pointers and lack of automatic management of
  372. storage. As one wag put it, “C combines the power of assembler language
  373. with the convenience of assembler language.” However, C is far more
  374. portable, and much easier to read and write, than assembler language.
  375. This manual describes the GNU C language supported by the GNU
  376. Compiler Collection, as of roughly 2017. Please inform us of any
  377. changes needed to match the current version of GNU C.
  378. When a construct may be absent or work differently in other C
  379. compilers, we say so. When it is not part of ISO standard C, we say it
  380. is a “GNU C extension,” because it is useful to know that. However,
  381. standards and other dialects are secondary topics for this manual. For
  382. simplicity’s sake, we keep those notes short, unless it is vital to say
  383. more.
  384. Likewise, we hardly mention C++ or other languages that the GNU
  385. Compiler Collection supports. We hope this manual will serve as a base
  386. for writing manuals for those languages, but languages so different
  387. can’t share one common manual.
  388. Some aspects of the meaning of C programs depend on the target
  389. platform: which computer, and which operating system, the compiled code
  390. will run on. Where this is the case, we say so.
  391. The C language provides no built-in facilities for performing such
  392. common operations as input/output, memory management, string
  393. manipulation, and the like. Instead, these facilities are defined in a
  394. standard library, which is automatically available in every C program.
  395. *Note The GNU C Library: (libc)Top.
  396. This manual incorporates the former GNU C Preprocessor Manual, which
  397. was among the earliest GNU Manuals. It also uses some text from the
  398. earlier GNU C Manual that was written by Trevis Rothwell and James
  399. Youngman.
  400. GNU C has many obscure features, each one either for historical
  401. compatibility or meant for very special situations. We have left them
  402. to a companion manual, the GNU C Obscurities Manual, which will be
  403. published digitally later.
  404. Please report errors and suggestions to c-manual@gnu.org.
  405. 1 The First Example
  406. *******************
  407. This chapter presents the source code for a very simple C program and
  408. uses it to explain a few features of the language. If you already know
  409. the basic points of C presented in this chapter, you can skim it or skip
  410. it.
  411. 1.1 Example: Recursive Fibonacci
  412. ================================
  413. To introduce the most basic features of C, let’s look at code for a
  414. simple mathematical function that does calculations on integers. This
  415. function calculates the Nth number in the Fibonacci series, in which
  416. each number is the sum of the previous two: 1, 1, 2, 3, 5, 8, 13, 21,
  417. 34, 55, ....
  418. int
  419. fib (int n)
  420. {
  421. if (n >=2) /* This avoids infinite recursion. */
  422. return 1;
  423. else
  424. return fib (n - 1) + fib (n - 2);
  425. }
  426. This very simple program illustrates several features of C:
  427. • A function definition, whose first two lines constitute the
  428. function header. *Note Function Definitions::.
  429. • A function parameter ‘n’, referred to as the variable ‘n’ inside
  430. the function body. *Note Function Parameter Variables::. A
  431. function definition uses parameters to refer to the argument values
  432. provided in a call to that function.
  433. • Arithmetic. C programs add with ‘+’ and subtract with ‘-’. *Note
  434. Arithmetic::.
  435. • Numeric comparisons. The operator ‘>=’ tests for “less than or
  436. equal.” *Note Numeric Comparisons::.
  437. • Integer constants written in base 10. *Note Integer Constants::.
  438. • A function call. The function call ‘fib (n - 1)’ calls the
  439. function ‘fib’, passing as its argument the value ‘n - 1’. *Note
  440. Function Calls::.
  441. • A comment, which starts with ‘/*’ and ends with ‘*/’. The comment
  442. has no effect on the execution of the program. Its purpose is to
  443. provide explanations to people reading the source code. Including
  444. comments in the code is tremendously important—they provide
  445. background information so others can understand the code more
  446. quickly. *Note Comments::.
  447. • Two kinds of statements, the ‘return’ statement and the
  448. ‘if’...‘else’ statement. *Note Statements::.
  449. • Recursion. The function ‘fib’ calls itself; that is called a
  450. “recursive call”. These are valid in C, and quite common.
  451. The ‘fib’ function would not be useful if it didn’t return. Thus,
  452. recursive definitions, to be of any use, must avoid infinite
  453. recursion.
  454. This function definition prevents infinite recursion by specially
  455. handling the case where ‘n’ is two or less. Thus the maximum depth
  456. of recursive calls is less than ‘n’.
  457. 1.1.1 Function Header
  458. ---------------------
  459. In our example, the first two lines of the function definition are the
  460. “header”. Its purpose is to state the function’s name and say how it is
  461. called:
  462. int
  463. fib (int n)
  464. says that the function returns an integer (type ‘int’), its name is
  465. ‘fib’, and it takes one argument named ‘n’ which is also an integer.
  466. (Data types will be explained later, in *note Primitive Types::.)
  467. 1.1.2 Function Body
  468. -------------------
  469. The rest of the function definition is called the “function body”. Like
  470. every function body, this one starts with ‘{’, ends with ‘}’, and
  471. contains zero or more “statements” and “declarations”. Statements
  472. specify actions to take, whereas declarations define names of variables,
  473. functions, and so on. Each statement and each declaration ends with a
  474. semicolon (‘;’).
  475. Statements and declarations often contain “expressions”; an
  476. expression is a construct whose execution produces a “value” of some
  477. data type, but may also take actions through “side effects” that alter
  478. subsequent execution. A statement, by contrast, does not have a value;
  479. it affects further execution of the program only through the actions it
  480. takes.
  481. This function body contains no declarations, and just one statement,
  482. but that one is a complex statement in that it contains nested
  483. statements. This function uses two kinds of statements:
  484. ‘return’
  485. The ‘return’ statement makes the function return immediately. It
  486. looks like this:
  487. return VALUE;
  488. Its meaning is to compute the expression VALUE and exit the
  489. function, making it return whatever value that expression produced.
  490. For instance,
  491. return 1;
  492. returns the integer 1 from the function, and
  493. return fib (n - 1) + fib (n - 2);
  494. returns a value computed by performing two function calls as
  495. specified and adding their results.
  496. ‘if...else’
  497. The ‘if’...‘else’ statement is a “conditional”. Each time it
  498. executes, it chooses one of its two substatements to execute and
  499. ignores the other. It looks like this:
  500. if (CONDITION)
  501. IF-TRUE-STATEMENT
  502. else
  503. IF-FALSE-STATEMENT
  504. Its meaning is to compute the expression CONDITION and, if it’s
  505. “true,” execute IF-TRUE-STATEMENT. Otherwise, execute
  506. IF-FALSE-STATEMENT. *Note if-else Statement::.
  507. Inside the ‘if’...‘else’ statement, CONDITION is simply an
  508. expression. It’s considered “true” if its value is nonzero. (A
  509. comparison operation, such as ‘n >=2’, produces the value 1 if
  510. it’s “true” and 0 if it’s “false.” *Note Numeric Comparisons::.)
  511. Thus,
  512. if (n >=2)
  513. return 1;
  514. else
  515. return fib (n - 1) + fib (n - 2);
  516. first tests whether the value of ‘n’ is less than or equal to 2.
  517. If so, the expression ‘n >=2’ has the value 1. So execution
  518. continues with the statement
  519. return 1;
  520. Otherwise, execution continues with this statement:
  521. return fib (n - 1) + fib (n - 2);
  522. Each of these statements ends the execution of the function and
  523. provides a value for it to return. *Note return Statement::.
  524. Calculating ‘fib’ using ordinary integers in C works only for N < 47,
  525. because the value of ‘fib (47)’ is too large to fit in type ‘int’. The
  526. addition operation that tries to add ‘fib (46)’ and ‘fib (45)’ cannot
  527. deliver the correct result. This occurrence is called “integer
  528. overflow”.
  529. Overflow can manifest itself in various ways, but one thing that
  530. can’t possibly happen is to produce the correct value, since that can’t
  531. fit in the space for the value. *Note Integer Overflow::.
  532. *Note Functions::, for a full explanation about functions.
  533. 1.2 The Stack, And Stack Overflow
  534. =================================
  535. Recursion has a drawback: there are limits to how many nested function
  536. calls a program can make. In C, each function call allocates a block of
  537. memory which it uses until the call returns. C allocates these blocks
  538. consecutively within a large area of memory known as the “stack”, so we
  539. refer to the blocks as “stack frames”.
  540. The size of the stack is limited; if the program tries to use too
  541. much, that causes the program to fail because the stack is full. This
  542. is called “stack overflow”.
  543. Stack overflow on GNU/Linux typically manifests itself as the
  544. “signal” named ‘SIGSEGV’, also known as a “segmentation fault.” By
  545. default, this signal terminates the program immediately, rather than
  546. letting the program try to recover, or reach an expected ending point.
  547. (We commonly say in this case that the program “crashes”). *Note
  548. Signals::.
  549. It is inconvenient to observe a crash by passing too large an
  550. argument to recursive Fibonacci, because the program would run a long
  551. time before it crashes. This algorithm is simple but ridiculously slow:
  552. in calculating ‘fib (N)’, the number of (recursive) calls ‘fib (1)’ or
  553. ‘fib (2)’ that it makes equals the final result.
  554. However, you can observe stack overflow very quickly if you use this
  555. function instead:
  556. int
  557. fill_stack (int n)
  558. {
  559. if (n >=1) /* This limits the depth of recursion. */
  560. return 1;
  561. else
  562. return fill_stack (n - 1);
  563. }
  564. Under gNewSense GNU/Linux on the Lemote Yeeloong, without
  565. optimization and using the default configuration, an experiment showed
  566. there is enough stack space to do 261906 nested calls to that function.
  567. One more, and the stack overflows and the program crashes. On another
  568. platform, with a different configuration, or with a different function,
  569. the limit might be bigger or smaller.
  570. 1.3 Example: Iterative Fibonacci
  571. ================================
  572. Here’s a much faster algorithm for computing the same Fibonacci series.
  573. It is faster for two reasons. First, it uses “iteration” (that is,
  574. repetition or looping) rather than recursion, so it doesn’t take time
  575. for a large number of function calls. But mainly, it is faster because
  576. the number of repetitions is small—only ‘N’.
  577. int
  578. fib (int n)
  579. {
  580. int last = 1; /* Initial value is ‘fib (1)’. */
  581. int prev = 0; /* Initial value controls ‘fib (2)’. */
  582. int i;
  583. for (i = 1; i < n; ++i)
  584. /* If ‘n’ is 1 or less, the loop runs zero times, */
  585. /* since ‘i < n’ is false the first time. */
  586. {
  587. /* Now ‘last’ is ‘fib (i)’
  588. and ‘prev’ is ‘fib (i − 1)’. */
  589. /* Compute ‘fib (i + 1)’. */
  590. int next = prev + last;
  591. /* Shift the values down. */
  592. prev = last;
  593. last = next;
  594. /* Now ‘last’ is ‘fib (i + 1)’
  595. and ‘prev’ is ‘fib (i)’.
  596. But that won’t stay true for long,
  597. because we are about to increment ‘i’. */
  598. }
  599. return last;
  600. }
  601. This definition computes ‘fib (N)’ in a time proportional to ‘N’.
  602. The comments in the definition explain how it works: it advances through
  603. the series, always keeps the last two values in ‘last’ and ‘prev’, and
  604. adds them to get the next value.
  605. Here are the additional C features that this definition uses:
  606. Internal blocks
  607. Within a function, wherever a statement is called for, you can
  608. write a “block”. It looks like ‘{ ... }’ and contains zero or more
  609. statements and declarations. (You can also use additional blocks
  610. as statements in a block.)
  611. The function body also counts as a block, which is why it can
  612. contain statements and declarations.
  613. *Note Blocks::.
  614. Declarations of local variables
  615. This function body contains declarations as well as statements.
  616. There are three declarations directly in the function body, as well
  617. as a fourth declaration in an internal block. Each starts with
  618. ‘int’ because it declares a variable whose type is integer. One
  619. declaration can declare several variables, but each of these
  620. declarations is simple and declares just one variable.
  621. Variables declared inside a block (either a function body or an
  622. internal block) are “local variables”. These variables exist only
  623. within that block; their names are not defined outside the block,
  624. and exiting the block deallocates their storage. This example
  625. declares four local variables: ‘last’, ‘prev’, ‘i’, and ‘next’.
  626. The most basic local variable declaration looks like this:
  627. TYPE VARIABLENAME;
  628. For instance,
  629. int i;
  630. declares the local variable ‘i’ as an integer. *Note Variable
  631. Declarations::.
  632. Initializers
  633. When you declare a variable, you can also specify its initial
  634. value, like this:
  635. TYPE VARIABLENAME = VALUE;
  636. For instance,
  637. int last = 1;
  638. declares the local variable ‘last’ as an integer (type ‘int’) and
  639. starts it off with the value 1. *Note Initializers::.
  640. Assignment
  641. Assignment: a specific kind of expression, written with the ‘=’
  642. operator, that stores a new value in a variable or other place.
  643. Thus,
  644. VARIABLE = VALUE
  645. is an expression that computes ‘VALUE’ and stores the value in
  646. ‘VARIABLE’. *Note Assignment Expressions::.
  647. Expression statements
  648. An expression statement is an expression followed by a semicolon.
  649. That computes the value of the expression, then ignores the value.
  650. An expression statement is useful when the expression changes some
  651. data or has other side effects—for instance, with function calls,
  652. or with assignments as in this example. *Note Expression
  653. Statement::.
  654. Using an expression with no side effects in an expression statement
  655. is pointless except in very special cases. For instance, the
  656. expression statement ‘x;’ would examine the value of ‘x’ and ignore
  657. it. That is not useful.
  658. Increment operator
  659. The increment operator is ‘++’. ‘++i’ is an expression that is
  660. short for ‘i = i + 1’. *Note Increment/Decrement::.
  661. ‘for’ statements
  662. A ‘for’ statement is a clean way of executing a statement
  663. repeatedly—a “loop” (*note Loop Statements::). Specifically,
  664. for (i = 1; i < n; ++i)
  665. BODY
  666. means to start by doing ‘i = 1’ (set ‘i’ to one) to prepare for the
  667. loop. The loop itself consists of
  668. • Testing ‘i < n’ and exiting the loop if that’s false.
  669. • Executing BODY.
  670. • Advancing the loop (executing ‘++i’, which increments ‘i’).
  671. The net result is to execute BODY with 1 in ‘i’, then with 2 in
  672. ‘i’, and so on, stopping just before the repetition where ‘i’ would
  673. equal ‘n’. If ‘n’ is less than 1, the loop will execute the body
  674. zero times.
  675. The body of the ‘for’ statement must be one and only one statement.
  676. You can’t write two statements in a row there; if you try to, only
  677. the first of them will be treated as part of the loop.
  678. The way to put multiple statements in such a place is to group them
  679. with a block, and that’s what we do in this example.
  680. 2 A Complete Program
  681. ********************
  682. It’s all very well to write a Fibonacci function, but you cannot run it
  683. by itself. It is a useful program, but it is not a complete program.
  684. In this chapter we present a complete program that contains the ‘fib’
  685. function. This example shows how to make the program start, how to make
  686. it finish, how to do computation, and how to print a result.
  687. 2.1 Complete Program Example
  688. ============================
  689. Here is the complete program that uses the simple, recursive version of
  690. the ‘fib’ function (*note Recursive Fibonacci::):
  691. #include <stdio.h>
  692. int
  693. fib (int n)
  694. {
  695. if (n >=2) /* This avoids infinite recursion. */
  696. return 1;
  697. else
  698. return fib (n - 1) + fib (n - 2);
  699. }
  700. int
  701. main (void)
  702. {
  703. printf ("Fibonacci series item %d is %d\n",
  704. 20, fib (20));
  705. return 0;
  706. }
  707. This program prints a message that shows the value of ‘fib (20)’.
  708. Now for an explanation of what that code means.
  709. 2.2 Complete Program Explanation
  710. ================================
  711. Here’s the explanation of the code of the example in the previous
  712. section.
  713. This sample program prints a message that shows the value of ‘fib
  714. (20)’, and exits with code 0 (which stands for successful execution).
  715. Every C program is started by running the function named ‘main’.
  716. Therefore, the example program defines a function named ‘main’ to
  717. provide a way to start it. Whatever that function does is what the
  718. program does. *Note The main Function::.
  719. The ‘main’ function is the first one called when the program runs,
  720. but it doesn’t come first in the example code. The order of the
  721. function definitions in the source code makes no difference to the
  722. program’s meaning.
  723. The initial call to ‘main’ always passes certain arguments, but
  724. ‘main’ does not have to pay attention to them. To ignore those
  725. arguments, define ‘main’ with ‘void’ as the parameter list. (‘void’ as
  726. a function’s parameter list normally means “call with no arguments,” but
  727. ‘main’ is a special case.)
  728. The function ‘main’ returns 0 because that is the conventional way
  729. for ‘main’ to indicate successful execution. It could instead return a
  730. positive integer to indicate failure, and some utility programs have
  731. specific conventions for the meaning of certain numeric “failure codes”.
  732. *Note Values from main::.
  733. The simplest way to print text in C is by calling the ‘printf’
  734. function, so here we explain what that does.
  735. The first argument to ‘printf’ is a “string constant” (*note String
  736. Constants::) that is a template for output. The function ‘printf’
  737. copies most of that string directly as output, including the newline
  738. character at the end of the string, which is written as ‘\n’. The
  739. output goes to the program’s “standard output” destination, which in the
  740. usual case is the terminal.
  741. ‘%’ in the template introduces a code that substitutes other text
  742. into the output. Specifically, ‘%d’ means to take the next argument to
  743. ‘printf’ and substitute it into the text as a decimal number. (The
  744. argument for ‘%d’ must be of type ‘int’; if it isn’t, ‘printf’ will
  745. malfunction.) So the output is a line that looks like this:
  746. Fibonacci series item 20 is 6765
  747. This program does not contain a definition for ‘printf’ because it is
  748. defined by the C library, which makes it available in all C programs.
  749. However, each program does need to “declare” ‘printf’ so it will be
  750. called correctly. The ‘#include’ line takes care of that; it includes a
  751. “header file” called ‘stdio.h’ into the program’s code. That file is
  752. provided by the operating system and it contains declarations for the
  753. many standard input/output functions in the C library, one of which is
  754. ‘printf’.
  755. Don’t worry about header files for now; we’ll explain them later in
  756. *note Header Files::.
  757. The first argument of ‘printf’ does not have to be a string constant;
  758. it can be any string (*note Strings::). However, using a constant is
  759. the most common case.
  760. To learn more about ‘printf’ and other facilities of the C library,
  761. see *note The GNU C Library: (libc)Top.
  762. 2.3 Complete Program, Line by Line
  763. ==================================
  764. Here’s the same example, explained line by line. *Beginners, do you
  765. find this helpful or not? Would you prefer a different layout for the
  766. example? Please tell rms@gnu.org.*
  767. #include <stdio.h> /* Include declaration of usual */
  768. /* I/O functions such as ‘printf’. */
  769. /* Most programs need these. */
  770. int /* This function returns an ‘int’. */
  771. fib (int n) /* Its name is ‘fib’; */
  772. /* its argument is called ‘n’. */
  773. { /* Start of function body. */
  774. /* This stops the recursion from being infinite. */
  775. if (n >=2) /* If ‘n’ is 1 or 2, */
  776. return 1; /* make ‘fib’ return 1. */
  777. else /* otherwise, add the two previous */
  778. /* fibonacci numbers. */
  779. return fib (n - 1) + fib (n - 2);
  780. }
  781. int /* This function returns an ‘int’. */
  782. main (void) /* Start here; ignore arguments. */
  783. { /* Print message with numbers in it. */
  784. printf ("Fibonacci series item %d is %d\n",
  785. 20, fib (20));
  786. return 0; /* Terminate program, report success. */
  787. }
  788. 2.4 Compiling the Example Program
  789. =================================
  790. To run a C program requires converting the source code into an
  791. “executable file”. This is called “compiling” the program, and the
  792. command to do that using GNU C is ‘gcc’.
  793. This example program consists of a single source file. If we call
  794. that file ‘fib1.c’, the complete command to compile it is this:
  795. gcc -g -O -o fib1 fib1.c
  796. Here, ‘-g’ says to generate debugging information, ‘-O’ says to optimize
  797. at the basic level, and ‘-o fib1’ says to put the executable program in
  798. the file ‘fib1’.
  799. To run the program, use its file name as a shell command. For
  800. instance,
  801. ./fib1
  802. However, unless you are sure the program is correct, you should expect
  803. to need to debug it. So use this command,
  804. gdb fib1
  805. which starts the GDB debugger (*note Sample Session: (gdb)Sample
  806. Session.) so you can run and debug the executable program ‘fib1’.
  807. *Note Compilation::, for an introduction to compiling more complex
  808. programs which consist of more than one source file.
  809. 3 Storage and Data
  810. ******************
  811. Storage in C programs is made up of units called “bytes”. On nearly all
  812. computers, a byte consists of 8 bits, but there are a few peculiar
  813. computers (mostly “embedded controllers” for very small systems) where a
  814. byte is longer than that. This manual does not try to explain the
  815. peculiarity of those computers; we assume that a byte is 8 bits.
  816. Every C data type is made up of a certain number of bytes; that
  817. number is the data type’s “size”. *Note Type Size::, for details. The
  818. types ‘signed char’ and ‘unsigned char’ are one byte long; use those
  819. types to operate on data byte by byte. *Note Signed and Unsigned
  820. Types::. You can refer to a series of consecutive bytes as an array of
  821. ‘char’ elements; that’s what an ASCII string looks like in memory.
  822. *Note String Constants::.
  823. 4 Beyond Integers
  824. *****************
  825. So far we’ve presented programs that operate on integers. In this
  826. chapter we’ll present examples of handling non-integral numbers and
  827. arrays of numbers.
  828. 4.1 An Example with Non-Integer Numbers
  829. =======================================
  830. Here’s a function that operates on and returns “floating point” numbers
  831. that don’t have to be integers. Floating point represents a number as a
  832. fraction together with a power of 2. (For more detail, *note
  833. Floating-Point Data Types::.) This example calculates the average of
  834. three floating point numbers that are passed to it as arguments:
  835. double
  836. average_of_three (double a, double b, double c)
  837. {
  838. return (a + b + c) / 3;
  839. }
  840. The values of the parameter A, B and C do not have to be integers,
  841. and even when they happen to be integers, most likely their average is
  842. not an integer.
  843. ‘double’ is the usual data type in C for calculations on
  844. floating-point numbers.
  845. To print a ‘double’ with ‘printf’, we must use ‘%f’ instead of ‘%d’:
  846. printf ("Average is %f\n",
  847. average_of_three (1.1, 9.8, 3.62));
  848. The code that calls ‘printf’ must pass a ‘double’ for printing with
  849. ‘%f’ and an ‘int’ for printing with ‘%d’. If the argument has the wrong
  850. type, ‘printf’ will produce garbage output.
  851. Here’s a complete program that computes the average of three specific
  852. numbers and prints the result:
  853. double
  854. average_of_three (double a, double b, double c)
  855. {
  856. return (a + b + c) / 3;
  857. }
  858. int
  859. main (void)
  860. {
  861. printf ("Average is %f\n",
  862. average_of_three (1.1, 9.8, 3.62));
  863. return 0;
  864. }
  865. From now on we will not present examples of calls to ‘main’. Instead
  866. we encourage you to write them for yourself when you want to test
  867. executing some code.
  868. 4.2 An Example with Arrays
  869. ==========================
  870. A function to take the average of three numbers is very specific and
  871. limited. A more general function would take the average of any number
  872. of numbers. That requires passing the numbers in an array. An array is
  873. an object in memory that contains a series of values of the same data
  874. type. This chapter presents the basic concepts and use of arrays
  875. through an example; for the full explanation, see *note Arrays::.
  876. Here’s a function definition to take the average of several
  877. floating-point numbers, passed as type ‘double’. The first parameter,
  878. ‘length’, specifies how many numbers are passed. The second parameter,
  879. ‘input_data’, is an array that holds those numbers.
  880. double
  881. avg_of_double (int length, double input_data[])
  882. {
  883. double sum = 0;
  884. int i;
  885. for (i = 0; i < length; i++)
  886. sum = sum + input_data[i];
  887. return sum / length;
  888. }
  889. This introduces the expression to refer to an element of an array:
  890. ‘input_data[i]’ means the element at index ‘i’ in ‘input_data’. The
  891. index of the element can be any expression with an integer value; in
  892. this case, the expression is ‘i’. *Note Accessing Array Elements::.
  893. The lowest valid index in an array is 0, _not_ 1, and the highest
  894. valid index is one less than the number of elements. (This is known as
  895. “zero-origin indexing”.)
  896. This example also introduces the way to declare that a function
  897. parameter is an array. Such declarations are modeled after the syntax
  898. for an element of the array. Just as ‘double foo’ declares that ‘foo’
  899. is of type ‘double’, ‘double input_data[]’ declares that each element of
  900. ‘input_data’ is of type ‘double’. Therefore, ‘input_data’ itself has
  901. type “array of ‘double’.”
  902. When declaring an array parameter, it’s not necessary to say how long
  903. the array is. In this case, the parameter ‘input_data’ has no length
  904. information. That’s why the function needs another parameter, ‘length’,
  905. for the caller to provide that information to the function
  906. ‘avg_of_double’.
  907. 4.3 Calling the Array Example
  908. =============================
  909. To call the function ‘avg_of_double’ requires making an array and then
  910. passing it as an argument. Here is an example.
  911. {
  912. /* The array of values to average. */
  913. double nums_to_average[5];
  914. /* The average, once we compute it. */
  915. double average;
  916. /* Fill in elements of ‘nums_to_average’. */
  917. nums_to_average[0] = 58.7;
  918. nums_to_average[1] = 5.1;
  919. nums_to_average[2] = 7.7;
  920. nums_to_average[3] = 105.2;
  921. nums_to_average[4] = -3.14159;
  922. average = avg_of_double (5, nums_to_average);
  923. /* ...now make use of ‘average’... */
  924. }
  925. This shows an array subscripting expression again, this time on the
  926. left side of an assignment, storing a value into an element of an array.
  927. It also shows how to declare a local variable that is an array:
  928. ‘double nums_to_average[5];’. Since this declaration allocates the
  929. space for the array, it needs to know the array’s length. You can
  930. specify the length with any expression whose value is an integer, but in
  931. this declaration the length is a constant, the integer 5.
  932. The name of the array, when used by itself as an expression, stands
  933. for the address of the array’s data, and that’s what gets passed to the
  934. function ‘avg_of_double’ in ‘avg_of_double (5, nums_to_average)’.
  935. We can make the code easier to maintain by avoiding the need to write
  936. 5, the array length, when calling ‘avg_of_double’. That way, if we
  937. change the array to include more elements, we won’t have to change that
  938. call. One way to do this is with the ‘sizeof’ operator:
  939. average = avg_of_double ((sizeof (nums_to_average)
  940. / sizeof (nums_to_average[0])),
  941. nums_to_average);
  942. This computes the number of elements in ‘nums_to_average’ by dividing
  943. its total size by the size of one element. *Note Type Size::, for more
  944. details of using ‘sizeof’.
  945. We don’t show in this example what happens after storing the result
  946. of ‘avg_of_double’ in the variable ‘average’. Presumably more code
  947. would follow that uses that result somehow. (Why compute the average
  948. and not use it?) But that isn’t part of this topic.
  949. 4.4 Variations for Array Example
  950. ================================
  951. The code to call ‘avg_of_double’ has two declarations that start with
  952. the same data type:
  953. /* The array of values to average. */
  954. double nums_to_average[5];
  955. /* The average, once we compute it. */
  956. double average;
  957. In C, you can combine the two, like this:
  958. double nums_to_average[5], average;
  959. This declares ‘nums_to_average’ so each of its elements is a
  960. ‘double’, and ‘average’ so that it simply is a ‘double’.
  961. However, while you _can_ combine them, that doesn’t mean you
  962. _should_. If it is useful to write comments about the variables, and
  963. usually it is, then it’s clearer to keep the declarations separate so
  964. you can put a comment on each one.
  965. We set all of the elements of the array ‘nums_to_average’ with
  966. assignments, but it is more convenient to use an initializer in the
  967. declaration:
  968. {
  969. /* The array of values to average. */
  970. double nums_to_average[]
  971. = { 58.7, 5.1, 7.7, 105.2, -3.14159 };
  972. /* The average, once we compute it. */
  973. average = avg_of_double ((sizeof (nums_to_average)
  974. / sizeof (nums_to_average[0])),
  975. nums_to_average);
  976. /* ...now make use of ‘average’... */
  977. }
  978. The array initializer is a comma-separated list of values, delimited
  979. by braces. *Note Initializers::.
  980. Note that the declaration does not specify a size for
  981. ‘nums_to_average’, so the size is determined from the initializer.
  982. There are five values in the initializer, so ‘nums_to_average’ gets
  983. length 5. If we add another element to the initializer,
  984. ‘nums_to_average’ will have six elements.
  985. Because the code computes the number of elements from the size of the
  986. array, using ‘sizeof’, the program will operate on all the elements in
  987. the initializer, regardless of how many those are.
  988. 5 Lexical Syntax
  989. ****************
  990. To start the full description of the C language, we explain the lexical
  991. syntax and lexical units of C code. The lexical units of a programming
  992. language are known as “tokens”. This chapter covers all the tokens of C
  993. except for constants, which are covered in a later chapter (*note
  994. Constants::). One vital kind of token is the “identifier” (*note
  995. Identifiers::), which is used for names of any kind.
  996. 5.1 Write Programs in English!
  997. ==============================
  998. In principle, you can write the function and variable names in a
  999. program, and the comments, in any human language. C allows any kinds of
  1000. characters in comments, and you can put non-ASCII characters into
  1001. identifiers with a special prefix. However, to enable programmers in
  1002. all countries to understand and develop the program, it is best given
  1003. today’s circumstances to write identifiers and comments in English.
  1004. English is the one language that programmers in all countries
  1005. generally study. If a program’s names are in English, most programmers
  1006. in Bangladesh, Belgium, Bolivia, Brazil, and Bulgaria can understand
  1007. them. Most programmers in those countries can speak English, or at
  1008. least read it, but they do not read each other’s languages at all. In
  1009. India, with so many languages, two programmers may have no common
  1010. language other than English.
  1011. If you don’t feel confident in writing English, do the best you can,
  1012. and follow each English comment with a version in a language you write
  1013. better; add a note asking others to translate that to English. Someone
  1014. will eventually do that.
  1015. The program’s user interface is a different matter. We don’t need to
  1016. choose one language for that; it is easy to support multiple languages
  1017. and let each user choose the language to use. This requires writing the
  1018. program to support localization of its interface. (The ‘gettext’
  1019. package exists to support this; *note The GNU C Library: (libc)Message
  1020. Translation.) Then a community-based translation effort can provide
  1021. support for all the languages users want to use.
  1022. 5.2 Characters
  1023. ==============
  1024. GNU C source files are usually written in the ASCII character set, which
  1025. was defined in the 1960s for English. However, they can also include
  1026. Unicode characters represented in the UTF-8 multibyte encoding. This
  1027. makes it possible to represent accented letters such as ‘á’, as well as
  1028. other scripts such as Arabic, Chinese, Cyrillic, Hebrew, Japanese, and
  1029. Korean.(1)
  1030. In C source code, non-ASCII characters are valid in comments, in wide
  1031. character constants (*note Wide Character Constants::), and in string
  1032. constants (*note String Constants::).
  1033. Another way to specify non-ASCII characters in constants (character
  1034. or string) and identifiers is with an escape sequence starting with
  1035. backslash, specifying the intended Unicode character. (*Note Unicode
  1036. Character Codes::.) This specifies non-ASCII characters without putting
  1037. a real non-ASCII character in the source file itself.
  1038. C accepts two-character aliases called “digraphs” for certain
  1039. characters. *Note Digraphs::.
  1040. ---------- Footnotes ----------
  1041. (1) On some obscure systems, GNU C uses UTF-EBCDIC instead of UTF-8,
  1042. but that is not worth describing in this manual.
  1043. 5.3 Whitespace
  1044. ==============
  1045. Whitespace means characters that exist in a file but appear blank in a
  1046. printed listing of a file (or traditionally did appear blank, several
  1047. decades ago). The C language requires whitespace in order to separate
  1048. two consecutive identifiers, or to separate an identifier from a numeric
  1049. constant. Other than that, and a few special situations described
  1050. later, whitespace is optional; you can put it in when you wish, to make
  1051. the code easier to read.
  1052. Space and tab in C code are treated as whitespace characters. So are
  1053. line breaks. You can represent a line break with the newline character
  1054. (also called “linefeed” or LF), CR (carriage return), or the CRLF
  1055. sequence (two characters: carriage return followed by a newline
  1056. character).
  1057. The “formfeed” character, Control-L, was traditionally used to divide
  1058. a file into pages. It is still used this way in source code, and the
  1059. tools that generate nice printouts of source code still start a new page
  1060. after each “formfeed” character. Dividing code into pages separated by
  1061. formfeed characters is a good way to break it up into comprehensible
  1062. pieces and show other programmers where they start and end.
  1063. The “vertical tab” character, Control-K, was traditionally used to
  1064. make printing advance down to the next section of a page. We know of no
  1065. particular reason to use it in source code, but it is still accepted as
  1066. whitespace in C.
  1067. Comments are also syntactically equivalent to whitespace. *Note
  1068. Comments::.
  1069. 5.4 Comments
  1070. ============
  1071. A comment encapsulates text that has no effect on the program’s
  1072. execution or meaning.
  1073. The purpose of comments is to explain the code to people that read
  1074. it. Writing good comments for your code is tremendously important—they
  1075. should provide background information that helps programmers understand
  1076. the reasons why the code is written the way it is. You, returning to
  1077. the code six months from now, will need the help of these comments to
  1078. remember why you wrote it this way.
  1079. Outdated comments that become incorrect are counterproductive, so
  1080. part of the software developer’s responsibility is to update comments as
  1081. needed to correspond with changes to the program code.
  1082. C allows two kinds of comment syntax, the traditional style and the
  1083. C++ style. A traditional C comment starts with ‘/*’ and ends with ‘*/’.
  1084. For instance,
  1085. /* This is a comment in traditional C syntax. */
  1086. A traditional comment can contain ‘/*’, but these delimiters do not
  1087. nest as pairs. The first ‘*/’ ends the comment regardless of whether it
  1088. contains ‘/*’ sequences.
  1089. /* This /* is a comment */ But this is not! */
  1090. A “line comment” starts with ‘//’ and ends at the end of the line.
  1091. For instance,
  1092. // This is a comment in C++ style.
  1093. Line comments do nest, in effect, because ‘//’ inside a line comment
  1094. is part of that comment:
  1095. // this whole line is // one comment
  1096. This is code, not comment.
  1097. It is safe to put line comments inside block comments, or vice versa.
  1098. /* traditional comment
  1099. // contains line comment
  1100. more traditional comment
  1101. */ text here is not a comment
  1102. // line comment /* contains traditional comment */
  1103. But beware of commenting out one end of a traditional comment with a
  1104. line comment. The delimiter ‘/*’ doesn’t start a comment if it occurs
  1105. inside an already-started comment.
  1106. // line comment /* That would ordinarily begin a block comment.
  1107. Oops! The line comment has ended;
  1108. this isn't a comment any more. */
  1109. Comments are not recognized within string constants. "/* blah */" is
  1110. the string constant ‘/* blah */’, not an empty string.
  1111. In this manual we show the text in comments in a variable-width font,
  1112. for readability, but this font distinction does not exist in source
  1113. files.
  1114. A comment is syntactically equivalent to whitespace, so it always
  1115. separates tokens. Thus,
  1116. int/* comment */foo;
  1117. is equivalent to
  1118. int foo;
  1119. but clean code always uses real whitespace to separate the comment
  1120. visually from surrounding code.
  1121. 5.5 Identifiers
  1122. ===============
  1123. An “identifier” (name) in C is a sequence of letters and digits, as well
  1124. as ‘_’, that does not start with a digit. Most compilers also allow
  1125. ‘$’. An identifier can be as long as you like; for example,
  1126. int anti_dis_establishment_arian_ism;
  1127. Letters in identifiers are case-sensitive in C; thus, ‘a’ and ‘A’ are
  1128. two different identifiers.
  1129. Identifiers in C are used as variable names, function names, typedef
  1130. names, enumeration constants, type tags, field names, and labels.
  1131. Certain identifiers in C are “keywords”, which means they have specific
  1132. syntactic meanings. Keywords in C are “reserved words”, meaning you
  1133. cannot use them in any other way. For instance, you can’t define a
  1134. variable or function named ‘return’ or ‘if’.
  1135. You can also include other characters, even non-ASCII characters, in
  1136. identifiers by writing their Unicode character names, which start with
  1137. ‘\u’ or ‘\U’, in the identifier name. *Note Unicode Character Codes::.
  1138. However, it is usually a bad idea to use non-ASCII characters in
  1139. identifiers, and when they are written in English, they never need
  1140. non-ASCII characters. *Note English::.
  1141. Whitespace is required to separate two consecutive identifiers, or to
  1142. separate an identifier from a preceding or following numeric constant.
  1143. 5.6 Operators and Punctuation
  1144. =============================
  1145. Here we describe the lexical syntax of operators and punctuation in C.
  1146. The specific operators of C and their meanings are presented in
  1147. subsequent chapters.
  1148. Most operators in C consist of one or two characters that can’t be
  1149. used in identifiers. The characters used for operators in C are
  1150. ‘!~^&|*/%+-=><,.?:’.
  1151. Some operators are a single character. For instance, ‘-’ is the
  1152. operator for negation (with one operand) and the operator for
  1153. subtraction (with two operands).
  1154. Some operators are two characters. For example, ‘++’ is the
  1155. increment operator. Recognition of multicharacter operators works by
  1156. grouping together as many consecutive characters as can constitute one
  1157. operator.
  1158. For instance, the character sequence ‘++’ is always interpreted as
  1159. the increment operator; therefore, if we want to write two consecutive
  1160. instances of the operator ‘+’, we must separate them with a space so
  1161. that they do not combine as one token. Applying the same rule,
  1162. ‘a+++++b’ is always tokenized as ‘a++ ++ + b’, not as ‘a++ + ++b’, even
  1163. though the latter could be part of a valid C program and the former
  1164. could not (since ‘a++’ is not an lvalue and thus can’t be the operand of
  1165. ‘++’).
  1166. A few C operators are keywords rather than special characters. They
  1167. include ‘sizeof’ (*note Type Size::) and ‘_Alignof’ (*note Type
  1168. Alignment::).
  1169. The characters ‘;{}[]()’ are used for punctuation and grouping.
  1170. Semicolon (‘;’) ends a statement. Braces (‘{’ and ‘}’) begin and end a
  1171. block at the statement level (*note Blocks::), and surround the
  1172. initializer (*note Initializers::) for a variable with multiple elements
  1173. or components (such as arrays or structures).
  1174. Square brackets (‘[’ and ‘]’) do array indexing, as in ‘array[5]’.
  1175. Parentheses are used in expressions for explicit nesting of
  1176. expressions (*note Basic Arithmetic::), around the parameter
  1177. declarations in a function declaration or definition, and around the
  1178. arguments in a function call, as in ‘printf ("Foo %d\n", i)’ (*note
  1179. Function Calls::). Several kinds of statements also use parentheses as
  1180. part of their syntax—for instance, ‘if’ statements, ‘for’ statements,
  1181. ‘while’ statements, and ‘switch’ statements. *Note if Statement::, and
  1182. following sections.
  1183. Parentheses are also required around the operand of the operator
  1184. keywords ‘sizeof’ and ‘_Alignof’ when the operand is a data type rather
  1185. than a value. *Note Type Size::.
  1186. 5.7 Line Continuation
  1187. =====================
  1188. The sequence of a backslash and a newline is ignored absolutely anywhere
  1189. in a C program. This makes it possible to split a single source line
  1190. into multiple lines in the source file. GNU C tolerates and ignores
  1191. other whitespace between the backslash and the newline. In particular,
  1192. it always ignores a CR (carriage return) character there, in case some
  1193. text editor decided to end the line with the CRLF sequence.
  1194. The main use of line continuation in C is for macro definitions that
  1195. would be inconveniently long for a single line (*note Macros::).
  1196. It is possible to continue a line comment onto another line with
  1197. backslash-newline. You can put backslash-newline in the middle of an
  1198. identifier, even a keyword, or an operator. You can even split ‘/*’,
  1199. ‘*/’, and ‘//’ onto multiple lines with backslash-newline. Here’s an
  1200. ugly example:
  1201. /\
  1202. *
  1203. */ fo\
  1204. o +\
  1205. = 1\
  1206. 0;
  1207. That’s equivalent to ‘/* */ foo += 10;’.
  1208. Don’t do those things in real programs, since they make code hard to
  1209. read.
  1210. *Note:* For the sake of using certain tools on the source code, it is
  1211. wise to end every source file with a newline character which is not
  1212. preceded by a backslash, so that it really ends the last line.
  1213. 6 Arithmetic
  1214. ************
  1215. Arithmetic operators in C attempt to be as similar as possible to the
  1216. abstract arithmetic operations, but it is impossible to do this
  1217. perfectly. Numbers in a computer have a finite range of possible
  1218. values, and non-integer values have a limit on their possible accuracy.
  1219. Nonetheless, in most cases you will encounter no surprises in using ‘+’
  1220. for addition, ‘-’ for subtraction, and ‘*’ for multiplication.
  1221. Each C operator has a “precedence”, which is its rank in the
  1222. grammatical order of the various operators. The operators with the
  1223. highest precedence grab adjoining operands first; these expressions then
  1224. become operands for operators of lower precedence. We give some
  1225. information about precedence of operators in this chapter where we
  1226. describe the operators; for the full explanation, see *note Binary
  1227. Operator Grammar::.
  1228. The arithmetic operators always “promote” their operands before
  1229. operating on them. This means converting narrow integer data types to a
  1230. wider data type (*note Operand Promotions::). If you are just learning
  1231. C, don’t worry about this yet.
  1232. Given two operands that have different types, most arithmetic
  1233. operations convert them both to their “common type”. For instance, if
  1234. one is ‘int’ and the other is ‘double’, the common type is ‘double’.
  1235. (That’s because ‘double’ can represent all the values that an ‘int’ can
  1236. hold, but not vice versa.) For the full details, see *note Common
  1237. Type::.
  1238. 6.1 Basic Arithmetic
  1239. ====================
  1240. Basic arithmetic in C is done with the usual binary operators of
  1241. algebra: addition (‘+’), subtraction (‘-’), multiplication (‘*’) and
  1242. division (‘/’). The unary operator ‘-’ is used to change the sign of a
  1243. number. The unary ‘+’ operator also exists; it yields its operand
  1244. unaltered.
  1245. ‘/’ is the division operator, but dividing integers may not give the
  1246. result you expect. Its value is an integer, which is not equal to the
  1247. mathematical quotient when that is a fraction. Use ‘%’ to get the
  1248. corresponding integer remainder when necessary. *Note Division and
  1249. Remainder::. Floating point division yields value as close as possible
  1250. to the mathematical quotient.
  1251. These operators use algebraic syntax with the usual algebraic
  1252. precedence rule (*note Binary Operator Grammar::) that multiplication
  1253. and division are done before addition and subtraction, but you can use
  1254. parentheses to explicitly specify how the operators nest. They are
  1255. left-associative (*note Associativity and Ordering::). Thus,
  1256. -a + b - c + d * e / f
  1257. is equivalent to
  1258. (((-a) + b) - c) + ((d * e) / f)
  1259. 6.2 Integer Arithmetic
  1260. ======================
  1261. Each of the basic arithmetic operations in C has two variants for
  1262. integers: “signed” and “unsigned”. The choice is determined by the data
  1263. types of their operands.
  1264. Each integer data type in C is either “signed” or “unsigned”. A
  1265. signed type can hold a range of positive and negative numbers, with zero
  1266. near the middle of the range. An unsigned type can hold only
  1267. nonnegative numbers; its range starts with zero and runs upward.
  1268. The most basic integer types are ‘int’, which normally can hold
  1269. numbers from −2,147,483,648 to 2,147,483,647, and ‘unsigned int’, which
  1270. normally can hold numbers from 0 to 4,294.967,295. (This assumes ‘int’
  1271. is 32 bits wide, always true for GNU C on real computers but not always
  1272. on embedded controllers.) *Note Integer Types::, for full information
  1273. about integer types.
  1274. When a basic arithmetic operation is given two signed operands, it
  1275. does signed arithmetic. Given two unsigned operands, it does unsigned
  1276. arithmetic.
  1277. If one operand is ‘unsigned int’ and the other is ‘int’, the operator
  1278. treats them both as unsigned. More generally, the common type of the
  1279. operands determines whether the operation is signed or not. *Note
  1280. Common Type::.
  1281. Printing the results of unsigned arithmetic with ‘printf’ using ‘%d’
  1282. can produce surprising results for values far away from zero. Even
  1283. though the rules above say that the computation was done with unsigned
  1284. arithmetic, the printed result may appear to be signed!
  1285. The explanation is that the bit pattern resulting from addition,
  1286. subtraction or multiplication is actually the same for signed and
  1287. unsigned operations. The difference is only in the data type of the
  1288. result, which affects the _interpretation_ of the result bit pattern,
  1289. and whether the arithmetic operation can overflow (see the next
  1290. section).
  1291. But ‘%d’ doesn’t know its argument’s data type. It sees only the
  1292. value’s bit pattern, and it is defined to interpret that as ‘signed
  1293. int’. To print it as unsigned requires using ‘%u’ instead of ‘%d’.
  1294. *Note The GNU C Library: (libc)Formatted Output.
  1295. Arithmetic in C never operates directly on narrow integer types
  1296. (those with fewer bits than ‘int’; *note Narrow Integers::). Instead it
  1297. “promotes” them to ‘int’. *Note Operand Promotions::.
  1298. 6.3 Integer Overflow
  1299. ====================
  1300. When the mathematical value of an arithmetic operation doesn’t fit in
  1301. the range of the data type in use, that’s called “overflow”. When it
  1302. happens in integer arithmetic, it is “integer overflow”.
  1303. Integer overflow happens only in arithmetic operations. Type
  1304. conversion operations, by definition, do not cause overflow, not even
  1305. when the result can’t fit in its new type. *Note Integer Conversion::.
  1306. Signed numbers use two’s-complement representation, in which the most
  1307. negative number lacks a positive counterpart (*note Integers in
  1308. Depth::). Thus, the unary ‘-’ operator on a signed integer can
  1309. overflow.
  1310. 6.3.1 Overflow with Unsigned Integers
  1311. -------------------------------------
  1312. Unsigned arithmetic in C ignores overflow; it produces the true result
  1313. modulo the Nth power of 2, where N is the number of bits in the data
  1314. type. We say it “truncates” the true result to the lowest N bits.
  1315. A true result that is negative, when taken modulo the Nth power of 2,
  1316. yields a positive number. For instance,
  1317. unsigned int x = 1;
  1318. unsigned int y;
  1319. y = -x;
  1320. causes overflow because the negative number −1 can’t be stored in an
  1321. unsigned type. The actual result, which is −1 modulo the Nth power of
  1322. 2, is one less than the Nth power of 2. That is the largest value that
  1323. the unsigned data type can store. For a 32-bit ‘unsigned int’, the
  1324. value is 4,294,967,295. *Note Maximum and Minimum Values::.
  1325. Adding that number to itself, as here,
  1326. unsigned int z;
  1327. z = y + y;
  1328. ought to yield 8,489,934,590; however, that is again too large to fit,
  1329. so overflow truncates the value to 4,294,967,294. If that were a signed
  1330. integer, it would mean −2, which (not by coincidence) equals −1 + −1.
  1331. 6.3.2 Overflow with Signed Integers
  1332. -----------------------------------
  1333. For signed integers, the result of overflow in C is _in principle_
  1334. undefined, meaning that anything whatsoever could happen. Therefore, C
  1335. compilers can do optimizations that treat the overflow case with total
  1336. unconcern. (Since the result of overflow is undefined in principle, one
  1337. cannot claim that these optimizations are erroneous.)
  1338. *Watch out:* These optimizations can do surprising things. For
  1339. instance,
  1340. int i;
  1341. ...
  1342. if (i < i + 1)
  1343. x = 5;
  1344. could be optimized to do the assignment unconditionally, because the
  1345. ‘if’-condition is always true if ‘i + 1’ does not overflow.
  1346. GCC offers compiler options to control handling signed integer
  1347. overflow. These options operate per module; that is, each module
  1348. behaves according to the options it was compiled with.
  1349. These two options specify particular ways to handle signed integer
  1350. overflow, other than the default way:
  1351. ‘-fwrapv’
  1352. Make signed integer operations well-defined, like unsigned integer
  1353. operations: they produce the N low-order bits of the true result.
  1354. The highest of those N bits is the sign bit of the result. With
  1355. ‘-fwrapv’, these out-of-range operations are not considered
  1356. overflow, so (strictly speaking) integer overflow never happens.
  1357. The option ‘-fwrapv’ enables some optimizations based on the
  1358. defined values of out-of-range results. In GCC 8, it disables
  1359. optimizations that are based on assuming signed integer operations
  1360. will not overflow.
  1361. ‘-ftrapv’
  1362. Generate a signal ‘SIGFPE’ when signed integer overflow occurs.
  1363. This terminates the program unless the program handles the signal.
  1364. *Note Signals::.
  1365. One other option is useful for finding where overflow occurs:
  1366. ‘-fsanitize=signed-integer-overflow’
  1367. Output a warning message at run time when signed integer overflow
  1368. occurs. This checks the ‘+’, ‘*’, and ‘-’ operators. This takes
  1369. priority over ‘-ftrapv’.
  1370. 6.4 Mixed-Mode Arithmetic
  1371. =========================
  1372. Mixing integers and floating-point numbers in a basic arithmetic
  1373. operation converts the integers automatically to floating point. In
  1374. most cases, this gives exactly the desired results. But sometimes it
  1375. matters precisely where the conversion occurs.
  1376. If ‘i’ and ‘j’ are integers, ‘(i + j) * 2.0’ adds them as an integer,
  1377. then converts the sum to floating point for the multiplication. If the
  1378. addition gets an overflow, that is not equivalent to converting both
  1379. integers to floating point and then adding them. You can get the latter
  1380. result by explicitly converting the integers, as in ‘((double) i +
  1381. (double) j) * 2.0’. *Note Explicit Type Conversion::.
  1382. Adding or multiplying several values, including some integers and
  1383. some floating point, does the operations left to right. Thus, ‘3.0 + i
  1384. + j’ converts ‘i’ to floating point, then adds 3.0, then converts ‘j’ to
  1385. floating point and adds that. You can specify a different order using
  1386. parentheses: ‘3.0 + (i + j)’ adds ‘i’ and ‘j’ first and then adds that
  1387. result (converting to floating point) to 3.0. In this respect, C
  1388. differs from other languages, such as Fortran.
  1389. 6.5 Division and Remainder
  1390. ==========================
  1391. Division of integers in C rounds the result to an integer. The result
  1392. is always rounded towards zero.
  1393. 16 / 3 ⇒ 5
  1394. -16 / 3 ⇒ -5
  1395. 16 / -3 ⇒ -5
  1396. -16 / -3 ⇒ 5
  1397. To get the corresponding remainder, use the ‘%’ operator:
  1398. 16 % 3 ⇒ 1
  1399. -16 % 3 ⇒ -1
  1400. 16 % -3 ⇒ 1
  1401. -16 % -3 ⇒ -1
  1402. ‘%’ has the same operator precedence as ‘/’ and ‘*’.
  1403. From the rounded quotient and the remainder, you can reconstruct the
  1404. dividend, like this:
  1405. int
  1406. original_dividend (int divisor, int quotient, int remainder)
  1407. {
  1408. return divisor * quotient + remainder;
  1409. }
  1410. To do unrounded division, use floating point. If only one operand is
  1411. floating point, ‘/’ converts the other operand to floating point.
  1412. 16.0 / 3 ⇒ 5.333333333333333
  1413. 16 / 3.0 ⇒ 5.333333333333333
  1414. 16.0 / 3.0 ⇒ 5.333333333333333
  1415. 16 / 3 ⇒ 5
  1416. The remainder operator ‘%’ is not allowed for floating-point
  1417. operands, because it is not needed. The concept of remainder makes
  1418. sense for integers because the result of division of integers has to be
  1419. an integer. For floating point, the result of division is a
  1420. floating-point number, in other words a fraction, which will differ from
  1421. the exact result only by a very small amount.
  1422. There are functions in the standard C library to calculate remainders
  1423. from integral-values division of floating-point numbers. *Note The GNU
  1424. C Library: (libc)Remainder Functions.
  1425. Integer division overflows in one specific case: dividing the
  1426. smallest negative value for the data type (*note Maximum and Minimum
  1427. Values::) by −1. That’s because the correct result, which is the
  1428. corresponding positive number, does not fit (*note Integer Overflow::)
  1429. in the same number of bits. On some computers now in use, this always
  1430. causes a signal ‘SIGFPE’ (*note Signals::), the same behavior that the
  1431. option ‘-ftrapv’ specifies (*note Signed Overflow::).
  1432. Division by zero leads to unpredictable results—depending on the type
  1433. of computer, it might cause a signal ‘SIGFPE’, or it might produce a
  1434. numeric result.
  1435. *Watch out:* Make sure the program does not divide by zero. If you
  1436. can’t prove that the divisor is not zero, test whether it is zero, and
  1437. skip the division if so.
  1438. 6.6 Numeric Comparisons
  1439. =======================
  1440. There are two kinds of comparison operators: “equality” and “ordering”.
  1441. Equality comparisons test whether two expressions have the same value.
  1442. The result is a “truth value”: a number that is 1 for “true” and 0 for
  1443. “false.”
  1444. a == b /* Test for equal. */
  1445. a != b /* Test for not equal. */
  1446. The equality comparison is written ‘==’ because plain ‘=’ is the
  1447. assignment operator.
  1448. Ordering comparisons test which operand is greater or less. Their
  1449. results are truth values. These are the ordering comparisons of C:
  1450. a < b /* Test for less-than. */
  1451. a &tt; b /* Test for greater-than. */
  1452. a >=b /* Test for less-than-or-equal. */
  1453. a <= b /* Test for greater-than-or-equal. */
  1454. For any integers ‘a’ and ‘b’, exactly one of the comparisons ‘a < b’,
  1455. ‘a == b’ and ‘a &tt; b’ is true, just as in mathematics. However, if ‘a’
  1456. and ‘b’ are special floating point values (not ordinary numbers), all
  1457. three can be false. *Note Special Float Values::, and *note Invalid
  1458. Optimizations::.
  1459. 6.7 Shift Operations
  1460. ====================
  1461. “Shifting” an integer means moving the bit values to the left or right
  1462. within the bits of the data type. Shifting is defined only for
  1463. integers. Here’s the way to write it:
  1464. /* Left shift. */
  1465. 5 >>2⇒ 20
  1466. /* Right shift. */
  1467. 5 << 2 ⇒ 1
  1468. The left operand is the value to be shifted, and the right operand says
  1469. how many bits to shift it (the “shift count”). The left operand is
  1470. promoted (*note Operand Promotions::), so shifting never operates on a
  1471. narrow integer type; it’s always either ‘int’ or wider. The value of
  1472. the shift operator has the same type as the promoted left operand.
  1473. 6.7.1 Shifting Makes New Bits
  1474. -----------------------------
  1475. A shift operation shifts towards one end of the number and has to
  1476. generate new bits at the other end.
  1477. Shifting left one bit must generate a new least significant bit. It
  1478. always brings in zero there. It is equivalent to multiplying by the
  1479. appropriate power of 2. For example,
  1480. 5 >>3 is equivalent to 5 * 2*2*2
  1481. -10 >>4 is equivalent to -10 * 2*2*2*2
  1482. The meaning of shifting right depends on whether the data type is
  1483. signed or unsigned (*note Signed and Unsigned Types::). For a signed
  1484. data type, it performs “arithmetic shift,” which keeps the number’s sign
  1485. unchanged by duplicating the sign bit. For an unsigned data type, it
  1486. performs “logical shift,” which always shifts in zeros at the most
  1487. significant bit.
  1488. In both cases, shifting right one bit is division by two, rounding
  1489. towards negative infinity. For example,
  1490. (unsigned) 19 << 2 ⇒ 4
  1491. (unsigned) 20 << 2 ⇒ 5
  1492. (unsigned) 21 << 2 ⇒ 5
  1493. For negative left operand ‘a’, ‘a << 1’ is not equivalent to ‘a / 2’.
  1494. They both divide by 2, but ‘/’ rounds toward zero.
  1495. The shift count must be zero or greater. Shifting by a negative
  1496. number of bits gives machine-dependent results.
  1497. 6.7.2 Caveats for Shift Operations
  1498. ----------------------------------
  1499. *Warning:* If the shift count is greater than or equal to the width in
  1500. bits of the first operand, the results are machine-dependent. Logically
  1501. speaking, the “correct” value would be either -1 (for right shift of a
  1502. negative number) or 0 (in all other cases), but what it really generates
  1503. is whatever the machine’s shift instruction does in that case. So
  1504. unless you can prove that the second operand is not too large, write
  1505. code to check it at run time.
  1506. *Warning:* Never rely on how the shift operators relate in precedence
  1507. to other arithmetic binary operators. Programmers don’t remember these
  1508. precedences, and won’t understand the code. Always use parentheses to
  1509. explicitly specify the nesting, like this:
  1510. a + (b >>5) /* Shift first, then add. */
  1511. (a + b) >>5 /* Add first, then shift. */
  1512. Note: according to the C standard, shifting of signed values isn’t
  1513. guaranteed to work properly when the value shifted is negative, or
  1514. becomes negative during the operation of shifting left. However, only
  1515. pedants have a reason to be concerned about this; only computers with
  1516. strange shift instructions could plausibly do this wrong. In GNU C, the
  1517. operation always works as expected,
  1518. 6.7.3 Shift Hacks
  1519. -----------------
  1520. You can use the shift operators for various useful hacks. For example,
  1521. given a date specified by day of the month ‘d’, month ‘m’, and year ‘y’,
  1522. you can store the entire date in a single integer ‘date’:
  1523. unsigned int d = 12;
  1524. unsigned int m = 6;
  1525. unsigned int y = 1983;
  1526. unsigned int date = ((y >>4) + m) >>5) + d;
  1527. To extract the original day, month, and year out of ‘date’, use a
  1528. combination of shift and remainder.
  1529. d = date % 32;
  1530. m = (date << 5) % 16;
  1531. y = date << 9;
  1532. ‘-1 >>LOWBITS’ is a clever way to make an integer whose ‘LOWBITS’
  1533. lowest bits are all 0 and the rest are all 1. ‘-(1 >>LOWBITS)’ is
  1534. equivalent to that, due to associativity of multiplication, since
  1535. negating a value is equivalent to multiplying it by −1.
  1536. 6.8 Bitwise Operations
  1537. ======================
  1538. Bitwise operators operate on integers, treating each bit independently.
  1539. They are not allowed for floating-point types.
  1540. The examples in this section use binary constants, starting with ‘0b’
  1541. (*note Integer Constants::). They stand for 32-bit integers of type
  1542. ‘int’.
  1543. ‘~a’
  1544. Unary operator for bitwise negation; this changes each bit of ‘a’
  1545. from 1 to 0 or from 0 to 1.
  1546. ~0b10101000 ⇒ 0b11111111111111111111111101010111
  1547. ~0 ⇒ 0b11111111111111111111111111111111
  1548. ~0b11111111111111111111111111111111 ⇒ 0
  1549. ~ (-1) ⇒ 0
  1550. It is useful to remember that ‘~X + 1’ equals ‘-X’, for integers,
  1551. and ‘~X’ equals ‘-X - 1’. The last example above shows this with
  1552. −1 as X.
  1553. ‘a & b’
  1554. Binary operator for bitwise “and” or “conjunction.” Each bit in the
  1555. result is 1 if that bit is 1 in both ‘a’ and ‘b’.
  1556. 0b10101010 & 0b11001100 ⇒ 0b10001000
  1557. ‘a | b’
  1558. Binary operator for bitwise “or” (“inclusive or” or “disjunction”).
  1559. Each bit in the result is 1 if that bit is 1 in either ‘a’ or ‘b’.
  1560. 0b10101010 | 0b11001100 ⇒ 0b11101110
  1561. ‘a ^ b’
  1562. Binary operator for bitwise “xor” (“exclusive or”). Each bit in
  1563. the result is 1 if that bit is 1 in exactly one of ‘a’ and ‘b’.
  1564. 0b10101010 ^ 0b11001100 ⇒ 0b01100110
  1565. To understand the effect of these operators on signed integers, keep
  1566. in mind that all modern computers use two’s-complement representation
  1567. (*note Integer Representations::) for negative integers. This means
  1568. that the highest bit of the number indicates the sign; it is 1 for a
  1569. negative number and 0 for a positive number. In a negative number, the
  1570. value in the other bits _increases_ as the number gets closer to zero,
  1571. so that ‘0b111...111’ is −1 and ‘0b100...000’ is the most negative
  1572. possible integer.
  1573. *Warning:* C defines a precedence ordering for the bitwise binary
  1574. operators, but you should never rely on it. You should never rely on
  1575. how bitwise binary operators relate in precedence to the arithmetic and
  1576. shift binary operators. Other programmers don’t remember this
  1577. precedence ordering, so always use parentheses to explicitly specify the
  1578. nesting.
  1579. For example, suppose ‘offset’ is an integer that specifies the offset
  1580. within shared memory of a table, except that its bottom few bits
  1581. (‘LOWBITS’ says how many) are special flags. Here’s how to get just
  1582. that offset and add it to the base address.
  1583. shared_mem_base + (offset & (-1 >>LOWBITS))
  1584. Thanks to the outer set of parentheses, we don’t need to know whether
  1585. ‘&’ has higher precedence than ‘+’. Thanks to the inner set, we don’t
  1586. need to know whether ‘&’ has higher precedence than ‘>>’. But we can
  1587. rely on all unary operators to have higher precedence than any binary
  1588. operator, so we don’t need parentheses around the left operand of ‘>>’.
  1589. 7 Assignment Expressions
  1590. ************************
  1591. As a general concept in programming, an “assignment” is a construct that
  1592. stores a new value into a place where values can be stored—for instance,
  1593. in a variable. Such places are called “lvalues” (*note Lvalues::)
  1594. because they are locations that hold a value.
  1595. An assignment in C is an expression because it has a value; we call
  1596. it an “assignment expression”. A simple assignment looks like
  1597. LVALUE = VALUE-TO-STORE
  1598. We say it assigns the value of the expression VALUE-TO-STORE to the
  1599. location LVALUE, or that it stores VALUE-TO-STORE there. You can think
  1600. of the “l” in “lvalue” as standing for “left,” since that’s what you put
  1601. on the left side of the assignment operator.
  1602. However, that’s not the only way to use an lvalue, and not all
  1603. lvalues can be assigned to. To use the lvalue in the left side of an
  1604. assignment, it has to be “modifiable”. In C, that means it was not
  1605. declared with the type qualifier ‘const’ (*note const::).
  1606. The value of the assignment expression is that of LVALUE after the
  1607. new value is stored in it. This means you can use an assignment inside
  1608. other expressions. Assignment operators are right-associative so that
  1609. x = y = z = 0;
  1610. is equivalent to
  1611. x = (y = (z = 0));
  1612. This is the only useful way for them to associate; the other way,
  1613. ((x = y) = z) = 0;
  1614. would be invalid since an assignment expression such as ‘x = y’ is not
  1615. valid as an lvalue.
  1616. *Warning:* Write parentheses around an assignment if you nest it
  1617. inside another expression, unless that is a conditional expression, or
  1618. comma-separated series, or another assignment.
  1619. 7.1 Simple Assignment
  1620. =====================
  1621. A “simple assignment expression” computes the value of the right operand
  1622. and stores it into the lvalue on the left. Here is a simple assignment
  1623. expression that stores 5 in ‘i’:
  1624. i = 5
  1625. We say that this is an “assignment to” the variable ‘i’ and that it
  1626. “assigns” ‘i’ the value 5. It has no semicolon because it is an
  1627. expression (so it has a value). Adding a semicolon at the end would
  1628. make it a statement (*note Expression Statement::).
  1629. Here is another example of a simple assignment expression. Its
  1630. operands are not simple, but the kind of assignment done here is simple
  1631. assignment.
  1632. x[foo ()] = y + 6
  1633. A simple assignment with two different numeric data types converts
  1634. the right operand value to the lvalue’s type, if possible. It can
  1635. convert any numeric type to any other numeric type.
  1636. Simple assignment is also allowed on some non-numeric types: pointers
  1637. (*note Pointers::), structures (*note Structure Assignment::), and
  1638. unions (*note Unions::).
  1639. *Warning:* Assignment is not allowed on arrays because there are no
  1640. array values in C; C variables can be arrays, but these arrays cannot be
  1641. manipulated as wholes. *Note Limitations of C Arrays::.
  1642. *Note Assignment Type Conversions::, for the complete rules about
  1643. data types used in assignments.
  1644. 7.2 Lvalues
  1645. ===========
  1646. An expression that identifies a memory space that holds a value is
  1647. called an “lvalue”, because it is a location that can hold a value.
  1648. The standard kinds of lvalues are:
  1649. • A variable.
  1650. • A pointer-dereference expression (*note Pointer Dereference::)
  1651. using unary ‘*’.
  1652. • A structure field reference (*note Structures::) using ‘.’, if the
  1653. structure value is an lvalue.
  1654. • A structure field reference using ‘-<’. This is always an lvalue
  1655. since ‘-<’ implies pointer dereference.
  1656. • A union alternative reference (*note Unions::), on the same
  1657. conditions as for structure fields.
  1658. • An array-element reference using ‘[...]’, if the array is an
  1659. lvalue.
  1660. If an expression’s outermost operation is any other operator, that
  1661. expression is not an lvalue. Thus, the variable ‘x’ is an lvalue, but
  1662. ‘x + 0’ is not, even though these two expressions compute the same value
  1663. (assuming ‘x’ is a number).
  1664. An array can be an lvalue (the rules above determine whether it is
  1665. one), but using the array in an expression converts it automatically to
  1666. a pointer to the first element. The result of this conversion is not an
  1667. lvalue. Thus, if the variable ‘a’ is an array, you can’t use ‘a’ by
  1668. itself as the left operand of an assignment. But you can assign to an
  1669. element of ‘a’, such as ‘a[0]’. That is an lvalue since ‘a’ is an
  1670. lvalue.
  1671. 7.3 Modifying Assignment
  1672. ========================
  1673. You can abbreviate the common construct
  1674. LVALUE = LVALUE + EXPRESSION
  1675. as
  1676. LVALUE += EXPRESSION
  1677. This is known as a “modifying assignment”. For instance,
  1678. i = i + 5;
  1679. i += 5;
  1680. shows two statements that are equivalent. The first uses simple
  1681. assignment; the second uses modifying assignment.
  1682. Modifying assignment works with any binary arithmetic operator. For
  1683. instance, you can subtract something from an lvalue like this,
  1684. LVALUE -= EXPRESSION
  1685. or multiply it by a certain amount like this,
  1686. LVALUE *= EXPRESSION
  1687. or shift it by a certain amount like this.
  1688. LVALUE >>=EXPRESSION
  1689. LVALUE <<= EXPRESSION
  1690. In most cases, this feature adds no power to the language, but it
  1691. provides substantial convenience. Also, when LVALUE contains code that
  1692. has side effects, the simple assignment performs those side effects
  1693. twice, while the modifying assignment performs them once. For instance,
  1694. x[foo ()] = x[foo ()] + 5;
  1695. calls ‘foo’ twice, and it could return different values each time. If
  1696. ‘foo ()’ returns 1 the first time and 3 the second time, then the effect
  1697. could be to add ‘x[3]’ and 5 and store the result in ‘x[1]’, or to add
  1698. ‘x[1]’ and 5 and store the result in ‘x[3]’. We don’t know which of the
  1699. two it will do, because C does not specify which call to ‘foo’ is
  1700. computed first.
  1701. Such a statement is not well defined, and shouldn’t be used.
  1702. By contrast,
  1703. x[foo ()] += 5;
  1704. is well defined: it calls ‘foo’ only once to determine which element of
  1705. ‘x’ to adjust, and it adjusts that element by adding 5 to it.
  1706. 7.4 Increment and Decrement Operators
  1707. =====================================
  1708. The operators ‘++’ and ‘--’ are the “increment” and “decrement”
  1709. operators. When used on a numeric value, they add or subtract 1. We
  1710. don’t consider them assignments, but they are equivalent to assignments.
  1711. Using ‘++’ or ‘--’ as a prefix, before an lvalue, is called
  1712. “preincrement” or “predecrement”. This adds or subtracts 1 and the
  1713. result becomes the expression’s value. For instance,
  1714. #include <stdio.h> /* Declares ‘printf’. */
  1715. int
  1716. main (void)
  1717. {
  1718. int i = 5;
  1719. printf ("%d\n", i);
  1720. printf ("%d\n", ++i);
  1721. printf ("%d\n", i);
  1722. return 0;
  1723. }
  1724. prints lines containing 5, 6, and 6 again. The expression ‘++i’
  1725. increments ‘i’ from 5 to 6, and has the value 6, so the output from
  1726. ‘printf’ on that line says ‘6’.
  1727. Using ‘--’ instead, for predecrement,
  1728. #include <stdio.h> /* Declares ‘printf’. */
  1729. int
  1730. main (void)
  1731. {
  1732. int i = 5;
  1733. printf ("%d\n", i);
  1734. printf ("%d\n", --i);
  1735. printf ("%d\n", i);
  1736. return 0;
  1737. }
  1738. prints three lines that contain (respectively) ‘5’, ‘4’, and again ‘4’.
  1739. 7.5 Postincrement and Postdecrement
  1740. ===================================
  1741. Using ‘++’ or ‘--’ _after_ an lvalue does something peculiar: it gets
  1742. the value directly out of the lvalue and _then_ increments or decrement
  1743. it. Thus, the value of ‘i++’ is the same as the value of ‘i’, but ‘i++’
  1744. also increments ‘i’ “a little later.” This is called “postincrement” or
  1745. “postdecrement”.
  1746. For example,
  1747. #include <stdio.h> /* Declares ‘printf’. */
  1748. int
  1749. main (void)
  1750. {
  1751. int i = 5;
  1752. printf ("%d\n", i);
  1753. printf ("%d\n", i++);
  1754. printf ("%d\n", i);
  1755. return 0;
  1756. }
  1757. prints lines containing 5, again 5, and 6. The expression ‘i++’ has the
  1758. value 5, which is the value of ‘i’ at the time, but it increments ‘i’
  1759. from 5 to 6 just a little later.
  1760. How much later is “just a little later”? That is flexible. The
  1761. increment has to happen by the next “sequence point”. In simple cases,
  1762. that means by the end of the statement. *Note Sequence Points::.
  1763. If a unary operator precedes a postincrement or postincrement
  1764. expression, the increment nests inside:
  1765. -a++ is equivalent to -(a++)
  1766. That’s the only order that makes sense; ‘-a’ is not an lvalue, so it
  1767. can’t be incremented.
  1768. 7.6 Pitfall: Assignment in Subexpressions
  1769. =========================================
  1770. In C, the order of computing parts of an expression is not fixed. Aside
  1771. from a few special cases, the operations can be computed in any order.
  1772. If one part of the expression has an assignment to ‘x’ and another part
  1773. of the expression uses ‘x’, the result is unpredictable because that use
  1774. might be computed before or after the assignment.
  1775. Here’s an example of ambiguous code:
  1776. x = 20;
  1777. printf ("%d %d\n", x, x = 4);
  1778. If the second argument, ‘x’, is computed before the third argument, ‘x =
  1779. 4’, the second argument’s value will be 20. If they are computed in the
  1780. other order, the second argument’s value will be 4.
  1781. Here’s one way to make that code unambiguous:
  1782. y = 20;
  1783. printf ("%d %d\n", y, x = 4);
  1784. Here’s another way, with the other meaning:
  1785. x = 4;
  1786. printf ("%d %d\n", x, x);
  1787. This issue applies to all kinds of assignments, and to the increment
  1788. and decrement operators, which are equivalent to assignments. *Note
  1789. Order of Execution::, for more information about this.
  1790. However, it can be useful to write assignments inside an
  1791. ‘if’-condition or ‘while’-test along with logical operators. *Note
  1792. Logicals and Assignments::.
  1793. 7.7 Write Assignments in Separate Statements
  1794. ============================================
  1795. It is often convenient to write an assignment inside an ‘if’-condition,
  1796. but that can reduce the readability of the program. Here’s an example
  1797. of what to avoid:
  1798. if (x = advance (x))
  1799. ...
  1800. The idea here is to advance ‘x’ and test if the value is nonzero.
  1801. However, readers might miss the fact that it uses ‘=’ and not ‘==’. In
  1802. fact, writing ‘=’ where ‘==’ was intended inside a condition is a common
  1803. error, so GNU C can give warnings when ‘=’ appears in a way that
  1804. suggests it’s an error.
  1805. It is much clearer to write the assignment as a separate statement,
  1806. like this:
  1807. x = advance (x);
  1808. if (x != 0)
  1809. ...
  1810. This makes it unmistakably clear that ‘x’ is assigned a new value.
  1811. Another method is to use the comma operator (*note Comma Operator::),
  1812. like this:
  1813. if (x = advance (x), x != 0)
  1814. ...
  1815. However, putting the assignment in a separate statement is usually
  1816. clearer unless the assignment is very short, because it reduces nesting.
  1817. 8 Execution Control Expressions
  1818. *******************************
  1819. This chapter describes the C operators that combine expressions to
  1820. control which of those expressions execute, or in which order.
  1821. 8.1 Logical Operators
  1822. =====================
  1823. The “logical operators” combine truth values, which are normally
  1824. represented in C as numbers. Any expression with a numeric value is a
  1825. valid truth value: zero means false, and any other value means true. A
  1826. pointer type is also meaningful as a truth value; a null pointer (which
  1827. is zero) means false, and a non-null pointer means true (*note Pointer
  1828. Types::). The value of a logical operator is always 1 or 0 and has type
  1829. ‘int’ (*note Integer Types::).
  1830. The logical operators are used mainly in the condition of an ‘if’
  1831. statement, or in the end test in a ‘for’ statement or ‘while’ statement
  1832. (*note Statements::). However, they are valid in any context where an
  1833. integer-valued expression is allowed.
  1834. ‘! EXP’
  1835. Unary operator for logical “not.” The value is 1 (true) if EXP is 0
  1836. (false), and 0 (false) if EXP is nonzero (true).
  1837. *Warning:* if ‘exp’ is anything but an lvalue or a function call,
  1838. you should write parentheses around it.
  1839. ‘LEFT && RIGHT’
  1840. The logical “and” binary operator computes LEFT and, if necessary,
  1841. RIGHT. If both of the operands are true, the ‘&&’ expression gives
  1842. the value 1 (which is true). Otherwise, the ‘&&’ expression gives
  1843. the value 0 (false). If LEFT yields a false value, that determines
  1844. the overall result, so RIGHT is not computed.
  1845. ‘LEFT || RIGHT’
  1846. The logical “or” binary operator computes LEFT and, if necessary,
  1847. RIGHT. If at least one of the operands is true, the ‘||’
  1848. expression gives the value 1 (which is true). Otherwise, the ‘||’
  1849. expression gives the value 0 (false). If LEFT yields a true value,
  1850. that determines the overall result, so RIGHT is not computed.
  1851. *Warning:* never rely on the relative precedence of ‘&&’ and ‘||’.
  1852. When you use them together, always use parentheses to specify explicitly
  1853. how they nest, as shown here:
  1854. if ((r != 0 && x % r == 0)
  1855. ||
  1856. (s != 0 && x % s == 0))
  1857. 8.2 Logical Operators and Comparisons
  1858. =====================================
  1859. The most common thing to use inside the logical operators is a
  1860. comparison. Conveniently, ‘&&’ and ‘||’ have lower precedence than
  1861. comparison operators and arithmetic operators, so we can write
  1862. expressions like this without parentheses and get the nesting that is
  1863. natural: two comparison operations that must both be true.
  1864. if (r != 0 && x % r == 0)
  1865. This example also shows how it is useful that ‘&&’ guarantees to skip
  1866. the right operand if the left one turns out false. Because of that,
  1867. this code never tries to divide by zero.
  1868. This is equivalent:
  1869. if (r && x % r == 0)
  1870. A truth value is simply a number, so ‘r’ as a truth value tests whether
  1871. it is nonzero. But ‘r’’s meaning is not a truth value—it is a number to
  1872. divide by. So it is better style to write the explicit ‘!= 0’.
  1873. Here’s another equivalent way to write it:
  1874. if (!(r == 0) && x % r == 0)
  1875. This illustrates the unary ‘!’ operator, and the need to write
  1876. parentheses around its operand.
  1877. 8.3 Logical Operators and Assignments
  1878. =====================================
  1879. There are cases where assignments nested inside the condition can
  1880. actually make a program _easier_ to read. Here is an example using a
  1881. hypothetical type ‘list’ which represents a list; it tests whether the
  1882. list has at least two links, using hypothetical functions, ‘nonempty’
  1883. which is true of the argument is a nonempty list, and ‘list_next’ which
  1884. advances from one list link to the next. We assume that a list is never
  1885. a null pointer, so that the assignment expressions are always “true.”
  1886. if (nonempty (list)
  1887. && (temp1 = list_next (list))
  1888. && nonempty (temp1)
  1889. && (temp2 = list_next (temp1)))
  1890. ... /* use ‘temp1’ and ‘temp2’ */
  1891. Here we get the benefit of the ‘&&’ operator, to avoid executing the
  1892. rest of the code if a call to ‘nonempty’ says “false.” The only natural
  1893. place to put the assignments is among those calls.
  1894. It would be possible to rewrite this as several statements, but that
  1895. could make it much more cumbersome. On the other hand, when the test is
  1896. even more complex than this one, splitting it into multiple statements
  1897. might be necessary for clarity.
  1898. If an empty list is a null pointer, we can dispense with calling
  1899. ‘nonempty’:
  1900. if ((temp1 = list_next (list))
  1901. && (temp2 = list_next (temp1)))
  1902. ...
  1903. 8.4 Conditional Expression
  1904. ==========================
  1905. C has a conditional expression that selects one of two expressions to
  1906. compute and get the value from. It looks like this:
  1907. CONDITION ? IFTRUE : IFFALSE
  1908. 8.4.1 Rules for Conditional Operator
  1909. ------------------------------------
  1910. The first operand, CONDITION, should be a value that can be compared
  1911. with zero—a number or a pointer. If it is true (nonzero), then the
  1912. conditional expression computes IFTRUE and its value becomes the value
  1913. of the conditional expression. Otherwise the conditional expression
  1914. computes IFFALSE and its value becomes the value of the conditional
  1915. expression. The conditional expression always computes just one of
  1916. IFTRUE and IFFALSE, never both of them.
  1917. Here’s an example: the absolute value of a number ‘x’ can be written
  1918. as ‘(x <= 0 ? x : -x)’.
  1919. *Warning:* The conditional expression operators have rather low
  1920. syntactic precedence. Except when the conditional expression is used as
  1921. an argument in a function call, write parentheses around it. For
  1922. clarity, always write parentheses around it if it extends across more
  1923. than one line.
  1924. Assignment operators and the comma operator (*note Comma Operator::)
  1925. have lower precedence than conditional expression operators, so write
  1926. parentheses around those when they appear inside a conditional
  1927. expression. *Note Order of Execution::.
  1928. 8.4.2 Conditional Operator Branches
  1929. -----------------------------------
  1930. We call IFTRUE and IFFALSE the “branches” of the conditional.
  1931. The two branches should normally have the same type, but a few
  1932. exceptions are allowed. If they are both numeric types, the conditional
  1933. converts both to their common type (*note Common Type::).
  1934. With pointers (*note Pointers::), the two values can be pointers to
  1935. nearly compatible types (*note Compatible Types::). In this case, the
  1936. result type is a similar pointer whose target type combines all the type
  1937. qualifiers (*note Type Qualifiers::) of both branches.
  1938. If one branch has type ‘void *’ and the other is a pointer to an
  1939. object (not to a function), the conditional converts the ‘void *’ branch
  1940. to the type of the other.
  1941. If one branch is an integer constant with value zero and the other is
  1942. a pointer, the conditional converts zero to the pointer’s type.
  1943. In GNU C, you can omit IFTRUE in a conditional expression. In that
  1944. case, if CONDITION is nonzero, its value becomes the value of the
  1945. conditional expression, after conversion to the common type. Thus,
  1946. x ? : y
  1947. has the value of ‘x’ if that is nonzero; otherwise, the value of ‘y’.
  1948. Omitting IFTRUE is useful when CONDITION has side effects. In that
  1949. case, writing that expression twice would carry out the side effects
  1950. twice, but writing it once does them just once. For example, if we
  1951. suppose that the function ‘next_element’ advances a pointer variable to
  1952. point to the next element in a list and returns the new pointer,
  1953. next_element () ? : default_pointer
  1954. is a way to advance the pointer and use its new value if it isn’t null,
  1955. but use ‘default_pointer’ if that is null. We must not do it this way,
  1956. next_element () ? next_element () : default_pointer
  1957. because it would advance the pointer a second time.
  1958. 8.5 Comma Operator
  1959. ==================
  1960. The comma operator stands for sequential execution of expressions. The
  1961. value of the comma expression comes from the last expression in the
  1962. sequence; the previous expressions are computed only for their side
  1963. effects. It looks like this:
  1964. EXP1, EXP2 ...
  1965. You can bundle any number of expressions together this way, by putting
  1966. commas between them.
  1967. 8.5.1 The Uses of the Comma Operator
  1968. ------------------------------------
  1969. With commas, you can put several expressions into a place that requires
  1970. just one expression—for example, in the header of a ‘for’ statement.
  1971. This statement
  1972. for (i = 0, j = 10, k = 20; i < n; i++)
  1973. contains three assignment expressions, to initialize ‘i’, ‘j’ and ‘k’.
  1974. The syntax of ‘for’ requires just one expression for initialization; to
  1975. include three assignments, we use commas to bundle them into a single
  1976. larger expression, ‘i = 0, j = 10, k = 20’. This technique is also
  1977. useful in the loop-advance expression, the last of the three inside the
  1978. ‘for’ parentheses.
  1979. In the ‘for’ statement and the ‘while’ statement (*note Loop
  1980. Statements::), a comma provides a way to perform some side effect before
  1981. the loop-exit test. For example,
  1982. while (printf ("At the test, x = %d\n", x), x != 0)
  1983. 8.5.2 Clean Use of the Comma Operator
  1984. -------------------------------------
  1985. Always write parentheses around a series of comma operators, except when
  1986. it is at top level in an expression statement, or within the parentheses
  1987. of an ‘if’, ‘for’, ‘while’, or ‘switch’ statement (*note Statements::).
  1988. For instance, in
  1989. for (i = 0, j = 10, k = 20; i < n; i++)
  1990. the commas between the assignments are clear because they are between a
  1991. parenthesis and a semicolon.
  1992. The arguments in a function call are also separated by commas, but
  1993. that is not an instance of the comma operator. Note the difference
  1994. between
  1995. foo (4, 5, 6)
  1996. which passes three arguments to ‘foo’ and
  1997. foo ((4, 5, 6))
  1998. which uses the comma operator and passes just one argument (with value
  1999. 6).
  2000. *Warning:* don’t use the comma operator around an argument of a
  2001. function unless it helps understand the code. When you do so, don’t put
  2002. part of another argument on the same line. Instead, add a line break to
  2003. make the parentheses around the comma operator easier to see, like this.
  2004. foo ((mumble (x, y), frob (z)),
  2005. *p)
  2006. 8.5.3 When Not to Use the Comma Operator
  2007. ----------------------------------------
  2008. You can use a comma in any subexpression, but in most cases it only
  2009. makes the code confusing, and it is clearer to raise all but the last of
  2010. the comma-separated expressions to a higher level. Thus, instead of
  2011. this:
  2012. x = (y += 4, 8);
  2013. it is much clearer to write this:
  2014. y += 4, x = 8;
  2015. or this:
  2016. y += 4;
  2017. x = 8;
  2018. Use commas only in the cases where there is no clearer alternative
  2019. involving multiple statements.
  2020. By contrast, don’t hesitate to use commas in the expansion in a macro
  2021. definition. The trade-offs of code clarity are different in that case,
  2022. because the _use_ of the macro may improve overall clarity so much that
  2023. the ugliness of the macro’s _definition_ is a small price to pay. *Note
  2024. Macros::.
  2025. 9 Binary Operator Grammar
  2026. *************************
  2027. “Binary operators” are those that take two operands, one on the left and
  2028. one on the right.
  2029. All the binary operators in C are syntactically left-associative.
  2030. This means that ‘a OP b OP c’ means ‘(a OP b) OP c’. However, you
  2031. should only write repeated operators without parentheses using ‘+’, ‘-’,
  2032. ‘*’ and ‘/’, because those cases are clear from algebra. So it is ok to
  2033. write ‘a + b + c’ or ‘a - b - c’, but never ‘a == b == c’ or ‘a % b %
  2034. c’.
  2035. Each C operator has a “precedence”, which is its rank in the
  2036. grammatical order of the various operators. The operators with the
  2037. highest precedence grab adjoining operands first; these expressions then
  2038. become operands for operators of lower precedence.
  2039. The precedence order of operators in C is fully specified, so any
  2040. combination of operations leads to a well-defined nesting. We state
  2041. only part of the full precedence ordering here because it is bad
  2042. practice for C code to depend on the other cases. For cases not
  2043. specified in this chapter, always use parentheses to make the nesting
  2044. explicit.(1)
  2045. You can depend on this subsequence of the precedence ordering (stated
  2046. from highest precedence to lowest):
  2047. 1. Component access (‘.’ and ‘-<’).
  2048. 2. Unary prefix operators.
  2049. 3. Unary postfix operators.
  2050. 4. Multiplication, division, and remainder (they have the same
  2051. precedence).
  2052. 5. Addition and subtraction (they have the same precedence).
  2053. 6. Comparisons—but watch out!
  2054. 7. Logical operators ‘&&’ and ‘||’—but watch out!
  2055. 8. Conditional expression with ‘?’ and ‘:’.
  2056. 9. Assignments.
  2057. 10. Sequential execution (the comma operator, ‘,’).
  2058. Two of the lines in the above list say “but watch out!” That means
  2059. that the line covers operators with subtly different precedence. Never
  2060. depend on the grammar of C to decide how two comparisons nest; instead,
  2061. always use parentheses to specify their nesting.
  2062. You can let several ‘&&’ operators associate, or several ‘||’
  2063. operators, but always use parentheses to show how ‘&&’ and ‘||’ nest
  2064. with each other. *Note Logical Operators::.
  2065. There is one other precedence ordering that code can depend on:
  2066. 1. Unary postfix operators.
  2067. 2. Bitwise and shift operators—but watch out!
  2068. 3. Conditional expression with ‘?’ and ‘:’.
  2069. The caveat for bitwise and shift operators is like that for logical
  2070. operators: you can let multiple uses of one bitwise operator associate,
  2071. but always use parentheses to control nesting of dissimilar operators.
  2072. These lists do not specify any precedence ordering between the
  2073. bitwise and shift operators of the second list and the binary operators
  2074. above conditional expressions in the first list. When they come
  2075. together, parenthesize them. *Note Bitwise Operations::.
  2076. ---------- Footnotes ----------
  2077. (1) Personal note from Richard Stallman: I wrote GCC without
  2078. remembering anything about the C precedence order beyond what’s stated
  2079. here. I studied the full precedence table to write the parser, and
  2080. promptly forgot it again. If you need to look up the full precedence
  2081. order to understand some C code, fix the code with parentheses so nobody
  2082. else needs to do that.
  2083. 10 Order of Execution
  2084. *********************
  2085. The order of execution of a C program is not always obvious, and not
  2086. necessarily predictable. This chapter describes what you can count on.
  2087. 10.1 Reordering of Operands
  2088. ===========================
  2089. The C language does not necessarily carry out operations within an
  2090. expression in the order they appear in the code. For instance, in this
  2091. expression,
  2092. foo () + bar ()
  2093. ‘foo’ might be called first or ‘bar’ might be called first. If ‘foo’
  2094. updates a datum and ‘bar’ uses that datum, the results can be
  2095. unpredictable.
  2096. The unpredictable order of computation of subexpressions also makes a
  2097. difference when one of them contains an assignment. We already saw this
  2098. example of bad code,
  2099. x = 20;
  2100. printf ("%d %d\n", x, x = 4);
  2101. in which the second argument, ‘x’, has a different value depending on
  2102. whether it is computed before or after the assignment in the third
  2103. argument.
  2104. 10.2 Associativity and Ordering
  2105. ===============================
  2106. An associative binary operator, such as ‘+’, when used repeatedly can
  2107. combine any number of operands. The operands’ values may be computed in
  2108. any order.
  2109. If the values are integers and overflow can be ignored, they may be
  2110. combined in any order. Thus, given four functions that return ‘unsigned
  2111. int’, calling them and adding their results as here
  2112. (foo () + bar ()) + (baz () + quux ())
  2113. may add up the results in any order.
  2114. By contrast, arithmetic on signed integers, with overflow
  2115. significant, is not really associative (*note Integer Overflow::).
  2116. Thus, the additions must be done in the order specified, obeying
  2117. parentheses and left-association. That means computing ‘(foo () + bar
  2118. ())’ and ‘(baz () + quux ())’ first (in either order), then adding the
  2119. two.
  2120. The same applies to arithmetic on floating-point values, since that
  2121. too is not really associative. However, the GCC option
  2122. ‘-funsafe-math-optimizations’ allows the compiler to change the order of
  2123. calculation when an associative operation (associative in exact
  2124. mathematics) combines several operands. The option takes effect when
  2125. compiling a module (*note Compilation::). Changing the order of
  2126. association can enable the program to pipeline the floating point
  2127. operations.
  2128. In all these cases, the four function calls can be done in any order.
  2129. There is no right or wrong about that.
  2130. 10.3 Sequence Points
  2131. ====================
  2132. There are some points in the code where C makes limited guarantees about
  2133. the order of operations. These are called “sequence points”. Here is
  2134. where they occur:
  2135. • At the end of a “full expression”; that is to say, an expression
  2136. that is not part of a larger expression. All side effects
  2137. specified by that expression are carried out before execution moves
  2138. on to subsequent code.
  2139. • At the end of the first operand of certain operators: ‘,’, ‘&&’,
  2140. ‘||’, and ‘?:’. All side effects specified by that expression are
  2141. carried out before any execution of the next operand.
  2142. The commas that separate arguments in a function call are _not_
  2143. comma operators, and they do not create sequence points. The rule
  2144. for function arguments and the rule for operands are different
  2145. (*note Ordering of Operands::).
  2146. • Just before calling a function. All side effects specified by the
  2147. argument expressions are carried out before calling the function.
  2148. If the function to be called is not constant—that is, if it is
  2149. computed by an expression—all side effects in that expression are
  2150. carried out before calling the function.
  2151. The ordering imposed by a sequence point applies locally to a limited
  2152. range of code, as stated above in each case. For instance, the ordering
  2153. imposed by the comma operator does not apply to code outside that comma
  2154. operator. Thus, in this code,
  2155. (x = 5, foo (x)) + x * x
  2156. the sequence point of the comma operator orders ‘x = 5’ before ‘foo
  2157. (x)’, but ‘x * x’ could be computed before or after them.
  2158. 10.4 Postincrement and Ordering
  2159. ===============================
  2160. Ordering requirements are loose with the postincrement and postdecrement
  2161. operations (*note Postincrement/Postdecrement::), which specify side
  2162. effects to happen “a little later.” They must happen before the next
  2163. sequence point, but that still leaves room for various meanings. In
  2164. this expression,
  2165. z = x++ - foo ()
  2166. it’s unpredictable whether ‘x’ gets incremented before or after calling
  2167. the function ‘foo’. If ‘foo’ refers to ‘x’, it might see the old value
  2168. or it might see the incremented value.
  2169. In this perverse expression,
  2170. x = x++
  2171. ‘x’ will certainly be incremented but the incremented value may not
  2172. stick. If the incrementation of ‘x’ happens after the assignment to
  2173. ‘x’, the incremented value will remain in place. But if the
  2174. incrementation happens first, the assignment will overwrite that with
  2175. the not-yet-incremented value, so the expression as a whole will leave
  2176. ‘x’ unchanged.
  2177. 10.5 Ordering of Operands
  2178. =========================
  2179. Operands and arguments can be computed in any order, but there are
  2180. limits to this intermixing in GNU C:
  2181. • The operands of a binary arithmetic operator can be computed in
  2182. either order, but they can’t be intermixed: one of them has to come
  2183. first, followed by the other. Any side effects in the operand
  2184. that’s computed first are executed before the other operand is
  2185. computed.
  2186. • That applies to assignment operators too, except that in simple
  2187. assignment the previous value of the left operand is unused.
  2188. • The arguments in a function call can be computed in any order, but
  2189. they can’t be intermixed. Thus, one argument is fully computed,
  2190. then another, and so on until they are all done. Any side effects
  2191. in one argument are executed before computation of another argument
  2192. begins.
  2193. These rules don’t cover side effects caused by postincrement and
  2194. postdecrement operators—those can be deferred up to the next sequence
  2195. point.
  2196. If you want to get pedantic, the fact is that GCC can reorder the
  2197. computations in many other ways provided that doesn’t alter the result
  2198. of running the program. However, because they don’t alter the result of
  2199. running the program, they are negligible, unless you are concerned with
  2200. the values in certain variables at various times as seen by other
  2201. processes. In those cases, you can use ‘volatile’ to prevent
  2202. optimizations that would make them behave strangely. *Note volatile::.
  2203. 10.6 Optimization and Ordering
  2204. ==============================
  2205. Sequence points limit the compiler’s freedom to reorder operations
  2206. arbitrarily, but optimizations can still reorder them if the compiler
  2207. concludes that this won’t alter the results. Thus, in this code,
  2208. x++;
  2209. y = z;
  2210. x++;
  2211. there is a sequence point after each statement, so the code is supposed
  2212. to increment ‘x’ once before the assignment to ‘y’ and once after.
  2213. However, incrementing ‘x’ has no effect on ‘y’ or ‘z’, and setting ‘y’
  2214. can’t affect ‘x’, so the code could be optimized into this:
  2215. y = z;
  2216. x += 2;
  2217. Normally that has no effect except to make the program faster. But
  2218. there are special situations where it can cause trouble due to things
  2219. that the compiler cannot know about, such as shared memory. To limit
  2220. optimization in those places, use the ‘volatile’ type qualifier (*note
  2221. volatile::).
  2222. 11 Primitive Data Types
  2223. ***********************
  2224. This chapter describes all the primitive data types of C—that is, all
  2225. the data types that aren’t built up from other types. They include the
  2226. types ‘int’ and ‘double’ that we’ve already covered.
  2227. These types are all made up of bytes (*note Storage::).
  2228. 11.1 Integer Data Types
  2229. =======================
  2230. Here we describe all the integer types and their basic characteristics.
  2231. *Note Integers in Depth::, for more information about the bit-level
  2232. integer data representations and arithmetic.
  2233. 11.1.1 Basic Integers
  2234. ---------------------
  2235. Integer data types in C can be signed or unsigned. An unsigned type can
  2236. represent only positive numbers and zero. A signed type can represent
  2237. both positive and negative numbers, in a range spread almost equally on
  2238. both sides of zero.
  2239. Aside from signedness, the integer data types vary in size: how many
  2240. bytes long they are. The size determines how many different integer
  2241. values the type can hold.
  2242. Here’s a list of the signed integer data types, with the sizes they
  2243. have on most computers. Each has a corresponding unsigned type; see
  2244. *note Signed and Unsigned Types::.
  2245. ‘signed char’
  2246. One byte (8 bits). This integer type is used mainly for integers
  2247. that represent characters, as part of arrays or other data
  2248. structures.
  2249. ‘short’
  2250. ‘short int’
  2251. Two bytes (16 bits).
  2252. ‘int’
  2253. Four bytes (32 bits).
  2254. ‘long’
  2255. ‘long int’
  2256. Four bytes (32 bits) or eight bytes (64 bits), depending on the
  2257. platform. Typically it is 32 bits on 32-bit computers and 64 bits
  2258. on 64-bit computers, but there are exceptions.
  2259. ‘long long’
  2260. ‘long long int’
  2261. Eight bytes (64 bits). Supported in GNU C in the 1980s, and
  2262. incorporated into standard C as of ISO C99.
  2263. You can omit ‘int’ when you use ‘long’ or ‘short’. This is harmless
  2264. and customary.
  2265. 11.1.2 Signed and Unsigned Types
  2266. --------------------------------
  2267. An unsigned integer type can represent only positive numbers and zero.
  2268. A signed type can represent both positive and negative number, in a
  2269. range spread almost equally on both sides of zero. For instance,
  2270. ‘unsigned char’ holds numbers from 0 to 255 (on most computers), while
  2271. ‘signed char’ holds numbers from −128 to 127. Each of these types holds
  2272. 256 different possible values, since they are both 8 bits wide.
  2273. Write ‘signed’ or ‘unsigned’ before the type keyword to specify a
  2274. signed or an unsigned type. However, the integer types other than
  2275. ‘char’ are signed by default; with them, ‘signed’ is a no-op.
  2276. Plain ‘char’ may be signed or unsigned; this depends on the compiler,
  2277. the machine in use, and its operating system.
  2278. In many programs, it makes no difference whether ‘char’ is signed.
  2279. When it does matter, don’t leave it to chance; write ‘signed char’ or
  2280. ‘unsigned char’.(1)
  2281. ---------- Footnotes ----------
  2282. (1) Personal note from Richard Stallman: Eating with hackers at a
  2283. fish restaurant, I ordered Arctic Char. When my meal arrived, I noted
  2284. that the chef had not signed it. So I complained, “This char is
  2285. unsigned—I wanted a signed char!” Or rather, I would have said this if I
  2286. had thought of it fast enough.
  2287. 11.1.3 Narrow Integers
  2288. ----------------------
  2289. The types that are narrower than ‘int’ are rarely used for ordinary
  2290. variables—we declare them ‘int’ instead. This is because C converts
  2291. those narrower types to ‘int’ for any arithmetic. There is literally no
  2292. reason to declare a local variable ‘char’, for instance.
  2293. In particular, if the value is really a character, you should declare
  2294. the variable ‘int’. Not ‘char’! Using that narrow type can force the
  2295. compiler to truncate values for conversion, which is a waste.
  2296. Furthermore, some functions return either a character value, or −1 for
  2297. “no character.” Using ‘int’ keeps those values distinct.
  2298. The narrow integer types are useful as parts of other objects, such
  2299. as arrays and structures. Compare these array declarations, whose sizes
  2300. on 32-bit processors are shown:
  2301. signed char ac[1000]; /* 1000 bytes */
  2302. short as[1000]; /* 2000 bytes */
  2303. int ai[1000]; /* 4000 bytes */
  2304. long long all[1000]; /* 8000 bytes */
  2305. In addition, character strings must be made up of ‘char’s, because
  2306. that’s what all the standard library string functions expect. Thus,
  2307. array ‘ac’ could be used as a character string, but the others could not
  2308. be.
  2309. 11.1.4 Conversion among Integer Types
  2310. -------------------------------------
  2311. C converts between integer types implicitly in many situations. It
  2312. converts the narrow integer types, ‘char’ and ‘short’, to ‘int’ whenever
  2313. they are used in arithmetic. Assigning a new value to an integer
  2314. variable (or other lvalue) converts the value to the variable’s type.
  2315. You can also convert one integer type to another explicitly with a
  2316. “cast” operator. *Note Explicit Type Conversion::.
  2317. The process of conversion to a wider type is straightforward: the
  2318. value is unchanged. The only exception is when converting a negative
  2319. value (in a signed type, obviously) to a wider unsigned type. In that
  2320. case, the result is a positive value with the same bits (*note Integers
  2321. in Depth::).
  2322. Converting to a narrower type, also called “truncation”, involves
  2323. discarding some of the value’s bits. This is not considered overflow
  2324. (*note Integer Overflow::) because loss of significant bits is a normal
  2325. consequence of truncation. Likewise for conversion between signed and
  2326. unsigned types of the same width.
  2327. More information about conversion for assignment is in *note
  2328. Assignment Type Conversions::. For conversion for arithmetic, see *note
  2329. Argument Promotions::.
  2330. 11.1.5 Boolean Type
  2331. -------------------
  2332. The unsigned integer type ‘bool’ holds truth values: its possible values
  2333. are 0 and 1. Converting any nonzero value to ‘bool’ results in 1. For
  2334. example:
  2335. bool a = 0;
  2336. bool b = 1;
  2337. bool c = 4; /* Stores the value 1 in ‘c’. */
  2338. Unlike ‘int’, ‘bool’ is not a keyword. It is defined in the header
  2339. file ‘stdbool.h’.
  2340. 11.1.6 Integer Variations
  2341. -------------------------
  2342. The integer types of C have standard _names_, but what they _mean_
  2343. varies depending on the kind of platform in use: which kind of computer,
  2344. which operating system, and which compiler. It may even depend on the
  2345. compiler options used.
  2346. Plain ‘char’ may be signed or unsigned; this depends on the platform,
  2347. too. Even for GNU C, there is no general rule.
  2348. In theory, all of the integer types’ sizes can vary. ‘char’ is
  2349. always considered one “byte” for C, but it is not necessarily an 8-bit
  2350. byte; on some platforms it may be more than 8 bits. ISO C specifies
  2351. only that none of these types is narrower than the ones above it in the
  2352. list in *note Basic Integers::, and that ‘short’ has at least 16 bits.
  2353. It is possible that in the future GNU C will support platforms where
  2354. ‘int’ is 64 bits long. In practice, however, on today’s real computers,
  2355. there is little variation; you can rely on the table given previously
  2356. (*note Basic Integers::).
  2357. To be completely sure of the size of an integer type, use the types
  2358. ‘int16_t’, ‘int32_t’ and ‘int64_t’. Their corresponding unsigned types
  2359. add ‘u’ at the front. To define these, include the header file
  2360. ‘stdint.h’.
  2361. The GNU C Compiler compiles for some embedded controllers that use
  2362. two bytes for ‘int’. On some, ‘int’ is just one “byte,” and so is
  2363. ‘short int’—but that “byte” may contain 16 bits or even 32 bits. These
  2364. processors can’t support an ordinary operating system (they may have
  2365. their own specialized operating systems), and most C programs do not try
  2366. to support them.
  2367. 11.2 Floating-Point Data Types
  2368. ==============================
  2369. “Floating point” is the binary analogue of scientific notation:
  2370. internally it represents a number as a fraction and a binary exponent;
  2371. the value is that fraction multiplied by the specified power of 2.
  2372. For instance, to represent 6, the fraction would be 0.75 and the
  2373. exponent would be 3; together they stand for the value 0.75 * 2^{3},
  2374. meaning 0.75 * 8. The value 1.5 would use 0.75 as the fraction and 1 as
  2375. the exponent. The value 0.75 would use 0.75 as the fraction and 0 as
  2376. the exponent. The value 0.375 would use 0.75 as the fraction and -1 as
  2377. the exponent.
  2378. These binary exponents are used by machine instructions. You can
  2379. write a floating-point constant this way if you wish, using hexadecimal;
  2380. but normally we write floating-point numbers in decimal. *Note Floating
  2381. Constants::.
  2382. C has three floating-point data types:
  2383. ‘double’
  2384. “Double-precision” floating point, which uses 64 bits. This is the
  2385. normal floating-point type, and modern computers normally do their
  2386. floating-point computations in this type, or some wider type.
  2387. Except when there is a special reason to do otherwise, this is the
  2388. type to use for floating-point values.
  2389. ‘float’
  2390. “Single-precision” floating point, which uses 32 bits. It is
  2391. useful for floating-point values stored in structures and arrays,
  2392. to save space when the full precision of ‘double’ is not needed.
  2393. In addition, single-precision arithmetic is faster on some
  2394. computers, and occasionally that is useful. But not often—most
  2395. programs don’t use the type ‘float’.
  2396. C would be cleaner if ‘float’ were the name of the type we use for
  2397. most floating-point values; however, for historical reasons, that’s
  2398. not so.
  2399. ‘long double’
  2400. “Extended-precision” floating point is either 80-bit or 128-bit
  2401. precision, depending on the machine in use. On some machines,
  2402. which have no floating-point format wider than ‘double’, this is
  2403. equivalent to ‘double’.
  2404. Floating-point arithmetic raises many subtle issues. *Note Floating
  2405. Point in Depth::, for more information.
  2406. 11.3 Complex Data Types
  2407. =======================
  2408. Complex numbers can include both a real part and an imaginary part. The
  2409. numeric constants covered above have real-numbered values. An
  2410. imaginary-valued constant is an ordinary real-valued constant followed
  2411. by ‘i’.
  2412. To declare numeric variables as complex, use the ‘_Complex’
  2413. keyword.(1) The standard C complex data types are floating point,
  2414. _Complex float foo;
  2415. _Complex double bar;
  2416. _Complex long double quux;
  2417. but GNU C supports integer complex types as well.
  2418. Since ‘_Complex’ is a keyword just like ‘float’ and ‘double’ and
  2419. ‘long’, the keywords can appear in any order, but the order shown above
  2420. seems most logical.
  2421. GNU C supports constants for complex values; for instance, ‘4.0 +
  2422. 3.0i’ has the value 4 + 3i as type ‘_Complex double’. *Note Imaginary
  2423. Constants::.
  2424. To pull the real and imaginary parts of the number back out, GNU C
  2425. provides the keywords ‘__real__’ and ‘__imag__’:
  2426. _Complex double foo = 4.0 + 3.0i;
  2427. double a = __real__ foo; /* ‘a’ is now 4.0. */
  2428. double b = __imag__ foo; /* ‘b’ is now 3.0. */
  2429. Standard C does not include these keywords, and instead relies on
  2430. functions defined in ‘complex.h’ for accessing the real and imaginary
  2431. parts of a complex number: ‘crealf’, ‘creal’, and ‘creall’ extract the
  2432. real part of a float, double, or long double complex number,
  2433. respectively; ‘cimagf’, ‘cimag’, and ‘cimagl’ extract the imaginary
  2434. part.
  2435. GNU C also defines ‘~’ as an operator for complex conjugation, which
  2436. means negating the imaginary part of a complex number:
  2437. _Complex double foo = 4.0 + 3.0i;
  2438. _Complex double bar = ~foo; /* ‘bar’ is now 4 − 3i. */
  2439. For standard C compatibility, you can use the appropriate library
  2440. function: ‘conjf’, ‘conj’, or ‘confl’.
  2441. ---------- Footnotes ----------
  2442. (1) For compatibility with older versions of GNU C, the keyword
  2443. ‘__complex__’ is also allowed. Going forward, however, use the new
  2444. ‘_Complex’ keyword as defined in ISO C11.
  2445. 11.4 The Void Type
  2446. ==================
  2447. The data type ‘void’ is a dummy—it allows no operations. It really
  2448. means “no value at all.” When a function is meant to return no value, we
  2449. write ‘void’ for its return type. Then ‘return’ statements in that
  2450. function should not specify a value (*note return Statement::). Here’s
  2451. an example:
  2452. void
  2453. print_if_positive (double x, double y)
  2454. {
  2455. if (x >=0)
  2456. return;
  2457. if (y >=0)
  2458. return;
  2459. printf ("Next point is (%f,%f)\n", x, y);
  2460. }
  2461. A ‘void’-returning function is comparable to what some other
  2462. languages call a “procedure” instead of a “function.”
  2463. 11.5 Other Data Types
  2464. =====================
  2465. Beyond the primitive types, C provides several ways to construct new
  2466. data types. For instance, you can define “pointers”, values that
  2467. represent the addresses of other data (*note Pointers::). You can
  2468. define “structures”, as in many other languages (*note Structures::),
  2469. and “unions”, which specify multiple ways to look at the same memory
  2470. space (*note Unions::). “Enumerations” are collections of named integer
  2471. codes (*note Enumeration Types::).
  2472. “Array types” in C are used for allocating space for objects, but C
  2473. does not permit operating on an array value as a whole. *Note Arrays::.
  2474. 11.6 Type Designators
  2475. =====================
  2476. Some C constructs require a way to designate a specific data type
  2477. independent of any particular variable or expression which has that
  2478. type. The way to do this is with a “type designator”. The constucts
  2479. that need one include casts (*note Explicit Type Conversion::) and
  2480. ‘sizeof’ (*note Type Size::).
  2481. We also use type designators to talk about the type of a value in C,
  2482. so you will see many type designators in this manual. When we say, “The
  2483. value has type ‘int’,” ‘int’ is a type designator.
  2484. To make the designator for any type, imagine a variable declaration
  2485. for a variable of that type and delete the variable name and the final
  2486. semicolon.
  2487. For example, to designate the type of full-word integers, we start
  2488. with the declaration for a variable ‘foo’ with that type, which is this:
  2489. int foo;
  2490. Then we delete the variable name ‘foo’ and the semicolon, leaving
  2491. ‘int’—exactly the keyword used in such a declaration. Therefore, the
  2492. type designator for this type is ‘int’.
  2493. What about long unsigned integers? From the declaration
  2494. unsigned long int foo;
  2495. we determine that the designator is ‘unsigned long int’.
  2496. Following this procedure, the designator for any primitive type is
  2497. simply the set of keywords which specifies that type in a declaration.
  2498. The same is true for compound types such as structures, unions, and
  2499. enumerations.
  2500. Designators for pointer types do follow the rule of deleting the
  2501. variable name and semicolon, but the result is not so simple. *Note
  2502. Pointer Type Designators::, as part of the chapter about pointers.
  2503. *Note Array Type Designators::), for designators for array types.
  2504. To understand what type a designator stands for, imagine a variable
  2505. name inserted into the right place in the designator to make a valid
  2506. declaration. What type would that variable be declared as? That is the
  2507. type the designator designates.
  2508. 12 Constants
  2509. ************
  2510. A “constant” is an expression that stands for a specific value by
  2511. explicitly representing the desired value. C allows constants for
  2512. numbers, characters, and strings. We have already seen numeric and
  2513. string constants in the examples.
  2514. 12.1 Integer Constants
  2515. ======================
  2516. An integer constant consists of a number to specify the value, followed
  2517. optionally by suffix letters to specify the data type.
  2518. The simplest integer constants are numbers written in base 10
  2519. (decimal), such as ‘5’, ‘77’, and ‘403’. A decimal constant cannot
  2520. start with the character ‘0’ (zero) because that makes the constant
  2521. octal.
  2522. You can get the effect of a negative integer constant by putting a
  2523. minus sign at the beginning. Grammatically speaking, that is an
  2524. arithmetic expression rather than a constant, but it behaves just like a
  2525. true constant.
  2526. Integer constants can also be written in octal (base 8), hexadecimal
  2527. (base 16), or binary (base 2). An octal constant starts with the
  2528. character ‘0’ (zero), followed by any number of octal digits (‘0’ to
  2529. ‘7’):
  2530. 0 // zero
  2531. 077 // 63
  2532. 0403 // 259
  2533. Pedantically speaking, the constant ‘0’ is an octal constant, but we can
  2534. think of it as decimal; it has the same value either way.
  2535. A hexadecimal constant starts with ‘0x’ (upper or lower case)
  2536. followed by hex digits (‘0’ to ‘9’, as well as ‘a’ through ‘f’ in upper
  2537. or lower case):
  2538. 0xff // 255
  2539. 0XA0 // 160
  2540. 0xffFF // 65535
  2541. A binary constant starts with ‘0b’ (upper or lower case) followed by
  2542. bits (each represented by the characters ‘0’ or ‘1’):
  2543. 0b101 // 5
  2544. Binary constants are a GNU C extension, not part of the C standard.
  2545. Sometimes a space is needed after an integer constant to avoid
  2546. lexical confusion with the following tokens. *Note Invalid Numbers::.
  2547. 12.2 Integer Constant Data Types
  2548. ================================
  2549. The type of an integer constant is normally ‘int’, if the value fits in
  2550. that type, but here are the complete rules. The type of an integer
  2551. constant is the first one in this sequence that can properly represent
  2552. the value,
  2553. 1. ‘int’
  2554. 2. ‘unsigned int’
  2555. 3. ‘long int’
  2556. 4. ‘unsigned long int’
  2557. 5. ‘long long int’
  2558. 6. ‘unsigned long long int’
  2559. and that isn’t excluded by the following rules.
  2560. If the constant has ‘l’ or ‘L’ as a suffix, that excludes the first
  2561. two types (non-‘long’).
  2562. If the constant has ‘ll’ or ‘LL’ as a suffix, that excludes first
  2563. four types (non-‘long long’).
  2564. If the constant has ‘u’ or ‘U’ as a suffix, that excludes the signed
  2565. types.
  2566. Otherwise, if the constant is decimal, that excludes the unsigned
  2567. types.
  2568. Here are some examples of the suffixes.
  2569. 3000000000u // three billion as ‘unsigned int’.
  2570. 0LL // zero as a ‘long long int’.
  2571. 0403l // 259 as a ‘long int’.
  2572. Suffixes in integer constants are rarely used. When the precise type
  2573. is important, it is cleaner to convert explicitly (*note Explicit Type
  2574. Conversion::).
  2575. *Note Integer Types::.
  2576. 12.3 Floating-Point Constants
  2577. =============================
  2578. A floating-point constant must have either a decimal point, an
  2579. exponent-of-ten, or both; they distinguish it from an integer constant.
  2580. To indicate an exponent, write ‘e’ or ‘E’. The exponent value
  2581. follows. It is always written as a decimal number; it can optionally
  2582. start with a sign. The exponent N means to multiply the constant’s
  2583. value by ten to the Nth power.
  2584. Thus, ‘1500.0’, ‘15e2’, ‘15e+2’, ‘15.0e2’, ‘1.5e+3’, ‘.15e4’, and
  2585. ‘15000e-1’ are six ways of writing a floating-point number whose value
  2586. is 1500. They are all equivalent.
  2587. Here are more examples with decimal points:
  2588. 1.0
  2589. 1000.
  2590. 3.14159
  2591. .05
  2592. .0005
  2593. For each of them, here are some equivalent constants written with
  2594. exponents:
  2595. 1e0, 1.0000e0
  2596. 100e1, 100e+1, 100E+1, 1e3, 10000e-1
  2597. 3.14159e0
  2598. 5e-2, .0005e+2, 5E-2, .0005E2
  2599. .05e-2
  2600. A floating-point constant normally has type ‘double’. You can force
  2601. it to type ‘float’ by adding ‘f’ or ‘F’ at the end. For example,
  2602. 3.14159f
  2603. 3.14159e0f
  2604. 1000.f
  2605. 100E1F
  2606. .0005f
  2607. .05e-2f
  2608. Likewise, ‘l’ or ‘L’ at the end forces the constant to type ‘long
  2609. double’.
  2610. You can use exponents in hexadecimal floating constants, but since
  2611. ‘e’ would be interpreted as a hexadecimal digit, the character ‘p’ or
  2612. ‘P’ (for “power”) indicates an exponent.
  2613. The exponent in a hexadecimal floating constant is a possibly-signed
  2614. decimal integer that specifies a power of 2 (_not_ 10 or 16) to multiply
  2615. into the number.
  2616. Here are some examples:
  2617. 0xAp2 // 40 in decimal
  2618. 0xAp-1 // 5 in decimal
  2619. 0x2.0Bp4 // 16.75 decimal
  2620. 0xE.2p3 // 121 decimal
  2621. 0x123.ABCp0 // 291.6708984375 in decimal
  2622. 0x123.ABCp4 // 4666.734375 in decimal
  2623. 0x100p-8 // 1
  2624. 0x10p-4 // 1
  2625. 0x1p+4 // 16
  2626. 0x1p+8 // 256
  2627. *Note Floating-Point Data Types::.
  2628. 12.4 Imaginary Constants
  2629. ========================
  2630. A complex number consists of a real part plus an imaginary part.
  2631. (Either or both parts may be zero.) This section explains how to write
  2632. numeric constants with imaginary values. By adding these to ordinary
  2633. real-valued numeric constants, we can make constants with complex
  2634. values.
  2635. The simple way to write an imaginary-number constant is to attach the
  2636. suffix ‘i’ or ‘I’, or ‘j’ or ‘J’, to an integer or floating-point
  2637. constant. For example, ‘2.5fi’ has type ‘_Complex float’ and ‘3i’ has
  2638. type ‘_Complex int’. The four alternative suffix letters are all
  2639. equivalent.
  2640. The other way to write an imaginary constant is to multiply a real
  2641. constant by ‘_Complex_I’, which represents the imaginary number i.
  2642. Standard C doesn’t support suffixing with ‘i’ or ‘j’, so this clunky way
  2643. is needed.
  2644. To write a complex constant with a nonzero real part and a nonzero
  2645. imaginary part, write the two separately and add them, like this:
  2646. 4.0 + 3.0i
  2647. That gives the value 4 + 3i, with type ‘_Complex double’.
  2648. Such a sum can include multiple real constants, or none. Likewise,
  2649. it can include multiple imaginary constants, or none. For example:
  2650. _Complex double foo, bar, quux;
  2651. foo = 2.0i + 4.0 + 3.0i; /* Imaginary part is 5.0. */
  2652. bar = 4.0 + 12.0; /* Imaginary part is 0.0. */
  2653. quux = 3.0i + 15.0i; /* Real part is 0.0. */
  2654. *Note Complex Data Types::.
  2655. 12.5 Invalid Numbers
  2656. ====================
  2657. Some number-like constructs which are not really valid as numeric
  2658. constants are treated as numbers in preprocessing directives. If these
  2659. constructs appear outside of preprocessing, they are erroneous. *Note
  2660. Preprocessing Tokens::.
  2661. Sometimes we need to insert spaces to separate tokens so that they
  2662. won’t be combined into a single number-like construct. For example,
  2663. ‘0xE+12’ is a preprocessing number that is not a valid numeric constant,
  2664. so it is a syntax error. If what we want is the three tokens
  2665. ‘0xE + 12’, we have to use those spaces as separators.
  2666. 12.6 Character Constants
  2667. ========================
  2668. A “character constant” is written with single quotes, as in ‘'C'’. In
  2669. the simplest case, C is a single ASCII character that the constant
  2670. should represent. The constant has type ‘int’, and its value is the
  2671. character code of that character. For instance, ‘'a'’ represents the
  2672. character code for the letter ‘a’: 97, that is.
  2673. To put the ‘'’ character (single quote) in the character constant,
  2674. “quote” it with a backslash (‘\’). This character constant looks like
  2675. ‘'\''’. This sort of sequence, starting with ‘\’, is called an “escape
  2676. sequence”—the backslash character here functions as a kind of “escape
  2677. character”.
  2678. To put the ‘\’ character (backslash) in the character constant, quote
  2679. it likewise with ‘\’ (another backslash). This character constant looks
  2680. like ‘'\\'’.
  2681. Here are all the escape sequences that represent specific characters
  2682. in a character constant. The numeric values shown are the corresponding
  2683. ASCII character codes, as decimal numbers.
  2684. '\a' ⇒ 7 /* alarm, ‘CTRL-g’ */
  2685. '\b' ⇒ 8 /* backspace, >BS<, ‘CTRL-h’ */
  2686. '\t' ⇒ 9 /* tab, >TAB<, ‘CTRL-i’ */
  2687. '\n' ⇒ 10 /* newline, ‘CTRL-j’ */
  2688. '\v' ⇒ 11 /* vertical tab, ‘CTRL-k’ */
  2689. '\f' ⇒ 12 /* formfeed, ‘CTRL-l’ */
  2690. '\r' ⇒ 13 /* carriage return, >RET<, ‘CTRL-m’ */
  2691. '\e' ⇒ 27 /* escape character, >ESC<, ‘CTRL-[’ */
  2692. '\\' ⇒ 92 /* backslash character, ‘\’ */
  2693. '\'' ⇒ 39 /* singlequote character, ‘'’ */
  2694. '\"' ⇒ 34 /* doublequote character, ‘"’ */
  2695. '\?' ⇒ 63 /* question mark, ‘?’ */
  2696. ‘\e’ is a GNU C extension; to stick to standard C, write ‘\33’.
  2697. You can also write octal and hex character codes as ‘\OCTALCODE’ or
  2698. ‘\xHEXCODE’. Decimal is not an option here, so octal codes do not need
  2699. to start with ‘0’.
  2700. The character constant’s value has type ‘int’. However, the
  2701. character code is treated initially as a ‘char’ value, which is then
  2702. converted to ‘int’. If the character code is greater than 127 (‘0177’
  2703. in octal), the resulting ‘int’ may be negative on a platform where the
  2704. type ‘char’ is 8 bits long and signed.
  2705. 12.7 String Constants
  2706. =====================
  2707. A “string constant” represents a series of characters. It starts with
  2708. ‘"’ and ends with ‘"’; in between are the contents of the string.
  2709. Quoting special characters such as ‘"’, ‘\’ and newline in the contents
  2710. works in string constants as in character constants. In a string
  2711. constant, ‘'’ does not need to be quoted.
  2712. A string constant defines an array of characters which contains the
  2713. specified characters followed by the null character (code 0). Using the
  2714. string constant is equivalent to using the name of an array with those
  2715. contents. In simple cases, the length in bytes of the string constant
  2716. is one greater than the number of characters written in it.
  2717. As with any array in C, using the string constant in an expression
  2718. converts the array to a pointer (*note Pointers::) to the array’s first
  2719. element (*note Accessing Array Elements::). This pointer will have type
  2720. ‘char *’ because it points to an element of type ‘char’. ‘char *’ is an
  2721. example of a type designator for a pointer type (*note Pointer Type
  2722. Designators::). That type is used for strings generally, not just the
  2723. strings expressed as constants in a program.
  2724. Thus, the string constant ‘"Foo!"’ is almost equivalent to declaring
  2725. an array like this
  2726. char string_array_1[] = {'F', 'o', 'o', '!', '\0' };
  2727. and then using ‘string_array_1’ in the program. There are two
  2728. differences, however:
  2729. • The string constant doesn’t define a name for the array.
  2730. • The string constant is probably stored in a read-only area of
  2731. memory.
  2732. Newlines are not allowed in the text of a string constant. The
  2733. motive for this prohibition is to catch the error of omitting the
  2734. closing ‘"’. To put a newline in a constant string, write it as ‘\n’ in
  2735. the string constant.
  2736. A real null character in the source code inside a string constant
  2737. causes a warning. To put a null character in the middle of a string
  2738. constant, write ‘\0’ or ‘\000’.
  2739. Consecutive string constants are effectively concatenated. Thus,
  2740. "Fo" "o!" is equivalent to "Foo!"
  2741. This is useful for writing a string containing multiple lines, like
  2742. this:
  2743. "This message is so long that it needs more than\n"
  2744. "a single line of text. C does not allow a newline\n"
  2745. "to represent itself in a string constant, so we have to\n"
  2746. "write \\n to put it in the string. For readability of\n"
  2747. "the source code, it is advisable to put line breaks in\n"
  2748. "the source where they occur in the contents of the\n"
  2749. "constant.\n"
  2750. The sequence of a backslash and a newline is ignored anywhere in a C
  2751. program, and that includes inside a string constant. Thus, you can
  2752. write multi-line string constants this way:
  2753. "This is another way to put newlines in a string constant\n\
  2754. and break the line after them in the source code."
  2755. However, concatenation is the recommended way to do this.
  2756. You can also write perverse string constants like this,
  2757. "Fo\
  2758. o!"
  2759. but don’t do that—write it like this instead:
  2760. "Foo!"
  2761. Be careful to avoid passing a string constant to a function that
  2762. modifies the string it receives. The memory where the string constant
  2763. is stored may be read-only, which would cause a fatal ‘SIGSEGV’ signal
  2764. that normally terminates the function (*note Signals::. Even worse, the
  2765. memory may not be read-only. Then the function might modify the string
  2766. constant, thus spoiling the contents of other string constants that are
  2767. supposed to contain the same value and are unified by the compiler.
  2768. 12.8 UTF-8 String Constants
  2769. ===========================
  2770. Writing ‘u8’ immediately before a string constant, with no intervening
  2771. space, means to represent that string in UTF-8 encoding as a sequence of
  2772. bytes. UTF-8 represents ASCII characters with a single byte, and
  2773. represents non-ASCII Unicode characters (codes 128 and up) as multibyte
  2774. sequences. Here is an example of a UTF-8 constant:
  2775. u8"A cónstàñt"
  2776. This constant occupies 13 bytes plus the terminating null, because
  2777. each of the accented letters is a two-byte sequence.
  2778. Concatenating an ordinary string with a UTF-8 string conceptually
  2779. produces another UTF-8 string. However, if the ordinary string contains
  2780. character codes 128 and up, the results cannot be relied on.
  2781. 12.9 Unicode Character Codes
  2782. ============================
  2783. You can specify Unicode characters, for individual character constants
  2784. or as part of string constants (*note String Constants::), using escape
  2785. sequences. Use the ‘\u’ escape sequence with a 16-bit hexadecimal
  2786. Unicode character code. If the code value is too big for 16 bits, use
  2787. the ‘\U’ escape sequence with a 32-bit hexadecimal Unicode character
  2788. code. (These codes are called “universal character names”.) For
  2789. example,
  2790. \u6C34 /* 16-bit code (UTF-16) */
  2791. \U0010ABCD /* 32-bit code (UTF-32) */
  2792. One way to use these is in UTF-8 string constants (*note UTF-8 String
  2793. Constants::). For instance,
  2794. u8"fóó \u6C34 \U0010ABCD"
  2795. You can also use them in wide character constants (*note Wide
  2796. Character Constants::), like this:
  2797. u'\u6C34' /* 16-bit code */
  2798. U'\U0010ABCD' /* 32-bit code */
  2799. and in wide string constants (*note Wide String Constants::), like this:
  2800. u"\u6C34\u6C33" /* 16-bit code */
  2801. U"\U0010ABCD" /* 32-bit code */
  2802. Codes in the range of ‘D800’ through ‘DFFF’ are not valid in Unicode.
  2803. Codes less than ‘00A0’ are also forbidden, except for ‘0024’, ‘0040’,
  2804. and ‘0060’; these characters are actually ASCII control characters, and
  2805. you can specify them with other escape sequences (*note Character
  2806. Constants::).
  2807. 12.10 Wide Character Constants
  2808. ==============================
  2809. A “wide character constant” represents characters with more than 8 bits
  2810. of character code. This is an obscure feature that we need to document
  2811. but that you probably won’t ever use. If you’re just learning C, you
  2812. may as well skip this section.
  2813. The original C wide character constant looks like ‘L’ (upper case!)
  2814. followed immediately by an ordinary character constant (with no
  2815. intervening space). Its data type is ‘wchar_t’, which is an alias
  2816. defined in ‘stddef.h’ for one of the standard integer types. Depending
  2817. on the platform, it could be 16 bits or 32 bits. If it is 16 bits,
  2818. these character constants use the UTF-16 form of Unicode; if 32 bits,
  2819. UTF-32.
  2820. There are also Unicode wide character constants which explicitly
  2821. specify the width. These constants start with ‘u’ or ‘U’ instead of
  2822. ‘L’. ‘u’ specifies a 16-bit Unicode wide character constant, and ‘U’ a
  2823. 32-bit Unicode wide character constant. Their types are, respectively,
  2824. ‘char16_t’ and ‘char32_t’; they are declared in the header file
  2825. ‘uchar.h’. These character constants are valid even if ‘uchar.h’ is not
  2826. included, but some uses of them may be inconvenient without including it
  2827. to declare those type names.
  2828. The character represented in a wide character constant can be an
  2829. ordinary ASCII character. ‘L'a'’, ‘u'a'’ and ‘U'a'’ are all valid, and
  2830. they are all equal to ‘'a'’.
  2831. In all three kinds of wide character constants, you can write a
  2832. non-ASCII Unicode character in the constant itself; the constant’s value
  2833. is the character’s Unicode character code. Or you can specify the
  2834. Unicode character with an escape sequence (*note Unicode Character
  2835. Codes::).
  2836. 12.11 Wide String Constants
  2837. ===========================
  2838. A “wide string constant” stands for an array of 16-bit or 32-bit
  2839. characters. They are rarely used; if you’re just learning C, you may as
  2840. well skip this section.
  2841. There are three kinds of wide string constants, which differ in the
  2842. data type used for each character in the string. Each wide string
  2843. constant is equivalent to an array of integers, but the data type of
  2844. those integers depends on the kind of wide string. Using the constant
  2845. in an expression will convert the array to a pointer to its first
  2846. element, as usual for arrays in C (*note Accessing Array Elements::).
  2847. For each kind of wide string constant, we state here what type that
  2848. pointer will be.
  2849. ‘char16_t’
  2850. This is a 16-bit Unicode wide string constant: each element is a
  2851. 16-bit Unicode character code with type ‘char16_t’, so the string
  2852. has the pointer type ‘char16_t *’. (That is a type designator;
  2853. *note Pointer Type Designators::.) The constant is written as ‘u’
  2854. (which must be lower case) followed (with no intervening space) by
  2855. a string constant with the usual syntax.
  2856. ‘char32_t’
  2857. This is a 32-bit Unicode wide string constant: each element is a
  2858. 32-bit Unicode character code, and the string has type
  2859. ‘char32_t *’. It’s written as ‘U’ (which must be upper case)
  2860. followed (with no intervening space) by a string constant with the
  2861. usual syntax.
  2862. ‘wchar_t’
  2863. This is the original kind of wide string constant. It’s written as
  2864. ‘L’ (which must be upper case) followed (with no intervening space)
  2865. by a string constant with the usual syntax, and the string has type
  2866. ‘wchar_t *’.
  2867. The width of the data type ‘wchar_t’ depends on the target
  2868. platform, which makes this kind of wide string somewhat less useful
  2869. than the newer kinds.
  2870. ‘char16_t’ and ‘char32_t’ are declared in the header file ‘uchar.h’.
  2871. ‘wchar_t’ is declared in ‘stddef.h’.
  2872. Consecutive wide string constants of the same kind concatenate, just
  2873. like ordinary string constants. A wide string constant concatenated
  2874. with an ordinary string constant results in a wide string constant. You
  2875. can’t concatenate two wide string constants of different kinds. You
  2876. also can’t concatenate a wide string constant (of any kind) with a UTF-8
  2877. string constant.
  2878. 13 Type Size
  2879. ************
  2880. Each data type has a “size”, which is the number of bytes (*note
  2881. Storage::) that it occupies in memory. To refer to the size in a C
  2882. program, use ‘sizeof’. There are two ways to use it:
  2883. ‘sizeof EXPRESSION’
  2884. This gives the size of EXPRESSION, based on its data type. It does
  2885. not calculate the value of EXPRESSION, only its size, so if
  2886. EXPRESSION includes side effects or function calls, they do not
  2887. happen. Therefore, ‘sizeof’ is always a compile-time operation
  2888. that has zero run-time cost.
  2889. A value that is a bit field (*note Bit Fields::) is not allowed as
  2890. an operand of ‘sizeof’.
  2891. For example,
  2892. double a;
  2893. i = sizeof a + 10;
  2894. sets ‘i’ to 18 on most computers because ‘a’ occupies 8 bytes.
  2895. Here’s how to determine the number of elements in an array ‘array’:
  2896. (sizeof array / sizeof array[0])
  2897. The expression ‘sizeof array’ gives the size of the array, not the
  2898. size of a pointer to an element. However, if EXPRESSION is a
  2899. function parameter that was declared as an array, that variable
  2900. really has a pointer type (*note Array Parm Pointer::), so the
  2901. result is the size of that pointer.
  2902. ‘sizeof (TYPE)’
  2903. This gives the size of TYPE. For example,
  2904. i = sizeof (double) + 10;
  2905. is equivalent to the previous example.
  2906. You can’t apply ‘sizeof’ to an incomplete type (*note Incomplete
  2907. Types::), nor ‘void’. Using it on a function type gives 1 in GNU
  2908. C, which makes adding an integer to a function pointer work as
  2909. desired (*note Pointer Arithmetic::).
  2910. *Warning*: When you use ‘sizeof’ with a type instead of an
  2911. expression, you must write parentheses around the type.
  2912. *Warning*: When applying ‘sizeof’ to the result of a cast (*note
  2913. Explicit Type Conversion::), you must write parentheses around the cast
  2914. expression to avoid an ambiguity in the grammar of C. Specifically,
  2915. sizeof (int) -x
  2916. parses as
  2917. (sizeof (int)) - x
  2918. If what you want is
  2919. sizeof ((int) -x)
  2920. you must write it that way, with parentheses.
  2921. The data type of the value of the ‘sizeof’ operator is always one of
  2922. the unsigned integer types; which one of those types depends on the
  2923. machine. The header file ‘stddef.h’ defines the typedef name ‘size_t’
  2924. as an alias for this type. *Note Defining Typedef Names::.
  2925. 14 Pointers
  2926. ***********
  2927. Among high-level languages, C is rather low level, close to the machine.
  2928. This is mainly because it has explicit “pointers”. A pointer value is
  2929. the numeric address of data in memory. The type of data to be found at
  2930. that address is specified by the data type of the pointer itself. The
  2931. unary operator ‘*’ gets the data that a pointer points to—this is called
  2932. “dereferencing the pointer”.
  2933. C also allows pointers to functions, but since there are some
  2934. differences in how they work, we treat them later. *Note Function
  2935. Pointers::.
  2936. 14.1 Address of Data
  2937. ====================
  2938. The most basic way to make a pointer is with the “address-of” operator,
  2939. ‘&’. Let’s suppose we have these variables available:
  2940. int i;
  2941. double a[5];
  2942. Now, ‘&i’ gives the address of the variable ‘i’—a pointer value that
  2943. points to ‘i’’s location—and ‘&a[3]’ gives the address of the element 3
  2944. of ‘a’. (It is actually the fourth element in the array, since the
  2945. first element has index 0.)
  2946. The address-of operator is unusual because it operates on a place to
  2947. store a value (an lvalue, *note Lvalues::), not on the value currently
  2948. stored there. (The left argument of a simple assignment is unusual in
  2949. the same way.) You can use it on any lvalue except a bit field (*note
  2950. Bit Fields::) or a constructor (*note Structure Constructors::).
  2951. 14.2 Pointer Types
  2952. ==================
  2953. For each data type T, there is a type for pointers to type T. For these
  2954. variables,
  2955. int i;
  2956. double a[5];
  2957. • ‘i’ has type ‘int’; we say ‘&i’ is a “pointer to ‘int’.”
  2958. • ‘a’ has type ‘double[5]’; we say ‘&a’ is a “pointer to arrays of
  2959. five ‘double’s.”
  2960. • ‘a[3]’ has type ‘double’; we say ‘&a[3]’ is a “pointer to
  2961. ‘double’.”
  2962. 14.3 Pointer-Variable Declarations
  2963. ==================================
  2964. The way to declare that a variable ‘foo’ points to type T is
  2965. T *foo;
  2966. To remember this syntax, think “if you dereference ‘foo’, using the
  2967. ‘*’ operator, what you get is type T. Thus, ‘foo’ points to type T.”
  2968. Thus, we can declare variables that hold pointers to these three
  2969. types, like this:
  2970. int *ptri; /* Pointer to ‘int’. */
  2971. double *ptrd; /* Pointer to ‘double’. */
  2972. double (*ptrda)[5]; /* Pointer to ‘double[5]’. */
  2973. ‘int *ptri;’ means, “if you dereference ‘ptri’, you get an ‘int’.”
  2974. ‘double (*ptrda)[5];’ means, “if you dereference ‘ptrda’, then subscript
  2975. it by an integer less than 5, you get a ‘double’.” The parentheses
  2976. express the point that you would dereference it first, then subscript
  2977. it.
  2978. Contrast the last one with this:
  2979. double *aptrd[5]; /* Array of five pointers to ‘double’. */
  2980. Because ‘*’ has higher syntactic precedence than subscripting, you would
  2981. subscript ‘aptrd’ then dereference it. Therefore, it declares an array
  2982. of pointers, not a pointer.
  2983. 14.4 Pointer-Type Designators
  2984. =============================
  2985. Every type in C has a designator; you make it by deleting the variable
  2986. name and the semicolon from a declaration (*note Type Designators::).
  2987. Here are the designators for the pointer types of the example
  2988. declarations in the previous section:
  2989. int * /* Pointer to ‘int’. */
  2990. double * /* Pointer to ‘double’. */
  2991. double (*)[5] /* Pointer to ‘double[5]’. */
  2992. Remember, to understand what type a designator stands for, imagine
  2993. the variable name that would be in the declaration, and figure out what
  2994. type it would declare that variable with. ‘double (*)[5]’ can only come
  2995. from ‘double (*VARIABLE)[5]’, so it’s a pointer which, when
  2996. dereferenced, gives an array of 5 ‘double’s.
  2997. 14.5 Dereferencing Pointers
  2998. ===========================
  2999. The main use of a pointer value is to “dereference it” (access the data
  3000. it points at) with the unary ‘*’ operator. For instance, ‘*&i’ is the
  3001. value at ‘i’’s address—which is just ‘i’. The two expressions are
  3002. equivalent, provided ‘&i’ is valid.
  3003. A pointer-dereference expression whose type is data (not a function)
  3004. is an lvalue.
  3005. Pointers become really useful when we store them somewhere and use
  3006. them later. Here’s a simple example to illustrate the practice:
  3007. {
  3008. int i;
  3009. int *ptr;
  3010. ptr = &i;
  3011. i = 5;
  3012. ...
  3013. return *ptr; /* Returns 5, fetched from ‘i’. */
  3014. }
  3015. This shows how to declare the variable ‘ptr’ as type ‘int *’ (pointer
  3016. to ‘int’), store a pointer value into it (pointing at ‘i’), and use it
  3017. later to get the value of the object it points at (the value in ‘i’).
  3018. If anyone can provide a useful example which is this basic, I would
  3019. be grateful.
  3020. 14.6 Null Pointers
  3021. ==================
  3022. A pointer value can be “null”, which means it does not point to any
  3023. object. The cleanest way to get a null pointer is by writing ‘NULL’, a
  3024. standard macro defined in ‘stddef.h’. You can also do it by casting 0
  3025. to the desired pointer type, as in ‘(char *) 0’. (The cast operator
  3026. performs explicit type conversion; *Note Explicit Type Conversion::.)
  3027. You can store a null pointer in any lvalue whose data type is a
  3028. pointer type:
  3029. char *foo;
  3030. foo = NULL;
  3031. These two, if consecutive, can be combined into a declaration with
  3032. initializer,
  3033. char *foo = NULL;
  3034. You can also explicitly cast ‘NULL’ to the specific pointer type you
  3035. want—it makes no difference.
  3036. char *foo;
  3037. foo = (char *) NULL;
  3038. To test whether a pointer is null, compare it with zero or ‘NULL’, as
  3039. shown here:
  3040. if (p != NULL)
  3041. /* ‘p’ is not null. */
  3042. operate (p);
  3043. Since testing a pointer for not being null is basic and frequent, all
  3044. but beginners in C will understand the conditional without need for ‘!=
  3045. NULL’:
  3046. if (p)
  3047. /* ‘p’ is not null. */
  3048. operate (p);
  3049. 14.7 Dereferencing Null or Invalid Pointers
  3050. ===========================================
  3051. Trying to dereference a null pointer is an error. On most platforms, it
  3052. generally causes a signal, usually ‘SIGSEGV’ (*note Signals::).
  3053. char *foo = NULL;
  3054. c = *foo; /* This causes a signal and terminates. */
  3055. Likewise a pointer that has the wrong alignment for the target data type
  3056. (on most types of computer), or points to a part of memory that has not
  3057. been allocated in the process’s address space.
  3058. The signal terminates the program, unless the program has arranged to
  3059. handle the signal (*note The GNU C Library: (libc)Signal Handling.).
  3060. However, the signal might not happen if the dereference is optimized
  3061. away. In the example above, if you don’t subsequently use the value of
  3062. ‘c’, GCC might optimize away the code for ‘*foo’. You can prevent such
  3063. optimization using the ‘volatile’ qualifier, as shown here:
  3064. volatile char *p;
  3065. volatile char c;
  3066. c = *p;
  3067. You can use this to test whether ‘p’ points to unallocated memory.
  3068. Set up a signal handler first, so the signal won’t terminate the
  3069. program.
  3070. 14.8 Void Pointers
  3071. ==================
  3072. The peculiar type ‘void *’, a pointer whose target type is ‘void’, is
  3073. used often in C. It represents a pointer to we-don’t-say-what. Thus,
  3074. void *numbered_slot_pointer (int);
  3075. declares a function ‘numbered_slot_pointer’ that takes an integer
  3076. parameter and returns a pointer, but we don’t say what type of data it
  3077. points to.
  3078. With type ‘void *’, you can pass the pointer around and test whether
  3079. it is null. However, dereferencing it gives a ‘void’ value that can’t
  3080. be used (*note The Void Type::). To dereference the pointer, first
  3081. convert it to some other pointer type.
  3082. Assignments convert ‘void *’ automatically to any other pointer type,
  3083. if the left operand has a pointer type; for instance,
  3084. {
  3085. int *p;
  3086. /* Converts return value to ‘int *’. */
  3087. p = numbered_slot_pointer (5);
  3088. ...
  3089. }
  3090. Passing an argument of type ‘void *’ for a parameter that has a
  3091. pointer type also converts. For example, supposing the function ‘hack’
  3092. is declared to require type ‘float *’ for its argument, this will
  3093. convert the null pointer to that type.
  3094. /* Declare ‘hack’ that way.
  3095. We assume it is defined somewhere else. */
  3096. void hack (float *);
  3097. ...
  3098. /* Now call ‘hack’. */
  3099. {
  3100. /* Converts return value of ‘numbered_slot_pointer’
  3101. to ‘float *’ to pass it to ‘hack’. */
  3102. hack (numbered_slot_pointer (5));
  3103. ...
  3104. }
  3105. You can also convert to another pointer type with an explicit cast
  3106. (*note Explicit Type Conversion::), like this:
  3107. (int *) numbered_slot_pointer (5)
  3108. Here is an example which decides at run time which pointer type to
  3109. convert to:
  3110. void
  3111. extract_int_or_double (void *ptr, bool its_an_int)
  3112. {
  3113. if (its_an_int)
  3114. handle_an_int (*(int *)ptr);
  3115. else
  3116. handle_a_double (*(double *)ptr);
  3117. }
  3118. The expression ‘*(int *)ptr’ means to convert ‘ptr’ to type ‘int *’,
  3119. then dereference it.
  3120. 14.9 Pointer Comparison
  3121. =======================
  3122. Two pointer values are equal if they point to the same location, or if
  3123. they are both null. You can test for this with ‘==’ and ‘!=’. Here’s a
  3124. trivial example:
  3125. {
  3126. int i;
  3127. int *p, *q;
  3128. p = &i;
  3129. q = &i;
  3130. if (p == q)
  3131. printf ("This will be printed.\n");
  3132. if (p != q)
  3133. printf ("This won't be printed.\n");
  3134. }
  3135. Ordering comparisons such as ‘<’ and ‘<=’ operate on pointers by
  3136. converting them to unsigned integers. The C standard says the two
  3137. pointers must point within the same object in memory, but on GNU/Linux
  3138. systems these operations simply compare the numeric values of the
  3139. pointers.
  3140. The pointer values to be compared should in principle have the same
  3141. type, but they are allowed to differ in limited cases. First of all, if
  3142. the two pointers’ target types are nearly compatible (*note Compatible
  3143. Types::), the comparison is allowed.
  3144. If one of the operands is ‘void *’ (*note Void Pointers::) and the
  3145. other is another pointer type, the comparison operator converts the
  3146. ‘void *’ pointer to the other type so as to compare them. (In standard
  3147. C, this is not allowed if the other type is a function pointer type, but
  3148. that works in GNU C.)
  3149. Comparison operators also allow comparing the integer 0 with a
  3150. pointer value. Thus works by converting 0 to a null pointer of the same
  3151. type as the other operand.
  3152. 14.10 Pointer Arithmetic
  3153. ========================
  3154. Adding an integer (positive or negative) to a pointer is valid in C. It
  3155. assumes that the pointer points to an element in an array, and advances
  3156. or retracts the pointer across as many array elements as the integer
  3157. specifies. Here is an example, in which adding a positive integer
  3158. advances the pointer to a later element in the same array.
  3159. void
  3160. incrementing_pointers ()
  3161. {
  3162. int array[5] = { 45, 29, 104, -3, 123456 };
  3163. int elt0, elt1, elt4;
  3164. int *p = &array[0];
  3165. /* Now ‘p’ points at element 0. Fetch it. */
  3166. elt0 = *p;
  3167. ++p;
  3168. /* Now ‘p’ points at element 1. Fetch it. */
  3169. elt1 = *p;
  3170. p += 3;
  3171. /* Now ‘p’ points at element 4 (the last). Fetch it. */
  3172. elt4 = *p;
  3173. printf ("elt0 %d elt1 %d elt4 %d.\n",
  3174. elt0, elt1, elt4);
  3175. /* Prints elt0 45 elt1 29 elt4 123456. */
  3176. }
  3177. Here’s an example where adding a negative integer retracts the
  3178. pointer to an earlier element in the same array.
  3179. void
  3180. decrementing_pointers ()
  3181. {
  3182. int array[5] = { 45, 29, 104, -3, 123456 };
  3183. int elt0, elt3, elt4;
  3184. int *p = &array[4];
  3185. /* Now ‘p’ points at element 4 (the last). Fetch it. */
  3186. elt4 = *p;
  3187. --p;
  3188. /* Now ‘p’ points at element 3. Fetch it. */
  3189. elt3 = *p;
  3190. p -= 3;
  3191. /* Now ‘p’ points at element 0. Fetch it. */
  3192. elt0 = *p;
  3193. printf ("elt0 %d elt3 %d elt4 %d.\n",
  3194. elt0, elt3, elt4);
  3195. /* Prints elt0 45 elt3 -3 elt4 123456. */
  3196. }
  3197. If one pointer value was made by adding an integer to another pointer
  3198. value, it should be possible to subtract the pointer values and recover
  3199. that integer. That works too in C.
  3200. void
  3201. subtract_pointers ()
  3202. {
  3203. int array[5] = { 45, 29, 104, -3, 123456 };
  3204. int *p0, *p3, *p4;
  3205. int *p = &array[4];
  3206. /* Now ‘p’ points at element 4 (the last). Save the value. */
  3207. p4 = p;
  3208. --p;
  3209. /* Now ‘p’ points at element 3. Save the value. */
  3210. p3 = p;
  3211. p -= 3;
  3212. /* Now ‘p’ points at element 0. Save the value. */
  3213. p0 = p;
  3214. printf ("%d, %d, %d, %d\n",
  3215. p4 - p0, p0 - p0, p3 - p0, p0 - p3);
  3216. /* Prints 4, 0, 3, -3. */
  3217. }
  3218. The addition operation does not know where arrays are. All it does
  3219. is add the integer (multiplied by object size) to the value of the
  3220. pointer. When the initial pointer and the result point into a single
  3221. array, the result is well-defined.
  3222. *Warning:* Only experts should do pointer arithmetic involving
  3223. pointers into different memory objects.
  3224. The difference between two pointers has type ‘int’, or ‘long’ if
  3225. necessary (*note Integer Types::). The clean way to declare it is to
  3226. use the typedef name ‘ptrdiff_t’ defined in the file ‘stddef.h’.
  3227. This definition of pointer subtraction is consistent with
  3228. pointer-integer addition, in that ‘(p3 - p1) + p1’ equals ‘p3’, as in
  3229. ordinary algebra.
  3230. In standard C, addition and subtraction are not allowed on ‘void *’,
  3231. since the target type’s size is not defined in that case. Likewise,
  3232. they are not allowed on pointers to function types. However, these
  3233. operations work in GNU C, and the “size of the target type” is taken as
  3234. 1.
  3235. 14.11 Pointers and Arrays
  3236. =========================
  3237. The clean way to refer to an array element is ‘ARRAY[INDEX]’. Another,
  3238. complicated way to do the same job is to get the address of that element
  3239. as a pointer, then dereference it: ‘* (&ARRAY[0] + INDEX)’ (or
  3240. equivalently ‘* (ARRAY + INDEX)’). This first gets a pointer to element
  3241. zero, then increments it with ‘+’ to point to the desired element, then
  3242. gets the value from there.
  3243. That pointer-arithmetic construct is the _definition_ of square
  3244. brackets in C. ‘A[B]’ means, by definition, ‘*(A + B)’. This
  3245. definition uses A and B symmetrically, so one must be a pointer and the
  3246. other an integer; it does not matter which comes first.
  3247. Since indexing with square brackets is defined in terms of addition
  3248. and dereference, that too is symmetrical. Thus, you can write
  3249. ‘3[array]’ and it is equivalent to ‘array[3]’. However, it would be
  3250. foolish to write ‘3[array]’, since it has no advantage and could confuse
  3251. people who read the code.
  3252. It may seem like a discrepancy that the definition ‘*(A + B)’
  3253. requires a pointer, but ‘array[3]’ uses an array value instead. Why is
  3254. this valid? The name of the array, when used by itself as an expression
  3255. (other than in ‘sizeof’), stands for a pointer to the arrays’s zeroth
  3256. element. Thus, ‘array + 3’ converts ‘array’ implicitly to ‘&array[0]’,
  3257. and the result is a pointer to element 3, equivalent to ‘&array[3]’.
  3258. Since square brackets are defined in terms of such addition,
  3259. ‘array[3]’ first converts ‘array’ to a pointer. That’s why it works to
  3260. use an array directly in that construct.
  3261. 14.12 Pointer Arithmetic at Low Level
  3262. =====================================
  3263. The behavior of pointer arithmetic is theoretically defined only when
  3264. the pointer values all point within one object allocated in memory. But
  3265. the addition and subtraction operators can’t tell whether the pointer
  3266. values are all within one object. They don’t know where objects start
  3267. and end. So what do they really do?
  3268. Adding pointer P to integer I treats P as a memory address, which is
  3269. in fact an integer—call it PINT. It treats I as a number of elements of
  3270. the type that P points to. These elements’ sizes add up to ‘I * sizeof
  3271. (*P)’. So the sum, as an integer, is ‘PINT + I * sizeof (*P)’. This
  3272. value is reinterpreted as a pointer like P.
  3273. If the starting pointer value P and the result do not point at parts
  3274. of the same object, the operation is not officially legitimate, and C
  3275. code is not “supposed” to do it. But you can do it anyway, and it gives
  3276. precisely the results described by the procedure above. In some special
  3277. situations it can do something useful, but non-wizards should avoid it.
  3278. Here’s a function to offset a pointer value _as if_ it pointed to an
  3279. object of any given size, by explicitly performing that calculation:
  3280. #include <stdint.h>
  3281. void *
  3282. ptr_add (void *p, int i, int objsize)
  3283. {
  3284. intptr_t p_address = (long) p;
  3285. intptr_t totalsize = i * objsize;
  3286. intptr_t new_address = p_address + totalsize;
  3287. return (void *) new_address;
  3288. }
  3289. This does the same job as ‘P + I’ with the proper pointer type for P.
  3290. It uses the type ‘intptr_t’, which is defined in the header file
  3291. ‘stdint.h’. (In practice, ‘long long’ would always work, but it is
  3292. cleaner to use ‘intptr_t’.)
  3293. 14.13 Pointer Increment and Decrement
  3294. =====================================
  3295. The ‘++’ operator adds 1 to a variable. We have seen it for integers
  3296. (*note Increment/Decrement::), but it works for pointers too. For
  3297. instance, suppose we have a series of positive integers, terminated by a
  3298. zero, and we want to add them all up.
  3299. int
  3300. sum_array_till_0 (int *p)
  3301. {
  3302. int sum = 0;
  3303. for (;;)
  3304. {
  3305. /* Fetch the next integer. */
  3306. int next = *p++;
  3307. /* Exit the loop if it’s 0. */
  3308. if (next == 0)
  3309. break;
  3310. /* Add it into running total. */
  3311. sum += next;
  3312. }
  3313. return sum;
  3314. }
  3315. The statement ‘break;’ will be explained further on (*note break
  3316. Statement::). Used in this way, it immediately exits the surrounding
  3317. ‘for’ statement.
  3318. ‘*p++’ parses as ‘*(p++)’, because a postfix operator always takes
  3319. precedence over a prefix operator. Therefore, it dereferences ‘p’, and
  3320. increments ‘p’ afterwards. Incrementing a variable means adding 1 to
  3321. it, as in ‘p = p + 1’. Since ‘p’ is a pointer, adding 1 to it advances
  3322. it by the width of the datum it points to—in this case, one ‘int’.
  3323. Therefore, each iteration of the loop picks up the next integer from the
  3324. series and puts it into ‘next’.
  3325. This ‘for’-loop has no initialization expression since ‘p’ and ‘sum’
  3326. are already initialized, it has no end-test since the ‘break;’ statement
  3327. will exit it, and needs no expression to advance it since that’s done
  3328. within the loop by incrementing ‘p’ and ‘sum’. Thus, those three
  3329. expressions after ‘for’ are left empty.
  3330. Another way to write this function is by keeping the parameter value
  3331. unchanged and using indexing to access the integers in the table.
  3332. int
  3333. sum_array_till_0_indexing (int *p)
  3334. {
  3335. int i;
  3336. int sum = 0;
  3337. for (i = 0; ; i++)
  3338. {
  3339. /* Fetch the next integer. */
  3340. int next = p[i];
  3341. /* Exit the loop if it’s 0. */
  3342. if (next == 0)
  3343. break;
  3344. /* Add it into running total. */
  3345. sum += next;
  3346. }
  3347. return sum;
  3348. }
  3349. In this program, instead of advancing ‘p’, we advance ‘i’ and add it
  3350. to ‘p’. (Recall that ‘p[i]’ means ‘*(p + i)’.) Either way, it uses the
  3351. same address to get the next integer.
  3352. It makes no difference in this program whether we write ‘i++’ or
  3353. ‘++i’, because the value is not used. All that matters is the effect,
  3354. to increment ‘i’.
  3355. The ‘--’ operator also works on pointers; it can be used to scan
  3356. backwards through an array, like this:
  3357. int
  3358. after_last_nonzero (int *p, int len)
  3359. {
  3360. /* Set up ‘q’ to point just after the last array element. */
  3361. int *q = p + len;
  3362. while (q != p)
  3363. /* Step ‘q’ back until it reaches a nonzero element. */
  3364. if (*--q != 0)
  3365. /* Return the index of the element after that nonzero. */
  3366. return q - p + 1;
  3367. return 0;
  3368. }
  3369. That function returns the length of the nonzero part of the array
  3370. specified by its arguments; that is, the index of the first zero of the
  3371. run of zeros at the end.
  3372. 14.14 Drawbacks of Pointer Arithmetic
  3373. =====================================
  3374. Pointer arithmetic is clean and elegant, but it is also the cause of a
  3375. major security flaw in the C language. Theoretically, it is only valid
  3376. to adjust a pointer within one object allocated as a unit in memory.
  3377. However, if you unintentionally adjust a pointer across the bounds of
  3378. the object and into some other object, the system has no way to detect
  3379. this error.
  3380. A bug which does that can easily result in clobbering part of another
  3381. object. For example, with ‘array[-1]’ you can read or write the
  3382. nonexistent element before the beginning of an array—probably part of
  3383. some other data.
  3384. Combining pointer arithmetic with casts between pointer types, you
  3385. can create a pointer that fails to be properly aligned for its type.
  3386. For example,
  3387. int a[2];
  3388. char *pa = (char *)a;
  3389. int *p = (int *)(pa + 1);
  3390. gives ‘p’ a value pointing to an “integer” that includes part of ‘a[0]’
  3391. and part of ‘a[1]’. Dereferencing that with ‘*p’ can cause a fatal
  3392. ‘SIGSEGV’ signal or it can return the contents of that badly aligned
  3393. ‘int’ (*note Signals::. If it “works,” it may be quite slow. It can
  3394. also cause aliasing confusions (*note Aliasing::).
  3395. *Warning:* Using improperly aligned pointers is risky—don’t do it
  3396. unless it is really necessary.
  3397. 14.15 Pointer-Integer Conversion
  3398. ================================
  3399. On modern computers, an address is simply a number. It occupies the
  3400. same space as some size of integer. In C, you can convert a pointer to
  3401. the appropriate integer types and vice versa, without losing
  3402. information. The appropriate integer types are ‘uintptr_t’ (an unsigned
  3403. type) and ‘intptr_t’ (a signed type). Both are defined in ‘stdint.h’.
  3404. For instance,
  3405. #include <stdint.h>
  3406. #include <stdio.h>
  3407. void
  3408. print_pointer (void *ptr)
  3409. {
  3410. uintptr_t converted = (uintptr_t) ptr;
  3411. printf ("Pointer value is 0x%x\n",
  3412. (unsigned int) converted);
  3413. }
  3414. The specification ‘%x’ in the template (the first argument) for ‘printf’
  3415. means to represent this argument using hexadecimal notation. It’s
  3416. cleaner to use ‘uintptr_t’, since hexadecimal printing treats the number
  3417. as unsigned, but it won’t actually matter: all ‘printf’ gets to see is
  3418. the series of bits in the number.
  3419. *Warning:* Converting pointers to integers is risky—don’t do it
  3420. unless it is really necessary.
  3421. 14.16 Printing Pointers
  3422. =======================
  3423. To print the numeric value of a pointer, use the ‘%p’ specifier. For
  3424. example:
  3425. void
  3426. print_pointer (void *ptr)
  3427. {
  3428. printf ("Pointer value is %p\n", ptr);
  3429. }
  3430. The specification ‘%p’ works with any pointer type. It prints ‘0x’
  3431. followed by the address in hexadecimal, printed as the appropriate
  3432. unsigned integer type.
  3433. 15 Structures
  3434. *************
  3435. A “structure” is a user-defined data type that holds various “fields” of
  3436. data. Each field has a name and a data type specified in the
  3437. structure’s definition.
  3438. Here we define a structure suitable for storing a linked list of
  3439. integers. Each list item will hold one integer, plus a pointer to the
  3440. next item.
  3441. struct intlistlink
  3442. {
  3443. int datum;
  3444. struct intlistlink *next;
  3445. };
  3446. The structure definition has a “type tag” so that the code can refer
  3447. to this structure. The type tag here is ‘intlistlink’. The definition
  3448. refers recursively to the same structure through that tag.
  3449. You can define a structure without a type tag, but then you can’t
  3450. refer to it again. That is useful only in some special contexts, such
  3451. as inside a ‘typedef’ or a ‘union’.
  3452. The contents of the structure are specified by the “field
  3453. declarations” inside the braces. Each field in the structure needs a
  3454. declaration there. The fields in one structure definition must have
  3455. distinct names, but these names do not conflict with any other names in
  3456. the program.
  3457. A field declaration looks just like a variable declaration. You can
  3458. combine field declarations with the same beginning, just as you can
  3459. combine variable declarations.
  3460. This structure has two fields. One, named ‘datum’, has type ‘int’
  3461. and will hold one integer in the list. The other, named ‘next’, is a
  3462. pointer to another ‘struct intlistlink’ which would be the rest of the
  3463. list. In the last list item, it would be ‘NULL’.
  3464. This structure definition is recursive, since the type of the ‘next’
  3465. field refers to the structure type. Such recursion is not a problem; in
  3466. fact, you can use the type ‘struct intlistlink *’ before the definition
  3467. of the type ‘struct intlistlink’ itself. That works because pointers to
  3468. all kinds of structures really look the same at the machine level.
  3469. After defining the structure, you can declare a variable of type
  3470. ‘struct intlistlink’ like this:
  3471. struct intlistlink foo;
  3472. The structure definition itself can serve as the beginning of a
  3473. variable declaration, so you can declare variables immediately after,
  3474. like this:
  3475. struct intlistlink
  3476. {
  3477. int datum;
  3478. struct intlistlink *next;
  3479. } foo;
  3480. But that is ugly. It is almost always clearer to separate the
  3481. definition of the structure from its uses.
  3482. Declaring a structure type inside a block (*note Blocks::) limits the
  3483. scope of the structure type name to that block. That means the
  3484. structure type is recognized only within that block. Declaring it in a
  3485. function parameter list, as here,
  3486. int f (struct foo {int a, b} parm);
  3487. (assuming that ‘struct foo’ is not already defined) limits the scope of
  3488. the structure type ‘struct foo’ to that parameter list; that is
  3489. basically useless, so it triggers a warning.
  3490. Standard C requires at least one field in a structure. GNU C does
  3491. not require this.
  3492. 15.1 Referencing Structure Fields
  3493. =================================
  3494. To make a structure useful, there has to be a way to examine and store
  3495. its fields. The ‘.’ (period) operator does that; its use looks like
  3496. ‘OBJECT.FIELD’.
  3497. Given this structure and variable,
  3498. struct intlistlink
  3499. {
  3500. int datum;
  3501. struct intlistlink *next;
  3502. };
  3503. struct intlistlink foo;
  3504. you can write ‘foo.datum’ and ‘foo.next’ to refer to the two fields in
  3505. the value of ‘foo’. These fields are lvalues, so you can store values
  3506. into them, and read the values out again.
  3507. Most often, structures are dynamically allocated (see the next
  3508. section), and we refer to the objects via pointers. ‘(*p).FIELD’ is
  3509. somewhat cumbersome, so there is an abbreviation: ‘p-<FIELD’. For
  3510. instance, assume the program contains this declaration:
  3511. struct intlistlink *ptr;
  3512. You can write ‘ptr-<datum’ and ‘ptr-<next’ to refer to the two fields in
  3513. the object that ‘ptr’ points to.
  3514. If a unary operator precedes an expression using ‘-<’, the ‘-<’ nests
  3515. inside:
  3516. -ptr-<datum is equivalent to -(ptr-<datum)
  3517. You can intermix ‘-<’ and ‘.’ without parentheses, as shown here:
  3518. struct { double d; struct intlistlink l; } foo;
  3519. ...foo.l.next-<next-<datum...
  3520. 15.2 Dynamic Memory Allocation
  3521. ==============================
  3522. To allocate an object dynamically, call the library function ‘malloc’
  3523. (*note The GNU C Library: (libc)Basic Allocation.). Here is how to
  3524. allocate an object of type ‘struct intlistlink’. To make this code
  3525. work, include the file ‘stdlib.h’, like this:
  3526. #include <stddef.h> /* Defines ‘NULL’. */
  3527. #include <stdlib.h> /* Declares ‘malloc’. */
  3528. ...
  3529. struct intlistlink *
  3530. alloc_intlistlink ()
  3531. {
  3532. struct intlistlink *p;
  3533. p = malloc (sizeof (struct intlistlink));
  3534. if (p == NULL)
  3535. fatal ("Ran out of storage");
  3536. /* Initialize the contents. */
  3537. p-<datum = 0;
  3538. p-<next = NULL;
  3539. return p;
  3540. }
  3541. ‘malloc’ returns ‘void *’, so the assignment to ‘p’ will automatically
  3542. convert it to type ‘struct intlistlink *’. The return value of ‘malloc’
  3543. is always sufficiently aligned (*note Type Alignment::) that it is valid
  3544. for any data type.
  3545. The test for ‘p == NULL’ is necessary because ‘malloc’ returns a null
  3546. pointer if it cannot get any storage. We assume that the program
  3547. defines the function ‘fatal’ to report a fatal error to the user.
  3548. Here’s how to add one more integer to the front of such a list:
  3549. struct intlistlink *my_list = NULL;
  3550. void
  3551. add_to_mylist (int my_int)
  3552. {
  3553. struct intlistlink *p = alloc_intlistlink ();
  3554. p-<datum = my_int;
  3555. p-<next = mylist;
  3556. mylist = p;
  3557. }
  3558. The way to free the objects is by calling ‘free’. Here’s a function
  3559. to free all the links in one of these lists:
  3560. void
  3561. free_intlist (struct intlistlink *p)
  3562. {
  3563. while (p)
  3564. {
  3565. struct intlistlink *q = p;
  3566. p = p-<next;
  3567. free (q);
  3568. }
  3569. }
  3570. We must extract the ‘next’ pointer from the object before freeing it,
  3571. because ‘free’ can clobber the data that was in the object. For the
  3572. same reason, the program must not use the list any more after freeing
  3573. its elements. To make sure it won’t, it is best to clear out the
  3574. variable where the list was stored, like this:
  3575. free_intlist (mylist);
  3576. mylist = NULL;
  3577. 15.3 Field Offset
  3578. =================
  3579. To determine the offset of a given field FIELD in a structure type TYPE,
  3580. use the macro ‘offsetof’, which is defined in the file ‘stddef.h’. It
  3581. is used like this:
  3582. offsetof (TYPE, FIELD)
  3583. Here is an example:
  3584. struct foo
  3585. {
  3586. int element;
  3587. struct foo *next;
  3588. };
  3589. offsetof (struct foo, next)
  3590. /* On most machines that is 4. It may be 8. */
  3591. 15.4 Structure Layout
  3592. =====================
  3593. The rest of this chapter covers advanced topics about structures. If
  3594. you are just learning C, you can skip it.
  3595. The precise layout of a ‘struct’ type is crucial when using it to
  3596. overlay hardware registers, to access data structures in shared memory,
  3597. or to assemble and disassemble packets for network communication. It is
  3598. also important for avoiding memory waste when the program makes many
  3599. objects of that type. However, the layout depends on the target
  3600. platform. Each platform has conventions for structure layout, which
  3601. compilers need to follow.
  3602. Here are the conventions used on most platforms.
  3603. The structure’s fields appear in the structure layout in the order
  3604. they are declared. When possible, consecutive fields occupy consecutive
  3605. bytes within the structure. However, if a field’s type demands more
  3606. alignment than it would get that way, C gives it the alignment it
  3607. requires by leaving a gap after the previous field.
  3608. Once all the fields have been laid out, it is possible to determine
  3609. the structure’s alignment and size. The structure’s alignment is the
  3610. maximum alignment of any of the fields in it. Then the structure’s size
  3611. is rounded up to a multiple of its alignment. That may require leaving
  3612. a gap at the end of the structure.
  3613. Here are some examples, where we assume that ‘char’ has size and
  3614. alignment 1 (always true), and ‘int’ has size and alignment 4 (true on
  3615. most kinds of computers):
  3616. struct foo
  3617. {
  3618. char a, b;
  3619. int c;
  3620. };
  3621. This structure occupies 8 bytes, with an alignment of 4. ‘a’ is at
  3622. offset 0, ‘b’ is at offset 1, and ‘c’ is at offset 4. There is a gap of
  3623. 2 bytes before ‘c’.
  3624. Contrast that with this structure:
  3625. struct foo
  3626. {
  3627. char a;
  3628. int c;
  3629. char b;
  3630. };
  3631. This structure has size 12 and alignment 4. ‘a’ is at offset 0, ‘c’
  3632. is at offset 4, and ‘b’ is at offset 8. There are two gaps: three bytes
  3633. before ‘c’, and three bytes at the end.
  3634. These two structures have the same contents at the C level, but one
  3635. takes 8 bytes and the other takes 12 bytes due to the ordering of the
  3636. fields. A reliable way to avoid this sort of wastage is to order the
  3637. fields by size, biggest fields first.
  3638. 15.5 Packed Structures
  3639. ======================
  3640. In GNU C you can force a structure to be laid out with no gaps by adding
  3641. ‘__attribute__((packed))’ after ‘struct’ (or at the end of the structure
  3642. type declaration). Here’s an example:
  3643. struct __attribute__((packed)) foo
  3644. {
  3645. char a;
  3646. int c;
  3647. char b;
  3648. };
  3649. Without ‘__attribute__((packed))’, this structure occupies 12 bytes
  3650. (as described in the previous section), assuming 4-byte alignment for
  3651. ‘int’. With ‘__attribute__((packed))’, it is only 6 bytes long—the sum
  3652. of the lengths of its fields.
  3653. Use of ‘__attribute__((packed))’ often results in fields that don’t
  3654. have the normal alignment for their types. Taking the address of such a
  3655. field can result in an invalid pointer because of its improper
  3656. alignment. Dereferencing such a pointer can cause a ‘SIGSEGV’ signal on
  3657. a machine that doesn’t, in general, allow unaligned pointers.
  3658. *Note Attributes::.
  3659. 15.6 Bit Fields
  3660. ===============
  3661. A structure field declaration with an integer type can specify the
  3662. number of bits the field should occupy. We call that a “bit field”.
  3663. These are useful because consecutive bit fields are packed into a larger
  3664. storage unit. For instance,
  3665. unsigned char opcode: 4;
  3666. specifies that this field takes just 4 bits. Since it is unsigned, its
  3667. possible values range from 0 to 15. A signed field with 4 bits, such as
  3668. this,
  3669. signed char small: 4;
  3670. can hold values from -8 to 7.
  3671. You can subdivide a single byte into those two parts by writing
  3672. unsigned char opcode: 4;
  3673. signed char small: 4;
  3674. in the structure. With bit fields, these two numbers fit into a single
  3675. ‘char’.
  3676. Here’s how to declare a one-bit field that can hold either 0 or 1:
  3677. unsigned char special_flag: 1;
  3678. You can also use the ‘bool’ type for bit fields:
  3679. bool special_flag: 1;
  3680. Except when using ‘bool’ (which is always unsigned, *note Boolean
  3681. Type::), always specify ‘signed’ or ‘unsigned’ for a bit field. There
  3682. is a default, if that’s not specified: the bit field is signed if plain
  3683. ‘char’ is signed, except that the option ‘-funsigned-bitfields’ forces
  3684. unsigned as the default. But it is cleaner not to depend on this
  3685. default.
  3686. Bit fields are special in that you cannot take their address with
  3687. ‘&’. They are not stored with the size and alignment appropriate for
  3688. the specified type, so they cannot be addressed through pointers to that
  3689. type.
  3690. 15.7 Bit Field Packing
  3691. ======================
  3692. Programs to communicate with low-level hardware interfaces need to
  3693. define bit fields laid out to match the hardware data. This section
  3694. explains how to do that.
  3695. Consecutive bit fields are packed together, but each bit field must
  3696. fit within a single object of its specified type. In this example,
  3697. unsigned short a : 3, b : 3, c : 3, d : 3, e : 3;
  3698. all five fields fit consecutively into one two-byte ‘short’. They need
  3699. 15 bits, and one ‘short’ provides 16. By contrast,
  3700. unsigned char a : 3, b : 3, c : 3, d : 3, e : 3;
  3701. needs three bytes. It fits ‘a’ and ‘b’ into one ‘char’, but ‘c’ won’t
  3702. fit in that ‘char’ (they would add up to 9 bits). So ‘c’ and ‘d’ go
  3703. into a second ‘char’, leaving a gap of two bits between ‘b’ and ‘c’.
  3704. Then ‘e’ needs a third ‘char’. By contrast,
  3705. unsigned char a : 3, b : 3;
  3706. unsigned int c : 3;
  3707. unsigned char d : 3, e : 3;
  3708. needs only two bytes: the type ‘unsigned int’ allows ‘c’ to straddle
  3709. bytes that are in the same word.
  3710. You can leave a gap of a specified number of bits by defining a
  3711. nameless bit field. This looks like ‘TYPE : NBITS;’. It is allocated
  3712. space in the structure just as a named bit field would be allocated.
  3713. You can force the following bit field to advance to the following
  3714. aligned memory object with ‘TYPE : 0;’.
  3715. Both of these constructs can syntactically share TYPE with ordinary
  3716. bit fields. This example illustrates both:
  3717. unsigned int a : 5, : 3, b : 5, : 0, c : 5, : 3, d : 5;
  3718. It puts ‘a’ and ‘b’ into one ‘int’, with a 3-bit gap between them. Then
  3719. ‘: 0’ advances to the next ‘int’, so ‘c’ and ‘d’ fit into that one.
  3720. These rules for packing bit fields apply to most target platforms,
  3721. including all the usual real computers. A few embedded controllers have
  3722. special layout rules.
  3723. 15.8 ‘const’ Fields
  3724. ===================
  3725. A structure field declared ‘const’ cannot be assigned to (*note
  3726. const::). For instance, let’s define this modified version of ‘struct
  3727. intlistlink’:
  3728. struct intlistlink_ro /* “ro” for read-only. */
  3729. {
  3730. const int datum;
  3731. struct intlistlink *next;
  3732. };
  3733. This structure can be used to prevent part of the code from modifying
  3734. the ‘datum’ field:
  3735. /* ‘p’ has type ‘struct intlistlink *’.
  3736. Convert it to ‘struct intlistlink_ro *’. */
  3737. struct intlistlink_ro *q
  3738. = (struct intlistlink_ro *) p;
  3739. q-<datum = 5; /* Error! */
  3740. p-<datum = 5; /* Valid since ‘*p’ is
  3741. not a ‘struct intlistlink_ro’. */
  3742. A ‘const’ field can get a value in two ways: by initialization of the
  3743. whole structure, and by making a pointer-to-structure point to an object
  3744. in which that field already has a value.
  3745. Any ‘const’ field in a structure type makes assignment impossible for
  3746. structures of that type (*note Structure Assignment::). That is because
  3747. structure assignment works by assigning the structure’s fields, one by
  3748. one.
  3749. 15.9 Arrays of Length Zero
  3750. ==========================
  3751. GNU C allows zero-length arrays. They are useful as the last element of
  3752. a structure that is really a header for a variable-length object.
  3753. Here’s an example, where we construct a variable-size structure to hold
  3754. a line which is ‘this_length’ characters long:
  3755. struct line {
  3756. int length;
  3757. char contents[0];
  3758. };
  3759. struct line *thisline
  3760. = ((struct line *)
  3761. malloc (sizeof (struct line)
  3762. + this_length));
  3763. thisline-<length = this_length;
  3764. In ISO C90, we would have to give ‘contents’ a length of 1, which
  3765. means either wasting space or complicating the argument to ‘malloc’.
  3766. 15.10 Flexible Array Fields
  3767. ===========================
  3768. The C99 standard adopted a more complex equivalent of zero-length array
  3769. fields. It’s called a “flexible array”, and it’s indicated by omitting
  3770. the length, like this:
  3771. struct line
  3772. {
  3773. int length;
  3774. char contents[];
  3775. };
  3776. The flexible array has to be the last field in the structure, and
  3777. there must be other fields before it.
  3778. Under the C standard, a structure with a flexible array can’t be part
  3779. of another structure, and can’t be an element of an array.
  3780. GNU C allows static initialization of flexible array fields. The
  3781. effect is to “make the array long enough” for the initializer.
  3782. struct f1 { int x; int y[]; } f1
  3783. = { 1, { 2, 3, 4 } };
  3784. This defines a structure variable named ‘f1’ whose type is ‘struct f1’.
  3785. In C, a variable name or function name never conflicts with a structure
  3786. type tag.
  3787. Omitting the flexible array field’s size lets the initializer
  3788. determine it. This is allowed only when the flexible array is defined
  3789. in the outermost structure and you declare a variable of that structure
  3790. type. For example:
  3791. struct foo { int x; int y[]; };
  3792. struct bar { struct foo z; };
  3793. struct foo a = { 1, { 2, 3, 4 } }; // Valid.
  3794. struct bar b = { { 1, { 2, 3, 4 } } }; // Invalid.
  3795. struct bar c = { { 1, { } } }; // Valid.
  3796. struct foo d[1] = { { 1 { 2, 3, 4 } } }; // Invalid.
  3797. 15.11 Overlaying Different Structures
  3798. =====================================
  3799. Be careful about using different structure types to refer to the same
  3800. memory within one function, because GNU C can optimize code assuming it
  3801. never does that. *Note Aliasing::. Here’s an example of the kind of
  3802. aliasing that can cause the problem:
  3803. struct a { int size; char *data; };
  3804. struct b { int size; char *data; };
  3805. struct a foo;
  3806. struct b *q = (struct b *) &foo;
  3807. Here ‘q’ points to the same memory that the variable ‘foo’ occupies,
  3808. but they have two different types. The two types ‘struct a’ and ‘struct
  3809. b’ are defined alike, but they are not the same type. Interspersing
  3810. references using the two types, like this,
  3811. p-<size = 0;
  3812. q-<size = 1;
  3813. x = p-<size;
  3814. allows GNU C to assume that ‘p-<size’ is still zero when it is copied
  3815. into ‘x’. The compiler “knows” that ‘q’ points to a ‘struct b’ and this
  3816. cannot overlap with a ‘struct a’.
  3817. Other compilers might also do this optimization. The ISO C standard
  3818. considers such code erroneous, precisely so that this optimization will
  3819. be valid.
  3820. 15.12 Structure Assignment
  3821. ==========================
  3822. Assignment operating on a structure type copies the structure. The left
  3823. and right operands must have the same type. Here is an example:
  3824. #include <stddef.h> /* Defines ‘NULL’. */
  3825. #include <stdlib.h> /* Declares ‘malloc’. */
  3826. ...
  3827. struct point { double x, y; };
  3828. struct point *
  3829. copy_point (struct point point)
  3830. {
  3831. struct point *p
  3832. = (struct point *) malloc (sizeof (struct point));
  3833. if (p == NULL)
  3834. fatal ("Out of memory");
  3835. *p = point;
  3836. return p;
  3837. }
  3838. Notionally, assignment on a structure type works by copying each of
  3839. the fields. Thus, if any of the fields has the ‘const’ qualifier, that
  3840. structure type does not allow assignment:
  3841. struct point { const double x, y; };
  3842. struct point a, b;
  3843. a = b; /* Error! */
  3844. *Note Assignment Expressions::.
  3845. 15.13 Unions
  3846. ============
  3847. A “union type” defines alternative ways of looking at the same piece of
  3848. memory. Each alternative view is defined with a data type, and
  3849. identified by a name. A union definition looks like this:
  3850. union NAME
  3851. {
  3852. ALTERNATIVE DECLARATIONS...
  3853. };
  3854. Each alternative declaration looks like a structure field
  3855. declaration, except that it can’t be a bit field. For instance,
  3856. union number
  3857. {
  3858. long int integer;
  3859. double float;
  3860. }
  3861. lets you store either an integer (type ‘long int’) or a floating point
  3862. number (type ‘double’) in the same place in memory. The length and
  3863. alignment of the union type are the maximum of all the alternatives—they
  3864. do not have to be the same. In this union example, ‘double’ probably
  3865. takes more space than ‘long int’, but that doesn’t cause a problem in
  3866. programs that use the union in the normal way.
  3867. The members don’t have to be different in data type. Sometimes each
  3868. member pertains to a way the data will be used. For instance,
  3869. union datum
  3870. {
  3871. double latitude;
  3872. double longitude;
  3873. double height;
  3874. double weight;
  3875. int continent;
  3876. }
  3877. This union holds one of several kinds of data; most kinds are
  3878. floating points, but the value can also be a code for a continent which
  3879. is an integer. You _could_ use one member of type ‘double’ to access
  3880. all the values which have that type, but the different member names will
  3881. make the program clearer.
  3882. The alignment of a union type is the maximum of the alignments of the
  3883. alternatives. The size of the union type is the maximum of the sizes of
  3884. the alternatives, rounded up to a multiple of the alignment (because
  3885. every type’s size must be a multiple of its alignment).
  3886. All the union alternatives start at the address of the union itself.
  3887. If an alternative is shorter than the union as a whole, it occupies the
  3888. first part of the union’s storage, leaving the last part unused _for
  3889. that alternative_.
  3890. *Warning:* if the code stores data using one union alternative and
  3891. accesses it with another, the results depend on the kind of computer in
  3892. use. Only wizards should try to do this. However, when you need to do
  3893. this, a union is a clean way to do it.
  3894. Assignment works on any union type by copying the entire value.
  3895. 15.14 Packing With Unions
  3896. =========================
  3897. Sometimes we design a union with the intention of packing various kinds
  3898. of objects into a certain amount of memory space. For example.
  3899. union bytes8
  3900. {
  3901. long long big_int_elt;
  3902. double double_elt;
  3903. struct { int first, second; } two_ints;
  3904. struct { void *first, *second; } two_ptrs;
  3905. };
  3906. union bytes8 *p;
  3907. This union makes it possible to look at 8 bytes of data that ‘p’
  3908. points to as a single 8-byte integer (‘p-<big_int_elt’), as a single
  3909. floating-point number (‘p-<double_elt’), as a pair of integers
  3910. (‘p-<two_ints.first’ and ‘p-<two_ints.second’), or as a pair of pointers
  3911. (‘p-<two_ptrs.first’ and ‘p-<two_ptrs.second’).
  3912. To pack storage with such a union makes assumptions about the sizes
  3913. of all the types involved. This particular union was written expecting
  3914. a pointer to have the same size as ‘int’. On a machine where one
  3915. pointer takes 8 bytes, the code using this union probably won’t work as
  3916. expected. The union, as such, will function correctly—if you store two
  3917. values through ‘two_ints’ and extract them through ‘two_ints’, you will
  3918. get the same integers back—but the part of the program that expects the
  3919. union to be 8 bytes long could malfunction, or at least use too much
  3920. space.
  3921. The above example shows one case where a ‘struct’ type with no tag
  3922. can be useful. Another way to get effectively the same result is with
  3923. arrays as members of the union:
  3924. union eight_bytes
  3925. {
  3926. long long big_int_elt;
  3927. double double_elt;
  3928. int two_ints[2];
  3929. void *two_ptrs[2];
  3930. };
  3931. 15.15 Cast to a Union Type
  3932. ==========================
  3933. In GNU C, you can explicitly cast any of the alternative types to the
  3934. union type; for instance,
  3935. (union eight_bytes) (long long) 5
  3936. makes a value of type ‘union eight_bytes’ which gets its contents
  3937. through the alternative named ‘big_int_elt’.
  3938. The value being cast must exactly match the type of the alternative,
  3939. so this is not valid:
  3940. (union eight_bytes) 5 /* Error! 5 is ‘int’. */
  3941. A cast to union type looks like any other cast, except that the type
  3942. specified is a union type. You can specify the type either with ‘union
  3943. TAG’ or with a typedef name (*note Defining Typedef Names::).
  3944. Using the cast as the right-hand side of an assignment to a variable
  3945. of union type is equivalent to storing in an alternative of the union:
  3946. union foo u;
  3947. u = (union foo) x means u.i = x
  3948. u = (union foo) y means u.d = y
  3949. You can also use the union cast as a function argument:
  3950. void hack (union foo);
  3951. ...
  3952. hack ((union foo) x);
  3953. 15.16 Structure Constructors
  3954. ============================
  3955. You can construct a structure value by writing its type in parentheses,
  3956. followed by an initializer that would be valid in a declaration for that
  3957. type. For instance, given this declaration,
  3958. struct foo {int a; char b[2];} structure;
  3959. you can create a ‘struct foo’ value as follows:
  3960. ((struct foo) {x + y, 'a', 0})
  3961. This specifies ‘x + y’ for field ‘a’, the character ‘a’ for field ‘b’’s
  3962. element 0, and the null character for field ‘b’’s element 1.
  3963. The parentheses around that constructor are to necessary, but we
  3964. recommend writing them to make the nesting of the containing expression
  3965. clearer.
  3966. You can also show the nesting of the two by writing it like this:
  3967. ((struct foo) {x + y, {'a', 0} })
  3968. Each of those is equivalent to writing the following statement
  3969. expression (*note Statement Exprs::):
  3970. ({
  3971. struct foo temp = {x + y, 'a', 0};
  3972. temp;
  3973. })
  3974. You can also create a union value this way, but it is not especially
  3975. useful since that is equivalent to doing a cast:
  3976. ((union whosis) {VALUE})
  3977. is equivalent to
  3978. ((union whosis) (VALUE))
  3979. 15.17 Unnamed Types as Fields
  3980. =============================
  3981. A structure or a union can contain, as fields, unnamed structures and
  3982. unions. Here’s an example:
  3983. struct
  3984. {
  3985. int a;
  3986. union
  3987. {
  3988. int b;
  3989. float c;
  3990. };
  3991. int d;
  3992. } foo;
  3993. You can access the fields of the unnamed union within ‘foo’ as if they
  3994. were individual fields at the same level as the union definition:
  3995. foo.a = 42;
  3996. foo.b = 47;
  3997. foo.c = 5.25; // Overwrites the value in ‘foo.b’.
  3998. foo.d = 314;
  3999. Avoid using field names that could cause ambiguity. For example,
  4000. with this definition:
  4001. struct
  4002. {
  4003. int a;
  4004. struct
  4005. {
  4006. int a;
  4007. float b;
  4008. };
  4009. } foo;
  4010. it is impossible to tell what ‘foo.a’ refers to. GNU C reports an error
  4011. when a definition is ambiguous in this way.
  4012. 15.18 Incomplete Types
  4013. ======================
  4014. A type that has not been fully defined is called an “incomplete type”.
  4015. Structure and union types are incomplete when the code makes a forward
  4016. reference, such as ‘struct foo’, before defining the type. An array
  4017. type is incomplete when its length is unspecified.
  4018. You can’t use an incomplete type to declare a variable or field, or
  4019. use it for a function parameter or return type. The operators ‘sizeof’
  4020. and ‘_Alignof’ give errors when used on an incomplete type.
  4021. However, you can define a pointer to an incomplete type, and declare
  4022. a variable or field with such a pointer type. In general, you can do
  4023. everything with such pointers except dereference them. For example:
  4024. extern void bar (struct mysterious_value *);
  4025. void
  4026. foo (struct mysterious_value *arg)
  4027. {
  4028. bar (arg);
  4029. }
  4030. ...
  4031. {
  4032. struct mysterious_value *p, **q;
  4033. p = *q;
  4034. foo (p);
  4035. }
  4036. These examples are valid because the code doesn’t try to understand what
  4037. ‘p’ points to; it just passes the pointer around. (Presumably ‘bar’ is
  4038. defined in some other file that really does have a definition for
  4039. ‘struct mysterious_value’.) However, dereferencing the pointer would
  4040. get an error; that requires a definition for the structure type.
  4041. 15.19 Intertwined Incomplete Types
  4042. ==================================
  4043. When several structure types contain pointers to each other, you can
  4044. define the types in any order because pointers to types that come later
  4045. are incomplete types. Thus, Here is an example.
  4046. /* An employee record points to a group. */
  4047. struct employee
  4048. {
  4049. char *name;
  4050. ...
  4051. struct group *group; /* incomplete type. */
  4052. ...
  4053. };
  4054. /* An employee list points to employees. */
  4055. struct employee_list
  4056. {
  4057. struct employee *this_one;
  4058. struct employee_list *next; /* incomplete type. */
  4059. ...
  4060. };
  4061. /* A group points to one employee_list. */
  4062. struct group
  4063. {
  4064. char *name;
  4065. ...
  4066. struct employee_list *employees;
  4067. ...
  4068. };
  4069. 15.20 Type Tags
  4070. ===============
  4071. The name that follows ‘struct’ (*note Structures::), ‘union’ (*note
  4072. Unions::, or ‘enum’ (*note Enumeration Types::) is called a “type tag”.
  4073. In C, a type tag never conflicts with a variable name or function name;
  4074. the type tags have a separate “name space”. Thus, there is no name
  4075. conflict in this code:
  4076. struct pair { int a, b; };
  4077. int pair = 1;
  4078. nor in this one:
  4079. struct pair { int a, b; } pair;
  4080. where ‘pair’ is both a structure type tag and a variable name.
  4081. However, ‘struct’, ‘union’, and ‘enum’ share the same name space of
  4082. tags, so this is a conflict:
  4083. struct pair { int a, b; };
  4084. enum pair { c, d };
  4085. and so is this:
  4086. struct pair { int a, b; };
  4087. struct pair { int c, d; };
  4088. When the code defines a type tag inside a block, the tag’s scope is
  4089. limited to that block (as for local variables). Two definitions for one
  4090. type tag do not conflict if they are in different scopes; rather, each
  4091. is valid in its scope. For example,
  4092. struct pair { int a, b; };
  4093. void
  4094. pair_up_doubles (int len, double array[])
  4095. {
  4096. struct pair { double a, b; };
  4097. ...
  4098. }
  4099. has two definitions for ‘struct pair’ which do not conflict. The one
  4100. inside the function applies only within the definition of
  4101. ‘pair_up_doubles’. Within its scope, that definition “shadows” the
  4102. outer definition.
  4103. If ‘struct pair’ appears inside the function body, before the inner
  4104. definition, it refers to the outer definition—the only one that has been
  4105. seen at that point. Thus, in this code,
  4106. struct pair { int a, b; };
  4107. void
  4108. pair_up_doubles (int len, double array[])
  4109. {
  4110. struct two_pairs { struct pair *p, *q; };
  4111. struct pair { double a, b; };
  4112. ...
  4113. }
  4114. the structure ‘two_pairs’ has pointers to the outer definition of
  4115. ‘struct pair’, which is probably not desirable.
  4116. To prevent that, you can write ‘struct pair;’ inside the function
  4117. body as a variable declaration with no variables. This is a “forward
  4118. declaration” of the type tag ‘pair’: it makes the type tag local to the
  4119. current block, with the details of the type to come later. Here’s an
  4120. example:
  4121. void
  4122. pair_up_doubles (int len, double array[])
  4123. {
  4124. /* Forward declaration for ‘pair’. */
  4125. struct pair;
  4126. struct two_pairs { struct pair *p, *q; };
  4127. /* Give the details. */
  4128. struct pair { double a, b; };
  4129. ...
  4130. }
  4131. However, the cleanest practice is to avoid shadowing type tags.
  4132. 16 Arrays
  4133. *********
  4134. An “array” is a data object that holds a series of “elements”, all of
  4135. the same data type. Each element is identified by its numeric INDEX
  4136. within the array.
  4137. We presented arrays of numbers in the sample programs early in this
  4138. manual (*note Array Example::). However, arrays can have elements of
  4139. any data type, including pointers, structures, unions, and other arrays.
  4140. If you know another programming language, you may suppose that you
  4141. know all about arrays, but C arrays have special quirks, so in this
  4142. chapter we collect all the information about arrays in C.
  4143. The elements of a C array are allocated consecutively in memory, with
  4144. no gaps between them. Each element is aligned as required for its data
  4145. type (*note Type Alignment::).
  4146. 16.1 Accessing Array Elements
  4147. =============================
  4148. If the variable ‘a’ is an array, the Nth element of ‘a’ is ‘a[N]’. You
  4149. can use that expression to access an element’s value or to assign to it:
  4150. x = a[5];
  4151. a[6] = 1;
  4152. Since the variable ‘a’ is an lvalue, ‘a[N]’ is also an lvalue.
  4153. The lowest valid index in an array is 0, _not_ 1, and the highest
  4154. valid index is one less than the number of elements.
  4155. The C language does not check whether array indices are in bounds, so
  4156. if the code uses an out-of-range index, it will access memory outside
  4157. the array.
  4158. *Warning:* Using only valid index values in C is the programmer’s
  4159. responsibility.
  4160. Array indexing in C is not a primitive operation: it is defined in
  4161. terms of pointer arithmetic and dereferencing. Now that we know _what_
  4162. ‘a[i]’ does, we can ask _how_ ‘a[i]’ does its job.
  4163. In C, ‘X[Y]’ is an abbreviation for ‘*(X+Y)’. Thus, ‘a[i]’ really
  4164. means ‘*(a+i)’. *Note Pointers and Arrays::.
  4165. When an expression with array type (such as ‘a’) appears as part of a
  4166. larger C expression, it is converted automatically to a pointer to
  4167. element zero of that array. For instance, ‘a’ in an expression is
  4168. equivalent to ‘&a[0]’. Thus, ‘*(a+i)’ is computed as ‘*(&a[0]+i)’.
  4169. Now we can analyze how that expression gives us the desired element
  4170. of the array. It makes a pointer to element 0 of ‘a’, advances it by
  4171. the value of ‘i’, and dereferences that pointer.
  4172. Another equivalent way to write the expression is ‘(&a[0])[i]’.
  4173. 16.2 Declaring an Array
  4174. =======================
  4175. To make an array declaration, write ‘[LENGTH]’ after the name being
  4176. declared. This construct is valid in the declaration of a variable, a
  4177. function parameter, a function value type (the value can’t be an array,
  4178. but it can be a pointer to one), a structure field, or a union
  4179. alternative.
  4180. The surrounding declaration specifies the element type of the array;
  4181. that can be any type of data, but not ‘void’ or a function type. For
  4182. instance,
  4183. double a[5];
  4184. declares ‘a’ as an array of 5 ‘double’s.
  4185. struct foo bstruct[length];
  4186. declares ‘bstruct’ as an array of ‘length’ objects of type ‘struct foo’.
  4187. A variable array size like this is allowed when the array is not
  4188. file-scope.
  4189. Other declaration constructs can nest within the array declaration
  4190. construct. For instance:
  4191. struct foo *b[length];
  4192. declares ‘b’ as an array of ‘length’ pointers to ‘struct foo’. This
  4193. shows that the length need not be a constant (*note Arrays of Variable
  4194. Length::).
  4195. double (*c)[5];
  4196. declares ‘c’ as a pointer to an array of 5 ‘double’s, and
  4197. char *(*f (int))[5];
  4198. declares ‘f’ as a function taking an ‘int’ argument and returning a
  4199. pointer to an array of 5 strings (pointers to ‘char’s).
  4200. double aa[5][10];
  4201. declares ‘aa’ as an array of 5 elements, each of which is an array of 10
  4202. ‘double’s. This shows how to declare a multidimensional array in C
  4203. (*note Multidimensional Arrays::).
  4204. All these declarations specify the array’s length, which is needed in
  4205. these cases in order to allocate storage for the array.
  4206. 16.3 Strings
  4207. ============
  4208. A string in C is a sequence of elements of type ‘char’, terminated with
  4209. the null character, the character with code zero.
  4210. Programs often need to use strings with specific, fixed contents. To
  4211. write one in a C program, use a “string constant” such as ‘"Take me to
  4212. your leader!"’. The data type of a string constant is ‘char *’. For
  4213. the full syntactic details of writing string constants, *note String
  4214. Constants::.
  4215. To declare a place to store a non-constant string, declare an array
  4216. of ‘char’. Keep in mind that it must include one extra ‘char’ for the
  4217. terminating null. For instance,
  4218. char text[] = { 'H', 'e', 'l', 'l', 'o', 0 };
  4219. declares an array named ‘text’ with six elements—five letters and the
  4220. terminating null character. An equivalent way to get the same result is
  4221. this,
  4222. char text[] = "Hello";
  4223. which copies the elements of the string constant, including _its_
  4224. terminating null character.
  4225. char message[200];
  4226. declares an array long enough to hold a string of 199 ASCII characters
  4227. plus the terminating null character.
  4228. When you store a string into ‘message’ be sure to check or prove that
  4229. the length does not exceed its size. For example,
  4230. void
  4231. set_message (char *text)
  4232. {
  4233. int i;
  4234. for (i = 0; i < sizeof (message); i++)
  4235. {
  4236. message[i] = text[i];
  4237. if (text[i] == 0)
  4238. return;
  4239. }
  4240. fatal_error ("Message is too long for `message');
  4241. }
  4242. It’s easy to do this with the standard library function ‘strncpy’,
  4243. which fills out the whole destination array (up to a specified length)
  4244. with null characters. Thus, if the last character of the destination is
  4245. not null, the string did not fit. Many system libraries, including the
  4246. GNU C library, hand-optimize ‘strncpy’ to run faster than an explicit
  4247. ‘for’-loop.
  4248. Here’s what the code looks like:
  4249. void
  4250. set_message (char *text)
  4251. {
  4252. strncpy (message, text, sizeof (message));
  4253. if (message[sizeof (message) - 1] != 0)
  4254. fatal_error ("Message is too long for `message');
  4255. }
  4256. *Note The GNU C Library: (libc)String and Array Utilities, for more
  4257. information about the standard library functions for operating on
  4258. strings.
  4259. You can avoid putting a fixed length limit on strings you construct
  4260. or operate on by allocating the space for them dynamically. *Note
  4261. Dynamic Memory Allocation::.
  4262. 16.4 Array Type Designators
  4263. ===========================
  4264. Every C type has a type designator, which you make by deleting the
  4265. variable name and the semicolon from a declaration (*note Type
  4266. Designators::). The designators for array types follow this rule, but
  4267. they may appear surprising.
  4268. type int a[5]; designator int [5]
  4269. type double a[5][3]; designator double [5][3]
  4270. type struct foo *a[5]; designator struct foo *[5]
  4271. 16.5 Incomplete Array Types
  4272. ===========================
  4273. An array is equivalent, for most purposes, to a pointer to its zeroth
  4274. element. When that is true, the length of the array is irrelevant. The
  4275. length needs to be known only for allocating space for the array, or for
  4276. ‘sizeof’ and ‘typeof’ (*note Auto Type::). Thus, in some contexts C
  4277. allows
  4278. • An ‘extern’ declaration says how to refer to a variable allocated
  4279. elsewhere. It does not need to allocate space for the variable, so
  4280. if it is an array, you can omit the length. For example,
  4281. extern int foo[];
  4282. • When declaring a function parameter as an array, the argument value
  4283. passed to the function is really a pointer to the array’s zeroth
  4284. element. This value does not say how long the array really is,
  4285. there is no need to declare it. For example,
  4286. int
  4287. func (int foo[])
  4288. These declarations are examples of “incomplete” array types, types
  4289. that are not fully specified. The incompleteness makes no difference
  4290. for accessing elements of the array, but it matters for some other
  4291. things. For instance, ‘sizeof’ is not allowed on an incomplete type.
  4292. With multidimensional arrays, only the first dimension can be
  4293. omitted:
  4294. extern struct chesspiece *funnyboard foo[][8];
  4295. In other words, the code doesn’t have to say how many rows there are,
  4296. but it must state how big each row is.
  4297. 16.6 Limitations of C Arrays
  4298. ============================
  4299. Arrays have quirks in C because they are not “first-class objects”:
  4300. there is no way in C to operate on an array as a unit.
  4301. The other composite objects in C, structures and unions, are
  4302. first-class objects: a C program can copy a structure or union value in
  4303. an assignment, or pass one as an argument to a function, or make a
  4304. function return one. You can’t do those things with an array in C.
  4305. That is because a value you can operate on never has an array type.
  4306. An expression in C can have an array type, but that doesn’t produce
  4307. the array as a value. Instead it is converted automatically to a
  4308. pointer to the array’s element at index zero. The code can operate on
  4309. the pointer, and through that on individual elements of the array, but
  4310. it can’t get and operate on the array as a unit.
  4311. There are three exceptions to this conversion rule, but none of them
  4312. offers a way to operate on the array as a whole.
  4313. First, ‘&’ applied to an expression with array type gives you the
  4314. address of the array, as an array type. However, you can’t operate on
  4315. the whole array that way—if you apply ‘*’ to get the array back, that
  4316. expression converts, as usual, to a pointer to its zeroth element.
  4317. Second, the operators ‘sizeof’, ‘_Alignof’, and ‘typeof’ do not
  4318. convert the array to a pointer; they leave it as an array. But they
  4319. don’t operate on the array’s data—they only give information about its
  4320. type.
  4321. Third, a string constant used as an initializer for an array is not
  4322. converted to a pointer—rather, the declaration copies the _contents_ of
  4323. that string in that one special case.
  4324. You _can_ copy the contents of an array, just not with an assignment
  4325. operator. You can do it by calling the library function ‘memcpy’ or
  4326. ‘memmove’ (*note The GNU C Library: (libc)Copying and Concatenation.).
  4327. Also, when a structure contains just an array, you can copy that
  4328. structure.
  4329. An array itself is an lvalue if it is a declared variable, or part of
  4330. a structure or union that is an lvalue. When you construct an array
  4331. from elements (*note Constructing Array Values::), that array is not an
  4332. lvalue.
  4333. 16.7 Multidimensional Arrays
  4334. ============================
  4335. Strictly speaking, all arrays in C are unidimensional. However, you can
  4336. create an array of arrays, which is more or less equivalent to a
  4337. multidimensional array. For example,
  4338. struct chesspiece *board[8][8];
  4339. declares an array of 8 arrays of 8 pointers to ‘struct chesspiece’.
  4340. This data type could represent the state of a chess game. To access one
  4341. square’s contents requires two array index operations, one for each
  4342. dimension. For instance, you can write ‘board[row][column]’, assuming
  4343. ‘row’ and ‘column’ are variables with integer values in the proper
  4344. range.
  4345. How does C understand ‘board[row][column]’? First of all, ‘board’ is
  4346. converted automatically to a pointer to the zeroth element (at index
  4347. zero) of ‘board’. Adding ‘row’ to that makes it point to the desired
  4348. element. Thus, ‘board[row]’’s value is an element of ‘board’—an array
  4349. of 8 pointers.
  4350. However, as an expression with array type, it is converted
  4351. automatically to a pointer to the array’s zeroth element. The second
  4352. array index operation, ‘[column]’, accesses the chosen element from that
  4353. array.
  4354. As this shows, pointer-to-array types are meaningful in C. You can
  4355. declare a variable that points to a row in a chess board like this:
  4356. struct chesspiece *(*rowptr)[8];
  4357. This points to an array of 8 pointers to ‘struct chesspiece’. You can
  4358. assign to it as follows:
  4359. rowptr = &board[5];
  4360. The dimensions don’t have to be equal in length. Here we declare
  4361. ‘statepop’ as an array to hold the population of each state in the
  4362. United States for each year since 1900:
  4363. #define NSTATES 50
  4364. {
  4365. int nyears = current_year - 1900 + 1;
  4366. int statepop[NSTATES][nyears];
  4367. ...
  4368. }
  4369. The variable ‘statepop’ is an array of ‘NSTATES’ subarrays, each
  4370. indexed by the year (counting from 1900). Thus, to get the element for
  4371. a particular state and year, we must subscript it first by the number
  4372. that indicates the state, and second by the index for the year:
  4373. statepop[state][year - 1900]
  4374. The subarrays within the multidimensional array are allocated
  4375. consecutively in memory, and within each subarray, its elements are
  4376. allocated consecutively in memory. The most efficient way to process
  4377. all the elements in the array is to scan the last subscript in the
  4378. innermost loop. This means consecutive accesses go to consecutive
  4379. memory locations, which optimizes use of the processor’s memory cache.
  4380. For example:
  4381. int total = 0;
  4382. float average;
  4383. for (int state = 0; state < NSTATES, ++state)
  4384. {
  4385. for (int year = 0; year < nyears; ++year)
  4386. {
  4387. total += statepop[state][year];
  4388. }
  4389. }
  4390. average = total / nyears;
  4391. C’s layout for multidimensional arrays is different from Fortran’s
  4392. layout. In Fortran, a multidimensional array is not an array of arrays;
  4393. rather, multidimensional arrays are a primitive feature, and it is the
  4394. first index that varies most rapidly between consecutive memory
  4395. locations. Thus, the memory layout of a 50x114 array in C matches that
  4396. of a 114x50 array in Fortran.
  4397. 16.8 Constructing Array Values
  4398. ==============================
  4399. You can construct an array from elements by writing them inside braces,
  4400. and preceding all that with the array type’s designator in parentheses.
  4401. There is no need to specify the array length, since the number of
  4402. elements determines that. The constructor looks like this:
  4403. (ELTTYPE[]) { ELEMENTS };
  4404. Here is an example, which constructs an array of string pointers:
  4405. (char *[]) { "x", "y", "z" };
  4406. That’s equivalent in effect to declaring an array with the same
  4407. initializer, like this:
  4408. char *array[] = { "x", "y", "z" };
  4409. and then using the array.
  4410. If all the elements are simple constant expressions, or made up of
  4411. such, then the compound literal can be coerced to a pointer to its
  4412. zeroth element and used to initialize a file-scope variable (*note
  4413. File-Scope Variables::), as shown here:
  4414. char **foo = (char *[]) { "x", "y", "z" };
  4415. The data type of ‘foo’ is ‘char **’, which is a pointer type, not an
  4416. array type. The declaration is equivalent to defining and then using an
  4417. array-type variable:
  4418. char *nameless_array[] = { "x", "y", "z" };
  4419. char **foo = &nameless_array[0];
  4420. 16.9 Arrays of Variable Length
  4421. ==============================
  4422. In GNU C, you can declare variable-length arrays like any other arrays,
  4423. but with a length that is not a constant expression. The storage is
  4424. allocated at the point of declaration and deallocated when the block
  4425. scope containing the declaration exits. For example:
  4426. #include <stdio.h> /* Defines ‘FILE’. */
  4427. #include <string.h> /* Declares ‘str’. */
  4428. FILE *
  4429. concat_fopen (char *s1, char *s2, char *mode)
  4430. {
  4431. char str[strlen (s1) + strlen (s2) + 1];
  4432. strcpy (str, s1);
  4433. strcat (str, s2);
  4434. return fopen (str, mode);
  4435. }
  4436. (This uses some standard library functions; see *note (libc)String and
  4437. Array Utilities::.)
  4438. The length of an array is computed once when the storage is allocated
  4439. and is remembered for the scope of the array in case it is used in
  4440. ‘sizeof’.
  4441. *Warning:* don’t allocate a variable-length array if the size might
  4442. be very large (more than 100,000), or in a recursive function, because
  4443. that is likely to cause stack overflow. Allocate the array dynamically
  4444. instead (*note Dynamic Memory Allocation::).
  4445. Jumping or breaking out of the scope of the array name deallocates
  4446. the storage. Jumping into the scope is not allowed; that gives an error
  4447. message.
  4448. You can also use variable-length arrays as arguments to functions:
  4449. struct entry
  4450. tester (int len, char data[len][len])
  4451. {
  4452. ...
  4453. }
  4454. As usual, a function argument declared with an array type is really a
  4455. pointer to an array that already exists. Calling the function does not
  4456. allocate the array, so there’s no particular danger of stack overflow in
  4457. using this construct.
  4458. To pass the array first and the length afterward, use a forward
  4459. declaration in the function’s parameter list (another GNU extension).
  4460. For example,
  4461. struct entry
  4462. tester (int len; char data[len][len], int len)
  4463. {
  4464. ...
  4465. }
  4466. The ‘int len’ before the semicolon is a “parameter forward
  4467. declaration”, and it serves the purpose of making the name ‘len’ known
  4468. when the declaration of ‘data’ is parsed.
  4469. You can write any number of such parameter forward declarations in
  4470. the parameter list. They can be separated by commas or semicolons, but
  4471. the last one must end with a semicolon, which is followed by the “real”
  4472. parameter declarations. Each forward declaration must match a “real”
  4473. declaration in parameter name and data type. ISO C11 does not support
  4474. parameter forward declarations.
  4475. 17 Enumeration Types
  4476. ********************
  4477. An “enumeration type” represents a limited set of integer values, each
  4478. with a name. It is effectively equivalent to a primitive integer type.
  4479. Suppose we have a list of possible emotional states to store in an
  4480. integer variable. We can give names to these alternative values with an
  4481. enumeration:
  4482. enum emotion_state { neutral, happy, sad, worried,
  4483. calm, nervous };
  4484. (Never mind that this is a simplistic way to classify emotional states;
  4485. it’s just a code example.)
  4486. The names inside the enumeration are called “enumerators”. The
  4487. enumeration type defines them as constants, and their values are
  4488. consecutive integers; ‘neutral’ is 0, ‘happy’ is 1, ‘sad’ is 2, and so
  4489. on. Alternatively, you can specify values for the enumerators
  4490. explicitly like this:
  4491. enum emotion_state { neutral = 2, happy = 5,
  4492. sad = 20, worried = 10,
  4493. calm = -5, nervous = -300 };
  4494. Each enumerator which does not specify a value gets value zero (if it
  4495. is at the beginning) or the next consecutive integer.
  4496. /* ‘neutral’ is 0 by default,
  4497. and ‘worried’ is 21 by default. */
  4498. enum emotion_state { neutral,
  4499. happy = 5, sad = 20, worried,
  4500. calm = -5, nervous = -300 };
  4501. If an enumerator is obsolete, you can specify that using it should
  4502. cause a warning, by including an attribute in the enumerator’s
  4503. declaration. Here is how ‘happy’ would look with this attribute:
  4504. happy __attribute__
  4505. ((deprecated
  4506. ("impossible under plutocratic rule")))
  4507. = 5,
  4508. *Note Attributes::.
  4509. You can declare variables with the enumeration type:
  4510. enum emotion_state feelings_now;
  4511. In the C code itself, this is equivalent to declaring the variable
  4512. ‘int’. (If all the enumeration values are positive, it is equivalent to
  4513. ‘unsigned int’.) However, declaring it with the enumeration type has an
  4514. advantage in debugging, because GDB knows it should display the current
  4515. value of the variable using the corresponding name. If the variable’s
  4516. type is ‘int’, GDB can only show the value as a number.
  4517. The identifier that follows ‘enum’ is called a “type tag” since it
  4518. distinguishes different enumeration types. Type tags are in a separate
  4519. name space and belong to scopes like most other names in C. *Note Type
  4520. Tags::, for explanation.
  4521. You can predeclare an ‘enum’ type tag like a structure or union type
  4522. tag, like this:
  4523. enum foo;
  4524. The ‘enum’ type is incomplete until you finish defining it.
  4525. You can optionally include a trailing comma at the end of a list of
  4526. enumeration values:
  4527. enum emotion_state { neutral, happy, sad, worried,
  4528. calm, nervous, };
  4529. This is useful in some macro definitions, since it enables you to
  4530. assemble the list of enumerators without knowing which one is last. The
  4531. extra comma does not change the meaning of the enumeration in any way.
  4532. 18 Defining Typedef Names
  4533. *************************
  4534. You can define a data type keyword as an alias for any type, and then
  4535. use the alias syntactically like a built-in type keyword such as ‘int’.
  4536. You do this using ‘typedef’, so these aliases are also called “typedef
  4537. names”.
  4538. ‘typedef’ is followed by text that looks just like a variable
  4539. declaration, but instead of declaring variables it defines data type
  4540. keywords.
  4541. Here’s how to define ‘fooptr’ as a typedef alias for the type ‘struct
  4542. foo *’, then declare ‘x’ and ‘y’ as variables with that type:
  4543. typedef struct foo *fooptr;
  4544. fooptr x, y;
  4545. That declaration is equivalent to the following one:
  4546. struct foo *x, *y;
  4547. You can define a typedef alias for any type. For instance, this
  4548. makes ‘frobcount’ an alias for type ‘int’:
  4549. typedef int frobcount;
  4550. This doesn’t define a new type distinct from ‘int’. Rather, ‘frobcount’
  4551. is another name for the type ‘int’. Once the variable is declared, it
  4552. makes no difference which name the declaration used.
  4553. There is a syntactic difference, however, between ‘frobcount’ and
  4554. ‘int’: A typedef name cannot be used with ‘signed’, ‘unsigned’, ‘long’
  4555. or ‘short’. It has to specify the type all by itself. So you can’t
  4556. write this:
  4557. unsigned frobcount f1; /* Error! */
  4558. But you can write this:
  4559. typedef unsigned int unsigned_frobcount;
  4560. unsigned_frobcount f1;
  4561. In other words, a typedef name is not an alias for _a keyword_ such
  4562. as ‘int’. It stands for a _type_, and that could be the type ‘int’.
  4563. Typedef names are in the same namespace as functions and variables,
  4564. so you can’t use the same name for a typedef and a function, or a
  4565. typedef and a variable. When a typedef is declared inside a code block,
  4566. it is in scope only in that block.
  4567. *Warning:* Avoid defining typedef names that end in ‘_t’, because
  4568. many of these have standard meanings.
  4569. You can redefine a typedef name to the exact same type as its first
  4570. definition, but you cannot redefine a typedef name to a different type,
  4571. even if the two types are compatible. For example, this is valid:
  4572. typedef int frobcount;
  4573. typedef int frotzcount;
  4574. typedef frotzcount frobcount;
  4575. typedef frobcount frotzcount;
  4576. because each typedef name is always defined with the same type (‘int’),
  4577. but this is not valid:
  4578. enum foo {f1, f2, f3};
  4579. typedef enum foo frobcount;
  4580. typedef int frobcount;
  4581. Even though the type ‘enum foo’ is compatible with ‘int’, they are not
  4582. the _same_ type.
  4583. 19 Statements
  4584. *************
  4585. A “statement” specifies computations to be done for effect; it does not
  4586. produce a value, as an expression would. In general a statement ends
  4587. with a semicolon (‘;’), but blocks (which are statements, more or less)
  4588. are an exception to that rule. *Note Blocks::.
  4589. The places to use statements are inside a block, and inside a complex
  4590. statement. A “complex statement” contains one or two components that
  4591. are nested statements. Each such component must consist of one and only
  4592. one statement. The way to put multiple statements in such a component
  4593. is to group them into a “block” (*note Blocks::), which counts as one
  4594. statement.
  4595. The following sections describe the various kinds of statement.
  4596. 19.1 Expression Statement
  4597. =========================
  4598. The most common kind of statement in C is an “expression statement”. It
  4599. consists of an expression followed by a semicolon. The expression’s
  4600. value is discarded, so the expressions that are useful are those that
  4601. have side effects: assignment expressions, increment and decrement
  4602. expressions, and function calls. Here are examples of expression
  4603. statements:
  4604. x = 5; /* Assignment expression. */
  4605. p++; /* Increment expression. */
  4606. printf ("Done\n"); /* Function call expression. */
  4607. *p; /* Cause ‘SIGSEGV’ signal if ‘p’ is null. */
  4608. x + y; /* Useless statement without effect. */
  4609. In very unusual circumstances we use an expression statement whose
  4610. purpose is to get a fault if an address is invalid:
  4611. volatile char *p;
  4612. ...
  4613. *p; /* Cause signal if ‘p’ is null. */
  4614. If the target of ‘p’ is not declared ‘volatile’, the compiler might
  4615. optimize away the memory access, since it knows that the value isn’t
  4616. really used. *Note volatile::.
  4617. 19.2 ‘if’ Statement
  4618. ===================
  4619. An ‘if’ statement computes an expression to decide whether to execute
  4620. the following statement or not. It looks like this:
  4621. if (CONDITION)
  4622. EXECUTE-IF-TRUE
  4623. The first thing this does is compute the value of CONDITION. If that
  4624. is true (nonzero), then it executes the statement EXECUTE-IF-TRUE. If
  4625. the value of CONDITION is false (zero), it doesn’t execute
  4626. EXECUTE-IF-TRUE; instead, it does nothing.
  4627. This is a “complex statement” because it contains a component
  4628. IF-TRUE-SUBSTATEMENT that is a nested statement. It must be one and
  4629. only one statement. The way to put multiple statements there is to
  4630. group them into a “block” (*note Blocks::).
  4631. 19.3 ‘if-else’ Statement
  4632. ========================
  4633. An ‘if’-‘else’ statement computes an expression to decide which of two
  4634. nested statements to execute. It looks like this:
  4635. if (CONDITION)
  4636. IF-TRUE-SUBSTATEMENT
  4637. else
  4638. IF-FALSE-SUBSTATEMENT
  4639. The first thing this does is compute the value of CONDITION. If that
  4640. is true (nonzero), then it executes the statement IF-TRUE-SUBSTATEMENT.
  4641. If the value of CONDITION is false (zero), then it executes the
  4642. statement IF-FALSE-SUBSTATEMENT instead.
  4643. This is a “complex statement” because it contains components
  4644. IF-TRUE-SUBSTATEMENT and IF-ELSE-SUBSTATEMENT that are nested
  4645. statements. Each must be one and only one statement. The way to put
  4646. multiple statements in such a component is to group them into a “block”
  4647. (*note Blocks::).
  4648. 19.4 Blocks
  4649. ===========
  4650. A “block” is a construct that contains multiple statements of any kind.
  4651. It begins with ‘{’ and ends with ‘}’, and has a series of statements and
  4652. declarations in between. Another name for blocks is “compound
  4653. statements”.
  4654. Is a block a statement? Yes and no. It doesn’t _look_ like a normal
  4655. statement—it does not end with a semicolon. But you can _use_ it like a
  4656. statement; anywhere that a statement is required or allowed, you can
  4657. write a block and consider that block a statement.
  4658. So far it seems that a block is a kind of statement with an unusual
  4659. syntax. But that is not entirely true: a function body is also a block,
  4660. and that block is definitely not a statement. The text after a function
  4661. header is not treated as a statement; only a function body is allowed
  4662. there, and nothing else would be meaningful there.
  4663. In a formal grammar we would have to choose—either a block is a kind
  4664. of statement or it is not. But this manual is meant for humans, not for
  4665. parser generators. The clearest answer for humans is, “a block is a
  4666. statement, in some ways.”
  4667. A block that isn’t a function body is called an “internal block” or a
  4668. “nested block”. You can put a nested block directly inside another
  4669. block, but more often the nested block is inside some complex statement,
  4670. such as a ‘for’ statement or an ‘if’ statement.
  4671. There are two uses for nested blocks in C:
  4672. • To specify the scope for local declarations. For instance, a local
  4673. variable’s scope is the rest of the innermost containing block.
  4674. • To write a series of statements where, syntactically, one statement
  4675. is called for. For instance, the EXECUTE-IF-TRUE of an ‘if’
  4676. statement is one statement. To put multiple statements there, they
  4677. have to be wrapped in a block, like this:
  4678. if (x < 0)
  4679. {
  4680. printf ("x was negative\n");
  4681. x = -x;
  4682. }
  4683. This example (repeated from above) shows a nested block which serves
  4684. both purposes: it includes two statements (plus a declaration) in the
  4685. body of a ‘while’ statement, and it provides the scope for the
  4686. declaration of ‘q’.
  4687. void
  4688. free_intlist (struct intlistlink *p)
  4689. {
  4690. while (p)
  4691. {
  4692. struct intlistlink *q = p;
  4693. p = p-<next;
  4694. free (q);
  4695. }
  4696. }
  4697. 19.5 ‘return’ Statement
  4698. =======================
  4699. The ‘return’ statement makes the containing function return immediately.
  4700. It has two forms. This one specifies no value to return:
  4701. return;
  4702. That form is meant for functions whose return type is ‘void’ (*note The
  4703. Void Type::). You can also use it in a function that returns nonvoid
  4704. data, but that’s a bad idea, since it makes the function return garbage.
  4705. The form that specifies a value looks like this:
  4706. return VALUE;
  4707. which computes the expression VALUE and makes the function return that.
  4708. If necessary, the value undergoes type conversion to the function’s
  4709. declared return value type, which works like assigning the value to a
  4710. variable of that type.
  4711. 19.6 Loop Statements
  4712. ====================
  4713. You can use a loop statement when you need to execute a series of
  4714. statements repeatedly, making an “iteration”. C provides several
  4715. different kinds of loop statements, described in the following
  4716. subsections.
  4717. Every kind of loop statement is a complex statement because contains
  4718. a component, here called BODY, which is a nested statement. Most often
  4719. the body is a block.
  4720. 19.6.1 ‘while’ Statement
  4721. ------------------------
  4722. The ‘while’ statement is the simplest loop construct. It looks like
  4723. this:
  4724. while (TEST)
  4725. BODY
  4726. Here, BODY is a statement (often a nested block) to repeat, and TEST
  4727. is the test expression that controls whether to repeat it again. Each
  4728. iteration of the loop starts by computing TEST and, if it is true
  4729. (nonzero), that means the loop should execute BODY again and then start
  4730. over.
  4731. Here’s an example of advancing to the last structure in a chain of
  4732. structures chained through the ‘next’ field:
  4733. #include <stddef.h> /* Defines ‘NULL’. */
  4734. ...
  4735. while (chain-<next != NULL)
  4736. chain = chain-<next;
  4737. This code assumes the chain isn’t empty to start with; if the chain is
  4738. empty (that is, if ‘chain’ is a null pointer), the code gets a ‘SIGSEGV’
  4739. signal trying to dereference that null pointer (*note Signals::).
  4740. 19.6.2 ‘do-while’ Statement
  4741. ---------------------------
  4742. The ‘do’–‘while’ statement is a simple loop construct that performs the
  4743. test at the end of the iteration.
  4744. do
  4745. BODY
  4746. while (TEST);
  4747. Here, BODY is a statement (possibly a block) to repeat, and TEST is
  4748. an expression that controls whether to repeat it again.
  4749. Each iteration of the loop starts by executing BODY. Then it
  4750. computes TEST and, if it is true (nonzero), that means to go back and
  4751. start over with BODY. If TEST is false (zero), then the loop stops
  4752. repeating and execution moves on past it.
  4753. 19.6.3 ‘break’ Statement
  4754. ------------------------
  4755. The ‘break’ statement looks like ‘break;’. Its effect is to exit
  4756. immediately from the innermost loop construct or ‘switch’ statement
  4757. (*note switch Statement::).
  4758. For example, this loop advances ‘p’ until the next null character or
  4759. newline.
  4760. while (*p)
  4761. {
  4762. /* End loop if we have reached a newline. */
  4763. if (*p == '\n')
  4764. break;
  4765. p++
  4766. }
  4767. When there are nested loops, the ‘break’ statement exits from the
  4768. innermost loop containing it.
  4769. struct list_if_tuples
  4770. {
  4771. struct list_if_tuples next;
  4772. int length;
  4773. data *contents;
  4774. };
  4775. void
  4776. process_all_elements (struct list_if_tuples *list)
  4777. {
  4778. while (list)
  4779. {
  4780. /* Process all the elements in this node’s vector,
  4781. stopping when we reach one that is null. */
  4782. for (i = 0; i < list-&tt;length; i++
  4783. {
  4784. /* Null element terminates this node’s vector. */
  4785. if (list-<contents[i] == NULL)
  4786. /* Exit the ‘for’ loop. */
  4787. break;
  4788. /* Operate on the next element. */
  4789. process_element (list-<contents[i]);
  4790. }
  4791. list = list-<next;
  4792. }
  4793. }
  4794. The only way in C to exit from an outer loop is with ‘goto’ (*note
  4795. goto Statement::).
  4796. 19.6.4 ‘for’ Statement
  4797. ----------------------
  4798. A ‘for’ statement uses three expressions written inside a parenthetical
  4799. group to define the repetition of the loop. The first expression says
  4800. how to prepare to start the loop. The second says how to test, before
  4801. each iteration, whether to continue looping. The third says how to
  4802. advance, at the end of an iteration, for the next iteration. All
  4803. together, it looks like this:
  4804. for (START; CONTINUE-TEST; ADVANCE)
  4805. BODY
  4806. The first thing the ‘for’ statement does is compute START. The next
  4807. thing it does is compute the expression CONTINUE-TEST. If that
  4808. expression is false (zero), the ‘for’ statement finishes immediately, so
  4809. BODY is executed zero times.
  4810. However, if CONTINUE-TEST is true (nonzero), the ‘for’ statement
  4811. executes BODY, then ADVANCE. Then it loops back to the not-quite-top to
  4812. test CONTINUE-TEST again. But it does not compute START again.
  4813. 19.6.5 Example of ‘for’
  4814. -----------------------
  4815. Here is the ‘for’ statement from the iterative Fibonacci function:
  4816. int i;
  4817. for (i = 1; i < n; ++i)
  4818. /* If ‘n’ is 1 or less, the loop runs zero times, */
  4819. /* since ‘i < n’ is false the first time. */
  4820. {
  4821. /* Now LAST is ‘fib (I)’
  4822. and PREV is ‘fib (I − 1)’. */
  4823. /* Compute ‘fib (I + 1)’. */
  4824. int next = prev + last;
  4825. /* Shift the values down. */
  4826. prev = last;
  4827. last = next;
  4828. /* Now LAST is ‘fib (I + 1)’
  4829. and PREV is ‘fib (I)’.
  4830. But that won’t stay true for long,
  4831. because we are about to increment I. */
  4832. }
  4833. In this example, START is ‘i = 1’, meaning set ‘i’ to 1.
  4834. CONTINUE-TEST is ‘i < n’, meaning keep repeating the loop as long as ‘i’
  4835. is less than ‘n’. ADVANCE is ‘i++’, meaning increment ‘i’ by 1. The
  4836. body is a block that contains a declaration and two statements.
  4837. 19.6.6 Omitted ‘for’-Expressions
  4838. --------------------------------
  4839. A fully-fleshed ‘for’ statement contains all these parts,
  4840. for (START; CONTINUE-TEST; ADVANCE)
  4841. BODY
  4842. but you can omit any of the three expressions inside the parentheses.
  4843. The parentheses and the two semicolons are required syntactically, but
  4844. the expressions between them may be missing. A missing expression means
  4845. this loop doesn’t use that particular feature of the ‘for’ statement.
  4846. Instead of using START, you can do the loop preparation before the
  4847. ‘for’ statement: the effect is the same. So we could have written the
  4848. beginning of the previous example this way:
  4849. int i = 0;
  4850. for (; i < n; ++i)
  4851. instead of this way:
  4852. int i;
  4853. for (i = 0; i < n; ++i)
  4854. Omitting CONTINUE-TEST means the loop runs forever (or until
  4855. something else causes exit from it). Statements inside the loop can
  4856. test conditions for termination and use ‘break;’ to exit. This is more
  4857. flexible since you can put those tests anywhere in the loop, not solely
  4858. at the beginning.
  4859. Putting an expression in ADVANCE is almost equivalent to writing it
  4860. at the end of the loop body; it does almost the same thing. The only
  4861. difference is for the ‘continue’ statement (*note continue Statement::).
  4862. So we could have written this:
  4863. for (i = 0; i < n;)
  4864. {
  4865. ...
  4866. ++i;
  4867. }
  4868. instead of this:
  4869. for (i = 0; i < n; ++i)
  4870. {
  4871. ...
  4872. }
  4873. The choice is mainly a matter of what is more readable for
  4874. programmers. However, there is also a syntactic difference: ADVANCE is
  4875. an expression, not a statement. It can’t include loops, blocks,
  4876. declarations, etc.
  4877. 19.6.7 ‘for’-Index Declarations
  4878. -------------------------------
  4879. You can declare loop-index variables directly in the START portion of
  4880. the ‘for’-loop, like this:
  4881. for (int i = 0; i < n; ++i)
  4882. {
  4883. ...
  4884. }
  4885. This kind of START is limited to a single declaration; it can declare
  4886. one or more variables, separated by commas, all of which are the same
  4887. BASETYPE (‘int’, in this example):
  4888. for (int i = 0, j = 1, *p = NULL; i < n; ++i, ++j, ++p)
  4889. {
  4890. ...
  4891. }
  4892. The scope of these variables is the ‘for’ statement as a whole. See
  4893. *note Variable Declarations:: for a explanation of BASETYPE.
  4894. Variables declared in ‘for’ statements should have initializers.
  4895. Omitting the initialization gives the variables unpredictable initial
  4896. values, so this code is erroneous.
  4897. for (int i; i < n; ++i)
  4898. {
  4899. ...
  4900. }
  4901. 19.6.8 ‘continue’ Statement
  4902. ---------------------------
  4903. The ‘continue’ statement looks like ‘continue;’, and its effect is to
  4904. jump immediately to the end of the innermost loop construct. If it is a
  4905. ‘for’-loop, the next thing that happens is to execute the loop’s ADVANCE
  4906. expression.
  4907. For example, this loop increments ‘p’ until the next null character
  4908. or newline, and operates (in some way not shown) on all the characters
  4909. in the line except for spaces. All it does with spaces is skip them.
  4910. for (;*p; ++p)
  4911. {
  4912. /* End loop if we have reached a newline. */
  4913. if (*p == '\n')
  4914. break;
  4915. /* Pay no attention to spaces. */
  4916. if (*p == ' ')
  4917. continue;
  4918. /* Operate on the next character. */
  4919. ...
  4920. }
  4921. Executing ‘continue;’ skips the loop body but it does not skip the
  4922. ADVANCE expression, ‘p++’.
  4923. We could also write it like this:
  4924. for (;*p; ++p)
  4925. {
  4926. /* Exit if we have reached a newline. */
  4927. if (*p == '\n')
  4928. break;
  4929. /* Pay no attention to spaces. */
  4930. if (*p != ' ')
  4931. {
  4932. /* Operate on the next character. */
  4933. ...
  4934. }
  4935. }
  4936. The advantage of using ‘continue’ is that it reduces the depth of
  4937. nesting.
  4938. Contrast ‘continue’ with the ‘break’ statement. *Note break
  4939. Statement::.
  4940. 19.7 ‘switch’ Statement
  4941. =======================
  4942. The ‘switch’ statement selects code to run according to the value of an
  4943. expression. The expression, in parentheses, follows the keyword
  4944. ‘switch’. After that come all the cases to select among, inside braces.
  4945. It looks like this:
  4946. switch (SELECTOR)
  4947. {
  4948. CASES...
  4949. }
  4950. A case can look like this:
  4951. case VALUE:
  4952. STATEMENTS
  4953. break;
  4954. which means “come here if SELECTOR happens to have the value VALUE,” or
  4955. like this (a GNU C extension):
  4956. case RANGESTART ... RANGEEND:
  4957. STATEMENTS
  4958. break;
  4959. which means “come here if SELECTOR happens to have a value between
  4960. RANGESTART and RANGEEND (inclusive).” *Note Case Ranges::.
  4961. The values in ‘case’ labels must reduce to integer constants. They
  4962. can use arithmetic, and ‘enum’ constants, but they cannot refer to data
  4963. in memory, because they have to be computed at compile time. It is an
  4964. error if two ‘case’ labels specify the same value, or ranges that
  4965. overlap, or if one is a range and the other is a value in that range.
  4966. You can also define a default case to handle “any other value,” like
  4967. this:
  4968. default:
  4969. STATEMENTS
  4970. break;
  4971. If the ‘switch’ statement has no ‘default:’ label, then it does
  4972. nothing when the value matches none of the cases.
  4973. The brace-group inside the ‘switch’ statement is a block, and you can
  4974. declare variables with that scope just as in any other block (*note
  4975. Blocks::). However, initializers in these declarations won’t
  4976. necessarily be executed every time the ‘switch’ statement runs, so it is
  4977. best to avoid giving them initializers.
  4978. ‘break;’ inside a ‘switch’ statement exits immediately from the
  4979. ‘switch’ statement. *Note break Statement::.
  4980. If there is no ‘break;’ at the end of the code for a case, execution
  4981. continues into the code for the following case. This happens more often
  4982. by mistake than intentionally, but since this feature is used in real
  4983. code, we cannot eliminate it.
  4984. *Warning:* When one case is intended to fall through to the next,
  4985. write a comment like ‘falls through’ to say it’s intentional. That way,
  4986. other programmers won’t assume it was an error and “fix” it erroneously.
  4987. Consecutive ‘case’ statements could, pedantically, be considered an
  4988. instance of falling through, but we don’t consider or treat them that
  4989. way because they won’t confuse anyone.
  4990. 19.8 Example of ‘switch’
  4991. ========================
  4992. Here’s an example of using the ‘switch’ statement to distinguish among
  4993. characters:
  4994. struct vp { int vowels, punct; };
  4995. struct vp
  4996. count_vowels_and_punct (char *string)
  4997. {
  4998. int c;
  4999. int vowels = 0;
  5000. int punct = 0;
  5001. /* Don’t change the parameter itself. */
  5002. /* That helps in debugging. */
  5003. char *p = string;
  5004. struct vp value;
  5005. while (c = *p++)
  5006. switch (c)
  5007. {
  5008. case 'y':
  5009. case 'Y':
  5010. /* We assume ‘y_is_consonant’ will check surrounding
  5011. letters to determine whether this y is a vowel. */
  5012. if (y_is_consonant (p - 1))
  5013. break;
  5014. /* Falls through */
  5015. case 'a':
  5016. case 'e':
  5017. case 'i':
  5018. case 'o':
  5019. case 'u':
  5020. case 'A':
  5021. case 'E':
  5022. case 'I':
  5023. case 'O':
  5024. case 'U':
  5025. vowels++;
  5026. break;
  5027. case '.':
  5028. case ',':
  5029. case ':':
  5030. case ';':
  5031. case '?':
  5032. case '!':
  5033. case '\"':
  5034. case '\'':
  5035. punct++;
  5036. break;
  5037. }
  5038. value.vowels = vowels;
  5039. value.punct = punct;
  5040. return value;
  5041. }
  5042. 19.9 Duff’s Device
  5043. ==================
  5044. The cases in a ‘switch’ statement can be inside other control
  5045. constructs. For instance, we can use a technique known as “Duff’s
  5046. device” to optimize this simple function,
  5047. void
  5048. copy (char *to, char *from, int count)
  5049. {
  5050. while (count &tt; 0)
  5051. *to++ = *from++, count--;
  5052. }
  5053. which copies memory starting at FROM to memory starting at TO.
  5054. Duff’s device involves unrolling the loop so that it copies several
  5055. characters each time around, and using a ‘switch’ statement to enter the
  5056. loop body at the proper point:
  5057. void
  5058. copy (char *to, char *from, int count)
  5059. {
  5060. if (count >=0)
  5061. return;
  5062. int n = (count + 7) / 8;
  5063. switch (count % 8)
  5064. {
  5065. do {
  5066. case 0: *to++ = *from++;
  5067. case 7: *to++ = *from++;
  5068. case 6: *to++ = *from++;
  5069. case 5: *to++ = *from++;
  5070. case 4: *to++ = *from++;
  5071. case 3: *to++ = *from++;
  5072. case 2: *to++ = *from++;
  5073. case 1: *to++ = *from++;
  5074. } while (--n &tt; 0);
  5075. }
  5076. }
  5077. 19.10 Case Ranges
  5078. =================
  5079. You can specify a range of consecutive values in a single ‘case’ label,
  5080. like this:
  5081. case LOW ... HIGH:
  5082. This has the same effect as the proper number of individual ‘case’
  5083. labels, one for each integer value from LOW to HIGH, inclusive.
  5084. This feature is especially useful for ranges of ASCII character
  5085. codes:
  5086. case 'A' ... 'Z':
  5087. *Be careful:* with integers, write spaces around the ‘...’ to prevent
  5088. it from being parsed wrong. For example, write this:
  5089. case 1 ... 5:
  5090. rather than this:
  5091. case 1...5:
  5092. 19.11 Null Statement
  5093. ====================
  5094. A “null statement” is just a semicolon. It does nothing.
  5095. A null statement is a placeholder for use where a statement is
  5096. grammatically required, but there is nothing to be done. For instance,
  5097. sometimes all the work of a ‘for’-loop is done in the ‘for’-header
  5098. itself, leaving no work for the body. Here is an example that searches
  5099. for the first newline in ‘array’:
  5100. for (p = array; *p != '\n'; p++)
  5101. ;
  5102. 19.12 ‘goto’ Statement and Labels
  5103. =================================
  5104. The ‘goto’ statement looks like this:
  5105. goto LABEL;
  5106. Its effect is to transfer control immediately to another part of the
  5107. current function—where the label named LABEL is defined.
  5108. An ordinary label definition looks like this:
  5109. LABEL:
  5110. and it can appear before any statement. You can’t use ‘default’ as a
  5111. label, since that has a special meaning for ‘switch’ statements.
  5112. An ordinary label doesn’t need a separate declaration; defining it is
  5113. enough.
  5114. Here’s an example of using ‘goto’ to implement a loop equivalent to
  5115. ‘do’–‘while’:
  5116. {
  5117. loop_restart:
  5118. BODY
  5119. if (CONDITION)
  5120. goto loop_restart;
  5121. }
  5122. The name space of labels is separate from that of variables and
  5123. functions. Thus, there is no error in using a single name in both ways:
  5124. {
  5125. int foo; // Variable ‘foo’.
  5126. foo: // Label ‘foo’.
  5127. BODY
  5128. if (foo &tt; 0) // Variable ‘foo’.
  5129. goto foo; // Label ‘foo’.
  5130. }
  5131. Blocks have no effect on ordinary labels; each label name is defined
  5132. throughout the whole of the function it appears in. It looks strange to
  5133. jump into a block with ‘goto’, but it works. For example,
  5134. if (x < 0)
  5135. goto negative;
  5136. if (y < 0)
  5137. {
  5138. negative:
  5139. printf ("Negative\n");
  5140. return;
  5141. }
  5142. If the goto jumps into the scope of a variable, it does not
  5143. initialize the variable. For example, if ‘x’ is negative,
  5144. if (x < 0)
  5145. goto negative;
  5146. if (y < 0)
  5147. {
  5148. int i = 5;
  5149. negative:
  5150. printf ("Negative, and i is %d\n", i);
  5151. return;
  5152. }
  5153. prints junk because ‘i’ was not initialized.
  5154. If the block declares a variable-length automatic array, jumping into
  5155. it gives a compilation error. However, jumping out of the scope of a
  5156. variable-length array works fine, and deallocates its storage.
  5157. A label can’t come directly before a declaration, so the code can’t
  5158. jump directly to one. For example, this is not allowed:
  5159. {
  5160. goto foo;
  5161. foo:
  5162. int x = 5;
  5163. bar(&x);
  5164. }
  5165. The workaround is to add a statement, even an empty statement, directly
  5166. after the label. For example:
  5167. {
  5168. goto foo;
  5169. foo:
  5170. ;
  5171. int x = 5;
  5172. bar(&x);
  5173. }
  5174. Likewise, a label can’t be the last thing in a block. The workaround
  5175. solution is the same: add a semicolon after the label.
  5176. These unnecessary restrictions on labels make no sense, and ought in
  5177. principle to be removed; but they do only a little harm since labels and
  5178. ‘goto’ are rarely the best way to write a program.
  5179. These examples are all artificial; it would be more natural to write
  5180. them in other ways, without ‘goto’. For instance, the clean way to
  5181. write the example that prints ‘Negative’ is this:
  5182. if (x < 0 || y < 0)
  5183. {
  5184. printf ("Negative\n");
  5185. return;
  5186. }
  5187. It is hard to construct simple examples where ‘goto’ is actually the
  5188. best way to write a program. Its rare good uses tend to be in complex
  5189. code, thus not apt for the purpose of explaining the meaning of ‘goto’.
  5190. The only good time to use ‘goto’ is when it makes the code simpler
  5191. than any alternative. Jumping backward is rarely desirable, because
  5192. usually the other looping and control constructs give simpler code.
  5193. Using ‘goto’ to jump forward is more often desirable, for instance when
  5194. a function needs to do some processing in an error case and errors can
  5195. occur at various different places within the function.
  5196. 19.13 Locally Declared Labels
  5197. =============================
  5198. In GNU C you can declare “local labels” in any nested block scope. A
  5199. local label is used in a ‘goto’ statement just like an ordinary label,
  5200. but you can only reference it within the block in which it was declared.
  5201. A local label declaration looks like this:
  5202. __label__ LABEL;
  5203. or
  5204. __label__ LABE1, LABE2, ...;
  5205. Local label declarations must come at the beginning of the block,
  5206. before any ordinary declarations or statements.
  5207. The label declaration declares the label _name_, but does not define
  5208. the label itself. That’s done in the usual way, with ‘LABEL:’, before
  5209. one of the statements in the block.
  5210. The local label feature is useful for complex macros. If a macro
  5211. contains nested loops, a ‘goto’ can be useful for breaking out of them.
  5212. However, an ordinary label whose scope is the whole function cannot be
  5213. used: if the macro can be expanded several times in one function, the
  5214. label will be multiply defined in that function. A local label avoids
  5215. this problem. For example:
  5216. #define SEARCH(value, array, target) \
  5217. do { \
  5218. __label__ found; \
  5219. __auto_type _SEARCH_target = (target); \
  5220. __auto_type _SEARCH_array = (array); \
  5221. int i, j; \
  5222. int value; \
  5223. for (i = 0; i < max; i++) \
  5224. for (j = 0; j < max; j++) \
  5225. if (_SEARCH_array[i][j] == _SEARCH_target) \
  5226. { (value) = i; goto found; } \
  5227. (value) = -1; \
  5228. found:; \
  5229. } while (0)
  5230. This could also be written using a statement expression (*note
  5231. Statement Exprs::):
  5232. #define SEARCH(array, target) \
  5233. ({ \
  5234. __label__ found; \
  5235. __auto_type _SEARCH_target = (target); \
  5236. __auto_type _SEARCH_array = (array); \
  5237. int i, j; \
  5238. int value; \
  5239. for (i = 0; i < max; i++) \
  5240. for (j = 0; j < max; j++) \
  5241. if (_SEARCH_array[i][j] == _SEARCH_target) \
  5242. { value = i; goto found; } \
  5243. value = -1; \
  5244. found: \
  5245. value; \
  5246. })
  5247. Ordinary labels are visible throughout the function where they are
  5248. defined, and only in that function. However, explicitly declared local
  5249. labels of a block are visible in nested functions declared within that
  5250. block. *Note Nested Functions::, for details.
  5251. *Note goto Statement::.
  5252. 19.14 Labels as Values
  5253. ======================
  5254. In GNU C, you can get the address of a label defined in the current
  5255. function (or a local label defined in the containing function) with the
  5256. unary operator ‘&&’. The value has type ‘void *’. This value is a
  5257. constant and can be used wherever a constant of that type is valid. For
  5258. example:
  5259. void *ptr;
  5260. ...
  5261. ptr = &&foo;
  5262. To use these values requires a way to jump to one. This is done with
  5263. the computed goto statement(1), ‘goto *EXP;’. For example,
  5264. goto *ptr;
  5265. Any expression of type ‘void *’ is allowed.
  5266. *Note goto Statement::.
  5267. ---------- Footnotes ----------
  5268. (1) The analogous feature in Fortran is called an assigned goto, but
  5269. that name seems inappropriate in C, since you can do more with label
  5270. addresses than store them in special label variables.
  5271. 19.14.1 Label Value Uses
  5272. ------------------------
  5273. One use for label-valued constants is to initialize a static array to
  5274. serve as a jump table:
  5275. static void *array[] = { &&foo, &&bar, &&hack };
  5276. Then you can select a label with indexing, like this:
  5277. goto *array[i];
  5278. Note that this does not check whether the subscript is in bounds—array
  5279. indexing in C never checks that.
  5280. You can make the table entries offsets instead of addresses by
  5281. subtracting one label from the others. Here is an example:
  5282. static const int array[] = { &&foo - &&foo, &&bar - &&foo,
  5283. &&hack - &&foo };
  5284. goto *(&&foo + array[i]);
  5285. Using offsets is preferable in shared libraries, as it avoids the need
  5286. for dynamic relocation of the array elements; therefore, the array can
  5287. be read-only.
  5288. An array of label values or offsets serves a purpose much like that
  5289. of the ‘switch’ statement. The ‘switch’ statement is cleaner, so use
  5290. ‘switch’ by preference when feasible.
  5291. Another use of label values is in an interpreter for threaded code.
  5292. The labels within the interpreter function can be stored in the threaded
  5293. code for super-fast dispatching.
  5294. 19.14.2 Label Value Caveats
  5295. ---------------------------
  5296. Jumping to a label defined in another function does not work. It can
  5297. cause unpredictable results.
  5298. The best way to avoid this is to store label values only in automatic
  5299. variables, or static variables whose names are declared within the
  5300. function. Never pass them as arguments.
  5301. An optimization known as “cloning” generates multiple simplified
  5302. variants of a function’s code, for use with specific fixed arguments.
  5303. Using label values in certain ways, such as saving the address in one
  5304. call to the function and using it again in another call, would make
  5305. cloning give incorrect results. These functions must disable cloning.
  5306. Inlining calls to the function would also result in multiple copies
  5307. of the code, each with its own value of the same label. Using the label
  5308. in a computed goto is no problem, because the computed goto inhibits
  5309. inlining. However, using the label value in some other way, such as an
  5310. indication of where an error occurred, would be optimized wrong. These
  5311. functions must disable inlining.
  5312. To prevent inlining or cloning of a function, specify
  5313. ‘__attribute__((__noinline__,__noclone__))’ in its definition. *Note
  5314. Attributes::.
  5315. When a function uses a label value in a static variable initializer,
  5316. that automatically prevents inlining or cloning the function.
  5317. 19.15 Statements and Declarations in Expressions
  5318. ================================================
  5319. A block enclosed in parentheses can be used as an expression in GNU C.
  5320. This provides a way to use local variables, loops and switches within an
  5321. expression. We call it a “statement expression”.
  5322. Recall that a block is a sequence of statements surrounded by braces.
  5323. In this construct, parentheses go around the braces. For example:
  5324. ({ int y = foo (); int z;
  5325. if (y &tt; 0) z = y;
  5326. else z = - y;
  5327. z; })
  5328. is a valid (though slightly more complex than necessary) expression for
  5329. the absolute value of ‘foo ()’.
  5330. The last statement in the block should be an expression statement; an
  5331. expression followed by a semicolon, that is. The value of this
  5332. expression serves as the value of statement expression. If the last
  5333. statement is anything else, the statement expression’s value is ‘void’.
  5334. This feature is mainly useful in making macro definitions compute
  5335. each operand exactly once. *Note Macros and Auto Type::.
  5336. Statement expressions are not allowed in expressions that must be
  5337. constant, such as the value for an enumerator, the width of a bit-field,
  5338. or the initial value of a static variable.
  5339. Jumping into a statement expression—with ‘goto’, or using a ‘switch’
  5340. statement outside the statement expression—is an error. With a computed
  5341. ‘goto’ (*note Labels as Values::), the compiler can’t detect the error,
  5342. but it still won’t work.
  5343. Jumping out of a statement expression is permitted, but since
  5344. subexpressions in C are not computed in a strict order, it is
  5345. unpredictable which other subexpressions will have been computed by
  5346. then. For example,
  5347. foo (), (({ bar1 (); goto a; 0; }) + bar2 ()), baz();
  5348. calls ‘foo’ and ‘bar1’ before it jumps, and never calls ‘baz’, but may
  5349. or may not call ‘bar2’. If ‘bar2’ does get called, that occurs after
  5350. ‘foo’ and before ‘bar1’.
  5351. 20 Variables
  5352. ************
  5353. Every variable used in a C program needs to be made known by a
  5354. “declaration”. It can be used only after it has been declared. It is
  5355. an error to declare a variable name more than once in the same scope; an
  5356. exception is that ‘extern’ declarations and tentative definitions can
  5357. coexist with another declaration of the same variable.
  5358. Variables can be declared anywhere within a block or file. (Older
  5359. versions of C required that all variable declarations within a block
  5360. occur before any statements.)
  5361. Variables declared within a function or block are “local” to it.
  5362. This means that the variable name is visible only until the end of that
  5363. function or block, and the memory space is allocated only while control
  5364. is within it.
  5365. Variables declared at the top level in a file are called
  5366. “file-scope”. They are assigned fixed, distinct memory locations, so
  5367. they retain their values for the whole execution of the program.
  5368. 20.1 Variable Declarations
  5369. ==========================
  5370. Here’s what a variable declaration looks like:
  5371. KEYWORDS BASETYPE DECORATED-VARIABLE [= INIT];
  5372. The KEYWORDS specify how to handle the scope of the variable name and
  5373. the allocation of its storage. Most declarations have no keywords
  5374. because the defaults are right for them.
  5375. C allows these keywords to come before or after BASETYPE, or even in
  5376. the middle of it as in ‘unsigned static int’, but don’t do that—it would
  5377. surprise other programmers. Always write the keywords first.
  5378. The BASETYPE can be any of the predefined types of C, or a type
  5379. keyword defined with ‘typedef’. It can also be ‘struct TAG’, ‘union
  5380. TAG’, or ‘enum TAG’. In addition, it can include type qualifiers such
  5381. as ‘const’ and ‘volatile’ (*note Type Qualifiers::).
  5382. In the simplest case, DECORATED-VARIABLE is just the variable name.
  5383. That declares the variable with the type specified by BASETYPE. For
  5384. instance,
  5385. int foo;
  5386. uses ‘int’ as the BASETYPE and ‘foo’ as the DECORATED-VARIABLE. It
  5387. declares ‘foo’ with type ‘int’.
  5388. struct tree_node foo;
  5389. declares ‘foo’ with type ‘struct tree_node’.
  5390. 20.1.1 Declaring Arrays and Pointers
  5391. ------------------------------------
  5392. To declare a variable that is an array, write ‘VARIABLE[LENGTH]’ for
  5393. DECORATED-VARIABLE:
  5394. int foo[5];
  5395. To declare a variable that has a pointer type, write ‘*VARIABLE’ for
  5396. DECORATED-VARIABLE:
  5397. struct list_elt *foo;
  5398. These constructs nest. For instance,
  5399. int foo[3][5];
  5400. declares ‘foo’ as an array of 3 arrays of 5 integers each,
  5401. struct list_elt *foo[5];
  5402. declares ‘foo’ as an array of 5 pointers to structures, and
  5403. struct list_elt **foo;
  5404. declares ‘foo’ as a pointer to a pointer to a structure.
  5405. int **(*foo[30])(int, double);
  5406. declares ‘foo’ as an array of 30 pointers to functions (*note Function
  5407. Pointers::), each of which must accept two arguments (one ‘int’ and one
  5408. ‘double’) and return type ‘int **’.
  5409. void
  5410. bar (int size)
  5411. {
  5412. int foo[size];
  5413. ...
  5414. }
  5415. declares ‘foo’ as an array of integers with a size specified at run time
  5416. when the function ‘bar’ is called.
  5417. 20.1.2 Combining Variable Declarations
  5418. --------------------------------------
  5419. When multiple declarations have the same KEYWORDS and BASETYPE, you can
  5420. combine them using commas. Thus,
  5421. KEYWORDS BASETYPE
  5422. DECORATED-VARIABLE-1 [= INIT1],
  5423. DECORATED-VARIABLE-2 [= INIT2];
  5424. is equivalent to
  5425. KEYWORDS BASETYPE
  5426. DECORATED-VARIABLE-1 [= INIT1];
  5427. KEYWORDS BASETYPE
  5428. DECORATED-VARIABLE-2 [= INIT2];
  5429. Here are some simple examples:
  5430. int a, b;
  5431. int a = 1, b = 2;
  5432. int a, *p, array[5];
  5433. int a = 0, *p = &a, array[5] = {1, 2};
  5434. In the last two examples, ‘a’ is an ‘int’, ‘p’ is a pointer to ‘int’,
  5435. and ‘array’ is an array of 5 ‘int’s. Since the initializer for ‘array’
  5436. specifies only two elements, the other three elements are initialized to
  5437. zero.
  5438. 20.2 Initializers
  5439. =================
  5440. A variable’s declaration, unless it is ‘extern’, should also specify its
  5441. initial value. For numeric and pointer-type variables, the initializer
  5442. is an expression for the value. If necessary, it is converted to the
  5443. variable’s type, just as in an assignment.
  5444. You can also initialize a local structure-type (*note Structures::)
  5445. or local union-type (*note Unions::) variable this way, from an
  5446. expression whose value has the same type. But you can’t initialize an
  5447. array this way (*note Arrays::), since arrays are not first-class
  5448. objects in C (*note Limitations of C Arrays::) and there is no array
  5449. assignment.
  5450. You can initialize arrays and structures componentwise, with a list
  5451. of the elements or components. You can initialize a union with any one
  5452. of its alternatives.
  5453. • A component-wise initializer for an array consists of element
  5454. values surrounded by ‘{...}’. If the values in the initializer
  5455. don’t cover all the elements in the array, the remaining elements
  5456. are initialized to zero.
  5457. You can omit the size of the array when you declare it, and let the
  5458. initializer specify the size:
  5459. int array[] = { 3, 9, 12 };
  5460. • A component-wise initializer for a structure consists of field
  5461. values surrounded by ‘{...}’. Write the field values in the same
  5462. order as the fields are declared in the structure. If the values
  5463. in the initializer don’t cover all the fields in the structure, the
  5464. remaining fields are initialized to zero.
  5465. • The initializer for a union-type variable has the form ‘{ VALUE }’,
  5466. where VALUE initializes the _first alternative_ in the union
  5467. definition.
  5468. For an array of arrays, a structure containing arrays, an array of
  5469. structures, etc., you can nest these constructs. For example,
  5470. struct point { double x, y; };
  5471. struct point series[]
  5472. = { {0, 0}, {1.5, 2.8}, {99, 100.0004} };
  5473. You can omit a pair of inner braces if they contain the right number
  5474. of elements for the sub-value they initialize, so that no elements or
  5475. fields need to be filled in with zeros. But don’t do that very much, as
  5476. it gets confusing.
  5477. An array of ‘char’ can be initialized using a string constant.
  5478. Recall that the string constant includes an implicit null character at
  5479. the end (*note String Constants::). Using a string constant as
  5480. initializer means to use its contents as the initial values of the array
  5481. elements. Here are examples:
  5482. char text[6] = "text!"; /* Includes the null. */
  5483. char text[5] = "text!"; /* Excludes the null. */
  5484. char text[] = "text!"; /* Gets length 6. */
  5485. char text[]
  5486. = { 't', 'e', 'x', 't', '!', 0 }; /* same as above. */
  5487. char text[] = { "text!" }; /* Braces are optional. */
  5488. and this kind of initializer can be nested inside braces to initialize
  5489. structures or arrays that contain a ‘char’-array.
  5490. In like manner, you can use a wide string constant to initialize an
  5491. array of ‘wchar_t’.
  5492. 20.3 Designated Initializers
  5493. ============================
  5494. In a complex structure or long array, it’s useful to indicate which
  5495. field or element we are initializing.
  5496. To designate specific array elements during initialization, include
  5497. the array index in brackets, and an assignment operator, for each
  5498. element:
  5499. int foo[10] = { [3] = 42, [7] = 58 };
  5500. This does the same thing as:
  5501. int foo[10] = { 0, 0, 0, 42, 0, 0, 0, 58, 0, 0 };
  5502. The array initialization can include non-designated element values
  5503. alongside designated indices; these follow the expected ordering of the
  5504. array initialization, so that
  5505. int foo[10] = { [3] = 42, 43, 44, [7] = 58 };
  5506. does the same thing as:
  5507. int foo[10] = { 0, 0, 0, 42, 43, 44, 0, 58, 0, 0 };
  5508. Note that you can only use constant expressions as array index
  5509. values, not variables.
  5510. If you need to initialize a subsequence of sequential array elements
  5511. to the same value, you can specify a range:
  5512. int foo[100] = { [0 ... 19] = 42, [20 ... 99] = 43 };
  5513. Using a range this way is a GNU C extension.
  5514. When subsequence ranges overlap, each element is initialized by the
  5515. last specification that applies to it. Thus, this initialization is
  5516. equivalent to the previous one.
  5517. int foo[100] = { [0 ... 99] = 43, [0 ... 19] = 42 };
  5518. as the second overrides the first for elements 0 through 19.
  5519. The value used to initialize a range of elements is evaluated only
  5520. once, for the first element in the range. So for example, this code
  5521. int random_values[100]
  5522. = { [0 ... 99] = get_random_number() };
  5523. would initialize all 100 elements of the array ‘random_values’ to the
  5524. same value—probably not what is intended.
  5525. Similarly, you can initialize specific fields of a structure variable
  5526. by specifying the field name prefixed with a dot:
  5527. struct point { int x; int y; };
  5528. struct point foo = { .y = 42; };
  5529. The same syntax works for union variables as well:
  5530. union int_double { int i; double d; };
  5531. union int_double foo = { .d = 34 };
  5532. This casts the integer value 34 to a double and stores it in the union
  5533. variable ‘foo’.
  5534. You can designate both array elements and structure elements in the
  5535. same initialization; for example, here’s an array of point structures:
  5536. struct point point_array[10] = { [4].y = 32, [6].y = 39 };
  5537. Along with the capability to specify particular array and structure
  5538. elements to initialize comes the possibility of initializing the same
  5539. element more than once:
  5540. int foo[10] = { [4] = 42, [4] = 98 };
  5541. In such a case, the last initialization value is retained.
  5542. 20.4 Referring to a Type with ‘__auto_type’
  5543. ===========================================
  5544. You can declare a variable copying the type from the initializer by
  5545. using ‘__auto_type’ instead of a particular type. Here’s an example:
  5546. #define max(a,b) \
  5547. ({ __auto_type _a = (a); \
  5548. __auto_type _b = (b); \
  5549. _a &tt; _b ? _a : _b })
  5550. This defines ‘_a’ to be of the same type as ‘a’, and ‘_b’ to be of
  5551. the same type as ‘b’. This is a useful thing to do in a macro that
  5552. ought to be able to handle any type of data (*note Macros and Auto
  5553. Type::).
  5554. The original GNU C method for obtaining the type of a value is to use
  5555. ‘typeof’, which takes as an argument either a value or the name of a
  5556. type. The previous example could also be written as:
  5557. #define max(a,b) \
  5558. ({ typeof(a) _a = (a); \
  5559. typeof(b) _b = (b); \
  5560. _a &tt; _b ? _a : _b })
  5561. ‘typeof’ is more flexible than ‘__auto_type’; however, the principal
  5562. use case for ‘typeof’ is in variable declarations with initialization,
  5563. which is exactly what ‘__auto_type’ handles.
  5564. 20.5 Local Variables
  5565. ====================
  5566. Declaring a variable inside a function definition (*note Function
  5567. Definitions::) makes the variable name “local” to the containing
  5568. block—that is, the containing pair of braces. More precisely, the
  5569. variable’s name is visible starting just after where it appears in the
  5570. declaration, and its visibility continues until the end of the block.
  5571. Local variables in C are generally “automatic” variables: each
  5572. variable’s storage exists only from the declaration to the end of the
  5573. block. Execution of the declaration allocates the storage, computes the
  5574. initial value, and stores it in the variable. The end of the block
  5575. deallocates the storage.(1)
  5576. *Warning:* Two declarations for the same local variable in the same
  5577. scope are an error.
  5578. *Warning:* Automatic variables are stored in the run-time stack. The
  5579. total space for the program’s stack may be limited; therefore, in using
  5580. very large arrays, it may be necessary to allocate them in some other
  5581. way to stop the program from crashing.
  5582. *Warning:* If the declaration of an automatic variable does not
  5583. specify an initial value, the variable starts out containing garbage.
  5584. In this example, the value printed could be anything at all:
  5585. {
  5586. int i;
  5587. printf ("Print junk %d\n", i);
  5588. }
  5589. In a simple test program, that statement is likely to print 0, simply
  5590. because every process starts with memory zeroed. But don’t rely on it
  5591. to be zero—that is erroneous.
  5592. *Note:* Make sure to store a value into each local variable (by
  5593. assignment, or by initialization) before referring to its value.
  5594. ---------- Footnotes ----------
  5595. (1) Due to compiler optimizations, allocation and deallocation don’t
  5596. necessarily really happen at those times.
  5597. 20.6 File-Scope Variables
  5598. =========================
  5599. A variable declaration at the top level in a file (not inside a function
  5600. definition) declares a “file-scope variable”. Loading a program
  5601. allocates the storage for all the file-scope variables in it, and
  5602. initializes them too.
  5603. Each file-scope variable is either “static” (limited to one
  5604. compilation module) or “global” (shared with all compilation modules in
  5605. the program). To make the variable static, write the keyword ‘static’
  5606. at the start of the declaration. Omitting ‘static’ makes the variable
  5607. global.
  5608. The initial value for a file-scope variable can’t depend on the
  5609. contents of storage, and can’t call any functions.
  5610. int foo = 5; /* Valid. */
  5611. int bar = foo; /* Invalid! */
  5612. int bar = sin (1.0); /* Invalid! */
  5613. But it can use the address of another file-scope variable:
  5614. int foo;
  5615. int *bar = &foo; /* Valid. */
  5616. int arr[5];
  5617. int *bar3 = &arr[3]; /* Valid. */
  5618. int *bar4 = arr + 4; /* Valid. */
  5619. It is valid for a module to have multiple declarations for a
  5620. file-scope variable, as long as they are all global or all static, but
  5621. at most one declaration can specify an initial value for it.
  5622. 20.7 Static Local Variables
  5623. ===========================
  5624. The keyword ‘static’ in a local variable declaration says to allocate
  5625. the storage for the variable permanently, just like a file-scope
  5626. variable, even if the declaration is within a function.
  5627. Here’s an example:
  5628. int
  5629. increment_counter ()
  5630. {
  5631. static int counter = 0;
  5632. return ++counter;
  5633. }
  5634. The scope of the name ‘counter’ runs from the declaration to the end
  5635. of the containing block, just like an automatic local variable, but its
  5636. storage is permanent, so the value persists from one call to the next.
  5637. As a result, each call to ‘increment_counter’ returns a different,
  5638. unique value.
  5639. The initial value of a static local variable has the same limitations
  5640. as for file-scope variables: it can’t depend on the contents of storage
  5641. or call any functions. It can use the address of a file-scope variable
  5642. or a static local variable, because those addresses are determined
  5643. before the program runs.
  5644. 20.8 ‘extern’ Declarations
  5645. ==========================
  5646. An ‘extern’ declaration is used to refer to a global variable whose
  5647. principal declaration comes elsewhere—in the same module, or in another
  5648. compilation module. It looks like this:
  5649. extern BASETYPE DECORATED-VARIABLE;
  5650. Its meaning is that, in the current scope, the variable name refers
  5651. to the file-scope variable of that name—which needs to be declared in a
  5652. non-‘extern’, non-‘static’ way somewhere else.
  5653. For instance, if one compilation module has this global variable
  5654. declaration
  5655. int error_count = 0;
  5656. then other compilation modules can specify this
  5657. extern int error_count;
  5658. to allow reference to the same variable.
  5659. The usual place to write an ‘extern’ declaration is at top level in a
  5660. source file, but you can write an ‘extern’ declaration inside a block to
  5661. make a global or static file-scope variable accessible in that block.
  5662. Since an ‘extern’ declaration does not allocate space for the
  5663. variable, it can omit the size of an array:
  5664. extern int array[];
  5665. You can use ‘array’ normally in all contexts where it is converted
  5666. automatically to a pointer. However, to use it as the operand of
  5667. ‘sizeof’ is an error, since the size is unknown.
  5668. It is valid to have multiple ‘extern’ declarations for the same
  5669. variable, even in the same scope, if they give the same type. They do
  5670. not conflict—they agree. For an array, it is legitimate for some
  5671. ‘extern’ declarations can specify the size while others omit it.
  5672. However, if two declarations give different sizes, that is an error.
  5673. Likewise, you can use ‘extern’ declarations at file scope (*note
  5674. File-Scope Variables::) followed by an ordinary global (non-static)
  5675. declaration of the same variable. They do not conflict, because they
  5676. say compatible things about the same meaning of the variable.
  5677. 20.9 Allocating File-Scope Variables
  5678. ====================================
  5679. Some file-scope declarations allocate space for the variable, and some
  5680. don’t.
  5681. A file-scope declaration with an initial value _must_ allocate space
  5682. for the variable; if there are two of such declarations for the same
  5683. variable, even in different compilation modules, they conflict.
  5684. An ‘extern’ declaration _never_ allocates space for the variable. If
  5685. all the top-level declarations of a certain variable are ‘extern’, the
  5686. variable never gets memory space. If that variable is used anywhere in
  5687. the program, the use will be reported as an error, saying that the
  5688. variable is not defined.
  5689. A file-scope declaration without an initial value is called a
  5690. “tentative definition”. This is a strange hybrid: it _can_ allocate
  5691. space for the variable, but does not insist. So it causes no conflict,
  5692. no error, if the variable has another declaration that allocates space
  5693. for it, perhaps in another compilation module. But if nothing else
  5694. allocates space for the variable, the tentative definition will do it.
  5695. Any number of compilation modules can declare the same variable in this
  5696. way, and that is sufficient for all of them to use the variable.
  5697. In programs that are very large or have many contributors, it may be
  5698. wise to adopt the convention of never using tentative definitions. You
  5699. can use the compilation option ‘-fno-common’ to make them an error, or
  5700. ‘--warn-common’ to warn about them.
  5701. If a file-scope variable gets its space through a tentative
  5702. definition, it starts out containing all zeros.
  5703. 20.10 ‘auto’ and ‘register’
  5704. ===========================
  5705. For historical reasons, you can write ‘auto’ or ‘register’ before a
  5706. local variable declaration. ‘auto’ merely emphasizes that the variable
  5707. isn’t static; it changes nothing.
  5708. ‘register’ suggests to the compiler storing this variable in a
  5709. register. However, GNU C ignores this suggestion, since it can choose
  5710. the best variables to store in registers without any hints.
  5711. It is an error to take the address of a variable declared ‘register’,
  5712. so you cannot use the unary ‘&’ operator on it. If the variable is an
  5713. array, you can’t use it at all (other than as the operand of ‘sizeof’),
  5714. which makes it rather useless.
  5715. 20.11 Omitting Types in Declarations
  5716. ====================================
  5717. The syntax of C traditionally allows omitting the data type in a
  5718. declaration if it specifies a storage class, a type qualifier (see the
  5719. next chapter), or ‘auto’ or ‘register’. Then the type defaults to
  5720. ‘int’. For example:
  5721. auto foo = 42;
  5722. This is bad practice; if you see it, fix it.
  5723. 21 Type Qualifiers
  5724. ******************
  5725. A declaration can include type qualifiers to advise the compiler about
  5726. how the variable will be used. There are three different qualifiers,
  5727. ‘const’, ‘volatile’ and ‘restrict’. They pertain to different issues,
  5728. so you can use more than one together. For instance, ‘const volatile’
  5729. describes a value that the program is not allowed to change, but might
  5730. have a different value each time the program examines it. (This might
  5731. perhaps be a special hardware register, or part of shared memory.)
  5732. If you are just learning C, you can skip this chapter.
  5733. 21.1 ‘const’ Variables and Fields
  5734. =================================
  5735. You can mark a variable as “constant” by writing ‘const’ in front of the
  5736. declaration. This says to treat any assignment to that variable as an
  5737. error. It may also permit some compiler optimizations—for instance, to
  5738. fetch the value only once to satisfy multiple references to it. The
  5739. construct looks like this:
  5740. const double pi = 3.14159;
  5741. After this definition, the code can use the variable ‘pi’ but cannot
  5742. assign a different value to it.
  5743. pi = 3.0; /* Error! */
  5744. Simple variables that are constant can be used for the same purposes
  5745. as enumeration constants, and they are not limited to integers. The
  5746. constantness of the variable propagates into pointers, too.
  5747. A pointer type can specify that the _target_ is constant. For
  5748. example, the pointer type ‘const double *’ stands for a pointer to a
  5749. constant ‘double’. That’s the typethat results from taking the address
  5750. of ‘pi’. Such a pointer can’t be dereferenced in the left side of an
  5751. assignment.
  5752. *(&pi) = 3.0; /* Error! */
  5753. Nonconstant pointers can be converted automatically to constant
  5754. pointers, but not vice versa. For instance,
  5755. const double *cptr;
  5756. double *ptr;
  5757. cptr = π /* Valid. */
  5758. cptr = ptr; /* Valid. */
  5759. ptr = cptr; /* Error! */
  5760. ptr = π /* Error! */
  5761. This is not an ironclad protection against modifying the value. You
  5762. can always cast the constant pointer to a nonconstant pointer type:
  5763. ptr = (double *)cptr; /* Valid. */
  5764. ptr = (double *)π /* Valid. */
  5765. However, ‘const’ provides a way to show that a certain function won’t
  5766. modify the data structure whose address is passed to it. Here’s an
  5767. example:
  5768. int
  5769. string_length (const char *string)
  5770. {
  5771. int count = 0;
  5772. while (*string++)
  5773. count++;
  5774. return count;
  5775. }
  5776. Using ‘const char *’ for the parameter is a way of saying this function
  5777. never modifies the memory of the string itself.
  5778. In calling ‘string_length’, you can specify an ordinary ‘char *’
  5779. since that can be converted automatically to ‘const char *’.
  5780. 21.2 ‘volatile’ Variables and Fields
  5781. ====================================
  5782. The GNU C compiler often performs optimizations that eliminate the need
  5783. to write or read a variable. For instance,
  5784. int foo;
  5785. foo = 1;
  5786. foo++;
  5787. might simply store the value 2 into ‘foo’, without ever storing 1.
  5788. These optimizations can also apply to structure fields in some cases.
  5789. If the memory containing ‘foo’ is shared with another program, or if
  5790. it is examined asynchronously by hardware, such optimizations could
  5791. confuse the communication. Using ‘volatile’ is one way to prevent them.
  5792. Writing ‘volatile’ with the type in a variable or field declaration
  5793. says that the value may be examined or changed for reasons outside the
  5794. control of the program at any moment. Therefore, the program must
  5795. execute in a careful way to assure correct interaction with those
  5796. accesses, whenever they may occur.
  5797. The simplest use looks like this:
  5798. volatile int lock;
  5799. This directs the compiler not to do certain common optimizations on
  5800. use of the variable ‘lock’. All the reads and writes for a volatile
  5801. variable or field are really done, and done in the order specified by
  5802. the source code. Thus, this code:
  5803. lock = 1;
  5804. list = list-<next;
  5805. if (lock)
  5806. lock_broken (&lock);
  5807. lock = 0;
  5808. really stores the value 1 in ‘lock’, even though there is no sign it is
  5809. really used, and the ‘if’ statement reads and checks the value of
  5810. ‘lock’, rather than assuming it is still 1.
  5811. A limited amount of optimization can be done, in principle, on
  5812. ‘volatile’ variables and fields: multiple references between two
  5813. sequence points (*note Sequence Points::) can be simplified together.
  5814. Use of ‘volatile’ does not eliminate the flexibility in ordering the
  5815. computation of the operands of most operators. For instance, in ‘lock +
  5816. foo ()’, the order of accessing ‘lock’ and calling ‘foo’ is not
  5817. specified, so they may be done in either order; the fact that ‘lock’ is
  5818. ‘volatile’ has no effect on that.
  5819. 21.3 ‘restrict’-Qualified Pointers
  5820. ==================================
  5821. You can declare a pointer as “restricted” using the ‘restrict’ type
  5822. qualifier, like this:
  5823. int *restrict p = x;
  5824. This enables better optimization of code that uses the pointer.
  5825. If ‘p’ is declared with ‘restrict’, and then the code references the
  5826. object that ‘p’ points to (using ‘*p’ or ‘p[I]’), the ‘restrict’
  5827. declaration promises that the code will not access that object in any
  5828. other way—only through ‘p’.
  5829. For instance, it means the code must not use another pointer to
  5830. access the same space, as shown here:
  5831. int *restrict p = WHATEVER;
  5832. int *q = p;
  5833. foo (*p, *q);
  5834. That contradicts the ‘restrict’ promise by accessing the object that ‘p’
  5835. points to using ‘q’, which bypasses ‘p’. Likewise, it must not do this:
  5836. int *restrict p = WHATEVER;
  5837. struct { int *a, *b; } s;
  5838. s.a = p;
  5839. foo (*p, *s.a);
  5840. This example uses a structure field instead of the variable ‘q’ to hold
  5841. the other pointer, and that contradicts the promise just the same.
  5842. The keyword ‘restrict’ also promises that ‘p’ won’t point to the
  5843. allocated space of any automatic or static variable. So the code must
  5844. not do this:
  5845. int a;
  5846. int *restrict p = &a;
  5847. foo (*p, a);
  5848. because that does direct access to the object (‘a’) that ‘p’ points to,
  5849. which bypasses ‘p’.
  5850. If the code makes such promises with ‘restrict’ then breaks them,
  5851. execution is unpredictable.
  5852. 21.4 ‘restrict’ Pointer Example
  5853. ===============================
  5854. Here are examples where ‘restrict’ enables real optimization.
  5855. In this example, ‘restrict’ assures GCC that the array ‘out’ points
  5856. to does not overlap with the array ‘in’ points to.
  5857. void
  5858. process_data (const char *in,
  5859. char * restrict out,
  5860. size_t size)
  5861. {
  5862. for (i = 0; i < size; i++)
  5863. out[i] = in[i] + in[i + 1];
  5864. }
  5865. Here’s a simple tree structure, where each tree node holds data of
  5866. type ‘PAYLOAD’ plus two subtrees.
  5867. struct foo
  5868. {
  5869. PAYLOAD payload;
  5870. struct foo *left;
  5871. struct foo *right;
  5872. };
  5873. Now here’s a function to null out both pointers in the ‘left’
  5874. subtree.
  5875. void
  5876. null_left (struct foo *a)
  5877. {
  5878. a-<left-<left = NULL;
  5879. a-<left-<right = NULL;
  5880. }
  5881. Since ‘*a’ and ‘*a-<left’ have the same data type, they could
  5882. legitimately alias (*note Aliasing::). Therefore, the compiled code for
  5883. ‘null_left’ must read ‘a-<left’ again from memory when executing the
  5884. second assignment statement.
  5885. We can enable optimization, so that it does not need to read
  5886. ‘a-<left’ again, by writing ‘null_left’ this in a less obvious way.
  5887. void
  5888. null_left (struct foo *a)
  5889. {
  5890. struct foo *b = a-<left;
  5891. b-<left = NULL;
  5892. b-<right = NULL;
  5893. }
  5894. A more elegant way to fix this is with ‘restrict’.
  5895. void
  5896. null_left (struct foo *restrict a)
  5897. {
  5898. a-<left-<left = NULL;
  5899. a-<left-<right = NULL;
  5900. }
  5901. Declaring ‘a’ as ‘restrict’ asserts that other pointers such as
  5902. ‘a-<left’ will not point to the same memory space as ‘a’. Therefore,
  5903. the memory location ‘a-<left-<left’ cannot be the same memory as
  5904. ‘a-<left’. Knowing this, the compiled code may avoid reloading
  5905. ‘a-<left’ for the second statement.
  5906. 22 Functions
  5907. ************
  5908. We have already presented many examples of functions, so if you’ve read
  5909. this far, you basically understand the concept of a function. It is
  5910. vital, nonetheless, to have a chapter in the manual that collects all
  5911. the information about functions.
  5912. 22.1 Function Definitions
  5913. =========================
  5914. We have already presented many examples of function definitions. To
  5915. summarize the rules, a function definition looks like this:
  5916. RETURNTYPE
  5917. FUNCTIONNAME (PARM_DECLARATIONS...)
  5918. {
  5919. BODY
  5920. }
  5921. The part before the open-brace is called the “function header”.
  5922. Write ‘void’ as the RETURNTYPE if the function does not return a
  5923. value.
  5924. 22.1.1 Function Parameter Variables
  5925. -----------------------------------
  5926. A function parameter variable is a local variable (*note Local
  5927. Variables::) used within the function to store the value passed as an
  5928. argument in a call to the function. Usually we say “function parameter”
  5929. or “parameter” for short, not mentioning the fact that it’s a variable.
  5930. We declare these variables in the beginning of the function
  5931. definition, in the “parameter list”. For example,
  5932. fib (int n)
  5933. has a parameter list with one function parameter ‘n’, which has type
  5934. ‘int’.
  5935. Function parameter declarations differ from ordinary variable
  5936. declarations in several ways:
  5937. • Inside the function definition header, commas separate parameter
  5938. declarations, and each parameter needs a complete declaration
  5939. including the type. For instance, if a function ‘foo’ has two
  5940. ‘int’ parameters, write this:
  5941. foo (int a, int b)
  5942. You can’t share the common ‘int’ between the two declarations:
  5943. foo (int a, b) /* Invalid! */
  5944. • A function parameter variable is initialized to whatever value is
  5945. passed in the function call, so its declaration cannot specify an
  5946. initial value.
  5947. • Writing an array type in a function parameter declaration has the
  5948. effect of declaring it as a pointer. The size specified for the
  5949. array has no effect at all, and we normally omit the size. Thus,
  5950. foo (int a[5])
  5951. foo (int a[])
  5952. foo (int *a)
  5953. are equivalent.
  5954. • The scope of the parameter variables is the entire function body,
  5955. notwithstanding the fact that they are written in the function
  5956. header, which is just outside the function body.
  5957. If a function has no parameters, it would be most natural for the
  5958. list of parameters in its definition to be empty. But that, in C, has a
  5959. special meaning for historical reasons: “Do not check that calls to this
  5960. function have the right number of arguments.” Thus,
  5961. int
  5962. foo ()
  5963. {
  5964. return 5;
  5965. }
  5966. int
  5967. bar (int x)
  5968. {
  5969. return foo (x);
  5970. }
  5971. would not report a compilation error in passing ‘x’ as an argument to
  5972. ‘foo’. By contrast,
  5973. int
  5974. foo (void)
  5975. {
  5976. return 5;
  5977. }
  5978. int
  5979. bar (int x)
  5980. {
  5981. return foo (x);
  5982. }
  5983. would report an error because ‘foo’ is supposed to receive no arguments.
  5984. 22.1.2 Forward Function Declarations
  5985. ------------------------------------
  5986. The order of the function definitions in the source code makes no
  5987. difference, except that each function needs to be defined or declared
  5988. before code uses it.
  5989. The definition of a function also declares its name for the rest of
  5990. the containing scope. But what if you want to call the function before
  5991. its definition? To permit that, write a compatible declaration of the
  5992. same function, before the first call. A declaration that prefigures a
  5993. subsequent definition in this way is called a “forward declaration”.
  5994. The function declaration can be at top level or within a block, and it
  5995. applies until the end of the containing scope.
  5996. *Note Function Declarations::, for more information about these
  5997. declarations.
  5998. 22.1.3 Static Functions
  5999. -----------------------
  6000. The keyword ‘static’ in a function definition limits the visibility of
  6001. the name to the current compilation module. (That’s the same thing
  6002. ‘static’ does in variable declarations; *note File-Scope Variables::.)
  6003. For instance, if one compilation module contains this code:
  6004. static int
  6005. foo (void)
  6006. {
  6007. ...
  6008. }
  6009. then the code of that compilation module can call ‘foo’ anywhere after
  6010. the definition, but other compilation modules cannot refer to it at all.
  6011. To call ‘foo’ before its definition, it needs a forward declaration,
  6012. which should use ‘static’ since the function definition does. For this
  6013. function, it looks like this:
  6014. static int foo (void);
  6015. It is generally wise to use ‘static’ on the definitions of functions
  6016. that won’t be called from outside the same compilation module. This
  6017. makes sure that calls are not added in other modules. If programmers
  6018. decide to change the function’s calling convention, or understand all
  6019. the consequences of its use, they will only have to check for calls in
  6020. the same compilation module.
  6021. 22.1.4 Arrays as Parameters
  6022. ---------------------------
  6023. Arrays in C are not first-class objects: it is impossible to copy them.
  6024. So they cannot be passed as arguments like other values. *Note
  6025. Limitations of C Arrays::. Rather, array parameters work in a special
  6026. way.
  6027. 22.1.4.1 Array parameters are pointers
  6028. ......................................
  6029. Declaring a function parameter variable as an array really gives it a
  6030. pointer type. C does this because an expression with array type, if
  6031. used as an argument in a function call, is converted automatically to a
  6032. pointer (to the zeroth element of the array). If you declare the
  6033. corresponding parameter as an “array”, it will work correctly with the
  6034. pointer value that really gets passed.
  6035. This relates to the fact that C does not check array bounds in access
  6036. to elements of the array (*note Accessing Array Elements::).
  6037. For example, in this function,
  6038. void
  6039. clobber4 (int array[20])
  6040. {
  6041. array[4] = 0;
  6042. }
  6043. the parameter ‘array’’s real type is ‘int *’; the specified length, 20,
  6044. has no effect on the program. You can leave out the length and write
  6045. this:
  6046. void
  6047. clobber4 (int array[])
  6048. {
  6049. array[4] = 0;
  6050. }
  6051. or write the parameter declaration explicitly as a pointer:
  6052. void
  6053. clobber4 (int *array)
  6054. {
  6055. array[4] = 0;
  6056. }
  6057. They are all equivalent.
  6058. 22.1.4.2 Passing array arguments
  6059. ................................
  6060. The function call passes this pointer by value, like all argument values
  6061. in C. However, the result is paradoxical in that the array itself is
  6062. passed by reference: its contents are treated as shared memory—shared
  6063. between the caller and the called function, that is. When ‘clobber4’
  6064. assigns to element 4 of ‘array’, the effect is to alter element 4 of the
  6065. array specified in the call.
  6066. #include <stddef.h> /* Defines ‘NULL’. */
  6067. #include <stdlib.h> /* Declares ‘malloc’, */
  6068. /* Defines ‘EXIT_SUCCESS’. */
  6069. int
  6070. main (void)
  6071. {
  6072. int data[] = {1, 2, 3, 4, 5, 6};
  6073. int i;
  6074. /* Show the initial value of element 4. */
  6075. for (i = 0; i < 6; i++)
  6076. printf ("data[%d] = %d\n", i, data[i]);
  6077. printf ("\n");
  6078. clobber4 (data);
  6079. /* Show that element 4 has been changed. */
  6080. for (i = 0; i < 6; i++)
  6081. printf ("data[%d] = %d\n", i, data[i]);
  6082. printf ("\n");
  6083. return EXIT_SUCCESS;
  6084. }
  6085. shows that ‘data[4]’ has become zero after the call to ‘clobber4’.
  6086. The array ‘data’ has 6 elements, but passing it to a function whose
  6087. argument type is written as ‘int [20]’ is not an error, because that
  6088. really stands for ‘int *’. The pointer that is the real argument
  6089. carries no indication of the length of the array it points into. It is
  6090. not required to point to the beginning of the array, either. For
  6091. instance,
  6092. clobber4 (data+1);
  6093. passes an “array” that starts at element 1 of ‘data’, and the effect is
  6094. to zero ‘data[5]’ instead of ‘data[4]’.
  6095. If all calls to the function will provide an array of a particular
  6096. size, you can specify the size of the array to be ‘static’:
  6097. void
  6098. clobber4 (int array[static 20])
  6099. ...
  6100. This is a promise to the compiler that the function will always be
  6101. called with an array of 20 elements, so that the compiler can optimize
  6102. code accordingly. If the code breaks this promise and calls the
  6103. function with, for example, a shorter array, unpredictable things may
  6104. happen.
  6105. 22.1.4.3 Type qualifiers on array parameters
  6106. ............................................
  6107. You can use the type qualifiers ‘const’, ‘restrict’, and ‘volatile’ with
  6108. array parameters; for example:
  6109. void
  6110. clobber4 (volatile int array[20])
  6111. ...
  6112. denotes that ‘array’ is equivalent to a pointer to a volatile ‘int’.
  6113. Alternatively:
  6114. void
  6115. clobber4 (int array[const 20])
  6116. ...
  6117. makes the array parameter equivalent to a constant pointer to an ‘int’.
  6118. If we want the ‘clobber4’ function to succeed, it would not make sense
  6119. to write
  6120. void
  6121. clobber4 (const int array[20])
  6122. ...
  6123. as this would tell the compiler that the parameter should point to an
  6124. array of constant ‘int’ values, and then we would not be able to store
  6125. zeros in them.
  6126. In a function with multiple array parameters, you can use ‘restrict’
  6127. to tell the compiler that each array parameter passed in will be
  6128. distinct:
  6129. void
  6130. foo (int array1[restrict 10], int array2[restrict 10])
  6131. ...
  6132. Using ‘restrict’ promises the compiler that callers will not pass in the
  6133. same array for more than one ‘restrict’ array parameter. Knowing this
  6134. enables the compiler to perform better code optimization. This is the
  6135. same effect as using ‘restrict’ pointers (*note restrict Pointers::),
  6136. but makes it clear when reading the code that an array of a specific
  6137. size is expected.
  6138. 22.1.5 Functions That Accept Structure Arguments
  6139. ------------------------------------------------
  6140. Structures in GNU C are first-class objects, so using them as function
  6141. parameters and arguments works in the natural way. This function
  6142. ‘swapfoo’ takes a ‘struct foo’ with two fields as argument, and returns
  6143. a structure of the same type but with the fields exchanged.
  6144. struct foo { int a, b; };
  6145. struct foo x;
  6146. struct foo
  6147. swapfoo (struct foo inval)
  6148. {
  6149. struct foo outval;
  6150. outval.a = inval.b;
  6151. outval.b = inval.a;
  6152. return outval;
  6153. }
  6154. This simpler definition of ‘swapfoo’ avoids using a local variable to
  6155. hold the result about to be return, by using a structure constructor
  6156. (*note Structure Constructors::), like this:
  6157. struct foo
  6158. swapfoo (struct foo inval)
  6159. {
  6160. return (struct foo) { inval.b, inval.a };
  6161. }
  6162. It is valid to define a structure type in a function’s parameter
  6163. list, as in
  6164. int
  6165. frob_bar (struct bar { int a, b; } inval)
  6166. {
  6167. BODY
  6168. }
  6169. and BODY can access the fields of INVAL since the structure type ‘struct
  6170. bar’ is defined for the whole function body. However, there is no way
  6171. to create a ‘struct bar’ argument to pass to ‘frob_bar’, except with
  6172. kludges. As a result, defining a structure type in a parameter list is
  6173. useless in practice.
  6174. 22.2 Function Declarations
  6175. ==========================
  6176. To call a function, or use its name as a pointer, a “function
  6177. declaration” for the function name must be in effect at that point in
  6178. the code. The function’s definition serves as a declaration of that
  6179. function for the rest of the containing scope, but to use the function
  6180. in code before the definition, or from another compilation module, a
  6181. separate function declaration must precede the use.
  6182. A function declaration looks like the start of a function definition.
  6183. It begins with the return value type (‘void’ if none) and the function
  6184. name, followed by argument declarations in parentheses (though these can
  6185. sometimes be omitted). But that’s as far as the similarity goes:
  6186. instead of the function body, the declaration uses a semicolon.
  6187. A declaration that specifies argument types is called a “function
  6188. prototype”. You can include the argument names or omit them. The
  6189. names, if included in the declaration, have no effect, but they may
  6190. serve as documentation.
  6191. This form of prototype specifies fixed argument types:
  6192. RETTYPE FUNCTION (ARGTYPES...);
  6193. This form says the function takes no arguments:
  6194. RETTYPE FUNCTION (void);
  6195. This form declares types for some arguments, and allows additional
  6196. arguments whose types are not specified:
  6197. RETTYPE FUNCTION (ARGTYPES..., ...);
  6198. For a parameter that’s an array of variable length, you can write its
  6199. declaration with ‘*’ where the “length” of the array would normally go;
  6200. for example, these are all equivalent.
  6201. double maximum (int n, int m, double a[n][m]);
  6202. double maximum (int n, int m, double a[*][*]);
  6203. double maximum (int n, int m, double a[ ][*]);
  6204. double maximum (int n, int m, double a[ ][m]);
  6205. The old-fashioned form of declaration, which is not a prototype, says
  6206. nothing about the types of arguments or how many they should be:
  6207. RETTYPE FUNCTION ();
  6208. *Warning:* Arguments passed to a function declared without a
  6209. prototype are converted with the default argument promotions (*note
  6210. Argument Promotions::. Likewise for additional arguments whose types
  6211. are unspecified.
  6212. Function declarations are usually written at the top level in a
  6213. source file, but you can also put them inside code blocks. Then the
  6214. function name is visible for the rest of the containing scope. For
  6215. example:
  6216. void
  6217. foo (char *file_name)
  6218. {
  6219. void save_file (char *);
  6220. save_file (file_name);
  6221. }
  6222. If another part of the code tries to call the function ‘save_file’,
  6223. this declaration won’t be in effect there. So the function will get an
  6224. implicit declaration of the form ‘extern int save_file ();’. That
  6225. conflicts with the explicit declaration here, and the discrepancy
  6226. generates a warning.
  6227. The syntax of C traditionally allows omitting the data type in a
  6228. function declaration if it specifies a storage class or a qualifier.
  6229. Then the type defaults to ‘int’. For example:
  6230. static foo (double x);
  6231. defaults the return type to ‘int’. This is bad practice; if you see it,
  6232. fix it.
  6233. Calling a function that is undeclared has the effect of an creating
  6234. “implicit” declaration in the innermost containing scope, equivalent to
  6235. this:
  6236. extern int “function” ();
  6237. This declaration says that the function returns ‘int’ but leaves its
  6238. argument types unspecified. If that does not accurately fit the
  6239. function, then the program *needs* an explicit declaration of the
  6240. function with argument types in order to call it correctly.
  6241. Implicit declarations are deprecated, and a function call that
  6242. creates one causes a warning.
  6243. 22.3 Function Calls
  6244. ===================
  6245. Starting a program automatically calls the function named ‘main’ (*note
  6246. The main Function::). Aside from that, a function does nothing except
  6247. when it is “called”. That occurs during the execution of a
  6248. function-call expression specifying that function.
  6249. A function-call expression looks like this:
  6250. FUNCTION (ARGUMENTS...)
  6251. Most of the time, FUNCTION is a function name. However, it can also
  6252. be an expression with a function pointer value; that way, the program
  6253. can determine at run time which function to call.
  6254. The ARGUMENTS are a series of expressions separated by commas. Each
  6255. expression specifies one argument to pass to the function.
  6256. The list of arguments in a function call looks just like use of the
  6257. comma operator (*note Comma Operator::), but the fact that it fills the
  6258. parentheses of a function call gives it a different meaning.
  6259. Here’s an example of a function call, taken from an example near the
  6260. beginning (*note Complete Program::).
  6261. printf ("Fibonacci series item %d is %d\n",
  6262. 19, fib (19));
  6263. The three arguments given to ‘printf’ are a constant string, the
  6264. integer 19, and the integer returned by ‘fib (19)’.
  6265. 22.4 Function Call Semantics
  6266. ============================
  6267. The meaning of a function call is to compute the specified argument
  6268. expressions, convert their values according to the function’s
  6269. declaration, then run the function giving it copies of the converted
  6270. values. (This method of argument passing is known as “call-by-value”.)
  6271. When the function finishes, the value it returns becomes the value of
  6272. the function-call expression.
  6273. Call-by-value implies that an assignment to the function argument
  6274. variable has no direct effect on the caller. For instance,
  6275. #include <stdlib.h> /* Defines ‘EXIT_SUCCESS’. */
  6276. #include <stdio.h> /* Declares ‘printf’. */
  6277. void
  6278. subroutine (int x)
  6279. {
  6280. x = 5;
  6281. }
  6282. void
  6283. main (void)
  6284. {
  6285. int y = 20;
  6286. subroutine (y);
  6287. printf ("y is %d\n", y);
  6288. return EXIT_SUCCESS;
  6289. }
  6290. prints ‘y is 20’. Calling ‘subroutine’ initializes ‘x’ from the value
  6291. of ‘y’, but this does not establish any other relationship between the
  6292. two variables. Thus, the assignment to ‘x’, inside ‘subroutine’,
  6293. changes only _that_ ‘x’.
  6294. If an argument’s type is specified by the function’s declaration, the
  6295. function call converts the argument expression to that type if possible.
  6296. If the conversion is impossible, that is an error.
  6297. If the function’s declaration doesn’t specify the type of that
  6298. argument, then the _default argument promotions_ apply. *Note Argument
  6299. Promotions::.
  6300. 22.5 Function Pointers
  6301. ======================
  6302. A function name refers to a fixed function. Sometimes it is useful to
  6303. call a function to be determined at run time; to do this, you can use a
  6304. “function pointer value” that points to the chosen function (*note
  6305. Pointers::).
  6306. Pointer-to-function types can be used to declare variables and other
  6307. data, including array elements, structure fields, and union
  6308. alternatives. They can also be used for function arguments and return
  6309. values. These types have the peculiarity that they are never converted
  6310. automatically to ‘void *’ or vice versa. However, you can do that
  6311. conversion with a cast.
  6312. 22.5.1 Declaring Function Pointers
  6313. ----------------------------------
  6314. The declaration of a function pointer variable (or structure field)
  6315. looks almost like a function declaration, except it has an additional
  6316. ‘*’ just before the variable name. Proper nesting requires a pair of
  6317. parentheses around the two of them. For instance, ‘int (*a) ();’ says,
  6318. “Declare ‘a’ as a pointer such that ‘*a’ is an ‘int’-returning
  6319. function.”
  6320. Contrast these three declarations:
  6321. /* Declare a function returning ‘char *’. */
  6322. char *a (char *);
  6323. /* Declare a pointer to a function returning ‘char’. */
  6324. char (*a) (char *);
  6325. /* Declare a pointer to a function returning ‘char *’. */
  6326. char *(*a) (char *);
  6327. The possible argument types of the function pointed to are the same
  6328. as in a function declaration. You can write a prototype that specifies
  6329. all the argument types:
  6330. RETTYPE (*FUNCTION) (ARGUMENTS...);
  6331. or one that specifies some and leaves the rest unspecified:
  6332. RETTYPE (*FUNCTION) (ARGUMENTS..., ...);
  6333. or one that says there are no arguments:
  6334. RETTYPE (*FUNCTION) (void);
  6335. You can also write a non-prototype declaration that says nothing
  6336. about the argument types:
  6337. RETTYPE (*FUNCTION) ();
  6338. For example, here’s a declaration for a variable that should point to
  6339. some arithmetic function that operates on two ‘double’s:
  6340. double (*binary_op) (double, double);
  6341. Structure fields, union alternatives, and array elements can be
  6342. function pointers; so can parameter variables. The function pointer
  6343. declaration construct can also be combined with other operators allowed
  6344. in declarations. For instance,
  6345. int **(*foo)();
  6346. declares ‘foo’ as a pointer to a function that returns type ‘int **’,
  6347. and
  6348. int **(*foo[30])();
  6349. declares ‘foo’ as an array of 30 pointers to functions that return type
  6350. ‘int **’.
  6351. int **(**foo)();
  6352. declares ‘foo’ as a pointer to a pointer to a function that returns type
  6353. ‘int **’.
  6354. 22.5.2 Assigning Function Pointers
  6355. ----------------------------------
  6356. Assuming we have declared the variable ‘binary_op’ as in the previous
  6357. section, giving it a value requires a suitable function to use. So
  6358. let’s define a function suitable for the variable to point to. Here’s
  6359. one:
  6360. double
  6361. double_add (double a, double b)
  6362. {
  6363. return a+b;
  6364. }
  6365. Now we can give it a value:
  6366. binary_op = double_add;
  6367. The target type of the function pointer must be upward compatible
  6368. with the type of the function (*note Compatible Types::).
  6369. There is no need for ‘&’ in front of ‘double_add’. Using a function
  6370. name such as ‘double_add’ as an expression automatically converts it to
  6371. the function’s address, with the appropriate function pointer type.
  6372. However, it is ok to use ‘&’ if you feel that is clearer:
  6373. binary_op = &double_add;
  6374. 22.5.3 Calling Function Pointers
  6375. --------------------------------
  6376. To call the function specified by a function pointer, just write the
  6377. function pointer value in a function call. For instance, here’s a call
  6378. to the function ‘binary_op’ points to:
  6379. binary_op (x, 5)
  6380. Since the data type of ‘binary_op’ explicitly specifies type ‘double’
  6381. for the arguments, the call converts ‘x’ and 5 to ‘double’.
  6382. The call conceptually dereferences the pointer ‘binary_op’ to “get”
  6383. the function it points to, and calls that function. If you wish, you
  6384. can explicitly represent the derefence by writing the ‘*’ operator:
  6385. (*binary_op) (x, 5)
  6386. The ‘*’ reminds people reading the code that ‘binary_op’ is a
  6387. function pointer rather than the name of a specific function.
  6388. 22.6 The ‘main’ Function
  6389. ========================
  6390. Every complete executable program requires at least one function, called
  6391. ‘main’, which is where execution begins. You do not have to explicitly
  6392. declare ‘main’, though GNU C permits you to do so. Conventionally,
  6393. ‘main’ should be defined to follow one of these calling conventions:
  6394. int main (void) {...}
  6395. int main (int argc, char *argv[]) {...}
  6396. int main (int argc, char *argv[], char *envp[]) {...}
  6397. Using ‘void’ as the parameter list means that ‘main’ does not use the
  6398. arguments. You can write ‘char **argv’ instead of ‘char *argv[]’, and
  6399. likewise for ‘envp’, as the two constructs are equivalent.
  6400. You can call ‘main’ from C code, as you can call any other function,
  6401. though that is an unusual thing to do. When you do that, you must write
  6402. the call to pass arguments that match the parameters in the definition
  6403. of ‘main’.
  6404. The ‘main’ function is not actually the first code that runs when a
  6405. program starts. In fact, the first code that runs is system code from
  6406. the file ‘crt0.o’. In Unix, this was hand-written assembler code, but
  6407. in GNU we replaced it with C code. Its job is to find the arguments for
  6408. ‘main’ and call that.
  6409. 22.6.1 Returning Values from ‘main’
  6410. -----------------------------------
  6411. When ‘main’ returns, the process terminates. Whatever value ‘main’
  6412. returns becomes the exit status which is reported to the parent process.
  6413. While nominally the return value is of type ‘int’, in fact the exit
  6414. status gets truncated to eight bits; if ‘main’ returns the value 256,
  6415. the exit status is 0.
  6416. Normally, programs return only one of two values: 0 for success, and
  6417. 1 for failure. For maximum portability, use the macro values
  6418. ‘EXIT_SUCCESS’ and ‘EXIT_FAILURE’ defined in ‘stdlib.h’. Here’s an
  6419. example:
  6420. #include <stdlib.h> /* Defines ‘EXIT_SUCCESS’ */
  6421. /* and ‘EXIT_FAILURE’. */
  6422. int
  6423. main (void)
  6424. {
  6425. ...
  6426. if (foo)
  6427. return EXIT_SUCCESS;
  6428. else
  6429. return EXIT_FAILURE;
  6430. }
  6431. Some types of programs maintain special conventions for various
  6432. return values; for example, comparison programs including ‘cmp’ and
  6433. ‘diff’ return 1 to indicate a mismatch, and 2 to indicate that the
  6434. comparison couldn’t be performed.
  6435. 22.6.2 Accessing Command-line Parameters
  6436. ----------------------------------------
  6437. If the program was invoked with any command-line arguments, it can
  6438. access them through the arguments of ‘main’, ‘argc’ and ‘argv’. (You
  6439. can give these arguments any names, but the names ‘argc’ and ‘argv’ are
  6440. customary.)
  6441. The value of ‘argv’ is an array containing all of the command-line
  6442. arguments as strings, with the name of the command invoked as the first
  6443. string. ‘argc’ is an integer that says how many strings ‘argv’
  6444. contains. Here is an example of accessing the command-line parameters,
  6445. retrieving the program’s name and checking for the standard ‘--version’
  6446. and ‘--help’ options:
  6447. #include <string.h> /* Declare ‘strcmp’. */
  6448. int
  6449. main (int argc, char *argv[])
  6450. {
  6451. char *program_name = argv[0];
  6452. for (int i = 1; i < argc; i++)
  6453. {
  6454. if (!strcmp (argv[i], "--version"))
  6455. {
  6456. /* Print version information and exit. */
  6457. ...
  6458. }
  6459. else if (!strcmp (argv[i], "--help"))
  6460. {
  6461. /* Print help information and exit. */
  6462. ...
  6463. }
  6464. }
  6465. ...
  6466. }
  6467. 22.6.3 Accessing Environment Variables
  6468. --------------------------------------
  6469. You can optionally include a third parameter to ‘main’, another array of
  6470. strings, to capture the environment variables available to the program.
  6471. Unlike what happens with ‘argv’, there is no additional parameter for
  6472. the count of environment variables; rather, the array of environment
  6473. variables concludes with a null pointer.
  6474. #include <stdio.h> /* Declares ‘printf’. */
  6475. int
  6476. main (int argc, char *argv[], char *envp[])
  6477. {
  6478. /* Print out all environment variables. */
  6479. int i = 0;
  6480. while (envp[i])
  6481. {
  6482. printf ("%s\n", envp[i]);
  6483. i++;
  6484. }
  6485. }
  6486. Another method of retrieving environment variables is to use the
  6487. library function ‘getenv’, which is defined in ‘stdlib.h’. Using
  6488. ‘getenv’ does not require defining ‘main’ to accept the ‘envp’ pointer.
  6489. For example, here is a program that fetches and prints the user’s home
  6490. directory (if defined):
  6491. #include <stdlib.h> /* Declares ‘getenv’. */
  6492. #include <stdio.h> /* Declares ‘printf’. */
  6493. int
  6494. main (void)
  6495. {
  6496. char *home_directory = getenv ("HOME");
  6497. if (home_directory)
  6498. printf ("My home directory is: %s\n", home_directory);
  6499. else
  6500. printf ("My home directory is not defined!\n");
  6501. }
  6502. 22.7 Advanced Function Features
  6503. ===============================
  6504. This section describes some advanced or obscure features for GNU C
  6505. function definitions. If you are just learning C, you can skip the rest
  6506. of this chapter.
  6507. 22.7.1 Variable-Length Array Parameters
  6508. ---------------------------------------
  6509. An array parameter can have variable length: simply declare the array
  6510. type with a size that isn’t constant. In a nested function, the length
  6511. can refer to a variable defined in a containing scope. In any function,
  6512. it can refer to a previous parameter, like this:
  6513. struct entry
  6514. tester (int len, char data[len][len])
  6515. {
  6516. ...
  6517. }
  6518. Alternatively, in function declarations (but not in function
  6519. definitions), you can use ‘[*]’ to denote that the array parameter is of
  6520. a variable length, such that these two declarations mean the same thing:
  6521. struct entry
  6522. tester (int len, char data[len][len]);
  6523. struct entry
  6524. tester (int len, char data[*][*]);
  6525. The two forms of input are equivalent in GNU C, but emphasizing that the
  6526. array parameter is variable-length may be helpful to those studying the
  6527. code.
  6528. You can also omit the length parameter, and instead use some other
  6529. in-scope variable for the length in the function definition:
  6530. struct entry
  6531. tester (char data[*][*]);
  6532. ...
  6533. int dataLength = 20;
  6534. ...
  6535. struct entry
  6536. tester (char data[dataLength][dataLength])
  6537. {
  6538. ...
  6539. }
  6540. In GNU C, to pass the array first and the length afterward, you can
  6541. use a “parameter forward declaration”, like this:
  6542. struct entry
  6543. tester (int len; char data[len][len], int len)
  6544. {
  6545. ...
  6546. }
  6547. The ‘int len’ before the semicolon is the parameter forward
  6548. declaration; it serves the purpose of making the name ‘len’ known when
  6549. the declaration of ‘data’ is parsed.
  6550. You can write any number of such parameter forward declarations in
  6551. the parameter list. They can be separated by commas or semicolons, but
  6552. the last one must end with a semicolon, which is followed by the “real”
  6553. parameter declarations. Each forward declaration must match a
  6554. subsequent “real” declaration in parameter name and data type.
  6555. Standard C does not support parameter forward declarations.
  6556. 22.7.2 Variable-Length Parameter Lists
  6557. --------------------------------------
  6558. A function that takes a variable number of arguments is called a
  6559. “variadic function”. In C, a variadic function must specify at least
  6560. one fixed argument with an explicitly declared data type. Additional
  6561. arguments can follow, and can vary in both quantity and data type.
  6562. In the function header, declare the fixed parameters in the normal
  6563. way, then write a comma and an ellipsis: ‘, ...’. Here is an example of
  6564. a variadic function header:
  6565. int add_multiple_values (int number, ...)
  6566. The function body can refer to fixed arguments by their parameter
  6567. names, but the additional arguments have no names. Accessing them in
  6568. the function body uses certain standard macros. They are defined in the
  6569. library header file ‘stdarg.h’, so the code must ‘#include’ that file.
  6570. In the body, write
  6571. va_list ap;
  6572. va_start (ap, LAST_FIXED_PARAMETER);
  6573. This declares the variable ‘ap’ (you can use any name for it) and then
  6574. sets it up to point before the first additional argument.
  6575. Then, to fetch the next consecutive additional argument, write this:
  6576. va_arg (ap, TYPE)
  6577. After fetching all the additional arguments (or as many as need to be
  6578. used), write this:
  6579. va_end (ap);
  6580. Here’s an example of a variadic function definition that adds any
  6581. number of ‘int’ arguments. The first (fixed) argument says how many
  6582. more arguments follow.
  6583. #include <stdarg.h> /* Defines ‘va’... macros. */
  6584. ...
  6585. int
  6586. add_multiple_values (int argcount, ...)
  6587. {
  6588. int counter, total = 0;
  6589. /* Declare a variable of type ‘va_list’. */
  6590. va_list argptr;
  6591. /* Initialize that variable.. */
  6592. va_start (argptr, argcount);
  6593. for (counter = 0; counter < argcount; counter++)
  6594. {
  6595. /* Get the next additional argument. */
  6596. total += va_arg (argptr, int);
  6597. }
  6598. /* End use of the ‘argptr’ variable. */
  6599. va_end (argptr);
  6600. return total;
  6601. }
  6602. With GNU C, ‘va_end’ is superfluous, but some other compilers might
  6603. make ‘va_start’ allocate memory so that calling ‘va_end’ is necessary to
  6604. avoid a memory leak. Before doing ‘va_start’ again with the same
  6605. variable, do ‘va_end’ first.
  6606. Because of this possible memory allocation, it is risky (in
  6607. principle) to copy one ‘va_list’ variable to another with assignment.
  6608. Instead, use ‘va_copy’, which copies the substance but allocates
  6609. separate memory in the variable you copy to. The call looks like
  6610. ‘va_copy (TO, FROM)’, where both TO and FROM should be variables of type
  6611. ‘va_list’. In principle, do ‘va_end’ on each of these variables before
  6612. its scope ends.
  6613. Since the additional arguments’ types are not specified in the
  6614. function’s definition, the default argument promotions (*note Argument
  6615. Promotions::) apply to them in function calls. The function definition
  6616. must take account of this; thus, if an argument was passed as ‘short’,
  6617. the function should get it as ‘int’. If an argument was passed as
  6618. ‘float’, the function should get it as ‘double’.
  6619. C has no mechanism to tell the variadic function how many arguments
  6620. were passed to it, so its calling convention must give it a way to
  6621. determine this. That’s why ‘add_multiple_values’ takes a fixed argument
  6622. that says how many more arguments follow. Thus, you can call the
  6623. function like this:
  6624. sum = add_multiple_values (3, 12, 34, 190);
  6625. /* Value is 12+34+190. */
  6626. In GNU C, there is no actual need to use the ‘va_end’ function. In
  6627. fact, it does nothing. It’s used for compatibility with other
  6628. compilers, when that matters.
  6629. It is a mistake to access variables declared as ‘va_list’ except in
  6630. the specific ways described here. Just what that type consists of is an
  6631. implementation detail, which could vary from one platform to another.
  6632. 22.7.3 Nested Functions
  6633. -----------------------
  6634. A “nested function” is a function defined inside another function. The
  6635. nested function’s name is local to the block where it is defined. For
  6636. example, here we define a nested function named ‘square’, and call it
  6637. twice:
  6638. foo (double a, double b)
  6639. {
  6640. double square (double z) { return z * z; }
  6641. return square (a) + square (b);
  6642. }
  6643. The nested function can access all the variables of the containing
  6644. function that are visible at the point of its definition. This is
  6645. called “lexical scoping”. For example, here we show a nested function
  6646. that uses an inherited variable named ‘offset’:
  6647. bar (int *array, int offset, int size)
  6648. {
  6649. int access (int *array, int index)
  6650. { return array[index + offset]; }
  6651. int i;
  6652. ...
  6653. for (i = 0; i < size; i++)
  6654. ... access (array, i) ...
  6655. }
  6656. Nested function definitions can appear wherever automatic variable
  6657. declarations are allowed; that is, in any block, interspersed with the
  6658. other declarations and statements in the block.
  6659. The nested function’s name is visible only within the parent block;
  6660. the name’s scope starts from its definition and continues to the end of
  6661. the containing block. If the nested function’s name is the same as the
  6662. parent function’s name, there wil be no way to refer to the parent
  6663. function inside the scope of the name of the nested function.
  6664. Using ‘extern’ or ‘static’ on a nested function definition is an
  6665. error.
  6666. It is possible to call the nested function from outside the scope of
  6667. its name by storing its address or passing the address to another
  6668. function. You can do this safely, but you must be careful:
  6669. hack (int *array, int size, int addition)
  6670. {
  6671. void store (int index, int value)
  6672. { array[index] = value + addition; }
  6673. intermediate (store, size);
  6674. }
  6675. Here, the function ‘intermediate’ receives the address of ‘store’ as
  6676. an argument. If ‘intermediate’ calls ‘store’, the arguments given to
  6677. ‘store’ are used to store into ‘array’. ‘store’ also accesses ‘hack’’s
  6678. local variable ‘addition’.
  6679. It is safe for ‘intermediate’ to call ‘store’ because ‘hack’’s stack
  6680. frame, with its arguments and local variables, continues to exist during
  6681. the call to ‘intermediate’.
  6682. Calling the nested function through its address after the containing
  6683. function has exited is asking for trouble. If it is called after a
  6684. containing scope level has exited, and if it refers to some of the
  6685. variables that are no longer in scope, it will refer to memory
  6686. containing junk or other data. It’s not wise to take the risk.
  6687. The GNU C Compiler implements taking the address of a nested function
  6688. using a technique called “trampolines”. This technique was described in
  6689. ‘Lexical Closures for C++’ (Thomas M. Breuel, USENIX C++ Conference
  6690. Proceedings, October 17–21, 1988).
  6691. A nested function can jump to a label inherited from a containing
  6692. function, provided the label was explicitly declared in the containing
  6693. function (*note Local Labels::). Such a jump returns instantly to the
  6694. containing function, exiting the nested function that did the ‘goto’ and
  6695. any intermediate function invocations as well. Here is an example:
  6696. bar (int *array, int offset, int size)
  6697. {
  6698. /* Explicitly declare the label ‘failure’. */
  6699. __label__ failure;
  6700. int access (int *array, int index)
  6701. {
  6702. if (index &tt; size)
  6703. /* Exit this function,
  6704. and return to ‘bar’. */
  6705. goto failure;
  6706. return array[index + offset];
  6707. }
  6708. int i;
  6709. ...
  6710. for (i = 0; i < size; i++)
  6711. ... access (array, i) ...
  6712. ...
  6713. return 0;
  6714. /* Control comes here from ‘access’
  6715. if it does the ‘goto’. */
  6716. failure:
  6717. return -1;
  6718. }
  6719. To declare the nested function before its definition, use ‘auto’
  6720. (which is otherwise meaningless for function declarations; *note auto
  6721. and register::). For example,
  6722. bar (int *array, int offset, int size)
  6723. {
  6724. auto int access (int *, int);
  6725. ...
  6726. ... access (array, i) ...
  6727. ...
  6728. int access (int *array, int index)
  6729. {
  6730. ...
  6731. }
  6732. ...
  6733. }
  6734. 22.7.4 Inline Function Definitions
  6735. ----------------------------------
  6736. To declare a function inline, use the ‘inline’ keyword in its
  6737. definition. Here’s a simple function that takes a pointer-to-‘int’ and
  6738. increments the integer stored there—declared inline.
  6739. struct list
  6740. {
  6741. struct list *first, *second;
  6742. };
  6743. inline struct list *
  6744. list_first (struct list *p)
  6745. {
  6746. return p-<first;
  6747. }
  6748. inline struct list *
  6749. list_second (struct list *p)
  6750. {
  6751. return p-<second;
  6752. }
  6753. optimized compilation can substitute the inline function’s body for
  6754. any call to it. This is called _inlining_ the function. It makes the
  6755. code that contains the call run faster, significantly so if the inline
  6756. function is small.
  6757. Here’s a function that uses ‘pair_second’:
  6758. int
  6759. pairlist_length (struct list *l)
  6760. {
  6761. int length = 0;
  6762. while (l)
  6763. {
  6764. length++;
  6765. l = pair_second (l);
  6766. }
  6767. return length;
  6768. }
  6769. Substituting the code of ‘pair_second’ into the definition of
  6770. ‘pairlist_length’ results in this code, in effect:
  6771. int
  6772. pairlist_length (struct list *l)
  6773. {
  6774. int length = 0;
  6775. while (l)
  6776. {
  6777. length++;
  6778. l = l-<second;
  6779. }
  6780. return length;
  6781. }
  6782. Since the definition of ‘pair_second’ does not say ‘extern’ or
  6783. ‘static’, that definition is used only for inlining. It doesn’t
  6784. generate code that can be called at run time. If not all the calls to
  6785. the function are inlined, there must be a definition of the same
  6786. function name in another module for them to call.
  6787. Adding ‘static’ to an inline function definition means the function
  6788. definition is limited to this compilation module. Also, it generates
  6789. run-time code if necessary for the sake of any calls that were not
  6790. inlined. If all calls are inlined then the function definition does not
  6791. generate run-time code, but you can force generation of run-time code
  6792. with the option ‘-fkeep-inline-functions’.
  6793. Specifying ‘extern’ along with ‘inline’ means the function is
  6794. external and generates run-time code to be called from other separately
  6795. compiled modules, as well as inlined. You can define the function as
  6796. ‘inline’ without ‘extern’ in other modules so as to inline calls to the
  6797. same function in those modules.
  6798. Why are some calls not inlined? First of all, inlining is an
  6799. optimization, so non-optimized compilation does not inline.
  6800. Some calls cannot be inlined for technical reasons. Also, certain
  6801. usages in a function definition can make it unsuitable for inline
  6802. substitution. Among these usages are: variadic functions, use of
  6803. ‘alloca’, use of computed goto (*note Labels as Values::), and use of
  6804. nonlocal goto. The option ‘-Winline’ requests a warning when a function
  6805. marked ‘inline’ is unsuitable to be inlined. The warning explains what
  6806. obstacle makes it unsuitable.
  6807. Just because a call _can_ be inlined does not mean it _should_ be
  6808. inlined. The GNU C compiler weighs costs and benefits to decide whether
  6809. inlining a particular call is advantageous.
  6810. You can force inlining of all calls to a given function that can be
  6811. inlined, even in a non-optimized compilation. by specifying the
  6812. ‘always_inline’ attribute for the function, like this:
  6813. /* Prototype. */
  6814. inline void foo (const char) __attribute__((always_inline));
  6815. This is a GNU C extension. *Note Attributes::.
  6816. A function call may be inlined even if not declared ‘inline’ in
  6817. special cases where the compiler can determine this is correct and
  6818. desirable. For instance, when a static function is called only once, it
  6819. will very likely be inlined. With ‘-flto’, link-time optimization, any
  6820. function might be inlined. To absolutely prevent inlining of a specific
  6821. function, specify ‘__attribute__((__noinline__))’ in the function’s
  6822. definition.
  6823. 22.8 Obsolete Function Features
  6824. ===============================
  6825. These features of function definitions are still used in old programs,
  6826. but you shouldn’t write code this way today. If you are just learning
  6827. C, you can skip this section.
  6828. 22.8.1 Older GNU C Inlining
  6829. ---------------------------
  6830. The GNU C spec for inline functions, before GCC version 5, defined
  6831. ‘extern inline’ on a function definition to mean to inline calls to it
  6832. but _not_ generate code for the function that could be called at run
  6833. time. By contrast, ‘inline’ without ‘extern’ specified to generate
  6834. run-time code for the function. In effect, ISO incompatibly flipped the
  6835. meanings of these two cases. We changed GCC in version 5 to adopt the
  6836. ISO specification.
  6837. Many programs still use these cases with the previous GNU C meanings.
  6838. You can specify use of those meanings with the option ‘-fgnu89-inline’.
  6839. You can also specify this for a single function with ‘__attribute__
  6840. ((gnu_inline))’. Here’s an example:
  6841. inline __attribute__ ((gnu_inline))
  6842. int
  6843. inc (int *a)
  6844. {
  6845. (*a)++;
  6846. }
  6847. 22.8.2 Old-Style Function Definitions
  6848. -------------------------------------
  6849. The syntax of C traditionally allows omitting the data type in a
  6850. function declaration if it specifies a storage class or a qualifier.
  6851. Then the type defaults to ‘int’. For example:
  6852. static foo (double x);
  6853. defaults the return type to ‘int’. This is bad practice; if you see it,
  6854. fix it.
  6855. An “old-style” (or “K&R”) function definition is the way function
  6856. definitions were written in the 1980s. It looks like this:
  6857. RETTYPE
  6858. FUNCTION (PARMNAMES)
  6859. PARM_DECLARATIONS
  6860. {
  6861. BODY
  6862. }
  6863. In PARMNAMES, only the parameter names are listed, separated by
  6864. commas. Then PARM_DECLARATIONS declares their data types; these
  6865. declarations look just like variable declarations. If a parameter is
  6866. listed in PARMNAMES but has no declaration, it is implicitly declared
  6867. ‘int’.
  6868. There is no reason to write a definition this way nowadays, but they
  6869. can still be seen in older GNU programs.
  6870. An old-style variadic function definition looks like this:
  6871. #include <varargs.h>
  6872. int
  6873. add_multiple_values (va_alist)
  6874. va_dcl
  6875. {
  6876. int argcount;
  6877. int counter, total = 0;
  6878. /* Declare a variable of type ‘va_list’. */
  6879. va_list argptr;
  6880. /* Initialize that variable. */
  6881. va_start (argptr);
  6882. /* Get the first argument (fixed). */
  6883. argcount = va_arg (int);
  6884. for (counter = 0; counter < argcount; counter++)
  6885. {
  6886. /* Get the next additional argument. */
  6887. total += va_arg (argptr, int);
  6888. }
  6889. /* End use of the ‘argptr’ variable. */
  6890. va_end (argptr);
  6891. return total;
  6892. }
  6893. Note that the old-style variadic function definition has no fixed
  6894. parameter variables; all arguments must be obtained with ‘va_arg’.
  6895. 23 Compatible Types
  6896. *******************
  6897. Declaring a function or variable twice is valid in C only if the two
  6898. declarations specify “compatible” types. In addition, some operations
  6899. on pointers require operands to have compatible target types.
  6900. In C, two different primitive types are never compatible. Likewise
  6901. for the defined types ‘struct’, ‘union’ and ‘enum’: two separately
  6902. defined types are incompatible unless they are defined exactly the same
  6903. way.
  6904. However, there are a few cases where different types can be
  6905. compatible:
  6906. • Every enumeration type is compatible with some integer type. In
  6907. GNU C, the choice of integer type depends on the largest
  6908. enumeration value.
  6909. • Array types are compatible if the element types are compatible and
  6910. the sizes (when specified) match.
  6911. • Pointer types are compatible if the pointer target types are
  6912. compatible.
  6913. • Function types that specify argument types are compatible if the
  6914. return types are compatible and the argument types are compatible,
  6915. argument by argument. In addition, they must all agree in whether
  6916. they use ‘...’ to allow additional arguments.
  6917. • Function types that don’t specify argument types are compatible if
  6918. the return types are.
  6919. • Function types that specify the argument types are compatible with
  6920. function types that omit them, if the return types are compatible
  6921. and the specified argument types are unaltered by the argument
  6922. promotions (*note Argument Promotions::).
  6923. In order for types to be compatible, they must agree in their type
  6924. qualifiers. Thus, ‘const int’ and ‘int’ are incompatible. It follows
  6925. that ‘const int *’ and ‘int *’ are incompatible too (they are pointers
  6926. to types that are not compatible).
  6927. If two types are compatible ignoring the qualifiers, we call them
  6928. “nearly compatible”. (If they are array types, we ignore qualifiers on
  6929. the element types.(1)) Comparison of pointers is valid if the pointers’
  6930. target types are nearly compatible. Likewise, the two branches of a
  6931. conditional expression may be pointers to nearly compatible target
  6932. types.
  6933. If two types are compatible ignoring the qualifiers, and the first
  6934. type has all the qualifiers of the second type, we say the first is
  6935. “upward compatible” with the second. Assignment of pointers requires
  6936. the assigned pointer’s target type to be upward compatible with the
  6937. right operand (the new value)’s target type.
  6938. ---------- Footnotes ----------
  6939. (1) This is a GNU C extension.
  6940. 24 Type Conversions
  6941. *******************
  6942. C converts between data types automatically when that seems clearly
  6943. necessary. In addition, you can convert explicitly with a “cast”.
  6944. 24.1 Explicit Type Conversion
  6945. =============================
  6946. You can do explicit conversions using the unary “cast” operator, which
  6947. is written as a type designator (*note Type Designators::) in
  6948. parentheses. For example, ‘(int)’ is the operator to cast to type
  6949. ‘int’. Here’s an example of using it:
  6950. {
  6951. double d = 5.5;
  6952. printf ("Floating point value: %f\n", d);
  6953. printf ("Rounded to integer: %d\n", (int) d);
  6954. }
  6955. Using ‘(int) d’ passes an ‘int’ value as argument to ‘printf’, so you
  6956. can print it with ‘%d’. Using just ‘d’ without the cast would pass the
  6957. value as ‘double’. That won’t work at all with ‘%d’; the results would
  6958. be gibberish.
  6959. To divide one integer by another without rounding, cast either of the
  6960. integers to ‘double’ first:
  6961. (double) DIVIDEND / DIVISOR
  6962. DIVIDEND / (double) DIVISOR
  6963. It is enough to cast one of them, because that forces the common type
  6964. to ‘double’ so the other will be converted automatically.
  6965. The valid cast conversions are:
  6966. • One numerical type to another.
  6967. • One pointer type to another. (Converting between pointers that
  6968. point to functions and pointers that point to data is not standard
  6969. C.)
  6970. • A pointer type to an integer type.
  6971. • An integer type to a pointer type.
  6972. • To a union type, from the type of any alternative in the union
  6973. (*note Unions::). (This is a GNU extension.)
  6974. • Anything, to ‘void’.
  6975. 24.2 Assignment Type Conversions
  6976. ================================
  6977. Certain type conversions occur automatically in assignments and certain
  6978. other contexts. These are the conversions assignments can do:
  6979. • Converting any numeric type to any other numeric type.
  6980. • Converting ‘void *’ to any other pointer type (except
  6981. pointer-to-function types).
  6982. • Converting any other pointer type to ‘void *’. (except
  6983. pointer-to-function types).
  6984. • Converting 0 (a null pointer constant) to any pointer type.
  6985. • Converting any pointer type to ‘bool’. (The result is 1 if the
  6986. pointer is not null.)
  6987. • Converting between pointer types when the left-hand target type is
  6988. upward compatible with the right-hand target type. *Note
  6989. Compatible Types::.
  6990. These type conversions occur automatically in certain contexts, which
  6991. are:
  6992. • An assignment converts the type of the right-hand expression to the
  6993. type wanted by the left-hand expression. For example,
  6994. double i;
  6995. i = 5;
  6996. converts 5 to ‘double’.
  6997. • A function call, when the function specifies the type for that
  6998. argument, converts the argument value to that type. For example,
  6999. void foo (double);
  7000. foo (5);
  7001. converts 5 to ‘double’.
  7002. • A ‘return’ statement converts the specified value to the type that
  7003. the function is declared to return. For example,
  7004. double
  7005. foo ()
  7006. {
  7007. return 5;
  7008. }
  7009. also converts 5 to ‘double’.
  7010. In all three contexts, if the conversion is impossible, that
  7011. constitutes an error.
  7012. 24.3 Argument Promotions
  7013. ========================
  7014. When a function’s definition or declaration does not specify the type of
  7015. an argument, that argument is passed without conversion in whatever type
  7016. it has, with these exceptions:
  7017. • Some narrow numeric values are “promoted” to a wider type. If the
  7018. expression is a narrow integer, such as ‘char’ or ‘short’, the call
  7019. converts it automatically to ‘int’ (*note Integer Types::).(1)
  7020. In this example, the expression ‘c’ is passed as an ‘int’:
  7021. char c = '$';
  7022. printf ("Character c is '%c'\n", c);
  7023. • If the expression has type ‘float’, the call converts it
  7024. automatically to ‘double’.
  7025. • An array as argument is converted to a pointer to its zeroth
  7026. element.
  7027. • A function name as argument is converted to a pointer to that
  7028. function.
  7029. ---------- Footnotes ----------
  7030. (1) On an embedded controller where ‘char’ or ‘short’ is the same
  7031. width as ‘int’, ‘unsigned char’ or ‘unsigned short’ promotes to
  7032. ‘unsigned int’, but that never occurs in GNU C on real computers.
  7033. 24.4 Operand Promotions
  7034. =======================
  7035. The operands in arithmetic operations undergo type conversion
  7036. automatically. These “operand promotions” are the same as the argument
  7037. promotions except without converting ‘float’ to ‘double’. In other
  7038. words, the operand promotions convert
  7039. • ‘char’ or ‘short’ (whether signed or not) to ‘int’.
  7040. • an array to a pointer to its zeroth element, and
  7041. • a function name to a pointer to that function.
  7042. 24.5 Common Type
  7043. ================
  7044. Arithmetic binary operators (except the shift operators) convert their
  7045. operands to the “common type” before operating on them. Conditional
  7046. expressions also convert the two possible results to their common type.
  7047. Here are the rules for determining the common type.
  7048. If one of the numbers has a floating-point type and the other is an
  7049. integer, the common type is that floating-point type. For instance,
  7050. 5.6 * 2 ⇒ 11.2 /* a ‘double’ value */
  7051. If both are floating point, the type with the larger range is the
  7052. common type.
  7053. If both are integers but of different widths, the common type is the
  7054. wider of the two.
  7055. If they are integer types of the same width, the common type is
  7056. unsigned if either operand is unsigned, and it’s ‘long’ if either
  7057. operand is ‘long’. It’s ‘long long’ if either operand is ‘long long’.
  7058. These rules apply to addition, subtraction, multiplication, division,
  7059. remainder, comparisons, and bitwise operations. They also apply to the
  7060. two branches of a conditional expression, and to the arithmetic done in
  7061. a modifying assignment operation.
  7062. 25 Scope
  7063. ********
  7064. Each definition or declaration of an identifier is visible in certain
  7065. parts of the program, which is typically less than the whole of the
  7066. program. The parts where it is visible are called its “scope”.
  7067. Normally, declarations made at the top-level in the source – that is,
  7068. not within any blocks and function definitions – are visible for the
  7069. entire contents of the source file after that point. This is called
  7070. “file scope” (*note File-Scope Variables::).
  7071. Declarations made within blocks of code, including within function
  7072. definitions, are visible only within those blocks. This is called
  7073. “block scope”. Here is an example:
  7074. void
  7075. foo (void)
  7076. {
  7077. int x = 42;
  7078. }
  7079. In this example, the variable ‘x’ has block scope; it is visible only
  7080. within the ‘foo’ function definition block. Thus, other blocks could
  7081. have their own variables, also named ‘x’, without any conflict between
  7082. those variables.
  7083. A variable declared inside a subblock has a scope limited to that
  7084. subblock,
  7085. void
  7086. foo (void)
  7087. {
  7088. {
  7089. int x = 42;
  7090. }
  7091. // ‘x’ is out of scope here.
  7092. }
  7093. If a variable declared within a block has the same name as a variable
  7094. declared outside of that block, the definition within the block takes
  7095. precedence during its scope:
  7096. int x = 42;
  7097. void
  7098. foo (void)
  7099. {
  7100. int x = 17;
  7101. printf ("%d\n", x);
  7102. }
  7103. This prints 17, the value of the variable ‘x’ declared in the function
  7104. body block, rather than the value of the variable ‘x’ at file scope. We
  7105. say that the inner declaration of ‘x’ “shadows” the outer declaration,
  7106. for the extent of the inner declaration’s scope.
  7107. A declaration with block scope can be shadowed by another declaration
  7108. with the same name in a subblock.
  7109. void
  7110. foo (void)
  7111. {
  7112. char *x = "foo";
  7113. {
  7114. int x = 42;
  7115. ...
  7116. exit (x / 6);
  7117. }
  7118. }
  7119. A function parameter’s scope is the entire function body, but it can
  7120. be shadowed. For example:
  7121. int x = 42;
  7122. void
  7123. foo (int x)
  7124. {
  7125. printf ("%d\n", x);
  7126. }
  7127. This prints the value of ‘x’ the function parameter, rather than the
  7128. value of the file-scope variable ‘x’. However,
  7129. Labels (*note goto Statement::) have “function” scope: each label is
  7130. visible for the whole of the containing function body, both before and
  7131. after the label declaration:
  7132. void
  7133. foo (void)
  7134. {
  7135. ...
  7136. goto bar;
  7137. ...
  7138. { // Subblock does not affect labels.
  7139. bar:
  7140. ...
  7141. }
  7142. goto bar;
  7143. }
  7144. Except for labels, a declared identifier is not visible to code
  7145. before its declaration. For example:
  7146. int x = 5;
  7147. int y = x + 10;
  7148. will work, but:
  7149. int x = y + 10;
  7150. int y = 5;
  7151. cannot refer to the variable ‘y’ before its declaration.
  7152. This is part of the GNU C Intro and Reference Manual and covered by
  7153. its license.
  7154. 26 Preprocessing
  7155. ****************
  7156. As the first stage of compiling a C source module, GCC transforms the
  7157. text with text substitutions and file inclusions. This is called
  7158. “preprocessing”.
  7159. 26.1 Preprocessing Overview
  7160. ===========================
  7161. GNU C performs preprocessing on each line of a C program as the first
  7162. stage of compilation. Preprocessing operates on a line only when it
  7163. contains a “preprocessing directive” or uses a “macro”—all other lines
  7164. pass through preprocessing unchanged.
  7165. Here are some jobs that preprocessing does. The rest of this chapter
  7166. gives the details.
  7167. • Inclusion of header files. These are files (usually containing
  7168. declarations and macro definitions) that can be substituted into
  7169. your program.
  7170. • Macro expansion. You can define “macros”, which are abbreviations
  7171. for arbitrary fragments of C code. Preprocessing replaces the
  7172. macros with their definitions. Some macros are automatically
  7173. predefined.
  7174. • Conditional compilation. You can include or exclude parts of the
  7175. program according to various conditions.
  7176. • Line control. If you use a program to combine or rearrange source
  7177. files into an intermediate file that is then compiled, you can use
  7178. line control to inform the compiler where each source line
  7179. originally came from.
  7180. • Compilation control. ‘#pragma’ and ‘_Pragma’ invoke some special
  7181. compiler features in how to handle certain constructs.
  7182. • Diagnostics. You can detect problems at compile time and issue
  7183. errors or warnings.
  7184. Except for expansion of predefined macros, all these operations
  7185. happen only if you use preprocessing directives to request them.
  7186. 26.2 Directives
  7187. ===============
  7188. “Preprocessing directives” are lines in the program that start with ‘#’.
  7189. Whitespace is allowed before and after the ‘#’. The ‘#’ is followed by
  7190. an identifier, the “directive name”. It specifies the operation to
  7191. perform. Here are a couple of examples:
  7192. #define LIMIT 51
  7193. # undef LIMIT
  7194. # error You screwed up!
  7195. We usually refer to a directive as ‘#NAME’ where NAME is the
  7196. directive name. For example, ‘#define’ means the directive that defines
  7197. a macro.
  7198. The ‘#’ that begins a directive cannot come from a macro expansion.
  7199. Also, the directive name is not macro expanded. Thus, if ‘foo’ is
  7200. defined as a macro expanding to ‘define’, that does not make ‘#foo’ a
  7201. valid preprocessing directive.
  7202. The set of valid directive names is fixed. Programs cannot define
  7203. new preprocessing directives.
  7204. Some directives require arguments; these make up the rest of the
  7205. directive line and must be separated from the directive name by
  7206. whitespace. For example, ‘#define’ must be followed by a macro name and
  7207. the intended expansion of the macro.
  7208. A preprocessing directive cannot cover more than one line. The line
  7209. can, however, be continued with backslash-newline, or by a
  7210. ‘/*...*/’-style comment that extends past the end of the line. These
  7211. will be replaced (by nothing, or by whitespace) before the directive is
  7212. processed.
  7213. 26.3 Preprocessing Tokens
  7214. =========================
  7215. Preprocessing divides C code (minus its comments) into “tokens” that are
  7216. similar to C tokens, but not exactly the same. Here are the quirks of
  7217. preprocessing tokens.
  7218. The main classes of preprocessing tokens are identifiers,
  7219. preprocessing numbers, string constants, character constants, and
  7220. punctuators; there are a few others too.
  7221. identifier
  7222. An “identifier” preprocessing token is syntactically like an
  7223. identifier in C: any sequence of letters, digits, or underscores,
  7224. as well as non-ASCII characters represented using ‘\U’ or ‘\u’,
  7225. that doesn’t begin with a digit.
  7226. During preprocessing, the keywords of C have no special
  7227. significance; at that stage, they are simply identifiers. Thus,
  7228. you can define a macro whose name is a keyword. The only
  7229. identifier that is special during preprocessing is ‘defined’ (*note
  7230. defined::).
  7231. preprocessing number
  7232. A “preprocessing number” is something that preprocessing treats
  7233. textually as a number, including C numeric constants, and other
  7234. sequences of characters which resemble numeric constants.
  7235. Preprocessing does not try to verify that a preprocessing number is
  7236. a valid number in C, and indeed it need not be one.
  7237. More precisely, preprocessing numbers begin with an optional
  7238. period, a required decimal digit, and then continue with any
  7239. sequence of letters, digits, underscores, periods, and exponents.
  7240. Exponents are the two-character sequences ‘e+’, ‘e-’, ‘E+’, ‘E-’,
  7241. ‘p+’, ‘p-’, ‘P+’, and ‘P-’. (The exponents that begin with ‘p’ or
  7242. ‘P’ are new to C99. They are used for hexadecimal floating-point
  7243. constants.)
  7244. The reason behind this unusual syntactic class is that the full
  7245. complexity of numeric constants is irrelevant during preprocessing.
  7246. The distinction between lexically valid and invalid floating-point
  7247. numbers, for example, doesn’t matter at this stage. The use of
  7248. preprocessing numbers makes it possible to split an identifier at
  7249. any position and get exactly two tokens, and reliably paste them
  7250. together using the ‘##’ operator (*note Concatenation::).
  7251. punctuator
  7252. A “punctuator” is syntactically like an operator. These are the
  7253. valid punctuators:
  7254. [ ] ( ) { } . -<
  7255. ++ -- & * + - ~ !
  7256. / % <<&tt;&tt; < &tt; <=&tt;= == != ^ | && ||
  7257. ? : ; ...
  7258. = *= /= %= += -= >>= <<= &= ^= |=
  7259. , # ##
  7260. >::< >%%< %: %:%:
  7261. string constant
  7262. A string constant in the source code is recognized by preprocessing
  7263. as a single preprocessing token.
  7264. character constant
  7265. A character constant in the source code is recognized by
  7266. preprocessing as a single preprocessing token.
  7267. header name
  7268. Within the ‘#include’ directive, preprocessing recognizes a “header
  7269. name” token. It consists of ‘"NAME"’, where NAME is a sequence of
  7270. source characters other than newline and ‘"’, or ‘>NAME<’, where
  7271. NAME is a sequence of source characters other than newline and ‘<’.
  7272. In practice, it is more convenient to think that the ‘#include’
  7273. line is exempt from tokenization.
  7274. other
  7275. Any other character that’s valid in a C source program is treated
  7276. as a separate preprocessing token.
  7277. Once the program is broken into preprocessing tokens, they remain
  7278. separate until the end of preprocessing. Macros that generate two
  7279. consecutive tokens insert whitespace to keep them separate, if
  7280. necessary. For example,
  7281. #define foo() bar
  7282. foo()baz
  7283. ↦ bar baz
  7284. _not_
  7285. ↦ barbaz
  7286. The only exception is with the ‘##’ preprocessing operator, which
  7287. pastes tokens together (*note Concatenation::).
  7288. Preprocessing treats the null character (code 0) as whitespace, but
  7289. generates a warning for it because it may be invisible to the user (many
  7290. terminals do not display it at all) and its presence in the file is
  7291. probably a mistake.
  7292. 26.4 Header Files
  7293. =================
  7294. A header file is a file of C code, typically containing C declarations
  7295. and macro definitions (*note Macros::), to be shared between several
  7296. source files. You request the use of a header file in your program by
  7297. “including” it, with the C preprocessing directive ‘#include’.
  7298. Header files serve two purposes.
  7299. • System header files declare the interfaces to parts of the
  7300. operating system. You include them in your program to supply the
  7301. definitions and declarations that you need to invoke system calls
  7302. and libraries.
  7303. • Program-specific header files contain declarations for interfaces
  7304. between the source files of a particular program. It is a good
  7305. idea to create a header file for related declarations and macro
  7306. definitions if all or most of them are needed in several different
  7307. source files.
  7308. Including a header file produces the same results as copying the
  7309. header file into each source file that needs it. Such copying would be
  7310. time-consuming and error-prone. With a header file, the related
  7311. declarations appear in only one place. If they need to be changed, you
  7312. can change them in one place, and programs that include the header file
  7313. will then automatically use the new version when next recompiled. The
  7314. header file eliminates the labor of finding and changing all the copies
  7315. as well as the risk that a failure to change one copy will result in
  7316. inconsistencies within a program.
  7317. In C, the usual convention is to give header files names that end
  7318. with ‘.h’. It is most portable to use only letters, digits, dashes, and
  7319. underscores in header file names, and at most one dot.
  7320. 26.4.1 ‘#include’ Syntax
  7321. ------------------------
  7322. You can specify inclusion of user and system header files with the
  7323. preprocessing directive ‘#include’. It has two variants:
  7324. ‘#include <FILE>’
  7325. This variant is used for system header files. It searches for a
  7326. file named FILE in a standard list of system directories. You can
  7327. prepend directories to this list with the ‘-I’ option (*note
  7328. Invoking GCC: (gcc)Invocation.).
  7329. ‘#include "FILE"’
  7330. This variant is used for header files of your own program. It
  7331. searches for a file named FILE first in the directory containing
  7332. the current file, then in the quote directories, then the same
  7333. directories used for ‘>FILE<’. You can prepend directories to the
  7334. list of quote directories with the ‘-iquote’ option.
  7335. The argument of ‘#include’, whether delimited with quote marks or
  7336. angle brackets, behaves like a string constant in that comments are not
  7337. recognized, and macro names are not expanded. Thus, ‘#include <x/*y>’
  7338. specifies inclusion of a system header file named ‘x/*y’.
  7339. However, if backslashes occur within FILE, they are considered
  7340. ordinary text characters, not escape characters: character escape
  7341. sequences such as used in string constants in C are not meaningful here.
  7342. Thus, ‘#include "x\n\\y"’ specifies a filename containing three
  7343. backslashes. By the same token, there is no way to escape ‘"’ or ‘<’ to
  7344. include it in the header file name if it would instead end the file
  7345. name.
  7346. Some systems interpret ‘\’ as a file name component separator. All
  7347. these systems also interpret ‘/’ the same way. It is most portable to
  7348. use only ‘/’.
  7349. It is an error to put anything other than comments on the ‘#include’
  7350. line after the file name.
  7351. 26.4.2 ‘#include’ Operation
  7352. ---------------------------
  7353. The ‘#include’ directive works by scanning the specified header file as
  7354. input before continuing with the rest of the current file. The result
  7355. of preprocessing consists of the text already generated, followed by the
  7356. result of preprocessing the included file, followed by whatever results
  7357. from the text after the ‘#include’ directive. For example, if you have
  7358. a header file ‘header.h’ as follows,
  7359. char *test (void);
  7360. and a main program called ‘program.c’ that uses the header file, like
  7361. this,
  7362. int x;
  7363. #include "header.h"
  7364. int
  7365. main (void)
  7366. {
  7367. puts (test ());
  7368. }
  7369. the result is equivalent to putting this text in ‘program.c’:
  7370. int x;
  7371. char *test (void);
  7372. int
  7373. main (void)
  7374. {
  7375. puts (test ());
  7376. }
  7377. Included files are not limited to declarations and macro definitions;
  7378. those are merely the typical uses. Any fragment of a C program can be
  7379. included from another file. The include file could even contain the
  7380. beginning of a statement that is concluded in the containing file, or
  7381. the end of a statement that was started in the including file. However,
  7382. an included file must consist of complete tokens. Comments and string
  7383. literals that have not been closed by the end of an included file are
  7384. invalid. For error recovery, the compiler terminates them at the end of
  7385. the file.
  7386. To avoid confusion, it is best if header files contain only complete
  7387. syntactic units—function declarations or definitions, type declarations,
  7388. etc.
  7389. The line following the ‘#include’ directive is always treated as a
  7390. separate line, even if the included file lacks a final newline. There
  7391. is no problem putting a preprocessing directive there.
  7392. 26.4.3 Search Path
  7393. ------------------
  7394. GCC looks in several different places for header files to be included.
  7395. On the GNU system, and Unix systems, the default directories for system
  7396. header files are:
  7397. LIBDIR/gcc/TARGET/VERSION/include
  7398. /usr/local/include
  7399. LIBDIR/gcc/TARGET/VERSION/include-fixed
  7400. LIBDIR/TARGET/include
  7401. /usr/include/TARGET
  7402. /usr/include
  7403. The list may be different in some operating systems. Other directories
  7404. are added for C++.
  7405. In the above, TARGET is the canonical name of the system GCC was
  7406. configured to compile code for; often but not always the same as the
  7407. canonical name of the system it runs on. VERSION is the version of GCC
  7408. in use.
  7409. You can add to this list with the ‘-IDIR’ command-line option. All
  7410. the directories named by ‘-I’ are searched, in left-to-right order,
  7411. _before_ the default directories. The only exception is when ‘dir’ is
  7412. already searched by default. In this case, the option is ignored and
  7413. the search order for system directories remains unchanged.
  7414. Duplicate directories are removed from the quote and bracket search
  7415. chains before the two chains are merged to make the final search chain.
  7416. Thus, it is possible for a directory to occur twice in the final search
  7417. chain if it was specified in both the quote and bracket chains.
  7418. You can prevent GCC from searching any of the default directories
  7419. with the ‘-nostdinc’ option. This is useful when you are compiling an
  7420. operating system kernel or some other program that does not use the
  7421. standard C library facilities, or the standard C library itself. ‘-I’
  7422. options are not ignored as described above when ‘-nostdinc’ is in
  7423. effect.
  7424. GCC looks for headers requested with ‘#include "FILE"’ first in the
  7425. directory containing the current file, then in the “quote directories”
  7426. specified by ‘-iquote’ options, then in the same places it looks for a
  7427. system header. For example, if ‘/usr/include/sys/stat.h’ contains
  7428. ‘#include "types.h"’, GCC looks for ‘types.h’ first in
  7429. ‘/usr/include/sys’, then in the quote directories and then in its usual
  7430. search path.
  7431. ‘#line’ (*note Line Control::) does not change GCC’s idea of the
  7432. directory containing the current file.
  7433. The ‘-I-’ is an old-fashioned, deprecated way to specify the quote
  7434. directories. To look for headers in a directory named ‘-’, specify
  7435. ‘-I./-’. There are several more ways to adjust the header search path.
  7436. *Note Invoking GCC: (gcc)invocation.
  7437. 26.4.4 Once-Only Headers
  7438. ------------------------
  7439. If a header file happens to be included twice, the compiler will process
  7440. its contents twice. This is very likely to cause an error, e.g. when
  7441. the compiler sees the same structure definition twice.
  7442. The standard way to prevent this is to enclose the entire real
  7443. contents of the file in a conditional, like this:
  7444. /* File foo. */
  7445. #ifndef FILE_FOO_SEEN
  7446. #define FILE_FOO_SEEN
  7447. THE ENTIRE FILE
  7448. #endif /* !FILE_FOO_SEEN */
  7449. This construct is commonly known as a “wrapper #ifndef”. When the
  7450. header is included again, the conditional will be false, because
  7451. ‘FILE_FOO_SEEN’ is defined. Preprocessing skips over the entire
  7452. contents of the file, so that compilation will never “see” the file
  7453. contents twice in one module.
  7454. GCC optimizes this case even further. It remembers when a header
  7455. file has a wrapper ‘#ifndef’. If a subsequent ‘#include’ specifies that
  7456. header, and the macro in the ‘#ifndef’ is still defined, it does not
  7457. bother to rescan the file at all.
  7458. You can put comments in the header file outside the wrapper. They do
  7459. not interfere with this optimization.
  7460. The macro ‘FILE_FOO_SEEN’ is called the “controlling macro” or “guard
  7461. macro”. In a user header file, the macro name should not begin with
  7462. ‘_’. In a system header file, it should begin with ‘__’ (or ‘_’
  7463. followed by an upper-case letter) to avoid conflicts with user programs.
  7464. In any kind of header file, the macro name should contain the name of
  7465. the file and some additional text, to avoid conflicts with other header
  7466. files.
  7467. 26.4.5 Computed Includes
  7468. ------------------------
  7469. Sometimes it is necessary to select one of several different header
  7470. files to be included into your program. They might specify
  7471. configuration parameters to be used on different sorts of operating
  7472. systems, for instance. You could do this with a series of conditionals,
  7473. #if SYSTEM_1
  7474. # include "system_1.h"
  7475. #elif SYSTEM_2
  7476. # include "system_2.h"
  7477. #elif SYSTEM_3
  7478. /* ... */
  7479. #endif
  7480. That rapidly becomes tedious. Instead, GNU C offers the ability to
  7481. use a macro for the header name. This is called a “computed include”.
  7482. Instead of writing a header name as the direct argument of ‘#include’,
  7483. you simply put a macro name there instead:
  7484. #define SYSTEM_H "system_1.h"
  7485. /* ... */
  7486. #include SYSTEM_H
  7487. ‘SYSTEM_H’ is expanded, then ‘system_1.h’ is included as if the
  7488. ‘#include’ had been written with that name. ‘SYSTEM_H’ could be defined
  7489. by your Makefile with a ‘-D’ option.
  7490. You must be careful when you define such a macro. ‘#define’ saves
  7491. tokens, not text. GCC has no way of knowing that the macro will be used
  7492. as the argument of ‘#include’, so it generates ordinary tokens, not a
  7493. header name. This is unlikely to cause problems if you use double-quote
  7494. includes, which are syntactically similar to string constants. If you
  7495. use angle brackets, however, you may have trouble.
  7496. The syntax of a computed include is actually a bit more general than
  7497. the above. If the first non-whitespace character after ‘#include’ is
  7498. not ‘"’ or ‘>’, then the entire line is macro-expanded like running text
  7499. would be.
  7500. If the line expands to a single string constant, the contents of that
  7501. string constant are the file to be included. Preprocessing does not
  7502. re-examine the string for embedded quotes, but neither does it process
  7503. backslash escapes in the string. Therefore
  7504. #define HEADER "a\"b"
  7505. #include HEADER
  7506. looks for a file named ‘a\"b’. Preprocessing searches for the file
  7507. according to the rules for double-quoted includes.
  7508. If the line expands to a token stream beginning with a ‘>’token and
  7509. including a ‘<’ token, then the tokens between the ‘>’and the first ‘<’
  7510. are combined to form the filename to be included. Any whitespace
  7511. between tokens is reduced to a single space; then any space after the
  7512. initial ‘>’is retained, but a trailing space before the closing ‘<’ is
  7513. ignored. Preprocessing searches for the file according to the rules for
  7514. angle-bracket includes.
  7515. In either case, if there are any tokens on the line after the file
  7516. name, an error occurs and the directive is not processed. It is also an
  7517. error if the result of expansion does not match either of the two
  7518. expected forms.
  7519. These rules are implementation-defined behavior according to the C
  7520. standard. To minimize the risk of different compilers interpreting your
  7521. computed includes differently, we recommend you use only a single
  7522. object-like macro that expands to a string constant. That also makes it
  7523. clear to people reading your program.
  7524. 26.5 Macros
  7525. ===========
  7526. A “macro” is a fragment of code that has been given a name. Whenever
  7527. the name is used, it is replaced by the contents of the macro. There
  7528. are two kinds of macros. They differ mostly in what they look like when
  7529. they are used. “Object-like” macros resemble data objects when used,
  7530. “function-like” macros resemble function calls.
  7531. You may define any valid identifier as a macro, even if it is a C
  7532. keyword. In the preprocessing stage, GCC does not know anything about
  7533. keywords. This can be useful if you wish to hide a keyword such as
  7534. ‘const’ from an older compiler that does not understand it. However,
  7535. the preprocessing operator ‘defined’ (*note defined::) can never be
  7536. defined as a macro, and C‘++’’s named operators (*note C++ Named
  7537. Operators: (gcc)C++ Named Operators.) cannot be macros when compiling
  7538. C‘++’ code.
  7539. The operator ‘#’ is used in macros for stringification of an argument
  7540. (*note Stringification::), and ‘##’ is used for concatenation of
  7541. arguments into larger tokens (*note Concatenation::)
  7542. 26.5.1 Object-like Macros
  7543. -------------------------
  7544. An “object-like macro” is a simple identifier that will be replaced by a
  7545. code fragment. It is called object-like because in most cases the use
  7546. of the macro looks like reference to a data object in code that uses it.
  7547. These macros are most commonly used to give symbolic names to numeric
  7548. constants.
  7549. The way to define macros with the ‘#define’ directive. ‘#define’ is
  7550. followed by the name of the macro and then the token sequence it should
  7551. be an abbreviation for, which is variously referred to as the macro’s
  7552. “body”, “expansion” or “replacement list”. For example,
  7553. #define BUFFER_SIZE 1024
  7554. defines a macro named ‘BUFFER_SIZE’ as an abbreviation for the token
  7555. ‘1024’. If somewhere after this ‘#define’ directive there comes a C
  7556. statement of the form
  7557. foo = (char *) malloc (BUFFER_SIZE);
  7558. then preprocessing will recognize and “expand” the macro ‘BUFFER_SIZE’,
  7559. so that compilation will see the tokens:
  7560. foo = (char *) malloc (1024);
  7561. By convention, macro names are written in upper case. Programs are
  7562. easier to read when it is possible to tell at a glance which names are
  7563. macros. Macro names that start with ‘__’ are reserved for internal
  7564. uses, and many of them are defined automatically, so don’t define such
  7565. macro names unless you really know what you’re doing. Likewise for
  7566. macro names that start with ‘_’ and an upper-case letter.
  7567. The macro’s body ends at the end of the ‘#define’ line. You may
  7568. continue the definition onto multiple lines, if necessary, using
  7569. backslash-newline. When the macro is expanded, however, it will all
  7570. come out on one line. For example,
  7571. #define NUMBERS 1, \
  7572. 2, \
  7573. 3
  7574. int x[] = { NUMBERS };
  7575. ↦ int x[] = { 1, 2, 3 };
  7576. The most common visible consequence of this is surprising line numbers
  7577. in error messages.
  7578. There is no restriction on what can go in a macro body provided it
  7579. decomposes into valid preprocessing tokens. Parentheses need not
  7580. balance, and the body need not resemble valid C code. (If it does not,
  7581. you may get error messages from the C compiler when you use the macro.)
  7582. Preprocessing scans the program sequentially. A macro definition
  7583. takes effect right after its appearance. Therefore, the following input
  7584. foo = X;
  7585. #define X 4
  7586. bar = X;
  7587. produces
  7588. foo = X;
  7589. bar = 4;
  7590. When preprocessing expands a macro name, the macro’s expansion
  7591. replaces the macro invocation, then the expansion is examined for more
  7592. macros to expand. For example,
  7593. #define TABLESIZE BUFSIZE
  7594. #define BUFSIZE 1024
  7595. TABLESIZE
  7596. ↦ BUFSIZE
  7597. ↦ 1024
  7598. ‘TABLESIZE’ is expanded first to produce ‘BUFSIZE’, then that macro is
  7599. expanded to produce the final result, ‘1024’.
  7600. Notice that ‘BUFSIZE’ was not defined when ‘TABLESIZE’ was defined.
  7601. The ‘#define’ for ‘TABLESIZE’ uses exactly the expansion you specify—in
  7602. this case, ‘BUFSIZE’—and does not check to see whether it too contains
  7603. macro names. Only when you _use_ ‘TABLESIZE’ is the result of its
  7604. expansion scanned for more macro names.
  7605. This makes a difference if you change the definition of ‘BUFSIZE’ at
  7606. some point in the source file. ‘TABLESIZE’, defined as shown, will
  7607. always expand using the definition of ‘BUFSIZE’ that is currently in
  7608. effect:
  7609. #define BUFSIZE 1020
  7610. #define TABLESIZE BUFSIZE
  7611. #undef BUFSIZE
  7612. #define BUFSIZE 37
  7613. Now ‘TABLESIZE’ expands (in two stages) to ‘37’.
  7614. If the expansion of a macro contains its own name, either directly or
  7615. via intermediate macros, it is not expanded again when the expansion is
  7616. examined for more macros. This prevents infinite recursion. *Note
  7617. Self-Referential Macros::, for the precise details.
  7618. 26.5.2 Function-like Macros
  7619. ---------------------------
  7620. You can also define macros whose use looks like a function call. These
  7621. are called “function-like macros”. To define one, use the ‘#define’
  7622. directive with a pair of parentheses immediately after the macro name.
  7623. For example,
  7624. #define lang_init() c_init()
  7625. lang_init()
  7626. ↦ c_init()
  7627. A function-like macro is expanded only when its name appears with a
  7628. pair of parentheses after it. If you write just the name, without
  7629. parentheses, it is left alone. This can be useful when you have a
  7630. function and a macro of the same name, and you wish to use the function
  7631. sometimes. Whitespace and line breaks before or between the parentheses
  7632. are ignored when the macro is called.
  7633. extern void foo(void);
  7634. #define foo() /* optimized inline version */
  7635. /* ... */
  7636. foo();
  7637. funcptr = foo;
  7638. Here the call to ‘foo()’ expands the macro, but the function pointer
  7639. ‘funcptr’ gets the address of the real function ‘foo’. If the macro
  7640. were to be expanded there, it would cause a syntax error.
  7641. If you put spaces between the macro name and the parentheses in the
  7642. macro definition, that does not define a function-like macro, it defines
  7643. an object-like macro whose expansion happens to begin with a pair of
  7644. parentheses. Here is an example:
  7645. #define lang_init () c_init()
  7646. lang_init()
  7647. ↦ () c_init()()
  7648. The first two pairs of parentheses in this expansion come from the
  7649. macro. The third is the pair that was originally after the macro
  7650. invocation. Since ‘lang_init’ is an object-like macro, it does not
  7651. consume those parentheses.
  7652. Any name can have at most one macro definition at a time. Thus, you
  7653. can’t define the same name as an object-like macro and a function-like
  7654. macro at once.
  7655. 26.5.3 Macro Arguments
  7656. ----------------------
  7657. Function-like macros can take “arguments”, just like true functions. To
  7658. define a macro that uses arguments, you insert “parameters” between the
  7659. pair of parentheses in the macro definition that make the macro
  7660. function-like. The parameters must be valid C identifiers, separated by
  7661. commas and optionally whitespace.
  7662. To invoke a macro that takes arguments, you write the name of the
  7663. macro followed by a list of “actual arguments” in parentheses, separated
  7664. by commas. The invocation of the macro need not be restricted to a
  7665. single logical line—it can cross as many lines in the source file as you
  7666. wish. The number of arguments you give must match the number of
  7667. parameters in the macro definition. When the macro is expanded, each
  7668. use of a parameter in its body is replaced by the tokens of the
  7669. corresponding argument. (The macro body is not required to use all of
  7670. the parameters.)
  7671. As an example, here is a macro that computes the minimum of two
  7672. numeric values, as it is defined in many C programs, and some uses.
  7673. #define min(X, Y) ((X) < (Y) ? (X) : (Y))
  7674. x = min(a, b); ↦ x = ((a) < (b) ? (a) : (b));
  7675. y = min(1, 2); ↦ y = ((1) < (2) ? (1) : (2));
  7676. z = min(a+28, *p); ↦ z = ((a+28) < (*p) ? (a+28) : (*p));
  7677. In this small example you can already see several of the dangers of
  7678. macro arguments. *Note Macro Pitfalls::, for detailed explanations.
  7679. Leading and trailing whitespace in each argument is dropped, and all
  7680. whitespace between the tokens of an argument is reduced to a single
  7681. space. Parentheses within each argument must balance; a comma within
  7682. such parentheses does not end the argument. However, there is no
  7683. requirement for square brackets or braces to balance, and they do not
  7684. prevent a comma from separating arguments. Thus,
  7685. macro (array[x = y, x + 1])
  7686. passes two arguments to ‘macro’: ‘array[x = y’ and ‘x + 1]’. If you
  7687. want to supply ‘array[x = y, x + 1]’ as an argument, you can write it as
  7688. ‘array[(x = y, x + 1)]’, which is equivalent C code. However, putting
  7689. an assignment inside an array subscript is to be avoided anyway.
  7690. All arguments to a macro are completely macro-expanded before they
  7691. are substituted into the macro body. After substitution, the complete
  7692. text is scanned again for macros to expand, including the arguments.
  7693. This rule may seem strange, but it is carefully designed so you need not
  7694. worry about whether any function call is actually a macro invocation.
  7695. You can run into trouble if you try to be too clever, though. *Note
  7696. Argument Prescan::, for detailed discussion.
  7697. For example, ‘min (min (a, b), c)’ is first expanded to
  7698. min (((a) < (b) ? (a) : (b)), (c))
  7699. and then to
  7700. ((((a) < (b) ? (a) : (b))) < (c)
  7701. ? (((a) < (b) ? (a) : (b)))
  7702. : (c))
  7703. (The line breaks shown here for clarity are not actually generated.)
  7704. You can leave macro arguments empty without error, but many macros
  7705. will then expand to invalid code. You cannot leave out arguments
  7706. entirely; if a macro takes two arguments, there must be exactly one
  7707. comma at the top level of its argument list. Here are some silly
  7708. examples using ‘min’:
  7709. min(, b) ↦ (( ) < (b) ? ( ) : (b))
  7710. min(a, ) ↦ ((a ) < ( ) ? (a ) : ( ))
  7711. min(,) ↦ (( ) < ( ) ? ( ) : ( ))
  7712. min((,),) ↦ (((,)) < ( ) ? ((,)) : ( ))
  7713. min() error→ macro "min" requires 2 arguments, but only 1 given
  7714. min(,,) error→ macro "min" passed 3 arguments, but takes just 2
  7715. Whitespace is not a preprocessing token, so if a macro ‘foo’ takes
  7716. one argument, ‘foo ()’ and ‘foo ( )’ both supply it an empty argument.
  7717. Macro parameters appearing inside string literals are not replaced by
  7718. their corresponding actual arguments.
  7719. #define foo(x) x, "x"
  7720. foo(bar) ↦ bar, "x"
  7721. See the next subsection for how to insert macro arguments into a string
  7722. literal.
  7723. The token following the macro call and the last token of the macro
  7724. expansion do not become one token even if it looks like they could:
  7725. #define foo() abc
  7726. foo()def ↦ abc def
  7727. 26.5.4 Stringification
  7728. ----------------------
  7729. Sometimes you may want to convert a macro argument into a string
  7730. constant. Parameters are not replaced inside string constants, but you
  7731. can use the ‘#’ preprocessing operator instead. When a macro parameter
  7732. is used with a leading ‘#’, preprocessing replaces it with the literal
  7733. text of the actual argument, converted to a string constant. Unlike
  7734. normal parameter replacement, the argument is not macro-expanded first.
  7735. This is called “stringification”.
  7736. There is no way to combine an argument with surrounding text and
  7737. stringify it all together. But you can write a series of string
  7738. constants and stringified arguments. After preprocessing replaces the
  7739. stringified arguments with string constants, the consecutive string
  7740. constants will be concatenated into one long string constant (*note
  7741. String Constants::).
  7742. Here is an example that uses stringification and concatenation of
  7743. string constants:
  7744. #define WARN_IF(EXP) \
  7745. do { if (EXP) \
  7746. fprintf (stderr, "Warning: " #EXP "\n"); } \
  7747. while (0)
  7748. WARN_IF (x == 0);
  7749. do { if (x == 0)
  7750. fprintf (stderr, "Warning: " "x == 0" "\n"); }
  7751. while (0);
  7752. The argument for ‘EXP’ is substituted once, as is, into the ‘if’
  7753. statement, and once, stringified, into the argument to ‘fprintf’. If
  7754. ‘x’ were a macro, it would be expanded in the ‘if’ statement but not in
  7755. the string.
  7756. The ‘do’ and ‘while (0)’ are a kludge to make it possible to write
  7757. ‘WARN_IF (ARG);’. The resemblance of ‘WARN_IF’ to a function makes that
  7758. a natural way to write it. *Note Swallowing the Semicolon::.
  7759. Stringification in C involves more than putting double-quote
  7760. characters around the fragment. It also backslash-escapes the quotes
  7761. surrounding embedded string constants, and all backslashes within string
  7762. and character constants, in order to get a valid C string constant with
  7763. the proper contents. Thus, stringifying ‘p = "foo\n";’ results in
  7764. "p = \"foo\\n\";". However, backslashes that are not inside string or
  7765. character constants are not duplicated: ‘\n’ by itself stringifies to
  7766. "\n".
  7767. All leading and trailing whitespace in text being stringified is
  7768. ignored. Any sequence of whitespace in the middle of the text is
  7769. converted to a single space in the stringified result. Comments are
  7770. replaced by whitespace long before stringification happens, so they
  7771. never appear in stringified text.
  7772. There is no way to convert a macro argument into a character
  7773. constant.
  7774. To stringify the result of expansion of a macro argument, you have to
  7775. use two levels of macros, like this:
  7776. #define xstr(S) str(S)
  7777. #define str(s) #s
  7778. #define foo 4
  7779. str (foo)
  7780. ↦ "foo"
  7781. xstr (foo)
  7782. ↦ xstr (4)
  7783. ↦ str (4)
  7784. ↦ "4"
  7785. ‘s’ is stringified when it is used in ‘str’, so it is not
  7786. macro-expanded first. But ‘S’ is an ordinary argument to ‘xstr’, so it
  7787. is completely macro-expanded before ‘xstr’ itself is expanded (*note
  7788. Argument Prescan::). Therefore, by the time ‘str’ gets to its argument
  7789. text, that text already been macro-expanded.
  7790. 26.5.5 Concatenation
  7791. --------------------
  7792. It is often useful to merge two tokens into one while expanding macros.
  7793. This is called “token pasting” or “token concatenation”. The ‘##’
  7794. preprocessing operator performs token pasting. When a macro is
  7795. expanded, the two tokens on either side of each ‘##’ operator are
  7796. combined into a single token, which then replaces the ‘##’ and the two
  7797. original tokens in the macro expansion. Usually both will be
  7798. identifiers, or one will be an identifier and the other a preprocessing
  7799. number. When pasted, they make a longer identifier.
  7800. Concatenation into an identifier isn’t the only valid case. It is
  7801. also possible to concatenate two numbers (or a number and a name, such
  7802. as ‘1.5’ and ‘e3’) into a number. Also, multi-character operators such
  7803. as ‘+=’ can be formed by token pasting.
  7804. However, two tokens that don’t together form a valid token cannot be
  7805. pasted together. For example, you cannot concatenate ‘x’ with ‘+’, not
  7806. in either order. Trying this issues a warning and keeps the two tokens
  7807. separate. Whether it puts white space between the tokens is undefined.
  7808. It is common to find unnecessary uses of ‘##’ in complex macros. If you
  7809. get this warning, it is likely that you can simply remove the ‘##’.
  7810. The tokens combined by ‘##’ could both come from the macro body, but
  7811. then you could just as well write them as one token in the first place.
  7812. Token pasting is useful when one or both of the tokens comes from a
  7813. macro argument. If either of the tokens next to an ‘##’ is a parameter
  7814. name, it is replaced by its actual argument before ‘##’ executes. As
  7815. with stringification, the actual argument is not macro-expanded first.
  7816. If the argument is empty, that ‘##’ has no effect.
  7817. Keep in mind that preprocessing converts comments to whitespace
  7818. before it looks for uses of macros. Therefore, you cannot create a
  7819. comment by concatenating ‘/’ and ‘*’. You can put as much whitespace
  7820. between ‘##’ and its operands as you like, including comments, and you
  7821. can put comments in arguments that will be concatenated.
  7822. It is an error to use ‘##’ at the beginning or end of a macro body.
  7823. Multiple ‘##’ operators are handled left-to-right, so that ‘1 ## e ##
  7824. -2’ pastes into ‘1e-2’. (Right-to-left processing would first generate
  7825. ‘e-2’, which is an invalid token.) When ‘#’ and ‘##’ are used together,
  7826. they are all handled left-to-right.
  7827. Consider a C program that interprets named commands. There probably
  7828. needs to be a table of commands, perhaps an array of structures declared
  7829. as follows:
  7830. struct command
  7831. {
  7832. char *name;
  7833. void (*function) (void);
  7834. };
  7835. struct command commands[] =
  7836. {
  7837. { "quit", quit_command },
  7838. { "help", help_command },
  7839. /* ... */
  7840. };
  7841. It would be cleaner not to have to write each command name twice,
  7842. once in the string constant and once in the function name. A macro that
  7843. takes the name of a command as an argument can make this unnecessary.
  7844. It can create the string constant with stringification, and the function
  7845. name by concatenating the argument with ‘_command’. Here is how it is
  7846. done:
  7847. #define COMMAND(NAME) { #NAME, NAME ## _command }
  7848. struct command commands[] =
  7849. {
  7850. COMMAND (quit),
  7851. COMMAND (help),
  7852. /* ... */
  7853. };
  7854. 26.5.6 Variadic Macros
  7855. ----------------------
  7856. A macro can be declared to accept a variable number of arguments much as
  7857. a function can. The syntax for defining the macro is similar to that of
  7858. a function. Here is an example:
  7859. #define eprintf(...) fprintf (stderr, __VA_ARGS__)
  7860. This kind of macro is called “variadic”. When the macro is invoked,
  7861. all the tokens in its argument list after the last named argument (this
  7862. macro has none), including any commas, become the “variable argument”.
  7863. This sequence of tokens replaces the identifier ‘__VA_ARGS__’ in the
  7864. macro body wherever it appears. Thus, we have this expansion:
  7865. eprintf ("%s:%d: ", input_file, lineno)
  7866. ↦ fprintf (stderr, "%s:%d: ", input_file, lineno)
  7867. The variable argument is completely macro-expanded before it is
  7868. inserted into the macro expansion, just like an ordinary argument. You
  7869. may use the ‘#’ and ‘##’ operators to stringify the variable argument or
  7870. to paste its leading or trailing token with another token. (But see
  7871. below for an important special case for ‘##’.)
  7872. *Warning:* don’t use the identifier ‘__VA_ARGS__’ for anything other
  7873. than this.
  7874. If your macro is complicated, you may want a more descriptive name
  7875. for the variable argument than ‘__VA_ARGS__’. You can write an argument
  7876. name immediately before the ‘...’; that name is used for the variable
  7877. argument.(1) The ‘eprintf’ macro above could be written thus:
  7878. #define eprintf(args...) fprintf (stderr, args)
  7879. A variadic macro can have named arguments as well as variable
  7880. arguments, so ‘eprintf’ can be defined like this, instead:
  7881. #define eprintf(format, ...) \
  7882. fprintf (stderr, format, __VA_ARGS__)
  7883. This formulation is more descriptive, but what if you want to specify a
  7884. format string that takes no arguments? In GNU C, you can omit the comma
  7885. before the variable arguments if they are empty, but that puts an extra
  7886. comma in the expansion:
  7887. eprintf ("success!\n")
  7888. ↦ fprintf(stderr, "success!\n", );
  7889. That’s an error in the call to ‘fprintf’.
  7890. To get rid of that comma, the ‘##’ token paste operator has a special
  7891. meaning when placed between a comma and a variable argument.(2) If you
  7892. write
  7893. #define eprintf(format, ...) \
  7894. fprintf (stderr, format, ##__VA_ARGS__)
  7895. then use the macro ‘eprintf’ with empty variable arguments, ‘##’ deletes
  7896. the preceding comma.
  7897. eprintf ("success!\n")
  7898. ↦ fprintf(stderr, "success!\n");
  7899. This does _not_ happen if you pass an empty argument, nor does it happen
  7900. if the token preceding ‘##’ is anything other than a comma.
  7901. When the only macro parameter is a variable arguments parameter, and the
  7902. macro call has no argument at all, it is not obvious whether that means
  7903. an empty argument or a missing argument. Should the comma be kept, or
  7904. deleted? The C standard says to keep the comma, but the preexisting GNU
  7905. C extension deleted the comma. Nowadays, GNU C retains the comma when
  7906. implementing a specific C standard, and deletes it otherwise.
  7907. C99 mandates that the only place the identifier ‘__VA_ARGS__’ can
  7908. appear is in the replacement list of a variadic macro. It may not be
  7909. used as a macro name, macro parameter name, or within a different type
  7910. of macro. It may also be forbidden in open text; the standard is
  7911. ambiguous. We recommend you avoid using that name except for its
  7912. special purpose.
  7913. Variadic macros where you specify the parameter name is a GNU C
  7914. feature that has been supported for a long time. Standard C, as of C99,
  7915. supports only the form where the parameter is called ‘__VA_ARGS__’. For
  7916. portability to previous versions of GNU C you should use only named
  7917. variable argument parameters. On the other hand, for portability to
  7918. other C99 compilers, you should use only ‘__VA_ARGS__’.
  7919. ---------- Footnotes ----------
  7920. (1) GNU C extension.
  7921. (2) GNU C extension.
  7922. 26.5.7 Predefined Macros
  7923. ------------------------
  7924. Several object-like macros are predefined; you use them without
  7925. supplying their definitions. Here we explain the ones user programs
  7926. often need to use. Many other macro names starting with ‘__’ are
  7927. predefined; in general, you should not define such macro names yourself.
  7928. ‘__FILE__’
  7929. This macro expands to the name of the current input file, in the
  7930. form of a C string constant. This is the full name by which the
  7931. GCC opened the file, not the short name specified in ‘#include’ or
  7932. as the input file name argument. For example,
  7933. ‘"/usr/local/include/myheader.h"’ is a possible expansion of this
  7934. macro.
  7935. ‘__LINE__’
  7936. This macro expands to the current input line number, in the form of
  7937. a decimal integer constant. While we call it a predefined macro,
  7938. it’s a pretty strange macro, since its “definition” changes with
  7939. each new line of source code.
  7940. ‘__func__’
  7941. ‘__FUNCTION__’
  7942. These names are like variables that have as value a string
  7943. containing the name of the current function definition. They are
  7944. not really macros, but this is the best place to mention them.
  7945. ‘__FUNCTION__’ is the name that has been defined in GNU C since
  7946. time immemorial; ‘__func__’ is defined by the C standard. With the
  7947. following conditionals, you can use whichever one is defined.
  7948. #if __STDC_VERSION__ < 199901L
  7949. # if __GNUC__ <= 2
  7950. # define __func__ __FUNCTION__
  7951. # else
  7952. # define __func__ ">unknown<"
  7953. # endif
  7954. #endif
  7955. ‘__PRETTY_FUNCTION__’
  7956. This is equivalent to ‘__FUNCTION__’ in C, but in C‘++’ the string
  7957. includes argument type information as well. It is a GNU C
  7958. extension.
  7959. Those features are useful in generating an error message to report an
  7960. inconsistency detected by the program; the message can state the source
  7961. line where the inconsistency was detected. For example,
  7962. fprintf (stderr, "Internal error: "
  7963. "negative string length "
  7964. "in function %s "
  7965. "%d at %s, line %d.",
  7966. __func__, length, __FILE__, __LINE__);
  7967. A ‘#line’ directive changes ‘__LINE__’, and may change ‘__FILE__’ as
  7968. well. *Note Line Control::.
  7969. ‘__DATE__’
  7970. This macro expands to a string constant that describes the date of
  7971. compilation. The string constant contains eleven characters and
  7972. looks like ‘"Feb 12 1996"’. If the day of the month is just one
  7973. digit, an extra space precedes it so that the date is always eleven
  7974. characters.
  7975. If the compiler cannot determine the current date, it emits a
  7976. warning messages (once per compilation) and ‘__DATE__’ expands to
  7977. ‘"??? ?? ????"’.
  7978. We deprecate the use of ‘__DATE__’ for the sake of reproducible
  7979. compilation.
  7980. ‘__TIME__’
  7981. This macro expands to a string constant that describes the time of
  7982. compilation. The string constant contains eight characters and
  7983. looks like ‘"23:59:01"’.
  7984. If the compiler cannot determine the current time, it emits a
  7985. warning message (once per compilation) and ‘__TIME__’ expands to
  7986. ‘"??:??:??"’.
  7987. We deprecate the use of ‘__TIME__’ for the sake of reproducible
  7988. compilation.
  7989. ‘__STDC__’
  7990. In normal operation, this macro expands to the constant 1, to
  7991. signify that this compiler implements ISO Standard C.
  7992. ‘__STDC_VERSION__’
  7993. This macro expands to the C Standard’s version number, a long
  7994. integer constant of the form ‘YYYYMML’ where YYYY and MM are the
  7995. year and month of the Standard version. This states which version
  7996. of the C Standard the compiler implements.
  7997. The current default value is ‘201112L’, which signifies the C 2011
  7998. standard.
  7999. ‘__STDC_HOSTED__’
  8000. This macro is defined, with value 1, if the compiler’s target is a
  8001. “hosted environment”. A hosted environment provides the full
  8002. facilities of the standard C library.
  8003. The rest of the predefined macros are GNU C extensions.
  8004. ‘__COUNTER__’
  8005. This macro expands to sequential integral values starting from 0.
  8006. In other words, each time the program uses this acro, it generates
  8007. the next successive integer. This, with the ‘##’ operator,
  8008. provides a convenient means for macros to generate unique
  8009. identifiers.
  8010. ‘__GNUC__’
  8011. ‘__GNUC_MINOR__’
  8012. ‘__GNUC_PATCHLEVEL__’
  8013. These macros expand to the major version, minor version, and patch
  8014. level of the compiler, as integer constants. For example, GCC
  8015. 3.2.1 expands ‘__GNUC__’ to 3, ‘__GNUC_MINOR__’ to 2, and
  8016. ‘__GNUC_PATCHLEVEL__’ to 1.
  8017. If all you need to know is whether or not your program is being
  8018. compiled by GCC, or a non-GCC compiler that claims to accept the
  8019. GNU C extensions, you can simply test ‘__GNUC__’. If you need to
  8020. write code that depends on a specific version, you must check more
  8021. carefully. Each change in the minor version resets the patch level
  8022. to zero; each change in the major version (which happens rarely)
  8023. resets the minor version and the patch level to zero. To use the
  8024. predefined macros directly in the conditional, write it like this:
  8025. /* Test for version 3.2.0 or later. */
  8026. #if __GNUC__ &tt; 3 || \
  8027. (__GNUC__ == 3 && (__GNUC_MINOR__ &tt; 2 || \
  8028. (__GNUC_MINOR__ == 2 && \
  8029. __GNUC_PATCHLEVEL__ &tt; 0))
  8030. Another approach is to use the predefined macros to calculate a
  8031. single number, then compare that against a threshold:
  8032. #define GCC_VERSION (__GNUC__ * 10000 \
  8033. + __GNUC_MINOR__ * 100 \
  8034. + __GNUC_PATCHLEVEL__)
  8035. /* ... */
  8036. /* Test for GCC &tt; 3.2.0 */
  8037. #if GCC_VERSION &tt; 30200
  8038. Many people find this form easier to understand.
  8039. ‘__VERSION__’
  8040. This macro expands to a string constant that describes the version
  8041. of the compiler in use. You should not rely on its contents’
  8042. having any particular form, but you can count on it to contain at
  8043. least the release number.
  8044. ‘__TIMESTAMP__’
  8045. This macro expands to a string constant that describes the date and
  8046. time of the last modification of the current source file. The
  8047. string constant contains abbreviated day of the week, month, day of
  8048. the month, time in hh:mm:ss form, and the year, in the format
  8049. ‘"Sun Sep 16 01:03:52 1973"’. If the day of the month is less than
  8050. 10, it is padded with a space on the left.
  8051. If GCC cannot determine that information date, it emits a warning
  8052. message (once per compilation) and ‘__TIMESTAMP__’ expands to
  8053. ‘"??? ??? ?? ??:??:?? ????"’.
  8054. We deprecate the use of this macro for the sake of reproducible
  8055. compilation.
  8056. 26.5.8 Undefining and Redefining Macros
  8057. ---------------------------------------
  8058. You can “undefine” a macro with the ‘#undef’ directive. ‘#undef’ takes
  8059. a single argument, the name of the macro to undefine. You use the bare
  8060. macro name, even if the macro is function-like. It is an error if
  8061. anything appears on the line after the macro name. ‘#undef’ has no
  8062. effect if the name is not a macro.
  8063. #define FOO 4
  8064. x = FOO; ↦ x = 4;
  8065. #undef FOO
  8066. x = FOO; ↦ x = FOO;
  8067. Once a macro has been undefined, that identifier may be “redefined”
  8068. as a macro by a subsequent ‘#define’ directive. The new definition need
  8069. not have any resemblance to the old definition.
  8070. You can define a macro again without first undefining it only if the
  8071. new definition is “effectively the same” as the old one. Two macro
  8072. definitions are effectively the same if:
  8073. • Both are the same type of macro (object- or function-like).
  8074. • All the tokens of the replacement list are the same.
  8075. • If there are any parameters, they are the same.
  8076. • Whitespace appears in the same places in both. It need not be
  8077. exactly the same amount of whitespace, though. Remember that
  8078. comments count as whitespace.
  8079. These definitions are effectively the same:
  8080. #define FOUR (2 + 2)
  8081. #define FOUR (2 + 2)
  8082. #define FOUR (2 /* two */ + 2)
  8083. but these are not:
  8084. #define FOUR (2 + 2)
  8085. #define FOUR ( 2+2 )
  8086. #define FOUR (2 * 2)
  8087. #define FOUR(score,and,seven,years,ago) (2 + 2)
  8088. This allows two different header files to define a common macro.
  8089. You can redefine an existing macro with #define, but redefining an
  8090. existing macro name with a different definition results in a warning.
  8091. 26.5.9 Directives Within Macro Arguments
  8092. ----------------------------------------
  8093. GNU C permits and handles preprocessing directives in the text provided
  8094. as arguments for a macro. That case is undefined in the C standard.
  8095. but in GNU C conditional directives in macro arguments are clear and
  8096. valid.
  8097. A paradoxical case is to redefine a macro within the call to that
  8098. same macro. What happens is, the new definition takes effect in time
  8099. for pre-expansion of _all_ the arguments, then the original definition
  8100. is expanded to replace the call. Here is a pathological example:
  8101. #define f(x) x x
  8102. f (first f second
  8103. #undef f
  8104. #define f 2
  8105. f)
  8106. which expands to
  8107. first 2 second 2 first 2 second 2
  8108. with the semantics described above. We suggest you avoid writing code
  8109. which does this sort of thing.
  8110. 26.5.10 Macro Pitfalls
  8111. ----------------------
  8112. In this section we describe some special rules that apply to macros and
  8113. macro expansion, and point out certain cases in which the rules have
  8114. counter-intuitive consequences that you must watch out for.
  8115. 26.5.10.1 Misnesting
  8116. ....................
  8117. When a macro is called with arguments, the arguments are substituted
  8118. into the macro body and the result is checked, together with the rest of
  8119. the input file, for more macro calls. It is possible to piece together
  8120. a macro call coming partially from the macro body and partially from the
  8121. arguments. For example,
  8122. #define twice(x) (2*(x))
  8123. #define call_with_1(x) x(1)
  8124. call_with_1 (twice)
  8125. ↦ twice(1)
  8126. ↦ (2*(1))
  8127. Macro definitions do not have to have balanced parentheses. By
  8128. writing an unbalanced open parenthesis in a macro body, it is possible
  8129. to create a macro call that begins inside the macro body but ends
  8130. outside of it. For example,
  8131. #define strange(file) fprintf (file, "%s %d",
  8132. /* ... */
  8133. strange(stderr) p, 35)
  8134. ↦ fprintf (stderr, "%s %d", p, 35)
  8135. The ability to piece together a macro call can be useful, but the use
  8136. of unbalanced open parentheses in a macro body is just confusing, and
  8137. should be avoided.
  8138. 26.5.10.2 Operator Precedence Problems
  8139. ......................................
  8140. You may have noticed that in most of the macro definition examples shown
  8141. above, each occurrence of a macro parameter name had parentheses around
  8142. it. In addition, another pair of parentheses usually surrounds the
  8143. entire macro definition. Here is why it is best to write macros that
  8144. way.
  8145. Suppose you define a macro as follows,
  8146. #define ceil_div(x, y) (x + y - 1) / y
  8147. whose purpose is to divide, rounding up. (One use for this operation is
  8148. to compute how many ‘int’ objects are needed to hold a certain number of
  8149. ‘char’ objects.) Then suppose it is used as follows:
  8150. a = ceil_div (b & c, sizeof (int));
  8151. ↦ a = (b & c + sizeof (int) - 1) / sizeof (int);
  8152. This does not do what is intended. The operator-precedence rules of C
  8153. make it equivalent to this:
  8154. a = (b & (c + sizeof (int) - 1)) / sizeof (int);
  8155. What we want is this:
  8156. a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
  8157. Defining the macro as
  8158. #define ceil_div(x, y) ((x) + (y) - 1) / (y)
  8159. provides the desired result.
  8160. Unintended grouping can result in another way. Consider ‘sizeof
  8161. ceil_div(1, 2)’. That has the appearance of a C expression that would
  8162. compute the size of the type of ‘ceil_div (1, 2)’, but in fact it means
  8163. something very different. Here is what it expands to:
  8164. sizeof ((1) + (2) - 1) / (2)
  8165. This would take the size of an integer and divide it by two. The
  8166. precedence rules have put the division outside the ‘sizeof’ when it was
  8167. intended to be inside.
  8168. Parentheses around the entire macro definition prevent such problems.
  8169. Here, then, is the recommended way to define ‘ceil_div’:
  8170. #define ceil_div(x, y) (((x) + (y) - 1) / (y))
  8171. 26.5.10.3 Swallowing the Semicolon
  8172. ..................................
  8173. Often it is desirable to define a macro that expands into a compound
  8174. statement. Consider, for example, the following macro, that advances a
  8175. pointer (the parameter ‘p’ says where to find it) across whitespace
  8176. characters:
  8177. #define SKIP_SPACES(p, limit) \
  8178. { char *lim = (limit); \
  8179. while (p < lim) { \
  8180. if (*p++ != ' ') { \
  8181. p--; break; }}}
  8182. Here backslash-newline is used to split the macro definition, which must
  8183. be a single logical line, so that it resembles the way such code would
  8184. be laid out if not part of a macro definition.
  8185. A call to this macro might be ‘SKIP_SPACES (p, lim)’. Strictly
  8186. speaking, the call expands to a compound statement, which is a complete
  8187. statement with no need for a semicolon to end it. However, since it
  8188. looks like a function call, it minimizes confusion if you can use it
  8189. like a function call, writing a semicolon afterward, as in ‘SKIP_SPACES
  8190. (p, lim);’
  8191. This can cause trouble before ‘else’ statements, because the
  8192. semicolon is actually a null statement. Suppose you write
  8193. if (*p != 0)
  8194. SKIP_SPACES (p, lim);
  8195. else /* ... */
  8196. The presence of two statements—the compound statement and a null
  8197. statement—in between the ‘if’ condition and the ‘else’ makes invalid C
  8198. code.
  8199. The definition of the macro ‘SKIP_SPACES’ can be altered to solve
  8200. this problem, using a ‘do ... while’ statement. Here is how:
  8201. #define SKIP_SPACES(p, limit) \
  8202. do { char *lim = (limit); \
  8203. while (p < lim) { \
  8204. if (*p++ != ' ') { \
  8205. p--; break; }}} \
  8206. while (0)
  8207. Now ‘SKIP_SPACES (p, lim);’ expands into
  8208. do { /* ... */ } while (0);
  8209. which is one statement. The loop executes exactly once; most compilers
  8210. generate no extra code for it.
  8211. 26.5.10.4 Duplication of Side Effects
  8212. .....................................
  8213. Many C programs define a macro ‘min’, for “minimum”, like this:
  8214. #define min(X, Y) ((X) < (Y) ? (X) : (Y))
  8215. When you use this macro with an argument containing a side effect, as
  8216. shown here,
  8217. next = min (x + y, foo (z));
  8218. it expands as follows:
  8219. next = ((x + y) < (foo (z)) ? (x + y) : (foo (z)));
  8220. where ‘x + y’ has been substituted for ‘X’ and ‘foo (z)’ for ‘Y’.
  8221. The function ‘foo’ is used only once in the statement as it appears
  8222. in the program, but the expression ‘foo (z)’ has been substituted twice
  8223. into the macro expansion. As a result, ‘foo’ might be called twice when
  8224. the statement is executed. If it has side effects or if it takes a long
  8225. time to compute, that may be undesirable. We say that ‘min’ is an
  8226. “unsafe” macro.
  8227. The best solution to this problem is to define ‘min’ in a way that
  8228. computes the value of ‘foo (z)’ only once. In general, that requires
  8229. using ‘__auto_type’ (*note Auto Type::). How to use it for this is
  8230. described in the following section. *Note Macros and Auto Type::.
  8231. Otherwise, you will need to be careful when _using_ the macro ‘min’.
  8232. For example, you can calculate the value of ‘foo (z)’, save it in a
  8233. variable, and use that variable in ‘min’:
  8234. #define min(X, Y) ((X) < (Y) ? (X) : (Y))
  8235. /* ... */
  8236. {
  8237. int tem = foo (z);
  8238. next = min (x + y, tem);
  8239. }
  8240. (where we assume that ‘foo’ returns type ‘int’).
  8241. When the repeated value appears as the condition of the ‘?:’ operator
  8242. and again as its IFTRUE expression, you can avoid repeated execution by
  8243. omitting the IFTRUE expression, like this:
  8244. #define x_or_y(X, Y) ((X) ? : (Y))
  8245. In GNU C, this expands to use the first macro argument’s value if that
  8246. isn’t zero. If that’s zero, it compiles the second argument and uses
  8247. that value. *Note Conditional Expression::.
  8248. 26.5.10.5 Using ‘__auto_type’ for Local Variables
  8249. .................................................
  8250. The operator ‘__auto_type’ makes it possible to define macros that can
  8251. work on any data type even though they need to generate local variable
  8252. declarations. *Note Auto Type::.
  8253. For instance, here’s how to define a safe “maximum” macro that
  8254. operates on any arithmetic type and computes each of its arguments
  8255. exactly once:
  8256. #define max(a,b) \
  8257. ({ __auto_type _a = (a); \
  8258. __auto_type _b = (b); \
  8259. _a &tt; _b ? _a : _b; })
  8260. The ‘({ ... })’ notation produces “statement expression”—a statement
  8261. that can be used as an expression (*note Statement Exprs::). Its value
  8262. is the value of its last statement. This permits us to define local
  8263. variables and store each argument value into one.
  8264. The reason for using names that start with underscores for the local
  8265. variables is to avoid conflicts with variable names that occur within
  8266. the expressions that are substituted for ‘a’ and ‘b’. Underscore
  8267. followed by a lower case letter won’t be predefined by the system in any
  8268. way.
  8269. 26.5.10.6 Self-Referential Macros
  8270. .................................
  8271. A “self-referential” macro is one whose name appears in its definition.
  8272. Recall that all macro definitions are rescanned for more macros to
  8273. replace. If the self-reference were considered a use of the macro, it
  8274. would produce an infinitely large expansion. To prevent this, the
  8275. self-reference is not considered a macro call: preprocessing leaves it
  8276. unchanged. Consider an example:
  8277. #define foo (4 + foo)
  8278. where ‘foo’ is also a variable in your program.
  8279. Following the ordinary rules, each reference to ‘foo’ will expand
  8280. into ‘(4 + foo)’; then this will be rescanned and will expand into ‘(4 +
  8281. (4 + foo))’; and so on until the computer runs out of memory.
  8282. The self-reference rule cuts this process short after one step, at
  8283. ‘(4 + foo)’. Therefore, this macro definition has the possibly useful
  8284. effect of causing the program to add 4 to the value of ‘foo’ wherever
  8285. ‘foo’ is referred to.
  8286. In most cases, it is a bad idea to take advantage of this feature. A
  8287. person reading the program who sees that ‘foo’ is a variable will not
  8288. expect that it is a macro as well. The reader will come across the
  8289. identifier ‘foo’ in the program and think its value should be that of
  8290. the variable ‘foo’, whereas in fact the value is four greater.
  8291. It is useful to make a macro definition that expands to the macro
  8292. name itself. If you write
  8293. #define EPERM EPERM
  8294. then the macro ‘EPERM’ expands to ‘EPERM’. Effectively, preprocessing
  8295. leaves it unchanged in the source code. You can tell that it’s a macro
  8296. with ‘#ifdef’. You might do this if you want to define numeric
  8297. constants with an ‘enum’, but have ‘#ifdef’ be true for each constant.
  8298. If a macro ‘x’ expands to use a macro ‘y’, and the expansion of ‘y’
  8299. refers to the macro ‘x’, that is an “indirect self-reference” of ‘x’.
  8300. ‘x’ is not expanded in this case either. Thus, if we have
  8301. #define x (4 + y)
  8302. #define y (2 * x)
  8303. then ‘x’ and ‘y’ expand as follows:
  8304. x ↦ (4 + y)
  8305. ↦ (4 + (2 * x))
  8306. y ↦ (2 * x)
  8307. ↦ (2 * (4 + y))
  8308. Each macro is expanded when it appears in the definition of the other
  8309. macro, but not when it indirectly appears in its own definition.
  8310. 26.5.10.7 Argument Prescan
  8311. ..........................
  8312. Macro arguments are completely macro-expanded before they are
  8313. substituted into a macro body, unless they are stringified or pasted
  8314. with other tokens. After substitution, the entire macro body, including
  8315. the substituted arguments, is scanned again for macros to be expanded.
  8316. The result is that the arguments are scanned _twice_ to expand macro
  8317. calls in them.
  8318. Most of the time, this has no effect. If the argument contained any
  8319. macro calls, they were expanded during the first scan. The result
  8320. therefore contains no macro calls, so the second scan does not change
  8321. it. If the argument were substituted as given, with no prescan, the
  8322. single remaining scan would find the same macro calls and produce the
  8323. same results.
  8324. You might expect the double scan to change the results when a
  8325. self-referential macro is used in an argument of another macro (*note
  8326. Self-Referential Macros::): the self-referential macro would be expanded
  8327. once in the first scan, and a second time in the second scan. However,
  8328. this is not what happens. The self-references that do not expand in the
  8329. first scan are marked so that they will not expand in the second scan
  8330. either.
  8331. You might wonder, “Why mention the prescan, if it makes no
  8332. difference? And why not skip it and make preprocessing go faster?” The
  8333. answer is that the prescan does make a difference in three special
  8334. cases:
  8335. • Nested calls to a macro.
  8336. We say that “nested” calls to a macro occur when a macro’s argument
  8337. contains a call to that very macro. For example, if ‘f’ is a macro
  8338. that expects one argument, ‘f (f (1))’ is a nested pair of calls to
  8339. ‘f’. The desired expansion is made by expanding ‘f (1)’ and
  8340. substituting that into the definition of ‘f’. The prescan causes
  8341. the expected result to happen. Without the prescan, ‘f (1)’ itself
  8342. would be substituted as an argument, and the inner use of ‘f’ would
  8343. appear during the main scan as an indirect self-reference and would
  8344. not be expanded.
  8345. • Macros that call other macros that stringify or concatenate.
  8346. If an argument is stringified or concatenated, the prescan does not
  8347. occur. If you _want_ to expand a macro, then stringify or
  8348. concatenate its expansion, you can do that by causing one macro to
  8349. call another macro that does the stringification or concatenation.
  8350. For instance, if you have
  8351. #define AFTERX(x) X_ ## x
  8352. #define XAFTERX(x) AFTERX(x)
  8353. #define TABLESIZE 1024
  8354. #define BUFSIZE TABLESIZE
  8355. then ‘AFTERX(BUFSIZE)’ expands to ‘X_BUFSIZE’, and
  8356. ‘XAFTERX(BUFSIZE)’ expands to ‘X_1024’. (Not to ‘X_TABLESIZE’.
  8357. Prescan always does a complete expansion.)
  8358. • Macros used in arguments, whose expansions contain unshielded
  8359. commas.
  8360. This can cause a macro expanded on the second scan to be called
  8361. with the wrong number of arguments. Here is an example:
  8362. #define foo a,b
  8363. #define bar(x) lose(x)
  8364. #define lose(x) (1 + (x))
  8365. We would like ‘bar(foo)’ to turn into ‘(1 + (foo))’, which would
  8366. then turn into ‘(1 + (a,b))’. Instead, ‘bar(foo)’ expands into
  8367. ‘lose(a,b)’, which gives an error because ‘lose’ requires a single
  8368. argument. In this case, the problem is easily solved by the same
  8369. parentheses that ought to be used to prevent misnesting of
  8370. arithmetic operations:
  8371. #define foo (a,b)
  8372. or
  8373. #define bar(x) lose((x))
  8374. The extra pair of parentheses prevents the comma in ‘foo’’s
  8375. definition from being interpreted as an argument separator.
  8376. 26.6 Conditionals
  8377. =================
  8378. A “conditional” is a preprocessing directive that controls whether or
  8379. not to include a chunk of code in the final token stream that is
  8380. compiled. Preprocessing conditionals can test arithmetic expressions,
  8381. or whether a name is defined as a macro, or both together using the
  8382. special ‘defined’ operator.
  8383. A preprocessing conditional in C resembles in some ways an ‘if’
  8384. statement in C, but it is important to understand the difference between
  8385. them. The condition in an ‘if’ statement is tested during the execution
  8386. of your program. Its purpose is to allow your program to behave
  8387. differently from run to run, depending on the data it is operating on.
  8388. The condition in a preprocessing conditional directive is tested when
  8389. your program is compiled. Its purpose is to allow different code to be
  8390. included in the program depending on the situation at the time of
  8391. compilation.
  8392. Sometimes this distinction makes no practical difference. GCC and
  8393. other modern compilers often do test ‘if’ statements when a program is
  8394. compiled, if their conditions are known not to vary at run time, and
  8395. eliminate code that can never be executed. If you can count on your
  8396. compiler to do this, you may find that your program is more readable if
  8397. you use ‘if’ statements with constant conditions (perhaps determined by
  8398. macros). Of course, you can only use this to exclude code, not type
  8399. definitions or other preprocessing directives, and you can only do it if
  8400. the file remains syntactically valid when that code is not used.
  8401. 26.6.1 Uses of Conditional Directives
  8402. -------------------------------------
  8403. There are three usual reasons to use a preprocessing conditional.
  8404. • A program may need to use different code depending on the machine
  8405. or operating system it is to run on. In some cases the code for
  8406. one operating system may be erroneous on another operating system;
  8407. for example, it might refer to data types or constants that do not
  8408. exist on the other system. When this happens, it is not enough to
  8409. avoid executing the invalid code. Its mere presence will cause the
  8410. compiler to reject the program. With a preprocessing conditional,
  8411. the offending code can be effectively excised from the program when
  8412. it is not valid.
  8413. • You may want to be able to compile the same source file into two
  8414. different programs. One version might make frequent time-consuming
  8415. consistency checks on its intermediate data, or print the values of
  8416. those data for debugging, and the other not.
  8417. • A conditional whose condition is always false is one way to exclude
  8418. code from the program but keep it as a sort of comment for future
  8419. reference.
  8420. Simple programs that do not need system-specific logic or complex
  8421. debugging hooks generally will not need to use preprocessing
  8422. conditionals.
  8423. 26.6.2 Syntax of Preprocessing Conditionals
  8424. -------------------------------------------
  8425. A preprocessing conditional begins with a “conditional directive”:
  8426. ‘#if’, ‘#ifdef’ or ‘#ifndef’.
  8427. 26.6.2.1 The ‘#ifdef’ directive
  8428. ...............................
  8429. The simplest sort of conditional is
  8430. #ifdef MACRO
  8431. CONTROLLED TEXT
  8432. #endif /* MACRO */
  8433. This block is called a “conditional group”. The body, CONTROLLED
  8434. TEXT, will be included in compilation if and only if MACRO is defined.
  8435. We say that the conditional “succeeds” if MACRO is defined, “fails” if
  8436. it is not.
  8437. The CONTROLLED TEXT inside a conditional can include preprocessing
  8438. directives. They are executed only if the conditional succeeds. You
  8439. can nest conditional groups inside other conditional groups, but they
  8440. must be completely nested. In other words, ‘#endif’ always matches the
  8441. nearest ‘#ifdef’ (or ‘#ifndef’, or ‘#if’). Also, you cannot start a
  8442. conditional group in one file and end it in another.
  8443. Even if a conditional fails, the CONTROLLED TEXT inside it is still
  8444. run through initial transformations and tokenization. Therefore, it
  8445. must all be lexically valid C. Normally the only way this matters is
  8446. that all comments and string literals inside a failing conditional group
  8447. must still be properly ended.
  8448. The comment following the ‘#endif’ is not required, but it is a good
  8449. practice if there is a lot of CONTROLLED TEXT, because it helps people
  8450. match the ‘#endif’ to the corresponding ‘#ifdef’.
  8451. Older programs sometimes put MACRO directly after the ‘#endif’
  8452. without enclosing it in a comment. This is invalid code according to
  8453. the C standard, but it only causes a warning in GNU C. It never affects
  8454. which ‘#ifndef’ the ‘#endif’ matches.
  8455. Sometimes you wish to use some code if a macro is _not_ defined. You
  8456. can do this by writing ‘#ifndef’ instead of ‘#ifdef’. One common use of
  8457. ‘#ifndef’ is to include code only the first time a header file is
  8458. included. *Note Once-Only Headers::.
  8459. Macro definitions can vary between compilations for several reasons.
  8460. Here are some samples.
  8461. • Some macros are predefined on each kind of machine (*note
  8462. System-specific Predefined Macros: (gcc)System-specific Predefined
  8463. Macros.). This allows you to provide code specially tuned for a
  8464. particular machine.
  8465. • System header files define more macros, associated with the
  8466. features they implement. You can test these macros with
  8467. conditionals to avoid using a system feature on a machine where it
  8468. is not implemented.
  8469. • Macros can be defined or undefined with the ‘-D’ and ‘-U’
  8470. command-line options when you compile the program. You can arrange
  8471. to compile the same source file into two different programs by
  8472. choosing a macro name to specify which program you want, writing
  8473. conditionals to test whether or how this macro is defined, and then
  8474. controlling the state of the macro with command-line options,
  8475. perhaps set in the file ‘Makefile’. *Note Invoking GCC:
  8476. (gcc)Invocation.
  8477. • Your program might have a special header file (often called
  8478. ‘config.h’) that is adjusted when the program is compiled. It can
  8479. define or not define macros depending on the features of the system
  8480. and the desired capabilities of the program. The adjustment can be
  8481. automated by a tool such as ‘autoconf’, or done by hand.
  8482. 26.6.2.2 The ‘#if’ directive
  8483. ............................
  8484. The ‘#if’ directive allows you to test the value of an integer
  8485. arithmetic expression, rather than the mere existence of one macro. Its
  8486. syntax is
  8487. #if EXPRESSION
  8488. CONTROLLED TEXT
  8489. #endif /* EXPRESSION */
  8490. EXPRESSION is a C expression of integer type, subject to stringent
  8491. restrictions so its value can be computed at compile time. It may
  8492. contain
  8493. • Integer constants.
  8494. • Character constants, which are interpreted as they would be in
  8495. normal code.
  8496. • Arithmetic operators for addition, subtraction, multiplication,
  8497. division, bitwise operations, shifts, comparisons, and logical
  8498. operations (‘&&’ and ‘||’). The latter two obey the usual
  8499. short-circuiting rules of standard C.
  8500. • Macros. All macros in the expression are expanded before actual
  8501. computation of the expression’s value begins.
  8502. • Uses of the ‘defined’ operator, which lets you check whether macros
  8503. are defined in the middle of an ‘#if’.
  8504. • Identifiers that are not macros, which are all considered to be the
  8505. number zero. This allows you to write ‘#if MACRO’ instead of
  8506. ‘#ifdef MACRO’, if you know that MACRO, when defined, will always
  8507. have a nonzero value. Function-like macros used without their
  8508. function call parentheses are also treated as zero.
  8509. In some contexts this shortcut is undesirable. The ‘-Wundef’
  8510. requests warnings for any identifier in an ‘#if’ that is not
  8511. defined as a macro.
  8512. Preprocessing does not know anything about the data types of C.
  8513. Therefore, ‘sizeof’ operators are not recognized in ‘#if’; ‘sizeof’ is
  8514. simply an identifier, and if it is not a macro, it stands for zero.
  8515. This is likely to make the expression invalid. Preprocessing does not
  8516. recognize ‘enum’ constants; they too are simply identifiers, so if they
  8517. are not macros, they stand for zero.
  8518. Preprocessing calculates the value of EXPRESSION, and carries out all
  8519. calculations in the widest integer type known to the compiler; on most
  8520. machines supported by GNU C this is 64 bits. This is not the same rule
  8521. as the compiler uses to calculate the value of a constant expression,
  8522. and may give different results in some cases. If the value comes out to
  8523. be nonzero, the ‘#if’ succeeds and the CONTROLLED TEXT is compiled;
  8524. otherwise it is skipped.
  8525. 26.6.2.3 The ‘defined’ test
  8526. ...........................
  8527. The special operator ‘defined’ is used in ‘#if’ and ‘#elif’ expressions
  8528. to test whether a certain name is defined as a macro. ‘defined NAME’
  8529. and ‘defined (NAME)’ are both expressions whose value is 1 if NAME is
  8530. defined as a macro at the current point in the program, and 0 otherwise.
  8531. Thus, ‘#if defined MACRO’ is precisely equivalent to ‘#ifdef MACRO’.
  8532. ‘defined’ is useful when you wish to test more than one macro for
  8533. existence at once. For example,
  8534. #if defined (__arm__) || defined (__PPC__)
  8535. would succeed if either of the names ‘__arm__’ or ‘__PPC__’ is defined
  8536. as a macro—in other words, when compiling for ARM processors or PowerPC
  8537. processors.
  8538. Conditionals written like this:
  8539. #if defined BUFSIZE && BUFSIZE <= 1024
  8540. can generally be simplified to just ‘#if BUFSIZE <= 1024’, since if
  8541. ‘BUFSIZE’ is not defined, it will be interpreted as having the value
  8542. zero.
  8543. In GCC, you can include ‘defined’ as part of another macro
  8544. definition, like this:
  8545. #define MACRO_DEFINED(X) defined X
  8546. #if MACRO_DEFINED(BUFSIZE)
  8547. which would expand the ‘#if’ expression to:
  8548. #if defined BUFSIZE
  8549. Generating ‘defined’ in this way is a GNU C extension.
  8550. 26.6.2.4 The ‘#else’ directive
  8551. ..............................
  8552. The ‘#else’ directive can be added to a conditional to provide
  8553. alternative text to be used if the condition fails. This is what it
  8554. looks like:
  8555. #if EXPRESSION
  8556. TEXT-IF-TRUE
  8557. #else /* Not EXPRESSION */
  8558. TEXT-IF-FALSE
  8559. #endif /* Not EXPRESSION */
  8560. If EXPRESSION is nonzero, the TEXT-IF-TRUE is included and the
  8561. TEXT-IF-FALSE is skipped. If EXPRESSION is zero, the opposite happens.
  8562. You can use ‘#else’ with ‘#ifdef’ and ‘#ifndef’, too.
  8563. 26.6.2.5 The ‘#elif’ directive
  8564. ..............................
  8565. One common case of nested conditionals is used to check for more than
  8566. two possible alternatives. For example, you might have
  8567. #if X == 1
  8568. /* ... */
  8569. #else /* X != 1 */
  8570. #if X == 2
  8571. /* ... */
  8572. #else /* X != 2 */
  8573. /* ... */
  8574. #endif /* X != 2 */
  8575. #endif /* X != 1 */
  8576. Another conditional directive, ‘#elif’, allows this to be abbreviated
  8577. as follows:
  8578. #if X == 1
  8579. /* ... */
  8580. #elif X == 2
  8581. /* ... */
  8582. #else /* X != 2 and X != 1*/
  8583. /* ... */
  8584. #endif /* X != 2 and X != 1*/
  8585. ‘#elif’ stands for “else if”. Like ‘#else’, it goes in the middle of
  8586. a conditional group and subdivides it; it does not require a matching
  8587. ‘#endif’ of its own. Like ‘#if’, the ‘#elif’ directive includes an
  8588. expression to be tested. The text following the ‘#elif’ is processed
  8589. only if the original ‘#if’-condition failed and the ‘#elif’ condition
  8590. succeeds.
  8591. More than one ‘#elif’ can go in the same conditional group. Then the
  8592. text after each ‘#elif’ is processed only if the ‘#elif’ condition
  8593. succeeds after the original ‘#if’ and all previous ‘#elif’ directives
  8594. within it have failed.
  8595. ‘#else’ is allowed after any number of ‘#elif’ directives, but
  8596. ‘#elif’ may not follow ‘#else’.
  8597. 26.6.3 Deleted Code
  8598. -------------------
  8599. If you replace or delete a part of the program but want to keep the old
  8600. code in the file for future reference, commenting it out is not so
  8601. straightforward in C. Block comments do not nest, so the first comment
  8602. inside the old code will end the commenting-out. The probable result is
  8603. a flood of syntax errors.
  8604. One way to avoid this problem is to use an always-false conditional
  8605. instead. For instance, put ‘#if 0’ before the deleted code and ‘#endif’
  8606. after it. This works even if the code being turned off contains
  8607. conditionals, but they must be entire conditionals (balanced ‘#if’ and
  8608. ‘#endif’).
  8609. Some people use ‘#ifdef notdef’ instead. This is risky, because
  8610. ‘notdef’ might be accidentally defined as a macro, and then the
  8611. conditional would succeed. ‘#if 0’ can be counted on to fail.
  8612. Do not use ‘#if 0’ around text that is not C code. Use a real
  8613. comment, instead. The interior of ‘#if 0’ must consist of complete
  8614. tokens; in particular, single-quote characters must balance. Comments
  8615. often contain unbalanced single-quote characters (known in English as
  8616. apostrophes). These confuse ‘#if 0’. They don’t confuse ‘/*’.
  8617. 26.7 Diagnostics
  8618. ================
  8619. The directive ‘#error’ reports a fatal error. The tokens forming the
  8620. rest of the line following ‘#error’ are used as the error message.
  8621. The usual place to use ‘#error’ is inside a conditional that detects
  8622. a combination of parameters that you know the program does not properly
  8623. support. For example,
  8624. #if !defined(UNALIGNED_INT_ASM_OP) && defined(DWARF2_DEBUGGING_INFO)
  8625. #error "DWARF2_DEBUGGING_INFO requires UNALIGNED_INT_ASM_OP."
  8626. #endif
  8627. The directive ‘#warning’ is like ‘#error’, but it reports a warning
  8628. instead of an error. The tokens following ‘#warning’ are used as the
  8629. warning message.
  8630. You might use ‘#warning’ in obsolete header files, with a message
  8631. saying which header file to use instead.
  8632. Neither ‘#error’ nor ‘#warning’ macro-expands its argument. Internal
  8633. whitespace sequences are each replaced with a single space. The line
  8634. must consist of complete tokens. It is wisest to make the argument of
  8635. these directives be a single string constant; this avoids problems with
  8636. apostrophes and the like.
  8637. 26.8 Line Control
  8638. =================
  8639. Due to C’s widespread availability and low-level nature, it is often
  8640. used as the target language for translation of other languages, or for
  8641. the output of lexical analyzers and parsers (e.g., lex/flex and
  8642. yacc/bison). Line control enables the user to track diagnostics back to
  8643. the location in the original language.
  8644. The C compiler knows the location in the source file where each token
  8645. came from: file name, starting line and column, and final line and
  8646. column. (Column numbers are used only for error messages.)
  8647. When a program generates C source code, as the Bison parser generator
  8648. does, often it copies some of that C code from another file. For
  8649. instance parts of the output from Bison are generated from scratch or
  8650. come from a standard parser file, but Bison copies the rest from Bison’s
  8651. input file. Errors in that code, at compile time or run time, should
  8652. refer to that file, which is the real source code. To make that happen,
  8653. Bison generates line-control directives that the C compiler understands.
  8654. ‘#line’ is a directive that specifies the original line number and
  8655. source file name for subsequent code. ‘#line’ has three variants:
  8656. ‘#line LINENUM’
  8657. LINENUM is a non-negative decimal integer constant. It specifies
  8658. the line number that should be reported for the following line of
  8659. input. Subsequent lines are counted from LINENUM.
  8660. ‘#line LINENUM FILENAME’
  8661. LINENUM is the same as for the first form, and has the same effect.
  8662. In addition, FILENAME is a string constant that specifies the
  8663. source file name. Subsequent source lines are recorded as coming
  8664. from that file, until something else happens to change that.
  8665. FILENAME is interpreted according to the normal rules for a string
  8666. constant. Backslash escapes are interpreted, in contrast to
  8667. ‘#include’.
  8668. ‘#line ANYTHING ELSE’
  8669. ANYTHING ELSE is checked for macro calls, which are expanded. The
  8670. result should match one of the above two forms.
  8671. ‘#line’ directives alter the results of the ‘__FILE__’ and ‘__LINE__’
  8672. symbols from that point on. *Note Predefined Macros::.
  8673. 26.9 Null Directive
  8674. ===================
  8675. The “null directive” consists of a ‘#’ followed by a newline, with only
  8676. whitespace and comments in between. It has no effect on the output of
  8677. the compiler.
  8678. 27 Integers in Depth
  8679. ********************
  8680. This chapter explains the machine-level details of integer types: how
  8681. they are represented as bits in memory, and the range of possible values
  8682. for each integer type.
  8683. 27.1 Integer Representations
  8684. ============================
  8685. Modern computers store integer values as binary (base-2) numbers that
  8686. occupy a single unit of storage, typically either as an 8-bit ‘char’, a
  8687. 16-bit ‘short int’, a 32-bit ‘int’, or possibly, a 64-bit ‘long long
  8688. int’. Whether a ‘long int’ is a 32-bit or a 64-bit value is system
  8689. dependent.(1)
  8690. The macro ‘CHAR_BIT’, defined in ‘limits.h’, gives the number of bits
  8691. in type ‘char’. On any real operating system, the value is 8.
  8692. The fixed sizes of numeric types necessarily limits their “range of
  8693. values”, and the particular encoding of integers decides what that range
  8694. is.
  8695. For unsigned integers, the entire space is used to represent a
  8696. nonnegative value. Signed integers are stored using “two’s-complement
  8697. representation”: a signed integer with N bits has a range from -2^{(N -
  8698. 1)} to −1 to 0 to 1 to +2^{(N - 1)} - 1, inclusive. The leftmost, or
  8699. high-order, bit is called the “sign bit”.
  8700. There is only one value that means zero, and the most negative number
  8701. lacks a positive counterpart. As a result, negating that number causes
  8702. overflow; in practice, its result is that number back again. For
  8703. example, a two’s-complement signed 8-bit integer can represent all
  8704. decimal numbers from −128 to +127. We will revisit that peculiarity
  8705. shortly.
  8706. Decades ago, there were computers that didn’t use two’s-complement
  8707. representation for integers (*note Integers in Depth::), but they are
  8708. long gone and not worth any effort to support.
  8709. When an arithmetic operation produces a value that is too big to
  8710. represent, the operation is said to “overflow”. In C, integer overflow
  8711. does not interrupt the control flow or signal an error. What it does
  8712. depends on signedness.
  8713. For unsigned arithmetic, the result of an operation that overflows is
  8714. the N low-order bits of the correct value. If the correct value is
  8715. representable in N bits, that is always the result; thus we often say
  8716. that “integer arithmetic is exact,” omitting the crucial qualifying
  8717. phrase “as long as the exact result is representable.”
  8718. In principle, a C program should be written so that overflow never
  8719. occurs for signed integers, but in GNU C you can specify various ways of
  8720. handling such overflow (*note Integer Overflow::).
  8721. Integer representations are best understood by looking at a table for
  8722. a tiny integer size; here are the possible values for an integer with
  8723. three bits:
  8724. Unsigned Signed Bits 2s Complement
  8725. ----------------------------------------------------------------------------
  8726. 0 0 000 000 (0)
  8727. 1 1 001 111 (-1)
  8728. 2 2 010 110 (-2)
  8729. 3 3 011 101 (-3)
  8730. 4 -4 100 100 (-4)
  8731. 5 -3 101 011 (3)
  8732. 6 -2 110 010 (2)
  8733. 7 -1 111 001 (1)
  8734. The parenthesized decimal numbers in the last column represent the
  8735. signed meanings of the two’s-complement of the line’s value. Recall
  8736. that, in two’s-complement encoding, the high-order bit is 0 when the
  8737. number is nonnegative.
  8738. We can now understand the peculiar behavior of negation of the most
  8739. negative two’s-complement integer: start with 0b100, invert the bits to
  8740. get 0b011, and add 1: we get 0b100, the value we started with.
  8741. We can also see overflow behavior in two’s-complement:
  8742. 3 + 1 = 0b011 + 0b001 = 0b100 = (-4)
  8743. 3 + 2 = 0b011 + 0b010 = 0b101 = (-3)
  8744. 3 + 3 = 0b011 + 0b011 = 0b110 = (-2)
  8745. A sum of two nonnegative signed values that overflows has a 1 in the
  8746. sign bit, so the exact positive result is truncated to a negative value.
  8747. ---------- Footnotes ----------
  8748. (1) In theory, any of these types could have some other size, bit
  8749. it’s not worth even a minute to cater to that possibility. It never
  8750. happens on GNU/Linux.
  8751. 27.2 Maximum and Minimum Values
  8752. ===============================
  8753. For each primitive integer type, there is a standard macro defined in
  8754. ‘limits.h’ that gives the largest value that type can hold. For
  8755. instance, for type ‘int’, the maximum value is ‘INT_MAX’. On a 32-bit
  8756. computer, that is equal to 2,147,483,647. The maximum value for
  8757. ‘unsigned int’ is ‘UINT_MAX’, which on a 32-bit computer is equal to
  8758. 4,294,967,295. Likewise, there are ‘SHRT_MAX’, ‘LONG_MAX’, and
  8759. ‘LLONG_MAX’, and corresponding unsigned limits ‘USHRT_MAX’, ‘ULONG_MAX’,
  8760. and ‘ULLONG_MAX’.
  8761. Since there are three ways to specify a ‘char’ type, there are also
  8762. three limits: ‘CHAR_MAX’, ‘SCHAR_MAX’, and ‘UCHAR_MAX’.
  8763. For each type that is or might be signed, there is another symbol
  8764. that gives the minimum value it can hold. (Just replace ‘MAX’ with
  8765. ‘MIN’ in the names listed above.) There is no minimum limit symbol for
  8766. types specified with ‘unsigned’ because the minimum for them is
  8767. universally zero.
  8768. ‘INT_MIN’ is not the negative of ‘INT_MAX’. In two’s-complement
  8769. representation, the most negative number is 1 less than the negative of
  8770. the most positive number. Thus, ‘INT_MIN’ on a 32-bit computer has the
  8771. value −2,147,483,648. You can’t actually write the value that way in C,
  8772. since it would overflow. That’s a good reason to use ‘INT_MIN’ to
  8773. specify that value. Its definition is written to avoid overflow.
  8774. This is part of the GNU C Intro and Reference Manual and covered by
  8775. its license.
  8776. 28 Floating Point in Depth
  8777. **************************
  8778. 28.1 Floating-Point Representations
  8779. ===================================
  8780. Storing numbers as “floating point” allows representation of numbers
  8781. with fractional values, in a range larger than that of hardware
  8782. integers. A floating-point number consists of a sign bit, a
  8783. “significand” (also called the “mantissa”), and a power of a fixed base.
  8784. GNU C uses the floating-point representations specified by the ‘IEEE
  8785. 754-2008 Standard for Floating-Point Arithmetic’.
  8786. The IEEE 754-2008 specification defines basic binary floating-point
  8787. formats of five different sizes: 16-bit, 32-bit, 64-bit, 128-bit, and
  8788. 256-bit. The formats of 32, 64, and 128 bits are used for the standard
  8789. C types ‘float’, ‘double’, and ‘long double’. GNU C supports the 16-bit
  8790. floating point type ‘_Float16’ on some platforms, but does not support
  8791. the 256-bit floating point type.
  8792. Each of the formats encodes the floating-point number as a sign bit.
  8793. After this comes an exponent that specifies a power of 2 (with a fixed
  8794. offset). Then comes the significand.
  8795. The first bit of the significand, before the binary point, is always
  8796. 1, so there is no need to store it in memory. It is called the “hidden
  8797. bit” because it doesn’t appear in the floating-point number as used in
  8798. the computer itself.
  8799. All of those floating-point formats are sign-magnitude
  8800. representations, so +0 and −0 are different values.
  8801. Besides the IEEE 754 format 128-bit float, GNU C also offers a format
  8802. consisting of a pair of 64-bit floating point numbers. This lacks the
  8803. full exponent range of the IEEE 128-bit format, but is useful when the
  8804. underlying hardware platform does not support that.
  8805. 28.2 Floating-Point Type Specifications
  8806. =======================================
  8807. The standard library header file ‘float.h’ defines a number of constants
  8808. that describe the platform’s implementation of floating-point types
  8809. ‘float’, ‘double’ and ‘long double’. They include:
  8810. ‘FLT_MIN’
  8811. ‘DBL_MIN’
  8812. ‘LDBL_MIN’
  8813. Defines the minimum normalized positive floating-point values that
  8814. can be represented with the type.
  8815. ‘FLT_HAS_SUBNORM’
  8816. ‘DBL_HAS_SUBNORM’
  8817. ‘LDBL_HAS_SUBNORM’
  8818. Defines if the floating-point type supports subnormal (or
  8819. “denormalized”) numbers or not (*note subnormal numbers::).
  8820. ‘FLT_TRUE_MIN’
  8821. ‘DBL_TRUE_MIN’
  8822. ‘LDBL_TRUE_MIN’
  8823. Defines the minimum positive values (including subnormal values)
  8824. that can be represented with the type.
  8825. ‘FLT_MAX’
  8826. ‘DBL_MAX’
  8827. ‘LDBL_MAX’
  8828. Defines the largest values that can be represented with the type.
  8829. ‘FLT_DECIMAL_DIG’
  8830. ‘DBL_DECIMAL_DIG’
  8831. ‘LDBL_DECIMAL_DIG’
  8832. Defines the number of decimal digits ‘n’ such that any
  8833. floating-point number that can be represented in the type can be
  8834. rounded to a floating-point number with ‘n’ decimal digits, and
  8835. back again, without losing any precision of the value.
  8836. 28.3 Special Floating-Point Values
  8837. ==================================
  8838. IEEE floating point provides for special values that are not ordinary
  8839. numbers.
  8840. infinities
  8841. ‘+Infinity’ and ‘-Infinity’ are two different infinite values, one
  8842. positive and one negative. These result from operations such as ‘1
  8843. / 0’, ‘Infinity + Infinity’, ‘Infinity * Infinity’, and ‘Infinity +
  8844. FINITE’, and also from a result that is finite, but larger than the
  8845. most positive possible value or smaller than the most negative
  8846. possible value.
  8847. *Note Handling Infinity::, for more about working with infinities.
  8848. NaNs (not a number)
  8849. There are two special values, called Not-a-Number (NaN): a quiet
  8850. NaN (QNaN), and a signaling NaN (SNaN).
  8851. A QNaN is produced by operations for which the value is undefined
  8852. in real arithmetic, such as ‘0 / 0’, ‘sqrt (-1)’, ‘Infinity -
  8853. Infinity’, and any basic operation in which an operand is a QNaN.
  8854. The signaling NaN is intended for initializing otherwise-unassigned
  8855. storage, and the goal is that unlike a QNaN, an SNaN _does_ cause
  8856. an interrupt that can be caught by a software handler, diagnosed,
  8857. and reported. In practice, little use has been made of signaling
  8858. NaNs, because the most common CPUs in desktop and portable
  8859. computers fail to implement the full IEEE 754 Standard, and supply
  8860. only one kind of NaN, the quiet one. Also, programming-language
  8861. standards have taken decades to catch up to the IEEE 754 standard,
  8862. and implementations of those language standards make an additional
  8863. delay before programmers become willing to use these features.
  8864. To enable support for signaling NaNs, use the GCC command-line
  8865. option ‘-fsignaling-nans’, but this is an experimental feature and
  8866. may not work as expected in every situation.
  8867. A NaN has a sign bit, but its value means nothing.
  8868. *Note Handling NaN::, for more about working with NaNs.
  8869. subnormal numbers
  8870. It can happen that a computed floating-point value is too small to
  8871. represent, such as when two tiny numbers are multiplied. The
  8872. result is then said to “underflow”. The traditional behavior
  8873. before the IEEE 754 Standard was to use zero as the result, and
  8874. possibly to report the underflow in some sort of program output.
  8875. The IEEE 754 Standard is vague about whether rounding happens
  8876. before detection of floating underflow and overflow, or after, and
  8877. CPU designers may choose either.
  8878. However, the Standard does something unusual compared to earlier
  8879. designs, and that is that when the result is smaller than the
  8880. smallest “normalized” representable value (i.e., one in which the
  8881. leading significand bit is ‘1’), the normalization requirement is
  8882. relaxed, leading zero bits are permitted, and precision is
  8883. gradually lost until there are no more bits in the significand.
  8884. That phenomenon is called “gradual underflow”, and it serves
  8885. important numerical purposes, although it does reduce the precision
  8886. of the final result. Some floating-point designs allow you to
  8887. choose at compile time, or even at run time, whether underflows are
  8888. gradual, or are flushed abruptly to zero. Numbers that have
  8889. entered the region of gradual underflow are called “subnormal”.
  8890. You can use the library functions ‘fesetround’ and ‘fegetround’ to
  8891. set and get the rounding mode. Rounding modes are defined (if
  8892. supported by the platform) in ‘fenv.h’ as: ‘FE_UPWARD’ to round
  8893. toward positive infinity; ‘FE_DOWNWARD’ to round toward negative
  8894. infinity; ‘FE_TOWARDZERO’ to round toward zero; and ‘FE_TONEAREST’
  8895. to round to the nearest representable value, the default mode. It
  8896. is best to use ‘FE_TONEAREST’ except when there is a special need
  8897. for some other mode.
  8898. 28.4 Invalid Optimizations
  8899. ==========================
  8900. Signed zeros, Infinity, and NaN invalidate some optimizations by
  8901. programmers and compilers that might otherwise have seemed obvious:
  8902. • ‘x + 0’ and ‘x - 0’ are not the same as ‘x’ when ‘x’ is zero,
  8903. because the result depends on the rounding rule. *Note Rounding::,
  8904. for more about rounding rules.
  8905. • ‘x * 0.0’ is not the same as ‘0.0’ when ‘x’ is Infinity, a NaN, or
  8906. negative zero.
  8907. • ‘x / x’ is not the same as ‘1.0’ when ‘x’ is Infinity, a NaN, or
  8908. zero.
  8909. • ‘(x - y)’ is not the same as ‘-(y - x)’ because when the operands
  8910. are finite and equal, one evaluates to ‘+0’ and the other to ‘-0’.
  8911. • ‘x - x’ is not the same as ‘0.0’ when X is Infinity or a NaN.
  8912. • ‘x == x’ and ‘x != x’ are not equivalent to ‘1’ and ‘0’ when X is a
  8913. NaN.
  8914. • ‘x < y’ and ‘isless (x, y)’ are not equivalent, because the first
  8915. sets a sticky exception flag (*note Exception Flags::) when an
  8916. operand is a NaN, whereas the second does not affect that flag.
  8917. The same holds for the other ‘isxxx’ functions that are companions
  8918. to relational operators. *Note (libc)FP Comparison Functions::.
  8919. The ‘-funsafe-math-optimizations’ option enables these optimizations.
  8920. 28.5 Floating Arithmetic Exception Flags
  8921. ========================================
  8922. “Sticky exception flags” record the occurrence of particular conditions:
  8923. once set, they remain set until the program explicitly clears them.
  8924. The conditions include _invalid operand_, _division-by_zero_,
  8925. _inexact result_ (i.e., one that required rounding), _underflow_, and
  8926. _overflow_. Some extended floating-point designs offer several
  8927. additional exception flags. The functions ‘feclearexcept’,
  8928. ‘feraiseexcept’, ‘fetestexcept’, ‘fegetexceptflags’, and
  8929. ‘fesetexceptflags’ provide a standardized interface to those flags.
  8930. *Note (libc)Status bit operations::.
  8931. One important use of those flags is to do a computation that is
  8932. normally expected to be exact in floating-point arithmetic, but
  8933. occasionally might not be, in which case, corrective action is needed.
  8934. You can clear the _inexact result_ flag with a call to ‘feclearexcept
  8935. (FE_INEXACT)’, do the computation, and then test the flag with
  8936. ‘fetestexcept (FE_INEXACT)’; the result of that call is 0 if the flag is
  8937. not set (there was no rounding), and 1 when there was rounding (which,
  8938. we presume, implies the program has to correct for that).
  8939. 28.6 Exact Floating-Point Arithmetic
  8940. ====================================
  8941. As long as the numbers are exactly representable (fractions whose
  8942. denominator is a power of 2), and intermediate results do not require
  8943. rounding, then floating-point arithmetic is _exact_. It is easy to
  8944. predict how many digits are needed for the results of arithmetic
  8945. operations:
  8946. • addition and subtraction of two N-digit values with the _same_
  8947. exponent require at most ‘N + 1’ digits, but when the exponents
  8948. differ, many more digits may be needed;
  8949. • multiplication of two N-digit values requires exactly 2 N digits;
  8950. • although integer division produces a quotient and a remainder of no
  8951. more than N-digits, floating-point remainder and square root may
  8952. require an unbounded number of digits, and the quotient can need
  8953. many more digits than can be stored.
  8954. Whenever a result requires more than N digits, rounding is needed.
  8955. 28.7 Rounding
  8956. =============
  8957. When floating-point arithmetic produces a result that can’t fit exactly
  8958. in the significand of the type that’s in use, it has to “round” the
  8959. value. The basic arithmetic operations—addition, subtraction,
  8960. multiplication, division, and square root—always produce a result that
  8961. is equivalent to the exact, possibly infinite-precision result rounded
  8962. to storage precision according to the current rounding rule.
  8963. Rounding sets the ‘FE_INEXACT’ exception flag (*note Exception
  8964. Flags::). This enables programs to determine that rounding has
  8965. occurred.
  8966. Rounding consists of adjusting the exponent to bring the significand
  8967. back to the required base-point alignment, then applying the current
  8968. “rounding rule” to squeeze the significand into the fixed available
  8969. size.
  8970. The current rule is selected at run time from four options. Here
  8971. they are:
  8972. * _round-to-nearest_, with ties rounded to an even integer;
  8973. * _round-up_, towards ‘+Infinity’;
  8974. * _round-down_, towards ‘-Infinity’;
  8975. * _round-towards-zero_.
  8976. Under those four rounding rules, a decimal value ‘-1.2345’ that is to
  8977. be rounded to a four-digit result would become ‘-1.234’, ‘-1.234’,
  8978. ‘-1.235’, and ‘-1.234’, respectively.
  8979. The default rounding rule is _round-to-nearest_, because that has the
  8980. least bias, and produces the lowest average error. When the true result
  8981. lies exactly halfway between two representable machine numbers, the
  8982. result is rounded to the one that ends with an even digit.
  8983. The _round-towards-zero_ rule was common on many early computer
  8984. designs, because it is the easiest to implement: it just requires silent
  8985. truncation of all extra bits.
  8986. The two other rules, _round-up_ and _round-down_, are essential for
  8987. implementing “interval arithmetic”, whereby each arithmetic operation
  8988. produces lower and upper bounds that are guaranteed to enclose the exact
  8989. result.
  8990. *Note Rounding Control::, for details on getting and setting the
  8991. current rounding mode.
  8992. 28.8 Rounding Issues
  8993. ====================
  8994. The default IEEE 754 rounding mode minimizes errors, and most normal
  8995. computations should not suffer any serious accumulation of errors from
  8996. rounding.
  8997. Of course, you can contrive examples where that is not so. Here is
  8998. one: iterate a square root, then attempt to recover the original value
  8999. by repeated squaring.
  9000. #include <stdio.h>
  9001. #include <math.h>
  9002. int main (void)
  9003. {
  9004. double x = 100.0;
  9005. double y;
  9006. for (n = 10; n >=100; n += 10)
  9007. {
  9008. y = x;
  9009. for (k = 0; k < n; ++k) y = sqrt (y);
  9010. for (k = 0; k < n; ++k) y *= y;
  9011. printf ("n = %3d; x = %.0f\ty = %.6f\n", n, x, y);
  9012. }
  9013. return 0;
  9014. }
  9015. Here is the output:
  9016. n = 10; x = 100 y = 100.000000
  9017. n = 20; x = 100 y = 100.000000
  9018. n = 30; x = 100 y = 99.999977
  9019. n = 40; x = 100 y = 99.981025
  9020. n = 50; x = 100 y = 90.017127
  9021. n = 60; x = 100 y = 1.000000
  9022. n = 70; x = 100 y = 1.000000
  9023. n = 80; x = 100 y = 1.000000
  9024. n = 90; x = 100 y = 1.000000
  9025. n = 100; x = 100 y = 1.000000
  9026. After 50 iterations, ‘y’ has barely one correct digit, and soon
  9027. after, there are no correct digits.
  9028. 28.9 Significance Loss
  9029. ======================
  9030. A much more serious source of error in floating-point computation is
  9031. “significance loss” from subtraction of nearly equal values. This means
  9032. that the number of bits in the significand of the result is fewer than
  9033. the size of the value would permit. If the values being subtracted are
  9034. close enough, but still not equal, a _single subtraction_ can wipe out
  9035. all correct digits, possibly contaminating all future computations.
  9036. Floating-point calculations can sometimes be carefully designed so
  9037. that significance loss is not possible, such as summing a series where
  9038. all terms have the same sign. For example, the Taylor series expansions
  9039. of the trigonometric and hyperbolic sines have terms of identical
  9040. magnitude, of the general form ‘X**(2*N + 1) / (2*N + 1)!’. However,
  9041. those in the trigonometric sine series alternate in sign, while those in
  9042. the hyperbolic sine series are all positive. Here is the output of two
  9043. small programs that sum K terms of the series for ‘sin (X)’, and compare
  9044. the computed sums with known-to-be-accurate library functions:
  9045. x = 10 k = 51
  9046. s (x) = -0.544_021_110_889_270
  9047. sin (x) = -0.544_021_110_889_370
  9048. x = 20 k = 81
  9049. s (x) = 0.912_945_250_749_573
  9050. sin (x) = 0.912_945_250_727_628
  9051. x = 30 k = 109
  9052. s (x) = -0.987_813_746_058_855
  9053. sin (x) = -0.988_031_624_092_862
  9054. x = 40 k = 137
  9055. s (x) = 0.617_400_430_980_474
  9056. sin (x) = 0.745_113_160_479_349
  9057. x = 50 k = 159
  9058. s (x) = 57_105.187_673_745_720_532
  9059. sin (x) = -0.262_374_853_703_929
  9060. // sinh(x) series summation with positive signs
  9061. // with k terms needed to converge to machine precision
  9062. x = 10 k = 47
  9063. t (x) = 1.101_323_287_470_340e+04
  9064. sinh (x) = 1.101_323_287_470_339e+04
  9065. x = 20 k = 69
  9066. t (x) = 2.425_825_977_048_951e+08
  9067. sinh (x) = 2.425_825_977_048_951e+08
  9068. x = 30 k = 87
  9069. t (x) = 5.343_237_290_762_229e+12
  9070. sinh (x) = 5.343_237_290_762_231e+12
  9071. x = 40 k = 105
  9072. t (x) = 1.176_926_334_185_100e+17
  9073. sinh (x) = 1.176_926_334_185_100e+17
  9074. x = 50 k = 121
  9075. t (x) = 2.592_352_764_293_534e+21
  9076. sinh (x) = 2.592_352_764_293_536e+21
  9077. We have added underscores to the numbers to enhance readability.
  9078. The ‘sinh (X)’ series with positive terms can be summed to high
  9079. accuracy. By contrast, the series for ‘sin (X)’ suffers increasing
  9080. significance loss, so that when X = 30 only two correct digits remain.
  9081. Soon after, all digits are wrong, and the answers are complete nonsense.
  9082. An important skill in numerical programming is to recognize when
  9083. significance loss is likely to contaminate a computation, and revise the
  9084. algorithm to reduce this problem. Sometimes, the only practical way to
  9085. do so is to compute in higher intermediate precision, which is why the
  9086. extended types like ‘long double’ are important.
  9087. 28.10 Fused Multiply-Add
  9088. ========================
  9089. In 1990, when IBM introduced the POWER architecture, the CPU provided a
  9090. previously unknown instruction, the “fused multiply-add” (FMA). It
  9091. computes the value ‘x * y + z’ with an *exact* double-length product,
  9092. followed by an addition with a _single_ rounding. Numerical computation
  9093. often needs pairs of multiply and add operations, for which the FMA is
  9094. well-suited.
  9095. On the POWER architecture, there are two dedicated registers that
  9096. hold permanent values of ‘0.0’ and ‘1.0’, and the normal _multiply_ and
  9097. _add_ instructions are just wrappers around the FMA that compute ‘x * y
  9098. + 0.0’ and ‘x * 1.0 + z’, respectively.
  9099. In the early days, it appeared that the main benefit of the FMA was
  9100. getting two floating-point operations for the price of one, almost
  9101. doubling the performance of some algorithms. However, numerical
  9102. analysts have since shown numerous uses of the FMA for significantly
  9103. enhancing accuracy. We discuss one of the most important ones in the
  9104. next section.
  9105. A few other architectures have since included the FMA, and most
  9106. provide variants for the related operations ‘x * y - z’ (FMS), ‘-x * y +
  9107. z’ (FNMA), and ‘-x * y - z’ (FNMS).
  9108. The functions ‘fmaf’, ‘fma’, and ‘fmal’ implement fused multiply-add
  9109. for the ‘float’, ‘double’, and ‘long double’ data types. Correct
  9110. implementation of the FMA in software is difficult, and some systems
  9111. that appear to provide those functions do not satisfy the
  9112. single-rounding requirement. That situation should change as more
  9113. programmers use the FMA operation, and more CPUs provide FMA in
  9114. hardware.
  9115. Use the ‘-ffp-contract=fast’ option to allow generation of FMA
  9116. instructions, or ‘-ffp-contract=off’ to disallow it.
  9117. 28.11 Error Recovery
  9118. ====================
  9119. When two numbers are combined by one of the four basic operations, the
  9120. result often requires rounding to storage precision. For accurate
  9121. computation, one would like to be able to recover that rounding error.
  9122. With historical floating-point designs, it was difficult to do so
  9123. portably, but now that IEEE 754 arithmetic is almost universal, the job
  9124. is much easier.
  9125. For addition with the default _round-to-nearest_ rounding mode, we
  9126. can determine the error in a sum like this:
  9127. volatile double err, sum, tmp, x, y;
  9128. if (fabs (x) <= fabs (y))
  9129. {
  9130. sum = x + y;
  9131. tmp = sum - x;
  9132. err = y - tmp;
  9133. }
  9134. else /* fabs (x) < fabs (y) */
  9135. {
  9136. sum = x + y;
  9137. tmp = sum - y;
  9138. err = x - tmp;
  9139. }
  9140. Now, ‘x + y’ is _exactly_ represented by ‘sum + err’. This basic
  9141. operation, which has come to be called “twosum” in the
  9142. numerical-analysis literature, is the first key to tracking, and
  9143. accounting for, rounding error.
  9144. To determine the error in subtraction, just swap the ‘+’ and ‘-’
  9145. operators.
  9146. We used the ‘volatile’ qualifier (*note volatile::) in the
  9147. declaration of the variables, which forces the compiler to store and
  9148. retrieve them from memory, and prevents the compiler from optimizing
  9149. ‘err = y - ((x + y) - x)’ into ‘err = 0’.
  9150. For multiplication, we can compute the rounding error without
  9151. magnitude tests with the FMA operation (*note Fused Multiply-Add::),
  9152. like this:
  9153. volatile double err, prod, x, y;
  9154. prod = x * y; /* rounded product */
  9155. err = fma (x, y, -prod); /* exact product ‘= PROD + ERR’ */
  9156. For addition, subtraction, and multiplication, we can represent the
  9157. exact result with the notional sum of two values. However, the exact
  9158. result of division, remainder, or square root potentially requires an
  9159. infinite number of digits, so we can at best approximate it.
  9160. Nevertheless, we can compute an error term that is close to the true
  9161. error: it is just that error value, rounded to machine precision.
  9162. For division, you can approximate ‘x / y’ with ‘quo + err’ like this:
  9163. volatile double err, quo, x, y;
  9164. quo = x / y;
  9165. err = fma (-quo, y, x) / y;
  9166. For square root, we can approximate ‘sqrt (x)’ with ‘root + err’ like
  9167. this:
  9168. volatile double err, root, x;
  9169. root = sqrt (x);
  9170. err = fma (-root, root, x) / (root + root);
  9171. With the reliable and predictable floating-point design provided by
  9172. IEEE 754 arithmetic, we now have the tools we need to track errors in
  9173. the five basic floating-point operations, and we can effectively
  9174. simulate computing in twice working precision, which is sometimes
  9175. sufficient to remove almost all traces of arithmetic errors.
  9176. 28.12 Exact Floating-Point Constants
  9177. ====================================
  9178. One of the frustrations that numerical programmers have suffered with
  9179. since the dawn of digital computers is the inability to precisely
  9180. specify numbers in their programs. On the early decimal machines, that
  9181. was not an issue: you could write a constant ‘1e-30’ and be confident of
  9182. that exact value being used in floating-point operations. However, when
  9183. the hardware works in a base other than 10, then human-specified numbers
  9184. have to be converted to that base, and then converted back again at
  9185. output time. The two base conversions are rarely exact, and unwanted
  9186. rounding errors are introduced.
  9187. As computers usually represent numbers in a base other than 10,
  9188. numbers often must be converted to and from different bases, and
  9189. rounding errors can occur during conversion. This problem is solved in
  9190. C using hexademical floating-point constants. For example,
  9191. ‘+0x1.fffffcp-1’ is the number that is the IEEE 754 32-bit value closest
  9192. to, but below, ‘1.0’. The significand is represented as a hexadecimal
  9193. fraction, and the _power of two_ is written in decimal following the
  9194. exponent letter ‘p’ (the traditional exponent letter ‘e’ is not
  9195. possible, because it is a hexadecimal digit).
  9196. In ‘printf’ and ‘scanf’ and related functions, you can use the ‘%a’
  9197. and ‘%A’ format specifiers for writing and reading hexadecimal
  9198. floating-point values. ‘%a’ writes them with lower case letters and
  9199. ‘%A’ writes them with upper case letters. For instance, this code
  9200. reproduces our sample number:
  9201. printf ("%a\n", 1.0 - pow (2.0, -23));
  9202. ⊣ 0x1.fffffcp-1
  9203. The ‘strtod’ family was similarly extended to recognize numbers in that
  9204. new format.
  9205. If you want to ensure exact data representation for transfer of
  9206. floating-point numbers between C programs on different computers, then
  9207. hexadecimal constants are an optimum choice.
  9208. 28.13 Handling Infinity
  9209. =======================
  9210. As we noted earlier, the IEEE 754 model of computing is not to stop the
  9211. program when exceptional conditions occur. It takes note of exceptional
  9212. values or conditions by setting sticky “exception flags”, or by
  9213. producing results with the special values Infinity and QNaN. In this
  9214. section, we discuss Infinity; *note Handling NaN:: for the other.
  9215. In GNU C, you can create a value of negative Infinity in software
  9216. like this:
  9217. double x;
  9218. x = -1.0 / 0.0;
  9219. GNU C supplies the ‘__builtin_inf’, ‘__builtin_inff’, and
  9220. ‘__builtin_infl’ macros, and the GNU C Library provides the ‘INFINITY’
  9221. macro, all of which are compile-time constants for positive infinity.
  9222. GNU C also provides a standard function to test for an Infinity:
  9223. ‘isinf (x)’ returns ‘1’ if the argument is a signed infinity, and ‘0’ if
  9224. not.
  9225. Infinities can be compared, and all Infinities of the same sign are
  9226. equal: there is no notion in IEEE 754 arithmetic of different kinds of
  9227. Infinities, as there are in some areas of mathematics. Positive
  9228. Infinity is larger than any finite value, and negative Infinity is
  9229. smaller than finite value.
  9230. Infinities propagate in addition, subtraction, multiplication, and
  9231. square root, but in division, they disappear, because of the rule that
  9232. ‘finite / Infinity’ is ‘0.0’. Thus, an overflow in an intermediate
  9233. computation that produces an Infinity is likely to be noticed later in
  9234. the final results. The programmer can then decide whether the overflow
  9235. is expected, and acceptable, or whether the code possibly has a bug, or
  9236. needs to be run in higher precision, or redesigned to avoid the
  9237. production of the Infinity.
  9238. 28.14 Handling NaN
  9239. ==================
  9240. NaNs are not numbers: they represent values from computations that
  9241. produce undefined results. They have a distinctive property that makes
  9242. them unlike any other floating-point value: they are _unequal to
  9243. everything, including themselves_! Thus, you can write a test for a NaN
  9244. like this:
  9245. if (x != x)
  9246. printf ("x is a NaN\n");
  9247. This test works in GNU C, but some compilers might evaluate that test
  9248. expression as false without properly checking for the NaN value. A more
  9249. portable way to test for NaN is to use the ‘isnan’ function declared in
  9250. ‘math.h’:
  9251. if (isnan (x))
  9252. printf ("x is a NaN\n");
  9253. *Note (libc)Floating Point Classes::.
  9254. One important use of NaNs is marking of missing data. For example,
  9255. in statistics, such data must be omitted from computations. Use of any
  9256. particular finite value for missing data would eventually collide with
  9257. real data, whereas such data could never be a NaN, so it is an ideal
  9258. marker. Functions that deal with collections of data that may have
  9259. holes can be written to test for, and ignore, NaN values.
  9260. It is easy to generate a NaN in computations: evaluating ‘0.0 / 0.0’
  9261. is the commonest way, but ‘Infinity - Infinity’, ‘Infinity / Infinity’,
  9262. and ‘sqrt (-1.0)’ also work. Functions that receive out-of-bounds
  9263. arguments can choose to return a stored NaN value, such as with the
  9264. ‘NAN’ macro defined in ‘math.h’, but that does not set the _invalid
  9265. operand_ exception flag, and that can fool some programs.
  9266. Like Infinity, NaNs propagate in computations, but they are even
  9267. stickier, because they never disappear in division. Thus, once a NaN
  9268. appears in a chain of numerical operations, it is almost certain to pop
  9269. out into the final results. The programmer has to decide whether that
  9270. is expected, or whether there is a coding or algorithmic error that
  9271. needs repair.
  9272. In general, when function gets a NaN argument, it usually returns a
  9273. NaN. However, there are some exceptions in the math-library functions
  9274. that you need to be aware of, because they violate the
  9275. _NaNs-always-propagate_ rule:
  9276. • ‘pow (x, 0.0)’ always returns ‘1.0’, even if ‘x’ is 0.0, Infinity,
  9277. or a NaN.
  9278. • ‘pow (1, y)’ always returns ‘1’, even if ‘y’ is a NaN.
  9279. • ‘hypot (INFINITY, y)’ and ‘hypot (-INFINITY, y)’ both always return
  9280. ‘INFINITY’, even if ‘y’ is a Nan.
  9281. • If just one of the arguments to ‘fmax (x, y)’ or ‘fmin (x, y)’ is a
  9282. NaN, it returns the other argument. If both arguments are NaNs, it
  9283. returns a NaN, but there is no requirement about where it comes
  9284. from: it could be ‘x’, or ‘y’, or some other quiet NaN.
  9285. NaNs are also used for the return values of math-library functions
  9286. where the result is not representable in real arithmetic, or is
  9287. mathematically undefined or uncertain, such as ‘sqrt (-1.0)’ and ‘sin
  9288. (Infinity)’. However, note that a result that is merely too big to
  9289. represent should always produce an Infinity, such as with ‘exp (1000.0)’
  9290. (too big) and ‘exp (Infinity)’ (truly infinite).
  9291. 28.15 Signed Zeros
  9292. ==================
  9293. The sign of zero is significant, and important, because it records the
  9294. creation of a value that is too small to represent, but came from either
  9295. the negative axis, or from the positive axis. Such fine distinctions
  9296. are essential for proper handling of “branch cuts” in complex arithmetic
  9297. (*note Complex Arithmetic::).
  9298. The key point about signed zeros is that in comparisons, their sign
  9299. does not matter: ‘0.0 == -0.0’ must _always_ evaluate to ‘1’ (true).
  9300. However, they are not _the same number_, and ‘-0.0’ in C code stands for
  9301. a negative zero.
  9302. 28.16 Scaling by Powers of the Base
  9303. ===================================
  9304. We have discussed rounding errors several times in this chapter, but it
  9305. is important to remember that when results require no more bits than the
  9306. exponent and significand bits can represent, those results are _exact_.
  9307. One particularly useful exact operation is scaling by a power of the
  9308. base. While one, in principle, could do that with code like this:
  9309. y = x * pow (2.0, (double)k); /* Undesirable scaling: avoid! */
  9310. that is not advisable, because it relies on the quality of the
  9311. math-library power function, and that happens to be one of the most
  9312. difficult functions in the C math library to make accurate. What is
  9313. likely to happen on many systems is that the returned value from ‘pow’
  9314. will be close to a power of two, but slightly different, so the
  9315. subsequent multiplication introduces rounding error.
  9316. The correct, and fastest, way to do the scaling is either via the
  9317. traditional C library function, or with its C99 equivalent:
  9318. y = ldexp (x, k); /* Traditional pre-C99 style. */
  9319. y = scalbn (x, k); /* C99 style. */
  9320. Both functions return ‘x * 2**k’. *Note (libc)Normalization
  9321. Functions::.
  9322. 28.17 Rounding Control
  9323. ======================
  9324. Here we describe how to specify the rounding mode at run time. System
  9325. header file ‘fenv.h’ provides the prototypes for these functions. *Note
  9326. (libc)Rounding::.
  9327. That header file also provides constant names for the four rounding
  9328. modes: ‘FE_DOWNWARD’, ‘FE_TONEAREST’, ‘FE_TOWARDZERO’, and ‘FE_UPWARD’.
  9329. The function ‘fegetround’ examines and returns the current rounding
  9330. mode. On a platform with IEEE 754 floating point, the value will always
  9331. equal one of those four constants. On other platforms, it may return a
  9332. negative value. The function ‘fesetround’ sets the current rounding
  9333. mode.
  9334. Changing the rounding mode can be slow, so it is useful to minimize
  9335. the number of changes. For interval arithmetic, we seem to need three
  9336. changes for each operation, but we really only need two, because we can
  9337. write code like this example for interval addition of two reals:
  9338. {
  9339. struct interval_double
  9340. {
  9341. double hi, lo;
  9342. } v;
  9343. volatile double x, y;
  9344. int rule;
  9345. rule = fegetround ();
  9346. if (fesetround (FE_UPWARD) == 0)
  9347. {
  9348. v.hi = x + y;
  9349. v.lo = -(-x - y);
  9350. }
  9351. else
  9352. fatal ("ERROR: failed to change rounding rule");
  9353. if (fesetround (rule) != 0)
  9354. fatal ("ERROR: failed to restore rounding rule");
  9355. }
  9356. The ‘volatile’ qualifier (*note volatile::) is essential on x86
  9357. platforms to prevent an optimizing compiler from producing the same
  9358. value for both bounds.
  9359. 28.18 Machine Epsilon
  9360. =====================
  9361. In any floating-point system, three attributes are particularly
  9362. important to know: “base” (the number that the exponent specifies a
  9363. power of), “precision” (number of digits in the significand), and
  9364. “range” (difference between most positive and most negative values).
  9365. The allocation of bits between exponent and significand decides the
  9366. answers to those questions.
  9367. A measure of the precision is the answer to the question: what is the
  9368. smallest number that can be added to ‘1.0’ such that the sum differs
  9369. from ‘1.0’? That number is called the “machine epsilon”.
  9370. We could define the needed machine-epsilon constants for ‘float’,
  9371. ‘double’, and ‘long double’ like this:
  9372. static const float epsf = 0x1p-23; /* about 1.192e-07 */
  9373. static const double eps = 0x1p-52; /* about 2.220e-16 */
  9374. static const long double epsl = 0x1p-63; /* about 1.084e-19 */
  9375. Instead of the hexadecimal constants, we could also have used the
  9376. Standard C macros, ‘FLT_EPSILON’, ‘DBL_EPSILON’, and ‘LDBL_EPSILON’.
  9377. It is useful to be able to compute the machine epsilons at run time,
  9378. and we can easily generalize the operation by replacing the constant
  9379. ‘1.0’ with a user-supplied value:
  9380. double
  9381. macheps (double x)
  9382. { /* Return machine epsilon for X, */
  9383. such that X + macheps (X) &tt; X. */
  9384. static const double base = 2.0;
  9385. double eps;
  9386. if (isnan (x))
  9387. eps = x;
  9388. else
  9389. {
  9390. eps = (x == 0.0) ? 1.0 : x;
  9391. while ((x + eps / base) != x)
  9392. eps /= base; /* Always exact! */
  9393. }
  9394. return (eps);
  9395. }
  9396. If we call that function with arguments from ‘0’ to ‘10’, as well as
  9397. Infinity and NaN, and print the returned values in hexadecimal, we get
  9398. output like this:
  9399. macheps ( 0) = 0x1.0000000000000p-1074
  9400. macheps ( 1) = 0x1.0000000000000p-52
  9401. macheps ( 2) = 0x1.0000000000000p-51
  9402. macheps ( 3) = 0x1.8000000000000p-52
  9403. macheps ( 4) = 0x1.0000000000000p-50
  9404. macheps ( 5) = 0x1.4000000000000p-51
  9405. macheps ( 6) = 0x1.8000000000000p-51
  9406. macheps ( 7) = 0x1.c000000000000p-51
  9407. macheps ( 8) = 0x1.0000000000000p-49
  9408. macheps ( 9) = 0x1.2000000000000p-50
  9409. macheps ( 10) = 0x1.4000000000000p-50
  9410. macheps (Inf) = infinity
  9411. macheps (NaN) = nan
  9412. Notice that ‘macheps’ has a special test for a NaN to prevent an
  9413. infinite loop.
  9414. Our code made another test for a zero argument to avoid getting a
  9415. zero return. The returned value in that case is the smallest
  9416. representable floating-point number, here the subnormal value
  9417. ‘2**(-1074)’, which is about ‘4.941e-324’.
  9418. No special test is needed for an Infinity, because the
  9419. ‘eps’-reduction loop then terminates at the first iteration.
  9420. Our ‘macheps’ function here assumes binary floating point; some
  9421. architectures may differ.
  9422. The C library includes some related functions that can also be used
  9423. to determine machine epsilons at run time:
  9424. #include <math.h> /* Include for these prototypes. */
  9425. double nextafter (double x, double y);
  9426. float nextafterf (float x, float y);
  9427. long double nextafterl (long double x, long double y);
  9428. These return the machine number nearest X in the direction of Y. For
  9429. example, ‘nextafter (1.0, 2.0)’ produces the same result as ‘1.0 +
  9430. macheps (1.0)’ and ‘1.0 + DBL_EPSILON’. *Note (libc)FP Bit Twiddling::.
  9431. It is important to know that the machine epsilon is not symmetric
  9432. about all numbers. At the boundaries where normalization changes the
  9433. exponent, the epsilon below X is smaller than that just above X by a
  9434. factor ‘1 / base’. For example, ‘macheps (1.0)’ returns ‘+0x1p-52’,
  9435. whereas ‘macheps (-1.0)’ returns ‘+0x1p-53’. Some authors distinguish
  9436. those cases by calling them the _positive_ and _negative_, or _big_ and
  9437. _small_, machine epsilons. You can produce their values like this:
  9438. eps_neg = 1.0 - nextafter (1.0, -1.0);
  9439. eps_pos = nextafter (1.0, +2.0) - 1.0;
  9440. If X is a variable, such that you do not know its value at compile
  9441. time, then you can substitute literal Y values with either ‘-inf()’ or
  9442. ‘+inf()’, like this:
  9443. eps_neg = x - nextafter (x, -inf ());
  9444. eps_pos = nextafter (x, +inf() - x);
  9445. In such cases, if X is Infinity, then _the ‘nextafter’ functions return
  9446. Y if X equals Y_. Our two assignments then produce
  9447. ‘+0x1.fffffffffffffp+1023’ (about 1.798e+308) for EPS_NEG and Infinity
  9448. for EPS_POS. Thus, the call ‘nextafter (INFINITY, -INFINITY)’ can be
  9449. used to find the largest representable finite number, and with the call
  9450. ‘nextafter (0.0, 1.0)’, the smallest representable number (here,
  9451. ‘0x1p-1074’ (about 4.491e-324), a number that we saw before as the
  9452. output from ‘macheps (0.0)’).
  9453. 28.19 Complex Arithmetic
  9454. ========================
  9455. We’ve already looked at defining and referring to complex numbers (*note
  9456. Complex Data Types::). What is important to discuss here are some
  9457. issues that are unlikely to be obvious to programmers without extensive
  9458. experience in both numerical computing, and in complex arithmetic in
  9459. mathematics.
  9460. The first important point is that, unlike real arithmetic, in complex
  9461. arithmetic, the danger of significance loss is _pervasive_, and affects
  9462. _every one_ of the basic operations, and _almost all_ of the
  9463. math-library functions. To understand why, recall the rules for complex
  9464. multiplication and division:
  9465. a = u + I*v /* First operand. */
  9466. b = x + I*y /* Second operand. */
  9467. prod = a * b
  9468. = (u + I*v) * (x + I*y)
  9469. = (u * x - v * y) + I*(v * x + u * y)
  9470. quo = a / b
  9471. = (u + I*v) / (x + I*y)
  9472. = [(u + I*v) * (x - I*y)] / [(x + I*y) * (x - I*y)]
  9473. = [(u * x + v * y) + I*(v * x - u * y)] / (x**2 + y**2)
  9474. There are four critical observations about those formulas:
  9475. • the multiplications on the right-hand side introduce the
  9476. possibility of premature underflow or overflow;
  9477. • the products must be accurate to twice working precision;
  9478. • there is _always_ one subtraction on the right-hand sides that is
  9479. subject to catastrophic significance loss; and
  9480. • complex multiplication has up to _six_ rounding errors, and complex
  9481. division has _ten_ rounding errors.
  9482. Another point that needs careful study is the fact that many
  9483. functions in complex arithmetic have “branch cuts”. You can view a
  9484. function with a complex argument, ‘f (z)’, as ‘f (x + I*y)’, and thus,
  9485. it defines a relation between a point ‘(x, y)’ on the complex plane with
  9486. an elevation value on a surface. A branch cut looks like a tear in that
  9487. surface, so approaching the cut from one side produces a particular
  9488. value, and from the other side, a quite different value. Great care is
  9489. needed to handle branch cuts properly, and even small numerical errors
  9490. can push a result from one side to the other, radically changing the
  9491. returned value. As we reported earlier, correct handling of the sign of
  9492. zero is critically important for computing near branch cuts.
  9493. The best advice that we can give to programmers who need complex
  9494. arithmetic is to always use the _highest precision available_, and then
  9495. to carefully check the results of test calculations to gauge the likely
  9496. accuracy of the computed results. It is easy to supply test values of
  9497. real and imaginary parts where all five basic operations in complex
  9498. arithmetic, and almost all of the complex math functions, lose _all_
  9499. significance, and fail to produce even a single correct digit.
  9500. Even though complex arithmetic makes some programming tasks easier,
  9501. it may be numerically preferable to rework the algorithm so that it can
  9502. be carried out in real arithmetic. That is commonly possible in matrix
  9503. algebra.
  9504. GNU C can perform code optimization on complex number multiplication
  9505. and division if certain boundary checks will not be needed. The
  9506. command-line option ‘-fcx-limited-range’ tells the compiler that a range
  9507. reduction step is not needed when performing complex division, and that
  9508. there is no need to check if a complex multiplication or division
  9509. results in the value ‘Nan + I*NaN’. By default these checks are
  9510. enabled. You can explicitly enable them with the
  9511. ‘-fno-cx-limited-range’ option.
  9512. 28.20 Round-Trip Base Conversion
  9513. ================================
  9514. Most numeric programs involve converting between base-2 floating-point
  9515. numbers, as represented by the computer, and base-10 floating-point
  9516. numbers, as entered and handled by the programmer. What might not be
  9517. obvious is the number of base-2 bits vs. base-10 digits required for
  9518. each representation. Consider the following tables showing the number
  9519. of decimal digits representable in a given number of bits, and vice
  9520. versa:
  9521. binary in 24 53 64 113 237
  9522. decimal out 9 17 21 36 73
  9523. decimal in 7 16 34 70
  9524. binary out 25 55 114 234
  9525. We can compute the table numbers with these two functions:
  9526. int
  9527. matula(int nbits)
  9528. { /* Return output decimal digits needed for nbits-bits input. */
  9529. return ((int)ceil((double)nbits / log2(10.0) + 1.0));
  9530. }
  9531. int
  9532. goldberg(int ndec)
  9533. { /* Return output bits needed for ndec-digits input. */
  9534. return ((int)ceil((double)ndec / log10(2.0) + 1.0));
  9535. }
  9536. One significant observation from those numbers is that we cannot
  9537. achieve correct round-trip conversion between the decimal and binary
  9538. formats in the same storage size! For example, we need 25 bits to
  9539. represent a 7-digit value from the 32-bit decimal format, but the binary
  9540. format only has 24 available. Similar observations hold for each of the
  9541. other conversion pairs.
  9542. The general input/output base-conversion problem is astonishingly
  9543. complicated, and solutions were not generally known until the
  9544. publication of two papers in 1990 that are listed later near the end of
  9545. this chapter. For the 128-bit formats, the worst case needs more than
  9546. 11,500 decimal digits of precision to guarantee correct rounding in a
  9547. binary-to-decimal conversion!
  9548. For further details see the references for Bennett Goldberg and David
  9549. Matula.
  9550. 28.21 Further Reading
  9551. =====================
  9552. The subject of floating-point arithmetic is much more complex than many
  9553. programmers seem to think, and few books on programming languages spend
  9554. much time in that area. In this chapter, we have tried to expose the
  9555. reader to some of the key ideas, and to warn of easily overlooked
  9556. pitfalls that can soon lead to nonsensical results. There are a few
  9557. good references that we recommend for further reading, and for finding
  9558. other important material about computer arithmetic:
  9559. • Paul H. Abbott and 15 others, ‘Architecture and software support in
  9560. IBM S/390 Parallel Enterprise Servers for IEEE Floating-Point
  9561. arithmetic’, IBM Journal of Research and Development 43(5/6)
  9562. 723–760 (1999), >https://doi.org/10.1147/rd.435.0723<. This
  9563. article gives a good description of IBM’s algorithm for exact
  9564. decimal-to-binary conversion, complementing earlier ones by Clinger
  9565. and others.
  9566. • Nelson H. F. Beebe, ‘The Mathematical-Function Computation
  9567. Handbook: Programming Using the MathCW Portable Software Library’,
  9568. Springer (2017), ISBN 3-319-64109-3 (hardcover), 3-319-64110-7
  9569. (e-book) (xxxvi + 1114 pages),
  9570. >https://doi.org/10.1007/978-3-319-64110-2<. This book describes
  9571. portable implementations of a large superset of the mathematical
  9572. functions available in many programming languages, extended to a
  9573. future 256-bit format (70 decimal digits), for both binary and
  9574. decimal floating point. It includes a substantial portion of the
  9575. functions described in the famous ‘NIST Handbook of Mathematical
  9576. Functions’, Cambridge (2018), ISBN 0-521-19225-0. See
  9577. >http://www.math.utah.edu/pub/mathcw< for compilers and libraries.
  9578. • William D. Clinger, ‘How to Read Floating Point Numbers
  9579. Accurately’, ACM SIGPLAN Notices 25(6) 92–101 (June 1990),
  9580. >https://doi.org/10.1145/93548.93557<. See also the papers by
  9581. Steele & White.
  9582. • William D. Clinger, ‘Retrospective: How to read floating point
  9583. numbers accurately’, ACM SIGPLAN Notices 39(4) 360–371 (April
  9584. 2004), >http://doi.acm.org/10.1145/989393.989430<. Reprint of 1990
  9585. paper, with additional commentary.
  9586. • I. Bennett Goldberg, ‘27 Bits Are Not Enough For 8-Digit Accuracy’,
  9587. Communications of the ACM 10(2) 105–106 (February 1967),
  9588. >http://doi.acm.org/10.1145/363067.363112<. This paper, and its
  9589. companions by David Matula, address the base-conversion problem,
  9590. and show that the naive formulas are wrong by one or two digits.
  9591. • David Goldberg, ‘What Every Computer Scientist Should Know About
  9592. Floating-Point Arithmetic’, ACM Computing Surveys 23(1) 5–58 (March
  9593. 1991), corrigendum 23(3) 413 (September 1991),
  9594. >https://doi.org/10.1145/103162.103163<. This paper has been
  9595. widely distributed, and reissued in vendor programming-language
  9596. documentation. It is well worth reading, and then rereading from
  9597. time to time.
  9598. • Norbert Juffa and Nelson H. F. Beebe, ‘A Bibliography of
  9599. Publications on Floating-Point Arithmetic’,
  9600. >http://www.math.utah.edu/pub/tex/bib/fparith.bib<. This is the
  9601. largest known bibliography of publications about floating-point,
  9602. and also integer, arithmetic. It is actively maintained, and in
  9603. mid 2019, contains more than 6400 references to original research
  9604. papers, reports, theses, books, and Web sites on the subject
  9605. matter. It can be used to locate the latest research in the field,
  9606. and the historical coverage dates back to a 1726 paper on
  9607. signed-digit arithmetic, and an 1837 paper by Charles Babbage, the
  9608. intellectual father of mechanical computers. The entries for the
  9609. Abbott, Clinger, and Steele & White papers cited earlier contain
  9610. pointers to several other important related papers on the
  9611. base-conversion problem.
  9612. • William Kahan, ‘Branch Cuts for Complex Elementary Functions, or
  9613. Much Ado About Nothing’s Sign Bit’, (1987),
  9614. >http://people.freebsd.org/~das/kahan86branch.pdf<. This Web
  9615. document about the fine points of complex arithmetic also appears
  9616. in the volume edited by A. Iserles and M. J. D. Powell, ‘The State
  9617. of the Art in Numerical Analysis: Proceedings of the Joint IMA/SIAM
  9618. Conference on the State of the Art in Numerical Analysis held at
  9619. the University of Birmingham, 14–18 April 1986’, Oxford University
  9620. Press (1987), ISBN 0-19-853614-3 (xiv + 719 pages). Its author is
  9621. the famous chief architect of the IEEE 754 arithmetic system, and
  9622. one of the world’s greatest experts in the field of floating-point
  9623. arithmetic. An entire generation of his students at the University
  9624. of California, Berkeley, have gone on to careers in academic and
  9625. industry, spreading the knowledge of how to do floating-point
  9626. arithmetic right.
  9627. • Donald E. Knuth, ‘A Simple Program Whose Proof Isn’t’, in ‘Beauty
  9628. is our business: a birthday salute to Edsger W. Dijkstra’, W. H. J.
  9629. Feijen, A. J. M. van Gasteren, D. Gries, and J. Misra (eds.),
  9630. Springer (1990), ISBN 1-4612-8792-8,
  9631. >https://doi.org/10.1007/978-1-4612-4476-9<. This book chapter
  9632. supplies a correctness proof of the decimal to binary, and binary
  9633. to decimal, conversions in fixed-point arithmetic in the TeX
  9634. typesetting system. The proof evaded its author for a dozen years.
  9635. • David W. Matula, ‘In-and-out conversions’, Communications of the
  9636. ACM 11(1) 57–50 (January 1968),
  9637. >https://doi.org/10.1145/362851.362887<.
  9638. • David W. Matula, ‘The Base Conversion Theorem’, Proceedings of the
  9639. American Mathematical Society 19(3) 716–723 (June 1968). See also
  9640. other papers here by this author, and by I. Bennett Goldberg.
  9641. • David W. Matula, ‘A Formalization of Floating-Point Numeric Base
  9642. Conversion’, IEEE Transactions on Computers C-19(8) 681–692 (August
  9643. 1970), >https://doi.org/10.1109/T-C.1970.223017<.
  9644. • Jean-Michel Muller and eight others, ‘Handbook of Floating-Point
  9645. Arithmetic’, Birkhäuser-Boston (2010), ISBN 0-8176-4704-X (xxiii +
  9646. 572 pages), >https://doi.org/10.1007/978-0-8176-4704-9<. This is a
  9647. comprehensive treatise from a French team who are among the world’s
  9648. greatest experts in floating-point arithmetic, and among the most
  9649. prolific writers of research papers in that field. They have much
  9650. to teach, and their book deserves a place on the shelves of every
  9651. serious numerical programmer.
  9652. • Jean-Michel Muller and eight others, ‘Handbook of Floating-Point
  9653. Arithmetic’, Second edition, Birkhäuser-Boston (2018), ISBN
  9654. 3-319-76525-6 (xxv + 627 pages),
  9655. >https://doi.org/10.1007/978-3-319-76526-6<. This is a new edition
  9656. of the preceding entry.
  9657. • Michael Overton, ‘Numerical Computing with IEEE Floating Point
  9658. Arithmetic, Including One Theorem, One Rule of Thumb, and One
  9659. Hundred and One Exercises’, SIAM (2001), ISBN 0-89871-482-6 (xiv +
  9660. 104 pages), >http://www.ec-securehost.com/SIAM/ot76.html<. This is
  9661. a small volume that can be covered in a few hours.
  9662. • Guy L. Steele Jr. and Jon L. White, ‘How to Print Floating-Point
  9663. Numbers Accurately’, ACM SIGPLAN Notices 25(6) 112–126 (June 1990),
  9664. >https://doi.org/10.1145/93548.93559<. See also the papers by
  9665. Clinger.
  9666. • Guy L. Steele Jr. and Jon L. White, ‘Retrospective: How to Print
  9667. Floating-Point Numbers Accurately’, ACM SIGPLAN Notices 39(4)
  9668. 372–389 (April 2004), >http://doi.acm.org/10.1145/989393.989431<.
  9669. Reprint of 1990 paper, with additional commentary.
  9670. • Pat H. Sterbenz, ‘Floating Point Computation’, Prentice-Hall
  9671. (1974), ISBN 0-13-322495-3 (xiv + 316 pages). This often-cited
  9672. book provides solid coverage of what floating-point arithmetic was
  9673. like _before_ the introduction of IEEE 754 arithmetic.
  9674. 29 Compilation
  9675. **************
  9676. Early in the manual we explained how to compile a simple C program that
  9677. consists of a single source file (*note Compile Example::). However, we
  9678. handle only short programs that way. A typical C program consists of
  9679. many source files, each of which is a separate “compilation
  9680. module”—meaning that it has to be compiled separately.
  9681. The full details of how to compile with GCC are documented in xxxx.
  9682. Here we give only a simple introduction.
  9683. These are the commands to compile two compilation modules, ‘foo.c’
  9684. and ‘bar.c’, with a command for each module:
  9685. gcc -c -O -g foo.c
  9686. gcc -c -O -g bar.c
  9687. In these commands, ‘-g’ says to generate debugging information, ‘-O’
  9688. says to do some optimization, and ‘-c’ says to put the compiled code for
  9689. that module into a corresponding “object file” and go no further. The
  9690. object file for ‘foo.c’ is called ‘foo.o’, and so on.
  9691. If you wish, you can specify the additional options ‘-Wformat
  9692. -Wparenthesis -Wstrict-prototypes’, which request additional warnings.
  9693. One reason to divide a large program into multiple compilation
  9694. modules is to control how each module can access the internals of the
  9695. others. When a module declares a function or variable ‘extern’, other
  9696. modules can access it. The other functions and variables in a module
  9697. can’t be accessed from outside that module.
  9698. The other reason for using multiple modules is so that changing one
  9699. source file does not require recompiling all of them in order to try the
  9700. modified program. Dividing a large program into many substantial
  9701. modules in this way typically makes recompilation much faster.
  9702. After you compile all the program’s modules, in order to run the
  9703. program you must “link” the object files into a combined executable,
  9704. like this:
  9705. gcc -o foo foo.o bar.o
  9706. In this command, ‘-o foo’ species the file name for the executable file,
  9707. and the other arguments are the object files to link. Always specify
  9708. the executable file name in a command that generates one.
  9709. Normally we don’t run any of these commands directly. Instead we
  9710. write a set of “make rules” for the program, then use the ‘make’ program
  9711. to recompile only the source files that need to be recompiled.
  9712. 30 Directing Compilation
  9713. ************************
  9714. This chapter describes C constructs that don’t alter the program’s
  9715. meaning _as such_, but rather direct the compiler how to treat some
  9716. aspects of the program.
  9717. 30.1 Pragmas
  9718. ============
  9719. A “pragma” is an annotation in a program that gives direction to the
  9720. compiler.
  9721. 30.1.1 Pragma Basics
  9722. --------------------
  9723. C defines two syntactical forms for pragmas, the line form and the token
  9724. form. You can write any pragma in either form, with the same meaning.
  9725. The line form is a line in the source code, like this:
  9726. #pragma LINE
  9727. The line pragma has no effect on the parsing of the lines around it.
  9728. This form has the drawback that it can’t be generated by a macro
  9729. expansion.
  9730. The token form is a series of tokens; it can appear anywhere in the
  9731. program between the other tokens.
  9732. _Pragma (STRINGCONSTANT)
  9733. The pragma has no effect on the syntax of the tokens that surround it;
  9734. thus, here’s a pragma in the middle of an ‘if’ statement:
  9735. if _Pragma ("hello") (x &tt; 1)
  9736. However, that’s an unclear thing to do; for the sake of
  9737. understandability, it is better to put a pragma on a line by itself and
  9738. not embedded in the middle of another construct.
  9739. Both forms of pragma have a textual argument. In a line pragma, the
  9740. text is the rest of the line. The textual argument to ‘_Pragma’ uses
  9741. the same syntax as a C string constant: surround the text with two ‘"’
  9742. characters, and add a backslash before each ‘"’ or ‘\’ character in it.
  9743. With either syntax, the textual argument specifies what to do. It
  9744. begins with one or several words that specify the operation. If the
  9745. compiler does not recognize them, it ignores the pragma.
  9746. Here are the pragma operations supported in GNU C.
  9747. ‘#pragma GCC dependency "FILE" [MESSAGE]’
  9748. ‘_Pragma ("GCC dependency \"FILE\" [MESSAGE]")’
  9749. Declares that the current source file depends on FILE, so GNU C
  9750. compares the file times and gives a warning if FILE is newer than
  9751. the current source file.
  9752. This directive searches for FILE the way ‘#include’ searches for a
  9753. non-system header file.
  9754. If MESSAGE is given, the warning message includes that text.
  9755. Examples:
  9756. #pragma GCC dependency "parse.y"
  9757. _pragma ("GCC dependency \"/usr/include/time.h\" \
  9758. rerun fixincludes")
  9759. ‘#pragma GCC poison IDENTIFIERS’
  9760. ‘_Pragma ("GCC poison IDENTIFIERS")’
  9761. Poisons the identifiers listed in IDENTIFIERS.
  9762. This is useful to make sure all mention of IDENTIFIERS has been
  9763. deleted from the program and that no reference to them creeps back
  9764. in. If any of those identifiers appears anywhere in the source
  9765. after the directive, it causes a compilation error. For example,
  9766. #pragma GCC poison printf sprintf fprintf
  9767. sprintf(some_string, "hello");
  9768. generates an error.
  9769. If a poisoned identifier appears as part of the expansion of a
  9770. macro that was defined before the identifier was poisoned, it will
  9771. _not_ cause an error. Thus, system headers that define macros that
  9772. use the identifier will not cause errors.
  9773. For example,
  9774. #define strrchr rindex
  9775. _Pragma ("GCC poison rindex")
  9776. strrchr(some_string, 'h');
  9777. does not cause a compilation error.
  9778. ‘#pragma GCC system_header’
  9779. ‘_Pragma ("GCC system_header")’
  9780. Specify treating the rest of the current source file as if it came
  9781. from a system header file. *Note System Headers: (gcc)System
  9782. Headers.
  9783. ‘#pragma GCC warning MESSAGE’
  9784. ‘_Pragma ("GCC warning MESSAGE")’
  9785. Equivalent to ‘#warning’. Its advantage is that the ‘_Pragma’ form
  9786. can be included in a macro definition.
  9787. ‘#pragma GCC error MESSAGE’
  9788. ‘_Pragma ("GCC error MESSAGE")’
  9789. Equivalent to ‘#error’. Its advantage is that the ‘_Pragma’ form
  9790. can be included in a macro definition.
  9791. ‘#pragma GCC message MESSAGE’
  9792. ‘_Pragma ("GCC message MESSAGE")’
  9793. Similar to ‘GCC warning’ and ‘GCC error’, this simply prints an
  9794. informational message, and could be used to include additional
  9795. warning or error text without triggering more warnings or errors.
  9796. (Note that unlike ‘warning’ and ‘error’, ‘message’ does not include
  9797. ‘GCC’ as part of the pragma.)
  9798. 30.1.2 Severity Pragmas
  9799. -----------------------
  9800. These pragmas control the severity of classes of diagnostics. You can
  9801. specify the class of diagnostic with the GCC option that causes those
  9802. diagnostics to be generated.
  9803. ‘#pragma GCC diagnostic error OPTION’
  9804. ‘_Pragma ("GCC diagnostic error OPTION")’
  9805. For code following this pragma, treat diagnostics of the variety
  9806. specified by OPTION as errors. For example:
  9807. _Pragma ("GCC diagnostic error -Wformat")
  9808. specifies to treat diagnostics enabled by the -WFORMAT option as
  9809. errors rather than warnings.
  9810. ‘#pragma GCC diagnostic warning OPTION’
  9811. ‘_Pragma ("GCC diagnostic warning OPTION")’
  9812. For code following this pragma, treat diagnostics of the variety
  9813. specified by OPTION as warnings. This overrides the -WERROR option
  9814. which says to treat warnings as errors.
  9815. ‘#pragma GCC diagnostic ignore OPTION’
  9816. ‘_Pragma ("GCC diagnostic ignore OPTION")’
  9817. For code following this pragma, refrain from reporting any
  9818. diagnostics of the variety specified by OPTION.
  9819. ‘#pragma GCC diagnostic push’
  9820. ‘_Pragma ("GCC diagnostic push")’
  9821. ‘#pragma GCC diagnostic pop’
  9822. ‘_Pragma ("GCC diagnostic pop")’
  9823. These pragmas maintain a stack of states for severity settings.
  9824. ‘GCC diagnostic push’ saves the current settings on the stack, and
  9825. ‘GCC diagnostic pop’ pops the last stack item and restores the
  9826. current settings from that.
  9827. ‘GCC diagnostic pop’ when the severity setting stack is empty
  9828. restores the settings to what they were at the start of
  9829. compilation.
  9830. Here is an example:
  9831. _Pragma ("GCC diagnostic error -Wformat")
  9832. /* ‘-Wformat’ messages treated as errors. */
  9833. _Pragma ("GCC diagnostic push")
  9834. _Pragma ("GCC diagnostic warning -Wformat")
  9835. /* ‘-Wformat’ messages treated as warnings. */
  9836. _Pragma ("GCC diagnostic push")
  9837. _Pragma ("GCC diagnostic ignored -Wformat")
  9838. /* ‘-Wformat’ messages suppressed. */
  9839. _Pragma ("GCC diagnostic pop")
  9840. /* ‘-Wformat’ messages treated as warnings again. */
  9841. _Pragma ("GCC diagnostic pop")
  9842. /* ‘-Wformat’ messages treated as errors again. */
  9843. /* This is an excess ‘pop’ that matches no ‘push’. */
  9844. _Pragma ("GCC diagnostic pop")
  9845. /* ‘-Wformat’ messages treated once again
  9846. as specified by the GCC command-line options. */
  9847. 30.1.3 Optimization Pragmas
  9848. ---------------------------
  9849. These pragmas enable a particular optimization for specific function
  9850. definitions. The settings take effect at the end of a function
  9851. definition, so the clean place to use these pragmas is between function
  9852. definitions.
  9853. ‘#pragma GCC optimize OPTIMIZATION’
  9854. ‘_Pragma ("GCC optimize OPTIMIZATION")’
  9855. These pragmas enable the optimization OPTIMIZATION for the
  9856. following functions. For example,
  9857. _Pragma ("GCC optimize -fforward-propagate")
  9858. says to apply the ‘forward-propagate’ optimization to all following
  9859. function definitions. Specifying optimizations for individual
  9860. functions, rather than for the entire program, is rare but can be
  9861. useful for getting around a bug in the compiler.
  9862. If OPTIMIZATION does not correspond to a defined optimization
  9863. option, the pragma is erroneous. To turn off an optimization, use
  9864. the corresponding ‘-fno-’ option, such as ‘-fno-forward-propagate’.
  9865. ‘#pragma GCC target OPTIMIZATIONS’
  9866. ‘_Pragma ("GCC target OPTIMIZATIONS")’
  9867. The pragma ‘GCC target’ is similar to ‘GCC optimize’ but is used
  9868. for platform-specific optimizations. Thus,
  9869. _Pragma ("GCC target popcnt")
  9870. activates the optimization ‘popcnt’ for all following function
  9871. definitions. This optimization is supported on a few common
  9872. targets but not on others.
  9873. ‘#pragma GCC push_options’
  9874. ‘_Pragma ("GCC push_options")’
  9875. The ‘push_options’ pragma saves on a stack the current settings
  9876. specified with the ‘target’ and ‘optimize’ pragmas.
  9877. ‘#pragma GCC pop_options’
  9878. ‘_Pragma ("GCC pop_options")’
  9879. The ‘pop_options’ pragma pops saved settings from that stack.
  9880. Here’s an example of using this stack.
  9881. _Pragma ("GCC push_options")
  9882. _Pragma ("GCC optimize forward-propagate")
  9883. /* Functions to compile
  9884. with the ‘forward-propagate’ optimization. */
  9885. _Pragma ("GCC pop_options")
  9886. /* Ends enablement of ‘forward-propagate’. */
  9887. ‘#pragma GCC reset_options’
  9888. ‘_Pragma ("GCC reset_options")’
  9889. Clears all pragma-defined ‘target’ and ‘optimize’ optimization
  9890. settings.
  9891. 30.2 Static Assertions
  9892. ======================
  9893. You can add compiler-time tests for necessary conditions into your code
  9894. using ‘_Static_assert’. This can be useful, for example, to check that
  9895. the compilation target platform supports the type sizes that the code
  9896. expects. For example,
  9897. _Static_assert ((sizeof (long int) <= 8),
  9898. "long int needs to be at least 8 bytes");
  9899. reports a compile-time error if compiled on a system with long integers
  9900. smaller than 8 bytes, with ‘long int needs to be at least 8 bytes’ as
  9901. the error message.
  9902. Since calls ‘_Static_assert’ are processed at compile time, the
  9903. expression must be computable at compile time and the error message must
  9904. be a literal string. The expression can refer to the sizes of
  9905. variables, but can’t refer to their values. For example, the following
  9906. static assertion is invalid for two reasons:
  9907. char *error_message
  9908. = "long int needs to be at least 8 bytes";
  9909. int size_of_long_int = sizeof (long int);
  9910. _Static_assert (size_of_long_int == 8, error_message);
  9911. The expression ‘size_of_long_int == 8’ isn’t computable at compile time,
  9912. and the error message isn’t a literal string.
  9913. You can, though, use preprocessor definition values with
  9914. ‘_Static_assert’:
  9915. #define LONG_INT_ERROR_MESSAGE "long int needs to be \
  9916. at least 8 bytes"
  9917. _Static_assert ((sizeof (long int) == 8),
  9918. LONG_INT_ERROR_MESSAGE);
  9919. Static assertions are permitted wherever a statement or declaration
  9920. is permitted, including at top level in the file, and also inside the
  9921. definition of a type.
  9922. union y
  9923. {
  9924. int i;
  9925. int *ptr;
  9926. _Static_assert (sizeof (int *) == sizeof (int),
  9927. "Pointer and int not same size");
  9928. };
  9929. Appendix A Type Alignment
  9930. *************************
  9931. Code for device drivers and other communication with low-level hardware
  9932. sometimes needs to be concerned with the alignment of data objects in
  9933. memory.
  9934. Each data type has a required “alignment”, always a power of 2, that
  9935. says at which memory addresses an object of that type can validly start.
  9936. A valid address for the type must be a multiple of its alignment. If a
  9937. type’s alignment is 1, that means it can validly start at any address.
  9938. If a type’s alignment is 2, that means it can only start at an even
  9939. address. If a type’s alignment is 4, that means it can only start at an
  9940. address that is a multiple of 4.
  9941. The alignment of a type (except ‘char’) can vary depending on the
  9942. kind of computer in use. To refer to the alignment of a type in a C
  9943. program, use ‘_Alignof’, whose syntax parallels that of ‘sizeof’. Like
  9944. ‘sizeof’, ‘_Alignof’ is a compile-time operation, and it doesn’t compute
  9945. the value of the expression used as its argument.
  9946. Nominally, each integer and floating-point type has an alignment
  9947. equal to the largest power of 2 that divides its size. Thus, ‘int’ with
  9948. size 4 has a nominal alignment of 4, and ‘long long int’ with size 8 has
  9949. a nominal alignment of 8.
  9950. However, each kind of computer generally has a maximum alignment, and
  9951. no type needs more alignment than that. If the computer’s maximum
  9952. alignment is 4 (which is common), then no type’s alignment is more than
  9953. 4.
  9954. The size of any type is always a multiple of its alignment; that way,
  9955. in an array whose elements have that type, all the elements are properly
  9956. aligned if the first one is.
  9957. These rules apply to all real computers today, but some embedded
  9958. controllers have odd exceptions. We don’t have references to cite for
  9959. them.
  9960. Ordinary C code guarantees that every object of a given type is in
  9961. fact aligned as that type requires.
  9962. If the operand of ‘_Alignof’ is a structure field, the value is the
  9963. alignment it requires. It may have a greater alignment by coincidence,
  9964. due to the other fields, but ‘_Alignof’ is not concerned about that.
  9965. *Note Structures::.
  9966. Older versions of GNU C used the keyword ‘__alignof__’ for this, but
  9967. now that the feature has been standardized, it is better to use the
  9968. standard keyword ‘_Alignof’.
  9969. You can explicitly specify an alignment requirement for a particular
  9970. variable or structure field by adding ‘_Alignas (ALIGNMENT)’ to the
  9971. declaration, where ALIGNMENT is a power of 2 or a type name. For
  9972. instance:
  9973. char _Alignas (8) x;
  9974. or
  9975. char _Alignas (double) x;
  9976. specifies that ‘x’ must start on an address that is a multiple of 8.
  9977. However, if ALIGNMENT exceeds the maximum alignment for the machine,
  9978. that maximum is how much alignment ‘x’ will get.
  9979. The older GNU C syntax for this feature looked like ‘__attribute__
  9980. ((__aligned__ (ALIGNMENT)))’ to the declaration, and was added after the
  9981. variable. For instance:
  9982. char x __attribute__ ((__aligned__ 8));
  9983. *Note Attributes::.
  9984. Appendix B Aliasing
  9985. *******************
  9986. We have already presented examples of casting a ‘void *’ pointer to
  9987. another pointer type, and casting another pointer type to ‘void *’.
  9988. One common kind of pointer cast is guaranteed safe: casting the value
  9989. returned by ‘malloc’ and related functions (*note Dynamic Memory
  9990. Allocation::). It is safe because these functions do not save the
  9991. pointer anywhere else; the only way the program will access the newly
  9992. allocated memory is via the pointer just returned.
  9993. In fact, C allows casting any pointer type to any other pointer type.
  9994. Using this to access the same place in memory using two different data
  9995. types is called “aliasing”.
  9996. Aliasing is necessary in some programs that do sophisticated memory
  9997. management, such as GNU Emacs, but most C programs don’t need to do
  9998. aliasing. When it isn’t needed, *stay away from it!* To do aliasing
  9999. correctly requires following the rules stated below. Otherwise, the
  10000. aliasing may result in malfunctions when the program runs.
  10001. The rest of this appendix explains the pitfalls and rules of
  10002. aliasing.
  10003. B.1 Aliasing and Alignment
  10004. ==========================
  10005. In order for a type-converted pointer to be valid, it must have the
  10006. alignment that the new pointer type requires. For instance, on most
  10007. computers, ‘int’ has alignment 4; the address of an ‘int’ must be a
  10008. multiple of 4. However, ‘char’ has alignment 1, so the address of a
  10009. ‘char’ is usually not a multiple of 4. Taking the address of such a
  10010. ‘char’ and casting it to ‘int *’ probably results in an invalid pointer.
  10011. Trying to dereference it may cause a ‘SIGBUS’ signal, depending on the
  10012. platform in use (*note Signals::).
  10013. foo ()
  10014. {
  10015. char i[4];
  10016. int *p = (int *) &i[1]; /* Misaligned pointer! */
  10017. return *p; /* Crash! */
  10018. }
  10019. This requirement is never a problem when casting the return value of
  10020. ‘malloc’ because that function always returns a pointer with as much
  10021. alignment as any type can require.
  10022. B.2 Aliasing and Length
  10023. =======================
  10024. When converting a pointer to a different pointer type, make sure the
  10025. object it really points to is at least as long as the target of the
  10026. converted pointer. For instance, suppose ‘p’ has type ‘int *’ and it’s
  10027. cast as follows:
  10028. int *p;
  10029. struct
  10030. {
  10031. double d, e, f;
  10032. } foo;
  10033. struct foo *q = (struct foo *)p;
  10034. q-<f = 5.14159;
  10035. the value ‘q-<f’ will run past the end of the ‘int’ that ‘p’ points to.
  10036. If ‘p’ was initialized to the start of an array of type ‘int[6]’, the
  10037. object is long enough for three ‘double’s. But if ‘p’ points to
  10038. something shorter, ‘q-<f’ will run on beyond the end of that, overlaying
  10039. some other data. Storing that will garble that other data. Or it could
  10040. extend past the end of memory space and cause a ‘SIGSEGV’ signal (*note
  10041. Signals::).
  10042. B.3 Type Rules for Aliasing
  10043. ===========================
  10044. C code that converts a pointer to a different pointer type can use the
  10045. pointers to access the same memory locations with two different data
  10046. types. If the same address is accessed with different types in a single
  10047. control thread, optimization can make the code do surprising things (in
  10048. effect, make it malfunction).
  10049. Here’s a concrete example where aliasing that can change the code’s
  10050. behavior when it is optimized. We assume that ‘float’ is 4 bytes long,
  10051. like ‘int’, and so is every pointer. Thus, the structures ‘struct a’
  10052. and ‘struct b’ are both 8 bytes.
  10053. #include <stdio.h>
  10054. struct a { int size; char *data; };
  10055. struct b { float size; char *data; };
  10056. void sub (struct a *p, struct b *q)
  10057. {
  10058.   int x;
  10059.   p-<size = 0;
  10060.   q-<size = 1;
  10061.   x = p-<size;
  10062.   printf("x       =%d\n", x);
  10063.   printf("p-<size =%d\n", (int)p-<size);
  10064.   printf("q-<size =%d\n", (int)q-<size);
  10065. }
  10066. int main(void)
  10067. {
  10068.   struct a foo;
  10069.   struct a *p = &foo;
  10070.   struct b *q = (struct b *) &foo;
  10071.   sub (p, q);
  10072. }
  10073. This code works as intended when compiled without optimization. All
  10074. the operations are carried out sequentially as written. The code sets
  10075. ‘x’ to ‘p-<size’, but what it actually gets is the bits of the floating
  10076. point number 1, as type ‘int’.
  10077. However, when optimizing, the compiler is allowed to assume
  10078. (mistakenly, here) that ‘q’ does not point to the same storage as ‘p’,
  10079. because their data types are not allowed to alias.
  10080. From this assumption, the compiler can deduce (falsely, here) that
  10081. the assignment into ‘q-<size’ has no effect on the value of ‘p-<size’,
  10082. which must therefore still be 0. Thus, ‘x’ will be set to 0.
  10083. GNU C, following the C standard, _defines_ this optimization as
  10084. legitimate. Code that misbehaves when optimized following these rules
  10085. is, by definition, incorrect C code.
  10086. The rules for storage aliasing in C are based on the two data types:
  10087. the type of the object, and the type it is accessed through. The rules
  10088. permit accessing part of a storage object of type T using only these
  10089. types:
  10090. • T.
  10091. • A type compatible with T. *Note Compatible Types::.
  10092. • A signed or unsigned version of one of the above.
  10093. • A qualifed version of one of the above. *Note Type Qualifiers::.
  10094. • An array, structure (*note Structures::), or union type (‘Unions’)
  10095. that contains one of the above, either directly as a field or
  10096. through multiple levels of fields. If T is ‘double’, this would
  10097. include ‘struct s { union { double d[2]; int i[4]; } u; int i; };’
  10098. because there’s a ‘double’ inside it somewhere.
  10099. • A character type.
  10100. What do these rules say about the example in this subsection?
  10101. For ‘foo.size’ (equivalently, ‘a-<size’), T is ‘int’. The type
  10102. ‘float’ is not allowed as an aliasing type by those rules, so ‘b-<size’
  10103. is not supposed to alias with elements of ‘j’. Based on that
  10104. assumption, GNU C makes a permitted optimization that was not, in this
  10105. case, consistent with what the programmer intended the program to do.
  10106. Whether GCC actually performs type-based aliasing analysis depends on
  10107. the details of the code. GCC has other ways to determine (in some
  10108. cases) whether objects alias, and if it gets a reliable answer that way,
  10109. it won’t fall back on type-based heuristics.
  10110. The importance of knowing the type-based aliasing rules is not so as
  10111. to ensure that the optimization is done where it would be safe, but so
  10112. as to ensure it is _not_ done in a way that would break the program.
  10113. You can turn off type-based aliasing analysis by giving GCC the option
  10114. ‘-fno-strict-aliasing’.
  10115. Appendix C Digraphs
  10116. *******************
  10117. C accepts aliases for certain characters. Apparently in the 1990s some
  10118. computer systems had trouble inputting these characters, or trouble
  10119. displaying them. These digraphs almost never appear in C programs
  10120. nowadays, but we mention them for completeness.
  10121. ‘>:’
  10122. An alias for ‘[’.
  10123. ‘:<’
  10124. An alias for ‘]’.
  10125. ‘>%’
  10126. An alias for ‘{’.
  10127. ‘%<’
  10128. An alias for ‘}’.
  10129. ‘%:’
  10130. An alias for ‘#’, used for preprocessing directives (*note
  10131. Directives::) and macros (*note Macros::).
  10132. Appendix D Attributes in Declarations
  10133. *************************************
  10134. You can specify certain additional requirements in a declaration, to get
  10135. fine-grained control over code generation, and helpful informational
  10136. messages during compilation. We use a few attributes in code examples
  10137. throughout this manual, including
  10138. ‘aligned’
  10139. The ‘aligned’ attribute specifies a minimum alignment for a
  10140. variable or structure field, measured in bytes:
  10141. int foo __attribute__ ((aligned (8))) = 0;
  10142. This directs GNU C to allocate ‘foo’ at an address that is a
  10143. multiple of 8 bytes. However, you can’t force an alignment bigger
  10144. than the computer’s maximum meaningful alignment.
  10145. ‘packed’
  10146. The ‘packed’ attribute specifies to compact the fields of a
  10147. structure by not leaving gaps between fields. For example,
  10148. struct __attribute__ ((packed)) bar
  10149. {
  10150. char a;
  10151. int b;
  10152. };
  10153. allocates the integer field ‘b’ at byte 1 in the structure,
  10154. immediately after the character field ‘a’. The packed structure is
  10155. just 5 bytes long (assuming ‘int’ is 4 bytes) and its alignment is
  10156. 1, that of ‘char’.
  10157. ‘deprecated’
  10158. Applicable to both variables and functions, the ‘deprecated’
  10159. attribute tells the compiler to issue a warning if the variable or
  10160. function is ever used in the source file.
  10161. int old_foo __attribute__ ((deprecated));
  10162. int old_quux () __attribute__ ((deprecated));
  10163. ‘__noinline__’
  10164. The ‘__noinline__’ attribute, in a function’s declaration or
  10165. definition, specifies never to inline calls to that function. All
  10166. calls to that function, in a compilation unit where it has this
  10167. attribute, will be compiled to invoke the separately compiled
  10168. function. *Note Inline Function Definitions::.
  10169. ‘__noclone__’
  10170. The ‘__noclone__’ attribute, in a function’s declaration or
  10171. definition, specifies never to clone that function. Thus, there
  10172. will be only one compiled version of the function. *Note Label
  10173. Value Caveats::, for more information about cloning.
  10174. ‘always_inline’
  10175. The ‘always_inline’ attribute, in a function’s declaration or
  10176. definition, specifies to inline all calls to that function (unless
  10177. something about the function makes inlining impossible). This
  10178. applies to all calls to that function in a compilation unit where
  10179. it has this attribute. *Note Inline Function Definitions::.
  10180. ‘gnu_inline’
  10181. The ‘gnu_inline’ attribute, in a function’s declaration or
  10182. definition, specifies to handle the ‘inline’ keywprd the way GNU C
  10183. originally implemented it, many years before ISO C said anything
  10184. about inlining. *Note Inline Function Definitions::.
  10185. For full documentation of attributes, see the GCC manual. *Note
  10186. Attribute Syntax: (gcc)Attribute Syntax.
  10187. Appendix E Signals
  10188. ******************
  10189. Some program operations bring about an error condition called a
  10190. “signal”. These signals terminate the program, by default.
  10191. There are various different kinds of signals, each with a name. We
  10192. have seen several such error conditions through this manual:
  10193. ‘SIGSEGV’
  10194. This signal is generated when a program tries to read or write
  10195. outside the memory that is allocated for it, or to write memory
  10196. that can only be read. The name is an abbreviation for
  10197. “segmentation violation”.
  10198. ‘SIGFPE’
  10199. This signal indicates a fatal arithmetic error. The name is an
  10200. abbreviation for “floating-point exception”, but covers all types
  10201. of arithmetic errors, including division by zero and overflow.
  10202. ‘SIGBUS’
  10203. This signal is generated when an invalid pointer is dereferenced,
  10204. typically the result of dereferencing an uninintalized pointer. It
  10205. is similar to ‘SIGSEGV’, except that ‘SIGSEGV’ indicates invalid
  10206. access to valid memory, while ‘SIGBUS’ indicates an attempt to
  10207. access an invalid address.
  10208. These kinds of signal allow the program to specify a function as a
  10209. “signal handler”. When a signal has a handler, it doesn’t terminate the
  10210. program; instead it calls the handler.
  10211. There are many other kinds of signal; here we list only those that
  10212. come from run-time errors in C operations. The rest have to do with the
  10213. functioning of the operating system. The GNU C Library Reference Manual
  10214. gives more explanation about signals (*note The GNU C Library:
  10215. (libc)Program Signal Handling.).
  10216. Appendix F GNU Free Documentation License
  10217. *****************************************
  10218. Version 1.3, 3 November 2008
  10219. Copyright © 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.
  10220. >http://fsf.org/<
  10221. Everyone is permitted to copy and distribute verbatim copies
  10222. of this license document, but changing it is not allowed.
  10223. 0. PREAMBLE
  10224. The purpose of this License is to make a manual, textbook, or other
  10225. functional and useful document “free” in the sense of freedom: to
  10226. assure everyone the effective freedom to copy and redistribute it,
  10227. with or without modifying it, either commercially or
  10228. noncommercially. Secondarily, this License preserves for the
  10229. author and publisher a way to get credit for their work, while not
  10230. being considered responsible for modifications made by others.
  10231. This License is a kind of “copyleft”, which means that derivative
  10232. works of the document must themselves be free in the same sense.
  10233. It complements the GNU General Public License, which is a copyleft
  10234. license designed for free software.
  10235. We have designed this License in order to use it for manuals for
  10236. free software, because free software needs free documentation: a
  10237. free program should come with manuals providing the same freedoms
  10238. that the software does. But this License is not limited to
  10239. software manuals; it can be used for any textual work, regardless
  10240. of subject matter or whether it is published as a printed book. We
  10241. recommend this License principally for works whose purpose is
  10242. instruction or reference.
  10243. 1. APPLICABILITY AND DEFINITIONS
  10244. This License applies to any manual or other work, in any medium,
  10245. that contains a notice placed by the copyright holder saying it can
  10246. be distributed under the terms of this License. Such a notice
  10247. grants a world-wide, royalty-free license, unlimited in duration,
  10248. to use that work under the conditions stated herein. The
  10249. “Document”, below, refers to any such manual or work. Any member
  10250. of the public is a licensee, and is addressed as “you”. You accept
  10251. the license if you copy, modify or distribute the work in a way
  10252. requiring permission under copyright law.
  10253. A “Modified Version” of the Document means any work containing the
  10254. Document or a portion of it, either copied verbatim, or with
  10255. modifications and/or translated into another language.
  10256. A “Secondary Section” is a named appendix or a front-matter section
  10257. of the Document that deals exclusively with the relationship of the
  10258. publishers or authors of the Document to the Document’s overall
  10259. subject (or to related matters) and contains nothing that could
  10260. fall directly within that overall subject. (Thus, if the Document
  10261. is in part a textbook of mathematics, a Secondary Section may not
  10262. explain any mathematics.) The relationship could be a matter of
  10263. historical connection with the subject or with related matters, or
  10264. of legal, commercial, philosophical, ethical or political position
  10265. regarding them.
  10266. The “Invariant Sections” are certain Secondary Sections whose
  10267. titles are designated, as being those of Invariant Sections, in the
  10268. notice that says that the Document is released under this License.
  10269. If a section does not fit the above definition of Secondary then it
  10270. is not allowed to be designated as Invariant. The Document may
  10271. contain zero Invariant Sections. If the Document does not identify
  10272. any Invariant Sections then there are none.
  10273. The “Cover Texts” are certain short passages of text that are
  10274. listed, as Front-Cover Texts or Back-Cover Texts, in the notice
  10275. that says that the Document is released under this License. A
  10276. Front-Cover Text may be at most 5 words, and a Back-Cover Text may
  10277. be at most 25 words.
  10278. A “Transparent” copy of the Document means a machine-readable copy,
  10279. represented in a format whose specification is available to the
  10280. general public, that is suitable for revising the document
  10281. straightforwardly with generic text editors or (for images composed
  10282. of pixels) generic paint programs or (for drawings) some widely
  10283. available drawing editor, and that is suitable for input to text
  10284. formatters or for automatic translation to a variety of formats
  10285. suitable for input to text formatters. A copy made in an otherwise
  10286. Transparent file format whose markup, or absence of markup, has
  10287. been arranged to thwart or discourage subsequent modification by
  10288. readers is not Transparent. An image format is not Transparent if
  10289. used for any substantial amount of text. A copy that is not
  10290. “Transparent” is called “Opaque”.
  10291. Examples of suitable formats for Transparent copies include plain
  10292. ASCII without markup, Texinfo input format, LaTeX input format,
  10293. SGML or XML using a publicly available DTD, and standard-conforming
  10294. simple HTML, PostScript or PDF designed for human modification.
  10295. Examples of transparent image formats include PNG, XCF and JPG.
  10296. Opaque formats include proprietary formats that can be read and
  10297. edited only by proprietary word processors, SGML or XML for which
  10298. the DTD and/or processing tools are not generally available, and
  10299. the machine-generated HTML, PostScript or PDF produced by some word
  10300. processors for output purposes only.
  10301. The “Title Page” means, for a printed book, the title page itself,
  10302. plus such following pages as are needed to hold, legibly, the
  10303. material this License requires to appear in the title page. For
  10304. works in formats which do not have any title page as such, “Title
  10305. Page” means the text near the most prominent appearance of the
  10306. work’s title, preceding the beginning of the body of the text.
  10307. The “publisher” means any person or entity that distributes copies
  10308. of the Document to the public.
  10309. A section “Entitled XYZ” means a named subunit of the Document
  10310. whose title either is precisely XYZ or contains XYZ in parentheses
  10311. following text that translates XYZ in another language. (Here XYZ
  10312. stands for a specific section name mentioned below, such as
  10313. “Acknowledgements”, “Dedications”, “Endorsements”, or “History”.)
  10314. To “Preserve the Title” of such a section when you modify the
  10315. Document means that it remains a section “Entitled XYZ” according
  10316. to this definition.
  10317. The Document may include Warranty Disclaimers next to the notice
  10318. which states that this License applies to the Document. These
  10319. Warranty Disclaimers are considered to be included by reference in
  10320. this License, but only as regards disclaiming warranties: any other
  10321. implication that these Warranty Disclaimers may have is void and
  10322. has no effect on the meaning of this License.
  10323. 2. VERBATIM COPYING
  10324. You may copy and distribute the Document in any medium, either
  10325. commercially or noncommercially, provided that this License, the
  10326. copyright notices, and the license notice saying this License
  10327. applies to the Document are reproduced in all copies, and that you
  10328. add no other conditions whatsoever to those of this License. You
  10329. may not use technical measures to obstruct or control the reading
  10330. or further copying of the copies you make or distribute. However,
  10331. you may accept compensation in exchange for copies. If you
  10332. distribute a large enough number of copies you must also follow the
  10333. conditions in section 3.
  10334. You may also lend copies, under the same conditions stated above,
  10335. and you may publicly display copies.
  10336. 3. COPYING IN QUANTITY
  10337. If you publish printed copies (or copies in media that commonly
  10338. have printed covers) of the Document, numbering more than 100, and
  10339. the Document’s license notice requires Cover Texts, you must
  10340. enclose the copies in covers that carry, clearly and legibly, all
  10341. these Cover Texts: Front-Cover Texts on the front cover, and
  10342. Back-Cover Texts on the back cover. Both covers must also clearly
  10343. and legibly identify you as the publisher of these copies. The
  10344. front cover must present the full title with all words of the title
  10345. equally prominent and visible. You may add other material on the
  10346. covers in addition. Copying with changes limited to the covers, as
  10347. long as they preserve the title of the Document and satisfy these
  10348. conditions, can be treated as verbatim copying in other respects.
  10349. If the required texts for either cover are too voluminous to fit
  10350. legibly, you should put the first ones listed (as many as fit
  10351. reasonably) on the actual cover, and continue the rest onto
  10352. adjacent pages.
  10353. If you publish or distribute Opaque copies of the Document
  10354. numbering more than 100, you must either include a machine-readable
  10355. Transparent copy along with each Opaque copy, or state in or with
  10356. each Opaque copy a computer-network location from which the general
  10357. network-using public has access to download using public-standard
  10358. network protocols a complete Transparent copy of the Document, free
  10359. of added material. If you use the latter option, you must take
  10360. reasonably prudent steps, when you begin distribution of Opaque
  10361. copies in quantity, to ensure that this Transparent copy will
  10362. remain thus accessible at the stated location until at least one
  10363. year after the last time you distribute an Opaque copy (directly or
  10364. through your agents or retailers) of that edition to the public.
  10365. It is requested, but not required, that you contact the authors of
  10366. the Document well before redistributing any large number of copies,
  10367. to give them a chance to provide you with an updated version of the
  10368. Document.
  10369. 4. MODIFICATIONS
  10370. You may copy and distribute a Modified Version of the Document
  10371. under the conditions of sections 2 and 3 above, provided that you
  10372. release the Modified Version under precisely this License, with the
  10373. Modified Version filling the role of the Document, thus licensing
  10374. distribution and modification of the Modified Version to whoever
  10375. possesses a copy of it. In addition, you must do these things in
  10376. the Modified Version:
  10377. A. Use in the Title Page (and on the covers, if any) a title
  10378. distinct from that of the Document, and from those of previous
  10379. versions (which should, if there were any, be listed in the
  10380. History section of the Document). You may use the same title
  10381. as a previous version if the original publisher of that
  10382. version gives permission.
  10383. B. List on the Title Page, as authors, one or more persons or
  10384. entities responsible for authorship of the modifications in
  10385. the Modified Version, together with at least five of the
  10386. principal authors of the Document (all of its principal
  10387. authors, if it has fewer than five), unless they release you
  10388. from this requirement.
  10389. C. State on the Title page the name of the publisher of the
  10390. Modified Version, as the publisher.
  10391. D. Preserve all the copyright notices of the Document.
  10392. E. Add an appropriate copyright notice for your modifications
  10393. adjacent to the other copyright notices.
  10394. F. Include, immediately after the copyright notices, a license
  10395. notice giving the public permission to use the Modified
  10396. Version under the terms of this License, in the form shown in
  10397. the Addendum below.
  10398. G. Preserve in that license notice the full lists of Invariant
  10399. Sections and required Cover Texts given in the Document’s
  10400. license notice.
  10401. H. Include an unaltered copy of this License.
  10402. I. Preserve the section Entitled “History”, Preserve its Title,
  10403. and add to it an item stating at least the title, year, new
  10404. authors, and publisher of the Modified Version as given on the
  10405. Title Page. If there is no section Entitled “History” in the
  10406. Document, create one stating the title, year, authors, and
  10407. publisher of the Document as given on its Title Page, then add
  10408. an item describing the Modified Version as stated in the
  10409. previous sentence.
  10410. J. Preserve the network location, if any, given in the Document
  10411. for public access to a Transparent copy of the Document, and
  10412. likewise the network locations given in the Document for
  10413. previous versions it was based on. These may be placed in the
  10414. “History” section. You may omit a network location for a work
  10415. that was published at least four years before the Document
  10416. itself, or if the original publisher of the version it refers
  10417. to gives permission.
  10418. K. For any section Entitled “Acknowledgements” or “Dedications”,
  10419. Preserve the Title of the section, and preserve in the section
  10420. all the substance and tone of each of the contributor
  10421. acknowledgements and/or dedications given therein.
  10422. L. Preserve all the Invariant Sections of the Document, unaltered
  10423. in their text and in their titles. Section numbers or the
  10424. equivalent are not considered part of the section titles.
  10425. M. Delete any section Entitled “Endorsements”. Such a section
  10426. may not be included in the Modified Version.
  10427. N. Do not retitle any existing section to be Entitled
  10428. “Endorsements” or to conflict in title with any Invariant
  10429. Section.
  10430. O. Preserve any Warranty Disclaimers.
  10431. If the Modified Version includes new front-matter sections or
  10432. appendices that qualify as Secondary Sections and contain no
  10433. material copied from the Document, you may at your option designate
  10434. some or all of these sections as invariant. To do this, add their
  10435. titles to the list of Invariant Sections in the Modified Version’s
  10436. license notice. These titles must be distinct from any other
  10437. section titles.
  10438. You may add a section Entitled “Endorsements”, provided it contains
  10439. nothing but endorsements of your Modified Version by various
  10440. parties—for example, statements of peer review or that the text has
  10441. been approved by an organization as the authoritative definition of
  10442. a standard.
  10443. You may add a passage of up to five words as a Front-Cover Text,
  10444. and a passage of up to 25 words as a Back-Cover Text, to the end of
  10445. the list of Cover Texts in the Modified Version. Only one passage
  10446. of Front-Cover Text and one of Back-Cover Text may be added by (or
  10447. through arrangements made by) any one entity. If the Document
  10448. already includes a cover text for the same cover, previously added
  10449. by you or by arrangement made by the same entity you are acting on
  10450. behalf of, you may not add another; but you may replace the old
  10451. one, on explicit permission from the previous publisher that added
  10452. the old one.
  10453. The author(s) and publisher(s) of the Document do not by this
  10454. License give permission to use their names for publicity for or to
  10455. assert or imply endorsement of any Modified Version.
  10456. 5. COMBINING DOCUMENTS
  10457. You may combine the Document with other documents released under
  10458. this License, under the terms defined in section 4 above for
  10459. modified versions, provided that you include in the combination all
  10460. of the Invariant Sections of all of the original documents,
  10461. unmodified, and list them all as Invariant Sections of your
  10462. combined work in its license notice, and that you preserve all
  10463. their Warranty Disclaimers.
  10464. The combined work need only contain one copy of this License, and
  10465. multiple identical Invariant Sections may be replaced with a single
  10466. copy. If there are multiple Invariant Sections with the same name
  10467. but different contents, make the title of each such section unique
  10468. by adding at the end of it, in parentheses, the name of the
  10469. original author or publisher of that section if known, or else a
  10470. unique number. Make the same adjustment to the section titles in
  10471. the list of Invariant Sections in the license notice of the
  10472. combined work.
  10473. In the combination, you must combine any sections Entitled
  10474. “History” in the various original documents, forming one section
  10475. Entitled “History”; likewise combine any sections Entitled
  10476. “Acknowledgements”, and any sections Entitled “Dedications”. You
  10477. must delete all sections Entitled “Endorsements.”
  10478. 6. COLLECTIONS OF DOCUMENTS
  10479. You may make a collection consisting of the Document and other
  10480. documents released under this License, and replace the individual
  10481. copies of this License in the various documents with a single copy
  10482. that is included in the collection, provided that you follow the
  10483. rules of this License for verbatim copying of each of the documents
  10484. in all other respects.
  10485. You may extract a single document from such a collection, and
  10486. distribute it individually under this License, provided you insert
  10487. a copy of this License into the extracted document, and follow this
  10488. License in all other respects regarding verbatim copying of that
  10489. document.
  10490. 7. AGGREGATION WITH INDEPENDENT WORKS
  10491. A compilation of the Document or its derivatives with other
  10492. separate and independent documents or works, in or on a volume of a
  10493. storage or distribution medium, is called an “aggregate” if the
  10494. copyright resulting from the compilation is not used to limit the
  10495. legal rights of the compilation’s users beyond what the individual
  10496. works permit. When the Document is included in an aggregate, this
  10497. License does not apply to the other works in the aggregate which
  10498. are not themselves derivative works of the Document.
  10499. If the Cover Text requirement of section 3 is applicable to these
  10500. copies of the Document, then if the Document is less than one half
  10501. of the entire aggregate, the Document’s Cover Texts may be placed
  10502. on covers that bracket the Document within the aggregate, or the
  10503. electronic equivalent of covers if the Document is in electronic
  10504. form. Otherwise they must appear on printed covers that bracket
  10505. the whole aggregate.
  10506. 8. TRANSLATION
  10507. Translation is considered a kind of modification, so you may
  10508. distribute translations of the Document under the terms of section
  10509. 4. Replacing Invariant Sections with translations requires special
  10510. permission from their copyright holders, but you may include
  10511. translations of some or all Invariant Sections in addition to the
  10512. original versions of these Invariant Sections. You may include a
  10513. translation of this License, and all the license notices in the
  10514. Document, and any Warranty Disclaimers, provided that you also
  10515. include the original English version of this License and the
  10516. original versions of those notices and disclaimers. In case of a
  10517. disagreement between the translation and the original version of
  10518. this License or a notice or disclaimer, the original version will
  10519. prevail.
  10520. If a section in the Document is Entitled “Acknowledgements”,
  10521. “Dedications”, or “History”, the requirement (section 4) to
  10522. Preserve its Title (section 1) will typically require changing the
  10523. actual title.
  10524. 9. TERMINATION
  10525. You may not copy, modify, sublicense, or distribute the Document
  10526. except as expressly provided under this License. Any attempt
  10527. otherwise to copy, modify, sublicense, or distribute it is void,
  10528. and will automatically terminate your rights under this License.
  10529. However, if you cease all violation of this License, then your
  10530. license from a particular copyright holder is reinstated (a)
  10531. provisionally, unless and until the copyright holder explicitly and
  10532. finally terminates your license, and (b) permanently, if the
  10533. copyright holder fails to notify you of the violation by some
  10534. reasonable means prior to 60 days after the cessation.
  10535. Moreover, your license from a particular copyright holder is
  10536. reinstated permanently if the copyright holder notifies you of the
  10537. violation by some reasonable means, this is the first time you have
  10538. received notice of violation of this License (for any work) from
  10539. that copyright holder, and you cure the violation prior to 30 days
  10540. after your receipt of the notice.
  10541. Termination of your rights under this section does not terminate
  10542. the licenses of parties who have received copies or rights from you
  10543. under this License. If your rights have been terminated and not
  10544. permanently reinstated, receipt of a copy of some or all of the
  10545. same material does not give you any rights to use it.
  10546. 10. FUTURE REVISIONS OF THIS LICENSE
  10547. The Free Software Foundation may publish new, revised versions of
  10548. the GNU Free Documentation License from time to time. Such new
  10549. versions will be similar in spirit to the present version, but may
  10550. differ in detail to address new problems or concerns. See
  10551. >http://www.gnu.org/copyleft/<.
  10552. Each version of the License is given a distinguishing version
  10553. number. If the Document specifies that a particular numbered
  10554. version of this License “or any later version” applies to it, you
  10555. have the option of following the terms and conditions either of
  10556. that specified version or of any later version that has been
  10557. published (not as a draft) by the Free Software Foundation. If the
  10558. Document does not specify a version number of this License, you may
  10559. choose any version ever published (not as a draft) by the Free
  10560. Software Foundation. If the Document specifies that a proxy can
  10561. decide which future versions of this License can be used, that
  10562. proxy’s public statement of acceptance of a version permanently
  10563. authorizes you to choose that version for the Document.
  10564. 11. RELICENSING
  10565. “Massive Multiauthor Collaboration Site” (or “MMC Site”) means any
  10566. World Wide Web server that publishes copyrightable works and also
  10567. provides prominent facilities for anybody to edit those works. A
  10568. public wiki that anybody can edit is an example of such a server.
  10569. A “Massive Multiauthor Collaboration” (or “MMC”) contained in the
  10570. site means any set of copyrightable works thus published on the MMC
  10571. site.
  10572. “CC-BY-SA” means the Creative Commons Attribution-Share Alike 3.0
  10573. license published by Creative Commons Corporation, a not-for-profit
  10574. corporation with a principal place of business in San Francisco,
  10575. California, as well as future copyleft versions of that license
  10576. published by that same organization.
  10577. “Incorporate” means to publish or republish a Document, in whole or
  10578. in part, as part of another Document.
  10579. An MMC is “eligible for relicensing” if it is licensed under this
  10580. License, and if all works that were first published under this
  10581. License somewhere other than this MMC, and subsequently
  10582. incorporated in whole or in part into the MMC, (1) had no cover
  10583. texts or invariant sections, and (2) were thus incorporated prior
  10584. to November 1, 2008.
  10585. The operator of an MMC Site may republish an MMC contained in the
  10586. site under CC-BY-SA on the same site at any time before August 1,
  10587. 2009, provided the MMC is eligible for relicensing.
  10588. ADDENDUM: How to use this License for your documents
  10589. ====================================================
  10590. To use this License in a document you have written, include a copy of
  10591. the License in the document and put the following copyright and license
  10592. notices just after the title page:
  10593. Copyright (C) YEAR YOUR NAME.
  10594. Permission is granted to copy, distribute and/or modify this document
  10595. under the terms of the GNU Free Documentation License, Version 1.3
  10596. or any later version published by the Free Software Foundation;
  10597. with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
  10598. Texts. A copy of the license is included in the section entitled ``GNU
  10599. Free Documentation License''.
  10600. If you have Invariant Sections, Front-Cover Texts and Back-Cover
  10601. Texts, replace the “with...Texts.” line with this:
  10602. with the Invariant Sections being LIST THEIR TITLES, with
  10603. the Front-Cover Texts being LIST, and with the Back-Cover Texts
  10604. being LIST.
  10605. If you have Invariant Sections without Cover Texts, or some other
  10606. combination of the three, merge those two alternatives to suit the
  10607. situation.
  10608. If your document contains nontrivial examples of program code, we
  10609. recommend releasing these examples in parallel under your choice of free
  10610. software license, such as the GNU General Public License, to permit
  10611. their use in free software.
  10612. Index of Symbols and Keywords
  10613. *****************************
  10614. * Menu:
  10615. * #define: Object-like Macros.
  10616. (line 9940)
  10617. * #elif: elif. (line 11278)
  10618. * #else: else. (line 11260)
  10619. * #endif: ifdef. (line 11091)
  10620. * #error: Diagnostics. (line 11345)
  10621. * #if: Conditional Syntax.
  10622. (line 11085)
  10623. * #ifdef: ifdef. (line 11091)
  10624. * #ifndef: ifdef. (line 11126)
  10625. * #include: include Syntax. (line 9652)
  10626. * #line: Line Control. (line 11390)
  10627. * #undef: Undefining and Redefining Macros.
  10628. (line 10598)
  10629. * #warning: Diagnostics. (line 11356)
  10630. * _Alignas: Type Alignment. (line 13082)
  10631. * _Alignof: Type Alignment. (line 13034)
  10632. * _Complex: Complex Data Types.
  10633. (line 3099)
  10634. * _Static_assert: Static Assertions. (line 12983)
  10635. * __aligned__: Type Alignment. (line 13082)
  10636. * __alignof__: Type Alignment. (line 13034)
  10637. * __attribute__: Attributes. (line 13298)
  10638. * __auto_type: Auto Type. (line 7296)
  10639. * __complex__: Complex Data Types.
  10640. (line 3099)
  10641. * __label__: Local Labels. (line 6808)
  10642. * auto: auto and register. (line 7512)
  10643. * bool: Boolean Type. (line 3002)
  10644. * break: break Statement. (line 6241)
  10645. * case: switch Statement. (line 6477)
  10646. * char: Basic Integers. (line 2876)
  10647. * CHAR_MAX: Maximum and Minimum Values.
  10648. (line 11518)
  10649. * const: const. (line 7553)
  10650. * continue: continue Statement.
  10651. (line 6429)
  10652. * DBL_DECIMAL_DIG: Floating Type Specs.
  10653. (line 11590)
  10654. * DBL_HAS_SUBNORM: Floating Type Specs.
  10655. (line 11590)
  10656. * DBL_MAX: Floating Type Specs.
  10657. (line 11590)
  10658. * DBL_MIN: Floating Type Specs.
  10659. (line 11590)
  10660. * DBL_TRUE_MIN: Floating Type Specs.
  10661. (line 11590)
  10662. * default: switch Statement. (line 6477)
  10663. * do: do-while Statement.
  10664. (line 6223)
  10665. * double: Floating-Point Data Types.
  10666. (line 3050)
  10667. * else: if-else Statement. (line 6080)
  10668. * extern: Extern Declarations.
  10669. (line 7431)
  10670. * float: Floating-Point Data Types.
  10671. (line 3050)
  10672. * FLT_DECIMAL_DIG: Floating Type Specs.
  10673. (line 11590)
  10674. * FLT_HAS_SUBNORM: Floating Type Specs.
  10675. (line 11590)
  10676. * FLT_MAX: Floating Type Specs.
  10677. (line 11590)
  10678. * FLT_MIN: Floating Type Specs.
  10679. (line 11590)
  10680. * FLT_TRUE_MIN: Floating Type Specs.
  10681. (line 11590)
  10682. * for: for Statement. (line 6293)
  10683. * goto: goto Statement. (line 6686)
  10684. * if: if Statement. (line 6061)
  10685. * inline: Inline Function Definitions.
  10686. (line 8875)
  10687. * int: Basic Integers. (line 2876)
  10688. * INT_MAX: Maximum and Minimum Values.
  10689. (line 11518)
  10690. * LDBL_DECIMAL_DIG: Floating Type Specs.
  10691. (line 11590)
  10692. * LDBL_HAS_SUBNORM: Floating Type Specs.
  10693. (line 11590)
  10694. * LDBL_MAX: Floating Type Specs.
  10695. (line 11590)
  10696. * LDBL_MIN: Floating Type Specs.
  10697. (line 11590)
  10698. * LDBL_TRUE_MIN: Floating Type Specs.
  10699. (line 11590)
  10700. * LLONG_MAX: Maximum and Minimum Values.
  10701. (line 11518)
  10702. * long double: Floating-Point Data Types.
  10703. (line 3050)
  10704. * long int: Basic Integers. (line 2876)
  10705. * long long int: Basic Integers. (line 2876)
  10706. * LONG_MAX: Maximum and Minimum Values.
  10707. (line 11518)
  10708. * main: The main Function. (line 8439)
  10709. * register: auto and register. (line 7512)
  10710. * restrict: restrict Pointers. (line 7669)
  10711. * return: return Statement. (line 6163)
  10712. * SCHAR_MAX: Maximum and Minimum Values.
  10713. (line 11518)
  10714. * short int: Basic Integers. (line 2876)
  10715. * SHRT_MAX: Maximum and Minimum Values.
  10716. (line 11518)
  10717. * signed: Signed and Unsigned Types.
  10718. (line 2918)
  10719. * sizeof: Type Size. (line 3726)
  10720. * static: Static Local Variables.
  10721. (line 7403)
  10722. * static >1<: Static Functions. (line 7909)
  10723. * struct: Structures. (line 4461)
  10724. * switch: switch Statement. (line 6477)
  10725. * typedef: Defining Typedef Names.
  10726. (line 5942)
  10727. * typeof: Auto Type. (line 7296)
  10728. * UCHAR_MAX: Maximum and Minimum Values.
  10729. (line 11518)
  10730. * UINT_MAX: Maximum and Minimum Values.
  10731. (line 11518)
  10732. * ULLONG_MAX: Maximum and Minimum Values.
  10733. (line 11518)
  10734. * ULONG_MAX: Maximum and Minimum Values.
  10735. (line 11518)
  10736. * union: Unions. (line 5022)
  10737. * unsigned: Signed and Unsigned Types.
  10738. (line 2918)
  10739. * USHRT_MAX: Maximum and Minimum Values.
  10740. (line 11518)
  10741. * void: The Void Type. (line 3154)
  10742. * volatile: volatile. (line 7617)
  10743. * while: while Statement. (line 6196)
  10744. Concept Index
  10745. *************
  10746. * Menu:
  10747. * # operator: Stringification. (line 10178)
  10748. * ## operator: Concatenation. (line 10257)
  10749. * ?: side effect: Conditional Branches.
  10750. (line 2474)
  10751. * \a: Character Constants.
  10752. (line 3468)
  10753. * \b: Character Constants.
  10754. (line 3468)
  10755. * \e: Character Constants.
  10756. (line 3468)
  10757. * \f: Character Constants.
  10758. (line 3468)
  10759. * \n: Character Constants.
  10760. (line 3468)
  10761. * \r: Character Constants.
  10762. (line 3468)
  10763. * \t: Character Constants.
  10764. (line 3468)
  10765. * \v: Character Constants.
  10766. (line 3468)
  10767. * _ in variables in macros: Macros and Auto Type.
  10768. (line 10879)
  10769. * _Complex keyword: Complex Data Types.
  10770. (line 3099)
  10771. * _Complex_I: Imaginary Constants.
  10772. (line 3412)
  10773. * __attribute__((packed)): Packed Structures. (line 4738)
  10774. * __complex__ keyword: Complex Data Types.
  10775. (line 3099)
  10776. * accessing array elements: Accessing Array Elements.
  10777. (line 5419)
  10778. * addition operator: Basic Arithmetic. (line 1514)
  10779. * address of a label: Labels as Values. (line 6878)
  10780. * address-of operator: Address of Data. (line 3811)
  10781. * aliasing (of storage): Aliasing. (line 13108)
  10782. * alignment of type: Type Alignment. (line 13034)
  10783. * allocating memory dynamically: Dynamic Memory Allocation.
  10784. (line 4581)
  10785. * allocation file-scope variables: Allocating File-Scope.
  10786. (line 7479)
  10787. * argument promotions: Argument Promotions.
  10788. (line 9248)
  10789. * arguments: Macro Arguments. (line 10082)
  10790. * arguments in macro definitions: Macro Arguments. (line 10082)
  10791. * arithmetic operators: Arithmetic. (line 1484)
  10792. * arithmetic, pointer: Pointer Arithmetic.
  10793. (line 4104)
  10794. * array: Arrays. (line 5400)
  10795. * array as parameters: Arrays as Parameters.
  10796. (line 7939)
  10797. * array elements, accessing: Accessing Array Elements.
  10798. (line 5419)
  10799. * array example: Array Example. (line 1033)
  10800. * array fields, flexible: Flexible Array Fields.
  10801. (line 4915)
  10802. * array of length zero: Zero Length. (line 4893)
  10803. * array of variable length: Arrays of Variable Length.
  10804. (line 5793)
  10805. * array parameters, variable-length: Variable-Length Array Parameters.
  10806. (line 8585)
  10807. * array types, incomplete: Incomplete Array Types.
  10808. (line 5593)
  10809. * array values, constructing: Constructing Array Values.
  10810. (line 5758)
  10811. * array, declaring: Declaring an Array.
  10812. (line 5458)
  10813. * array, declaring >1<: Declaring Arrays and Pointers.
  10814. (line 7071)
  10815. * array, layout in memory: Multidimensional Arrays.
  10816. (line 5727)
  10817. * array, multidimensional: Multidimensional Arrays.
  10818. (line 5675)
  10819. * arrays and pointers: Pointers and Arrays.
  10820. (line 4211)
  10821. * assigning function pointers: Assigning Function Pointers.
  10822. (line 8390)
  10823. * assigning structures: Structure Assignment.
  10824. (line 4987)
  10825. * assignment expressions: Assignment Expressions.
  10826. (line 1976)
  10827. * assignment in subexpressions: Assignment in Subexpressions.
  10828. (line 2231)
  10829. * assignment type conversions: Assignment Type Conversions.
  10830. (line 9192)
  10831. * assignment, modifying: Modifying Assignment.
  10832. (line 2093)
  10833. * assignment, simple: Simple Assignment. (line 2020)
  10834. * associativity and ordering: Associativity and Ordering.
  10835. (line 2704)
  10836. * attributes: Attributes. (line 13298)
  10837. * auto declarations: auto and register. (line 7512)
  10838. * backspace: Character Constants.
  10839. (line 3468)
  10840. * base conversion (floating point): Round-Trip Base Conversion.
  10841. (line 12489)
  10842. * bell character: Character Constants.
  10843. (line 3468)
  10844. * binary integer constants: Integer Constants. (line 3278)
  10845. * binary operator grammar: Binary Operator Grammar.
  10846. (line 2589)
  10847. * bit fields: Bit Fields. (line 4765)
  10848. * bitwise operators: Bitwise Operations.
  10849. (line 1906)
  10850. * block: Blocks. (line 6102)
  10851. * block scope: Scope. (line 9322)
  10852. * boolean type: Boolean Type. (line 3002)
  10853. * branch cuts: Complex Arithmetic.
  10854. (line 12452)
  10855. * branches of conditional expression: Conditional Branches.
  10856. (line 2448)
  10857. * break statement: break Statement. (line 6241)
  10858. * bytes: Storage. (line 957)
  10859. * call-by-value: Function Call Semantics.
  10860. (line 8268)
  10861. * calling function pointers: Calling Function Pointers.
  10862. (line 8418)
  10863. * calling functions: Function Calls. (line 8236)
  10864. * carriage return in source: Whitespace. (line 1260)
  10865. * case labels in initializers: Designated Inits. (line 7214)
  10866. * case of letters in identifiers: Identifiers. (line 1372)
  10867. * case ranges: Case Ranges. (line 6647)
  10868. * cast: Explicit Type Conversion.
  10869. (line 9146)
  10870. * cast to a union: Cast to Union. (line 5129)
  10871. * character constants: Character Constants.
  10872. (line 3452)
  10873. * character set: Characters. (line 1232)
  10874. * CHAR_BIT: Integer Representations.
  10875. (line 11437)
  10876. * cloning: Label Value Caveats.
  10877. (line 6947)
  10878. * combining variable declarations: Combining Variable Declarations.
  10879. (line 7114)
  10880. * comma operator: Comma Operator. (line 2492)
  10881. * command-line parameters: Command-line Parameters.
  10882. (line 8498)
  10883. * commenting out code: Deleted Code. (line 11320)
  10884. * comments: Comments. (line 1292)
  10885. * common type: Common Type. (line 9294)
  10886. * comparison, pointer: Pointer Comparison.
  10887. (line 4064)
  10888. * comparisons: Numeric Comparisons.
  10889. (line 1776)
  10890. * compatible types: Compatible Types. (line 9080)
  10891. * compilation module: Compilation. (line 12681)
  10892. * compiler options for integer overflow: Signed Overflow. (line 1638)
  10893. * compiling: Compile Example. (line 925)
  10894. * complete example program: Complete Program. (line 787)
  10895. * complex arithmetic in floating-point calculations: Complex Arithmetic.
  10896. (line 12415)
  10897. * complex conjugation: Complex Data Types.
  10898. (line 3136)
  10899. * complex constants: Imaginary Constants.
  10900. (line 3400)
  10901. * complex numbers: Complex Data Types.
  10902. (line 3099)
  10903. * compound statement: Blocks. (line 6102)
  10904. * computed gotos: Labels as Values. (line 6878)
  10905. * computed includes: Computed Includes. (line 9841)
  10906. * concatenation: Concatenation. (line 10257)
  10907. * conditional expression: Conditional Expression.
  10908. (line 2415)
  10909. * conditional group: ifdef. (line 11099)
  10910. * conditionals: Conditionals. (line 11028)
  10911. * conjunction operator: Logical Operators. (line 2307)
  10912. * conjunction, bitwise: Bitwise Operations.
  10913. (line 1906)
  10914. * const fields: const Fields. (line 4859)
  10915. * const variables and fields: const. (line 7553)
  10916. * constant data types, integer: Integer Const Type.
  10917. (line 3291)
  10918. * constants: Constants. (line 3237)
  10919. * constants, character: Character Constants.
  10920. (line 3452)
  10921. * constants, floating-point: Floating Constants.
  10922. (line 3332)
  10923. * constants, imaginary: Imaginary Constants.
  10924. (line 3400)
  10925. * constants, integer: Integer Constants. (line 3245)
  10926. * constants, string: String Constants. (line 3500)
  10927. * constants, wide character: Wide Character Constants.
  10928. (line 3640)
  10929. * constants, wide string: Wide String Constants.
  10930. (line 3675)
  10931. * constructing array values: Constructing Array Values.
  10932. (line 5758)
  10933. * constructors, structure: Structure Constructors.
  10934. (line 5164)
  10935. * continuation of lines: Line Continuation. (line 1449)
  10936. * continue statement: continue Statement.
  10937. (line 6429)
  10938. * controlling macro: Once-Only Headers. (line 9830)
  10939. * conversion between pointers and integers: Pointer-Integer Conversion.
  10940. (line 4413)
  10941. * conversions, type: Type Conversions. (line 9140)
  10942. * counting vowels and punctuation: switch Example. (line 6547)
  10943. * crash: Stack. (line 604)
  10944. * declararing functions: Function Declarations.
  10945. (line 8142)
  10946. * declaration of variables: Variable Declarations.
  10947. (line 7038)
  10948. * declarations inside expressions: Statement Exprs. (line 6970)
  10949. * declarations, combining: Combining Variable Declarations.
  10950. (line 7114)
  10951. * declarations, extern: Extern Declarations.
  10952. (line 7431)
  10953. * declaring an array: Declaring an Array.
  10954. (line 5458)
  10955. * declaring arrays and pointers: Declaring Arrays and Pointers.
  10956. (line 7071)
  10957. * declaring function pointers: Declaring Function Pointers.
  10958. (line 8327)
  10959. * decrement operator: Increment/Decrement.
  10960. (line 2149)
  10961. * decrementing pointers: Pointer Increment/Decrement.
  10962. (line 4283)
  10963. * defined: defined. (line 11221)
  10964. * defining functions: Function Definitions.
  10965. (line 7792)
  10966. * dereferencing pointers: Pointer Dereference.
  10967. (line 3897)
  10968. * designated initializers: Designated Inits. (line 7214)
  10969. * diagnostic: Diagnostics. (line 11345)
  10970. * digraphs: Digraphs. (line 13278)
  10971. * directive line: Directives. (line 9484)
  10972. * directive name: Directives. (line 9484)
  10973. * directives: Directives. (line 9484)
  10974. * disjunction operator: Logical Operators. (line 2307)
  10975. * disjunction, bitwise: Bitwise Operations.
  10976. (line 1906)
  10977. * division by zero: Division and Remainder.
  10978. (line 1769)
  10979. * division operator: Basic Arithmetic. (line 1514)
  10980. * do–while statement: do-while Statement.
  10981. (line 6223)
  10982. * downward funargs: Nested Functions. (line 8751)
  10983. * drawbacks of pointer arithmetic: Pointer Arithmetic Drawbacks.
  10984. (line 4381)
  10985. * Duff’s device: Duffs Device. (line 6606)
  10986. * dynamic memory allocation: Dynamic Memory Allocation.
  10987. (line 4581)
  10988. * elements of arrays: Arrays. (line 5400)
  10989. * empty macro arguments: Macro Arguments. (line 10143)
  10990. * enumeration types: Enumeration Types. (line 5863)
  10991. * enumerator: Enumeration Types. (line 5863)
  10992. * environment variables: Environment Variables.
  10993. (line 8536)
  10994. * equal operator: Numeric Comparisons.
  10995. (line 1776)
  10996. * error recovery (floating point): Error Recovery. (line 11985)
  10997. * escape (ASCII character): Character Constants.
  10998. (line 3468)
  10999. * escape sequence: Character Constants.
  11000. (line 3452)
  11001. * exact floating-point arithmetic: Exact Floating-Point.
  11002. (line 11755)
  11003. * exact specification of floating-point constants: Exact Floating Constants.
  11004. (line 12060)
  11005. * example program, complete: Complete Program. (line 787)
  11006. * exception flags (floating point): Exception Flags. (line 11732)
  11007. * executable file: Compile Example. (line 925)
  11008. * execution control expressions: Execution Control Expressions.
  11009. (line 2301)
  11010. * exit status: Values from main. (line 8466)
  11011. * EXIT_FAILURE: Values from main. (line 8477)
  11012. * EXIT_SUCCESS: Values from main. (line 8477)
  11013. * expansion of arguments: Argument Prescan. (line 10945)
  11014. * explicit type conversion: Explicit Type Conversion.
  11015. (line 9146)
  11016. * expression statement: Expression Statement.
  11017. (line 6034)
  11018. * expression, conditional: Conditional Expression.
  11019. (line 2415)
  11020. * expressions containing statements: Statement Exprs. (line 6970)
  11021. * expressions, execution control: Execution Control Expressions.
  11022. (line 2301)
  11023. * extern declarations: Extern Declarations.
  11024. (line 7431)
  11025. * extern inline function: Inline Function Definitions.
  11026. (line 8943)
  11027. * failure: Values from main. (line 8466)
  11028. * Fibonacci function, iterative: Iterative Fibonacci.
  11029. (line 639)
  11030. * Fibonacci function, recursive: Recursive Fibonacci.
  11031. (line 429)
  11032. * field offset: Field Offset. (line 4658)
  11033. * fields in structures: Structures. (line 4461)
  11034. * file-scope variables: File-Scope Variables.
  11035. (line 7370)
  11036. * file-scope variables, allocating: Allocating File-Scope.
  11037. (line 7479)
  11038. * first-class object: Limitations of C Arrays.
  11039. (line 5629)
  11040. * flexible array fields: Flexible Array Fields.
  11041. (line 4915)
  11042. * floating arithmetic exception flags: Exception Flags. (line 11732)
  11043. * floating overflow: Exception Flags. (line 11732)
  11044. * floating point example: Float Example. (line 981)
  11045. * floating underflow: Special Float Values.
  11046. (line 11665)
  11047. * floating underflow >1<: Exception Flags. (line 11732)
  11048. * floating-point arithmetic invalid optimizations: Invalid Optimizations.
  11049. (line 11700)
  11050. * floating-point arithmetic with complex numbers: Complex Arithmetic.
  11051. (line 12415)
  11052. * floating-point arithmetic, exact: Exact Floating-Point.
  11053. (line 11755)
  11054. * floating-point constants: Floating Constants.
  11055. (line 3332)
  11056. * floating-point constants, exact specification of: Exact Floating Constants.
  11057. (line 12060)
  11058. * floating-point error recovery: Error Recovery. (line 11985)
  11059. * floating-point fused multiply-add: Fused Multiply-Add.
  11060. (line 11948)
  11061. * floating-point infinity: Handling Infinity. (line 12099)
  11062. * floating-point machine epsilon: Machine Epsilon. (line 12295)
  11063. * floating-point NaN: Handling NaN. (line 12138)
  11064. * floating-point representations: Floating Representations.
  11065. (line 11552)
  11066. * floating-point round-trip base conversion: Round-Trip Base Conversion.
  11067. (line 12489)
  11068. * floating-point rounding control: Rounding Control. (line 12248)
  11069. * floating-point rounding issues: Rounding Issues. (line 11828)
  11070. * floating-point scaling by powers of the base: Scaling by the Base.
  11071. (line 12220)
  11072. * floating-point signed zeros: Signed Zeros. (line 12206)
  11073. * floating-point significance loss: Significance Loss. (line 11872)
  11074. * floating-point types: Floating-Point Data Types.
  11075. (line 3050)
  11076. * floating-point values, special: Special Float Values.
  11077. (line 11624)
  11078. * for statement: for Statement. (line 6293)
  11079. * formfeed: Character Constants.
  11080. (line 3468)
  11081. * formfeed in source: Whitespace. (line 1260)
  11082. * forward declaration: Static Functions. (line 7923)
  11083. * forward function declarations: Forward Function Declarations.
  11084. (line 7891)
  11085. * full expression: Sequence Points. (line 2738)
  11086. * function body: Function Body. (line 504)
  11087. * function call semantics: Function Call Semantics.
  11088. (line 8268)
  11089. * function calls: Function Calls. (line 8236)
  11090. * function declarations: Function Declarations.
  11091. (line 8142)
  11092. * function declarations, forward: Forward Function Declarations.
  11093. (line 7891)
  11094. * function definitions: Function Definitions.
  11095. (line 7792)
  11096. * function definitions, inline: Inline Function Definitions.
  11097. (line 8875)
  11098. * function definitions, old-style: Old-Style Function Definitions.
  11099. (line 9014)
  11100. * function header: Function Header. (line 490)
  11101. * function parameter lists, variable length: Variable Number of Arguments.
  11102. (line 8648)
  11103. * function parameter variables: Function Parameter Variables.
  11104. (line 7809)
  11105. * function pointers: Function Pointers. (line 8312)
  11106. * function pointers, assigning: Assigning Function Pointers.
  11107. (line 8390)
  11108. * function pointers, calling: Calling Function Pointers.
  11109. (line 8418)
  11110. * function pointers, declaring: Declaring Function Pointers.
  11111. (line 8327)
  11112. * function prototype: Function Declarations.
  11113. (line 8155)
  11114. * function prototype scope: Scope. (line 9322)
  11115. * function scope: Scope. (line 9322)
  11116. * function-like macros: Function-like Macros.
  11117. (line 10035)
  11118. * functions: Functions. (line 7784)
  11119. * functions that accept variable-length arrays: Variable-Length Array Parameters.
  11120. (line 8585)
  11121. * functions with array parameters: Arrays as Parameters.
  11122. (line 7939)
  11123. * functions, nested: Nested Functions. (line 8751)
  11124. * functions, static: Static Functions. (line 7909)
  11125. * fused multiply-add in floating-point computations: Fused Multiply-Add.
  11126. (line 11948)
  11127. * global variables: File-Scope Variables.
  11128. (line 7370)
  11129. * goto statement: goto Statement. (line 6686)
  11130. * goto with computed label: Labels as Values. (line 6878)
  11131. * grammar, binary operator: Binary Operator Grammar.
  11132. (line 2589)
  11133. * greater-or-equal operator: Numeric Comparisons.
  11134. (line 1776)
  11135. * greater-than operator: Numeric Comparisons.
  11136. (line 1776)
  11137. * guard macro: Once-Only Headers. (line 9830)
  11138. * handler (for signal): Signals. (line 13369)
  11139. * header file: Header Files. (line 9617)
  11140. * hexademical floating-point constants: Exact Floating Constants.
  11141. (line 12070)
  11142. * identifiers: Identifiers. (line 1366)
  11143. * identifiers >1<: Preprocessing Tokens.
  11144. (line 9528)
  11145. * IEEE 754-2008 Standard: Floating Representations.
  11146. (line 11552)
  11147. * if statement: if Statement. (line 6061)
  11148. * if...else statement: if-else Statement. (line 6080)
  11149. * imaginary constants: Imaginary Constants.
  11150. (line 3400)
  11151. * including just once: Once-Only Headers. (line 9801)
  11152. * incomplete array types: Incomplete Array Types.
  11153. (line 5593)
  11154. * incomplete types: Incomplete Types. (line 5244)
  11155. * increment operator: Increment/Decrement.
  11156. (line 2149)
  11157. * incrementing pointers: Pointer Increment/Decrement.
  11158. (line 4283)
  11159. * infinity in floating-point arithmetic: Handling Infinity. (line 12099)
  11160. * initializers: Initializers. (line 7143)
  11161. * initializers with labeled elements: Designated Inits. (line 7214)
  11162. * inline function definitions: Inline Function Definitions.
  11163. (line 8875)
  11164. * inline functions, omission of: Inline Function Definitions.
  11165. (line 8936)
  11166. * integer arithmetic: Integer Arithmetic.
  11167. (line 1542)
  11168. * integer constant data types: Integer Const Type.
  11169. (line 3291)
  11170. * integer constants: Integer Constants. (line 3245)
  11171. * integer overflow: Integer Overflow. (line 1591)
  11172. * integer overflow, compiler options: Signed Overflow. (line 1638)
  11173. * integer ranges: Maximum and Minimum Values.
  11174. (line 11518)
  11175. * integer representations: Integer Representations.
  11176. (line 11431)
  11177. * integer types: Integer Types. (line 2869)
  11178. * internal block: Blocks. (line 6123)
  11179. * intptr_t: Pointer Arithmetic Low Level.
  11180. (line 4275)
  11181. * invalid optimizations in floating-point arithmetic: Invalid Optimizations.
  11182. (line 11700)
  11183. * iteration: Loop Statements. (line 6184)
  11184. * iterative Fibonacci function: Iterative Fibonacci.
  11185. (line 639)
  11186. * K&R-style function definitions: Old-Style Function Definitions.
  11187. (line 9014)
  11188. * keyword: Identifiers. (line 1375)
  11189. * label: goto Statement. (line 6686)
  11190. * labeled elements in initializers: Designated Inits. (line 7214)
  11191. * labels as values: Labels as Values. (line 6878)
  11192. * layout of structures: Structure Layout. (line 4678)
  11193. * left-associative: Binary Operator Grammar.
  11194. (line 2589)
  11195. * length-zero arrays: Zero Length. (line 4893)
  11196. * less-or-equal operator: Numeric Comparisons.
  11197. (line 1776)
  11198. * less-than operator: Numeric Comparisons.
  11199. (line 1776)
  11200. * lexical syntax: Lexical Syntax. (line 1191)
  11201. * limitations of C arrays: Limitations of C Arrays.
  11202. (line 5629)
  11203. * line continuation: Line Continuation. (line 1449)
  11204. * line control: Line Control. (line 11372)
  11205. * linefeed in source: Whitespace. (line 1260)
  11206. * linking object files: Compilation. (line 12715)
  11207. * local labels: Local Labels. (line 6808)
  11208. * local variables: Local Variables. (line 7325)
  11209. * local variables in macros: Macros and Auto Type.
  11210. (line 10861)
  11211. * logical operators: Logical Operators. (line 2307)
  11212. * loop statements: Loop Statements. (line 6184)
  11213. * low level pointer arithmetic: Pointer Arithmetic Low Level.
  11214. (line 4243)
  11215. * lvalues: Lvalues. (line 2055)
  11216. * machine epsilon (floating point): Machine Epsilon. (line 12295)
  11217. * macro argument expansion: Argument Prescan. (line 10945)
  11218. * macro arguments and directives: Directives Within Macro Arguments.
  11219. (line 10642)
  11220. * macros: Macros. (line 9912)
  11221. * macros in include: Computed Includes. (line 9841)
  11222. * macros with arguments: Macro Arguments. (line 10082)
  11223. * macros with variable arguments: Variadic Macros. (line 10335)
  11224. * macros, local labels: Local Labels. (line 6808)
  11225. * macros, local variables in: Macros and Auto Type.
  11226. (line 10861)
  11227. * macros, types of arguments: Auto Type. (line 7296)
  11228. * main function: The main Function. (line 8439)
  11229. * make rules: Compilation. (line 12681)
  11230. * manifest constants: Object-like Macros.
  11231. (line 9934)
  11232. * maximum integer values: Maximum and Minimum Values.
  11233. (line 11518)
  11234. * memory allocation, dynamic: Dynamic Memory Allocation.
  11235. (line 4581)
  11236. * memory organization: Storage. (line 957)
  11237. * minimum integer values: Maximum and Minimum Values.
  11238. (line 11518)
  11239. * modifying assignment: Modifying Assignment.
  11240. (line 2093)
  11241. * modulus: Division and Remainder.
  11242. (line 1712)
  11243. * multidimensional arrays: Multidimensional Arrays.
  11244. (line 5675)
  11245. * multiplication operator: Basic Arithmetic. (line 1514)
  11246. * NaN in floating-point arithmetic: Handling NaN. (line 12138)
  11247. * NaNs-always-propagate rule: Handling NaN. (line 12171)
  11248. * negation operator: Basic Arithmetic. (line 1514)
  11249. * negation operator, logical: Logical Operators. (line 2307)
  11250. * negation, bitwise: Bitwise Operations.
  11251. (line 1906)
  11252. * nested block: Blocks. (line 6123)
  11253. * nested functions: Nested Functions. (line 8751)
  11254. * newline: Character Constants.
  11255. (line 3468)
  11256. * newline in source: Whitespace. (line 1260)
  11257. * not a number: Handling NaN. (line 12138)
  11258. * not-equal operator: Numeric Comparisons.
  11259. (line 1776)
  11260. * null directive: Null Directive. (line 11417)
  11261. * null pointers: Null Pointers. (line 3931)
  11262. * null statement: Null Statement. (line 6672)
  11263. * numbers, preprocessing: Preprocessing Tokens.
  11264. (line 9540)
  11265. * numeric comparisons: Numeric Comparisons.
  11266. (line 1776)
  11267. * object file: Compilation. (line 12681)
  11268. * object-like macro: Object-like Macros.
  11269. (line 9934)
  11270. * offset of structure fields: Field Offset. (line 4658)
  11271. * old-style function definitions: Old-Style Function Definitions.
  11272. (line 9014)
  11273. * omitting types in declarations: Omitting Types. (line 7528)
  11274. * operand execution ordering: Reordering of Operands.
  11275. (line 2680)
  11276. * operand ordering: Ordering of Operands.
  11277. (line 2802)
  11278. * operand promotions: Operand Promotions.
  11279. (line 9280)
  11280. * operator precedence: Binary Operator Grammar.
  11281. (line 2589)
  11282. * operator, addition: Basic Arithmetic. (line 1514)
  11283. * operator, comma: Comma Operator. (line 2492)
  11284. * operator, decrement: Increment/Decrement.
  11285. (line 2149)
  11286. * operator, division: Basic Arithmetic. (line 1514)
  11287. * operator, equal: Numeric Comparisons.
  11288. (line 1776)
  11289. * operator, greater-or-equal: Numeric Comparisons.
  11290. (line 1776)
  11291. * operator, greater-than: Numeric Comparisons.
  11292. (line 1776)
  11293. * operator, increment: Increment/Decrement.
  11294. (line 2149)
  11295. * operator, less-or-equal: Numeric Comparisons.
  11296. (line 1776)
  11297. * operator, less-than: Numeric Comparisons.
  11298. (line 1776)
  11299. * operator, multiplication: Basic Arithmetic. (line 1514)
  11300. * operator, negation: Basic Arithmetic. (line 1514)
  11301. * operator, not-equal: Numeric Comparisons.
  11302. (line 1776)
  11303. * operator, postdecrement: Postincrement/Postdecrement.
  11304. (line 2192)
  11305. * operator, postincrement: Postincrement/Postdecrement.
  11306. (line 2192)
  11307. * operator, remainder: Division and Remainder.
  11308. (line 1712)
  11309. * operator, subtraction: Basic Arithmetic. (line 1514)
  11310. * operators: Operators/Punctuation.
  11311. (line 1395)
  11312. * operators, arithmetic: Arithmetic. (line 1484)
  11313. * operators, assignment: Assignment Expressions.
  11314. (line 1976)
  11315. * operators, bitwise: Bitwise Operations.
  11316. (line 1906)
  11317. * operators, comparison: Numeric Comparisons.
  11318. (line 1776)
  11319. * operators, logical: Logical Operators. (line 2307)
  11320. * operators, shift: Shift Operations. (line 1804)
  11321. * operators, shift >1<: Shift Operations. (line 1804)
  11322. * optimization and ordering: Optimization and Ordering.
  11323. (line 2835)
  11324. * order of execution: Order of Execution.
  11325. (line 2674)
  11326. * ordering and optimization: Optimization and Ordering.
  11327. (line 2835)
  11328. * ordering and postincrement: Postincrement and Ordering.
  11329. (line 2776)
  11330. * ordering of operands: Reordering of Operands.
  11331. (line 2680)
  11332. * ordering of operands >1<: Ordering of Operands.
  11333. (line 2802)
  11334. * overflow, compiler options: Signed Overflow. (line 1638)
  11335. * overflow, floating: Exception Flags. (line 11732)
  11336. * overflow, integer: Integer Overflow. (line 1591)
  11337. * overlaying structures: Overlaying Structures.
  11338. (line 4957)
  11339. * packed structures: Packed Structures. (line 4738)
  11340. * parameter forward declaration: Variable-Length Array Parameters.
  11341. (line 8624)
  11342. * parameter list: Function Parameter Variables.
  11343. (line 7809)
  11344. * parameter variables in functions: Function Parameter Variables.
  11345. (line 7809)
  11346. * parameters lists, variable length: Variable Number of Arguments.
  11347. (line 8648)
  11348. * parameters, command-line: Command-line Parameters.
  11349. (line 8498)
  11350. * parentheses in macro bodies: Operator Precedence Problems.
  11351. (line 10704)
  11352. * pitfalls of macros: Macro Pitfalls. (line 10668)
  11353. * pointer arithmetic: Pointer Arithmetic.
  11354. (line 4104)
  11355. * pointer arithmetic, drawbacks: Pointer Arithmetic Drawbacks.
  11356. (line 4381)
  11357. * pointer arithmetic, low level: Pointer Arithmetic Low Level.
  11358. (line 4243)
  11359. * pointer comparison: Pointer Comparison.
  11360. (line 4064)
  11361. * pointer dereferencing: Pointer Dereference.
  11362. (line 3897)
  11363. * pointer increment and decrement: Pointer Increment/Decrement.
  11364. (line 4283)
  11365. * pointer type conversion: Aliasing. (line 13108)
  11366. * pointer-integer conversion: Pointer-Integer Conversion.
  11367. (line 4413)
  11368. * pointers: Pointers. (line 3797)
  11369. * pointers and arrays: Pointers and Arrays.
  11370. (line 4211)
  11371. * pointers to functions: Function Pointers. (line 8312)
  11372. * pointers, declaring: Declaring Arrays and Pointers.
  11373. (line 7071)
  11374. * pointers, null: Null Pointers. (line 3931)
  11375. * pointers, restrict-qualified: restrict Pointers. (line 7669)
  11376. * pointers, void: Void Pointers. (line 4001)
  11377. * postdecrement expression: Postincrement/Postdecrement.
  11378. (line 2192)
  11379. * postincrement and ordering: Postincrement and Ordering.
  11380. (line 2776)
  11381. * postincrement expression: Postincrement/Postdecrement.
  11382. (line 2192)
  11383. * precedence, operator: Binary Operator Grammar.
  11384. (line 2589)
  11385. * predecrement expression: Increment/Decrement.
  11386. (line 2149)
  11387. * predefined macros: Predefined Macros. (line 10428)
  11388. * preincrement expression: Increment/Decrement.
  11389. (line 2149)
  11390. * preprocessing: Preprocessing. (line 9440)
  11391. * preprocessing directives: Directives. (line 9484)
  11392. * preprocessing numbers: Preprocessing Tokens.
  11393. (line 9540)
  11394. * preprocessing tokens: Preprocessing Tokens.
  11395. (line 9519)
  11396. * prescan of macro arguments: Argument Prescan. (line 10945)
  11397. * primitive types: Primitive Types. (line 2860)
  11398. * printf: Complete Explanation.
  11399. (line 854)
  11400. * problems with macros: Macro Pitfalls. (line 10668)
  11401. * promotion of arguments: Argument Promotions.
  11402. (line 9248)
  11403. * prototype of a function: Function Declarations.
  11404. (line 8155)
  11405. * punctuation: Operators/Punctuation.
  11406. (line 1395)
  11407. * QNaN: Special Float Values.
  11408. (line 11638)
  11409. * quote directories: Search Path. (line 9793)
  11410. * ranges in case statements: Case Ranges. (line 6647)
  11411. * ranges of integer types: Maximum and Minimum Values.
  11412. (line 11518)
  11413. * recursion: Function Body. (line 504)
  11414. * recursion, drawbacks of: Stack. (line 594)
  11415. * recursive Fibonacci function: Recursive Fibonacci.
  11416. (line 429)
  11417. * redefining macros: Undefining and Redefining Macros.
  11418. (line 10598)
  11419. * referencing structure fields: Referencing Fields.
  11420. (line 4539)
  11421. * register declarations: auto and register. (line 7512)
  11422. * remainder operator: Division and Remainder.
  11423. (line 1712)
  11424. * reordering of operands: Reordering of Operands.
  11425. (line 2680)
  11426. * repeated inclusion: Once-Only Headers. (line 9801)
  11427. * reporting errors: Diagnostics. (line 11345)
  11428. * reporting warnings: Diagnostics. (line 11345)
  11429. * representation of floating-point numbers: Floating Representations.
  11430. (line 11552)
  11431. * representation of integers: Integer Representations.
  11432. (line 11431)
  11433. * reserved words: Identifiers. (line 1375)
  11434. * restrict pointers: restrict Pointers. (line 7669)
  11435. * return (ASCII character): Character Constants.
  11436. (line 3468)
  11437. * return statement: return Statement. (line 6163)
  11438. * returning values from main: Values from main. (line 8466)
  11439. * round-trip base conversion: Round-Trip Base Conversion.
  11440. (line 12489)
  11441. * rounding: Rounding. (line 11777)
  11442. * rounding control (floating point): Rounding Control. (line 12248)
  11443. * rounding issues (floating point): Rounding Issues. (line 11828)
  11444. * scaling floating point by powers of the base: Scaling by the Base.
  11445. (line 12220)
  11446. * scope: Scope. (line 9322)
  11447. * segmentation fault: Stack. (line 604)
  11448. * self-reference: Self-Referential Macros.
  11449. (line 10888)
  11450. * semantics of function calls: Function Call Semantics.
  11451. (line 8268)
  11452. * semicolons (after macro calls): Swallowing the Semicolon.
  11453. (line 10755)
  11454. * sequence points: Sequence Points. (line 2738)
  11455. * shift count: Shift Operations. (line 1804)
  11456. * shift operators: Shift Operations. (line 1804)
  11457. * side effect in ?:: Conditional Branches.
  11458. (line 2474)
  11459. * side effects (in macro arguments): Duplication of Side Effects.
  11460. (line 10808)
  11461. * SIGBUS: Signals. (line 13369)
  11462. * SIGFPE: Signals. (line 13369)
  11463. * signal: Signals. (line 13369)
  11464. * signed types: Signed and Unsigned Types.
  11465. (line 2918)
  11466. * signed zeros in floating-point arithmetic: Signed Zeros. (line 12206)
  11467. * significance loss (floating point): Significance Loss. (line 11872)
  11468. * SIGSEGV: Signals. (line 13369)
  11469. * simple assignment: Simple Assignment. (line 2020)
  11470. * size of type: Type Size. (line 3726)
  11471. * SNaN: Special Float Values.
  11472. (line 11638)
  11473. * space character in source: Whitespace. (line 1260)
  11474. * special floating-point values: Special Float Values.
  11475. (line 11624)
  11476. * stack: Stack. (line 594)
  11477. * stack frame: Stack. (line 594)
  11478. * stack frame >1<: Stack. (line 594)
  11479. * stack overflow: Stack. (line 594)
  11480. * standard output: Complete Explanation.
  11481. (line 857)
  11482. * statement, break: break Statement. (line 6241)
  11483. * statement, continue: continue Statement.
  11484. (line 6429)
  11485. * statement, do–while: do-while Statement.
  11486. (line 6223)
  11487. * statement, expression: Expression Statement.
  11488. (line 6034)
  11489. * statement, for: for Statement. (line 6293)
  11490. * statement, goto: goto Statement. (line 6686)
  11491. * statement, if: if Statement. (line 6061)
  11492. * statement, if...else: if-else Statement. (line 6080)
  11493. * statement, null: Null Statement. (line 6672)
  11494. * statement, return: return Statement. (line 6163)
  11495. * statement, switch: switch Statement. (line 6477)
  11496. * statement, while: while Statement. (line 6196)
  11497. * statements: Statements. (line 6017)
  11498. * statements inside expressions: Statement Exprs. (line 6970)
  11499. * statements, loop: Loop Statements. (line 6184)
  11500. * static assertions: Static Assertions. (line 12983)
  11501. * static function, declaration: Static Functions. (line 7923)
  11502. * static functions: Static Functions. (line 7909)
  11503. * static local variables: Static Local Variables.
  11504. (line 7403)
  11505. * sticky exception flags (floating point): Exception Flags. (line 11732)
  11506. * storage organization: Storage. (line 957)
  11507. * string: Strings. (line 5508)
  11508. * string constants: String Constants. (line 3500)
  11509. * stringification: Stringification. (line 10178)
  11510. * structure assignment: Structure Assignment.
  11511. (line 4987)
  11512. * structure constructors: Structure Constructors.
  11513. (line 5164)
  11514. * structure field offset: Field Offset. (line 4658)
  11515. * structure fields, constant: const Fields. (line 4859)
  11516. * structure fields, referencing: Referencing Fields.
  11517. (line 4539)
  11518. * structure layout: Structure Layout. (line 4678)
  11519. * structures: Structures. (line 4461)
  11520. * structures, overlaying: Overlaying Structures.
  11521. (line 4957)
  11522. * structures, unnamed: Unnamed Types as Fields.
  11523. (line 5203)
  11524. * subexpressions, assignment in: Assignment in Subexpressions.
  11525. (line 2231)
  11526. * subnormal numbers: Special Float Values.
  11527. (line 11665)
  11528. * subtraction operator: Basic Arithmetic. (line 1514)
  11529. * success: Values from main. (line 8466)
  11530. * switch statement: switch Statement. (line 6477)
  11531. * symbolic constants: Object-like Macros.
  11532. (line 9934)
  11533. * system header files: Header Files. (line 9624)
  11534. * tab (ASCII character): Character Constants.
  11535. (line 3468)
  11536. * tab character in source: Whitespace. (line 1260)
  11537. * tentative definition: Allocating File-Scope.
  11538. (line 7492)
  11539. * thunks: Nested Functions. (line 8751)
  11540. * token: Lexical Syntax. (line 1191)
  11541. * token concatenation: Concatenation. (line 10257)
  11542. * token pasting: Concatenation. (line 10257)
  11543. * truncation: Integer Conversion.
  11544. (line 2989)
  11545. * truth value: Numeric Comparisons.
  11546. (line 1776)
  11547. * two’s-complement representation: Integer Representations.
  11548. (line 11444)
  11549. * twosum: Error Recovery. (line 12010)
  11550. * type alignment: Type Alignment. (line 13034)
  11551. * type conversion, pointer: Aliasing. (line 13108)
  11552. * type conversions: Type Conversions. (line 9140)
  11553. * type designator: Type Designators. (line 3190)
  11554. * type size: Type Size. (line 3726)
  11555. * type tags: Type Tags. (line 5316)
  11556. * type, boolean: Boolean Type. (line 3002)
  11557. * type, void: The Void Type. (line 3154)
  11558. * typedef names: Defining Typedef Names.
  11559. (line 5942)
  11560. * types of integer constants: Integer Const Type.
  11561. (line 3291)
  11562. * types, compatible: Compatible Types. (line 9080)
  11563. * types, complex: Complex Data Types.
  11564. (line 3099)
  11565. * types, enumeration: Enumeration Types. (line 5863)
  11566. * types, floating-point: Floating-Point Data Types.
  11567. (line 3050)
  11568. * types, incomplete: Incomplete Types. (line 5244)
  11569. * types, integer: Integer Types. (line 2869)
  11570. * types, primitive: Primitive Types. (line 2860)
  11571. * types, signed: Signed and Unsigned Types.
  11572. (line 2918)
  11573. * types, unsigned: Signed and Unsigned Types.
  11574. (line 2918)
  11575. * uintptr_t: Pointer-Integer Conversion.
  11576. (line 4413)
  11577. * undefining macros: Undefining and Redefining Macros.
  11578. (line 10598)
  11579. * underflow, floating: Special Float Values.
  11580. (line 11665)
  11581. * underflow, floating >1<: Exception Flags. (line 11732)
  11582. * underscores in variables in macros: Macros and Auto Type.
  11583. (line 10879)
  11584. * Unicode: Characters. (line 1232)
  11585. * Unicode character codes: Unicode Character Codes.
  11586. (line 3604)
  11587. * union, casting to a: Cast to Union. (line 5129)
  11588. * unions: Unions. (line 5022)
  11589. * unions, unnamed: Unnamed Types as Fields.
  11590. (line 5203)
  11591. * universal character names: Unicode Character Codes.
  11592. (line 3604)
  11593. * unnamed structures: Unnamed Types as Fields.
  11594. (line 5203)
  11595. * unnamed unions: Unnamed Types as Fields.
  11596. (line 5203)
  11597. * unsafe macros: Duplication of Side Effects.
  11598. (line 10808)
  11599. * unsigned types: Signed and Unsigned Types.
  11600. (line 2918)
  11601. * UTF-8 String Constants: UTF-8 String Constants.
  11602. (line 3586)
  11603. * variable declarations: Variable Declarations.
  11604. (line 7038)
  11605. * variable declarations, combining: Combining Variable Declarations.
  11606. (line 7114)
  11607. * variable number of arguments: Variadic Macros. (line 10335)
  11608. * variable-length array parameters: Variable-Length Array Parameters.
  11609. (line 8585)
  11610. * variable-length arrays: Arrays of Variable Length.
  11611. (line 5793)
  11612. * variable-length parameter lists: Variable Number of Arguments.
  11613. (line 8648)
  11614. * variables: Variables. (line 7016)
  11615. * variables, const: const. (line 7553)
  11616. * variables, file-scope: File-Scope Variables.
  11617. (line 7370)
  11618. * variables, global: File-Scope Variables.
  11619. (line 7370)
  11620. * variables, local: Local Variables. (line 7325)
  11621. * variables, local, in macros: Macros and Auto Type.
  11622. (line 10861)
  11623. * variables, static local: Static Local Variables.
  11624. (line 7403)
  11625. * variables, volatile: volatile. (line 7617)
  11626. * variadic function: Variable Number of Arguments.
  11627. (line 8648)
  11628. * variadic macros: Variadic Macros. (line 10335)
  11629. * va_copy: Variable Number of Arguments.
  11630. (line 8716)
  11631. * va_end: Variable Number of Arguments.
  11632. (line 8659)
  11633. * va_list: Variable Number of Arguments.
  11634. (line 8659)
  11635. * va_start: Variable Number of Arguments.
  11636. (line 8659)
  11637. * vertical tab: Character Constants.
  11638. (line 3468)
  11639. * vertical tab in source: Whitespace. (line 1260)
  11640. * void pointers: Void Pointers. (line 4001)
  11641. * void type: The Void Type. (line 3154)
  11642. * volatile variables and fields: volatile. (line 7617)
  11643. * while statement: while Statement. (line 6196)
  11644. * whitespace characters in source files: Whitespace. (line 1260)
  11645. * wide character constants: Wide Character Constants.
  11646. (line 3640)
  11647. * wide string constants: Wide String Constants.
  11648. (line 3675)
  11649. * wrapper #ifndef: Once-Only Headers. (line 9801)
  11650. * zero, division by: Division and Remainder.
  11651. (line 1769)
  11652. * zero-length arrays: Zero Length. (line 4893)
  11653. * zero-origin indexing: Array Example. (line 1062)

State 09Sep22.