File: c.texi

package info (click to toggle)
gnu-c-intro-and-ref 0.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 1,172 kB
  • sloc: makefile: 29
file content (13406 lines) | stat: -rw-r--r-- 446,610 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
\input texinfo


@c Copyright (C) 2022, 2023, 2025 Richard Stallman
@c   and Free Software Foundation, Inc.

@c (The work of Trevis Rothwell and Nelson Beebe has been assigned to the FSF.)

@c move alignment later?

@c ??? alloca

@setfilename ./c.info
@include version.texi
@settitle GNU C Language Manual
@documentencoding UTF-8

@c Merge variable index into the function index.
@synindex vr fn
@codequoteundirected on
@codequotebacktick on

@copying
This is Edition @value{VERSION}.

Copyright @copyright{} 2022, 2023, 2025 Richard Stallman
and Free Software Foundation, Inc.

(The work of Trevis Rothwell and Nelson Beebe has been assigned to the FSF.)

@quotation
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``GNU General Public License,'' with the
Front-Cover Texts being ``A GNU Manual,'' and with the Back-Cover
Texts as in (a) below.  A copy of the license is included in the
section entitled ``GNU Free Documentation License.''

(a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
modify this GNU manual.''
@end quotation
@end copying

@dircategory Programming
@direntry
* C: (c).       GNU C Language Intro and Reference Manual
@end direntry

@titlepage
@sp 6
@center @titlefont{GNU C Language Introduction}
@center @titlefont{and Reference Manual}
@sp 4
@center Edition @value{VERSION}
@sp 5
@center Richard Stallman
@center and
@center Trevis Rothwell
@center plus Nelson Beebe
@center on floating point
@page
@vskip 0pt plus 1filll

@insertcopying

@sp 2
@ignore
WILL BE Published by the Free Software Foundation @*
31 Milk St # 960789 @*
Boston, MA 02110 USA @*
ISBN ?-??????-??-?
@end ignore

@ignore
@sp 1
Cover art by J. Random Artist
@end ignore

@end titlepage

@summarycontents
@contents


@node Top
@ifnottex
@top GNU C Manual
@end ifnottex
@iftex
@top Preface
@end iftex

This manual explains the C language for use with the GNU Compiler
Collection (GCC) on the GNU/Linux operating system and other systems.
We refer to this dialect as GNU C.  If you already know C, you can use
this as a reference manual.

If you understand basic concepts of programming but know nothing about
C, you can read this manual sequentially from the beginning to learn
the C language.

If you are a beginner in programming, we recommend you first learn a
language with automatic garbage collection and no explicit pointers,
rather than starting with C@.  Good choices include Lisp, Scheme,
Python and Java.  Because of C's explicit pointers, programmers must be
careful to avoid certain kinds of errors in memory usage.

C is a venerable language; it was first used in 1973.  The GNU C
Compiler, which was subsequently extended into the GNU Compiler
Collection, was first released in 1987.  Other important languages
were designed based on C: once you know C, it gives you a useful base
for learning C@t{++}, C#, Java, Scala, D, Go, and more.

The special advantage of C is that it is fairly simple while allowing
close access to the computer's hardware, which previously required
writing in assembler language to describe the individual machine
instructions.  Some have called C a ``high-level assembler language''
because of its explicit pointers and lack of automatic management of
storage.  As one wag put it, ``C combines the power of assembler
language with the convenience of assembler language.''  However, C is
far more portable, and much easier to read and write, than assembler
language.

This manual describes the GNU C language supported by the GNU Compiler
Collection, as of roughly 2017.  Please inform us of any changes
needed to match the current version of GNU C.

When a construct may be absent or work differently in other C
compilers, we say so.  When it is not part of ISO standard C, we say
it is a ``GNU C extension,'' because it is useful to know that.
However, standards and other dialects are secondary topics for this
manual.  For simplicity's sake, we keep those notes short, unless it
is vital to say more.

Some aspects of the meaning of C programs depend on the target
platform: which computer, and which operating system, the compiled
code will run on.  Where this is the case, we say so.

When compiling for a ``real computer'', one that is a reasonable
platform for running the GNU/Linux system, the type @code{int} is
always 32 bits in size.  This manual assumes you are compiling for the
computer where you are running the compiler, which implies @code{int}
has that size.  GNU C can also compile code for some microprocessors
on which type @code{int} has fewer bits, but this manual does not try
to cover the complications of those peculiar platforms.

We hardly mention C@t{++} or other languages that the GNU
Compiler Collection supports.  We hope this manual will serve as a
base for writing manuals for those languages, but languages so
different can't share one common manual.

The C language provides no built-in facilities for performing such
common operations as input/output, memory management, string
manipulation, and the like.  Instead, these facilities are provided by
functions defined in the standard library, which is automatically
available in every C program.  @xref{Top, The GNU C Library, , libc,
The GNU C Library Reference Manual}.

Most GNU/Linux systems use the GNU C Library to provide those facilities.
It is itself written in C, so once you know C you can read its source
code and see how its library functions do their jobs.  Some fraction
of the functions are implemented as @dfn{system calls}, which means
they contain a special instruction that asks the system kernel (Linux)
to do a specific task.  To understand how those are implemented, you'd
need to read Linux source code.  Whether a library function is
a system call is an internal implementation detail that makes no
difference for how to call the function.

This manual incorporates the former GNU C Preprocessor Manual, which
was among the earliest GNU manuals.  It also uses some text from the
earlier GNU C Manual that was written by Trevis Rothwell and James
Youngman.

GNU C has many obscure features, each one either for historical
compatibility or meant for very special situations.  We have left them
to a companion manual, the GNU C Obscurities Manual, which will be
published digitally later.

Please report errors and suggestions to c-manual@@gnu.org.

@menu
* The First Example::             Getting started with basic C code.
* Complete Program::              A whole example program
                                    that can be compiled and run.
* Storage::                       Basic layout of storage; bytes.
* Beyond Integers::               Exploring different numeric types.
* Lexical Syntax::                The various lexical components of C programs.
* Arithmetic::                    Numeric computations.
* Assignment Expressions::        Storing values in variables.
* Execution Control Expressions:: Expressions combining values in various ways.
* Binary Operator Grammar::       An overview of operator precedence.
* Order of Execution::            The order of program execution.
* Primitive Types::               More details about primitive data types.
* Constants::                     Explicit constant values:
                                    details and examples.
* Type Size::                     The memory space occupied by a type.
* Pointers::                      Creating and manipulating memory pointers.
* Structures::                    Compound data types built
                                    by grouping other types.
* Arrays::                        Creating and manipulating arrays.
* Enumeration Types::             Sets of integers with named values.
* Defining Typedef Names::        Using @code{typedef} to define type names.
* Statements::                    Controlling program flow.
* Variables::                     Details about declaring, initializing,
                                    and using variables.
* Type Qualifiers::               Mark variables for certain intended uses.
* Functions::                     Declaring, defining, and calling functions.
* Compatible Types::              How to tell if two types are compatible
                                    with each other.
* Type Conversions::              Converting between types.
* Scope::                         Different categories of identifier scope.
* Preprocessing::                 Using the GNU C preprocessor.
* Integers in Depth::             How integer numbers are represented.
* Floating Point in Depth::       How floating-point numbers are represented.
* Compilation::                   How to compile multi-file programs.
* Directing Compilation::         Operations that affect compilation
                                    but don't change the program.

Appendices

* Type Alignment::                Where in memory a type can validly start.
* Aliasing::                      Accessing the same data in two types.
* Digraphs::                      Two-character aliases for some characters.
* Attributes::                    Specifying additional information
                                    in a declaration.
* Signals::                       Fatal errors triggered in various scenarios.
* GNU Free Documentation License::      The license for this manual.
* GNU General Public License::
* Symbol Index::                  Keyword and symbol index.
* Concept Index::                 Detailed topical index.

@detailmenu
--- The Detailed Node Listing ---

* Recursive Fibonacci::          Writing a simple function recursively.
* Stack::                        Each function call uses space in the stack.
* Iterative Fibonacci::          Writing the same function iteratively.
* Complete Example::             Turn the simple function into a full program.
* Complete Explanation::         Explanation of each part of the example.
* Complete Line-by-Line::        Explaining each line of the example.
* Compile Example::              Using GCC to compile the example.
* Float Example::                A function that uses floating-point numbers.
* Array Example::                A function that works with arrays.
* Array Example Call::           How to call that function.
* Array Example Variations::     Different ways to write the call example.

Lexical Syntax

* English::                      Write programs in English!
* Characters::                   The characters allowed in C programs.
* Whitespace::                   The particulars of whitespace characters.
* Comments::                     How to include comments in C code.
* Identifiers::                  How to form identifiers (names).
* Operators/Punctuation::        Characters used as operators or punctuation.
* Line Continuation::            Splitting one line into multiple lines.
* Digraphs::                     Two-character substitutes for some characters.

Arithmetic

* Basic Arithmetic::             Addition, subtraction, multiplication,
                                   and division.
* Integer Arithmetic::           How C performs arithmetic with integer values.
* Integer Overflow::             When an integer value exceeds the range
                                   of its type.
* Mixed Mode::                   Calculating with both integer values
                                   and floating-point values.
* Division and Remainder::       How integer division works.
* Numeric Comparisons::          Comparing numeric values for
                                   equality or order.
* Shift Operations::             Shift integer bits left or right.
* Bitwise Operations::           Bitwise conjunction, disjunction, negation.

Assignment Expressions

* Simple Assignment::            The basics of storing a value.
* Lvalues::                      Expressions into which a value can be stored.
* Modifying Assignment::         Shorthand for changing an lvalue's contents.
* Increment/Decrement::          Shorthand for incrementing and decrementing
                                   an lvalue's contents.
* Postincrement/Postdecrement::  Accessing then incrementing or decrementing.
* Assignment in Subexpressions:: How to avoid ambiguity.
* Write Assignments Separately:: Write assignments as separate statements.

Execution Control Expressions

* Logical Operators::            Logical conjunction, disjunction, negation.
* Logicals and Comparison::      Logical operators with comparison operators.
* Logicals and Assignments::     Assignments with logical operators.
* Conditional Expression::       An if/else construct inside expressions.
* Comma Operator::               Build a sequence of subexpressions.

Order of Execution

* Reordering of Operands::       Operations in C are not necessarily computed
                                   in the order they are written.
* Associativity and Ordering::   Some associative operations are performed
                                   in a particular order; others are not.
* Sequence Points::              Some guarantees about the order of operations.
* Postincrement and Ordering::   Ambiguous execution order with postincrement.
* Ordering of Operands::         Evaluation order of operands
                                   and function arguments.
* Optimization and Ordering::    Compiler optimizations can reorder operations
                                   only if it has no impact on program results.

Primitive Data Types

* Integer Types::                Description of integer types.
* Floating-Point Data Types::    Description of floating-point types.
* Complex Data Types::           Description of complex number types.
* The Void Type::                A type indicating no value at all.
* Other Data Types::             A brief summary of other types.

Constants

* Integer Constants::            Literal integer values.
* Integer Const Type::           Types of literal integer values.
* Floating Constants::           Literal floating-point values.
* Imaginary Constants::          Literal imaginary number values.
* Invalid Numbers::              Avoiding preprocessing number misconceptions.
* Character Constants::          Literal character values.
* Unicode Character Codes::      Unicode characters represented
                                   in either UTF-16 or UTF-32.
* Wide Character Constants::     Literal characters values larger than 8 bits.
* String Constants::             Literal string values.
* UTF-8 String Constants::       Literal UTF-8 string values.
* Wide String Constants::        Literal string values made up of
                                   16- or 32-bit characters.

Pointers

* Address of Data::              Using the ``address-of'' operator.
* Pointer Types::                For each type, there is a pointer type.
* Pointer Declarations::         Declaring variables with pointer types.
* Pointer Type Designators::     Designators for pointer types.
* Pointer Dereference::          Accessing what a pointer points at.
* Null Pointers::                Pointers which do not point to any object.
* Invalid Dereference::          Dereferencing null or invalid pointers.
* Void Pointers::                Totally generic pointers, can cast to any.
* Pointer Comparison::           Comparing memory address values.
* Pointer Arithmetic::           Computing memory address values.
* Pointers and Arrays::          Using pointer syntax instead of array syntax.
* Low-Level Pointer Arithmetic:: More about computing memory address values.
* Pointer Increment/Decrement::  Incrementing and decrementing pointers.
* Pointer Arithmetic Drawbacks:: A common pointer bug to watch out for.
* Pointer-Integer Conversion::   Converting pointer types to integer types.
* Printing Pointers::            Using @code{printf} for a pointer's value.

Structures

* Referencing Fields::           Accessing field values in a structure object.
* Arrays as Fields::             Accessing arrays as structure fields.
* Dynamic Memory Allocation::    Allocating space for objects
                                   while the program is running.
* Field Offset::                 Memory layout of fields within a structure.
* Structure Layout::             Planning the memory layout of fields.
* Packed Structures::            Packing structure fields as close as possible.
* Bit Fields::                   Dividing integer fields
                                   into fields with fewer bits.
* Bit Field Packing::            How bit fields pack together in integers.
* const Fields::                 Making structure fields immutable.
* Zero Length::                  Zero-length array as a variable-length object.
* Flexible Array Fields::        Another approach to variable-length objects.
* Overlaying Structures::        Casting one structure type
                                   over an object of another structure type.
* Structure Assignment::         Assigning values to structure objects.
* Unions::                       Viewing the same object in different types.
* Packing With Unions::          Using a union type to pack various types into
                                   the same memory space.
* Cast to Union::                Casting a value one of the union's alternative
                                   types to the type of the union itself.
* Structure Constructors::       Building new structure objects.
* Unnamed Types as Fields::      Fields' types do not always need names.
* Incomplete Types::             Types which have not been fully defined.
* Intertwined Incomplete Types:: Defining mutually-recursive structure types.
* Type Tags::                    Scope of structure and union type tags.

Arrays

* Accessing Array Elements::     How to access individual elements of an array.
* Declaring an Array::           How to name and reserve space for a new array.
* Strings::                      A string in C is a special case of array.
* Incomplete Array Types::       Naming, but not allocating, a new array.
* Limitations of C Arrays::      Arrays are not first-class objects.
* Multidimensional Arrays::      Arrays of arrays.
* Constructing Array Values::    Assigning values to an entire array at once.
* Arrays of Variable Length::    Declaring arrays of non-constant size.

Statements

* Expression Statement::         Evaluate an expression, as a statement,
                                   usually done for a side effect.
* if Statement::                 Basic conditional execution.
* if-else Statement::            Multiple branches for conditional execution.
* Blocks::                       Grouping multiple statements together.
* return Statement::             Return a value from a function.
* Loop Statements::              Repeatedly executing a statement or block.
* switch Statement::             Multi-way conditional choices.
* switch Example::               A plausible example of using @code{switch}.
* Duffs Device::                 A special way to use @code{switch}.
* Case Ranges::                  Ranges of values for @code{switch} cases.
* Null Statement::               A statement that does nothing.
* goto Statement::               Jump to another point in the source code,
                                   identified by a label.
* Local Labels::                 Labels with limited scope.
* Labels as Values::             Getting the address of a label.
* Statement Exprs::              A series of statements used as an expression.

Variables

* Variable Declarations::        Name a variable and reserve space for it.
* Initializers::                 Assigning initial values to variables.
* Designated Inits::             Assigning initial values to array elements
                                   at particular array indices.
* Auto Type::                    Obtaining the type of a variable.
* Local Variables::              Variables declared in function definitions.
* File-Scope Variables::         Variables declared outside of
                                   function definitions.
* Static Local Variables::       Variables declared within functions,
                                   but with permanent storage allocation.
* Extern Declarations::          Declaring a variable
                                   which is allocated somewhere else.
* Allocating File-Scope::        When is space allocated
                                   for file-scope variables?
* auto and register::            Historically used storage directions.
* Omitting Types::               The bad practice of declaring variables
                                   with implicit type.

Type Qualifiers

* const::                        Variables whose values don't change.
* volatile::                     Variables whose values may be accessed
                                   or changed outside of the control of
                                   this program.
* restrict Pointers::            Restricted pointers for code optimization.
* restrict Pointer Example::     Example of how that works.

Functions

* Function Definitions::         Writing the body of a function.
* Function Declarations::        Declaring the interface of a function.
* Function Calls::               Using functions.
* Function Call Semantics::      Call-by-value argument passing.
* Function Pointers::            Using references to functions.
* The main Function::            Where execution of a GNU C program begins.

Type Conversions

* Explicit Type Conversion::     Casting a value from one type to another.
* Assignment Type Conversions::  Automatic conversion by assignment operation.
* Argument Promotions::          Automatic conversion of function parameters.
* Operand Promotions::           Automatic conversion of arithmetic operands.
* Common Type::                  When operand types differ, which one is used?

Scope

* Scope::                        Different categories of identifier scope.

Preprocessing

* Preproc Overview::             Introduction to the C preprocessor.
* Directives::                   The form of preprocessor directives.
* Preprocessing Tokens::         The lexical elements of preprocessing.
* Header Files::                 Including one source file in another.
* Macros::                       Macro expansion by the preprocessor.
* Conditionals::                 Controlling whether to compile some lines
                                   or ignore them.
* Diagnostics::                  Reporting warnings and errors.
* Line Control::                 Reporting source line numbers.
* Null Directive::               A preprocessing no-op.

Integers in Depth

* Integer Representations::      How integer values appear in memory.
* Maximum and Minimum Values::   Value ranges of integer types.

Floating Point in Depth

* Floating Representations::     How floating-point values appear in memory.
* Floating Type Specs::          Precise details of memory representations.
* Special Float Values::         Infinity, Not a Number, and Subnormal Numbers.
* Invalid Optimizations::        Don't mess up non-numbers and signed zeros.
* Exception Flags::              Handling certain conditions in floating point.
* Exact Floating-Point::         Not all floating calculations lose precision.
* Rounding::                     When a floating result can't be represented
                                   exactly in the floating-point type in use.
* Rounding Issues::              Avoid magnifying rounding errors.
* Significance Loss::            Subtracting numbers that are almost equal.
* Fused Multiply-Add::           Taking advantage of a special floating-point
                                   instruction for faster execution.
* Error Recovery::               Determining rounding errors.
* Exact Floating Constants::     Precisely specified floating-point numbers.
* Handling Infinity::            When floating calculation is out of range.
* Handling NaN::                 What floating calculation is undefined.
* Signed Zeros::                 Positive zero vs. negative zero.
* Scaling by the Base::          A useful exact floating-point operation.
* Rounding Control::             Specifying some rounding behaviors.
* Machine Epsilon::              The smallest number you can add to 1.0
                                   and get a sum which is larger than 1.0.
* Complex Arithmetic::           Details of arithmetic with complex numbers.
* Round-Trip Base Conversion::   What happens between base-2 and base-10.
* Further Reading::              References for floating-point numbers.

Directing Compilation

* Pragmas::                      Controlling compilation of some constructs.
* Static Assertions::            Compile-time tests for conditions.

@end detailmenu
@end menu

@node The First Example
@chapter The First Example

This chapter presents the source code for a very simple C program and
uses it to explain a few features of the language.  If you already
know the basic points of C presented in this chapter, you can skim it
or skip it.

We present examples of C source code (other than comments) using a
fixed-width typeface, since that's the way they look when you edit
them in an editor such as GNU Emacs.

@menu
* Recursive Fibonacci:: Writing a simple function recursively.
* Stack::               Each function call uses space in the stack.
* Iterative Fibonacci:: Writing the same function iteratively.
@end menu

@node Recursive Fibonacci
@section Example: Recursive Fibonacci
@cindex recursive Fibonacci function
@cindex Fibonacci function, recursive

To introduce the most basic features of C, let's look at code for a
simple mathematical function that does calculations on integers.  This
function calculates the @var{n}th number in the Fibonacci series, in
which each number is the sum of the previous two: 1, 1, 2, 3, 5, 8,
13, 21, 34, 55, @dots{}.

@example
int
fib (int n)
@{
  if (n <= 2)  /* @r{This avoids infinite recursion.}  */
    return 1;
  else
    return fib (n - 1) + fib (n - 2);
@}
@end example

This very simple program illustrates several features of C:

@itemize @bullet
@item
A function definition, whose first two lines constitute the function
header.  @xref{Function Definitions}.

@item
A function parameter @code{n}, referred to as the variable @code{n}
inside the function body.  @xref{Function Parameter Variables}.
A function definition uses parameters to refer to the argument
values provided in a call to that function.

@item
Arithmetic.  C programs add with @samp{+} and subtract with
@samp{-}.  @xref{Arithmetic}.

@item
Numeric comparisons.  The operator @samp{<=} tests for ``less than or
equal.''  @xref{Numeric Comparisons}.

@item
Integer constants written in base 10.
@xref{Integer Constants}.

@item
A function call.  The function call @code{fib (n - 1)} calls the
function @code{fib}, passing as its argument the value @code{n - 1}.
@xref{Function Calls}.

@item
A comment, which starts with @samp{/*} and ends with @samp{*/}.  The
comment has no effect on the execution of the program.  Its purpose is
to provide explanations to people reading the source code.  Including
comments in the code is tremendously important---they provide
background information so others can understand the code more quickly.
@xref{Comments}.

In this manual, we present comment text in the variable-width typeface
used for the text of the chapters, not in the fixed-width typeface
used for the rest of the code.  That is to make comments easier to
read.  This distinction of typeface does not exist in a real file of C
source code.

@item
Two kinds of statements, the @code{return} statement and the
@code{if}@dots{}@code{else} statement.  @xref{Statements}.

@item
Recursion.  The function @code{fib} calls itself; that is called a
@dfn{recursive call}.  These are valid in C, and quite common.

The @code{fib} function would not be useful if it didn't return.
Thus, recursive definitions, to be of any use, must avoid
@dfn{infinite recursion}.

This function definition prevents infinite recursion by specially
handling the case where @code{n} is two or less.  Thus the maximum
depth of recursive calls is less than @code{n}.
@end itemize

@menu
* Function Header:: The function's name and how it is called.
* Function Body::   Declarations and statements that implement the function.
@end menu

@node Function Header
@subsection Function Header
@cindex function header

In our example, the first two lines of the function definition are the
@dfn{header}.  Its purpose is to state the function's name and say how
it is called:

@example
int
fib (int n)
@end example

@noindent
says that the function returns an integer (type @code{int}), its name is
@code{fib}, and it takes one argument named @code{n} which is also an
integer.  (Data types will be explained later, in @ref{Primitive Types}.)

@node Function Body
@subsection Function Body
@cindex function body
@cindex recursion

The rest of the function definition is called the @dfn{function body}.
Like every function body, this one starts with @samp{@{}, ends with
@samp{@}}, and contains zero or more @dfn{statements} and
@dfn{declarations}.  Statements specify actions to take, whereas
declarations define names of variables, functions, and so on.  Each
statement and each declaration ends with a semicolon (@samp{;}).

Statements and declarations often contain @dfn{expressions}; an
expression is a construct whose execution produces a @dfn{value} of
some data type, but may also take actions through ``side effects''
that alter subsequent execution.  A statement, by contrast, does not
have a value; it affects further execution of the program only through
the actions it takes.

This function body contains no declarations, and just one statement,
but that one is a complex statement in that it contains nested
statements.  This function uses two kinds of statements:

@table @code
@item return
The @code{return} statement makes the function return immediately.
It looks like this:

@example
return @var{value};
@end example

Its meaning is to compute the expression @var{value} and exit the
function, making it return whatever value that expression produced.
For instance,

@example
return 1;
@end example

@noindent
returns the integer 1 from the function, and

@example
return fib (n - 1) + fib (n - 2);
@end example

@noindent
returns a value computed by performing two function calls
as specified and adding their results.

@item @code{if}@dots{}@code{else}
The @code{if}@dots{}@code{else} statement is a @dfn{conditional}.
Each time it executes, it chooses one of its two substatements to execute
and ignores the other.  It looks like this:

@example
if (@var{condition})
  @var{if-true-statement}
else
  @var{if-false-statement}
@end example

Its meaning is to compute the expression @var{condition} and, if it's
``true,'' execute @var{if-true-statement}.  Otherwise, execute
@var{if-false-statement}.  @xref{if-else Statement}.

Inside the @code{if}@dots{}@code{else} statement, @var{condition} is
simply an expression.  It's considered ``true'' if its value is
nonzero.  (A comparison operation, such as @code{n <= 2}, produces the
value 1 if it's ``true'' and 0 if it's ``false.''  @xref{Numeric
Comparisons}.)  Thus,

@example
if (n <= 2)
  return 1;
else
  return fib (n - 1) + fib (n - 2);
@end example

@noindent
first tests whether the value of @code{n} is less than or equal to 2.
If so, the expression @code{n <= 2} has the value 1.  So execution
continues with the statement

@example
return 1;
@end example

@noindent
Otherwise, execution continues with this statement:

@example
return fib (n - 1) + fib (n - 2);
@end example

Each of these statements ends the execution of the function and
provides a value for it to return.  @xref{return Statement}.
@end table

Calculating @code{fib} using ordinary integers in C works only for
@var{n} < 47 because the value of @code{fib (47)} is too large to fit
in type @code{int}.  In GNU C, type @code{int} holds 32 bits
(@pxref{Integer Types}), so the addition operation that tries to add
@code{fib (46)} and @code{fib (45)} cannot deliver the correct result.
This occurrence is called @dfn{integer overflow}.

Overflow can manifest itself in various ways, but one thing that can't
possibly happen is to produce the correct value, since that can't fit
in the space for the value.  @xref{Integer Overflow}, for more details
about this situation.

@xref{Functions}, for a full explanation about functions.

@node Stack
@section The Stack, And Stack Overflow
@cindex stack
@cindex stack frame
@cindex stack overflow
@cindex recursion, drawbacks of

@cindex stack frame
Recursion has a drawback: there are limits to how many nested levels of
function calls a program can make.  In C, each function call allocates a block
of memory which it uses until the call returns.  C allocates these
blocks consecutively within a large area of memory known as the
@dfn{stack}, so we refer to the blocks as @dfn{stack frames}.

The size of the stack is limited; if the program tries to use too
much, that causes the program to fail because the stack is full.  This
is called @dfn{stack overflow}.

@cindex crash
@cindex segmentation fault
Stack overflow on GNU/Linux typically manifests itself as the
@dfn{signal} named @code{SIGSEGV}, also known as a ``segmentation
fault.''  By default, this signal terminates the program immediately,
rather than letting the program try to recover, or reach an expected
ending point.  (We commonly say in this case that the program
``crashes.'')  @xref{Signals}.

It is inconvenient to observe a crash by passing too large
an argument to recursive Fibonacci, because the program would run a
long time before it crashes.  This algorithm is simple but
ridiculously slow: in calculating @code{fib (@var{n})}, the number of
(recursive) calls @code{fib (1)} or @code{fib (2)} that it makes equals
the final result.

However, you can observe stack overflow very quickly if you use
this function instead:

@example
int
fill_stack (int n)
@{
  if (n <= 1)  /* @r{This limits the depth of recursion.}  */
    return 1;
  else
    return fill_stack (n - 1);
@}
@end example

Under gNewSense GNU/Linux on the Lemote Yeeloong, without optimization
and using the default configuration, an experiment showed there is
enough stack space to do 261906 nested calls to that function.  One
more, and the stack overflows and the program crashes.  On another
platform, with a different configuration, or with a different
function, the limit might be bigger or smaller.

@node Iterative Fibonacci
@section Example: Iterative Fibonacci
@cindex iterative Fibonacci function
@cindex Fibonacci function, iterative

Here's a much faster algorithm for computing the same Fibonacci
series.  It is faster for two reasons.  First, it uses @dfn{iteration}
(that is, repetition or looping) rather than recursion, so it doesn't
take time for a large number of function calls.  But mainly, it is
faster because the number of repetitions is small---only @code{@var{n}}.

@c If you change this, change the duplicate in node Example of for.

@example
int
fib (int n)
@{
  int last = 1;   /* @r{Initial value is @code{fib (1)}.}  */
  int prev = 0;   /* @r{Initial value controls @code{fib (2)}.}  */
  int i;

  for (i = 1; i < n; ++i)
    /* @r{If @code{n} is 1 or less, the loop runs zero times,}  */
    /* @r{since in that case @code{i < n} is false the first time.}  */
    @{
      /* @r{Now @code{last} is @code{fib (@code{i})}}
         @r{and @code{prev} is @code{fib (@code{i} - 1)}.}  */
      /* @r{Compute @code{fib (@code{i} + 1)}.}  */
      int next = prev + last;
      /* @r{Shift the values down.}  */
      prev = last;
      last = next;
      /* @r{Now @code{last} is @code{fib (@code{i} + 1)}}
         @r{and @code{prev} is @code{fib (@code{i})}.}
         @r{But that won't stay true for long,}
         @r{because we are about to increment @code{i}.}  */
    @}

  return last;
@}
@end example

This definition computes @code{fib (@var{n})} in a time proportional
to @code{@var{n}}.  The comments in the definition explain how it works: it
advances through the series, always keeps the last two values in
@code{last} and @code{prev}, and adds them to get the next value.

Here are the additional C features that this definition uses:

@table @asis
@item Internal blocks
Within a function, wherever a statement is called for, you can write a
@dfn{block}.  It looks like @code{@{ @r{@dots{}} @}} and contains zero or
more statements and declarations.  (You can also use additional
blocks as statements in a block.)

The function body also counts as a block, which is why it can contain
statements and declarations.

@xref{Blocks}.

@item Declarations of local variables
This function body contains declarations as well as statements.  There
are three declarations directly in the function body, as well as a
fourth declaration in an internal block.  Each starts with @code{int}
because it declares a variable whose type is integer.  One declaration
can declare several variables, but each of these declarations is
simple and declares just one variable.

Variables declared inside a block (either a function body or an
internal block) are @dfn{local variables}.  These variables exist only
within that block; their names are not defined outside the block, and
exiting the block deallocates their storage.  This example declares
four local variables: @code{last}, @code{prev}, @code{i}, and
@code{next}.

The most basic local variable declaration looks like this:

@example
@var{type} @var{variablename};
@end example

For instance,

@example
int i;
@end example

@noindent
declares the local variable @code{i} as an integer.
@xref{Variable Declarations}.

@item Initializers
When you declare a variable, you can also specify its initial value,
like this:

@example
@var{type} @var{variablename} = @var{value};
@end example

For instance,

@example
int last = 1;
@end example

@noindent
declares the local variable @code{last} as an integer (type
@code{int}) and starts it off with the value 1.  @xref{Initializers}.

@item Assignment
Assignment: a specific kind of expression, written with the @samp{=}
operator, that stores a new value in a variable or other place.  Thus,

@example
@var{variable} = @var{value}
@end example

@noindent
is an expression that computes @code{@var{value}} and stores the value in
@code{@var{variable}}.  @xref{Assignment Expressions}.

@item Expression statements
An expression statement is an expression followed by a semicolon.
That computes the value of the expression, then ignores the value.

An expression statement is useful when the expression changes some
data or has other side effects---for instance, with function calls, or
with assignments as in this example.  @xref{Expression Statement}.

Using an expression with no side effects in an expression statement is
pointless; for instance, the expression statement @code{x;} would
examine the value of @code{x} and ignore it.  That is not
useful.@footnote{Computing an expression and ignoring the result can
be useful in peculiar cases.  For instance, dereferencing a pointer
and ignoring the value is a way to cause a fault if a pointer value is
invalid.  @xref{Signals}.  But you may need to declare the pointer
target @code{volatile} or the dereference may be optimized away.
@xref{volatile}.}

@item Increment operator
The increment operator is @samp{++}.  @code{++i} is an
expression that is short for @code{i = i + 1}.
@xref{Increment/Decrement}.

@item @code{for} statements
A @code{for} statement is a clean way of executing a statement
repeatedly---a @dfn{loop} (@pxref{Loop Statements}).  Specifically,

@example
for (i = 1; i < n; ++i)
  @var{body}
@end example

@noindent
means to start by doing @code{i = 1} (set @code{i} to one) to prepare
for the loop.  The loop itself consists of

@itemize @bullet
@item
Testing @code{i < n} and exiting the loop if that's false.

@item
Executing @var{body}.

@item
Advancing the loop (executing @code{++i}, which increments @code{i}).
@end itemize

The net result is to execute @var{body} with 1 in @code{i},
then with 2 in @code{i}, and so on, stopping just before the repetition
where @code{i} would equal @code{n}.  If @code{n} is less than 1,
the loop will execute the body zero times.

The body of the @code{for} statement must be one and only one
statement.  You can't write two statements in a row there; if you try
to, only the first of them will be treated as part of the loop.

The way to put multiple statements in such a place is to group them
with a block, and that's what we do in this example.
@end table

@node Complete Program
@chapter A Complete Program
@cindex complete example program
@cindex example program, complete

It's all very well to write a Fibonacci function, but you cannot run
it by itself.  It is a useful program, but it is not a complete
program.

In this chapter we present a complete program that contains the
@code{fib} function.  This example shows how to make the program
start, how to make it finish, how to do computation, and how to print
a result.

@menu
* Complete Example::            Turn the simple function into a full program.
* Complete Explanation::        Explanation of each part of the example.
* Complete Line-by-Line::       Explaining each line of the example.
* Compile Example::             Using GCC to compile the example.
@end menu

@node Complete Example
@section Complete Program Example

Here is the complete program that uses the simple, recursive version
of the @code{fib} function (@pxref{Recursive Fibonacci}):

@example
#include <stdio.h>

int
fib (int n)
@{
  if (n <= 2)  /* @r{This avoids infinite recursion.}  */
    return 1;
  else
    return fib (n - 1) + fib (n - 2);
@}

int
main (void)
@{
  printf ("Fibonacci series item %d is %d\n",
          20, fib (20));
  return 0;
@}
@end example

@noindent
This program prints a message that shows the value of @code{fib (20)}.

Now for an explanation of what that code means.

@node Complete Explanation
@section Complete Program Explanation

@ifnottex
Here's the explanation of the code of the example in the
previous section.
@end ifnottex

This sample program prints a message that shows the value of @code{fib
(20)}, and exits with code 0 (which stands for successful execution).

Every C program is started by running the function named @code{main}.
Therefore, the example program defines a function named @code{main} to
provide a way to start it.  Whatever that function does is what the
program does.  @xref{The main Function}.

The @code{main} function is the first one called when the program
runs, but it doesn't come first in the example code.  The order of the
function definitions in the source code makes no difference to the
program's meaning.

The initial call to @code{main} always passes certain arguments, but
@code{main} does not have to pay attention to them.  To ignore those
arguments, define @code{main} with @code{void} as the parameter list.
(@code{void} as a function's parameter list normally means ``call with
no arguments,'' but @code{main} is a special case.)

The function @code{main} returns 0 because that is
the conventional way for @code{main} to indicate successful execution.
It could instead return a positive integer to indicate failure, and
some utility programs have specific conventions for the meaning of
certain numeric @dfn{failure codes}.  @xref{Values from main}.

@cindex @code{printf}
The simplest way to print text in C is by calling the @code{printf}
function, so here we explain very briefly what that function does.
For a full explanation of @code{printf} and the other standard I/O
functions, see @ref{Input/Output on Streams, The GNU C Library, ,
libc, The GNU C Library Reference Manual}.

@cindex standard output
The first argument to @code{printf} is a @dfn{string constant}
(@pxref{String Constants}) that is a template for output.  The
function @code{printf} copies most of that string directly as output,
including the newline character at the end of the string, which is
written as @samp{\n}.  The output goes to the program's @dfn{standard
output} destination, which in the usual case is the terminal.

@samp{%} in the template introduces a code that substitutes other text
into the output.  Specifically, @samp{%d} means to take the next
argument to @code{printf} and substitute it into the text as a decimal
number.  (The argument for @samp{%d} must be of type @code{int}; if it
isn't, @code{printf} will malfunction.)  So the output is a line that
looks like this:

@example
Fibonacci series item 20 is 6765
@end example

This program does not contain a definition for @code{printf} because
it is defined by the C library, which makes it available in all C
programs.  However, each program does need to @dfn{declare}
@code{printf} so it will be called correctly.  The @code{#include}
line takes care of that; it includes a @dfn{header file} called
@file{stdio.h} into the program's code.  That file is provided by the
operating system and it contains declarations for the many standard
input/output functions in the C library, one of which is
@code{printf}.

Don't worry about header files for now; we'll explain them later in
@ref{Header Files}.

The first argument of @code{printf} does not have to be a string
constant; it can be any string (@pxref{Strings}).  However, using a
constant is the most common case.

@node Complete Line-by-Line
@section Complete Program, Line by Line

Here's the same example, explained line by line.
@strong{Beginners, do you find this helpful or not?
Would you prefer a different layout for the example?
Please tell rms@@gnu.org.}

@example
#include <stdio.h>      /* @r{Include declaration of usual} */
                        /*   @r{I/O functions such as @code{printf}.}  */
                        /* @r{Most programs need these.}  */

int                     /* @r{This function returns an @code{int}.}  */
fib (int n)             /* @r{Its name is @code{fib};}  */
                        /*   @r{its argument is called @code{n}.}  */
@{                       /* @r{Start of function body.}  */
  /* @r{This stops the recursion from being infinite.}  */
  if (n <= 2)           /* @r{If @code{n} is 1 or 2,}  */
    return 1;           /*   @r{make @code{fib} return 1.}  */
  else                  /* @r{Otherwise, add the two previous}  */
                        /* @r{Fibonacci numbers.}  */
    return fib (n - 1) + fib (n - 2);
@}

int                     /* @r{This function returns an @code{int}.}  */
main (void)             /* @r{Start here; ignore arguments.}  */
@{                       /* @r{Print message with numbers in it.}  */
  printf ("Fibonacci series item %d is %d\n",
          20, fib (20));
  return 0;             /* @r{Terminate program, report success.}  */
@}
@end example

@node Compile Example
@section Compiling the Example Program
@cindex compiling
@cindex executable file

To run a C program requires converting the source code into an
@dfn{executable file}.  This is called @dfn{compiling} the program,
and the command to do that using GNU C is @command{gcc}.

This example program consists of a single source file.  If we
call that file @file{fib1.c}, the complete command to compile it is
this:

@example
gcc -g -O -o fib1 fib1.c
@end example

@noindent
Here, @option{-g} says to generate debugging information, @option{-O}
says to optimize at the basic level, and @option{-o fib1} says to put
the executable program in the file @file{fib1}.

To run the program, use its file name as a shell command.
For instance,

@example
./fib1
@end example

@noindent
However, unless you are sure the program is correct, you should
expect to need to debug it.  So use this command,

@example
gdb fib1
@end example

@noindent
which starts the GDB debugger (@pxref{Sample Session, Sample Session,
A Sample GDB Session, gdb, Debugging with GDB}) so you can run and
debug the executable program @code{fib1}.

Richard Stallman's advice, from personal experience, is to turn to the
debugger as soon as you can reproduce the problem.  Don't try to avoid
it by using other methods instead---occasionally they are shortcuts,
but usually they waste an unbounded amount of time.  With the
debugger, you will surely find the bug in a reasonable time; overall,
you will get your work done faster.  The sooner you get serious and
start the debugger, the sooner you are likely to find the bug.

@xref{Compilation}, for an introduction to compiling more complex
programs which consist of more than one source file.

@node Storage
@chapter Storage and Data
@cindex bytes
@cindex storage organization
@cindex memory organization

Storage in C programs is made up of units called @dfn{bytes}.  A byte
is the smallest unit of storage that can be used in a first-class
manner.

On nearly all computers, a byte consists of 8 bits.  There are a few
peculiar computers (mostly ``embedded controllers'' for very small
systems) where a byte is longer than that, but this manual does not
try to explain the peculiarity of those computers; we assume that a
byte is 8 bits.

Every C data type is made up of a certain number of bytes; that number
is the data type's @dfn{size}.  @xref{Type Size}, for details.  The
types @code{signed char} and @code{unsigned char} are one byte long;
use those types to operate on data byte by byte.  @xref{Signed and
Unsigned Types}.  You can refer to a series of consecutive bytes as an
array of @code{char} elements; that's what a character string looks
like in memory.  @xref{String Constants}.

@node Beyond Integers
@chapter Beyond Integers

So far we've presented programs that operate on integers.  In this
chapter we'll present examples of handling non-integral numbers and
arrays of numbers.

@menu
* Float Example::       A function that uses floating-point numbers.
* Array Example::       A function that works with arrays.
* Array Example Call::  How to call that function.
* Array Example Variations::   Different ways to write the call example.
@end menu

@node Float Example
@section An Example with Non-Integer Numbers
@cindex floating point example

Here's a function that operates on and returns @dfn{floating point}
numbers that don't have to be integers.  Floating point represents a
number as a fraction together with a power of 2.  (For more detail,
@pxref{Floating-Point Data Types}.)  This example calculates the
average of three floating point numbers that are passed to it as
arguments:

@example
double
average_of_three (double a, double b, double c)
@{
  return (a + b + c) / 3;
@}
@end example

The values of the parameter @var{a}, @var{b} and @var{c} do not have to be
integers, and even when they happen to be integers, most likely their
average is not an integer.

@code{double} is the usual data type in C for calculations on
floating-point numbers.

To print a @code{double} with @code{printf}, we must use @samp{%f}
instead of @samp{%d}:

@example
printf ("Average is %f\n",
        average_of_three (1.1, 9.8, 3.62));
@end example

The code that calls @code{printf} must pass a @code{double} for
printing with @samp{%f} and an @code{int} for printing with @samp{%d}.
If the argument has the wrong type, @code{printf} will produce meaningless
output.

Here's a complete program that computes the average of three
specific numbers and prints the result:

@example
double
average_of_three (double a, double b, double c)
@{
  return (a + b + c) / 3;
@}

int
main (void)
@{
    printf ("Average is %f\n",
            average_of_three (1.1, 9.8, 3.62));
    return 0;
@}
@end example

From now on we will not present examples of calls to @code{main}.
Instead we encourage you to write them for yourself when you want
to test executing some code.

@node Array Example
@section An Example with Arrays
@cindex array example

A function to take the average of three numbers is very specific and
limited.  A more general function would take the average of any number
of numbers.  That requires passing the numbers in an array.  An array
is an object in memory that contains a series of values of the same
data type.  This chapter presents the basic concepts and use of arrays
through an example; for the full explanation, see @ref{Arrays}.

Here's a function definition to take the average of several
floating-point numbers, passed as type @code{double}.  The first
parameter, @code{length}, specifies how many numbers are passed.  The
second parameter, @code{input_data}, is an array that holds those
numbers.

@example
double
avg_of_double (int length, double input_data[])
@{
  double sum = 0;
  int i;

  for (i = 0; i < length; i++)
    sum = sum + input_data[i];

  return sum / length;
@}
@end example

This introduces the expression to refer to an element of an array:
@code{input_data[i]} means the element at index @code{i} in
@code{input_data}.  The index of the element can be any expression
with an integer value; in this case, the expression is @code{i}.
@xref{Accessing Array Elements}.

@cindex zero-origin indexing
The lowest valid index in an array is 0, @emph{not} 1, and the highest
valid index is one less than the number of elements.  (This is known
as @dfn{zero-origin indexing}.)

This example also introduces the way to declare that a function
parameter is an array.  Such declarations are modeled after the syntax
for an element of the array.  Just as @code{double foo} declares that
@code{foo} is of type @code{double}, @code{double input_data[]}
declares that each element of @code{input_data} is of type
@code{double}.  Therefore, @code{input_data} itself has type ``array
of @code{double}.''

When declaring an array parameter, it's not necessary to say how long
the array is.  In this case, the parameter @code{input_data} has no
length information.  That's why the function needs another parameter,
@code{length}, for the caller to provide that information to the
function @code{avg_of_double}.

@node Array Example Call
@section Calling the Array Example

To call the function @code{avg_of_double} requires making an
array and then passing it as an argument.  Here is an example.

@example
@{
  /* @r{The array of values to compute the average of.}  */
  double nums_to_average[5];
  /* @r{The average, once we compute it.}  */
  double average;

  /* @r{Fill in elements of @code{nums_to_average}.}  */

  nums_to_average[0] = 58.7;
  nums_to_average[1] = 5.1;
  nums_to_average[2] = 7.7;
  nums_to_average[3] = 105.2;
  nums_to_average[4] = -3.14159;

  average = avg_of_double (5, nums_to_average);

  /* @r{@dots{}now make use of @code{average}@dots{}} */
@}
@end example

This shows an array subscripting expression again, this time
on the left side of an assignment, storing a value into an
element of an array.

It also shows how to declare a local variable that is an array:
@code{double nums_to_average[5];}.  Since this declaration allocates the
space for the array, it needs to know the array's length.  You can
specify the length with any expression whose value is an integer, but
in this declaration the length is a constant, the integer 5.

The name of the array, when used by itself as an expression, stands
for the address of the array's data, and that's what gets passed to
the function @code{avg_of_double} in @code{avg_of_double (5,
nums_to_average)}.

We can make the code easier to maintain by avoiding the need to write
5, the array length, when calling @code{avg_of_double}.  That way, if
we change the array to include more elements, we won't have to change
that call.  One way to do this is with the @code{sizeof} operator:

@example
  average = avg_of_double ((sizeof (nums_to_average)
                            / sizeof (nums_to_average[0])),
                           nums_to_average);
@end example

This computes the number of elements in @code{nums_to_average} by dividing
its total size by the size of one element.  @xref{Type Size}, for more
details of using @code{sizeof}.

We don't show in this example what happens after storing the result of
@code{avg_of_double} in the variable @code{average}.  Presumably
more code would follow that uses that result somehow.  (Why compute
the average and not use it?)  But that isn't part of this topic.

@node Array Example Variations
@section Variations for Array Example

The code to call @code{avg_of_double} has two declarations that
start with the same data type:

@example
  /* @r{The array of values to average.}  */
  double nums_to_average[5];
  /* @r{The average, once we compute it.}  */
  double average;
@end example

In C, you can combine the two, like this:

@example
  double nums_to_average[5], average;
@end example

This declares @code{nums_to_average} so each of its elements is a
@code{double}, and @code{average} itself as a
@code{double}.

However, while you @emph{can} combine them, that doesn't mean you
@emph{should}.  If it is useful to write comments about the variables,
and usually it is, then it's clearer to keep the declarations separate
so you can put a comment on each one.  That also helps with using
textual tools to find occurrences of a variable in source files.

We set all of the elements of the array @code{nums_to_average} with
assignments, but it is more convenient to use an initializer in the
declaration:

@example
@{
  /* @r{The array of values to average.}  */
  double nums_to_average[]
    = @{ 58.7, 5.1, 7.7, 105.2, -3.14159 @};

  /* @r{The average, once we compute it.}  */
  average = avg_of_double ((sizeof (nums_to_average)
                            / sizeof (nums_to_average[0])),
                           nums_to_average);

  /* @r{@dots{}now make use of @code{average}@dots{}} */
@}
@end example

The array initializer is a comma-separated list of values, delimited
by braces.  @xref{Initializers}.

Note that the declaration does not specify a size for
@code{nums_to_average}, so the size is determined from the
initializer.  There are five values in the initializer, so
@code{nums_to_average} gets length 5.  If we add another element to
the initializer, @code{nums_to_average} will have six elements.

Because the code computes the number of elements from the size of
the array, using @code{sizeof}, the program will operate on all the
elements in the initializer, regardless of how many those are.

@node Lexical Syntax
@chapter Lexical Syntax
@cindex lexical syntax
@cindex token

To start the full description of the C language, we explain the
lexical syntax and lexical units of C code.  The lexical units of a
programming language are known as @dfn{tokens}.  This chapter covers
all the tokens of C except for constants, which are covered in a later
chapter (@pxref{Constants}).  One vital kind of token is the
@dfn{identifier} (@pxref{Identifiers}), which is used for names of any
kind.

@menu
* English::             Write programs in English!
* Characters::          The characters allowed in C programs.
* Whitespace::          The particulars of whitespace characters.
* Comments::            How to include comments in C code.
* Identifiers::         How to form identifiers (names).
* Operators/Punctuation::  Characters used as operators or punctuation.
* Line Continuation::   Splitting one line into multiple lines.
@end menu

@node English
@section Write Programs in English!

In principle, you can write the function and variable names in a
program, and the comments, in any human language.  C allows any kinds
of Unicode characters in comments, and you can put them into
identifiers with a special prefix (@pxref{Unicode Character Codes}).
However, to enable programmers in all countries to understand and
develop the program, it is best under today's circumstances to write
all identifiers and comments in English.

English is the common language of programmers; in all countries,
programmers generally learn English.  If names and comments in a
program are written in English, most programmers in Bangladesh,
Belgium, Bolivia, Brazil, Bulgaria and Burundi can understand them.
In all those countries, most programmers can speak English, or at least
read it, but they do not read each other's languages at all.  In
India, with so many languages, two programmers may have no common
language other than English.

If you don't feel confident in writing English, do the best you can,
and follow each English comment with a version in a language you
write better; add a note asking others to translate that to English.
Someone will eventually do that.

The program's user interface is a different matter.  We don't need to
choose one language for that; it is easy to support multiple languages
and let each user choose the language for display.  This requires writing
the program to support localization of its interface.  (The
@code{gettext} package exists to support this; @pxref{Message
Translation, The GNU C Library, , libc, The GNU C Library Reference
Manual}.)  Then a community-based translation effort can provide
support for all the languages users want to use.

@node Characters
@section Characters
@cindex character set
@cindex Unicode

@c ??? How to express ¶?

GNU C source files are usually written in the
@url{https://en.wikipedia.org/wiki/ASCII,,ASCII} character set, which
was defined in the 1960s for English.  However, they can also include
Unicode characters represented in the
@url{https://en.wikipedia.org/wiki/UTF-8,,UTF-8} multibyte encoding.
This makes it possible to represent accented letters such as @samp{á},
as well as other scripts such as Arabic, Chinese, Cyrillic, Hebrew,
Japanese, and Korean.@footnote{On some obscure systems, GNU C uses
UTF-EBCDIC instead of UTF-8, but that is not worth describing in this
manual.}

In C source code, non-ASCII characters are valid in comments, in wide
character constants (@pxref{Wide Character Constants}), and in string
constants (@pxref{String Constants}).

@c ??? valid in identifiers?
Another way to specify non-ASCII characters in constants (character or
string) and identifiers is with an escape sequence starting with
backslash, specifying the intended Unicode character.  (@xref{Unicode
Character Codes}.)  This specifies non-ASCII characters without
putting a real non-ASCII character in the source file itself.

C accepts two-character aliases called @dfn{digraphs} for certain
characters.  @xref{Digraphs}.

@node Whitespace
@section Whitespace
@cindex whitespace characters in source files
@cindex space character in source
@cindex tab character in source
@cindex formfeed in source
@cindex linefeed in source
@cindex newline in source
@cindex carriage return in source
@cindex vertical tab in source

Whitespace means characters that exist in a file but appear blank in a
printed listing of a file (or traditionally did appear blank, several
decades ago).  The C language requires whitespace in order to separate
two consecutive identifiers, or to separate an identifier from a
numeric constant.  Other than that, and a few special situations
described later, whitespace is optional; you can put it in when you
wish, to make the code easier to read.

Space and tab in C code are treated as whitespace characters.  So are
line breaks.  You can represent a line break with the newline
character (also called @dfn{linefeed} or LF), CR (carriage return), or
the CRLF sequence (two characters: carriage return followed by a
newline character).

The @dfn{formfeed} character, Control-L, was traditionally used to
divide a file into pages.  It is still used this way in source code,
and the tools that generate nice printouts of source code still start
a new page after each ``formfeed'' character.  Dividing code into
pages separated by formfeed characters is a good way to break it up
into comprehensible pieces and show other programmers where they start
and end.

The @dfn{vertical tab} character, Control-K, was traditionally used to
make printing advance down to the next section of a page.  We know of
no particular reason to use it in source code, but it is still
accepted as whitespace in C.

Comments are also syntactically equivalent to whitespace.
@ifinfo
@xref{Comments}.
@end ifinfo

@node Comments
@section Comments
@cindex comments

A comment encapsulates text that has no effect on the program's
execution or meaning.

The purpose of comments is to explain the code to people that read it.
Writing good comments for your code is tremendously important---they
should provide background information that helps programmers
understand the reasons why the code is written the way it is.  You,
returning to the code six months from now, will need the help of these
comments to remember why you wrote it this way.

Outdated comments that become incorrect are counterproductive, so part
of the software developer's responsibility is to update comments as
needed to correspond with changes to the program code.

C allows two kinds of comment syntax, the traditional style and the
C@t{++} style.  A traditional C comment starts with @samp{/*} and ends
with @samp{*/}.  For instance,

@example
/* @r{This is a comment in traditional C syntax.} */
@end example

A traditional comment can contain @samp{/*}, but these delimiters do
not nest as pairs.  The first @samp{*/} ends the comment regardless of
whether it contains @samp{/*} sequences.

@example
/* @r{This} /* @r{is a comment} */ But this is not! */
@end example

A @dfn{line comment} starts with @samp{//} and ends at the end of the line.
For instance,

@example
// @r{This is a comment in C@t{++} style.}
@end example

Line comments do nest, in effect, because @samp{//} inside a line
comment is part of that comment:

@example
// @r{this whole line is} // @r{one comment}
This is code, not comment.
@end example

It is safe to put line comments inside block comments, or vice versa.

@example
@group
/* @r{traditional comment}
   // @r{contains line comment}
   @r{more traditional comment}
 */ text here is not a comment

// @r{line comment} /* @r{contains traditional comment} */
@end group
@end example

But beware of commenting out one end of a traditional comment with a line
comment.  The delimiter @samp{/*} doesn't start a comment if it occurs
inside an already-started comment.

@example
@group
 // @r{line comment}  /* @r{That would ordinarily begin a block comment.}
    Oops! The line comment has ended;
    this isn't a comment any more.  */
@end group
@end example

Comments are not recognized within string constants.  @t{@w{"/* blah
*/"}} is the string constant @samp{@w{/* blah */}}, not an empty
string.

In this manual we show the text in comments in a variable-width font,
for readability, but this font distinction does not exist in source
files.

A comment is syntactically equivalent to whitespace, so it always
separates tokens.  Thus,

@example
@group
  int/* @r{comment} */foo;
@r{is equivalent to}
  int foo;
@end group
@end example

@noindent
but clean code always uses real whitespace to separate the comment
visually from surrounding code.

@node Identifiers
@section Identifiers
@cindex identifiers

An @dfn{identifier} (name) in C is a sequence of letters and digits,
as well as @samp{_}, that does not start with a digit.  Most C compilers
also allow @samp{$}; GNU C allows it.  An identifier can be as long as
you like; for example,

@example
int anti_dis_establishment_arian_ism;
@end example

@cindex case of letters in identifiers
Letters in identifiers are case-sensitive in C; thus, @code{a}
and @code{A} are two different identifiers.

@cindex keyword
@cindex reserved words
Identifiers in C are used as variable names, function names, typedef
names, enumeration constants, type tags, field names, and labels.
Certain identifiers in C are @dfn{keywords}, which means they have
specific syntactic meanings.  Keywords in C are @dfn{reserved words},
meaning you cannot use them in any other way.  For instance, you can't
define a variable or function named @code{return} or @code{if}.

You can also include other characters, even non-ASCII characters, in
identifiers by writing their Unicode character names, which start with
@samp{\u} or @samp{\U}, in the identifier name.  @xref{Unicode
Character Codes}.  However, it is usually a bad idea to use non-ASCII
characters in identifiers, and when the names are written in English,
they never need non-ASCII characters.  @xref{English}.

As stated above, whitespace is required to separate two consecutive
identifiers, or to separate an identifier from a preceding or
following numeric constant.

@node Operators/Punctuation
@section Operators and Punctuation
@cindex operators
@cindex punctuation

Here we describe the lexical syntax of operators and punctuation in C.
The specific operators of C and their meanings are presented in
subsequent chapters.

Some characters that are generally considered punctuation have a
different sort of meaning in the C language.  C uses double-quote
@samp{"} to delimit string constants (@pxref{String Constants}) and
@samp{'} to delimit character constants (@pxref{Character Constants}).
The characters @samp{$} and @samp{_} can be part of an identifier or a
keyword.

Most operators in C consist of one or two characters that can't be
used in identifiers.  The characters used for such operators in C are
@samp{!~^&|*/%+-=<>,.?:}.  (C preprocessing uses @dfn{preprocessing
operators}, based on @samp{#}, which are entirely different from
these operators; @ref{Preprocessing}.)

Some operators are a single character.  For instance, @samp{-} is the
operator for negation (with one operand) and the operator for
subtraction (with two operands).

Some operators are two characters.  For example, @samp{++} is the
increment operator.  Recognition of multicharacter operators works by
reading and grouping as many successive characters as can
constitute one operator, and making them one token.

For instance, the character sequence @samp{++} is always interpreted
as the increment operator; therefore, if we want to write two
consecutive instances of the operator @samp{+}, we must separate them
with a space so that they do not combine as one token.  Applying the
same rule, @code{a+++++b} is always tokenized as @code{@w{a++ ++ +
b}}, not as @code{@w{a++ + ++b}}, even though the latter could be part
of a valid C program and the former could not (since @code{a++}
is not an lvalue and thus can't be the operand of @code{++}).

A few C operators are keywords rather than special characters.  They
include @code{sizeof} (@pxref{Type Size}) and @code{_Alignof}
(@pxref{Type Alignment}).

The characters @samp{;@{@}[]()} are used for punctuation and grouping.
Semicolon (@samp{;}) ends a statement.  Braces (@samp{@{} and
@samp{@}}) begin and end a block at the statement level
(@pxref{Blocks}), and surround the initializer (@pxref{Initializers})
for a variable with multiple elements or fields (such as arrays or
structures).

Square brackets (@samp{[} and @samp{]}) do array indexing, as in
@code{array[5]}.

Parentheses are used in expressions for explicit nesting of
expressions (@pxref{Basic Arithmetic}), around the parameter
declarations in a function declaration or definition, and around the
arguments in a function call, as in @code{printf ("Foo %d\n", i)}
(@pxref{Function Calls}).  Several kinds of statements also use
parentheses as part of their syntax---for instance, @code{if}
statements, @code{for} statements, @code{while} statements, and
@code{switch} statements.  @xref{if Statement}, and following
sections.

Parentheses are also required around the operand of the operator
keywords @code{sizeof} and @code{_Alignof} when the operand is a data
type rather than a value.  @xref{Type Size}.

@node Line Continuation
@section Line Continuation
@cindex line continuation
@cindex continuation of lines

The sequence of a backslash and a newline is ignored absolutely
anywhere in a C program.  This makes it possible to split a single
source line into multiple lines in the source file.  GNU C tolerates
and ignores other whitespace between the backslash and the newline.
In particular, it always ignores a CR (carriage return) character
there, in case some text editor decided to end the line with the CRLF
sequence.

The main use of line continuation in C is for macro definitions that
would be inconveniently long for a single line (@pxref{Macros}).

It is possible to continue a line comment onto another line with
backslash-newline.  You can put backslash-newline in the middle of an
identifier, even a keyword, or an operator.  You can even split
@samp{/*}, @samp{*/}, and @samp{//} onto multiple lines with
backslash-newline.  Here's an ugly example:

@example
@group
/\
*
*/ fo\
o +\
= 1\
0;
@end group
@end example

@noindent
That's equivalent to @samp{/* */ foo += 10;}.

Don't do those things in real programs, since they make code hard to
read.

@strong{Note:} For the sake of using certain tools on the source code, it is
wise to end every source file with a newline character which is not
preceded by a backslash, so that it really ends the last line.

@node Arithmetic
@chapter Arithmetic
@cindex arithmetic operators
@cindex operators, arithmetic

@c ??? Duplication with other sections -- get rid of that?

Arithmetic operators in C attempt to be as similar as possible to the
abstract arithmetic operations, but it is impossible to do this
perfectly.  Numbers in a computer have a finite range of possible
values, and non-integer values have a limit on their possible
accuracy.  Nonetheless, except when results are out of range, you will
encounter no surprises in using @samp{+} for addition, @samp{-} for
subtraction, and @samp{*} for multiplication.

Each C operator has a @dfn{precedence}, which is its rank in the
grammatical order of the various operators.  The operators with the
highest precedence grab adjoining operands first; these expressions
then become operands for operators of lower precedence.  We give some
information about precedence of operators in this chapter where we
describe the operators; for the full explanation, see @ref{Binary
Operator Grammar}.

The arithmetic operators always @dfn{promote} their operands before
operating on them.  This means converting narrow integer data types to
a wider data type (@pxref{Operand Promotions}).  If you are just
learning C, don't worry about this yet.

Given two operands that have different types, most arithmetic
operations convert them both to their @dfn{common type}.  For
instance, if one is @code{int} and the other is @code{double}, the
common type is @code{double}.  (That's because @code{double} can
represent all the values that an @code{int} can hold, but not vice
versa.)  For the full details, see @ref{Common Type}.

@menu
* Basic Arithmetic::       Addition, subtraction, multiplication,
                             and division.
* Integer Arithmetic::     How C performs arithmetic with integer values.
* Integer Overflow::       When an integer value exceeds the range
                             of its type.
* Mixed Mode::             Calculating with both integer values
                             and floating-point values.
* Division and Remainder:: How integer division works.
* Numeric Comparisons::    Comparing numeric values for equality or order.
* Shift Operations::       Shift integer bits left or right.
* Bitwise Operations::     Bitwise conjunction, disjunction, negation.
@end menu

@node Basic Arithmetic
@section Basic Arithmetic
@cindex addition operator
@cindex subtraction operator
@cindex multiplication operator
@cindex division operator
@cindex negation operator
@cindex operator, addition
@cindex operator, subtraction
@cindex operator, multiplication
@cindex operator, division
@cindex operator, negation

Basic arithmetic in C is done with the usual binary operators of
algebra: addition (@samp{+}), subtraction (@samp{-}), multiplication
(@samp{*}) and division (@samp{/}).  The unary operator @samp{-} is
used to change the sign of a number.  The unary @code{+} operator also
exists; it yields its operand unaltered.

@samp{/} is the division operator, but dividing integers may not give
the result you expect.  Its value is an integer, which is not equal to
the mathematical quotient when that is a fraction.  Use @samp{%} to
get the corresponding integer remainder when necessary.
@xref{Division and Remainder}.  Floating-point division yields a value
as close as possible to the mathematical quotient.

These operators use algebraic syntax with the usual algebraic
precedence rule (@pxref{Binary Operator Grammar}) that multiplication
and division are done before addition and subtraction, but you can use
parentheses to explicitly specify how the operators nest.  They are
left-associative (@pxref{Associativity and Ordering}).  Thus,

@example
-a + b - c + d * e / f
@end example

@noindent
is equivalent to

@example
(((-a) + b) - c) + ((d * e) / f)
@end example

@node Integer Arithmetic
@section Integer Arithmetic
@cindex integer arithmetic

Each of the basic arithmetic operations in C has two variants for
integers: @dfn{signed} and @dfn{unsigned}.  The choice is determined
by the data types of their operands.

Each integer data type in C is either @dfn{signed} or @dfn{unsigned}.
A signed type can hold a range of positive and negative numbers, with
zero near the middle of the range.  An unsigned type can hold only
nonnegative numbers; its range starts with zero and runs upward.

The most basic integer types are @code{int}, which normally can hold
numbers from @minus{}2,147,483,648 to 2,147,483,647, and @code{unsigned
int}, which normally can hold numbers from 0 to 4,294,967,295.  (This
assumes @code{int} is 32 bits wide, always true for GNU C on real
computers but not always on embedded controllers.)  @xref{Integer
Types}, for full information about integer types.

When a basic arithmetic operation is given two signed operands, it
does signed arithmetic.  Given two unsigned operands, it does
unsigned arithmetic.

If one operand is @code{unsigned int} and the other is @code{int}, the
operator treats them both as unsigned.  More generally, the common
type of the operands determines whether the operation is signed or
not.  @xref{Common Type}.

Printing the results of unsigned arithmetic with @code{printf} using
@samp{%d} can produce surprising results for values far away from
zero.  Even though the rules above say that the computation was done
with unsigned arithmetic, the printed result may appear to be signed!

The explanation is that the bit pattern resulting from addition,
subtraction or multiplication is actually the same for signed and
unsigned operations.  The difference is only in the data type of the
result, which affects the @emph{interpretation} of the result bit pattern,
and whether the arithmetic operation can overflow (see the next section).

But @samp{%d} doesn't know its argument's data type.  It sees only the
value's bit pattern, and it is defined to interpret that as
@code{signed int}.  To print it as unsigned requires using @samp{%u}
instead of @samp{%d}.  @xref{Formatted Output, The GNU C Library, ,
libc, The GNU C Library Reference Manual}.

Arithmetic in C never operates directly on narrow integer types (those
with fewer bits than @code{int}; @ref{Narrow Integers}).  Instead it
``promotes'' them to @code{int}.  @xref{Operand Promotions}.

@node Integer Overflow
@section Integer Overflow
@cindex integer overflow
@cindex overflow, integer

When the mathematical value of an arithmetic operation doesn't fit in
the range of the data type in use, that's called @dfn{overflow}.
When it happens in integer arithmetic, it is @dfn{integer overflow}.

Integer overflow happens only in arithmetic operations.  Type conversion
operations, by definition, do not cause overflow, not even when the
result can't fit in its new type.  @xref{Integer Conversion}.

Signed numbers use two's-complement representation, in which the most
negative number lacks a positive counterpart (@pxref{Integers in
Depth}).  Thus, the unary @samp{-} operator on a signed integer can
overflow.

@menu
* Unsigned Overflow::           Overflow in unsigned integer arithmetic.
* Signed Overflow::             Overflow in signed integer arithmetic.
@end menu

@node Unsigned Overflow
@subsection Overflow with Unsigned Integers

Unsigned arithmetic in C ignores overflow; it produces the true result
modulo the @var{n}th power of 2, where @var{n} is the number of bits
in the data type.  We say it ``truncates'' the true result to the
lowest @var{n} bits.

A true result that is negative, when taken modulo the @var{n}th power
of 2, yields a positive number.  For instance,

@example
unsigned int x = 1;
unsigned int y;

y = -x;
@end example

@noindent
causes overflow because the negative number @minus{}1 can't be stored
in an unsigned type.  The actual result, which is @minus{}1 modulo the
@var{n}th power of 2, is one less than the @var{n}th power of 2.  That
is the largest value that the unsigned data type can store.  For a
32-bit @code{unsigned int}, the value is 4,294,967,295.  @xref{Maximum
and Minimum Values}.

Adding that number to itself, as here,

@example
unsigned int z;

z = y + y;
@end example

@noindent
ought to yield 8,489,934,590; however, that is again too large to fit,
so overflow truncates the value to 4,294,967,294.  If that were a
signed integer, it would mean @minus{}2, which (not by coincidence)
equals @minus{}1 + @minus{}1.

@node Signed Overflow
@subsection Overflow with Signed Integers
@cindex compiler options for integer overflow
@cindex integer overflow, compiler options
@cindex overflow, compiler options

For signed integers, the result of overflow in C is @emph{in
principle} undefined, meaning that anything whatsoever could happen.
Therefore, C compilers can do optimizations that treat the overflow
case with total unconcern.  (Since the result of overflow is undefined
in principle, one cannot claim that these optimizations are
erroneous.)

@strong{Watch out:} These optimizations can do surprising things.  For
instance,

@example
int i;
@r{@dots{}}
if (i < i + 1)
  x = 5;
@end example

@noindent
could be optimized to do the assignment unconditionally, because the
@code{if}-condition is always true if @code{i + 1} does not overflow.

GCC offers compiler options to control handling signed integer
overflow.  These options operate per module; that is, each module
behaves according to the options it was compiled with.

These two options specify particular ways to handle signed integer
overflow, other than the default way:

@table @option
@item -fwrapv
Make signed integer operations well-defined, like unsigned integer
operations: they produce the @var{n} low-order bits of the true
result.  The highest of those @var{n} bits is the sign bit of the
result.  With @option{-fwrapv}, these out-of-range operations are not
considered overflow, so (strictly speaking) integer overflow never
happens.

The option @option{-fwrapv} enables some optimizations based on the
defined values of out-of-range results.  In GCC 8, it disables
optimizations that are based on assuming signed integer operations
will not overflow.

@item -ftrapv
Generate a signal @code{SIGFPE} when signed integer overflow occurs.
This terminates the program unless the program handles the signal.
@xref{Signals}.
@end table

One other option is useful for finding where overflow occurs:

@ignore
@item -fno-strict-overflow
Disable optimizations that are based on assuming signed integer
operations will not overflow.
@end ignore

@table @option
@item -fsanitize=signed-integer-overflow
Output a warning message at run time when signed integer overflow
occurs.  This checks the @samp{+}, @samp{*}, and @samp{-} operators.
This takes priority over @option{-ftrapv}.
@end table

@node Mixed Mode
@section Mixed-Mode Arithmetic

Mixing integers and floating-point numbers in a basic arithmetic
operation converts the integers automatically to floating point.
In most cases, this gives exactly the desired results.
But sometimes it matters precisely where the conversion occurs.

If @code{i} and @code{j} are integers, @code{(i + j) * 2.0} adds them
as an integer, then converts the sum to floating point for the
multiplication.  If the addition causes an overflow, that is not
equivalent to converting each integer to floating point and then
adding the two floating point numbers.  You can get the latter result
by explicitly converting the integers, as in @code{((double) i +
(double) j) * 2.0}.  @xref{Explicit Type Conversion}.

@c Eggert's report
Adding or multiplying several values, including some integers and some
floating point, performs the operations left to right.  Thus, @code{3.0 +
i + j} converts @code{i} to floating point, then adds 3.0, then
converts @code{j} to floating point and adds that.  You can specify a
different order using parentheses: @code{3.0 + (i + j)} adds @code{i}
and @code{j} first and then adds that sum (converted to floating
point) to 3.0.  In this respect, C differs from other languages, such
as Fortran.

@node Division and Remainder
@section Division and Remainder
@cindex remainder operator
@cindex modulus
@cindex operator, remainder

Division of integers in C rounds the result to an integer.  The result
is always rounded towards zero.

@example
 16 / 3  @result{} 5
-16 / 3  @result{} -5
 16 / -3 @result{} -5
-16 / -3 @result{} 5
@end example

@noindent
To get the corresponding remainder, use the @samp{%} operator:

@example
 16 % 3  @result{} 1
-16 % 3  @result{} -1
 16 % -3 @result{} 1
-16 % -3 @result{} -1
@end example

@noindent
@samp{%} has the same operator precedence as @samp{/} and @samp{*}.

From the rounded quotient and the remainder, you can reconstruct
the dividend, like this:

@example
int
original_dividend (int divisor, int quotient, int remainder)
@{
  return divisor * quotient + remainder;
@}
@end example

To do unrounded division, use floating point.  If only one operand is
floating point, @samp{/} converts the other operand to floating
point.

@example
16.0 / 3   @result{} 5.333333333333333
16   / 3.0 @result{} 5.333333333333333
16.0 / 3.0 @result{} 5.333333333333333
16   / 3   @result{} 5
@end example

The remainder operator @samp{%} is not allowed for floating-point
operands, because it is not needed.  The concept of remainder makes
sense for integers because the result of division of integers has to
be an integer.  For floating point, the result of division is a
floating-point number, in other words a fraction, which will differ
from the exact result only by a very small amount.

There are functions in the standard C library to calculate remainders
from integral-values division of floating-point numbers.
@xref{Remainder Functions, The GNU C Library, , libc, The GNU C Library
Reference Manual}.

Integer division overflows in one specific case: dividing the smallest
negative value for the data type (@pxref{Maximum and Minimum Values})
by @minus{}1.  That's because the correct result, which is the
corresponding positive number, does not fit (@pxref{Integer Overflow})
in the same number of bits.  On some computers now in use, this always
causes a signal @code{SIGFPE} (@pxref{Signals}), the same behavior
that the option @option{-ftrapv} specifies (@pxref{Signed Overflow}).

Division by zero leads to unpredictable results---depending on the
type of computer, it might cause a signal @code{SIGFPE}, or it might
produce a numeric result.

@cindex division by zero
@cindex zero, division by
@strong{Watch out:} Make sure the program does not divide by zero.  If
you can't prove that the divisor is not zero, test whether it is zero,
and skip the division if so.

@node Numeric Comparisons
@section Numeric Comparisons
@cindex numeric comparisons
@cindex comparisons
@cindex operators, comparison
@cindex equal operator
@cindex not-equal operator
@cindex less-than operator
@cindex greater-than operator
@cindex less-or-equal operator
@cindex greater-or-equal operator
@cindex operator, equal
@cindex operator, not-equal
@cindex operator, less-than
@cindex operator, greater-than
@cindex operator, less-or-equal
@cindex operator, greater-or-equal
@cindex truth value

There are two kinds of comparison operators: @dfn{equality} and
@dfn{ordering}.  Equality comparisons test whether two expressions
have the same value.  The result is a @dfn{truth value}: a number that
is 1 for ``true'' and 0 for ``false.''

@example
a == b   /* @r{Test for equal.}  */
a != b   /* @r{Test for not equal.}  */
@end example

The equality comparison is written @code{==} because plain @code{=}
is the assignment operator.

Ordering comparisons test which operand is greater or less.  Their
results are truth values.  These are the ordering comparisons of C:

@example
a < b   /* @r{Test for less-than.}  */
a > b   /* @r{Test for greater-than.}  */
a <= b  /* @r{Test for less-than-or-equal.}  */
a >= b  /* @r{Test for greater-than-or-equal.}  */
@end example

For any integers @code{a} and @code{b}, exactly one of the comparisons
@code{a < b}, @code{a == b} and @code{a > b} is true, just as in
mathematics.  However, if @code{a} and @code{b} are special floating
point values (not ordinary numbers), all three can be false.
@xref{Special Float Values}, and @ref{Invalid Optimizations}.

@node Shift Operations
@section Shift Operations
@cindex shift operators
@cindex operators, shift
@cindex operators, shift
@cindex shift count

@dfn{Shifting} an integer means moving the bit values to the left or
right within the bits of the data type.  Shifting is defined only for
integers.  Here's the way to write it:

@example
/* @r{Left shift.}  */
5 << 2 @result{} 20

/* @r{Right shift.}  */
5 >> 2 @result{} 1
@end example

@noindent
The left operand is the value to be shifted, and the right operand
says how many bits to shift it (the @dfn{shift count}).  The left
operand is promoted (@pxref{Operand Promotions}), so shifting never
operates on a narrow integer type; it's always either @code{int} or
wider.  The result of the shift operation has the same type as the
promoted left operand.

The examples in this section use binary constants, starting with
@samp{0b} (@pxref{Integer Constants}).  They stand for 32-bit integers
of type @code{int}.

@menu
* Bits Shifted In::     How shifting makes new bits to shift in.
* Shift Caveats::       Caveats of shift operations.
* Shift Hacks::         Clever tricks with shift operations.
@end menu

@node Bits Shifted In
@subsection Shifting Makes New Bits

A shift operation shifts towards one end of the number and has to
generate new bits at the other end.

Shifting left one bit must generate a new least significant bit.  It
always brings in zero there.  It is equivalent to multiplying by the
appropriate power of 2.  For example,

@example
5 << 3     @r{is equivalent to}   5 * 2*2*2
-10 << 4   @r{is equivalent to}   -10 * 2*2*2*2
@end example

The meaning of shifting right depends on whether the data type is
signed or unsigned (@pxref{Signed and Unsigned Types}).  For a signed
data type, GNU C performs ``arithmetic shift,'' which keeps the number's
sign unchanged by duplicating the sign bit.  For an unsigned data
type, it performs ``logical shift,'' which always shifts in zeros at
the most significant bit.

In both cases, shifting right one bit is division by two, rounding
towards negative infinity.  For example,

@example
(unsigned) 19 >> 2 @result{} 4
(unsigned) 20 >> 2 @result{} 5
(unsigned) 21 >> 2 @result{} 5
@end example

For a negative left operand @code{a}, @code{a >> 1} is not equivalent
to @code{a / 2}.  Both operations divide by 2, but @samp{/} rounds
toward zero.

The shift count must be zero or greater.  Shifting by a negative
number of bits gives machine-dependent results.

@node Shift Caveats
@subsection Caveats for Shift Operations

@strong{Warning:} If the shift count is greater than or equal to the
width in bits of the promoted first operand, the results are
machine-dependent.  Logically speaking, the ``correct'' value would be
either @minus{}1 (for right shift of a negative number) or 0 (in all other
cases), but the actual result is whatever the machine's shift
instruction does in that case.  So unless you can prove that the
second operand is not too large, write code to check it at run time.

@strong{Warning:} Never rely on how the shift operators relate in
precedence to other arithmetic binary operators.  Programmers don't
remember these precedences, and won't understand the code.  Always use
parentheses to explicitly specify the nesting, like this:

@example
a + (b << 5)   /* @r{Shift first, then add.}  */
(a + b) << 5   /* @r{Add first, then shift.}  */
@end example

Note: according to the C standard, shifting of signed values isn't
guaranteed to work properly when the value shifted is negative, or
becomes negative during shifting.  However, only pedants have a reason
to be concerned about this; only computers with strange shift
instructions could plausibly do this wrong.  In GNU C, the operation
always works as expected.

@node Shift Hacks
@subsection Shift Hacks

You can use the shift operators for various useful hacks.  For
example, given a date specified by day of the month @code{d}, month
@code{m}, and year @code{y}, you can store the entire date in a single
integer @code{date}:

The examples in this section use binary constants, starting with
@samp{0b} (@pxref{Integer Constants}).  They stand for 32-bit integers
of type @code{int}.

@example
unsigned int d = 12;		/* @r{12 in binary is 0b1100.}  */
unsigned int m = 6;             /* @r{6 in binary is 0b110.}  */
unsigned int y = 1983;          /* @r{1983 in binary is 0b11110111111.}  */
unsigned int date = (((y << 4) + m) << 5) + d;
                                /* @r{Add 0b11110111111000000000}
                                   @r{and 0b11000000 and 0b1100.}
                                   @r{Sum is 0b11110111111011001100.}  */
@end example

@noindent
To extract the day, month, and year out of
@code{date}, use a combination of shift and remainder:

@example
/* @r{32 in binary is 0b100000.}  */
/* @r{Remainder dividing by 32 gives lowest 5 bits, 0b1100.}  */
d = date % 32;
/* @r{Shifting 5 bits right discards the day, leaving 0b111101111110110.}
   @r{Remainder dividing by 16 gives lowest remaining 4 bits, 0b110.}  */
m = (date >> 5) % 16;
/* @r{Shifting 9 bits right discards day and month,}
   @r{leaving 0b11110111111.}  */
y = date >> 9;
@end example

@code{-1 << LOWBITS} is a clever way to make an integer whose
@code{LOWBITS} lowest bits are all 0 and the rest are all 1.
@code{-(1 << LOWBITS)} is equivalent to that, since negating a value
is equivalent to multiplying it by @minus{}1.

@node Bitwise Operations
@section Bitwise Operations
@cindex bitwise operators
@cindex operators, bitwise
@cindex negation, bitwise
@cindex conjunction, bitwise
@cindex disjunction, bitwise

Bitwise operators operate on integers, treating each bit independently.
They are not allowed for floating-point types.

As in the previous section, the examples in this section use binary
constants, starting with @samp{0b} (@pxref{Integer Constants}).  They
stand for 32-bit integers of type @code{int}.

@table @code
@item ~@code{a}
Unary operator for bitwise negation; this changes each bit of
@code{a} from 1 to 0 or from 0 to 1.

@example
~0b10101000 @result{} 0b11111111111111111111111101010111
~0 @result{} 0b11111111111111111111111111111111
~0b11111111111111111111111111111111 @result{} 0
~ (-1) @result{} 0
@end example

It is useful to remember that @code{~@var{x} + 1} equals
@code{-@var{x}}, for integers, and @code{~@var{x}} equals
@code{-@var{x} - 1}.  The last example above shows this with @minus{}1
as @var{x}.

@item @code{a} & @code{b}
Binary operator for bitwise ``and'' or ``conjunction.''  Each bit in
the result is 1 if that bit is 1 in both @code{a} and @code{b}.

@example
0b10101010 & 0b11001100 @result{} 0b10001000
@end example

@item @code{a} | @code{b}
Binary operator for bitwise ``or'' (``inclusive or'' or
``disjunction'').  Each bit in the result is 1 if that bit is 1 in
either @code{a} or @code{b}.

@example
0b10101010 | 0b11001100 @result{} 0b11101110
@end example

@item @code{a} ^ @code{b}
Binary operator for bitwise ``xor'' (``exclusive or'').  Each bit in
the result is 1 if that bit is 1 in exactly one of @code{a} and @code{b}.

@example
0b10101010 ^ 0b11001100 @result{} 0b01100110
@end example
@end table

To understand the effect of these operators on signed integers, keep
in mind that all modern computers use two's-complement representation
(@pxref{Integer Representations}) for negative integers.  This means
that the highest bit of the number indicates the sign; it is 1 for a
negative number and 0 for a positive number.  In a negative number,
the value in the other bits @emph{increases} as the number gets closer
to zero, so that @code{0b111@r{@dots{}}111} is @minus{}1 and
@code{0b100@r{@dots{}}000} is the most negative possible integer.

@strong{Warning:} C defines a precedence ordering for the bitwise
binary operators, but you should never rely on it.  Likewise, you
should never rely on how bitwise binary operators relate in precedence
to the arithmetic and shift binary operators.  Other programmers don't
remember these aspects of C's precedence ordering; to make your
programs clear, always use parentheses to explicitly specify the
nesting among these operators.

For example, suppose @code{offset} is an integer that specifies
the offset within shared memory of a table, except that its bottom few
bits (@code{LOWBITS} says how many) are special flags.  Here's
how to get just that offset and add it to the base address.

@example
shared_mem_base + (offset & (-1 << LOWBITS))
@end example

Thanks to the outer set of parentheses, we don't need to know whether
@samp{&} has higher precedence than @samp{+}.  Thanks to the inner
set, we don't need to know whether @samp{&} has higher precedence than
@samp{<<}.  But we can rely on all unary operators to have higher
precedence than any binary operator, so we don't need parentheses
around the left operand of @samp{<<}.

@node Assignment Expressions
@chapter Assignment Expressions
@cindex assignment expressions
@cindex operators, assignment

As a general concept in programming, an @dfn{assignment} is a
construct that stores a new value into a place where values can be
stored---for instance, in a variable.  Such places are called
@dfn{lvalues} (@pxref{Lvalues}) because they are locations that hold a value.

In C, an assignment is an expression because it has a value; we call
it an @dfn{assignment expression}.  A simple assignment looks like

@example
@var{lvalue} = @var{value-to-store}
@end example

@noindent
We say it assigns the value of the expression @var{value-to-store} to
the location @var{lvalue}, or that it stores @var{value-to-store}
there.  You can think of the ``l'' in ``lvalue'' as standing for
``left,'' since that's what you put on the left side of the assignment
operator.

However, that's not the only way to use an lvalue, and not all lvalues
can be assigned to.  To use the lvalue in the left side of an
assignment, it has to be @dfn{modifiable}.  In C, that means it was
not declared with the type qualifier @code{const} (@pxref{const}).

The value of the assignment expression is that of @var{lvalue} after
the new value is stored in it.  This means you can use an assignment
inside other expressions.  Assignment operators are right-associative
so that

@example
x = y = z = 0;
@end example

@noindent
is equivalent to

@example
x = (y = (z = 0));
@end example

This is the only useful way for them to associate;
the other way,

@example
((x = y) = z) = 0;
@end example

@noindent
would be invalid since an assignment expression such as @code{x = y}
is not a valid lvalue.

@strong{Warning:} Write parentheses around an assignment if you nest
it inside another expression, unless that containing expression is a
comma-separated series or another assignment.  For example,
see @ref{Logicals and Assignments}, and @ref{Uses of Comma}.

@menu
* Simple Assignment::            The basics of storing a value.
* Lvalues::                      Expressions into which a value can be stored.
* Modifying Assignment::         Shorthand for changing an lvalue's contents.
* Increment/Decrement::          Shorthand for incrementing and decrementing
                                   an lvalue's contents.
* Postincrement/Postdecrement::  Accessing then incrementing or decrementing.
* Assignment in Subexpressions:: How to avoid ambiguity.
* Write Assignments Separately:: Write assignments as separate statements.
@end menu

@node Simple Assignment
@section Simple Assignment
@cindex simple assignment
@cindex assignment, simple

A @dfn{simple assignment expression} computes the value of the right
operand and stores it into the lvalue on the left.  Here is a simple
assignment expression that stores 5 in @code{i}:

@example
i = 5
@end example

@noindent
We say that this is an @dfn{assignment to} the variable @code{i} and
that it @dfn{assigns} @code{i} the value 5.  It has no semicolon
because it is an expression (so it has a value).  Adding a semicolon
at the end would make it a statement (@pxref{Expression Statement}).

Here is another example of a simple assignment expression.  Its
operands are not simple, but the kind of assignment done here is
simple assignment.

@example
x[foo ()] = y + 6
@end example

A simple assignment with two different numeric data types converts the
right operand value to the lvalue's type, if possible.  It can convert
any numeric type to any other numeric type.

Simple assignment is also allowed on some non-numeric types: pointers
(@pxref{Pointers}), structures (@pxref{Structure Assignment}), and
unions (@pxref{Unions}).

@strong{Warning:} Assignment is not allowed on arrays because
there are no array values in C; C variables can be arrays, but these
arrays cannot be manipulated as wholes.  @xref{Limitations of C
Arrays}.

@xref{Assignment Type Conversions}, for the complete rules about data
types used in assignments.

@node Lvalues
@section Lvalues
@cindex lvalues

An expression that identifies a memory space that holds a value is
called an @dfn{lvalue}, because it is a location that can hold a value.

The standard kinds of lvalues are:

@itemize @bullet
@item
A variable.

@item
A pointer-dereference expression (@pxref{Pointer Dereference}) using
unary @samp{*}, if its type is not a function type.

@item
A structure field reference (@pxref{Structures}) using @samp{.}, if
the structure value is an lvalue.

@item
A structure field reference using @samp{->}.  This is always an lvalue
since @samp{->} implies pointer dereference.

@item
A union alternative reference (@pxref{Unions}), on the same conditions
as for structure fields.

@item
An array-element reference using @samp{[@r{@dots{}}]}, if the array
is an lvalue.

@item
A string constant (@pxref{String Constants}).

@item
An array constructor (@pxref{Constructing Array Values}).

@item
A structure or union constructor (@pxref{Structure Constructors}).
@end itemize

If an expression's outermost operation is any other operator, that
expression is not an lvalue.  Thus, the variable @code{x} is an
lvalue, but @code{x + 0} is not, even though these two expressions
compute the same value (assuming @code{x} is a number).

It is rare that a structure value or an array value is not an lvalue,
but that does happen---for instance, the result of a function call or
a conditional operator can have a structure or array type, but is
never an lvalue.

If an array is an lvalue, using the array in an expression still
converts it automatically to a pointer to the zeroth element.  The
result of this conversion is not an lvalue.  Thus, if the variable
@code{a} is an array, you can't use @code{a} by itself as the left
operand of an assignment.  But you can assign to an element of
@code{a}, such as @code{a[0]}.  That is an lvalue since @code{a} is an
lvalue.

@node Modifying Assignment
@section Modifying Assignment
@cindex modifying assignment
@cindex assignment, modifying

You can abbreviate the common construct

@example
@var{lvalue} = @var{lvalue} + @var{expression}
@end example

@noindent
as

@example
@var{lvalue} += @var{expression}
@end example

This is known as a @dfn{modifying assignment}.  For instance,

@example
i = i + 5;
i += 5;
@end example

@noindent
shows two statements that are equivalent.  The first uses
simple assignment; the second uses modifying assignment.

Modifying assignment works with any binary arithmetic operator.  For
instance, you can subtract something from an lvalue like this,

@example
@var{lvalue} -= @var{expression}
@end example

@noindent
or multiply it by a certain amount like this,

@example
@var{lvalue} *= @var{expression}
@end example

@noindent
or shift it by a certain amount like this.

@example
@var{lvalue} <<= @var{expression}
@var{lvalue} >>= @var{expression}
@end example

In most cases, this feature adds no power to the language, but it
provides substantial convenience.  Also, when @var{lvalue} contains
code that has side effects, the simple assignment performs those side
effects twice, while the modifying assignment performs them once.  For
instance, suppose that the function @code{foo} has a side effect, perhaps
changing static storage.  This statement

@example
x[foo ()] = x[foo ()] + 5;
@end example

@noindent
calls @code{foo} twice.  If @code{foo} operates on static variables,
it could return a different value each time.  If @code{foo ()} will
return 1 the first time and 3 the second time, the effect could be to
add @code{x[3]} and 5 and store the result in @code{x[1]}, or to add
@code{x[1]} and 5 and store the result in @code{x[3]}.  We don't know
which of the two it will do, because C does not specify which call to
@code{foo} is computed first.

Such a statement is not well defined, and shouldn't be used.

By contrast,

@example
x[foo ()] += 5;
@end example

@noindent
is well defined: it calls @code{foo} only once to determine which
element of @code{x} to adjust, and it adjusts that element by adding 5
to it.

@node Increment/Decrement
@section Increment and Decrement Operators
@cindex increment operator
@cindex decrement operator
@cindex operator, increment
@cindex operator, decrement
@cindex preincrement expression
@cindex predecrement expression

The operators @samp{++} and @samp{--} are the @dfn{increment} and
@dfn{decrement} operators.  When used on a numeric value, they add or
subtract 1.  We don't consider them assignments, but they are
equivalent to assignments.

Using @samp{++} or @samp{--} as a prefix, before an lvalue, is called
@dfn{preincrement} or @dfn{predecrement}.  This adds or subtracts 1
and the result becomes the expression's value.  For instance,

@example
#include <stdio.h>   /* @r{Declares @code{printf}.} */

int
main (void)
@{
  int i = 5;
  printf ("%d\n", i);
  printf ("%d\n", ++i);
  printf ("%d\n", i);
  return 0;
@}
@end example

@noindent
prints lines containing @samp{5}, @samp{6}, and @samp{6} again.  The
expression @code{++i} increments @code{i} from 5 to 6, and has the
value 6, so the output from @code{printf} on that line says @samp{6}.

Using @samp{--} instead, for predecrement,

@example
#include <stdio.h>   /* @r{Declares @code{printf}.} */

int
main (void)
@{
  int i = 5;
  printf ("%d\n", i);
  printf ("%d\n", --i);
  printf ("%d\n", i);
  return 0;
@}
@end example

@noindent
prints three lines that contain (respectively) @samp{5}, @samp{4}, and
again @samp{4}.

@node Postincrement/Postdecrement
@section Postincrement and Postdecrement
@cindex postincrement expression
@cindex postdecrement expression
@cindex operator, postincrement
@cindex operator, postdecrement

Using @samp{++} or @samp{--} @emph{after} an lvalue does something
peculiar: it gets the value directly out of the lvalue and @emph{then}
increments or decrements it.  Thus, the value of @code{i++} is the same
as the value of @code{i}, but @code{i++} also increments @code{i} ``a
little later.''  This is called @dfn{postincrement} or
@dfn{postdecrement}.

For example,

@example
#include <stdio.h>   /* @r{Declares @code{printf}.} */

int
main (void)
@{
  int i = 5;
  printf ("%d\n", i);
  printf ("%d\n", i++);
  printf ("%d\n", i);
  return 0;
@}
@end example

@noindent
prints lines containing @samp{5}, again @samp{5}, and @samp{6}.  The
expression @code{i++} has the value 5, which is the value of @code{i}
at the time, but it increments @code{i} from 5 to 6 just a little
later.

How much later is ``just a little later''?  The compiler has some
flexibility in deciding that.  The rule is that the increment has to
happen by the next @dfn{sequence point}; in simple cases, that means
by the end of the statement.  @xref{Sequence Points}.

Regardless of precisely where the compiled code increments the value
of @code{i}, the crucial thing is that the value of @code{i++} is the
value that @code{i} has @emph{before} incrementing it.

If a unary operator precedes a postincrement or postdecrement expression,
the post-whatever expression nests inside:

@example
-a++   @r{is equivalent to}   -(a++)
@end example

In some cases, for instance this one, the other order would not even
make sense; @code{-a} is not an lvalue, so it can't be incremented.

The most common use of postincrement is with arrays.  Here's an
example of using postincrement to access one element of an array and
advance the index for the next access.  Compare this with the example
@code{avg_of_double} (@pxref{Array Example}), which is almost the same
but doesn't use postincrement for that.

@example
double
avg_of_double_alt (int length, double input_data[])
@{
  double sum = 0;
  int i;

  /* @r{Fetch each element and add it into @code{sum}.}  */
  for (i = 0; i < length;)
    /* @r{Use the index @code{i}, then increment it.}  */
    sum += input_data[i++];

  return sum / length;
@}
@end example

@node Assignment in Subexpressions
@section Pitfall: Assignment in Subexpressions
@cindex assignment in subexpressions
@cindex subexpressions, assignment in

In C, the order of computing parts of an expression is not fixed.
Aside from a few special cases, the operations can be computed in any
order.  If one part of the expression has an assignment to @code{x}
and another part of the expression uses @code{x}, the result is
unpredictable because that use might be computed before or after the
assignment.

Here's an example of ambiguous code:

@example
x = 20;
printf ("%d %d\n", x, x = 4);
@end example

@noindent
If the second argument, @code{x}, is computed before the third argument,
@code{x = 4}, the second argument's value will be 20.  If they are
computed in the other order, the second argument's value will be 4.

Here's one way to make that code unambiguous:

@example
y = 20;
printf ("%d %d\n", y, x = 4);
@end example

Here's another way, with the other meaning:

@example
x = 4;
printf ("%d %d\n", x, x);
@end example

This issue applies to all kinds of assignments, and to the increment
and decrement operators, which are equivalent to assignments.
@xref{Order of Execution}, for more information about this.

However, it can be useful to write assignments inside an
@code{if}-condition or @code{while}-test along with logical operators.
@xref{Logicals and Assignments}.

@node Write Assignments Separately
@section Write Assignments in Separate Statements

It is often convenient to write an assignment inside an
@code{if}-condition, but that can reduce the readability of the
program.  Here's an example of what to avoid:

@example
if (x = advance (x))
  @r{@dots{}}
@end example

The idea here is to advance @code{x} and test if the value is nonzero.
However, readers might miss the fact that it uses @samp{=} and not
@samp{==}.  In fact, writing @samp{=} where @samp{==} was intended
inside a condition is a common error, so GNU C can give warnings when
@samp{=} appears in a way that suggests it's an error.

It is much clearer to write the assignment as a separate statement, like this:

@example
x = advance (x);
if (x != 0)
  @r{@dots{}}
@end example

@noindent
This makes it unmistakably clear that @code{x} is assigned a new value.

Another method is to use the comma operator (@pxref{Comma Operator}),
like this:

@example
if (x = advance (x), x != 0)
  @r{@dots{}}
@end example

@noindent
However, putting the assignment in a separate statement is usually clearer
(unless the assignment is very short), because it reduces nesting.

@node Execution Control Expressions
@chapter Execution Control Expressions
@cindex execution control expressions
@cindex expressions, execution control

This chapter describes the C operators that combine expressions to
control which of those expressions execute, or in which order.

@menu
* Logical Operators::           Logical conjunction, disjunction, negation.
* Logicals and Comparison::     Logical operators with comparison operators.
* Logicals and Assignments::    Assignments with logical operators.
* Conditional Expression::      An if/else construct inside expressions.
* Comma Operator::              Build a sequence of subexpressions.
@end menu

@node Logical Operators
@section Logical Operators
@cindex logical operators
@cindex operators, logical
@cindex conjunction operator
@cindex disjunction operator
@cindex negation operator, logical

The @dfn{logical operators} combine truth values, which are normally
represented in C as numbers.  Any expression with a numeric value is a
valid truth value: zero means false, and any other value means true.
A pointer type is also meaningful as a truth value; a null pointer
(which is zero) means false, and a non-null pointer means true
(@pxref{Pointer Types}).  The value of a logical operator is always 1
or 0 and has type @code{int} (@pxref{Integer Types}).

The logical operators are used mainly in the condition of an @code{if}
statement, or in the end test in a @code{for} statement or
@code{while} statement (@pxref{Statements}).  However, they are valid
in any context where an integer-valued expression is allowed.

@table @samp
@item ! @var{exp}
Unary operator for logical ``not.''  The value is 1 (true) if
@var{exp} is 0 (false), and 0 (false) if @var{exp} is nonzero (true).

@strong{Warning:} If @var{exp} is anything but an lvalue or a
function call, you should write parentheses around it.

@item @var{left} && @var{right}
The logical ``and'' binary operator computes @var{left} and, if necessary,
@var{right}.  If both of the operands are true, the @samp{&&} expression
gives the value 1 (true).  Otherwise, the @samp{&&} expression
gives the value 0 (false).  If @var{left} yields a false value,
that determines the overall result, so @var{right} is not computed.

@item @var{left} || @var{right}
The logical ``or'' binary operator computes @var{left} and, if necessary,
@var{right}.  If at least one of the operands is true, the @samp{||} expression
gives the value 1 (which is true).  Otherwise, the @samp{||} expression
gives the value 0 (false).  If @var{left} yields a true value,
that determines the overall result, so @var{right} is not computed.
@end table

@strong{Warning:} Never rely on the relative precedence of @samp{&&}
and @samp{||}.  When you use them together, always use parentheses to
specify explicitly how they nest, as shown here:

@example
if ((r != 0 && x % r == 0)
    ||
    (s != 0 && x % s == 0))
@end example

@node Logicals and Comparison
@section Logical Operators and Comparisons

The most common thing to use inside the logical operators is a
comparison.  Conveniently, @samp{&&} and @samp{||} have lower
precedence than comparison operators and arithmetic operators, so we
can write expressions like this without parentheses and get the
nesting that is natural: two comparison operations that must both be
true.

@example
if (r != 0 && x % r == 0)
@end example

@noindent
This example also shows how it is useful that @samp{&&} guarantees to
skip the right operand if the left one turns out false.  Because of
that, this code never tries to divide by zero.

This is equivalent:

@example
if (r && x % r == 0)
@end example

@noindent
A truth value is simply a number, so using @code{r} as a truth value
tests whether it is nonzero.  But @code{r}'s meaning as an expression
is not a truth value---it is a number to divide by.  So it is clearer
style to write the explicit @code{!= 0}.

Here's another equivalent way to write it:

@example
if (!(r == 0) && x % r == 0)
@end example

@noindent
This illustrates the unary @samp{!} operator, as well as the need to
write parentheses around its operand.

@node Logicals and Assignments
@section Logical Operators and Assignments

There are cases where assignments nested inside the condition can
actually make a program @emph{easier} to read.  Here is an example
using a hypothetical type @code{list} which represents a list; it
tests whether the list has at least two links, using hypothetical
functions, @code{nonempty} which is true if the argument is a nonempty
list, and @code{list_next} which advances from one list link to the
next.  We assume that a list is never a null pointer, so that the
assignment expressions are always ``true.''

@example
if (nonempty (list)
    && (temp1 = list_next (list))
    && nonempty (temp1)
    && (temp2 = list_next (temp1)))
  @r{@dots{}}  /* @r{use @code{temp1} and @code{temp2}} */
@end example

@noindent
Here we take advantage of the @samp{&&} operator to avoid executing
the rest of the code if a call to @code{nonempty} returns ``false.''  The
only natural place to put the assignments is among those calls.

It would be possible to rewrite this as several statements, but that
could make it much more cumbersome.  On the other hand, when the test
is even more complex than this one, splitting it into multiple
statements might be necessary for clarity.

If an empty list is a null pointer, we can dispense with calling
@code{nonempty}:

@example
if (list
    && (temp1 = list_next (list))
    && (temp2 = list_next (temp1)))
 @r{@dots{}}
@end example

@node Conditional Expression
@section Conditional Expression
@cindex conditional expression
@cindex expression, conditional

C has a conditional expression that selects one of two expressions
to compute and get the value from.  It looks like this:

@example
@var{condition} ? @var{iftrue} : @var{iffalse}
@end example

@menu
* Conditional Rules::           Rules for the conditional operator.
* Conditional Branches::        About the two branches in a conditional.
@end menu

@node Conditional Rules
@subsection Rules for the Conditional Operator

The first operand, @var{condition}, should be a value that can be
compared with zero---a number or a pointer.  If it is true (nonzero),
then the conditional expression computes @var{iftrue} and its value
becomes the value of the conditional expression.  Otherwise the
conditional expression computes @var{iffalse} and its value becomes
the value of the conditional expression.  The conditional expression
always computes just one of @var{iftrue} and @var{iffalse}, never both
of them.

Here's an example: the absolute value of a number @code{x}
can be written as @code{(x >= 0 ? x : -x)}.

@strong{Warning:} The conditional expression has rather low
syntactic precedence.  Except when the conditional expression is used
as an argument in a function call, write parentheses around it.  For
clarity, always write parentheses around it if it extends across more
than one line.

@strong{Warning:} Assignment operators and the comma operator
(@pxref{Comma Operator}) have lower precedence than conditional
expressions, so write parentheses around those when they appear inside
a conditional expression.  @xref{Order of Execution}.

@c ??? Are there any other cases where it is fine to omit them?
@strong{Warning:} When nesting a conditional expression within another
conditional expression, unless a pair of matching delimiters surrounds
the inner conditional expression for some other reason, write
parentheses around it:

@example
((foo > 0 ? test1 : test2) ? (ifodd (foo) ? 5 : 10)
                           : (ifodd (whatever) ? 5 : 10));
@end example

@noindent
In the first operand, those parentheses are necessary to prevent
incorrect parsing.  In the second and third operands, the computer may
not need the parentheses, but they will help human beings.

@node Conditional Branches
@subsection Conditional Operator Branches
@cindex branches of conditional expression

We call @var{iftrue} and @var{iffalse} the @dfn{branches} of the
conditional.

The two branches should normally have the same type, but a few
exceptions are allowed.  If they are both numeric types, the
conditional converts both to their common type (@pxref{Common Type}).

With pointers (@pxref{Pointers}), the two values can be pointers to
nearly compatible types (@pxref{Compatible Types}).  In this case, the
result type is a similar pointer whose target type combines all the
type qualifiers (@pxref{Type Qualifiers}) of both branches.

If one branch has type @code{void *} and the other is a pointer to an
object (not to a function), the conditional converts the latter to
@code{void *}.

If one branch is an integer constant with value zero and the other is
a pointer, the conditional converts zero to the pointer's type.

In GNU C, you can omit @var{iftrue} in a conditional expression.  In
that case, if @var{condition} is nonzero, its value becomes the value of
the conditional expression, after conversion to the common type.
Thus,

@example
x ? : y
@end example

@noindent
has the value of @code{x} if that is nonzero; otherwise, the value of
@code{y}.

@cindex side effect in ?:
@cindex ?: side effect
Omitting @var{iftrue} is useful when @var{condition} has side effects.
In that case, writing that expression twice would carry out the side
effects twice, but writing it once does them just once.  For example,
if we suppose that the function @code{next_element} advances a pointer
variable to point to the next element in a list and returns the new
pointer,

@example
next_element () ? : default_pointer
@end example

@noindent
is a way to advance the pointer and use its new value if it isn't
null, but use @code{default_pointer} if that is null.  We cannot do
it this way,

@example
next_element () ? next_element () : default_pointer
@end example

@noindent
because that would advance the pointer a second time.

@node Comma Operator
@section Comma Operator
@cindex comma operator
@cindex operator, comma

The comma operator stands for sequential execution of expressions.
The value of the comma expression comes from the last expression in
the sequence; the previous expressions are computed only for their
side effects.  It looks like this:

@example
@var{exp1}, @var{exp2} @r{@dots{}}
@end example

@noindent
You can bundle any number of expressions together this way, by putting
commas between them.

@menu
* Uses of Comma::       When to use the comma operator.
* Clean Comma::         Clean use of the comma operator.
* Avoid Comma::         When to not use the comma operator.
@end menu

@node Uses of Comma
@subsection The Uses of the Comma Operator

With commas, you can put several expressions into a place that allows
one expression---for example, in the header of a @code{for} statement.
This statement

@example
for (i = 0, j = 10, k = 20; i < n; i++)
@end example

@noindent
contains three assignment expressions, to initialize @code{i}, @code{j}
and @code{k}.  The syntax of @code{for} requires just one expression
for initialization; to include three assignments, we use commas to
bundle them into a single larger expression, @code{i = 0, j = 10, k =
20}.  This technique is also useful in the loop-advance expression,
the last of the three inside the @code{for} parentheses.

In the @code{for} statement and the @code{while} statement
(@pxref{Loop Statements}), a comma provides a way to perform some side
effect before the loop-exit test.  For example,

@example
while (printf ("At the test, x = %d\n", x), x != 0)
@end example

@node Clean Comma
@subsection Clean Use of the Comma Operator

Always write parentheses around a series of comma operators, except
when it is at top level in an expression statement, or within the
parentheses of an @code{if}, @code{for}, @code{while}, or @code{switch}
statement (@pxref{Statements}).  For instance, in

@example
for (i = 0, j = 10, k = 20; i < n; i++)
@end example

@noindent
the commas between the assignments are clear because they are between
a parenthesis and a semicolon.

The arguments in a function call are also separated by commas, but that is
not an instance of the comma operator.  Note the difference between

@example
foo (4, 5, 6)
@end example

@noindent
which passes three arguments to @code{foo} and

@example
foo ((4, 5, 6))
@end example

@noindent
which uses the comma operator and passes just one argument
(with value 6).

@strong{Warning:} Don't use the comma operator within an argument
of a function unless it makes the code more readable.  When you do so,
don't put part of another argument on the same line.  Instead, add a
line break to make the parentheses around the comma operator easier to
see, like this.

@example
foo ((mumble (x, y), frob (z)),
     *p)
@end example

@node Avoid Comma
@subsection When Not to Use the Comma Operator

You can use a comma in any subexpression, but in most cases it only
makes the code confusing, and it is clearer to raise all but the last
of the comma-separated expressions to a higher level.  Thus, instead
of this:

@example
x = (y += 4, 8);
@end example

@noindent
it is much clearer to write this:

@example
y += 4, x = 8;
@end example

@noindent
or this:

@example
y += 4;
x = 8;
@end example

Use commas only in the cases where there is no clearer alternative
involving multiple statements.

By contrast, don't hesitate to use commas in the expansion in a macro
definition.  The trade-offs of code clarity are different in that
case, because the @emph{use} of the macro may improve overall clarity
so much that the ugliness of the macro's @emph{definition} is a small
price to pay.  @xref{Macros}.

@node Binary Operator Grammar
@chapter Binary Operator Grammar
@cindex binary operator grammar
@cindex grammar, binary operator
@cindex operator precedence
@cindex precedence, operator
@cindex left-associative

@dfn{Binary operators} are those that take two operands, one
on the left and one on the right.

All the binary operators in C are syntactically left-associative.
This means that @w{@code{a @var{op} b @var{op} c}} means @w{@code{(a
@var{op} b) @var{op} c}}.  However, the only operators you should
repeat in this way without parentheses are @samp{+}, @samp{-},
@samp{*} and @samp{/}, because those cases are clear from algebra.  So
it is OK to write @code{a + b + c} or @code{a - b - c}, but never
@code{a == b == c} or @code{a % b % c}.  For those operators, use
explicit parentheses to show how the operations nest.

Each C operator has a @dfn{precedence}, which is its rank in the
grammatical order of the various operators.  The operators with the
highest precedence grab adjoining operands first; these expressions
then become operands for operators of lower precedence.

The precedence order of operators in C is fully specified, so any
combination of operations leads to a well-defined nesting.  We state
only part of the full precedence ordering here because it is bad
practice for C code to depend on the other cases.  For cases not
specified in this chapter, always use parentheses to make the nesting
explicit.@footnote{Personal note from Richard Stallman: I wrote GCC
without remembering anything about the C precedence order beyond
what's stated here.  I studied the full precedence table to write the
parser, and promptly forgot it again.  If you need to look up the full
precedence order to understand some C code, add enough parentheses so
nobody else needs to do that.}

Clean code can depend on this subsequence of the precedence ordering
(stated from highest precedence to lowest):

@enumerate
@item
Postfix operations: access to a field or alternative (@samp{.} and
@samp{->}), array subscripting, function calls, and unary postfix
operators.

@item
Unary prefix operations.

@item
Multiplication, division, and remainder (they have the same precedence).

@item
Addition and subtraction (they have the same precedence).

@item
Comparisons---but watch out!

@item
Logical operations @samp{&&} and @samp{||}---but watch out!

@item
Conditional expression with @samp{?} and @samp{:}.

@item
Assignments.

@item
Sequential execution (the comma operator, @samp{,}).
@end enumerate

Two of the lines in the above list say ``but watch out!''  That means
that the line covers operations with subtly different precedence.  When
you use two comparison operations together, don't depend on the
grammar of C to control how they nest.  Instead, always use
parentheses to show their nesting.

You can let several @samp{&&} operations associate, or several
@samp{||} operations, but always use parentheses to show how @samp{&&}
and @samp{||} nest with each other.  @xref{Logical Operators}.

There is one other precedence ordering that clean code can depend on:

@enumerate
@item
Unary postfix operations.

@item
Bitwise and shift operations---but watch out!

@item
Conditional expression with @samp{?} and @samp{:}.
@end enumerate

The caveat for bitwise and shift operations is like that for logical
operators: you can let multiple uses of one bitwise operation
associate, but always use parentheses to control nesting of dissimilar
operations.

These lists do not specify any precedence ordering between the bitwise
and shift operations of the second list and the binary operations
above conditional expressions in the first list.  When they come
together, parenthesize them.  @xref{Bitwise Operations}.

@node Order of Execution
@chapter Order of Execution
@cindex order of execution

The order of execution of a C program is not always obvious, and not
necessarily predictable.  This chapter describes what you can count on.

@menu
* Reordering of Operands::       Operations in C are not necessarily computed
                                   in the order they are written.
* Associativity and Ordering::   Some associative operations are performed
                                   in a particular order; others are not.
* Sequence Points::              Some guarantees about the order of operations.
* Postincrement and Ordering::   Ambiguous execution order with postincrement.
* Ordering of Operands::         Evaluation order of operands
                                   and function arguments.
* Optimization and Ordering::    Compiler optimizations can reorder operations
                                   only if it has no impact on program results.
@end menu

@node Reordering of Operands
@section Reordering of Operands
@cindex ordering of operands
@cindex reordering of operands
@cindex operand execution ordering

The C language does not necessarily carry out operations within an
expression in the order they appear in the code.  For instance, in
this expression,

@example
foo () + bar ()
@end example

@noindent
@code{foo} might be called first or @code{bar} might be called first.
If @code{foo} updates a datum and @code{bar} uses that datum, the
results can be unpredictable.

The unpredictable order of computation of subexpressions also makes a
difference when one of them contains an assignment.  We already saw
this example of bad code,

@example
x = 20;
printf ("%d %d\n", x, x = 4);
@end example

@noindent
in which the second argument, @code{x}, has a different value
depending on whether it is computed before or after the assignment in
the third argument.

@node Associativity and Ordering
@section Associativity and Ordering
@cindex associativity and ordering

@c ??? What to say about signed overflow and associativity.

The bitwise binary operators, @code{&}, @code{|} and @code{^}, are
associative.  The arithmetic binary operators @code{+} and @code{*}
are associative if the operand type is unsigned.  An associative
binary operator, when used repeatedly, can combine any number of
operands.  The operands' values may be computed in any order, and
since the operation is associative, they can be combined in any order
too.

Thus, given four functions that return @code{unsigned int}, calling
them and adding their results as here

@example
(foo () + bar ()) + (baz () + quux ())
@end example

@noindent
may add up the results in any order.

By contrast, arithmetic on signed integers is not always associative
because there is the possibility of overflow (@pxref{Integer
Overflow}).  Thus, the additions must be done in the order specified,
obeying parentheses (or left-association in the absence of
parentheses).  That means computing @code{(foo () + bar ())} and
@code{(baz () + quux ())} first (in either order), then adding the
two.

@c ??? Does use of -fwrapv make signed addition count as associative?

The same applies to arithmetic on floating-point values, since that
too is not really associative.  However, the GCC option
@option{-funsafe-math-optimizations} allows the compiler to change the
order of calculation when an associative operation (associative in
exact mathematics) combines several operands.  The option takes effect
when compiling a module (@pxref{Compilation}).  Changing the order
of association can enable GCC to optimize the floating-point
operations better.

In all these examples, the four function calls can be done in any
order.  There is no right or wrong about that.

@node Sequence Points
@section Sequence Points
@cindex sequence points
@cindex full expression

There are some points in the code where C makes limited guarantees
about the order of operations.  These are called @dfn{sequence
points}.  Here is where they occur:

@itemize @bullet
@item
At the end of a @dfn{full expression}; that is to say, an expression
that is not part of a larger expression.  All side effects specified
by that expression are carried out before execution moves
on to subsequent code.

@item
At the end of the first operand of certain operators: @samp{,},
@samp{&&}, @samp{||}, and @samp{?:}.  All side effects specified by
that expression are carried out before any execution of the
next operand.

The commas that separate arguments in a function call are @emph{not}
comma operators, and they do not create sequence points.  The
sequence-point rule for function arguments and the rule for operands
(@pxref{Ordering of Operands}) are different.

@item
Just before calling a function.  All side effects specified by the
argument expressions are carried out before calling the function.

If the function to be called is not constant---that is, if it is
computed by an expression---all side effects in that expression are
carried out before calling the function.
@end itemize

The ordering imposed by a sequence point applies locally to a limited
range of code, as stated above in each case.  For instance, the
ordering imposed by the comma operator does not apply to code outside
the operands of that comma operator.  Thus, in this code,

@example
(x = 5, foo (x)) + x * x
@end example

@noindent
the sequence point of the comma operator orders @code{x = 5} before
@code{foo (x)}, but @code{x * x} could be computed before or after
them.

@node Postincrement and Ordering
@section Postincrement and Ordering
@cindex postincrement and ordering
@cindex ordering and postincrement

The ordering requirements for the postincrement and postdecrement
operations (@pxref{Postincrement/Postdecrement}) are loose: those side
effects must happen ``a little later,'' before the next sequence
point.  That still leaves room for various orders that give different
results.  In this expression,

@example
z = x++ - foo ()
@end example

@noindent
it's unpredictable whether @code{x} gets incremented before or after
calling the function @code{foo}.  If @code{foo} refers to @code{x},
it might see the old value or it might see the incremented value.

In this perverse expression,

@example
x = x++
@end example

@noindent
@code{x} will certainly be incremented but the incremented value may
be replaced with the old value.  That's because the incrementation and
the assignment may occur in either order.  If the incrementation of
@code{x} occurs after the assignment to @code{x}, the incremented
value will remain in place.  But if the incrementation happens first,
the assignment will put the not-yet-incremented value back into
@code{x}, so the expression as a whole will leave @code{x} unchanged.

The conclusion: @strong{avoid such expressions}.  Take care, when you
use postincrement and postdecrement, that the specific expression you
use is not ambiguous as to order of execution.

@node Ordering of Operands
@section Ordering of Operands
@cindex ordering of operands
@cindex operand ordering

Operands and arguments can be computed in any order, but there are limits to
this intermixing in GNU C:

@itemize @bullet
@item
The operands of a binary arithmetic operator can be computed in either
order, but they can't be intermixed: one of them has to come first,
followed by the other.  Any side effects in the operand that's computed
first are executed before the other operand is computed.

@item
That applies to assignment operators too, except that, in simple assignment,
the previous value of the left operand is unused.

@item
The arguments in a function call can be computed in any order, but
they can't be intermixed.  Thus, one argument is fully computed, then
another, and so on until they have all been done.  Any side effects in
one argument are executed before computation of another argument
begins.
@end itemize

These rules don't cover side effects caused by postincrement and
postdecrement operators---those can be deferred up to the next
sequence point.

If you want to get pedantic, the fact is that GCC can reorder the
computations in many other ways provided that it doesn't alter the result
of running the program.  However, because it doesn't alter the result
of running the program, it is negligible, unless you are concerned
with the values in certain variables at various times as seen by other
processes.  In those cases, you should use @code{volatile} to prevent
optimizations that would make them behave strangely.  @xref{volatile}.

@node Optimization and Ordering
@section Optimization and Ordering
@cindex optimization and ordering
@cindex ordering and optimization

Sequence points limit the compiler's freedom to reorder operations
arbitrarily, but optimizations can still reorder them if the compiler
concludes that this won't alter the results.  Thus, in this code,

@example
x++;
y = z;
x++;
@end example

@noindent
there is a sequence point after each statement, so the code is
supposed to increment @code{x} once before the assignment to @code{y}
and once after.  However, incrementing @code{x} has no effect on
@code{y} or @code{z}, and setting @code{y} can't affect @code{x}, so
the code could be optimized into this:

@example
y = z;
x += 2;
@end example

Normally that has no effect except to make the program faster.  But
there are special situations where it can cause trouble due to things
that the compiler cannot know about, such as shared memory.  To limit
optimization in those places, use the @code{volatile} type qualifier
(@pxref{volatile}).

@node Primitive Types
@chapter Primitive Data Types
@cindex primitive types
@cindex types, primitive

This chapter describes all the primitive data types of C---that is,
all the data types that aren't built up from other types.  They
include the types @code{int} and @code{double} that we've already covered.

@menu
* Integer Types::                Description of integer types.
* Floating-Point Data Types::    Description of floating-point types.
* Complex Data Types::           Description of complex number types.
* The Void Type::                A type indicating no value at all.
* Other Data Types::             A brief summary of other types.
* Type Designators::             Referring to a data type abstractly.
@end menu

These types are all made up of bytes (@pxref{Storage}).

@node Integer Types
@section Integer Data Types
@cindex integer types
@cindex types, integer

Here we describe all the integer types and their basic
characteristics.  @xref{Integers in Depth}, for more information about
the bit-level integer data representations and arithmetic.

@menu
* Basic Integers::              Overview of the various kinds of integers.
* Signed and Unsigned Types::   Integers can either hold both negative and
                                  non-negative values, or only non-negative.
* Narrow Integers::             When to use smaller integer types.
* Integer Conversion::          Casting a value from one integer type
                                  to another.
* Boolean Type::                An integer type for boolean values.
* Integer Variations::          Sizes of integer types can vary
                                  across platforms.
@end menu

@node Basic Integers
@subsection Basic Integers

@findex char
@findex int
@findex short int
@findex long int
@findex long long int

Integer data types in C can be signed or unsigned.  An unsigned type
can represent only positive numbers and zero.  A signed type can
represent both positive and negative numbers, in a range spread almost
equally on both sides of zero.

Aside from signedness, the integer data types vary in size: how many
bytes long they are.  The size determines the range of integer values
the type can hold.

Here's a list of the signed integer data types, with the sizes they
have on most computers.  Each has a corresponding unsigned type; see
@ref{Signed and Unsigned Types}.

@table @code
@item signed char
One byte (8 bits).  This integer type is used mainly for integers that
represent characters, usually as elements of arrays or fields of other
data structures.

@item short
@itemx short int
Two bytes (16 bits).

@item int
Four bytes (32 bits).

@item long
@itemx long int
Four bytes (32 bits) or eight bytes (64 bits), depending on the
platform.  Typically it is 32 bits on 32-bit computers
and 64 bits on 64-bit computers, but there are exceptions.

@item long long
@itemx long long int
Eight bytes (64 bits).  Supported in GNU C in the 1980s, and
incorporated into standard C as of ISO C99.
@end table

You can omit @code{int} when you use @code{long} or @code{short}.
This is harmless and customary.

@node Signed and Unsigned Types
@subsection Signed and Unsigned Types
@cindex signed types
@cindex unsigned types
@cindex types, signed
@cindex types, unsigned
@findex signed
@findex unsigned

An unsigned integer type can represent only positive numbers and zero.
A signed type can represent both positive and negative numbers, in a
range spread almost equally on both sides of zero.  For instance,
@code{unsigned char} holds numbers from 0 to 255 (on most computers),
while @code{signed char} holds numbers from @minus{}128 to 127.  Each of
these types holds 256 different possible values, since they are both 8
bits wide.

Write @code{signed} or @code{unsigned} before the type keyword to
specify a signed or an unsigned type.  However, the integer types
other than @code{char} are signed by default; with them, @code{signed}
is a no-op.

Plain @code{char} may be signed or unsigned; this depends on the
compiler, the machine in use, and its operating system.  It is not
@emph{the same type} as either @code{signed char} or @code{unsigned
char}, but it is always equivalent to one of those two.

In many programs, it makes no difference whether the type @code{char}
is signed.  When signedness does matter for a certain value, don't
leave it to chance; declare it as @code{signed char} or @code{unsigned
char} instead.@footnote{Personal note from Richard Stallman: Eating
with hackers at a fish restaurant, I ordered arctic char.  When my
meal arrived, I noted that the chef had not signed it.  So I told
other hackers, ``This char is unsigned---I wanted a signed char!''}

@node Narrow Integers
@subsection Narrow Integers

The types that are narrower than @code{int} are rarely used for
ordinary variables---we declare them @code{int} instead.  This is
because C converts those narrower types to @code{int} for any
arithmetic.  There is literally no reason to declare a local variable
@code{char}, for instance.

In particular, if the value is really a character, you should declare
the variable @code{int}.  Not @code{char}!  Using that narrow type can
force the compiled code to truncate values to @code{char} before
conversion, which is a waste.  Furthermore, some functions return
either a character value or @minus{}1 for ``no character.''  Using
type @code{int} makes it possible to distinguish @minus{}1 from any
character, by sign.

The narrow integer types are useful as parts of other objects, such as
arrays and structures.  Compare these array declarations, whose sizes
on 32-bit processors are shown:

@example
signed char ac[1000];   /* @r{1000 bytes} */
short as[1000];         /* @r{2000 bytes} */
int ai[1000];           /* @r{4000 bytes} */
long long all[1000];    /* @r{8000 bytes} */
@end example

In addition, character strings must be made up of @code{char}s,
because that's what all the standard library string functions expect.
Thus, array @code{ac} could be used as a character string, but the
others could not be.

@node Integer Conversion
@subsection Conversion among Integer Types

C converts between integer types implicitly in many situations.  It
converts the narrow integer types, @code{char} and @code{short}, to
@code{int} whenever they are used in arithmetic.  Assigning a new
value to an integer variable (or other lvalue) converts the value to
the variable's type.

You can also convert one integer type to another explicitly with a
@dfn{cast} operator.  @xref{Explicit Type Conversion}.

The process of conversion to a wider type is straightforward: the
value is unchanged.  The only exception is when converting a negative
value (in a signed type, obviously) to a wider unsigned type.  In that
case, the result is a positive value with the same bits
(@pxref{Integers in Depth}), padded on the left with zeros.

@cindex truncation
Converting to a narrower type, also called @dfn{truncation}, involves
discarding some of the value's bits.  This is not considered overflow
(@pxref{Integer Overflow}) because loss of significant bits is a
normal consequence of truncation.  Likewise for conversion between
signed and unsigned types of the same width.

More information about conversion for assignment is in
@ref{Assignment Type Conversions}.  For conversion for arithmetic,
see @ref{Argument Promotions}.

@node Boolean Type
@subsection Boolean Type
@cindex boolean type
@cindex type, boolean
@findex bool

The unsigned integer type @code{bool} holds truth values: its possible
values are 0 and 1.  Converting any nonzero value to @code{bool}
results in 1.  For example:

@example
bool a = 0;
bool b = 1;
bool c = 4; /* @r{Stores the value 1 in @code{c}.}  */
@end example

Unlike @code{int}, @code{bool} is not a keyword.  It is defined in
the header file @file{stdbool.h}.

@node Integer Variations
@subsection Integer Variations

The integer types of C have standard @emph{names}, but what they
@emph{mean} varies depending on the kind of platform in use:
which kind of computer, which operating system, and which compiler.
It may even depend on the compiler options used.

Plain @code{char} may be signed or unsigned; this depends on the
platform, too.  Even for GNU C, there is no general rule.

In theory, all of the integer types' sizes can vary.  @code{char} is
always considered one ``byte'' for C, but it is not necessarily an
8-bit byte; on some platforms it may be more than 8 bits.  @code{short
int} and @code{int} are at least two bytes long (it may be longer).
@code{long int} is at least four bytes long, and @code{long long int}
at least eight bytes long.

It is possible that in the future GNU C will support platforms where
@code{int} is 64 bits long.  In practice, however, on today's real
computers, there is little variation; you can rely on the table
given previously (@pxref{Basic Integers}).

To be completely sure of the size of an integer type,
use the types @code{int16_t}, @code{int32_t} and @code{int64_t}.
Their corresponding unsigned types add @samp{u} at the front:
@code{uint16_t}, @code{uint32_t} and @code{uint64_t}.
To define all these types, include the header file @file{stdint.h}.

The GNU C Compiler can compile for some embedded controllers that use two
bytes for @code{int}.  On some, @code{int} is just one ``byte,'' and
so is @code{short int}---but that ``byte'' may contain 16 bits or even
32 bits.  These processors can't support an ordinary operating system
(they may have their own specialized operating systems), and most C
programs do not try to support them.

@node Floating-Point Data Types
@section Floating-Point Data Types
@cindex floating-point types
@cindex types, floating-point
@findex double
@findex float
@findex long double

@dfn{Floating point} is the binary analogue of scientific notation:
internally it represents a number as a fraction and a binary exponent;
the value is that fraction multiplied by the specified power of 2.
(The C standard nominally permits other bases, but in GNU C the base
is always 2.)
@c ???

For instance, to represent 6, the fraction would be 0.75 and the
exponent would be 3; together they stand for the value @math{0.75 * 2@sup{3}},
meaning 0.75 * 8.  The value 1.5 would use 0.75 as the fraction and 1
as the exponent.  The value 0.75 would use 0.75 as the fraction and 0
as the exponent.  The value 0.375 would use 0.75 as the fraction and
@minus{}1 as the exponent.

These binary exponents are used by machine instructions.  You can
write a floating-point constant this way if you wish, using
hexadecimal; but normally we write floating-point numbers in decimal (base 10).
@xref{Floating Constants}.

C has three floating-point data types:

@table @code
@item double
``Double-precision'' floating point, which uses 64 bits.  This is the
normal floating-point type, and modern computers normally do
their floating-point computations in this type, or some wider type.
Except when there is a special reason to do otherwise, this is the
type to use for floating-point values.

@item float
``Single-precision'' floating point, which uses 32 bits.  It is useful
for floating-point values stored in structures and arrays, to save
space when the full precision of @code{double} is not needed.  In
addition, single-precision arithmetic is faster on some computers, and
occasionally that is useful.  But not often---most programs don't use
the type @code{float}.

C would be cleaner if @code{float} were the name of the type we
use for most floating-point values; however, for historical reasons,
that's not so.

@item long double
``Extended-precision'' floating point is either 80-bit or 128-bit
precision, depending on the machine in use.  On some machines, which
have no floating-point format wider than @code{double}, this is
equivalent to @code{double}.
@end table

Floating-point arithmetic raises many subtle issues.  @xref{Floating
Point in Depth}, for more information.

@node Complex Data Types
@section Complex Data Types
@cindex complex numbers
@cindex types, complex
@cindex @code{_Complex} keyword
@cindex @code{__complex__} keyword
@findex _Complex
@findex __complex__

Complex numbers can include both a real part and an imaginary part.
The numeric constants covered above have real-numbered values.  An
imaginary-valued constant is an ordinary real-valued constant followed
by @samp{i}.

To declare numeric variables as complex, use the @code{_Complex}
keyword.@footnote{For compatibility with older versions of GNU C, the
keyword @code{__complex__} is also allowed.  Going forward, however,
use the new @code{_Complex} keyword as defined in ISO C11.}  The
standard C complex data types are floating point,

@example
_Complex float foo;
_Complex double bar;
_Complex long double quux;
@end example

@noindent
but GNU C supports integer complex types as well.

Since @code{_Complex} is a keyword just like @code{float} and
@code{double} and @code{long}, the keywords can appear in any order,
but the order shown above seems most logical.

GNU C supports constants for complex values; for instance, @code{4.0 +
3.0i} has the value 4 + 3i as type @code{_Complex double}.
@samp{j} is equivalent to @samp{i}, as a numeric suffix.
@xref{Imaginary Constants}.

To pull the real and imaginary parts of the number back out, GNU C
provides the keywords @code{__real__} and @code{__imag__}:

@example
_Complex double foo = 4.0 + 3.0i;

double a = __real__ foo; /* @r{@code{a} is now 4.0.} */
double b = __imag__ foo; /* @r{@code{b} is now 3.0.} */
@end example

@noindent
Standard C does not include these keywords, and instead relies on
functions defined in @code{complex.h} for accessing the real and
imaginary parts of a complex number: @code{crealf}, @code{creal}, and
@code{creall} extract the real part of a float, double, or long double
complex number, respectively; @code{cimagf}, @code{cimag}, and
@code{cimagl} extract the imaginary part.

@cindex complex conjugation
GNU C also defines @samp{~} as an operator for complex conjugation,
which means negating the imaginary part of a complex number:

@example
_Complex double foo = 4.0 + 3.0i;
_Complex double bar = ~foo; /* @r{@code{bar} is now 4.0 @minus{} 3.0i.} */
@end example

@noindent
For standard C compatibility, you can use the appropriate library
function: @code{conjf}, @code{conj}, or @code{conjl}.

@node The Void Type
@section The Void Type
@cindex void type
@cindex type, void
@findex void

The data type @code{void} is a dummy---it allows no operations.  It
really means ``no value at all.''  When a function is meant to return
no value, we write @code{void} for its return type.  Then
@code{return} statements in that function should not specify a value
(@pxref{return Statement}).  Here's an example:

@example
void
print_if_positive (double x, double y)
@{
  if (x <= 0)
    return;
  if (y <= 0)
    return;
  printf ("Next point is (%f,%f)\n", x, y);
@}
@end example

A @code{void}-returning function is comparable to what some other
languages (for instance, Fortran and Pascal) call a ``procedure''
instead of a ``function.''

@c ??? Already presented
@c @samp{%f} in an output template specifies to format a @code{double} value
@c as a decimal number, using a decimal point if needed.

@node Other Data Types
@section Other Data Types

Beyond the primitive types, C provides several ways to construct new
data types.  For instance, you can define @dfn{pointers}, values that
represent the addresses of other data (@pxref{Pointers}).  You can
define @dfn{structures}, as in many other languages
(@pxref{Structures}), and @dfn{unions}, which define multiple ways to
interpret the contents of the same memory space (@pxref{Unions}).
@dfn{Enumerations} are collections of named integer codes
(@pxref{Enumeration Types}).

@dfn{Array types} in C are used for allocating space for objects,
but C does not permit operating on an array value as a whole.  @xref{Arrays}.

@node Type Designators
@section Type Designators
@cindex type designator

Some C constructs require a way to designate a specific data type
independent of any particular variable or expression which has that
type.  The way to do this is with a @dfn{type designator}.  The
constructs that need one include casts (@pxref{Explicit Type
Conversion}) and @code{sizeof} (@pxref{Type Size}).

We also use type designators to talk about the type of a value in C,
so you will see many type designators in this manual.  When we say,
``The value has type @code{int},'' @code{int} is a type designator.

To make the designator for any type, imagine a variable declaration
for a variable of that type and delete the variable name and the final
semicolon.

@c ??? Is the rest of this so obvious it can be shortened?
For example, to designate the type of full-word integers, we start
with the declaration for a variable @code{foo} with that type,
which is this:

@example
int foo;
@end example

@noindent
Then we delete the variable name @code{foo} and the semicolon, leaving
@code{int}---exactly the keyword used in such a declaration.
Therefore, the type designator for this type is @code{int}.

What about long unsigned integers?  From the declaration

@example
unsigned long int foo;
@end example

@noindent
we determine that the designator is @code{unsigned long int}.

Following this procedure, the designator for any primitive type is
simply the set of keywords which specifies that type in a declaration.
The same is true for structure types, union types, and
enumeration types.

@c ??? This graf is needed.

Designators for pointer types do follow the rule of deleting the
variable name and semicolon, but the result is not so simple.
@xref{Pointer Type Designators}, as part of the chapter about
pointers.  @xref{Array Type Designators}, for designators for array
types.

To understand what type a designator stands for, imagine a variable
name inserted into the right place in the designator to make a valid
declaration.  What type would that variable be declared as?  That is the
type the designator designates.

@node Constants
@chapter Constants
@cindex constants

A @dfn{constant} is an expression that stands for a specific value by
explicitly representing the desired value.  C allows constants for
numbers, characters, and strings.  We have already seen numeric and
string constants in the examples.

@menu
* Integer Constants::            Literal integer values.
* Integer Const Type::           Types of literal integer values.
* Floating Constants::           Literal floating-point values.
* Imaginary Constants::          Literal imaginary number values.
* Invalid Numbers::              Avoiding preprocessing number misconceptions.
* Character Constants::          Literal character values.
* String Constants::             Literal string values.
* UTF-8 String Constants::       Literal UTF-8 string values.
* Unicode Character Codes::      Unicode characters represented
                                   in either UTF-16 or UTF-32.
* Wide Character Constants::     Literal characters values larger than 8 bits.
* Wide String Constants::        Literal string values made up of
                                   16- or 32-bit characters.
@end menu

@node Integer Constants
@section Integer Constants
@cindex integer constants
@cindex constants, integer

An integer constant consists of a number to specify the value,
followed optionally by suffix letters to specify the data type.

The simplest integer constants are numbers written in base 10
(decimal), such as @code{5}, @code{77}, and @code{403}.  A decimal
constant cannot start with the character @samp{0} (zero) because
that makes the constant octal.

You can get the effect of a negative integer constant by putting a
minus sign at the beginning.  In grammatical terms, that is an
arithmetic expression rather than a constant, but it behaves just like
a true constant.

Integer constants can also be written in octal (base 8), hexadecimal
(base 16), or binary (base 2).  An octal constant starts with the
character @samp{0} (zero), followed by any number of octal digits
(@samp{0} to @samp{7}):

@example
0      // @r{zero}
077    // @r{63}
0403   // @r{259}
@end example

@noindent
Pedantically speaking, the constant @code{0} is an octal constant, but
we can think of it as decimal; it has the same value either way.

A hexadecimal constant starts with @samp{0x} (upper or lower case)
followed by hex digits (@samp{0} to @samp{9}, as well as @samp{a}
through @samp{f} in upper or lower case):

@example
0xff   // @r{255}
0XA0   // @r{160}
0xffFF // @r{65535}
@end example

@cindex binary integer constants
A binary constant starts with @samp{0b} (upper or lower case) followed
by bits (each represented by the characters @samp{0} or @samp{1}):

@example
0b101  // @r{5}
@end example

@noindent
Binary constants are a GNU C extension, not part of the C standard.

Sometimes a space is needed after an integer constant to avoid
lexical confusion with the following tokens.  @xref{Invalid Numbers}.

@node Integer Const Type
@section Integer Constant Data Types
@cindex integer constant data types
@cindex constant data types, integer
@cindex types of integer constants

The type of an integer constant is normally @code{int}, if the value
fits in that type, but here are the complete rules.  The type
of an integer constant is the first one in this sequence that can
properly represent the value,

@enumerate
@item
@code{int}
@item
@code{unsigned int}
@item
@code{long int}
@item
@code{unsigned long int}
@item
@code{long long int}
@item
@code{unsigned long long int}
@end enumerate

@noindent
and that isn't excluded by the following rules.

If the constant has @samp{l} or @samp{L} as a suffix, that excludes the
first two types (those that are not @code{long}).

If the constant has @samp{ll} or @samp{LL} as a suffix, that excludes
first four types (those that are not @code{long long}).

If the constant has @samp{u} or @samp{U} as a suffix, that excludes
the signed types.

Otherwise, if the constant is decimal (not binary, octal, or
hexadecimal), that excludes the unsigned types.
@c ### This said @code{unsigned int} is excluded.
@c ### See 17 April 2016

Here are some examples of the suffixes.

@example
3000000000u      // @r{three billion as @code{unsigned int}.}
0LL              // @r{zero as a @code{long long int}.}
0403l            // @r{259 as a @code{long int}.}
2147483648       // @r{This is of type @code{long long int}.}
                 // @r{on typical 32-bit machines,}
                 // @r{since it won't fit in 32 bits as a signed number.}
2147483648U      // @r{This is of type @code{unsigned int},}
                 // @r{since it fits in 32 unsigned bits.}
@end example

Suffixes in integer constants are rarely used.  When the precise type
is important, it is cleaner to convert explicitly (@pxref{Explicit
Type Conversion}).

@xref{Integer Types}.

@node Floating Constants
@section Floating-Point Constants
@cindex floating-point constants
@cindex constants, floating-point

A floating-point decimal constant must have either a decimal point, an
exponent-of-ten, or both; they distinguish it from an integer
constant.  Just adding the floating-point suffix, @samp{f}, to an
integer does not make a valid floating-point constant, and adding
@samp{l} would instead make it a long integer.

To indicate an exponent, write @samp{e} or @samp{E}.  The exponent
value follows.  It is always written as a decimal number; it can
optionally start with a sign.  The exponent @var{n} means to multiply
the constant's value by ten to the @var{n}th power.

Thus, @samp{1500.0}, @samp{15e2}, @samp{15e+2}, @samp{15.0e2},
@samp{1.5e+3}, @samp{.15e4}, and @samp{15000e-1} are six ways of
writing a floating-point number whose value is 1500.  They are all
equivalent in principle.
@c ??? Are the resulting valus guaranteed to be equal
@c ??? in GCC for the targets that we describe in this manual?

Here are more examples with decimal points:

@example
1.0
1000.
3.14159
.05
.0005
@end example

For each of them, here are some equivalent constants written with
exponents:

@example
1e0, 1.0000e0
100e1, 100e+1, 100E+1, 1e3, 10000e-1
3.14159e0
5e-2, .0005e+2, 5E-2, .0005E2
.05e-2
@end example

A floating-point constant normally has type @code{double}.  You can
force it to type @code{float} by adding @samp{f} or @samp{F}
at the end.  For example,

@example
3.14159f
3.14159e0f
1000.f
100E1F
.0005f
.05e-2f
@end example

Likewise, @samp{l} or @samp{L} at the end forces the constant
to type @code{long double}.

@cindex hexadecimal floating constants
There are also @dfn{hexadecimal floating constants}.  These
@emph{must} have an exponent, but since @samp{e} would be interpreted
as a hexadecimal digit, the character @samp{p} or @samp{P} (for
``power'') indicates the exponent.

The exponent in a hexadecimal floating constant is an optionally signed
decimal integer that specifies a power of 2 (@emph{not} 10 or 16) to
multiply into the number.

Here are some examples:

@example
@group
0xAp2        // @r{40 in decimal}
0xAp-1       // @r{5 in decimal}
0x2.0Bp4     // @r{32.6875 decimal}
0xE.2p3      // @r{113 decimal}
0x123.ABCp0  // @r{291.6708984375 in decimal}
0x123.ABCp4  // @r{4666.734375 in decimal}
0x100p-8     // @r{1}
0x10p-4      // @r{1}
0x1p+4       // @r{16}
0x1p+8       // @r{256}
@end group
@end example

@xref{Floating-Point Data Types}.

@node Imaginary Constants
@section Imaginary Constants
@cindex imaginary constants
@cindex complex constants
@cindex constants, imaginary

A complex number consists of a real part plus an imaginary part.  (You
may omit one part if it is zero.)  This section explains how to write
numeric constants with imaginary values.  By adding these to ordinary
real-valued numeric constants, we can make constants with complex
values.

The simple way to write an imaginary-number constant is to attach the
suffix @samp{i} or @samp{I}, or @samp{j} or @samp{J}, to an integer or
floating-point constant.  For example, @code{2.5fi} has type
@code{_Complex float} and @code{3i} has type @code{_Complex int}.
The four alternative suffix letters are all equivalent.

@cindex _Complex_I
The other way to write an imaginary constant is to multiply a real
constant by @code{_Complex_I}, which represents the imaginary number
i.  Standard C doesn't support suffixes for imaginary constants, so
this clunky method is needed.

To write a complex constant with a nonzero real part and a nonzero
imaginary part, write the two separately and add them, like this:

@example
4.0 + 3.0i
@end example

@noindent
That gives the value 4 + 3i, with type @code{_Complex double}.

Such a sum can include multiple real constants, or none.  Likewise, it
can include multiple imaginary constants, or none.  For example:

@example
_Complex double foo, bar, quux;

foo = 2.0i + 4.0 + 3.0i; /* @r{Imaginary part is 5.0.} */
bar = 4.0 + 12.0; /* @r{Imaginary part is 0.0.} */
quux = 3.0i + 15.0i; /* @r{Real part is 0.0.} */
buux = 3.0i + 15.0j; /* @r{Equal to @code{quux}.} */
@end example

@xref{Complex Data Types}.

@node Invalid Numbers
@section Invalid Numbers

Some number-like constructs which are not really valid as numeric
constants are treated as numbers in preprocessing directives.  If
these constructs appear outside of preprocessing, they are erroneous.
@xref{Preprocessing Tokens}.

Sometimes we need to insert spaces to separate tokens so that they
won't be combined into a single number-like construct.  For example,
@code{0xE+12} is a preprocessing number that is not a valid numeric
constant, so it is a syntax error.  If what we want is the three
tokens @code{@w{0xE + 12}}, we have to insert spaces as separators.

@node Character Constants
@section Character Constants
@cindex character constants
@cindex constants, character
@cindex escape sequence

A @dfn{character constant} is written with single quotes, as in
@code{'@var{c}'}.  In the simplest case, @var{c} is a single ASCII
character that the constant should represent.  The constant has type
@code{int}, and its value is the character code of that character.
For instance, @code{'a'} represents the character code for the letter
@samp{a}: 97, that is.

To put the @samp{'} character (single quote) in the character
constant, @dfn{escape} it with a backslash (@samp{\}).  This character
constant looks like @code{'\''}.  The backslash character here
functions as an @dfn{escape character}, and such a sequence,
starting with @samp{\}, is called an @dfn{escape sequence}.

To put the @samp{\} character (backslash) in the character constant,
escape it with @samp{\} (another backslash).  This character
constant looks like @code{'\\'}.

@cindex bell character
@cindex @samp{\a}
@cindex backspace
@cindex @samp{\b}
@cindex tab (ASCII character)
@cindex @samp{\t}
@cindex vertical tab
@cindex @samp{\v}
@cindex formfeed
@cindex @samp{\f}
@cindex newline
@cindex @samp{\n}
@cindex return (ASCII character)
@cindex @samp{\r}
@cindex escape (ASCII character)
@cindex @samp{\e}
Here are all the escape sequences that represent specific characters
in a character constant.  The numeric values shown are the
corresponding ASCII character codes, as decimal numbers.  The comments
give the characters' conventional or traditional names, as well as the
appearance for graphical characters.

@example
'\a' @result{} 7       /* @r{alarm, bell, @kbd{CTRL-g}} */
'\b' @result{} 8       /* @r{backspace, @key{BS}, @kbd{CTRL-h}} */
'\t' @result{} 9       /* @r{tab, @key{TAB}, @kbd{CTRL-i}} */
'\n' @result{} 10      /* @r{newline, @kbd{CTRL-j}} */
'\v' @result{} 11      /* @r{vertical tab, @kbd{CTRL-k}} */
'\f' @result{} 12      /* @r{formfeed, @kbd{CTRL-l}} */
'\r' @result{} 13      /* @r{carriage return, @key{RET}, @kbd{CTRL-m}} */
'\e' @result{} 27      /* @r{escape character, @key{ESC}, @kbd{CTRL-[}} */
'\\' @result{} 92      /* @r{backslash character, @kbd{\}} */
'\'' @result{} 39      /* @r{single quote character, @kbd{'}} */
'\"' @result{} 34      /* @r{double quote character, @kbd{"}} */
'\?' @result{} 63      /* @r{question mark, @kbd{?}} */
@end example

@samp{\e} is a GNU C extension; to stick to standard C, write
@samp{\33}.  (The number after @samp{\} is octal.)  To specify
a character constant using decimal, use a cast; for instance,
@code{(unsigned char) 27}.

You can also write octal and hex character codes as
@samp{\@var{octalcode}} or @samp{\x@var{hexcode}}.  Decimal is not an
option here, so octal codes do not need to start with @samp{0}.  An
octal code is limited to three octal digits, and any non-octal
character terminates it.

The character constant's value has type @code{int}.  However, the
character code is treated initially as a @code{char} value, which is
then converted to @code{int}.  If the character code is greater than
127 (@code{0177} in octal), the resulting @code{int} may be negative
on a platform where the type @code{char} is 8 bits long and signed.

@node String Constants
@section String Constants
@cindex string constants
@cindex constants, string

A @dfn{string constant} represents a series of characters.  It starts
with @samp{"} and ends with @samp{"}; in between are the contents of
the string.  Quoting special characters such as @samp{"}, @samp{\} and
newline in the contents works in string constants as in character
constants.  In a string constant, @samp{'} does not need to be quoted.

A string constant defines an array of characters which contains the
specified characters followed by the null character (code 0).  Using
the string constant is equivalent to using the name of an array with
those contents.  In simple cases, where there are no backslash escape
sequences, the length in bytes of the string constant is one greater
than the number of characters written in it.

As with any array in C, using the string constant in an expression
converts the array to a pointer (@pxref{Pointers}) to the array's
zeroth element (@pxref{Accessing Array Elements}).  This pointer will
have type @code{char *} because it points to an element of type
@code{char}.  @code{char *} is an example of a type designator for a
pointer type (@pxref{Pointer Type Designators}).  That type is used
for operating on strings generally, not just the strings expressed as
constants.

Thus, the string constant @code{"Foo!"} is almost
equivalent to declaring an array like this

@example
char string_array_1[] = @{'F', 'o', 'o', '!', '\0' @};
@end example

@noindent
and then using @code{string_array_1} in the program (which converts it
to type @code{char *}).  There are two differences, however:

@itemize @bullet
@item
The string constant doesn't define a name for the array.

@item
The string constant is probably stored in a read-only area of memory.
@end itemize

Newlines are not allowed in the text of a string constant.  The motive
for this prohibition is to catch the error of omitting the closing
@samp{"}.  To put a newline in a constant string, write it as
@samp{\n} in the string constant.

A real null character in the source code inside a string constant
causes a warning.  To put a null character in the middle of a string
constant, write @samp{\0} or @samp{\000}.

Consecutive string constants are effectively concatenated.  Thus,

@example
"Fo" "o!"   @r{is equivalent to}   "Foo!"
@end example

This is useful for writing a string containing multiple lines,
like this:

@example
"This message is so long that it needs more than\n"
"a single line of text.  C does not allow a newline\n"
"to represent itself in a string constant, so we have to\n"
"write \\n to put it in the string.  For readability of\n"
"the source code, it is advisable to put line breaks in\n"
"the source where they occur in the contents of the\n"
"constant.\n"
@end example

The sequence of a backslash and a newline is ignored anywhere
in a C program, and that includes inside a string constant.
Thus, you can write multi-line string constants this way:

@example
"This is another way to put newlines in a string constant\n\
and break the line after them in the source code."
@end example

@noindent
However, concatenation is the recommended way to do this.

You can also write perverse string constants like this,

@example
"Fo\
o!"
@end example

@noindent
but don't do that---write it like this instead:

@example
"Foo!"
@end example

Be careful to avoid passing a string constant to a function that
modifies the string it receives.  The memory where the string constant
is stored may be read-only, which would cause a fatal @code{SIGSEGV}
signal that normally terminates the function (@pxref{Signals}).  Even
worse, the memory may not be read-only.  Then the function might
modify the string constant, thus spoiling the contents of other string
constants that are supposed to contain the same value and are unified
by the compiler.

@node UTF-8 String Constants
@section UTF-8 String Constants
@cindex UTF-8 String Constants

Writing @samp{u8} immediately before a string constant, with no
intervening space, means to represent that string in UTF-8 encoding as
a sequence of bytes.  UTF-8 represents ASCII characters with a single
byte, and represents non-ASCII Unicode characters (codes 128 and up)
as multibyte sequences.  Here is an example of a UTF-8 constant:

@example
u8"A cónstàñt"
@end example

This constant occupies 13 bytes plus the terminating null,
because each of the accented letters is a two-byte sequence.

Concatenating an ordinary string with a UTF-8 string conceptually
produces another UTF-8 string.  However, if the ordinary string
contains character codes 128 and up, the results cannot be relied on.

@node Unicode Character Codes
@section Unicode Character Codes
@cindex Unicode character codes
@cindex universal character names
@cindex code point

You can specify Unicode characters using escape sequences called
@dfn{universal character names} that start with @samp{\u} and
@samp{\U}.  They are valid in C for individual character constants,
inside string constants (@pxref{String Constants}), and even in
identifiers.  These escape sequences include a hexadecimal Unicode
character code, also called a @dfn{code point} in Unicode terminology.

Use the @samp{\u} escape sequence with a 16-bit hexadecimal Unicode
character code.  If the character's numeric code is too big for 16
bits, use the @samp{\U} escape sequence with a 32-bit hexadecimal
Unicode character code.  Here are some examples.

@example
\u6C34      /* @r{16-bit code (Chinese for ``water''), UTF-16} */
\U0010ABCD  /* @r{32-bit code, UTF-32} */
@end example

@noindent
One way to use these is in UTF-8 string constants (@pxref{UTF-8 String
Constants}).  For instance, here we use two of them, each preceded by
a space.

@example
u8"fóó \u6C34 \U0010ABCD"
@end example

  You can also use them in wide character constants (@pxref{Wide
Character Constants}), like this:

@example
u'\u6C34'      /* @r{16-bit code (water)} */
U'\U0010ABCD'  /* @r{32-bit code} */
@end example

@noindent
and in wide string constants (@pxref{Wide String Constants}), like
this:

@example
u"\u6C34\u706B"  /* @r{16-bit codes (water, fire)} */
U"\U0010ABCD"    /* @r{32-bit code} */
@end example

@noindent
And in an identifier:

@example
int foo\u6C34bar = 0;
@end example

Codes in the range of D800 through DFFF are invalid in universal
character names.  Trying to write them using @samp{\u} causes an
error.  Unicode calls them ``surrogate code points'' and uses them in
UTF-16 for purposes too specialized to explain here.

Codes less than 00A0 are likewise invalid in universal character
names, and likewise cause errors, except for 0024 (@samp{$}), 0040
(@samp{@@}), and 0060 (@samp{`}).  Characters which can't be
represented with universal character names can be specified with octal
or hexadecimal escape sequences (@pxref{Character Constants}).

@node Wide Character Constants
@section Wide Character Constants
@cindex wide character constants
@cindex constants, wide character

A @dfn{wide character constant} represents characters with more than 8
bits of character code.  This is an obscure feature that we need to
document but that you probably won't ever use.  If you're just
learning C, you may as well skip this section.

The original C wide character constant looks like @samp{L} (upper
case!) followed immediately by an ordinary character constant (with no
intervening space).  Its data type is @code{wchar_t}, which is an
alias defined in @file{stddef.h} for one of the standard integer
types.  Depending on the platform, it could be 16 bits or 32 bits.  If
it is 16 bits, these character constants use the UTF-16 form of
Unicode; if 32 bits, UTF-32.

There are also Unicode wide character constants which explicitly
specify the width.  These constants start with @samp{u} or @samp{U}
instead of @samp{L}.  @samp{u} specifies a 16-bit Unicode wide
character constant, and @samp{U} a 32-bit Unicode wide character
constant.  Their types are, respectively, @code{char16_t} and
@w{@code{char32_t}}; they are declared in the header file
@file{uchar.h}.  These character constants are valid even if
@file{uchar.h} is not included, but some uses of them may be
inconvenient without including it to declare those type names.

The character represented in a wide character constant can be an
ordinary ASCII character.  @code{L'a'}, @code{u'a'} and @code{U'a'}
are all valid, and they are all equal to @code{'a'}.

In all three kinds of wide character constants, you can write a
non-ASCII Unicode character in the constant itself; the constant's
value is the character's Unicode character code.  Or you can specify
the Unicode character with an escape sequence (@pxref{Unicode
Character Codes}).

@node Wide String Constants
@section Wide String Constants
@cindex wide string constants
@cindex constants, wide string

A @dfn{wide string constant} stands for an array of 16-bit or 32-bit
characters.  They are rarely used; if you're just
learning C, you may as well skip this section.

There are three kinds of wide string constants, which differ in the
data type used for each character in the string.  Each wide string
constant is equivalent to an array of integers, but the data type of
those integers depends on the kind of wide string.  Using the constant
in an expression will convert the array to a pointer to its zeroth
element, as usual for arrays in C (@pxref{Accessing Array Elements}).
For each kind of wide string constant, we state here what type that
pointer will be.

@table @code
@item char16_t
This is a 16-bit Unicode wide string constant: each element is a
16-bit Unicode character code with type @code{char16_t}, so the string
has the array type @code{char16_t[]}.  (That is a type designator;
@pxref{Pointer Type Designators}.)  The constant is written as
@samp{u} (which must be lower case) followed (with no intervening
space) by a string constant with the usual syntax.

@item char32_t
This is a 32-bit Unicode wide string constant: each element is a
32-bit Unicode character code, and the string has type @code{char32_t[]}.
It's written as @samp{U} (which must be upper case) followed (with no
intervening space) by a string constant with the usual syntax.

@item wchar_t
This is the original kind of wide string constant.  It's written as
@samp{L} (which must be upper case) followed (with no intervening
space) by a string constant with the usual syntax, and the string has
type @code{wchar_t[]}.

The width of the data type @code{wchar_t} depends on the target
platform, which makes this kind of wide string somewhat less useful
than the newer kinds.
@end table

@code{char16_t} and @code{char32_t} are declared in the header file
@file{uchar.h}.  @code{wchar_t} is declared in @file{stddef.h}.

Consecutive wide string constants of the same kind concatenate, just
like ordinary string constants.  A wide string constant concatenated
with an ordinary string constant results in a wide string constant.
You can't concatenate two wide string constants of different kinds.
In addition, you can't concatenate a wide string constant (of any
kind) with a UTF-8 string constant.

@node Type Size
@chapter Type Size
@cindex type size
@cindex size of type
@findex sizeof

Each data type has a @dfn{size}, which is the number of bytes
(@pxref{Storage}) that it occupies in memory.  To refer to the size in
a C program, use @code{sizeof}.  There are two ways to use it:

@table @code
@item sizeof @var{expression}
This gives the size of @var{expression}, based on its data type.  It
does not calculate the value of @var{expression}, only its size, so if
@var{expression} includes side effects or function calls, they do not
happen.  Therefore, @code{sizeof} with an expression as argument is
always a compile-time operation that has zero run-time cost, unless it
applies to a variable-size array.

A value that is a bit field (@pxref{Bit Fields}) is not allowed as an
operand of @code{sizeof}.

For example,

@example
double a;

i = sizeof a + 10;
@end example

@noindent
sets @code{i} to 18 on most computers because @code{a} occupies 8 bytes.

Here's how to determine the number of elements in an array
@code{arr}:

@example
(sizeof arr / sizeof arr[0])
@end example

@noindent
The expression @code{sizeof arr} gives the size of the array, not
the size of a pointer to an element.  However, if @var{expression} is
a function parameter that was declared as an array, that
variable really has a pointer type (@pxref{Array Params are Ptrs}), so
the result is the size of that pointer.

@item sizeof (@var{type})
This gives the size of @var{type}.
For example,

@example
i = sizeof (double) + 10;
@end example

@noindent
is equivalent to the previous example.

@strong{Warning:} If @var{type} contains expressions which have side
effects, those expressions are actually computed and any side effects
in them do occur.

You can't apply @code{sizeof} to an incomplete type (@pxref{Incomplete
Types}).  Using it on a function type or @code{void} gives 1 in GNU C,
which makes adding an integer to these pointer types work as desired
(@pxref{Pointer Arithmetic}).
@end table

@strong{Warning}: When you use @code{sizeof} with a type
instead of an expression, you must write parentheses around the type.

@strong{Warning}: When applying @code{sizeof} to the result of a cast
(@pxref{Explicit Type Conversion}), you must write parentheses around
the cast expression to avoid an ambiguity in the grammar of C@.
Specifically,

@example
sizeof (int) -x
@end example

@noindent
parses as

@example
(sizeof (int)) - x
@end example

@noindent
If what you want is

@example
sizeof ((int) -x)
@end example

@noindent
you must write it that way, with parentheses.

The data type of the value of the @code{sizeof} operator is always an
unsigned integer type; which one of those types depends on the
machine.  The header file @code{stddef.h} defines @code{size_t} as a
name for such a type.  @xref{Defining Typedef Names}.

@node Pointers
@chapter Pointers
@cindex pointers

Among high-level languages, C is rather low-level, close to the
machine.  This is mainly because it has explicit @dfn{pointers}.  A
pointer value is the numeric address of data in memory.  The type of
data to be found at that address is specified by the data type of the
pointer itself.  Nothing in C can determine the ``correct'' data type
of data in memory; it can only blindly follow the data type of the
pointer you use to access the data.

The unary operator @samp{*} gets the data that a pointer points
to---this is called @dfn{dereferencing the pointer}.  Its value
always has the type that the pointer points to.

C also allows pointers to functions, but since there are some
differences in how they work, we treat them later.  @xref{Function
Pointers}.

@menu
* Address of Data::              Using the ``address-of'' operator.
* Pointer Types::                For each type, there is a pointer type.
* Pointer Declarations::         Declaring variables with pointer types.
* Pointer Type Designators::     Designators for pointer types.
* Pointer Dereference::          Accessing what a pointer points at.
* Null Pointers::                Pointers which do not point to any object.
* Invalid Dereference::          Dereferencing null or invalid pointers.
* Void Pointers::                Totally generic pointers, can cast to any.
* Pointer Comparison::           Comparing memory address values.
* Pointer Arithmetic::           Computing memory address values.
* Pointers and Arrays::          Using pointer syntax instead of array syntax.
* Low-Level Pointer Arithmetic:: More about computing memory address values.
* Pointer Increment/Decrement::  Incrementing and decrementing pointers.
* Pointer Arithmetic Drawbacks:: A common pointer bug to watch out for.
* Pointer-Integer Conversion::   Converting pointer types to integer types.
* Printing Pointers::            Using @code{printf} for a pointer's value.
@end menu

@node Address of Data
@section Address of Data

@cindex address-of operator
The most basic way to make a pointer is with the ``address-of''
operator, @samp{&}.  Let's suppose we have these variables available:

@example
int i;
double a[5];
@end example

Now, @code{&i} gives the address of the variable @code{i}---a pointer
value that points to @code{i}'s location---and @code{&a[3]} gives the
address of the element 3 of @code{a}.  (By the usual 1-origin
numbering convention of ordinary English, it is actually the fourth
element in the array, since the element at the start has index 0.)

The address-of operator is unusual because it operates on a place to
store a value (an lvalue, @pxref{Lvalues}), not on the value currently
stored there.  (The left argument of a simple assignment is unusual in
the same way.)  You can use it on any lvalue except a bit field
(@pxref{Bit Fields}) or a constructor (@pxref{Structure
Constructors}).


@node Pointer Types
@section Pointer Types

For each data type @var{t}, there is a type for pointers to type
@var{t}.  For these variables,

@example
int i;
double a[5];
@end example

@itemize @bullet
@item
@code{i} has type @code{int}; we say
@code{&i} is a ``pointer to @code{int}.''

@item
@code{a} has type @code{double[5]}; we say @code{&a} is a ``pointer to
an array of five @code{double}s.''

@item
@code{a[3]} has type @code{double}; we say @code{&a[3]} is a ``pointer
to @code{double}.''
@end itemize

@node Pointer Declarations
@section Pointer-Variable Declarations

The way to declare that a variable @code{foo} points to type @var{t} is

@example
@var{t} *foo;
@end example

To remember this syntax, think ``if you dereference @code{foo}, using
the @samp{*} operator, what you get is type @var{t}.  Thus, @code{foo}
points to type @var{t}.''

Thus, we can declare variables that hold pointers to these three
types, like this:

@example
int *ptri;            /* @r{Pointer to @code{int}.} */
double *ptrd;         /* @r{Pointer to @code{double}.} */
double (*ptrda)[5];   /* @r{Pointer to @code{double[5]}.} */
@end example

@samp{int *ptri;} means, ``if you dereference @code{ptri}, you get an
@code{int}.''  @samp{double (*ptrda)[5];} means, ``if you dereference
@code{ptrda}, then subscript it by an integer less than 5, you get a
@code{double}.''  The parentheses express the point that you would
dereference it first, then subscript it.

Contrast the last one with this:

@example
double *aptrd[5];     /* @r{Array of five pointers to @code{double}.} */
@end example

@noindent
Because @samp{*} has lower syntactic precedence than subscripting,
@samp{double *aptrd[5]} means, ``if you subscript @code{aptrd} by an
integer less than 5, then dereference it, you get a @code{double}.''
Therefore, @code{*aptrd[5]} declares an array of pointers, not a
pointer to an array.

@node Pointer Type Designators
@section Pointer-Type Designators

Every type in C has a designator; you make it by deleting the variable
name and the semicolon from a declaration (@pxref{Type
Designators}).  Here are the designators for the pointer
types of the example declarations in the previous section:

@example
int *           /* @r{Pointer to @code{int}.} */
double *        /* @r{Pointer to @code{double}.} */
double (*)[5]   /* @r{Pointer to @code{double[5]}.} */
@end example

Remember, to understand what type a designator stands for, imagine the
corresponding variable declaration with a variable name in it, and
figure out what type that variable would have.  Thus, the type
designator @code{double (*)[5]} corresponds to the variable declaration
@code{double (*@var{variable})[5]}.  That declares a pointer variable
which, when dereferenced, gives an array of 5 @code{double}s.
So the type designator means, ``pointer to an array of 5 @code{double}s.''

@node Pointer Dereference
@section Dereferencing Pointers
@cindex dereferencing pointers
@cindex pointer dereferencing

The main use of a pointer value is to @dfn{dereference it} (access the
data it points at) with the unary @samp{*} operator.  For instance,
@code{*&i} is the value at @code{i}'s address---which is just
@code{i}.  The two expressions are equivalent, provided @code{&i} is
valid.

A pointer-dereference expression whose type is data (not a function)
is an lvalue.

Pointers become really useful when we store them somewhere and use
them later.  Here's a simple example to illustrate the practice:

@example
@{
  int i;
  int *ptr;

  ptr = &i;

  i = 5;

  @r{@dots{}}

  return *ptr;   /* @r{Returns 5, fetched from @code{i}.}  */
@}
@end example

This shows how to declare the variable @code{ptr} as type
@code{int *} (pointer to @code{int}), store a pointer value into it
(pointing at @code{i}), and use it later to get the value of the
object it points at (the value in @code{i}).

Here is another example of using a pointer to a variable.

@example
/* @r{Define global variable @code{i}.}  */
int i = 2;

int
foo (void)
@{
  /* @r{Save global variable @code{i}'s address.}  */
  int *global_i = &i;

  /* @r{Declare local @code{i}, shadowing the global @code{i}.}  */
  int i = 5;

  /* @r{Print value of global @code{i} and value of local @code{i}.}  */
  printf ("global i: %d\nlocal i: %d\n", *global_i, i);
  return i;
@}
@end example

Of course, in a real program it would be much cleaner to use different
names for these two variables, rather than calling both of them
@code{i}.  But it is hard to illustrate this syntaxtical point with
clean code.  If anyone can provide a useful example to illustrate
this point with, that would be welcome.

@node Null Pointers
@section Null Pointers
@cindex null pointers
@cindex pointers, null

@c ???stdio loads sttddef

A pointer value can be @dfn{null}, which means it does not point to
any object.  The cleanest way to get a null pointer is by writing
@code{NULL}, a standard macro defined in @file{stddef.h}.  You can
also do it by casting 0 to the desired pointer type, as in
@code{(char *) 0}.  (The cast operator performs explicit type conversion;
@xref{Explicit Type Conversion}.)

You can store a null pointer in any lvalue whose data type
is a pointer type:

@example
char *foo;
foo = NULL;
@end example

These two, if consecutive, can be combined into a declaration with
initializer,

@example
char *foo = NULL;
@end example

You can also explicitly cast @code{NULL} to the specific pointer type
you want---it makes no difference.

@example
char *foo;
foo = (char *) NULL;
@end example

To test whether a pointer is null, compare it with zero or
@code{NULL}, as shown here:

@example
if (p != NULL)
  /* @r{@code{p} is not null.}  */
  operate (p);
@end example

Since testing a pointer for not being null is basic and frequent, all
but beginners in C will understand the conditional without need for
@code{!= NULL}:

@example
if (p)
  /* @r{@code{p} is not null.}  */
  operate (p);
@end example

@node Invalid Dereference
@section Dereferencing Null or Invalid Pointers

Trying to dereference a null pointer is an error.  On most platforms,
it generally causes a signal, usually @code{SIGSEGV}
(@pxref{Signals}).

@example
char *foo = NULL;
c = *foo;    /* @r{This causes a signal and terminates.}  */
@end example

@noindent
Likewise a pointer that has the wrong alignment for the target data type
(on most types of computer), or points to a part of memory that has
not been allocated in the process's address space.

The signal terminates the program, unless the program has arranged to
handle the signal (@pxref{Signal Handling, The GNU C Library, , libc,
The GNU C Library Reference Manual}).

However, the signal might not happen if the dereference is optimized
away.  In the example above, if you don't subsequently use the value
of @code{c}, GCC might optimize away the code for @code{*foo}.  You
can prevent such optimization using the @code{volatile} qualifier, as
shown here:

@example
volatile char *p;
volatile char c;
c = *p;
@end example

You can use this to test whether @code{p} points to unallocated
memory.  Set up a signal handler first, so the signal won't terminate
the program.

@node Void Pointers
@section Void Pointers
@cindex void pointers
@cindex pointers, void

The peculiar type @code{void *}, a pointer whose target type is
@code{void}, is used often in C@.  It represents a pointer to
we-don't-say-what.  Thus,

@example
void *numbered_slot_pointer (int);
@end example

@noindent
declares a function @code{numbered_slot_pointer} that takes an
integer parameter and returns a pointer, but we don't say what type of
data it points to.

The functions for dynamic memory allocation (@pxref{Dynamic Memory
Allocation}) use type @code{void *} to refer to blocks of memory,
regardless of what sort of data the program stores in those blocks.

With type @code{void *}, you can pass the pointer around and test
whether it is null.  However, dereferencing it gives a @code{void}
value that can't be used (@pxref{The Void Type}).  To dereference the
pointer, first convert it to some other pointer type.


Assignments convert @code{void *} automatically to any other pointer
type, if the left operand has a pointer type; for instance,

@example
@{
  int *p;
  /* @r{Converts return value to @code{int *}.}  */
  p = numbered_slot_pointer (5);
  @r{@dots{}}
@}
@end example

Passing an argument of type @code{void *} for a parameter that has a
pointer type also converts.  For example, supposing the function
@code{hack} is declared to require type @code{float *} for its
parameter, this call to @code{hack} will convert the argument to that
type.

@example
/* @r{Declare @code{hack} that way.}
   @r{We assume it is defined somewhere else.}  */
void hack (float *);
@dots{}
/* @r{Now call @code{hack}.}  */
@{
  /* @r{Converts return value of @code{numbered_slot_pointer}}
     @r{to @code{float *} to pass it to @code{hack}.}  */
  hack (numbered_slot_pointer (5));
  @r{@dots{}}
@}
@end example

  You can also convert to another pointer type with an explicit cast
(@pxref{Explicit Type Conversion}), like this:
@example
(int *) numbered_slot_pointer (5)
@end example

Here is an example which decides at run time which pointer
type to convert to:

@example
void
extract_int_or_double (void *ptr, bool its_an_int)
@{
  if (its_an_int)
    handle_an_int (*(int *)ptr);
  else
    handle_a_double (*(double *)ptr);
@}
@end example

The expression @code{*(int *)ptr} means to convert @code{ptr}
to type @code{int *}, then dereference it.

@node Pointer Comparison
@section Pointer Comparison
@cindex pointer comparison
@cindex comparison, pointer

Two pointer values are equal if they point to the same location, or if
they are both null.  You can test for this with @code{==} and
@code{!=}.  Here's a trivial example:

@example
@{
  int i;
  int *p, *q;

  p = &i;
  q = &i;
  if (p == q)
    printf ("This will be printed.\n");
  if (p != q)
    printf ("This won't be printed.\n");
@}
@end example

Ordering comparisons such as @code{>} and @code{>=} operate on
pointers by converting them to unsigned integers.  The C standard says
the two pointers must point within the same object in memory, but on
GNU/Linux systems these operations simply compare the numeric values
of the pointers.

The pointer values to be compared should in principle have the same type, but
they are allowed to differ in limited cases.  First of all, if the two
pointers' target types are nearly compatible (@pxref{Compatible
Types}), the comparison is allowed.

If one of the operands is @code{void *} (@pxref{Void Pointers}) and
the other is another pointer type, the comparison operator converts
the @code{void *} pointer to the other type so as to compare them.
(In standard C, this is not allowed if the other type is a function
pointer type, but it works in GNU C@.)

Comparison operators also allow comparing the integer 0 with a pointer
value.  This works by converting 0 to a null pointer of the same type
as the other operand.

@node Pointer Arithmetic
@section Pointer Arithmetic
@cindex pointer arithmetic
@cindex arithmetic, pointer

Adding an integer (positive or negative) to a pointer is valid in C@.
It assumes that the pointer points to an element in an array, and
advances or retracts the pointer across as many array elements as the
integer specifies.  Here is an example, in which adding a positive
integer advances the pointer to a later element in the same array.

@example
void
incrementing_pointers ()
@{
  int array[5] = @{ 45, 29, 104, -3, 123456 @};
  int elt0, elt1, elt4;

  int *p = &array[0];
  /* @r{Now @code{p} points at element 0.  Fetch it.}  */
  elt0 = *p;

  ++p;
  /* @r{Now @code{p} points at element 1.  Fetch it.}  */
  elt1 = *p;

  p += 3;
  /* @r{Now @code{p} points at element 4 (the last).  Fetch it.}  */
  elt4 = *p;

  printf ("elt0 %d  elt1 %d  elt4 %d.\n",
          elt0, elt1, elt4);
  /* @r{Prints elt0 45  elt1 29  elt4 123456.}  */
@}
@end example

Here's an example where adding a negative integer retracts the pointer
to an earlier element in the same array.

@example
void
decrementing_pointers ()
@{
  int array[5] = @{ 45, 29, 104, -3, 123456 @};
  int elt0, elt3, elt4;

  int *p = &array[4];
  /* @r{Now @code{p} points at element 4 (the last).  Fetch it.}  */
  elt4 = *p;

  --p;
  /* @r{Now @code{p} points at element 3.  Fetch it.}  */
  elt3 = *p;

  p -= 3;
  /* @r{Now @code{p} points at element 0.  Fetch it.}  */
  elt0 = *p;

  printf ("elt0 %d  elt3 %d  elt4 %d.\n",
          elt0, elt3, elt4);
  /* @r{Prints elt0 45  elt3 -3  elt4 123456.}  */
@}
@end example

If one pointer value was made by adding an integer to another
pointer value, it should be possible to subtract the pointer values
and recover that integer.  That works too in C@.

@example
void
subtract_pointers ()
@{
  int array[5] = @{ 45, 29, 104, -3, 123456 @};
  int *p0, *p3, *p4;

  int *p = &array[4];
  /* @r{Now @code{p} points at element 4 (the last).  Save the value.}  */
  p4 = p;

  --p;
  /* @r{Now @code{p} points at element 3.  Save the value.}  */
  p3 = p;

  p -= 3;
  /* @r{Now @code{p} points at element 0.  Save the value.}  */
  p0 = p;

  printf ("%d, %d, %d, %d\n",
          p4 - p0, p0 - p0, p3 - p0, p0 - p3);
  /* @r{Prints 4, 0, 3, -3.}  */
@}
@end example

The addition operation does not know where arrays begin or end in
memory.  All it does is add the integer (multiplied by target object
size) to the numeric value of the pointer.  When the initial pointer
and the result point into the same array, the result is well-defined.

@strong{Warning:} Only experts should do pointer arithmetic involving pointers
into different memory objects.

The difference between two pointers has type @code{int}, or
@code{long} if necessary (@pxref{Integer Types}).  The clean way to
declare it is to use the typedef name @code{ptrdiff_t} defined in the
file @file{stddef.h}.

C defines pointer subtraction to be consistent with pointer-integer
addition, so that @code{(p3 - p1) + p1} equals @code{p3}, as in
ordinary algebra.  Pointer subtraction works by subtracting
@code{p1}'s numeric value from @code{p3}'s, and dividing by target
object size.  The two pointer arguments should point into the same
array.

In standard C, addition and subtraction are not allowed on @code{void
*}, since the target type's size is not defined in that case.
Likewise, they are not allowed on pointers to function types.
However, these operations work in GNU C, and the ``size of the target
type'' is taken as 1 byte.

@node Pointers and Arrays
@section Pointers and Arrays
@cindex pointers and arrays
@cindex arrays and pointers

The clean way to refer to an array element is
@code{@var{array}[@var{index}]}.  Another, complicated way to do the
same job is to get the address of that element as a pointer, then
dereference it: @code{* (&@var{array}[0] + @var{index})} (or
equivalently @code{* (@var{array} + @var{index})}).  This first gets a
pointer to element zero, then increments it with @code{+} to point to
the desired element, then gets the value from there.

That pointer-arithmetic construct is the @emph{definition} of square
brackets in C@.  @code{@var{a}[@var{b}]} means, by definition,
@code{*(@var{a} + @var{b})}.  This definition uses @var{a} and @var{b}
symmetrically, so one must be a pointer and the other an integer; it
does not matter which comes first.

Since indexing with square brackets is defined in terms of addition
and dereferencing, that too is symmetrical.  Thus, you can write
@code{3[array]} and it is equivalent to @code{array[3]}.  However, it
would be foolish to write @code{3[array]}, since it has no advantage
and could confuse people who read the code.

It may seem like a discrepancy that the definition @code{*(@var{a} +
@var{b})} requires a pointer, while @code{array[3]} uses an array value
instead.  Why is this valid?  The name of the array, when used by
itself as an expression (other than in @code{sizeof}), stands for a
pointer to the array's zeroth element.  Thus, @code{array + 3}
converts @code{array} implicitly to @code{&array[0]}, and the result
is a pointer to element 3, equivalent to @code{&array[3]}.

Since square brackets are defined in terms of such an addition,
@code{array[3]} first converts @code{array} to a pointer.  That's why
it works to use an array directly in that construct.

@node Low-Level Pointer Arithmetic
@section Pointer Arithmetic at Low-Level
@cindex pointer arithmetic, low-level
@cindex low level pointer arithmetic

The behavior of pointer arithmetic is theoretically defined only when
the pointer values all point within one object allocated in memory.
But the addition and subtraction operators can't tell whether the
pointer values are all within one object.  They don't know where
objects start and end.  So what do they really do?

Adding pointer @var{p} to integer @var{i} treats @var{p} as a memory
address, which is in fact an integer---call it @var{pint}.  It treats
@var{i} as a number of elements of the type that @var{p} points to.
These elements' sizes add up to @code{@var{i} * sizeof (*@var{p})}.
So the sum, as an integer, is @code{@var{pint} + @var{i} * sizeof
(*@var{p})}.  This value is reinterpreted as a pointer of the same
type as @var{p}.

If the starting pointer value @var{p} and the result do not point at
parts of the same object, the operation is not officially legitimate,
and C code is not ``supposed'' to do it.  But you can do it anyway,
and it gives precisely the results described by the procedure above.
In some special situations it can do something useful, but non-wizards
should avoid it.

Here's a function to offset a pointer value @emph{as if} it pointed to
an object of any given size, by explicitly performing that calculation:

@example
#include <stdint.h>

void *
ptr_add (void *p, int i, int objsize)
@{
  intptr_t p_address = (long) p;
  intptr_t totalsize = i * objsize;
  intptr_t new_address = p_address + totalsize;
  return (void *) new_address;
@}
@end example

@noindent
@cindex @code{intptr_t}
This does the same job as @code{@var{p} + @var{i}} with the proper
pointer type for @var{p}.  It uses the type @code{intptr_t}, which is
defined in the header file @file{stdint.h}.  (In practice, @code{long
long} would always work, but it is cleaner to use @code{intptr_t}.)

@node Pointer Increment/Decrement
@section Pointer Increment and Decrement
@cindex pointer increment and decrement
@cindex incrementing pointers
@cindex decrementing pointers

The @samp{++} operator adds 1 to a variable.  We have seen it for
integers (@pxref{Increment/Decrement}), but it works for pointers too.
For instance, suppose we have a series of positive integers,
terminated by a zero, and we want to add them up.  Here is a simple
way to step forward through the array by advancing a pointer.

@example
int
sum_array_till_0 (int *p)
@{
  int sum = 0;

  for (;;)
    @{
      /* @r{Fetch the next integer.}  */
      int next = *p++;
      /* @r{Exit the loop if it's 0.}  */
      if (next == 0)
        break;
      /* @r{Add it into running total.}  */
      sum += next;
    @}

  return sum;
@}
@end example

@noindent
The statement @samp{break;} will be explained further on (@pxref{break
Statement}).  Used in this way, it immediately exits the surrounding
@code{for} statement.

@code{*p++} uses postincrement (@code{++};
@pxref{Postincrement/Postdecrement}) on the pointer @code{p}.  That
expression parses as @code{*(p++)}, because a postfix operator always
takes precedence over a prefix operator.  Therefore, it dereferences
the entering value of @code{p}, then increments @code{p} afterwards.

Incrementing a variable means adding 1 to it, as in @code{p = p + 1}.
Since @code{p} is a pointer, adding 1 to it advances it by the width
of the datum it points to---in this case, @code{sizeof (int)}.
Therefore, each iteration of the loop picks up the next integer from
the series and puts it into @code{next}.

This @code{for}-loop has no initialization expression since @code{p}
and @code{sum} are already initialized, has no end-test since the
@samp{break;} statement will exit it, and needs no expression to
advance it since that's done within the loop by incrementing @code{p}
and @code{sum}.  Thus, those three expressions after @code{for} are
left empty.

Another way to write this function is by keeping the parameter value unchanged
and using indexing to access the integers in the table.

@example
int
sum_array_till_0_indexing (int *p)
@{
  int i;
  int sum = 0;

  for (i = 0; ; i++)
    @{
      /* @r{Fetch the next integer.}  */
      int next = p[i];
      /* @r{Exit the loop if it's 0.}  */
      if (next == 0)
        break;
      /* @r{Add it into running total.}  */
      sum += next;
    @}

  return sum;
@}
@end example

In this program, instead of advancing @code{p}, we advance @code{i}
and add it to @code{p}.  (Recall that @code{p[i]} means @code{*(p +
i)}.)  Either way, it uses the same address to get the next integer.

It makes no difference in this program whether we write @code{i++} or
@code{++i}, because the value @emph{of that expression} is not used.
We use it for its effect, to increment @code{i}.

The @samp{--} operator also works on pointers; it can be used
to step backwards through an array, like this:

@example
int
after_last_nonzero (int *p, int len)
@{
  /* @r{Set up @code{q} to point just after the last array element.}  */
  int *q = p + len;

  while (q != p)
    /* @r{Step @code{q} back until it reaches a nonzero element.}  */
    if (*--q != 0)
      /* @r{Return the index of the element after that nonzero.}  */
      return q - p + 1;

  return 0;
@}
@end example

That function returns the length of the nonzero part of the
array specified by its arguments; that is, the index of the
first zero of the run of zeros at the end.

@node Pointer Arithmetic Drawbacks
@section Drawbacks of Pointer Arithmetic
@cindex drawbacks of pointer arithmetic
@cindex pointer arithmetic, drawbacks

Pointer arithmetic is clean and elegant, but it is also the cause of a
major security flaw in the C language.  Theoretically, it is only
valid to adjust a pointer within one object allocated as a unit in
memory.  However, if you unintentionally adjust a pointer across the
bounds of the object and into some other object, the system has no way
to detect this error.

A bug which does that can easily result in clobbering (overwriting)
part of another object.  For example, with @code{array[-1]} you can
read or write the nonexistent element before the beginning of an
array---probably part of some other data.

Combining pointer arithmetic with casts between pointer types, you can
create a pointer that fails to be properly aligned for its type.  For
example,

@example
int a[2];
char *pa = (char *)a;
int *p = (int *)(pa + 1);
@end example

@noindent
gives @code{p} a value pointing to an ``integer'' that includes part
of @code{a[0]} and part of @code{a[1]}.  Dereferencing that with
@code{*p} can cause a fatal @code{SIGSEGV} signal or it can return the
contents of that badly aligned @code{int} (@pxref{Signals}.  If it
``works,'' it may be quite slow.  It can also cause aliasing
confusions (@pxref{Aliasing}).

@strong{Warning:} Using improperly aligned pointers is risky---don't do it
unless it is really necessary.

@node Pointer-Integer Conversion
@section Pointer-Integer Conversion
@cindex pointer-integer conversion
@cindex conversion between pointers and integers
@cindex @code{uintptr_t}

On modern computers, an address is simply a number.  It occupies the
same space as some size of integer.  In C, you can convert a pointer
to the appropriate integer types and vice versa, without losing
information.  The appropriate integer types are @code{uintptr_t} (an
unsigned type) and @code{intptr_t} (a signed type).  Both are defined
in @file{stdint.h}.

For instance,

@example
#include <stdint.h>
#include <stdio.h>

void
print_pointer (void *ptr)
@{
  uintptr_t converted = (uintptr_t) ptr;

  printf ("Pointer value is 0x%x\n",
          (unsigned int) converted);
@}
@end example

@noindent
The specification @samp{%x} in the template (the first argument) for
@code{printf} means to represent this argument using hexadecimal
notation.  It's cleaner to use @code{uintptr_t}, since hexadecimal
printing treats the number as unsigned, but it won't actually matter:
all @code{printf} gets to see is the series of bits in the number.

@strong{Warning:} Converting pointers to integers is risky---don't do
it unless it is really necessary.

@node Printing Pointers
@section Printing Pointers

To print the numeric value of a pointer, use the @samp{%p} specifier.
For example:

@example
void
print_pointer (void *ptr)
@{
  printf ("Pointer value is %p\n", ptr);
@}
@end example

The specification @samp{%p} works with any pointer type.  It prints
@samp{0x} followed by the address in hexadecimal, printed as the
appropriate unsigned integer type.

@node Structures
@chapter Structures
@cindex structures
@findex struct
@cindex fields in structures
@cindex compound type

A @dfn{structure} is a user-defined data type that holds various
@dfn{fields} of data.  Each field has a name and a data type specified
in the structure's definition.  Because a structure combines various
fields, each of its own type, we call a structure type a @dfn{compound
type}.  (Union types are also compound types; @pxref{Unions}.)

Here we define a structure suitable for storing a linked list of
integers.  Each list item will hold one integer, plus a pointer
to the next item.

@example
struct intlistlink
  @{
    int datum;
    struct intlistlink *next;
  @};
@end example

The structure definition has a @dfn{type tag} so that the code can
refer to this structure.  The type tag here is @code{intlistlink}.
The definition refers recursively to the same structure through that
tag.

You can define a structure without a type tag, but then you can't
refer to it again.  That is useful only in some special contexts, such
as inside a @code{typedef} or a @code{union}.

The contents of the structure are specified by the @dfn{field
declarations} inside the braces.  Each field in the structure needs a
declaration there.  The fields in one structure definition must have
distinct names, but these names do not conflict with any other names
in the program.

A field declaration looks just like a variable declaration.  You can
combine field declarations with the same beginning, just as you can
combine variable declarations.

This structure has two fields.  One, named @code{datum}, has type
@code{int} and will hold one integer in the list.  The other, named
@code{next}, is a pointer to another @code{struct intlistlink}
which would be the rest of the list.  In the last list item, it would
be @code{NULL}.

This structure definition is recursive, since the type of the
@code{next} field refers to the structure type.  Such recursion is not
a problem; in fact, you can use the type @code{struct intlistlink *}
before the definition of the type @code{struct intlistlink} itself.
That works because pointers to all kinds of structures really look the
same at the machine level.

After defining the structure, you can declare a variable of type
@code{struct intlistlink} like this:

@example
struct intlistlink foo;
@end example

The structure definition itself can serve as the beginning of a
variable declaration, so you can declare variables immediately after,
like this:

@example
struct intlistlink
  @{
    int datum;
    struct intlistlink *next;
  @} foo;
@end example

@noindent
But that is ugly.  It is almost always clearer to separate the
definition of the structure from its uses.

Declaring a structure type inside a block (@pxref{Blocks}) limits
the scope of the structure type name to that block.  That means the
structure type is recognized only within that block.  Declaring it in
a function parameter list, as here,

@example
int f (struct foo @{int a, b@} parm);
@end example

@noindent
(assuming that @code{struct foo} is not already defined) limits the
scope of the structure type @code{struct foo} to that parameter list;
that is basically useless, so it triggers a warning.

Standard C requires at least one field in a structure.
GNU C does not require this.

@menu
* Referencing Fields::           Accessing field values in a structure object.
* Arrays as Fields::             Accessing arrays as structure fields.
* Dynamic Memory Allocation::    Allocating space for objects
                                   while the program is running.
* Field Offset::                 Memory layout of fields within a structure.
* Structure Layout::             Planning the memory layout of fields.
* Packed Structures::            Packing structure fields as close as possible.
* Bit Fields::                   Dividing integer fields
                                   into fields with fewer bits.
* Bit Field Packing::            How bit fields pack together in integers.
* const Fields::                 Making structure fields immutable.
* Zero Length::                  Zero-length array as a variable-length object.
* Flexible Array Fields::        Another approach to variable-length objects.
* Overlaying Structures::        Casting one structure type
                                   over an object of another structure type.
* Structure Assignment::         Assigning values to structure objects.
* Unions::                       Viewing the same object in different types.
* Packing With Unions::          Using a union type to pack various types into
                                   the same memory space.
* Cast to Union::                Casting a value one of the union's alternative
                                   types to the type of the union itself.
* Structure Constructors::       Building new structure objects.
* Unnamed Types as Fields::      Fields' types do not always need names.
* Incomplete Types::             Types which have not been fully defined.
* Intertwined Incomplete Types:: Defining mutually-recursive structure types.
* Type Tags::                    Scope of structure and union type tags.
@end menu

@node Referencing Fields
@section Referencing Structure Fields
@cindex referencing structure fields
@cindex structure fields, referencing

To make a structure useful, there has to be a way to examine and store
its fields.  The @samp{.} (period) operator does that; its use looks
like @code{@var{object}.@var{field}}.

Given this structure and variable,

@example
struct intlistlink
  @{
    int datum;
    struct intlistlink *next;
  @};

struct intlistlink foo;
@end example

@noindent
you can write @code{foo.datum} and @code{foo.next} to refer to the two
fields in the value of @code{foo}.  These fields are lvalues, so you
can store values into them, and read the values out again.

Most often, structures are dynamically allocated (see the next
section), and we refer to the objects via pointers.
@code{(*p).@var{field}} is somewhat cumbersome, so there is an
abbreviation: @code{p->@var{field}}.  For instance, assume the program
contains this declaration:

@example
struct intlistlink *ptr;
@end example

@noindent
You can write @code{ptr->datum} and @code{ptr->next} to refer
to the two fields in the object that @code{ptr} points to.

If a unary operator precedes an expression using @samp{->},
the @samp{->} nests inside:

@example
  -ptr->datum   @r{is equivalent to}   -(ptr->datum)
@end example

You can intermix @samp{->} and @samp{.} without parentheses,
as shown here:

@example
struct @{ double d; struct intlistlink l; @} foo;

@r{@dots{}}foo.l.next->next->datum@r{@dots{}}
@end example

@node Arrays as Fields
@section Arrays as Fields

When you declare field in a structure as an array, as here:

@example
struct record
  @{
    char *name;
    int data[4];
  @};
@end example

@noindent
Each @code{struct record} object holds one string (a pointer, of
course) and four integers, all part of a field called @code{data}.  If
@code{recptr} is a pointer of type @code{struct record *}, then it
points to a @code{struct record} which contains those things; you can
access the second integer in that record with @code{recptr->data[1]}.

If you have two objects of type @code{struct record}, each one contains
an array.  With this declaration,

@example
struct record r1, r2;
@end example

@code{r1.data} holds space for 4 @code{int}s, and @code{r2.data} holds
space for another 4 @code{int}s,

@node Dynamic Memory Allocation
@section Dynamic Memory Allocation
@cindex dynamic memory allocation
@cindex memory allocation, dynamic
@cindex allocating memory dynamically

To allocate an object dynamically, call the library function
@code{malloc} (@pxref{Basic Allocation, The GNU C Library,, libc, The GNU C Library
Reference Manual}).  Here is how to allocate an object of type
@code{struct intlistlink}.  To make this code work, include the file
@file{stdlib.h}, like this:

@example
#include <stddef.h>  /* @r{Defines @code{NULL}.} */
#include <stdlib.h>  /* @r{Declares @code{malloc}.}  */

@dots{}

struct intlistlink *
alloc_intlistlink ()
@{
  struct intlistlink *p;

  p = malloc (sizeof (struct intlistlink));

  if (p == NULL)
    fatal ("Ran out of storage");

  /* @r{Initialize the contents.} */
  p->datum = 0;
  p->next = NULL;

  return p;
@}
@end example

@noindent
@code{malloc} returns @code{void *}, so the assignment to @code{p}
will automatically convert it to type @code{struct intlistlink *}.
The return value of @code{malloc} is always sufficiently aligned
(@pxref{Type Alignment}) that it is valid for any data type.

The test for @code{p == NULL} is necessary because @code{malloc}
returns a null pointer if it cannot get any storage.  We assume that
the program defines the function @code{fatal} to report a fatal error
to the user.

Here's how to add one more integer to the front of such a list:

@example
struct intlistlink *my_list = NULL;

void
add_to_mylist (int my_int)
@{
  struct intlistlink *p = alloc_intlistlink ();

  p->datum = my_int;
  p->next = mylist;
  mylist = p;
@}
@end example

The way to free the objects is by calling @code{free}.  Here's
a function to free all the links in one of these lists:

@example
void
free_intlist (struct intlistlink *p)
@{
  while (p)
    @{
      struct intlistlink *q = p;
      p = p->next;
      free (q);
    @}
@}
@end example

We must extract the @code{next} pointer from the object before freeing
it, because @code{free} can clobber the data that was in the object.
For the same reason, the program must not use the list any more after
freeing its elements.  To make sure it won't, it is best to clear out
the variable where the list was stored, like this:

@example
free_intlist (mylist);

mylist = NULL;
@end example

@node Field Offset
@section Field Offset
@cindex field offset
@cindex structure field offset
@cindex offset of structure fields

To determine the offset of a given field @var{field} in a structure
type @var{type}, use the macro @code{offsetof}, which is defined in
the file @file{stddef.h}.  It is used like this:

@example
offsetof (@var{type}, @var{field})
@end example

Here is an example:

@example
struct foo
@{
  int element;
  struct foo *next;
@};

offsetof (struct foo, next)
/* @r{On most machines that is 4.  It may be 8.}  */
@end example

@node Structure Layout
@section Structure Layout
@cindex structure layout
@cindex layout of structures

The rest of this chapter covers advanced topics about structures.  If
you are just learning C, you can skip it.

The precise layout of a @code{struct} type is crucial when using it to
overlay hardware registers, to access data structures in shared
memory, or to assemble and disassemble packets for network
communication.  It is also important for avoiding memory waste when
the program makes many objects of that type.  However, the layout
depends on the target platform.  Each platform has conventions for
structure layout, which compilers need to follow.

Here are the conventions used on most platforms.

The structure's fields appear in the structure layout in the order
they are declared.  When possible, consecutive fields occupy
consecutive bytes within the structure.  However, if a field's type
demands more alignment than it would get that way, C gives it the
alignment it requires by leaving a gap after the previous field.

@cindex structure alignment
@cindex alignment of structures
Once all the fields have been laid out, it is possible to determine
the structure's alignment and size.  The structure's alignment is the
maximum alignment of any of the fields in it.  Then the structure's
size is rounded up to a multiple of its alignment.  That may require
leaving a gap at the end of the structure.

Here are some examples, where we assume that @code{char} has size and
alignment 1 (always true), and @code{int} has size and alignment 4
(true on most kinds of computers):

@example
struct foo
@{
  char a, b;
  int c;
@};
@end example

@noindent
This structure occupies 8 bytes, with an alignment of 4.  @code{a} is
at offset 0, @code{b} is at offset 1, and @code{c} is at offset 4.
There is a gap of 2 bytes before @code{c}.

Contrast that with this structure:

@example
struct foo
@{
  char a;
  int c;
  char b;
@};
@end example

This structure has size 12 and alignment 4.  @code{a} is at offset 0,
@code{c} is at offset 4, and @code{b} is at offset 8.  There are two
gaps: three bytes before @code{c}, and three bytes at the end.

These two structures have the same contents at the C level, but one
takes 8 bytes and the other takes 12 bytes due to the ordering of the
fields.  A reliable way to avoid this sort of wastage is to order the
fields by size, biggest fields first.

@node Packed Structures
@section Packed Structures
@cindex packed structures
@cindex @code{__attribute__((packed))}

In GNU C you can force a structure to be laid out with no gaps by
adding @code{__attribute__((packed))} after @code{struct} (or at the
end of the structure type declaration).  Here's an example:

@example
struct __attribute__((packed)) foo
@{
  char a;
  int c;
  char b;
@};
@end example

Without @code{__attribute__((packed))}, this structure occupies 12
bytes (as described in the previous section), assuming 4-byte
alignment for @code{int}.  With @code{__attribute__((packed))}, it is
only 6 bytes long---the sum of the lengths of its fields.

Use of @code{__attribute__((packed))} often results in fields that
don't have the normal alignment for their types.  Taking the address
of such a field can result in an invalid pointer because of its
improper alignment.  Dereferencing such a pointer can cause a
@code{SIGSEGV} signal on a machine that doesn't, in general, allow
unaligned pointers.

@xref{Attributes}.

@node Bit Fields
@section Bit Fields
@cindex bit fields

A structure field declaration with an integer type can specify the
number of bits the field should occupy.  We call that a @dfn{bit
field}.  These are useful because consecutive bit fields are packed
into a larger storage unit.  For instance,

@example
unsigned char opcode: 4;
@end example

@noindent
specifies that this field takes just 4 bits.
Since it is unsigned, its possible values range
from 0 to 15.  A signed field with 4 bits, such as this,

@example
signed char small: 4;
@end example

@noindent
can hold values from -8 to 7.

You can subdivide a single byte into those two parts by writing

@example
unsigned char opcode: 4;
signed char small: 4;
@end example

@noindent
in the structure.  With bit fields, these two numbers fit into
a single @code{char}.

Here's how to declare a one-bit field that can hold either 0 or 1:

@example
unsigned char special_flag: 1;
@end example

You can also use the @code{bool} type for bit fields:

@example
bool special_flag: 1;
@end example

Except when using @code{bool} (which is always unsigned,
@pxref{Boolean Type}), always specify @code{signed} or @code{unsigned}
for a bit field.  There is a default, if that's not specified: the bit
field is signed if plain @code{char} is signed, except that the option
@option{-funsigned-bitfields} forces unsigned as the default.  But it
is cleaner not to depend on this default.

Bit fields are special in that you cannot take their address with
@samp{&}.  They are not stored with the size and alignment appropriate
for the specified type, so they cannot be addressed through pointers
to that type.

@node Bit Field Packing
@section Bit Field Packing

Programs to communicate with low-level hardware interfaces need to
define bit fields laid out to match the hardware data.  This section
explains how to do that.

Consecutive bit fields are packed together, but each bit field must
fit within a single object of its specified type.  In this example,

@example
unsigned short a : 3, b : 3, c : 3, d : 3, e : 3;
@end example

@noindent
all five fields fit consecutively into one two-byte @code{short}.
They need 15 bits, and one @code{short} provides 16.  By contrast,

@example
unsigned char a : 3, b : 3, c : 3, d : 3, e : 3;
@end example

@noindent
needs three bytes.  It fits @code{a} and @code{b} into one
@code{char}, but @code{c} won't fit in that @code{char} (they would
add up to 9 bits).  So @code{c} and @code{d} go into a second
@code{char}, leaving a gap of two bits between @code{b} and @code{c}.
Then @code{e} needs a third @code{char}.  By contrast,

@example
unsigned char a : 3, b : 3;
unsigned int c : 3;
unsigned char d : 3, e : 3;
@end example

@noindent
needs only two bytes: the type @code{unsigned int}
allows @code{c} to straddle bytes that are in the same word.

You can leave a gap of a specified number of bits by defining a
nameless bit field.  This looks like @code{@var{type} : @var{nbits};}.
It is allocated space in the structure just as a named bit field would
be allocated.

You can force the following bit field to advance to the following
aligned memory object with @code{@var{type} : 0;}.

Both of these constructs can syntactically share @var{type} with
ordinary bit fields.  This example illustrates both:

@example
unsigned int a : 5, : 3, b : 5, : 0, c : 5, : 3, d : 5;
@end example

@noindent
It puts @code{a} and @code{b} into one @code{int}, with a 3-bit gap
between them.  Then @code{: 0} advances to the next @code{int},
so @code{c} and @code{d} fit into that one.

These rules for packing bit fields apply to most target platforms,
including all the usual real computers.  A few embedded controllers
have special layout rules.

@node const Fields
@section @code{const} Fields
@cindex const fields
@cindex structure fields, constant

@c ??? Is this a C standard feature?

A structure field declared @code{const} cannot be assigned to
(@pxref{const}).  For instance, let's define this modified version of
@code{struct intlistlink}:

@example
struct intlistlink_ro  /* @r{``ro'' for read-only.}  */
  @{
    const int datum;
    struct intlistlink *next;
  @};
@end example

This structure can be used to prevent part of the code from modifying
the @code{datum} field:

@example
/* @r{@code{p} has type @code{struct intlistlink *}.}
   @r{Convert it to @code{struct intlistlink_ro *}.}  */
struct intlistlink_ro *q
  = (struct intlistlink_ro *) p;

q->datum = 5;     /* @r{Error!} */
p->datum = 5;     /* @r{Valid since @code{*p} is}
                     @r{not a @code{struct intlistlink_ro}.}  */
@end example

A @code{const} field can get a value in two ways: by initialization of
the whole structure, and by making a pointer-to-structure point to an object
in which that field already has a value.

Any @code{const} field in a structure type makes assignment impossible
for structures of that type (@pxref{Structure Assignment}).  That is
because structure assignment works by assigning the structure's
fields, one by one.

@node Zero Length
@section Arrays of Length Zero
@cindex array of length zero
@cindex zero-length arrays
@cindex length-zero arrays

GNU C allows zero-length arrays.  They are useful as the last field
of a structure that is really a header for a variable-length object.
Here's an example, where we construct a variable-size structure
to hold a line which is @code{this_length} characters long:

@example
struct line @{
  int length;
  char contents[0];
@};

struct line *thisline
  = ((struct line *)
     malloc (sizeof (struct line)
             + this_length));
thisline->length = this_length;
@end example

In ISO C90, we would have to give @code{contents} a length of 1, which
means either wasting space or complicating the argument to @code{malloc}.

@node Flexible Array Fields
@section Flexible Array Fields
@cindex flexible array fields
@cindex array fields, flexible

The C99 standard adopted a more complex equivalent of zero-length
array fields.  It's called a @dfn{flexible array}, and it's indicated
by omitting the length, like this:

@example
struct line
@{
  int length;
  char contents[];
@};
@end example

The flexible array has to be the last field in the structure, and there
must be other fields before it.

Under the C standard, a structure with a flexible array can't be part
of another structure, and can't be an element of an array.

GNU C allows static initialization of flexible array fields.  The effect
is to ``make the array long enough'' for the initializer.

@example
struct f1 @{ int x; int y[]; @} f1
  = @{ 1, @{ 2, 3, 4 @} @};
@end example

@noindent
This defines a structure variable named @code{f1}
whose type is @code{struct f1}.  In C, a variable name or function name
never conflicts with a structure type tag.

Omitting the flexible array field's size lets the initializer
determine it.  This is allowed only when the flexible array is defined
in the outermost structure and you declare a variable of that
structure type.  For example:

@example
struct foo @{ int x; int y[]; @};
struct bar @{ struct foo z; @};

struct foo a = @{ 1, @{ 2, 3, 4 @} @};        // @r{Valid.}
struct bar b = @{ @{ 1, @{ 2, 3, 4 @} @} @};    // @r{Invalid.}
struct bar c = @{ @{ 1, @{ @} @} @};            // @r{Valid.}
struct foo d[1] = @{ @{ 1 @{ 2, 3, 4 @} @} @};  // @r{Invalid.}
@end example

@node Overlaying Structures
@section Overlaying Different Structures
@cindex overlaying structures
@cindex structures, overlaying

Be careful about using different structure types to refer to the same
memory within one function, because GNU C can optimize code assuming
it never does that.  @xref{Aliasing}.  Here's an example of the kind of
aliasing that can cause the problem:

@example
struct a @{ int size; char *data; @};
struct b @{ int size; char *data; @};
struct a foo;
struct a *p = &foo;
struct b *q = (struct b *) &foo;
@end example

Here @code{q} points to the same memory that the variable @code{foo}
occupies, but they have two different types.  The two types
@code{struct a} and @code{struct b} are defined alike, but they are
not the same type.  Interspersing references using the two types,
like this,

@example
p->size = 0;
q->size = 1;
x = p->size;
@end example

@noindent
allows GNU C to assume that @code{p->size} is still zero when it is
copied into @code{x}.  The GNU C compiler ``knows'' that @code{q}
points to a @code{struct b} and this is not supposed to overlap with a
@code{struct a}.  Other compilers might also do this optimization.

The ISO C standard considers such code erroneous, precisely so that
this optimization will not be incorrect.

@node Structure Assignment
@section Structure Assignment
@cindex structure assignment
@cindex assigning structures

Assignment operating on a structure type copies the structure.  The
left and right operands must have the same type.  Here is an example:

@example
#include <stddef.h>  /* @r{Defines @code{NULL}.} */
#include <stdlib.h>  /* @r{Declares @code{malloc}.}  */
@r{@dots{}}

struct point @{ double x, y; @};

struct point *
copy_point (struct point point)
@{
  struct point *p
    = (struct point *) malloc (sizeof (struct point));
  if (p == NULL)
    fatal ("Out of memory");
  *p = point;
  return p;
@}
@end example

Notionally, assignment on a structure type works by copying each of
the fields.  Thus, if any of the fields has the @code{const}
qualifier, that structure type does not allow assignment:

@example
struct point @{ const double x, y; @};

struct point a, b;

a = b;            /* @r{Error!} */
@end example

@xref{Assignment Expressions}.

When a structure type has a field which is an array, as here,

@example
struct record
  @{
    char *name;
    int data[4];
  @};

struct record r1, r2;
@end example

@noindent
structure assignment such as @code{r1 = r2} copies array fields'
contents just as it copies all the other fields.

This is the only way in C that you can operate on the whole contents
of a array with one operation: when the array is contained in a
@code{struct}.  You can't copy the contents of the @code{data} field
as an array, because

@example
r1.data = r2.data;
@end example

@noindent
would convert the array objects (as always) to pointers to the zeroth
elements of the arrays (of type @code{int *}), and the
assignment would be invalid because the left operand is not an lvalue.

@node Unions
@section Unions
@cindex unions
@findex union

A @dfn{union type} defines alternative ways of looking at the same
piece of memory.  Each alternative view is defined with a data type,
and identified by a name.  Because a union combines various types, it
is considered a @dfn{compound type}, like structures
(@pxref{Structures}).  A union definition looks like this:

@example
union @var{name}
@{
  @var{alternative declarations}@r{@dots{}}
@};
@end example

Each alternative declaration looks like a structure field declaration,
except that it can't be a bit field.  For instance,

@example
union number
@{
  long int integer;
  double float;
@}
@end example

@noindent
lets you store either an integer (type @code{long int}) or a floating
point number (type @code{double}) in the same place in memory.  The
length and alignment of the union type are the maximum of all the
alternatives---they do not have to be the same.  In this union
example, @code{double} probably takes more space than @code{long int},
but that doesn't cause a problem in programs that use the union in the
normal way.

The members don't have to be different in data type.  Sometimes
each member pertains to a way the data will be used.  For instance,

@example
union datum
@{
  double latitude;
  double longitude;
  double height;
  double weight;
  int continent;
@}
@end example

This union holds one of several kinds of data; most kinds are floating
points, but the value can also be a code for a continent which is an
integer.  You @emph{could} use one member of type @code{double} to
access all the values which have that type, but the different member
names will make the program clearer.

The alignment of a union type is the maximum of the alignments of the
alternatives.  The size of the union type is the maximum of the sizes
of the alternatives, rounded up to a multiple of the alignment
(because every type's size must be a multiple of its alignment).

All the union alternatives start at the address of the union itself.
If an alternative is shorter than the union as a whole, it occupies
the first part of the union's storage, leaving the last part unused
@emph{for that alternative}.

@strong{Warning:} If the code stores data using one union alternative
and accesses it with another, the results depend on the kind of
computer in use.  Only wizards should try to do this.  However, when
you need to do this, a union is a clean way to do it.

Assignment works on any union type by copying the entire value.

@node Packing With Unions
@section Packing With Unions

Sometimes we design a union with the intention of packing various
kinds of objects into a certain amount of memory space.  For example.

@example
union bytes8
@{
  long long big_int_elt;
  double double_elt;
  struct @{ int first, second; @} two_ints;
  struct @{ void *first, *second; @} two_ptrs;
@};

union bytes8 *p;
@end example

This union makes it possible to look at 8 bytes of data that @code{p}
points to as a single 8-byte integer (@code{p->big_int_elt}), as a
single floating-point number (@code{p->double_elt}), as a pair of
integers (@code{p->two_ints.first} and @code{p->two_ints.second}), or
as a pair of pointers (@code{p->two_ptrs.first} and
@code{p->two_ptrs.second}).

To pack storage with such a union makes assumptions about the sizes of
all the types involved.  This particular union was written expecting a
pointer to have the same size as @code{int}.  On a machine where one
pointer takes 8 bytes, the code using this union probably won't work
as expected.  The union, as such, will function correctly---if you
store two values through @code{two_ints} and extract them through
@code{two_ints}, you will get the same integers back---but the part of
the program that expects the union to be 8 bytes long could
malfunction, or at least use too much space.

The above example shows one case where a @code{struct} type with no
tag can be useful.  Another way to get effectively the same result
is with arrays as members of the union:

@example
union eight_bytes
@{
  long long big_int_elt;
  double double_elt;
  int two_ints[2];
  void *two_ptrs[2];
@};
@end example

@node Cast to Union
@section Cast to a Union Type
@cindex cast to a union
@cindex union, casting to a

In GNU C, you can explicitly cast any of the alternative types to the
union type; for instance,

@example
(union eight_bytes) (long long) 5
@end example

@noindent
makes a value of type @code{union eight_bytes} which gets its contents
through the alternative named @code{big_int_elt}.

The value being cast must exactly match the type of the alternative,
so this is not valid:

@example
(union eight_bytes) 5  /* @r{Error!  5 is @code{int}.} */
@end example

A cast to union type looks like any other cast, except that the type
specified is a union type.  You can specify the type either with
@code{union @var{tag}} or with a typedef name (@pxref{Defining
Typedef Names}).

Using the cast as the right-hand side of an assignment to a variable of
union type is equivalent to storing in an alternative of the union:

@example
/* @r{Define the union @code{foo}.}  */
union foo @{ int i; double d; @};

/* @r{Declare the union-valued variable, @code{u}.}  */
union foo u;

int x;  double y;

u = (union foo) x   @r{means}   u.i = x

u = (union foo) y   @r{means}   u.d = y
@end example

You can also use the union cast as a function argument:

@example
void hack (union foo);
@r{@dots{}}
hack ((union foo) x);
@end example

@node Structure Constructors
@section Structure Constructors
@cindex structure constructors
@cindex constructors, structure

You can construct a structure value by writing its type in
parentheses, followed by an initializer that would be valid in a
declaration for that type.  For instance, given this declaration,

@example
struct foo @{int a; char b[2];@} structure;
@end example

@noindent
you can create a @code{struct foo} value as follows:

@example
((struct foo) @{x + y, 'a', 0@})
@end example

@noindent
This specifies @code{x + y} for field @code{a},
the character @samp{a} for field @code{b}'s element 0,
and the null character for field @code{b}'s element 1.

The parentheses around that constructor are not necessary, but we
recommend writing them to make the nesting of the containing
expression clearer.

You can also show the nesting of the two by writing it like
this:

@example
((struct foo) @{x + y, @{'a', 0@} @})
@end example

Each of those is equivalent to writing the following statement
expression (@pxref{Statement Exprs}):

@example
(@{
  struct foo temp = @{x + y, 'a', 0@};
  temp;
@})
@end example

You can also use field labels in the structure constructor to indicate
which fields you're specifying values for, instead of using the order
of the fields to specify that:

@example
(struct foo) @{.a = x + y, .b = @{'a', 0@}@}
@end example

You can also create a union value this way, but it is not especially
useful since that is equivalent to doing a cast:

@example
  ((union whosis) @{@var{value}@})
@r{is equivalent to}
  ((union whosis) (@var{value}))
@end example

@node Unnamed Types as Fields
@section Unnamed Types as Fields
@cindex unnamed structures
@cindex unnamed unions
@cindex structures, unnamed
@cindex unions, unnamed

A structure or a union can contain, as fields,
unnamed structures and unions.  Here's an example:

@example
struct
@{
  int a;
  union
  @{
    int b;
    float c;
  @};
  int d;
@} foo;
@end example

@noindent
You can access the fields of the unnamed union within @code{foo} as if they
were individual fields at the same level as the union definition:

@example
foo.a = 42;
foo.b = 47;
foo.c = 5.25; // @r{Overwrites the value in @code{foo.b}}.
foo.d = 314;
@end example

Avoid using field names that could cause ambiguity.  For example, with
this definition:

@example
struct
@{
  int a;
  struct
  @{
    int a;
    float b;
  @};
@} foo;
@end example

@noindent
it is impossible to tell what @code{foo.a} refers to.  GNU C reports
an error when a definition is ambiguous in this way.

@node Incomplete Types
@section Incomplete Types
@cindex incomplete types
@cindex types, incomplete

A type that has not been fully defined is called an @dfn{incomplete
type}.  Structure and union types are incomplete when the code makes a
forward reference, such as @code{struct foo}, before defining the
type.  An array type is incomplete when its length is unspecified.

You can't use an incomplete type to declare a variable or field, or
use it for a function parameter or return type.  The operators
@code{sizeof} and @code{_Alignof} give errors when used on an
incomplete type.

However, you can define a pointer to an incomplete type, and declare a
variable or field with such a pointer type.  In general, you can do
everything with such pointers except dereference them, increment or
decrement them, or do pointer arithmetic with them (not even @code{p +
0}).  For example:

@example
extern void bar (struct mysterious_value *);

void
foo (struct mysterious_value *arg)
@{
  bar (arg);
@}

@r{@dots{}}

@{
  struct mysterious_value *p, **q;

  p = *q;
  foo (p);
@}
@end example

@noindent
These examples are valid because the code doesn't try to understand
what @code{p} points to; it just passes the pointer around.
(Presumably @code{bar} is defined in some other file that really does
have a definition for @code{struct mysterious_value}.)  However,
dereferencing the pointer would get an error; that requires a
definition for the structure type.

@node Intertwined Incomplete Types
@section Intertwined Incomplete Types

When several structure types contain pointers to each other, you can
define the types in any order because pointers to types that come
later are incomplete types.  Here is an example.

@example
/* @r{An employee record points to a group.}  */
struct employee
@{
  char *name;
  @r{@dots{}}
  struct group *group;  /* @r{incomplete type.}  */
  @r{@dots{}}
@};

/* @r{An employee list points to employees.}  */
struct employee_list
@{
  struct employee *this_one;
  struct employee_list *next;  /* @r{incomplete type.}  */
  @r{@dots{}}
@};

/* @r{A group points to one employee_list.}  */
struct group
@{
  char *name;
  @r{@dots{}}
  struct employee_list *employees;
  @r{@dots{}}
@};
@end example

@node Type Tags
@section Type Tags
@cindex type tags

The name that follows @code{struct} (@pxref{Structures}), @code{union}
(@pxref{Unions}, or @code{enum} (@pxref{Enumeration Types}) is called
a @dfn{type tag}.  In C, a type tag never conflicts with a variable
name or function name; the type tags have a separate @dfn{name space}.
Thus, there is no name conflict in this code:

@example
struct pair @{ int a, b; @};
int pair = 1;
@end example

@noindent
nor in this one:

@example
struct pair @{ int a, b; @} pair;
@end example

@noindent
where @code{pair} is both a structure type tag and a variable name.

However, @code{struct}, @code{union}, and @code{enum} share the same
name space of tags, so this is a conflict:

@example
struct pair @{ int a, b; @};
enum pair @{ c, d @};
@end example

@noindent
and so is this:

@example
struct pair @{ int a, b; @};
struct pair @{ int c, d; @};
@end example

When the code defines a type tag inside a block, the tag's scope is
limited to that block (as for local variables).  Two definitions for
one type tag do not conflict if they are in different scopes; rather,
each is valid in its scope.  For example,

@example
struct pair @{ int a, b; @};

void
pair_up_doubles (int len, double array[])
@{
  struct pair @{ double a, b; @};
  @r{@dots{}}
@}
@end example

@noindent
has two definitions for @code{struct pair} which do not conflict.  The
one inside the function applies only within the definition of
@code{pair_up_doubles}.  Within its scope, that definition
@dfn{shadows} the outer definition.

If @code{struct pair} appears inside the function body, before the
inner definition, it refers to the outer definition---the only one
that has been seen at that point.  Thus, in this code,

@example
struct pair @{ int a, b; @};

void
pair_up_doubles (int len, double array[])
@{
  struct two_pairs @{ struct pair *p, *q; @};
  struct pair @{ double a, b; @};
  @r{@dots{}}
@}
@end example

@noindent
the structure @code{two_pairs} has pointers to the outer definition of
@code{struct pair}, which is probably not desirable.

To prevent that, you can write @code{struct pair;} inside the function
body as a variable declaration with no variables.  This is a
@dfn{forward declaration} of the type tag @code{pair}: it makes the
type tag local to the current block, with the details of the type to
come later.  Here's an example:

@example
void
pair_up_doubles (int len, double array[])
@{
  /* @r{Forward declaration for @code{pair}.}  */
  struct pair;
  struct two_pairs @{ struct pair *p, *q; @};
  /* @r{Give the details.}  */
  struct pair @{ double a, b; @};
  @r{@dots{}}
@}
@end example

However, the cleanest practice is to avoid shadowing type tags.

@node Arrays
@chapter Arrays
@cindex array
@cindex elements of arrays

An @dfn{array} is a data object that holds a series of @dfn{elements},
all of the same data type.  Each element is identified by its numeric
@var{index} within the array.

We presented arrays of numbers in the sample programs early in this
manual (@pxref{Array Example}).  However, arrays can have elements of
any data type, including pointers, structures, unions, and other
arrays.

If you know another programming language, you may suppose that you know all
about arrays, but C arrays have special quirks, so in this chapter we
collect all the information about arrays in C@.

The elements of a C array are allocated consecutively in memory,
with no gaps between them.  Each element is aligned as required
for its data type (@pxref{Type Alignment}).

@menu
* Accessing Array Elements::     How to access individual elements of an array.
* Declaring an Array::           How to name and reserve space for a new array.
* Strings::                      A string in C is a special case of array.
* Array Type Designators::       Referring to a specific array type.
* Incomplete Array Types::       Naming, but not allocating, a new array.
* Limitations of C Arrays::      Arrays are not first-class objects.
* Multidimensional Arrays::      Arrays of arrays.
* Constructing Array Values::    Assigning values to an entire array at once.
* Arrays of Variable Length::    Declaring arrays of non-constant size.
@end menu

@node Accessing Array Elements
@section Accessing Array Elements
@cindex accessing array elements
@cindex array elements, accessing

If the variable @code{a} is an array, the @var{n}th element of
@code{a} is @code{a[@var{n}]}.  You can use that expression to access
an element's value or to assign to it:

@example
x = a[5];
a[6] = 1;
@end example

@noindent
Since the variable @code{a} is an lvalue, @code{a[@var{n}]} is also an
lvalue.

The lowest valid index in an array is 0, @emph{not} 1, and the highest
valid index is one less than the number of elements.

The C language does not check whether array indices are in bounds, so
if the code uses an out-of-range index, it will access memory outside the
array.

@strong{Warning:} Using only valid index values in C is the
programmer's responsibility.

Array indexing in C is not a primitive operation: it is defined in
terms of pointer arithmetic and dereferencing.  Now that we know
@emph{what} @code{a[i]} does, we can ask @emph{how} @code{a[i]} does
its job.

In C, @code{@var{x}[@var{y}]} is an abbreviation for
@code{*(@var{x}+@var{y})}.  Thus, @code{a[i]} really means
@code{*(a+i)}.  @xref{Pointers and Arrays}.

When an expression with array type (such as @code{a}) appears as part
of a larger C expression, it is converted automatically to a pointer
to element zero of that array.  For instance, @code{a} in an
expression is equivalent to @code{&a[0]}.  Thus, @code{*(a+i)} is
computed as @code{*(&a[0]+i)}.

Now we can analyze how that expression gives us the desired element of
the array.  It makes a pointer to element 0 of @code{a}, advances it
by the value of @code{i}, and dereferences that pointer.

Another equivalent way to write the expression is @code{(&a[0])[i]}.

@node Declaring an Array
@section Declaring an Array
@cindex declaring an array
@cindex array, declaring

To make an array declaration, write @code{[@var{length}]} after the
name being declared.  This construct is valid in the declaration of a
variable, a function parameter, a function value type (the value can't
be an array, but it can be a pointer to one), a structure field, or a
union alternative.

The surrounding declaration specifies the element type of the array;
that can be any type of data, but not @code{void} or a function type.
For instance,

@example
double a[5];
@end example

@noindent
declares @code{a} as an array of 5 @code{double}s.

@example
struct foo bstruct[length];
@end example

@noindent
declares @code{bstruct} as an array of @code{length} objects of type
@code{struct foo}.  A variable array size like this is allowed when
the array is not file-scope.

Other declaration constructs can nest within the array declaration
construct.  For instance:

@example
struct foo *b[length];
@end example

@noindent
declares @code{b} as an array of @code{length} pointers to
@code{struct foo}.  This shows that the length need not be a constant
(@pxref{Arrays of Variable Length}).

@example
double (*c)[5];
@end example

@noindent
declares @code{c} as a pointer to an array of 5 @code{double}s, and

@example
char *(*f (int))[5];
@end example

@noindent
declares @code{f} as a function taking an @code{int} argument and
returning a pointer to an array of 5 strings (pointers to
@code{char}s).

@example
double aa[5][10];
@end example

@noindent
declares @code{aa} as an array of 5 elements, each of which is an
array of 10 @code{double}s.  This shows how to declare a
multidimensional array in C (@pxref{Multidimensional Arrays}).

All these declarations specify the array's length, which is needed in
these cases in order to allocate storage for the array.

@node Strings
@section Strings
@cindex string

A string in C is a sequence of elements of type @code{char},
terminated with the null character, the character with code zero.
However, the C code that operates on strings normally uses
the pointer type @code{char *} to do it.

Programs often need to use strings with specific, fixed contents.  To
write one in a C program, use a @dfn{string constant} such as
@code{"Take me to your leader!"}.  The data type of a string constant
is @code{char *}.  For the full syntactic details of writing string
constants, @ref{String Constants}.

To declare a place to store a non-constant string, declare an array of
@code{char}.  Keep in mind that it must include one extra @code{char}
for the terminating null.  For instance,

@example
char text[] = @{ 'H', 'e', 'l', 'l', 'o', 0 @};
@end example

@noindent
declares an array named @samp{text} with six elements---five letters
and the terminating null character.  An equivalent way to get the same
result is this,

@example
char text[] = "Hello";
@end example

@noindent
which copies the elements of the string constant, including @emph{its}
terminating null character.

@example
char message[200];
@end example

@noindent
declares an array long enough to hold a string of 199 ASCII characters
plus the terminating null character.

When you store a string into @code{message} be sure to check or prove
that the length does not exceed its size.  For example,

@example
void
set_message (char *text)
@{
  int i;
  /* @r{Recall that @code{message} is declared above.}  */
  for (i = 0; i < sizeof (message); i++)
    @{
      message[i] = text[i];
      if (text[i] == 0)
        return;
    @}
  fatal_error ("Message is too long for `message'\n");
@}
@end example

It's easy to do this with the standard library function
@code{strncpy}, which fills out the whole destination array (up to a
specified length) with null characters.  Thus, if the last character
of the destination is not null, the string did not fit.  Many system
libraries, including the GNU C library, hand-optimize @code{strncpy}
to run faster than an explicit @code{for}-loop.

Here's what the code looks like:

@example
void
set_message (char *text)
@{
  strncpy (message, text, sizeof (message));
  if (message[sizeof (message) - 1] != 0)
    fatal_error ("Message is too long for `message');
@}
@end example

@xref{String and Array Utilities, The GNU C Library, , libc, The GNU C
Library Reference Manual}, for more information about the standard
library functions for operating on strings.

You can avoid putting a fixed length limit on strings you construct or
operate on by allocating the space for them dynamically.
@xref{Dynamic Memory Allocation}.

@node Array Type Designators
@section Array Type Designators

Every C type has a type designator, which you make by deleting the
variable name and the semicolon from a declaration (@pxref{Type
Designators}).  The designators for array types follow this rule, but
they may appear surprising.

@example
@r{type}   int a[5];           @r{designator}   int [5]
@r{type}   double a[5][3];     @r{designator}   double [5][3]
@r{type}   struct foo *a[5];   @r{designator}   struct foo *[5]
@end example

@node Incomplete Array Types
@section Incomplete Array Types
@cindex incomplete array types
@cindex array types, incomplete

An array is equivalent, for most purposes, to a pointer to its zeroth
element.  When that is true, the length of the array is irrelevant.
The length needs to be known only for allocating space for the array, or
for @code{sizeof} and @code{typeof} (@pxref{Auto Type}).  Thus, in some
contexts C allows

@itemize @bullet
@item
An @code{extern} declaration says how to refer to a variable allocated
elsewhere.  It does not need to allocate space for the variable,
so if it is an array, you can omit the length.  For example,

@example
extern int foo[];
@end example

@item
When declaring a function parameter as an array, the argument value
passed to the function is really a pointer to the array's zeroth
element.  This value does not say how long the array really is, there
is no need to declare it.  For example,

@example
int
func (int foo[])
@end example
@end itemize

These declarations are examples of @dfn{incomplete} array types, types
that are not fully specified.  The incompleteness makes no difference
for accessing elements of the array, but it matters for some other
things.  For instance, @code{sizeof} is not allowed on an incomplete
type.

With multidimensional arrays, only the first dimension can be omitted.
For example, suppose we want to represent the positions of pieces on a
chessboard which has the usual 8 files (columns), but more (or fewer)
ranks (rows) than the usual 8.  This declaration could hold a pointer
to a two-dimensional array that can hold that data.  Each element of
the array holds one row.

@example
struct chesspiece *funnyboard[][8];
@end example

Since it is just a pointer to the start of an array, its type can be
incomplete, but it must state how big each array element is---the
number of elements in each row.

@node Limitations of C Arrays
@section Limitations of C Arrays
@cindex limitations of C arrays
@cindex first-class object

Arrays have quirks in C because they are not ``first-class objects'':
there is no way in C to operate on an array as a unit.

The other composite objects in C, structures and unions, are
first-class objects: a C program can copy a structure or union value
in an assignment, or pass one as an argument to a function, or make a
function return one.  You can't do those things with an array in C@.
That is because a value you can operate on never has an array type.

An expression in C can have an array type, but that doesn't produce
the array as a value.  Instead it is converted automatically to a
pointer to the array's element at index zero.  The code can operate
on the pointer, and through that on individual elements of the array,
but it can't get and operate on the array as a unit.

There are three exceptions to this conversion rule, but none of them
offers a way to operate on the array as a whole.

First, @samp{&} applied to an expression with array type gives you the
address of the array, as an array type.  However, you can't operate on the
whole array that way---if you apply @samp{*} to get the array back,
that expression converts, as usual, to a pointer to its zeroth
element.

Second, the operators @code{sizeof}, @code{_Alignof}, and
@code{typeof} do not convert the array to a pointer; they leave it as
an array.  But they don't operate on the array's data---they only give
information about its type.

Third, a string constant used as an initializer for an array is not
converted to a pointer---rather, the declaration copies the
@emph{contents} of that string in that one special case.

You @emph{can} copy the contents of an array, just not with an
assignment operator.  You can do it by calling the library function
@code{memcpy} or @code{memmove} (@pxref{Copying and Concatenation, The
GNU C Library, , libc, The GNU C Library Reference Manual}).  Also,
when a structure contains just an array, you can copy that structure.

An array itself is an lvalue if it is a declared variable, or part of
a structure or union that is an lvalue.  When you construct an array
from elements (@pxref{Constructing Array Values}), that array is not
an lvalue.

@node Multidimensional Arrays
@section Multidimensional Arrays
@cindex multidimensional arrays
@cindex array, multidimensional

Strictly speaking, all arrays in C are unidimensional.  However, you
can create an array of arrays, which is more or less equivalent to a
multidimensional array.  For example,

@example
struct chesspiece *board[8][8];
@end example

@noindent
declares an array of 8 arrays of 8 pointers to @code{struct
chesspiece}.  This data type could represent the state of a chess
game.  To access one square's contents requires two array index
operations, one for each dimension.  For instance, you can write
@code{board[row][column]}, assuming @code{row} and @code{column}
are variables with integer values in the proper range.

How does C understand @code{board[row][column]}?  First of all,
@code{board} is converted automatically to a pointer to the zeroth
element (at index zero) of @code{board}.  Adding @code{row} to that
makes it point to the desired element.  Thus, @code{board[row]}'s
value is an element of @code{board}---an array of 8 pointers.

However, as an expression with array type, it is converted
automatically to a pointer to the array's zeroth element.  The second
array index operation, @code{[column]}, accesses the chosen element
from that array.

As this shows, pointer-to-array types are meaningful in C@.
You can declare a variable that points to a row in a chess board
like this:

@example
struct chesspiece *(*rowptr)[8];
@end example

@noindent
This points to an array of 8 pointers to @code{struct chesspiece}.
You can assign to it as follows:

@example
rowptr = &board[5];
@end example

The dimensions don't have to be equal in length.  Here we declare
@code{statepop} as an array to hold the population of each state in
the United States for each year since 1900:

@example
#define NSTATES 50
@{
  int nyears = current_year - 1900 + 1;
  int statepop[NSTATES][nyears];
  @r{@dots{}}
@}
@end example

The variable @code{statepop} is an array of @code{NSTATES} subarrays,
each indexed by the year (counting from 1900).  Thus, to get the
element for a particular state and year, we must subscript it first
by the number that indicates the state, and second by the index for
the year:

@example
statepop[state][year - 1900]
@end example

@cindex array, layout in memory
The subarrays within the multidimensional array are allocated
consecutively in memory, and within each subarray, its elements are
allocated consecutively in memory.  The most efficient way to process
all the elements in the array is to scan the last subscript in the
innermost loop.  This means consecutive accesses go to consecutive
memory locations, which optimizes use of the processor's memory cache.
For example:

@example
int total = 0;
float average;

for (int state = 0; state < NSTATES, ++state)
  @{
    for (int year = 0; year < nyears; ++year)
      @{
        total += statepop[state][year];
      @}
  @}

average = total / nyears;
@end example

C's layout for multidimensional arrays is different from Fortran's
layout.  In Fortran, a multidimensional array is not an array of
arrays; rather, multidimensional arrays are a primitive feature, and
it is the first index that varies most rapidly between consecutive
memory locations.  Thus, the memory layout of a 50x114 array in C
matches that of a 114x50 array in Fortran.

@node Constructing Array Values
@section Constructing Array Values
@cindex constructing array values
@cindex array values, constructing

You can construct an array from elements by writing them inside
braces, and preceding all that with the array type's designator in
parentheses.  There is no need to specify the array length, since the
number of elements determines that.  The constructor looks like this:

@example
(@var{elttype}[]) @{ @var{elements} @};
@end example

Here is an example, which constructs an array of string pointers:

@example
(char *[]) @{ "x", "y", "z" @};
@end example

That's equivalent in effect to declaring an array with the same
initializer, like this:

@example
char *array[] = @{ "x", "y", "z" @};
@end example

and then using the array.

If all the elements are simple constant expressions, or made up of
such, then the compound literal can be coerced to a pointer to its
zeroth element and used to initialize a file-scope variable
(@pxref{File-Scope Variables}), as shown here:

@example
char **foo = (char *[]) @{ "x", "y", "z" @};
@end example

@noindent
The data type of @code{foo} is @code{char **}, which is a pointer
type, not an array type.  The declaration is equivalent to defining
and then using an array-type variable:

@example
char *nameless_array[] = @{ "x", "y", "z" @};
char **foo = &nameless_array[0];
@end example

@node Arrays of Variable Length
@section Arrays of Variable Length
@cindex array of variable length
@cindex variable-length arrays

In GNU C, you can declare variable-length arrays like any other
arrays, but with a length that is not a constant expression.  The
storage is allocated at the point of declaration and deallocated when
the block scope containing the declaration exits.  For example:

@example
#include <stdio.h>  /* @r{Defines @code{FILE}.} */
#include <string.h> /* @r{Declares @code{str}.} */

FILE *
concat_fopen (char *s1, char *s2, char *mode)
@{
  char str[strlen (s1) + strlen (s2) + 1];
  strcpy (str, s1);
  strcat (str, s2);
  return fopen (str, mode);
@}
@end example

@noindent
(This uses some standard library functions; see @ref{String and Array
Utilities, , , libc, The GNU C Library Reference Manual}.)

The length of an array is computed once when the storage is allocated
and is remembered for the scope of the array in case it is used in
@code{sizeof}.

@strong{Warning:} Don't allocate a variable-length array if the size
might be very large (more than 100,000), or in a recursive function,
because that is likely to cause stack overflow.  Allocate the array
dynamically instead (@pxref{Dynamic Memory Allocation}).

Jumping or breaking out of the scope of the array name deallocates the
storage.  Jumping into the scope is not allowed; that gives an error
message.

You can also use variable-length arrays as arguments to functions:

@example
struct entry
tester (int len, char data[len][len])
@{
  @r{@dots{}}
@}
@end example

As usual, a function argument declared with an array type
is really a pointer to an array that already exists.
Calling the function does not allocate the array, so there's no
particular danger of stack overflow in using this construct.

To pass the array first and the length afterward, use a forward
declaration in the function's parameter list (another GNU extension).
For example,

@example
struct entry
tester (int len; char data[len][len], int len)
@{
  @r{@dots{}}
@}
@end example

The @code{int len} before the semicolon is a @dfn{parameter forward
declaration}, and it serves the purpose of making the name @code{len}
known when the declaration of @code{data} is parsed.

You can write any number of such parameter forward declarations in the
parameter list.  They can be separated by commas or semicolons, but
the last one must end with a semicolon, which is followed by the
``real'' parameter declarations.  Each forward declaration must match
a ``real'' declaration in parameter name and data type.  ISO C11 does
not support parameter forward declarations.

@node Enumeration Types
@chapter Enumeration Types
@cindex enumeration types
@cindex types, enumeration
@cindex enumerator

An @dfn{enumeration type} represents a limited set of integer values,
each with a name.  It is effectively equivalent to a primitive integer
type.

Suppose we have a list of possible emotional states to store in an
integer variable.  We can give names to these alternative values with
an enumeration:

@example
enum emotion_state @{ neutral, happy, sad, worried,
                     calm, nervous @};
@end example

@noindent
(Never mind that this is a simplistic way to classify emotional states;
it's just a code example.)

The names inside the enumeration are called @dfn{enumerators}.  The
enumeration type defines them as constants, and their values are
consecutive integers; @code{neutral} is 0, @code{happy} is 1,
@code{sad} is 2, and so on.  Alternatively, you can specify values for
the enumerators explicitly like this:

@example
enum emotion_state @{ neutral = 2, happy = 5,
                     sad = 20, worried = 10,
                     calm = -5, nervous = -300 @};
@end example

Each enumerator which does not specify a value gets value zero
(if it is at the beginning) or the next consecutive integer.

@example
/* @r{@code{neutral} is 0 by default,}
   @r{and @code{worried} is 21 by default.}  */
enum emotion_state @{ neutral,
                      happy = 5, sad = 20, worried,
                      calm = -5, nervous = -300 @};
@end example

If an enumerator is obsolete, you can specify that using it should
cause a warning, by including an attribute in the enumerator's
declaration.  Here is how @code{happy} would look with this
attribute:

@example
happy __attribute__
      ((deprecated
        ("impossible under plutocratic rule")))
      = 5,
@end example

@xref{Attributes}.

You can declare variables with the enumeration type:

@example
enum emotion_state feelings_now;
@end example

In the C code itself, this is equivalent to declaring the variable
@code{int}.  (If all the enumeration values are positive, it is
equivalent to @code{unsigned int}.)  However, declaring it with the
enumeration type has an advantage in debugging, because GDB knows it
should display the current value of the variable using the
corresponding name.  If the variable's type is @code{int}, GDB can
only show the value as a number.

The identifier that follows @code{enum} is called a @dfn{type tag}
since it distinguishes different enumeration types.  Type tags are in
a separate name space and belong to scopes like most other names in C@.
@xref{Type Tags}, for explanation.

You can predeclare an @code{enum} type tag like a structure or union
type tag, like this:

@example
enum foo;
@end example

@noindent
The @code{enum} type is incomplete until you finish defining it.

You can optionally include a trailing comma at the end of a list of
enumeration values:

@example
enum emotion_state @{ neutral, happy, sad, worried,
                     calm, nervous, @};
@end example

@noindent
This is useful in some macro definitions, since it enables you to
assemble the list of enumerators without knowing which one is last.
The extra comma does not change the meaning of the enumeration in any
way.

@node Defining Typedef Names
@chapter Defining Typedef Names
@cindex typedef names
@findex typedef

You can define a data type keyword as an alias for any type, and then
use the alias syntactically like a built-in type keyword such as
@code{int}.  You do this using @code{typedef}, so these aliases are
also called @dfn{typedef names}.

@code{typedef} is followed by text that looks just like a variable
declaration, but instead of declaring variables it defines data type
keywords.

Here's how to define @code{fooptr} as a typedef alias for the type
@code{struct foo *}, then declare @code{x} and @code{y} as variables
with that type:

@example
typedef struct foo *fooptr;

fooptr x, y;
@end example

@noindent
That declaration is equivalent to the following one:

@example
struct foo *x, *y;
@end example

You can define a typedef alias for any type.  For instance, this makes
@code{frobcount} an alias for type @code{int}:

@example
typedef int frobcount;
@end example

@noindent
This doesn't define a new type distinct from @code{int}.  Rather,
@code{frobcount} is another name for the type @code{int}.  Once the
variable is declared, it makes no difference which name the
declaration used.

There is a syntactic difference, however, between @code{frobcount} and
@code{int}: A typedef name cannot be used with
@code{signed}, @code{unsigned}, @code{long} or @code{short}.  It has
to specify the type all by itself.  So you can't write this:

@example
unsigned frobcount f1;  /* @r{Error!} */
@end example

But you can write this:

@example
typedef unsigned int unsigned_frobcount;

unsigned_frobcount f1;
@end example

In other words, a typedef name is not an alias for @emph{a keyword}
such as @code{int}.  It stands for a @emph{type}, and that could be
the type @code{int}.

Typedef names are in the same namespace as functions and variables, so
you can't use the same name for a typedef and a function, or a typedef
and a variable.  When a typedef is declared inside a code block, it is
in scope only in that block.

@strong{Warning:} Avoid defining typedef names that end in @samp{_t},
because many of these have standard meanings.

You can redefine a typedef name to the exact same type as its first
definition, but you cannot redefine a typedef name to a
different type, even if the two types are compatible. For example, this
is valid:

@example
typedef int frobcount;
typedef int frotzcount;
typedef frotzcount frobcount;
typedef frobcount frotzcount;
@end example

@noindent
because each typedef name is always defined with the same type
(@code{int}), but this is not valid:

@example
enum foo @{f1, f2, f3@};
typedef enum foo frobcount;
typedef int frobcount;
@end example

@noindent
Even though the type @code{enum foo} is compatible with @code{int},
they are not the @emph{same} type.

@node Statements
@chapter Statements
@cindex statements

A @dfn{statement} specifies computations to be done for effect; it
does not produce a value, as an expression would.  In general a
statement ends with a semicolon (@samp{;}), but blocks (which are
statements, more or less) are an exception to that rule.
@ifnottex
@xref{Blocks}.
@end ifnottex

The places to use statements are inside a block, and inside a
complex statement.  A @dfn{complex statement} contains one or two
components that are nested statements.  Each such component must
consist of one and only one statement.  The way to put multiple
statements in such a component is to group them into a @dfn{block}
(@pxref{Blocks}), which counts as one statement.

The following sections describe the various kinds of statement.

@menu
* Expression Statement::         Evaluate an expression, as a statement,
                                   usually done for a side effect.
* if Statement::                 Basic conditional execution.
* if-else Statement::            Multiple branches for conditional execution.
* Blocks::                       Grouping multiple statements together.
* return Statement::             Return a value from a function.
* Loop Statements::              Repeatedly executing a statement or block.
* switch Statement::             Multi-way conditional choices.
* switch Example::               A plausible example of using @code{switch}.
* Duffs Device::                 A special way to use @code{switch}.
* Case Ranges::                  Ranges of values for @code{switch} cases.
* Null Statement::               A statement that does nothing.
* goto Statement::               Jump to another point in the source code,
                                   identified by a label.
* Local Labels::                 Labels with limited scope.
* Labels as Values::             Getting the address of a label.
* Statement Exprs::              A series of statements used as an expression.
@end menu

@node Expression Statement
@section Expression Statement
@cindex expression statement
@cindex statement, expression

The most common kind of statement in C is an @dfn{expression statement}.
It consists of an expression followed by a
semicolon.  The expression's value is discarded, so the expressions
that are useful are those that have side effects: assignment
expressions, increment and decrement expressions, and function calls.
Here are examples of expression statements:

@smallexample
x = 5;              /* @r{Assignment expression.} */
p++;                /* @r{Increment expression.} */
printf ("Done\n");  /* @r{Function call expression.} */
*p;                 /* @r{Cause @code{SIGSEGV} signal if @code{p} is null.} */
x + y;              /* @r{Useless statement without effect.} */
@end smallexample

In very unusual circumstances we use an expression statement
whose purpose is to get a fault if an address is invalid:

@smallexample
volatile char *p;
@r{@dots{}}
*p;                 /* @r{Cause signal if @code{p} is null.} */
@end smallexample

If the target of @code{p} is not declared @code{volatile}, the
compiler might optimize away the memory access, since it knows that
the value isn't really used.  @xref{volatile}.

@node if Statement
@section @code{if} Statement
@cindex @code{if} statement
@cindex statement, @code{if}
@findex if

An @code{if} statement computes an expression to decide
whether to execute the following statement or not.
It looks like this:

@example
if (@var{condition})
  @var{execute-if-true}
@end example

The first thing this does is compute the value of @var{condition}.  If
that is true (nonzero), then it executes the statement
@var{execute-if-true}.  If the value of @var{condition} is false
(zero), it doesn't execute @var{execute-if-true}; instead, it does
nothing.

This is a @dfn{complex statement} because it contains a component
@var{execute-if-true} that is a nested statement.  It must be one
and only one statement.  The way to put multiple statements there is
to group them into a @dfn{block} (@pxref{Blocks}).

@node if-else Statement
@section @code{if-else} Statement
@cindex @code{if}@dots{}@code{else} statement
@cindex statement, @code{if}@dots{}@code{else}
@findex else

An @code{if}-@code{else} statement computes an expression to decide
which of two nested statements to execute.
It looks like this:

@example
if (@var{condition})
  @var{if-true-substatement}
else
  @var{if-false-substatement}
@end example

The first thing this does is compute the value of @var{condition}.  If
that is true (nonzero), then it executes the statement
@var{if-true-substatement}.  If the value of @var{condition} is false
(zero), then it executes the statement @var{if-false-substatement} instead.

This is a @dfn{complex statement} because it contains components
@var{if-true-substatement} and @var{if-else-substatement} that are
nested statements.  Each must be one and only one statement.  The way
to put multiple statements in such a component is to group them into a
@dfn{block} (@pxref{Blocks}).

@node Blocks
@section Blocks
@cindex block
@cindex compound statement

A @dfn{block} is a construct that contains multiple statements of any
kind.  It begins with @samp{@{} and ends with @samp{@}}, and has a
series of statements and declarations in between.  Another name for
blocks is @dfn{compound statements}.

Is a block a statement?  Yes and no.  It doesn't @emph{look} like a
normal statement---it does not end with a semicolon.  But you can
@emph{use} it like a statement; anywhere that a statement is required
or allowed, you can write a block and consider that block a statement.

So far it seems that a block is a kind of statement with an unusual
syntax.  But that is not entirely true: a function body is also a
block, and that block is definitely not a statement.  The text after a
function header is not treated as a statement; only a function body is
allowed there, and nothing else would be meaningful there.

In a formal grammar we would have to choose---either a block is a kind
of statement or it is not.  But this manual is meant for humans, not
for parser generators.  The clearest answer for humans is, ``a block
is a statement, in some ways.''

@cindex nested block
@cindex internal block
A block that isn't a function body is called an @dfn{internal block}
or a @dfn{nested block}.  You can put a nested block directly inside
another block, but more often the nested block is inside some complex
statement, such as a @code{for} statement or an @code{if} statement.

There are two uses for nested blocks in C:

@itemize @bullet
@item
To specify the scope for local declarations.  For instance, a local
variable's scope is the rest of the innermost containing block.

@item
To write a series of statements where, syntactically, one statement is
called for.  For instance, the @var{execute-if-true} of an @code{if}
statement is one statement.  To put multiple statements there, they
have to be wrapped in a block, like this:

@example
if (x < 0)
  @{
    printf ("x was negative\n");
    x = -x;
  @}
@end example
@end itemize

This example (repeated from above) shows a nested block which serves
both purposes: it includes two statements (plus a declaration) in the
body of a @code{while} statement, and it provides the scope for the
declaration of @code{q}.

@example
void
free_intlist (struct intlistlink *p)
@{
  while (p)
    @{
      struct intlistlink *q = p;
      p = p->next;
      free (q);
    @}
@}
@end example

@node return Statement
@section @code{return} Statement
@cindex @code{return} statement
@cindex statement, @code{return}
@findex return

The @code{return} statement makes the containing function return
immediately.  It has two forms.  This one specifies no value to
return:

@example
return;
@end example

@noindent
That form is meant for functions whose return type is @code{void}
(@pxref{The Void Type}).  You can also use it in a function that
returns nonvoid data, but that's a bad idea, since it makes the
function return garbage.

The form that specifies a value looks like this:

@example
return @var{value};
@end example

@noindent
which computes the expression @var{value} and makes the function
return that.  If necessary, the value undergoes type conversion to
the function's declared return value type, which works like
assigning the value to a variable of that type.

@node Loop Statements
@section Loop Statements
@cindex loop statements
@cindex statements, loop
@cindex iteration

You can use a loop statement when you need to execute a series of
statements repeatedly, making an @dfn{iteration}.  C provides several
different kinds of loop statements, described in the following
subsections.

Every kind of loop statement is a complex statement because contains a
component, here called @var{body}, which is a nested statement.
Most often the body is a block.

@menu
* while Statement::           Loop as long as a test expression is true.
* do-while Statement::        Execute a loop once, with further looping
                                as long as a test expression is true.
* break Statement::           End a loop immediately.
* for Statement::             Iterative looping.
* Example of for::            An example of iterative looping.
* Omitted for-Expressions::   for-loop expression options.
* for-Index Declarations::    for-loop declaration options.
* continue Statement::        Begin the next cycle of a loop.
@end menu

@node while Statement
@subsection @code{while} Statement
@cindex @code{while} statement
@cindex statement, @code{while}
@findex while

The @code{while} statement is the simplest loop construct.
It looks like this:

@example
while (@var{test})
  @var{body}
@end example

Here, @var{body} is a statement (often a nested block) to repeat, and
@var{test} is the test expression that controls whether to repeat it again.
Each iteration of the loop starts by computing @var{test} and, if it
is true (nonzero), that means the loop should execute @var{body} again
and then start over.

Here's an example of advancing to the last structure in a chain of
structures chained through the @code{next} field:

@example
#include <stddef.h> /* @r{Defines @code{NULL}.} */
@r{@dots{}}
while (chain->next != NULL)
  chain = chain->next;
@end example

@noindent
This code assumes the chain isn't empty to start with; if the chain is
empty (that is, if @code{chain} is a null pointer), the code gets a
@code{SIGSEGV} signal trying to dereference that null pointer (@pxref{Signals}).

@node do-while Statement
@subsection @code{do-while} Statement
@cindex @code{do}--@code{while} statement
@cindex statement, @code{do}--@code{while}
@findex do

The @code{do}--@code{while} statement is a simple loop construct that
performs the test at the end of the iteration.

@example
do
  @var{body}
while (@var{test});
@end example

Here, @var{body} is a statement (possibly a block) to repeat, and
@var{test} is an expression that controls whether to repeat it again.

Each iteration of the loop starts by executing @var{body}.  Then it
computes @var{test} and, if it is true (nonzero), that means to go
back and start over with @var{body}.  If @var{test} is false (zero),
then the loop stops repeating and execution moves on past it.

@strong{Warning:} Human beings tend to confuse the @code{do}--@code{while}
statement with the @code{while} statement using the null statement
as its @var{body} (@pxref{Null Statement}).  To avoid that, consistently
mark such constructs with a specific comment or with clearly different
indent styles.

@node break Statement
@subsection @code{break} Statement
@cindex @code{break} statement
@cindex statement, @code{break}
@findex break

The @code{break} statement looks like @samp{break;}.  Its effect is to
exit immediately from the innermost loop construct or @code{switch}
statement (@pxref{switch Statement}).

For example, this loop advances @code{p} until the next null
character or newline.

@example
while (*p)
  @{
    /* @r{End loop if we have reached a newline.}  */
    if (*p == '\n')
      break;
    p++
  @}
@end example

When there are nested loops, the @code{break} statement exits from the
innermost loop containing it.

@example
struct list_if_tuples
@{
  struct list_if_tuples *next;
  int length;
  data *contents;
@};

void
process_all_elements (struct list_if_tuples *list)
@{
  while (list)
    @{
      /* @r{Process all the elements in this node's vector,}
         @r{stopping when we reach one that is null.}  */
      for (i = 0; i < list->length; i++)
        @{
          /* @r{Null element terminates this node's vector.}  */
          if (list->contents[i] == NULL)
            /* @r{Exit the @code{for} loop.}  */
            break;
          /* @r{Operate on the next element.}  */
          process_element (list->contents[i]);
        @}

      list = list->next;
    @}
@}
@end example

The only way in C to exit from an outer loop is with
@code{goto} (@pxref{goto Statement}).

@node for Statement
@subsection @code{for} Statement
@cindex @code{for} statement
@cindex statement, @code{for}
@findex for

A @code{for} statement uses three expressions written inside a
parenthetical group to define the repetition of the loop.  The first
expression says how to prepare to start the loop.  The second says how
to test, before each iteration, whether to continue looping.  The
third says how to advance, at the end of an iteration, for the next
iteration.  All together, it looks like this:

@example
for (@var{start}; @var{continue-test}; @var{advance})
  @var{body}
@end example

The first thing the @code{for} statement does is compute @var{start}.
The next thing it does is compute the expression @var{continue-test}.
If that expression is false (zero), the @code{for} statement finishes
immediately, so @var{body} is executed zero times.

However, if @var{continue-test} is true (nonzero), the @code{for}
statement executes @var{body}, then @var{advance}.  Then it loops back
to the not-quite-top to test @var{continue-test} again.  But it does
not compute @var{start} again.

@node Example of for
@subsection Example of @code{for}

Here is the @code{for} statement from the iterative Fibonacci
function:

@example
int i;
for (i = 1; i < n; ++i)
  /* @r{If @code{n} is 1 or less, the loop runs zero times,}  */
  /* @r{since @code{i < n} is false the first time.}  */
  @{
    /* @r{Now @var{last} is @code{fib (@var{i})}}
       @r{and @var{prev} is @code{fib (@var{i} @minus{} 1)}.}  */
    /* @r{Compute @code{fib (@var{i} + 1)}.}  */
    int next = prev + last;
    /* @r{Shift the values down.}  */
    prev = last;
    last = next;
    /* @r{Now @var{last} is @code{fib (@var{i} + 1)}}
       @r{and @var{prev} is @code{fib (@var{i})}.}
       @r{But that won't stay true for long,}
       @r{because we are about to increment @var{i}.}  */
  @}
@end example

In this example, @var{start} is @code{i = 1}, meaning set @code{i} to
1.  @var{continue-test} is @code{i < n}, meaning keep repeating the
loop as long as @code{i} is less than @code{n}.  @var{advance} is
@code{i++}, meaning increment @code{i} by 1.  The body is a block
that contains a declaration and two statements.

@node Omitted for-Expressions
@subsection Omitted @code{for}-Expressions

A fully-fleshed @code{for} statement contains all these parts,

@example
for (@var{start}; @var{continue-test}; @var{advance})
  @var{body}
@end example

@noindent
but you can omit any of the three expressions inside the parentheses.
The parentheses and the two semicolons are required syntactically, but
the expressions between them may be missing.  A missing expression
means this loop doesn't use that particular feature of the @code{for}
statement.

@c ??? You can't do this if START is a declaration.
Instead of using @var{start}, you can do the loop preparation
before the @code{for} statement: the effect is the same.  So we
could have written the beginning of the previous example this way:

@example
int i = 0;
for (; i < n; ++i)
@end example

@noindent
instead of this way:

@example
int i;
for (i = 0; i < n; ++i)
@end example

Omitting @var{continue-test} means the loop runs forever (or until
something else causes exit from it).  Statements inside the loop can
test conditions for termination and use @samp{break;} to exit.  This
is more flexible since you can put those tests anywhere in the loop,
not solely at the beginning.

Putting an expression in @var{advance} is almost equivalent to writing
it at the end of the loop body; it does almost the same thing.  The
only difference is for the @code{continue} statement (@pxref{continue
Statement}).  So we could have written this:

@example
for (i = 0; i < n;)
  @{
    @r{@dots{}}
    ++i;
  @}
@end example

@noindent
instead of this:

@example
for (i = 0; i < n; ++i)
  @{
    @r{@dots{}}
  @}
@end example

The choice is mainly a matter of what is more readable for
programmers.  However, there is also a syntactic difference:
@var{advance} is an expression, not a statement.  It can't include
loops, blocks, declarations, etc.

@node for-Index Declarations
@subsection @code{for}-Index Declarations

You can declare loop-index variables directly in the @var{start}
portion of the @code{for}-loop, like this:

@example
for (int i = 0; i < n; ++i)
  @{
    @r{@dots{}}
  @}
@end example

This kind of @var{start} is limited to a single declaration; it can
declare one or more variables, separated by commas, all of which are
the same @var{basetype} (@code{int}, in this example):

@example
for (int i = 0, j = 1, *p = NULL; i < n; ++i, ++j, ++p)
  @{
    @r{@dots{}}
  @}
@end example

@noindent
The scope of these variables is the @code{for} statement as a whole.
See @ref{Variable Declarations} for an explanation of @var{basetype}.

Variables declared in @code{for} statements should have initializers.
Omitting the initialization gives the variables unpredictable initial
values, so this code is erroneous.

@example
for (int i; i < n; ++i)
  @{
    @r{@dots{}}
  @}
@end example

@node continue Statement
@subsection @code{continue} Statement
@cindex @code{continue} statement
@cindex statement, @code{continue}
@findex continue

The @code{continue} statement looks like @samp{continue;}, and its
effect is to jump immediately to the end of the innermost loop
construct.  If it is a @code{for}-loop, the next thing that happens
is to execute the loop's @var{advance} expression.

For example, this loop increments @code{p} until the next null character
or newline, and operates (in some way not shown) on all the characters
in the line except for spaces.  All it does with spaces is skip them.

@example
for (;*p; ++p)
  @{
    /* @r{End loop if we have reached a newline.}  */
    if (*p == '\n')
      break;
    /* @r{Pay no attention to spaces.}  */
    if (*p == ' ')
      continue;
    /* @r{Operate on the next character.}  */
    @r{@dots{}}
  @}
@end example

@noindent
Executing @samp{continue;} skips the loop body but it does not
skip the @var{advance} expression, @code{p++}.

We could also write it like this:

@example
for (;*p; ++p)
  @{
    /* @r{Exit if we have reached a newline.}  */
    if (*p == '\n')
      break;
    /* @r{Pay no attention to spaces.}  */
    if (*p != ' ')
      @{
        /* @r{Operate on the next character.}  */
        @r{@dots{}}
      @}
  @}
@end example

The advantage of using @code{continue} is that it reduces the
depth of nesting.

Contrast @code{continue} with the @code{break} statement.  @xref{break
Statement}.

@node switch Statement
@section @code{switch} Statement
@cindex @code{switch} statement
@cindex statement, @code{switch}
@findex switch
@findex case
@findex default

The @code{switch} statement selects code to run according to the value
of an expression.  The expression, in parentheses, follows the keyword
@code{switch}.  After that come all the cases to select among,
inside braces.  It looks like this:

@example
switch (@var{selector})
  @{
    @var{cases}@r{@dots{}}
  @}
@end example

A case can look like this:

@example
case @var{value}:
  @var{statements}
  break;
@end example

@noindent
which means ``come here if @var{selector} happens to have the value
@var{value},'' or like this (a GNU C extension):

@example
case @var{rangestart} ... @var{rangeend}:
  @var{statements}
  break;
@end example

@noindent
which means ``come here if @var{selector} happens to have a value
between @var{rangestart} and @var{rangeend} (inclusive).''  @xref{Case
Ranges}.

The values in @code{case} labels must reduce to integer constants.
They can use arithmetic, and @code{enum} constants, but they cannot
refer to data in memory, because they have to be computed at compile
time.  It is an error if two @code{case} labels specify the same
value, or ranges that overlap, or if one is a range and the other is a
value in that range.

You can also define a default case to handle ``any other value,'' like
this:

@example
default:
  @var{statements}
  break;
@end example

If the @code{switch} statement has no @code{default:} label, then it
does nothing when the value matches none of the cases.

The brace-group inside the @code{switch} statement is a block, and you
can declare variables with that scope just as in any other block
(@pxref{Blocks}).  However, initializers in these declarations won't
necessarily be executed every time the @code{switch} statement runs,
so it is best to avoid giving them initializers.

@code{break;} inside a @code{switch} statement exits immediately from
the @code{switch} statement.  @xref{break Statement}.

If there is no @code{break;} at the end of the code for a case,
execution continues into the code for the following case.  This
happens more often by mistake than intentionally, but since this
feature is used in real code, we cannot eliminate it.

@strong{Warning:} When one case is intended to fall through to the
next, write a comment like @samp{falls through} to say it's
intentional.  That way, other programmers won't assume it was an error
and ``fix'' it erroneously.

Consecutive @code{case} statements could, pedantically, be considered
an instance of falling through, but we don't consider or treat them that
way because they won't confuse anyone.

@node switch Example
@section Example of @code{switch}

Here's an example of using the @code{switch} statement
to distinguish among characters:

@cindex counting vowels and punctuation
@example
struct vp @{ int vowels, punct; @};

struct vp
count_vowels_and_punct (char *string)
@{
  int c;
  int vowels = 0;
  int punct = 0;
  /* @r{Don't change the parameter itself.}  */
  /* @r{That helps in debugging.}  */
  char *p = string;
  struct vp value;

  while (c = *p++)
    switch (c)
      @{
        case 'y':
        case 'Y':
          /* @r{We assume @code{y_is_consonant} will check surrounding
                letters to determine whether this y is a vowel.}  */
          if (y_is_consonant (p - 1))
            break;

          /* @r{Falls through} */

        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
          vowels++;
          break;

        case '.':
        case ',':
        case ':':
        case ';':
        case '?':
        case '!':
        case '\"':
        case '\'':
          punct++;
          break;
      @}

  value.vowels = vowels;
  value.punct = punct;

  return value;
@}
@end example

@node Duffs Device
@section Duff's Device
@cindex Duff's device

The cases in a @code{switch} statement can be inside other control
constructs.  For instance, we can use a technique known as @dfn{Duff's
device} to optimize this simple function,

@example
void
copy (char *to, char *from, int count)
@{
  while (count > 0)
    *to++ = *from++, count--;
@}
@end example

@noindent
which copies memory starting at @var{from} to memory starting at
@var{to}.

Duff's device involves unrolling the loop so that it copies
several characters each time around, and using a @code{switch} statement
to enter the loop body at the proper point:

@example
void
copy (char *to, char *from, int count)
@{
  if (count <= 0)
    return;
  int n = (count + 7) / 8;
  switch (count % 8)
    @{
      do @{
        case 0: *to++ = *from++;
        case 7: *to++ = *from++;
        case 6: *to++ = *from++;
        case 5: *to++ = *from++;
        case 4: *to++ = *from++;
        case 3: *to++ = *from++;
        case 2: *to++ = *from++;
        case 1: *to++ = *from++;
        @} while (--n > 0);
    @}
@}
@end example

@node Case Ranges
@section Case Ranges
@cindex case ranges
@cindex ranges in case statements

You can specify a range of consecutive values in a single @code{case} label,
like this:

@example
case @var{low} ... @var{high}:
@end example

@noindent
This has the same effect as the proper number of individual @code{case}
labels, one for each integer value from @var{low} to @var{high}, inclusive.

This feature is especially useful for ranges of ASCII character codes:

@example
case 'A' ... 'Z':
@end example

@strong{Be careful:} with integers, write spaces around the @code{...}
to prevent it from being parsed wrong.  For example, write this:

@example
case 1 ... 5:
@end example

@noindent
rather than this:

@example
case 1...5:
@end example

@node Null Statement
@section Null Statement
@cindex null statement
@cindex statement, null

A @dfn{null statement} is just a semicolon.  It does nothing.

A null statement is a placeholder for use where a statement is
grammatically required, but there is nothing to be done.  For
instance, sometimes all the work of a @code{for}-loop is done in the
@code{for}-header itself, leaving no work for the body.  Here is an
example that searches for the first newline in @code{array}:

@example
for (p = array; *p != '\n'; p++)
  ;
@end example

@node goto Statement
@section @code{goto} Statement and Labels
@cindex @code{goto} statement
@cindex statement, @code{goto}
@cindex label
@findex goto

The @code{goto} statement looks like this:

@example
goto @var{label};
@end example

@noindent
Its effect is to transfer control immediately to another part of the
current function---where the label named @var{label} is defined.

An ordinary label definition looks like this:

@example
@var{label}:
@end example

@noindent
and it can appear before any statement.  You can't use @code{default}
as a label, since that has a special meaning for @code{switch}
statements.

An ordinary label doesn't need a separate declaration; defining it is
enough.

Here's an example of using @code{goto} to implement a loop
equivalent to @code{do}--@code{while}:

@example
@{
 loop_restart:
  @var{body}
  if (@var{condition})
    goto loop_restart;
@}
@end example

The name space of labels is separate from that of variables and functions.
Thus, there is no error in using a single name in both ways:

@example
@{
  int foo;    // @r{Variable @code{foo}.}
 foo:         // @r{Label @code{foo}.}
  @var{body}
  if (foo > 0)  // @r{Variable @code{foo}.}
    goto foo;   // @r{Label @code{foo}.}
@}
@end example

Blocks have no effect on ordinary labels; each label name is defined
throughout the whole of the function it appears in.  It looks strange to
jump into a block with @code{goto}, but it works.  For example,

@example
if (x < 0)
  goto negative;
if (y < 0)
  @{
   negative:
    printf ("Negative\n");
    return;
  @}
@end example

If the goto jumps into the scope of a variable, it does not
initialize the variable.  For example, if @code{x} is negative,

@example
if (x < 0)
  goto negative;
if (y < 0)
  @{
    int i = 5;
   negative:
    printf ("Negative, and i is %d\n", i);
    return;
  @}
@end example

@noindent
prints junk because @code{i} was not initialized.

If the block declares a variable-length automatic array, jumping into
it gives a compilation error.  However, jumping out of the scope of a
variable-length array works fine, and deallocates its storage.

A label can't come directly before a declaration, so the code can't
jump directly to one.  For example, this is not allowed:

@example
@{
  goto foo;
foo:
  int x = 5;
  bar(&x);
@}
@end example

@noindent
The workaround is to add a statement, even an empty statement,
directly after the label.  For example:

@example
@{
  goto foo;
foo:
  ;
  int x = 5;
  bar(&x);
@}
@end example

Likewise, a label can't be the last thing in a block.  The workaround
solution is the same: add a semicolon after the label.

These unnecessary restrictions on labels make no sense, and ought in
principle to be removed; but they do only a little harm since labels
and @code{goto} are rarely the best way to write a program.

These examples are all artificial; it would be more natural to
write them in other ways, without @code{goto}.  For instance,
the clean way to write the example that prints @samp{Negative} is this:

@example
if (x < 0 || y < 0)
  @{
    printf ("Negative\n");
    return;
  @}
@end example

@noindent
It is hard to construct simple examples where @code{goto} is actually
the best way to write a program.  Its rare good uses tend to be in
complex code, thus not apt for the purpose of explaining the meaning
of @code{goto}.

The only good time to use @code{goto} is when it makes the code
simpler than any alternative.  Jumping backward is rarely desirable,
because usually the other looping and control constructs give simpler
code.  Using @code{goto} to jump forward is more often desirable, for
instance when a function needs to do some processing in an error case
and errors can occur at various different places within the function.

@node Local Labels
@section Locally Declared Labels
@cindex local labels
@cindex macros, local labels
@findex __label__

In GNU C you can declare @dfn{local labels} in any nested block
scope.  A local label is used in a @code{goto} statement just like an
ordinary label, but you can only reference it within the block in
which it was declared.

A local label declaration looks like this:

@example
__label__ @var{label};
@end example

@noindent
or

@example
__label__ @var{label1}, @var{label2}, @r{@dots{}};
@end example

Local label declarations must come at the beginning of the block,
before any ordinary declarations or statements.

The label declaration declares the label @emph{name}, but does not define
the label itself.  That's done in the usual way, with
@code{@var{label}:}, before one of the statements in the block.

The local label feature is useful for complex macros.  If a macro
contains nested loops, a @code{goto} can be useful for breaking out of
them.  However, an ordinary label whose scope is the whole function
cannot be used: if the macro can be expanded several times in one
function, the label will be multiply defined in that function.  A
local label avoids this problem.  For example:

@example
#define SEARCH(value, array, target)              \
do @{                                              \
  __label__ found;                                \
  __auto_type _SEARCH_target = (target);          \
  __auto_type _SEARCH_array = (array);            \
  int i, j;                                       \
  int value;                                      \
  for (i = 0; i < max; i++)                       \
    for (j = 0; j < max; j++)                     \
      if (_SEARCH_array[i][j] == _SEARCH_target)  \
        @{ (value) = i; goto found; @}              \
  (value) = -1;                                   \
 found:;                                          \
@} while (0)
@end example

This could also be written using a statement expression
(@pxref{Statement Exprs}):

@example
#define SEARCH(array, target)                     \
(@{                                                \
  __label__ found;                                \
  __auto_type _SEARCH_target = (target);      \
  __auto_type _SEARCH_array = (array);     \
  int i, j;                                       \
  int value;                                      \
  for (i = 0; i < max; i++)                       \
    for (j = 0; j < max; j++)                     \
      if (_SEARCH_array[i][j] == _SEARCH_target)  \
        @{ value = i; goto found; @}                \
  value = -1;                                     \
 found:                                           \
  value;                                          \
@})
@end example

Ordinary labels are visible throughout the function where they are
defined, and only in that function.  However, explicitly declared
local labels of a block are visible in nested function definitions
inside that block.  @xref{Nested Functions}, for details.

@xref{goto Statement}.

@node Labels as Values
@section Labels as Values
@cindex labels as values
@cindex computed gotos
@cindex goto with computed label
@cindex address of a label

In GNU C, you can get the address of a label defined in the current
function (or a local label defined in the containing function) with
the unary operator @samp{&&}.  The value has type @code{void *}.  This
value is a constant and can be used wherever a constant of that type
is valid.  For example:

@example
void *ptr;
@r{@dots{}}
ptr = &&foo;
@end example

To use these values requires a way to jump to one.  This is done
with the computed goto statement@footnote{The analogous feature in
Fortran is called an assigned goto, but that name seems inappropriate in
C, since you can do more with label addresses than store them in special label
variables.}, @code{goto *@var{exp};}.  For example,

@example
goto *ptr;
@end example

@noindent
Any expression of type @code{void *} is allowed.

@xref{goto Statement}.

@menu
* Label Value Uses::       Examples of using label values.
* Label Value Caveats::    Limitations of label values.
@end menu

@node Label Value Uses
@subsection Label Value Uses

One use for label-valued constants is to initialize a static array to
serve as a jump table:

@example
static void *array[] = @{ &&foo, &&bar, &&hack @};
@end example

Then you can select a label with indexing, like this:

@example
goto *array[i];
@end example

@noindent
Note that this does not check whether the subscript is in bounds---array
indexing in C never checks that.

You can make the table entries offsets instead of addresses
by subtracting one label from the others.  Here is an example:

@example
static const int array[] = @{ &&foo - &&foo, &&bar - &&foo,
                             &&hack - &&foo @};
goto *(&&foo + array[i]);
@end example

@noindent
Using offsets is preferable in shared libraries, as it avoids the need
for dynamic relocation of the array elements; therefore, the array can
be read-only.

An array of label values or offsets serves a purpose much like that of
the @code{switch} statement.  The @code{switch} statement is cleaner,
so use @code{switch} by preference when feasible.

Another use of label values is in an interpreter for threaded code.
The labels within the interpreter function can be stored in the
threaded code for super-fast dispatching.

@node Label Value Caveats
@subsection Label Value Caveats

Jumping to a label defined in another function does not work.
It can cause unpredictable results.

The best way to avoid this is to store label values only in
automatic variables, or static variables whose names are declared
within the function.  Never pass them as arguments.

@cindex cloning
An optimization known as @dfn{cloning} generates multiple simplified
variants of a function's code, for use with specific fixed arguments.
Using label values in certain ways, such as saving the address in one
call to the function and using it again in another call, would make cloning
give incorrect results.  These functions must disable cloning.

Inlining calls to the function would also result in multiple copies of
the code, each with its own value of the same label.  Using the label
in a computed goto is no problem, because the computed goto inhibits
inlining.  However, using the label value in some other way, such as
an indication of where an error occurred, would be optimized wrong.
These functions must disable inlining.

To prevent inlining or cloning of a function, specify
@code{__attribute__((__noinline__,__noclone__))} in its definition.
@xref{Attributes}.

When a function uses a label value in a static variable initializer,
that automatically prevents inlining or cloning the function.

@node Statement Exprs
@section Statements and Declarations in Expressions
@cindex statements inside expressions
@cindex declarations inside expressions
@cindex expressions containing statements

@c the above section title wrapped and causes an underfull hbox.. i
@c changed it from "within" to "in". --mew 4feb93
A block enclosed in parentheses can be used as an expression in GNU
C@.  This provides a way to use local variables, loops and switches within
an expression.  We call it a @dfn{statement expression}.

Recall that a block is a sequence of statements
surrounded by braces.  In this construct, parentheses go around the
braces.  For example:

@example
(@{ int y = foo (); int z;
   if (y > 0) z = y;
   else z = - y;
   z; @})
@end example

@noindent
is a valid (though slightly more complex than necessary) expression
for the absolute value of @code{foo ()}.

The last statement in the block should be an expression statement; an
expression followed by a semicolon, that is.  The value of this
expression serves as the value of statement expression.  If the last
statement is anything else, the statement expression's value is
@code{void}.

This feature is mainly useful in making macro definitions compute each
operand exactly once.  @xref{Macros and Auto Type}.

Statement expressions are not allowed in expressions that must be
constant, such as the value for an enumerator, the width of a
bit-field, or the initial value of a static variable.

Jumping into a statement expression---with @code{goto}, or using a
@code{switch} statement outside the statement expression---is an
error.  With a computed @code{goto} (@pxref{Labels as Values}), the
compiler can't detect the error, but it still won't work.

Jumping out of a statement expression is permitted, but since
subexpressions in C are not computed in a strict order, it is
unpredictable which other subexpressions will have been computed by
then.  For example,

@example
  foo (), ((@{ bar1 (); goto a; 0; @}) + bar2 ()), baz();
@end example

@noindent
calls @code{foo} and @code{bar1} before it jumps, and never
calls @code{baz}, but may or may not call @code{bar2}.  If @code{bar2}
does get called, that occurs after @code{foo} and before @code{bar1}.

@node Variables
@chapter Variables
@cindex variables

Every variable used in a C program needs to be made known by a
@dfn{declaration}.  It can be used only after it has been declared.
It is an error to declare a variable name more than once in the same
scope; an exception is that @code{extern} declarations and tentative
definitions can coexist with another declaration of the same
variable.

Variables can be declared anywhere within a block or file. (Older
versions of C required that all variable declarations within a block
occur before any statements.)

Variables declared within a function or block are @dfn{local} to
it.  This means that the variable name is visible only until the end
of that function or block, and the memory space is allocated only
while control is within it.

Variables declared at the top level in a file are called @dfn{file-scope}.
They are assigned fixed, distinct memory locations, so they retain
their values for the whole execution of the program.

@menu
* Variable Declarations::        Name a variable and reserve space for it.
* Initializers::                 Assigning initial values to variables.
* Designated Inits::             Assigning initial values to array elements
                                   at particular array indices.
* Auto Type::                    Obtaining the type of a variable.
* Local Variables::              Variables declared in function definitions.
* File-Scope Variables::         Variables declared outside of
                                   function definitions.
* Static Local Variables::       Variables declared within functions,
                                   but with permanent storage allocation.
* Extern Declarations::          Declaring a variable
                                   which is allocated somewhere else.
* Allocating File-Scope::        When is space allocated
                                   for file-scope variables?
* auto and register::            Historically used storage directions.
* Omitting Types::               The bad practice of declaring variables
                                   with implicit type.
@end menu

@node Variable Declarations
@section Variable Declarations
@cindex variable declarations
@cindex declaration of variables

Here's what a variable declaration looks like:

@example
@var{keywords} @var{basetype} @var{decorated-variable} @r{[}= @var{init}@r{]};
@end example

The @var{keywords} specify how to handle the scope of the variable
name and the allocation of its storage.  Most declarations have
no keywords because the defaults are right for them.

C allows these keywords to come before or after @var{basetype}, or
even in the middle of it as in @code{unsigned static int}, but don't
do that---it would surprise other programmers.  Always write the
keywords first.

The @var{basetype} can be any of the predefined types of C, or a type
keyword defined with @code{typedef}.  It can also be @code{struct
@var{tag}}, @code{union @var{tag}}, or @code{enum @var{tag}}.  In
addition, it can include type qualifiers such as @code{const} and
@code{volatile} (@pxref{Type Qualifiers}).

In the simplest case, @var{decorated-variable} is just the variable
name.  That declares the variable with the type specified by
@var{basetype}.  For instance,

@example
int foo;
@end example

@noindent
uses @code{int} as the @var{basetype} and @code{foo} as the
@var{decorated-variable}.  It declares @code{foo} with type
@code{int}.

@example
struct tree_node foo;
@end example

@noindent
declares @code{foo} with type @code{struct tree_node}.

@menu
* Declaring Arrays and Pointers::   Declaration syntax for variables of
                                      array and pointer types.
* Combining Variable Declarations:: More than one variable declaration
                                      in a single statement.
@end menu

@node Declaring Arrays and Pointers
@subsection Declaring Arrays and Pointers
@cindex declaring arrays and pointers
@cindex array, declaring
@cindex pointers, declaring

To declare a variable that is an array, write
@code{@var{variable}[@var{length}]} for @var{decorated-variable}:

@example
int foo[5];
@end example

To declare a variable that has a pointer type, write
@code{*@var{variable}} for @var{decorated-variable}:

@example
struct list_elt *foo;
@end example

These constructs nest.  For instance,

@example
int foo[3][5];
@end example

@noindent
declares @code{foo} as an array of 3 arrays of 5 integers each,

@example
struct list_elt *foo[5];
@end example

@noindent
declares @code{foo} as an array of 5 pointers to structures, and

@example
struct list_elt **foo;
@end example

@noindent
declares @code{foo} as a pointer to a pointer to a structure.

@example
int **(*foo[30])(int, double);
@end example

@noindent
declares @code{foo} as an array of 30 pointers to functions
(@pxref{Function Pointers}), each of which must accept two arguments
(one @code{int} and one @code{double}) and return type @code{int **}.

@example
void
bar (int size)
@{
  int foo[size];
  @r{@dots{}}
@}
@end example

@noindent
declares @code{foo} as an array of integers with a size specified at
run time when the function @code{bar} is called.

@node Combining Variable Declarations
@subsection Combining Variable Declarations
@cindex combining variable declarations
@cindex variable declarations, combining
@cindex declarations, combining

When multiple declarations have the same @var{keywords} and
@var{basetype}, you can combine them using commas.  Thus,

@example
@var{keywords} @var{basetype}
   @var{decorated-variable-1} @r{[}= @var{init1}@r{]},
   @var{decorated-variable-2} @r{[}= @var{init2}@r{]};
@end example

@noindent
is equivalent to

@example
@var{keywords} @var{basetype}
   @var{decorated-variable-1} @r{[}= @var{init1}@r{]};
@var{keywords} @var{basetype}
   @var{decorated-variable-2} @r{[}= @var{init2}@r{]};
@end example

Here are some simple examples:

@example
int a, b;
int a = 1, b = 2;
int a, *p, array[5];
int a = 0, *p = &a, array[5] = @{1, 2@};
@end example

@noindent
In the last two examples, @code{a} is an @code{int}, @code{p} is a
pointer to @code{int}, and @code{array} is an array of 5 @code{int}s.
Since the initializer for @code{array} specifies only two elements,
the other three elements are initialized to zero.

@node Initializers
@section Initializers
@cindex initializers

A variable's declaration, unless it is @code{extern}, should also
specify its initial value.  For numeric and pointer-type variables,
the initializer is an expression for the value.  If necessary, it is
converted to the variable's type, just as in an assignment.

You can also initialize a local structure-type (@pxref{Structures}) or
local union-type (@pxref{Unions}) variable this way, from an
expression whose value has the same type.  But you can't initialize an
array this way (@pxref{Arrays}), since arrays are not first-class
objects in C (@pxref{Limitations of C Arrays}) and there is no array
assignment.

You can initialize arrays and structures componentwise,
with a list of the elements or components.  You can initialize
a union with any one of its alternatives.

@itemize @bullet
@item
A component-wise initializer for an array consists of element values
surrounded by @samp{@{@r{@dots{}}@}}.  If the values in the initializer
don't cover all the elements in the array, the remaining elements are
initialized to zero.

You can omit the size of the array when you declare it, and let
the initializer specify the size:

@example
int array[] = @{ 3, 9, 12 @};
@end example

@item
A component-wise initializer for a structure consists of field values
surrounded by @samp{@{@r{@dots{}}@}}.  Write the field values in the same
order as the fields are declared in the structure.  If the values in
the initializer don't cover all the fields in the structure, the
remaining fields are initialized to zero.

@item
The initializer for a union-type variable has the form @code{@{
@var{value} @}}, where @var{value} initializes the @emph{first alternative}
in the union definition.
@end itemize

For an array of arrays, a structure containing arrays, an array of
structures, etc., you can nest these constructs.  For example,

@example
struct point @{ double x, y; @};

struct point series[]
  = @{ @{0, 0@}, @{1.5, 2.8@}, @{99, 100.0004@} @};
@end example

You can omit a pair of inner braces if they contain the right
number of elements for the sub-value they initialize, so that
no elements or fields need to be filled in with zeros.
But don't do that very much, as it gets confusing.

An array of @code{char} can be initialized using a string constant.
Recall that the string constant includes an implicit null character at
the end (@pxref{String Constants}).  Using a string constant as
initializer means to use its contents as the initial values of the
array elements.  Here are examples:

@example
char text[6] = "text!";     /* @r{Includes the null.} */
char text[5] = "text!";     /* @r{Excludes the null.} */
char text[] = "text!";      /* @r{Gets length 6.} */
char text[]
  = @{ 't', 'e', 'x', 't', '!', 0 @};  /* @r{same as above.} */
char text[] = @{ "text!" @};  /* @r{Braces are optional.} */
@end example

@noindent
and this kind of initializer can be nested inside braces to initialize
structures or arrays that contain a @code{char}-array.

In like manner, you can use a wide string constant to initialize
an array of @code{wchar_t}.

@node Designated Inits
@section Designated Initializers
@cindex initializers with labeled elements
@cindex labeled elements in initializers
@cindex case labels in initializers
@cindex designated initializers

In a complex structure or long array, it's useful to indicate
which field or element we are initializing.

To designate specific array elements during initialization, include
the array index in brackets, and an assignment operator, for each
element:

@example
int foo[10] = @{ [3] = 42, [7] = 58 @};
@end example

@noindent
This does the same thing as:

@example
int foo[10] = @{ 0, 0, 0, 42, 0, 0, 0, 58, 0, 0 @};
@end example

The array initialization can include non-designated element values
alongside designated indices; these follow the expected ordering
of the array initialization, so that

@example
int foo[10] = @{ [3] = 42, 43, 44, [7] = 58 @};
@end example

@noindent
does the same thing as:

@example
int foo[10] = @{ 0, 0, 0, 42, 43, 44, 0, 58, 0, 0 @};
@end example

Note that you can only use constant expressions as array index values,
not variables.

If you need to initialize a subsequence of sequential array elements to
the same value, you can specify a range:

@example
int foo[100] = @{ [0 ... 19] = 42, [20 ... 99] = 43 @};
@end example

@noindent
Using a range this way is a GNU C extension.

When subsequence ranges overlap, each element is initialized by the
last specification that applies to it.  Thus, this initialization is
equivalent to the previous one.

@example
int foo[100] = @{ [0 ... 99] = 43, [0 ... 19] = 42 @};
@end example

@noindent
as the second overrides the first for elements 0 through 19.

The value used to initialize a range of elements is evaluated only
once, for the first element in the range.  So for example, this code

@example
int random_values[100]
  = @{ [0 ... 99] = get_random_number() @};
@end example

@noindent
would initialize all 100 elements of the array @code{random_values} to
the same value---probably not what is intended.

Similarly, you can initialize specific fields of a structure variable
by specifying the field name prefixed with a dot:

@example
struct point @{ int x; int y; @};

struct point foo = @{ .y = 42 @};
@end example

@noindent
The same syntax works for union variables as well:

@example
union int_double @{ int i; double d; @};

union int_double foo = @{ .d = 34 @};
@end example

@noindent
This casts the integer value 34 to a double and stores it
in the union variable @code{foo}.

You can designate both array elements and structure elements in
the same initialization; for example, here's an array of point
structures:

@example
struct point point_array[10] = @{ [4].y = 32, [6].y = 39 @};
@end example

Along with the capability to specify particular array and structure
elements to initialize comes the possibility of initializing the same
element more than once:

@example
int foo[10] = @{ [4] = 42, [4] = 98 @};
@end example

@noindent
In such a case, the last initialization value is retained.

@node Auto Type
@section Referring to a Type with @code{__auto_type}
@findex __auto_type
@findex typeof
@cindex macros, types of arguments

You can declare a variable copying the type from
the initializer by using @code{__auto_type} instead of a particular type.
Here's an example:

@example
#define max(a,b) \
  (@{ __auto_type _a = (a); \
      __auto_type _b = (b); \
    _a > _b ? _a : _b @})
@end example

This defines @code{_a} to be of the same type as @code{a}, and
@code{_b} to be of the same type as @code{b}.  This is a useful thing
to do in a macro that ought to be able to handle any type of data
(@pxref{Macros and Auto Type}).

The original GNU C method for obtaining the type of a value is to use
@code{typeof}, which takes as an argument either a value or the name of
a type.  The previous example could also be written as:

@example
#define max(a,b) \
  (@{ typeof(a) _a = (a); \
      typeof(b) _b = (b); \
    _a > _b ? _a : _b @})
@end example

@code{typeof} is more flexible than @code{__auto_type}; however, the
principal use case for @code{typeof} is in variable declarations with
initialization, which is exactly what @code{__auto_type} handles.

@node Local Variables
@section Local Variables
@cindex local variables
@cindex variables, local

Declaring a variable inside a function definition (@pxref{Function
Definitions}) makes the variable name @dfn{local} to the containing
block---that is, the containing pair of braces.  More precisely, the
variable's name is visible starting just after where it appears in the
declaration, and its visibility continues until the end of the block.

Local variables in C are generally @dfn{automatic} variables: each
variable's storage exists only from the declaration to the end of the
block.  Execution of the declaration allocates the storage, computes
the initial value, and stores it in the variable.  The end of the
block deallocates the storage.@footnote{Due to compiler optimizations,
allocation and deallocation don't necessarily really happen at
those times.}

@strong{Warning:} Two declarations for the same local variable
in the same scope are an error.

@strong{Warning:} Automatic variables are stored in the run-time stack.
The total space for the program's stack may be limited; therefore,
in using very large arrays, it may be necessary to allocate
them in some other way to stop the program from crashing.

@strong{Warning:} If the declaration of an automatic variable does not
specify an initial value, the variable starts out containing garbage.
In this example, the value printed could be anything at all:

@example
@{
  int i;

  printf ("Print junk %d\n", i);
@}
@end example

In a simple test program, that statement is likely to print 0, simply
because every process starts with memory zeroed.  But don't rely on it
to be zero---that is erroneous.

@strong{Note:} Make sure to store a value into each local variable (by
assignment, or by initialization) before referring to its value.

@node File-Scope Variables
@section File-Scope Variables
@cindex file-scope variables
@cindex global variables
@cindex variables, file-scope
@cindex variables, global

A variable declaration at the top level in a file (not inside a
function definition) declares a @dfn{file-scope variable}.  Loading a
program allocates the storage for all the file-scope variables in it,
and initializes them too.

Each file-scope variable is either @dfn{static} (limited to one
compilation module) or @dfn{global} (shared with all compilation
modules in the program).  To make the variable static, write the
keyword @code{static} at the start of the declaration.  Omitting
@code{static} makes the variable global.

The initial value for a file-scope variable can't depend on the
contents of storage, and can't call any functions.

@example
int foo = 5;         /* @r{Valid.} */
int bar = foo;       /* @r{Invalid!} */
int bar = sin (1.0); /* @r{Invalid!} */
@end example

But it can use the address of another file-scope variable:

@example
int foo;
int *bar = &foo;     /* @r{Valid.} */
int arr[5];
int *bar3 = &arr[3]; /* @r{Valid.} */
int *bar4 = arr + 4; /* @r{Valid.} */
@end example

It is valid for a module to have multiple declarations for a
file-scope variable, as long as they are all global or all static, but
at most one declaration can specify an initial value for it.

@node Static Local Variables
@section Static Local Variables
@cindex static local variables
@cindex variables, static local
@findex static

The keyword @code{static} in a local variable declaration says to
allocate the storage for the variable permanently, just like a
file-scope variable, even if the declaration is within a function.

Here's an example:

@example
int
increment_counter ()
@{
  static int counter = 0;
  return ++counter;
@}
@end example

The scope of the name @code{counter} runs from the declaration to the
end of the containing block, just like an automatic local variable,
but its storage is permanent, so the value persists from one call to
the next.  As a result, each call to @code{increment_counter}
returns a different, unique value.

The initial value of a static local variable has the same limitations
as for file-scope variables: it can't depend on the contents of
storage or call any functions.  It can use the address of a file-scope
variable or a static local variable, because those addresses are
determined before the program runs.

@node Extern Declarations
@section @code{extern} Declarations
@cindex @code{extern} declarations
@cindex declarations, @code{extern}
@findex extern

An @code{extern} declaration is used to refer to a global variable
whose principal declaration comes elsewhere---in the same module, or in
another compilation module.  It looks like this:

@example
extern @var{basetype} @var{decorated-variable};
@end example

Its meaning is that, in the current scope, the variable name refers to
the file-scope variable of that name---which needs to be declared in a
non-@code{extern}, non-@code{static} way somewhere else.

For instance, if one compilation module has this global variable
declaration

@example
int error_count = 0;
@end example

@noindent
then other compilation modules can specify this

@example
extern int error_count;
@end example

@noindent
to allow reference to the same variable.

The usual place to write an @code{extern} declaration is at top level
in a source file, but you can write an @code{extern} declaration
inside a block to make a global or static file-scope variable
accessible in that block.

Since an @code{extern} declaration does not allocate space for the
variable, it can omit the size of an array:

@example
extern int array[];
@end example

You can use @code{array} normally in all contexts where it is
converted automatically to a pointer.  However, to use it as the
operand of @code{sizeof} is an error, since the size is unknown.

It is valid to have multiple @code{extern} declarations for the same
variable, even in the same scope, if they give the same type.  They do
not conflict---they agree.  For an array, it is legitimate for some
@code{extern} declarations can specify the size while others omit it.
However, if two declarations give different sizes, that is an error.

Likewise, you can use @code{extern} declarations at file scope
(@pxref{File-Scope Variables}) followed by an ordinary global
(non-static) declaration of the same variable.  They do not conflict,
because they say compatible things about the same meaning of the variable.

@node Allocating File-Scope
@section Allocating File-Scope Variables
@cindex allocation file-scope variables
@cindex file-scope variables, allocating

Some file-scope declarations allocate space for the variable, and some
don't.

A file-scope declaration with an initial value @emph{must} allocate
space for the variable; if there are two of such declarations for the
same variable, even in different compilation modules, they conflict.

An @code{extern} declaration @emph{never} allocates space for the variable.
If all the top-level declarations of a certain variable are
@code{extern}, the variable never gets memory space.  If that variable
is used anywhere in the program, the use will be reported as an error,
saying that the variable is not defined.

@cindex tentative definition
A file-scope declaration without an initial value is called a
@dfn{tentative definition}.  This is a strange hybrid: it @emph{can}
allocate space for the variable, but does not insist.  So it causes no
conflict, no error, if the variable has another declaration that
allocates space for it, perhaps in another compilation module.  But if
nothing else allocates space for the variable, the tentative
definition will do it.  Any number of compilation modules can declare
the same variable in this way, and that is sufficient for all of them
to use the variable.

@c @opindex -fno-common
@c @opindex -fcommon
In programs that are very large or have many contributors, it may be
wise to adopt the convention of never using tentative definitions.
You can use the compilation option @option{-fno-common} to make them
an error, or @option{-fcommon} to enable them.  The default depends
on the version of GCC and its target.

If a file-scope variable gets its space through a tentative
definition, it starts out containing all zeros.

@node auto and register
@section @code{auto} and @code{register}
@cindex @code{auto} declarations
@cindex @code{register} declarations
@findex auto
@findex register

For historical reasons, you can write @code{auto} or @code{register}
before a local variable declaration.  @code{auto} merely emphasizes
that the variable isn't static; it changes nothing.

@code{register} suggests to the compiler storing this variable in a
register.  However, GNU C ignores this suggestion, since it can
choose the best variables to store in registers without any hints.

It is an error to take the address of a variable declared
@code{register}, so you cannot use the unary @samp{&} operator on it.
If the variable is an array, you can't use it at all (other than as
the operand of @code{sizeof}), which makes it rather useless.

@node Omitting Types
@section Omitting Types in Declarations
@cindex omitting types in declarations

The syntax of C traditionally allows omitting the data type in a
declaration if it specifies a storage class, a type qualifier (see the
next chapter), or @code{auto} or @code{register}.  Then the type
defaults to @code{int}.  For example:

@example
auto foo = 42;
@end example

This is bad practice; if you see it, fix it.

@node Type Qualifiers
@chapter Type Qualifiers

A declaration can include type qualifiers to advise the compiler
about how the variable will be used.  There are three different
qualifiers, @code{const}, @code{volatile} and @code{restrict}.  They
pertain to different issues, so you can use more than one together.
For instance, @code{const volatile} describes a value that the
program is not allowed to change, but might have a different value
each time the program examines it.  (This might perhaps be a special
hardware register, or part of shared memory.)

If you are just learning C, you can skip this chapter.

@menu
* const::                        Variables whose values don't change.
* volatile::                     Variables whose values may be accessed
                                   or changed outside of the control of
                                   this program.
* restrict Pointers::            Restricted pointers for code optimization.
* restrict Pointer Example::     Example of how that works.
@end menu

@node const
@section @code{const} Variables and Fields
@cindex @code{const} variables and fields
@cindex variables, @code{const}
@findex const

You can mark a variable as ``constant'' by writing @code{const} in
front of the declaration.  This says to treat any assignment to that
variable as an error.  It may also permit some compiler
optimizations---for instance, to fetch the value only once to satisfy
multiple references to it.  The construct looks like this:

@example
const double pi = 3.14159;
@end example

After this definition, the code can use the variable @code{pi}
but cannot assign a different value to it.

@example
pi = 3.0; /* @r{Error!} */
@end example

Simple variables that are constant can be used for the same purposes
as enumeration constants, and they are not limited to integers.  The
constantness of the variable propagates into pointers, too.

A pointer type can specify that the @emph{target} is constant.  For
example, the pointer type @code{const double *} stands for a pointer
to a constant @code{double}.  That's the type that results from taking
the address of @code{pi}.  Such a pointer can't be dereferenced in the
left side of an assignment.

@example
*(&pi) = 3.0; /* @r{Error!} */
@end example

Nonconstant pointers can be converted automatically to constant
pointers, but not vice versa.  For instance,

@example
const double *cptr;
double *ptr;

cptr = &pi;    /* @r{Valid.} */
cptr = ptr;    /* @r{Valid.} */
ptr = cptr;    /* @r{Error!} */
ptr = &pi;     /* @r{Error!} */
@end example

This is not an ironclad protection against modifying the value.  You
can always cast the constant pointer to a nonconstant pointer type:

@example
ptr = (double *)cptr;    /* @r{Valid.} */
ptr = (double *)&pi;     /* @r{Valid.} */
@end example

However, @code{const} provides a way to show that a certain function
won't modify the data structure whose address is passed to it.  Here's
an example:

@example
int
string_length (const char *string)
@{
  int count = 0;
  while (*string++)
    count++;
  return count;
@}
@end example

@noindent
Using @code{const char *} for the parameter is a way of saying this
function never modifies the memory of the string itself.

In calling @code{string_length}, you can specify an ordinary
@code{char *} since that can be converted automatically to @code{const
char *}.

@node volatile
@section @code{volatile} Variables and Fields
@cindex @code{volatile} variables and fields
@cindex variables, @code{volatile}
@findex volatile

The GNU C compiler often performs optimizations that eliminate the
need to write or read a variable.  For instance,

@example
int foo;
foo = 1;
foo++;
@end example

@noindent
might simply store the value 2 into @code{foo}, without ever storing 1.
These optimizations can also apply to structure fields in some cases.

If the memory containing @code{foo} is shared with another program,
or if it is examined asynchronously by hardware, such optimizations
could confuse the communication.  Using @code{volatile} is one way
to prevent them.

Writing @code{volatile} with the type in a variable or field declaration
says that the value may be examined or changed for reasons outside the
control of the program at any moment.  Therefore, the program must
execute in a careful way to assure correct interaction with those
accesses, whenever they may occur.

The simplest use looks like this:

@example
volatile int lock;
@end example

This directs the compiler not to do certain common optimizations on
use of the variable @code{lock}.  All the reads and writes for a volatile
variable or field are really done, and done in the order specified
by the source code.  Thus, this code:

@example
lock = 1;
list = list->next;
if (lock)
  lock_broken (&lock);
lock = 0;
@end example

@noindent
really stores the value 1 in @code{lock}, even though there is no
sign it is really used, and the @code{if} statement reads and
checks the value of @code{lock}, rather than assuming it is still 1.

A limited amount of optimization can be done, in principle, on
@code{volatile} variables and fields: multiple references between two
sequence points (@pxref{Sequence Points}) can be simplified together.

Use of @code{volatile} does not eliminate the flexibility in ordering
the computation of the operands of most operators.  For instance, in
@code{lock + foo ()}, the order of accessing @code{lock} and calling
@code{foo} is not specified, so they may be done in either order; the
fact that @code{lock} is @code{volatile} has no effect on that.

@node restrict Pointers
@section @code{restrict}-Qualified Pointers
@cindex @code{restrict} pointers
@cindex pointers, @code{restrict}-qualified
@findex restrict

You can declare a pointer as ``restricted'' using the @code{restrict}
type qualifier, like this:

@example
int *restrict p = x;
@end example

@noindent
This enables better optimization of code that uses the pointer.

If @code{p} is declared with @code{restrict}, and then the code
references the object that @code{p} points to (using @code{*p} or
@code{p[@var{i}]}), the @code{restrict} declaration promises that the
code will not access that object in any other way---only through
@code{p}.

For instance, it means the code must not use another pointer
to access the same space, as shown here:

@example
int *restrict p = @var{whatever};
int *q = p;
foo (*p, *q);
@end example

@noindent
That contradicts the @code{restrict} promise by accessing the object
that @code{p} points to using @code{q}, which bypasses @code{p}.
Likewise, it must not do this:

@example
int *restrict p = @var{whatever};
struct @{ int *a, *b; @} s;
s.a = p;
foo (*p, *s.a);
@end example

@noindent
This example uses a structure field instead of the variable @code{q}
to hold the other pointer, and that contradicts the promise just the
same.

The keyword @code{restrict} also promises that @code{p} won't point to
the allocated space of any automatic or static variable.  So the code
must not do this:

@example
int a;
int *restrict p = &a;
foo (*p, a);
@end example

@noindent
because that does direct access to the object (@code{a}) that @code{p}
points to, which bypasses @code{p}.

If the code makes such promises with @code{restrict} then breaks them,
execution is unpredictable.

@node restrict Pointer Example
@section @code{restrict} Pointer Example

Here are examples where @code{restrict} enables real optimization.

In this example, @code{restrict} assures GCC that the array @code{out}
points to does not overlap with the array @code{in} points to.

@example
void
process_data (const char *in,
              char * restrict out,
              size_t size)
@{
  for (i = 0; i < size; i++)
    out[i] = in[i] + in[i + 1];
@}
@end example

Here's a simple tree structure, where each tree node holds data of
type @code{PAYLOAD} plus two subtrees.

@example
struct foo
  @{
    PAYLOAD payload;
    struct foo *left;
    struct foo *right;
  @};
@end example

Now here's a function to null out both pointers in the @code{left}
subtree.

@example
void
null_left (struct foo *a)
@{
  a->left->left = NULL;
  a->left->right = NULL;
@}
@end example

Since @code{*a} and @code{*a->left} have the same data type,
they could legitimately alias (@pxref{Aliasing}).  Therefore,
the compiled code for @code{null_left} must read @code{a->left}
again from memory when executing the second assignment statement.

We can enable optimization, so that it does not need to read
@code{a->left} again, by writing @code{null_left} in a less
obvious way.

@example
void
null_left (struct foo *a)
@{
  struct foo *b = a->left;
  b->left = NULL;
  b->right = NULL;
@}
@end example

A more elegant way to fix this is with @code{restrict}.

@example
void
null_left (struct foo *restrict a)
@{
  a->left->left = NULL;
  a->left->right = NULL;
@}
@end example

Declaring @code{a} as @code{restrict} asserts that other pointers such
as @code{a->left} will not point to the same memory space as @code{a}.
Therefore, the memory location @code{a->left->left} cannot be the same
memory as @code{a->left}.  Knowing this, the compiled code may avoid
reloading @code{a->left} for the second statement.

@node Functions
@chapter Functions
@cindex functions

We have already presented many examples of functions, so if you've
read this far, you basically understand the concept of a function.  It
is vital, nonetheless, to have a chapter in the manual that collects
all the information about functions.

@menu
* Function Definitions::         Writing the body of a function.
* Function Declarations::        Declaring the interface of a function.
* Function Calls::               Using functions.
* Function Call Semantics::      Call-by-value argument passing.
* Function Pointers::            Using references to functions.
* The main Function::            Where execution of a GNU C program begins.
* Advanced Definitions::         Advanced features of function definitions.
* Obsolete Definitions::         Obsolete features still used
                                   in function definitions in old code.
@end menu

@node Function Definitions
@section Function Definitions
@cindex function definitions
@cindex defining functions

We have already presented many examples of function definitions.  To
summarize the rules, a function definition looks like this:

@example
@var{returntype}
@var{functionname} (@var{parm_declarations}@r{@dots{}})
@{
  @var{body}
@}
@end example

The part before the open-brace is called the @dfn{function header}.

Write @code{void} as the @var{returntype} if the function does
not return a value.

@menu
* Function Parameter Variables::     Syntax and semantics
                                       of function parameters.
* Forward Function Declarations::    Functions can only be called after
                                       they have been defined or declared.
* Static Functions::                 Limiting visibility of a function.
* Arrays as Parameters::             Functions that accept array arguments.
* Structs as Parameters::            Functions that accept structure arguments.
@end menu

@node Function Parameter Variables
@subsection Function Parameter Variables
@cindex function parameter variables
@cindex parameter variables in functions
@cindex parameter list

A function parameter variable is a local variable (@pxref{Local
Variables}) used within the function to store the value passed as an
argument in a call to the function.  Usually we say ``function
parameter'' or ``parameter'' for short, not mentioning the fact that
it's a variable.

We declare these variables in the beginning of the function
definition, in the @dfn{parameter list}.  For example,

@example
fib (int n)
@end example

@noindent
has a parameter list with one function parameter @code{n}, which has
type @code{int}.

Function parameter declarations differ from ordinary variable
declarations in several ways:

@itemize @bullet
@item
Inside the function definition header, commas separate parameter
declarations, and each parameter needs a complete declaration
including the type.  For instance, if a function @code{foo} has two
@code{int} parameters, write this:

@example
foo (int a, int b)
@end example

You can't share the common @code{int} between the two declarations:

@example
foo (int a, b)      /* @r{Invalid!} */
@end example

@item
A function parameter variable is initialized to whatever value is
passed in the function call, so its declaration cannot specify an
initial value.

@item
Writing an array type in a function parameter declaration has the
effect of declaring it as a pointer.  The size specified for the array
has no effect at all, and we normally omit the size.  Thus,

@example
foo (int a[5])
foo (int a[])
foo (int *a)
@end example

@noindent
are equivalent.

@item
The scope of the parameter variables is the entire function body,
notwithstanding the fact that they are written in the function header,
which is just outside the function body.
@end itemize

If a function has no parameters, it would be most natural for the
list of parameters in its definition to be empty.  But that, in C, has
a special meaning for historical reasons: ``Do not check that calls to
this function have the right number of arguments.''  Thus,

@example
int
foo ()
@{
  return 5;
@}

int
bar (int x)
@{
  return foo (x);
@}
@end example

@noindent
would not report a compilation error in passing @code{x} as an
argument to @code{foo}.  By contrast,

@example
int
foo (void)
@{
  return 5;
@}

int
bar (int x)
@{
  return foo (x);
@}
@end example

@noindent
would report an error because @code{foo} is supposed to receive
no arguments.

@node Forward Function Declarations
@subsection Forward Function Declarations
@cindex forward function declarations
@cindex function declarations, forward

The order of the function definitions in the source code makes no
difference, except that each function needs to be defined or declared
before code uses it.

The definition of a function also declares its name for the rest of
the containing scope.  But what if you want to call the function
before its definition?  To permit that, write a compatible declaration
of the same function, before the first call.  A declaration that
prefigures a subsequent definition in this way is called a
@dfn{forward declaration}.  The function declaration can be at top
@c ??? file scope
level or within a block, and it applies until the end of the containing
scope.

@xref{Function Declarations}, for more information about these
declarations.

@node Static Functions
@subsection Static Functions
@cindex static functions
@cindex functions, static
@findex static

The keyword @code{static} in a function definition limits the
visibility of the name to the current compilation module.  (That's the
same thing @code{static} does in variable declarations;
@pxref{File-Scope Variables}.)  For instance, if one compilation module
contains this code:

@example
static int
foo (void)
@{
  @r{@dots{}}
@}
@end example

@noindent
then the code of that compilation module can call @code{foo} anywhere
after the definition, but other compilation modules cannot refer to it
at all.

@cindex forward declaration
@cindex static function, declaration
To call @code{foo} before its definition, it needs a forward
declaration, which should use @code{static} since the function
definition does.  For this function, it looks like this:

@example
static int foo (void);
@end example

It is generally wise to use @code{static} on the definitions of
functions that won't be called from outside the same compilation
module.  This makes sure that calls are not added in other modules.
If programmers decide to change the function's calling convention, or
understand all the consequences of its use, they will only have to
check for calls in the same compilation module.

@node Arrays as Parameters
@subsection Arrays as Parameters
@cindex arrays as parameters
@cindex functions with array parameters

Arrays in C are not first-class objects: it is impossible to copy
them.  So they cannot be passed as arguments like other values.
@xref{Limitations of C Arrays}.  Rather, array parameters work in
a special way.

@menu
* Array Params are Ptrs::
* Passing Array Args::
* Array Parm Qualifiers::
@end menu

@node Array Params are Ptrs
@subsubsection Array parameters are pointers

Declaring a function parameter variable as an array really gives it a
pointer type.  C does this because an expression with array type, if
used as an argument in a function call, is converted automatically to
a pointer (to the zeroth element of the array).  If you declare the
corresponding parameter as an ``array'', it will work correctly with
the pointer value that really gets passed.

This relates to the fact that C does not check array bounds in access
to elements of the array (@pxref{Accessing Array Elements}).

For example, in this function,

@example
void
clobber4 (int array[20])
@{
  array[4] = 0;
@}
@end example

@noindent
the parameter @code{array}'s real type is @code{int *}; the specified
length, 20, has no effect on the program.  You can leave out the length
and write this:

@example
void
clobber4 (int array[])
@{
  array[4] = 0;
@}
@end example

@noindent
or write the parameter declaration explicitly as a pointer:

@example
void
clobber4 (int *array)
@{
  array[4] = 0;
@}
@end example

They are all equivalent.

@node Passing Array Args
@subsubsection Passing array arguments

  The function call passes this pointer by
value, like all argument values in C@.  However, the result is
paradoxical in that the array itself is passed by reference: its
contents are treated as shared memory---shared between the caller and
the called function, that is.  When @code{clobber4} assigns to element
4 of @code{array}, the effect is to alter element 4 of the array
specified in the call.

@example
#include <stddef.h>  /* @r{Defines @code{NULL}.} */
#include <stdlib.h>  /* @r{Declares @code{malloc},} */
                     /* @r{Defines @code{EXIT_SUCCESS}.} */

int
main (void)
@{
  int data[] = @{1, 2, 3, 4, 5, 6@};
  int i;

  /* @r{Show the initial value of element 4.} */
  for (i = 0; i < 6; i++)
    printf ("data[%d] = %d\n", i, data[i]);

  printf ("\n");

  clobber4 (data);

  /* @r{Show that element 4 has been changed.} */
  for (i = 0; i < 6; i++)
    printf ("data[%d] = %d\n", i, data[i]);

  printf ("\n");

  return EXIT_SUCCESS;
@}
@end example

@noindent
shows that @code{data[4]} has become zero after the call to
@code{clobber4}.

The array @code{data} has 6 elements, but passing it to a function
whose argument type is written as @code{int [20]} is not an error,
because that really stands for @code{int *}.  The pointer that is the
real argument carries no indication of the length of the array it
points into.  It is not required to point to the beginning of the
array, either.  For instance,

@example
clobber4 (data+1);
@end example

@noindent
passes an ``array'' that starts at element 1 of @code{data}, and the
effect is to zero @code{data[5]} instead of @code{data[4]}.

If all calls to the function will provide an array of a particular
size, you can specify the size of the array to be @code{static}:

@example
void
clobber4 (int array[static 20])
@r{@dots{}}
@end example

@noindent
This is a promise to the compiler that the function will always be
called with an array of 20 elements, so that the compiler can optimize
code accordingly.  If the code breaks this promise and calls the
function with, for example, a shorter array, unpredictable things may
happen.

@node Array Parm Qualifiers
@subsubsection Type qualifiers on array parameters

You can use the type qualifiers @code{const}, @code{restrict}, and
@code{volatile} with array parameters; for example:

@example
void
clobber4 (volatile int array[20])
@r{@dots{}}
@end example

@noindent
denotes that @code{array} is equivalent to a pointer to a volatile
@code{int}.  Alternatively:

@example
void
clobber4 (int array[const 20])
@r{@dots{}}
@end example

@noindent
makes the array parameter equivalent to a constant pointer to an
@code{int}.  If we want the @code{clobber4} function to succeed, it
would not make sense to write

@example
void
clobber4 (const int array[20])
@r{@dots{}}
@end example

@noindent
as this would tell the compiler that the parameter should point to an
array of constant @code{int} values, and then we would not be able to
store zeros in them.

In a function with multiple array parameters, you can use @code{restrict}
to tell the compiler that each array parameter passed in will be distinct:

@example
void
foo (int array1[restrict 10], int array2[restrict 10])
@r{@dots{}}
@end example

@noindent
Using @code{restrict} promises the compiler that callers will
not pass in the same array for more than one @code{restrict} array
parameter.  Knowing this enables the compiler to perform better code
optimization. This is the same effect as using @code{restrict}
pointers (@pxref{restrict Pointers}), but makes it clear when reading
the code that an array of a specific size is expected.

@node Structs as Parameters
@subsection Functions That Accept Structure Arguments

Structures in GNU C are first-class objects, so using them as function
parameters and arguments works in the natural way.  This function
@code{swapfoo} takes a @code{struct foo} with two fields as argument,
and returns a structure of the same type but with the fields
exchanged.

@example
struct foo @{ int a, b; @};

struct foo x;

struct foo
swapfoo (struct foo inval)
@{
  struct foo outval;
  outval.a = inval.b;
  outval.b = inval.a;
  return outval;
@}
@end example

This simpler definition of @code{swapfoo} avoids using a local
variable to hold the result about to be return, by using a structure
constructor (@pxref{Structure Constructors}), like this:

@example
struct foo
swapfoo (struct foo inval)
@{
  return (struct foo) @{ inval.b, inval.a @};
@}
@end example

It is valid to define a structure type in a function's parameter list,
as in

@example
int
frob_bar (struct bar @{ int a, b; @} inval)
@{
  @var{body}
@}
@end example

@noindent
and @var{body} can access the fields of @var{inval} since the
structure type @code{struct bar} is defined for the whole function
body.  However, there is no way to create a @code{struct bar} argument
to pass to @code{frob_bar}, except with kludges.  As a result,
defining a structure type in a parameter list is useless in practice.

@node Function Declarations
@section Function Declarations
@cindex function declarations
@cindex declararing functions

To call a function, or use its name as a pointer, a @dfn{function
declaration} for the function name must be in effect at that point in
the code.  The function's definition serves as a declaration of that
function for the rest of the containing scope, but to use the function
in code before the definition, or from another compilation module, a
separate function declaration must precede the use.

A function declaration looks like the start of a function definition.
It begins with the return value type (@code{void} if none) and the
function name, followed by argument declarations in parentheses
(though these can sometimes be omitted).  But that's as far as the
similarity goes: instead of the function body, the declaration uses a
semicolon.

@cindex function prototype
@cindex prototype of a function
A declaration that specifies argument types is called a @dfn{function
prototype}.  You can include the argument names or omit them.  The
names, if included in the declaration, have no effect, but they may
serve as documentation.

This form of prototype specifies fixed argument types:

@example
@var{rettype} @var{function} (@var{argtypes}@r{@dots{}});
@end example

@noindent
This form says the function takes no arguments:

@example
@var{rettype} @var{function} (void);
@end example

@noindent
This form declares types for some arguments, and allows additional
arguments whose types are not specified:

@example
@var{rettype} @var{function} (@var{argtypes}@r{@dots{}}, ...);
@end example

For a parameter that's an array of variable length, you can write
its declaration with @samp{*} where the ``length'' of the array would
normally go; for example, these are all equivalent.

@example
double maximum (int n, int m, double a[n][m]);
double maximum (int n, int m, double a[*][*]);
double maximum (int n, int m, double a[ ][*]);
double maximum (int n, int m, double a[ ][m]);
@end example

@noindent
The old-fashioned form of declaration, which is not a prototype, says
nothing about the types of arguments or how many they should be:

@example
@var{rettype} @var{function} ();
@end example

@strong{Warning:} Arguments passed to a function declared without a
prototype are converted with the default argument promotions
(@pxref{Argument Promotions}.  Likewise for additional arguments whose
types are unspecified.

Function declarations are usually written at the top level in a source file,
but you can also put them inside code blocks.  Then the function name
is visible for the rest of the containing scope.  For example:

@example
void
foo (char *file_name)
@{
  void save_file (char *);
  save_file (file_name);
@}
@end example

If another part of the code tries to call the function
@code{save_file}, this declaration won't be in effect there.  So the
function will get an implicit declaration of the form @code{extern int
save_file ();}.   That conflicts with the explicit declaration
here, and the discrepancy generates a warning.

The syntax of C traditionally allows omitting the data type in a
function declaration if it specifies a storage class or a qualifier.
Then the type defaults to @code{int}.  For example:

@example
static foo (double x);
@end example

@noindent
defaults the return type to @code{int}.
This is bad practice; if you see it, fix it.

Calling a function that is undeclared has the effect of creating
an @dfn{implicit} declaration in the innermost containing scope,
equivalent to this:

@example
extern int @dfn{function} ();
@end example

@noindent
This declaration says that the function returns @code{int} but leaves
its argument types unspecified.  If that does not accurately fit the
function, then the program @strong{needs} an explicit declaration of
the function with argument types in order to call it correctly.

Implicit declarations are deprecated, and a function call that creates one
causes a warning.

@node Function Calls
@section Function Calls
@cindex function calls
@cindex calling functions

Starting a program automatically calls the function named @code{main}
(@pxref{The main Function}).  Aside from that, a function does nothing
except when it is @dfn{called}.  That occurs during the execution of a
function-call expression specifying that function.

A function-call expression looks like this:

@example
@var{function} (@var{arguments}@r{@dots{}})
@end example

Most of the time, @var{function} is a function name.  However, it can
also be an expression with a function pointer value; that way, the
program can determine at run time which function to call.

The @var{arguments} are a series of expressions separated by commas.
Each expression specifies one argument to pass to the function.

The list of arguments in a function call looks just like use of the
comma operator (@pxref{Comma Operator}), but the fact that it fills
the parentheses of a function call gives it a different meaning.

Here's an example of a function call, taken from an example near the
beginning (@pxref{Complete Program}).

@example
printf ("Fibonacci series item %d is %d\n",
        19, fib (19));
@end example

The three arguments given to @code{printf} are a constant string, the
integer 19, and the integer returned by @code{fib (19)}.

@node Function Call Semantics
@section Function Call Semantics
@cindex function call semantics
@cindex semantics of function calls
@cindex call-by-value

The meaning of a function call is to compute the specified argument
expressions, convert their values according to the function's
declaration, then run the function giving it copies of the converted
values.  (This method of argument passing is known as
@dfn{call-by-value}.)  When the function finishes, the value it
returns becomes the value of the function-call expression.

Call-by-value implies that an assignment to the function argument
variable has no direct effect on the caller.  For instance,

@example
#include <stdlib.h>  /* @r{Defines @code{EXIT_SUCCESS}.} */
#include <stdio.h>   /* @r{Declares @code{printf}.} */

void
subroutine (int x)
@{
  x = 5;
@}

void
main (void)
@{
  int y = 20;
  subroutine (y);
  printf ("y is %d\n", y);
  return EXIT_SUCCESS;
@}
@end example

@noindent
prints @samp{y is 20}.  Calling @code{subroutine} initializes @code{x}
from the value of @code{y}, but this does not establish any other
relationship between the two variables.  Thus, the assignment to
@code{x}, inside @code{subroutine}, changes only @emph{that} @code{x}.

If an argument's type is specified by the function's declaration, the
function call converts the argument expression to that type if
possible.  If the conversion is impossible, that is an error.

If the function's declaration doesn't specify the type of that
argument, then the @emph{default argument promotions} apply.
@xref{Argument Promotions}.

@node Function Pointers
@section Function Pointers
@cindex function pointers
@cindex pointers to functions

A function name refers to a fixed function.  Sometimes it is useful to
call a function to be determined at run time; to do this, you can use
a @dfn{function pointer value} that points to the chosen function
(@pxref{Pointers}).

Pointer-to-function types can be used to declare variables and other
data, including array elements, structure fields, and union
alternatives.  They can also be used for function arguments and return
values.  These types have the peculiarity that they are never
converted automatically to @code{void *} or vice versa.  However, you
can do that conversion with a cast.

@menu
* Declaring Function Pointers:: How to declare a pointer to a function.
* Assigning Function Pointers:: How to assign values to function pointers.
* Calling Function Pointers::   How to call functions through pointers.
@end menu

@node Declaring Function Pointers
@subsection Declaring Function Pointers
@cindex declaring function pointers
@cindex function pointers, declaring

The declaration of a function pointer variable (or structure field)
looks almost like a function declaration, except it has an additional
@samp{*} just before the variable name.  Proper nesting requires a
pair of parentheses around the two of them.  For instance, @code{int
(*a) ();} says, ``Declare @code{a} as a pointer such that @code{*a} is
an @code{int}-returning function.''

Contrast these three declarations:

@example
/* @r{Declare a function returning @code{char *}.}  */
char *a (char *);
/* @r{Declare a pointer to a function returning @code{char}.}  */
char (*a) (char *);
/* @r{Declare a pointer to a function returning @code{char *}.}  */
char *(*a) (char *);
@end example

The possible argument types of the function pointed to are the same
as in a function declaration.  You can write a prototype
that specifies all the argument types:

@example
@var{rettype} (*@var{function}) (@var{arguments}@r{@dots{}});
@end example

@noindent
or one that specifies some and leaves the rest unspecified:

@example
@var{rettype} (*@var{function}) (@var{arguments}@r{@dots{}}, ...);
@end example

@noindent
or one that says there are no arguments:

@example
@var{rettype} (*@var{function}) (void);
@end example

You can also write a non-prototype declaration that says
nothing about the argument types:

@example
@var{rettype} (*@var{function}) ();
@end example

For example, here's a declaration for a variable that should
point to some arithmetic function that operates on two @code{double}s:

@example
double (*binary_op) (double, double);
@end example

Structure fields, union alternatives, and array elements can be
function pointers; so can parameter variables.  The function pointer
declaration construct can also be combined with other operators
allowed in declarations.  For instance,

@example
int **(*foo)();
@end example

@noindent
declares @code{foo} as a pointer to a function that returns
type @code{int **}, and

@example
int **(*foo[30])();
@end example

@noindent
declares @code{foo} as an array of 30 pointers to functions that
return type @code{int **}.

@example
int **(**foo)();
@end example

@noindent
declares @code{foo} as a pointer to a pointer to a function that
returns type @code{int **}.

@node Assigning Function Pointers
@subsection Assigning Function Pointers
@cindex assigning function pointers
@cindex function pointers, assigning

Assuming we have declared the variable @code{binary_op} as in the
previous section, giving it a value requires a suitable function to
use.  So let's define a function suitable for the variable to point
to.  Here's one:

@example
double
double_add (double a, double b)
@{
  return a+b;
@}
@end example

Now we can give it a value:

@example
binary_op = double_add;
@end example

The target type of the function pointer must be upward compatible with
the type of the function (@pxref{Compatible Types}).

There is no need for @samp{&} in front of @code{double_add}.
Using a function name such as @code{double_add} as an expression
automatically converts it to the function's address, with the
appropriate function pointer type.  However, it is ok to use
@samp{&} if you feel that is clearer:

@example
binary_op = &double_add;
@end example

@node Calling Function Pointers
@subsection Calling Function Pointers
@cindex calling function pointers
@cindex function pointers, calling

To call the function specified by a function pointer, just write the
function pointer value in a function call.  For instance, here's a
call to the function @code{binary_op} points to:

@example
binary_op (x, 5)
@end example

Since the data type of @code{binary_op} explicitly specifies type
@code{double} for the arguments, the call converts @code{x} and 5 to
@code{double}.

The call conceptually dereferences the pointer @code{binary_op} to
``get'' the function it points to, and calls that function.  If you
wish, you can explicitly represent the dereference by writing the
@code{*} operator:

@example
(*binary_op) (x, 5)
@end example

The @samp{*} reminds people reading the code that @code{binary_op} is
a function pointer rather than the name of a specific function.

@node The main Function
@section The @code{main} Function
@cindex @code{main} function
@findex main

Every complete executable program requires at least one function,
called @code{main}, which is where execution begins.  You do not have
to explicitly declare @code{main}, though GNU C permits you to do so.
Conventionally, @code{main} should be defined to follow one of these
calling conventions:

@example
int main (void) @{@r{@dots{}}@}
int main (int argc, char *argv[]) @{@r{@dots{}}@}
int main (int argc, char *argv[], char *envp[]) @{@r{@dots{}}@}
@end example

@noindent
Using @code{void} as the parameter list means that @code{main} does
not use the arguments.  You can write @code{char **argv} instead of
@code{char *argv[]}, and likewise for @code{envp}, as the two
constructs are equivalent.

@ignore   @c Not so at present
Defining @code{main} in any other way generates a warning.  Your
program will still compile, but you may get unexpected results when
executing it.
@end ignore

You can call @code{main} from C code, as you can call any other
function, though that is an unusual thing to do.  When you do that,
you must write the call to pass arguments that match the parameters in
the definition of @code{main}.

The @code{main} function is not actually the first code that runs when
a program starts.  In fact, the first code that runs is system code
from the file @file{crt0.o}.  In Unix, this was hand-written assembler
code, but in GNU we replaced it with C code.  Its job is to find
the arguments for @code{main} and call that.

@menu
* Values from main::         Returning values from the main function.
* Command-Line Parameters::  Accessing command-line parameters
                               provided to the program.
* Environment Variables::    Accessing system environment variables.
@end menu

@node Values from main
@subsection Returning Values from @code{main}
@cindex returning values from @code{main}
@cindex success
@cindex failure
@cindex exit status

When @code{main} returns, the process terminates.  Whatever value
@code{main} returns becomes the exit status which is reported to the
parent process.  While nominally the return value is of type
@code{int}, in fact the exit status gets truncated to eight bits; if
@code{main} returns the value 256, the exit status is 0.

Normally, programs return only one of two values: 0 for success,
and 1 for failure. For maximum portability, use the macro
values @code{EXIT_SUCCESS} and @code{EXIT_FAILURE} defined in
@code{stdlib.h}.  Here's an example:

@cindex @code{EXIT_FAILURE}
@cindex @code{EXIT_SUCCESS}
@example
#include <stdlib.h>  /* @r{Defines @code{EXIT_SUCCESS}} */
                     /* @r{and @code{EXIT_FAILURE}.} */

int
main (void)
@{
  @r{@dots{}}
  if (foo)
    return EXIT_SUCCESS;
  else
    return EXIT_FAILURE;
@}
@end example

Some types of programs maintain special conventions for various return
values; for example, comparison programs including @code{cmp} and
@code{diff} return 1 to indicate a mismatch, and 2 to indicate that
the comparison couldn't be performed.

@node Command-Line Parameters
@subsection Accessing Command-Line Parameters
@cindex command-line parameters
@cindex parameters, command-line

If the program was invoked with any command-line arguments, it can
access them through the arguments of @code{main}, @code{argc} and
@code{argv}.  (You can give these arguments any names, but the names
@code{argc} and @code{argv} are customary.)

The value of @code{argv} is an array containing all of the
command-line arguments as strings, with the name of the command
invoked as the first string.  @code{argc} is an integer that says how
many strings @code{argv} contains.  Here is an example of accessing
the command-line parameters, retrieving the program's name and
checking for the standard @option{--version} and @option{--help} options:

@example
#include <string.h> /* @r{Declare @code{strcmp}.} */

int
main (int argc, char *argv[])
@{
  char *program_name = argv[0];

  for (int i = 1; i < argc; i++)
    @{
      if (!strcmp (argv[i], "--version"))
        @{
          /* @r{Print version information and exit.} */
          @r{@dots{}}
        @}
      else if (!strcmp (argv[i], "--help"))
        @{
          /* @r{Print help information and exit.} */
          @r{@dots{}}
        @}
    @}
  @r{@dots{}}
@}
@end example

@node Environment Variables
@subsection Accessing Environment Variables
@cindex environment variables

You can optionally include a third parameter to @code{main}, another
array of strings, to capture the environment variables available to
the program.  Unlike what happens with @code{argv}, there is no
additional parameter for the count of environment variables; rather,
the array of environment variables concludes with a null pointer.

@example
#include <stdio.h>   /* @r{Declares @code{printf}.} */

int
main (int argc, char *argv[], char *envp[])
@{
  /* @r{Print out all environment variables.} */
  int i = 0;
  while (envp[i])
    @{
      printf ("%s\n", envp[i]);
      i++;
    @}
@}
@end example

Another method of retrieving environment variables is to use the
library function @code{getenv}, which is defined in @code{stdlib.h}.
Using @code{getenv} does not require defining @code{main} to accept the
@code{envp} pointer.  For example, here is a program that fetches and prints
the user's home directory (if defined):

@example
#include <stdlib.h>  /* @r{Declares @code{getenv}.} */
#include <stdio.h>   /* @r{Declares @code{printf}.} */

int
main (void)
@{
  char *home_directory = getenv ("HOME");
  if (home_directory)
    printf ("My home directory is: %s\n", home_directory);
  else
    printf ("My home directory is not defined!\n");
@}
@end example

@node Advanced Definitions
@section Advanced Function Features

This section describes some advanced or obscure features for GNU C
function definitions.  If you are just learning C, you can skip the
rest of this chapter.

@menu
* Variable-Length Array Parameters:: Functions that accept arrays
                                       of variable length.
* Variable Number of Arguments::     Variadic functions.
* Nested Functions::                 Defining functions within functions.
* Inline Function Definitions::      A function call optimization technique.
@end menu

@node Variable-Length Array Parameters
@subsection Variable-Length Array Parameters
@cindex variable-length array parameters
@cindex array parameters, variable-length
@cindex functions that accept variable-length arrays

An array parameter can have variable length: simply declare the array
type with a size that isn't constant.  In a nested function, the
length can refer to a variable defined in a containing scope.  In any
function, it can refer to a previous parameter, like this:

@example
struct entry
tester (int len, char data[len][len])
@{
  @r{@dots{}}
@}
@end example

Alternatively, in function declarations (but not in function
definitions), you can use @code{[*]} to denote that the array
parameter is of a variable length, such that these two declarations
mean the same thing:

@example
struct entry
tester (int len, char data[len][len]);
@end example

@example
struct entry
tester (int len, char data[*][*]);
@end example

@noindent
The two forms of input are equivalent in GNU C, but emphasizing that
the array parameter is variable-length may be helpful to those
studying the code.

You can also omit the length parameter, and instead use some other
in-scope variable for the length in the function definition:

@example
struct entry
tester (char data[*][*]);
@r{@dots{}}
int data_length = 20;
@r{@dots{}}
struct entry
tester (char data[data_length][data_length])
@{
  @r{@dots{}}
@}
@end example

@c ??? check text above

@cindex parameter forward declaration
In GNU C, to pass the array first and the length afterward, you can
use a @dfn{parameter forward declaration}, like this:

@example
struct entry
tester (int len; char data[len][len], int len)
@{
  @r{@dots{}}
@}
@end example

The @samp{int len} before the semicolon is the parameter forward
declaration; it serves the purpose of making the name @code{len} known
when the declaration of @code{data} is parsed.

You can write any number of such parameter forward declarations in the
parameter list.  They can be separated by commas or semicolons, but
the last one must end with a semicolon, which is followed by the
``real'' parameter declarations.  Each forward declaration must match
a subsequent ``real'' declaration in parameter name and data type.

Standard C does not support parameter forward declarations.

@node Variable Number of Arguments
@subsection Variable-Length Parameter Lists
@cindex variable-length parameter lists
@cindex parameters lists, variable length
@cindex function parameter lists, variable length

@cindex variadic function
A function that takes a variable number of arguments is called a
@dfn{variadic function}.  In C, a variadic function must specify at
least one fixed argument with an explicitly declared data type.
Additional arguments can follow, and can vary in both quantity and
data type.

In the function header, declare the fixed parameters in the normal
way, then write a comma and an ellipsis: @samp{, ...}.  Here is an
example of a variadic function header:

@example
int add_multiple_values (int number, ...)
@end example

@cindex @code{va_list}
@cindex @code{va_start}
@cindex @code{va_end}
The function body can refer to fixed arguments by their parameter
names, but the additional arguments have no names.  Accessing them in
the function body uses certain standard macros.  They are defined in
the library header file @file{stdarg.h}, so the code must
@code{#include} that file.

In the body, write

@example
va_list ap;
va_start (ap, @var{last_fixed_parameter});
@end example

@noindent
This declares the variable @code{ap} (you can use any name for it)
and then sets it up to point before the first additional argument.

Then, to fetch the next consecutive additional argument, write this:

@example
va_arg (ap, @var{type})
@end example

After fetching all the additional arguments (or as many as need to be
used), write this:

@example
va_end (ap);
@end example

Here's an example of a variadic function definition that adds any
number of @code{int} arguments.  The first (fixed) argument says how
many more arguments follow.

@example
#include <stdarg.h> /* @r{Defines @code{va}@r{@dots{}} macros.} */
@r{@dots{}}

int
add_multiple_values (int argcount, ...)
@{
  int counter, total = 0;

  /* @r{Declare a variable of type @code{va_list}.} */
  va_list argptr;

  /* @r{Initialize that variable..} */
  va_start (argptr, argcount);

  for (counter = 0; counter < argcount; counter++)
    @{
      /* @r{Get the next additional argument.} */
      total += va_arg (argptr, int);
    @}

  /* @r{End use of the @code{argptr} variable.} */
  va_end (argptr);

  return total;
@}
@end example

With GNU C, @code{va_end} is superfluous, but some other compilers
might make @code{va_start} allocate memory so that calling
@code{va_end} is necessary to avoid a memory leak.  Before doing
@code{va_start} again with the same variable, do @code{va_end}
first.

@cindex @code{va_copy}
Because of this possible memory allocation, it is risky (in principle)
to copy one @code{va_list} variable to another with assignment.
Instead, use @code{va_copy}, which copies the substance but allocates
separate memory in the variable you copy to.  The call looks like
@code{va_copy (@var{to}, @var{from})}, where both @var{to} and
@var{from} should be variables of type @code{va_list}.  In principle,
do @code{va_end} on each of these variables before its scope ends.

Since the additional arguments' types are not specified in the
function's definition, the default argument promotions
(@pxref{Argument Promotions}) apply to them in function calls.  The
function definition must take account of this; thus, if an argument
was passed as @code{short}, the function should get it as @code{int}.
If an argument was passed as @code{float}, the function should get it
as @code{double}.

C has no mechanism to tell the variadic function how many arguments
were passed to it, so its calling convention must give it a way to
determine this.  That's why @code{add_multiple_values} takes a fixed
argument that says how many more arguments follow.  Thus, you can
call the function like this:

@example
sum = add_multiple_values (3, 12, 34, 190);
/* @r{Value is 12+34+190.} */
@end example

In GNU C, there is no actual need to use the @code{va_end} function.
In fact, it does nothing.  It's used for compatibility with other
compilers, when that matters.

It is a mistake to access variables declared as @code{va_list} except
in the specific ways described here.  Just what that type consists of
is an implementation detail, which could vary from one platform to
another.

@node Nested Functions
@subsection Nested Functions
@cindex nested functions
@cindex functions, nested
@cindex downward funargs
@cindex thunks

A @dfn{nested function} is a function defined inside another function.
(The ability to do this is indispensable for automatic translation of
certain programming languages into C.)  The nested function's name is
local to the block where it is defined.  For example, here we define a
nested function named @code{square}, then call it twice:

@example
@group
foo (double a, double b)
@{
  double square (double z) @{ return z * z; @}

  return square (a) + square (b);
@}
@end group
@end example

The nested function definition can access all the variables of the containing
function that are visible at the point of its definition.  This is
called @dfn{lexical scoping}.  For example, here we show a nested
function that uses an inherited variable named @code{offset}:

@example
@group
bar (int *array, int offset, int size)
@{
  int access (int *array, int index)
    @{ return array[index + offset]; @}
  int i;
  @r{@dots{}}
  for (i = 0; i < size; i++)
    @r{@dots{}} access (array, i) @r{@dots{}}
@}
@end group
@end example

Nested function definitions can appear wherever automatic variable
declarations are allowed; that is, in any block, interspersed with the
other declarations and statements in the block.

The nested function's name is visible only within the parent block;
the name's scope starts from its definition and continues to the end
of the containing block.  If the nested function's name
is the same as the parent function's name, there will be
no way to refer to the parent function inside the scope of the
name of the nested function.

Using @code{extern} or @code{static} on a nested function definition
is an error.

It is possible to call the nested function from outside the scope of its
name by storing its address or passing the address to another function.
You can do this safely, but you must be careful:

@example
@group
hack (int *array, int size, int addition)
@{
  void store (int index, int value)
    @{ array[index] = value + addition; @}

  intermediate (store, size);
@}
@end group
@end example

Here, the function @code{intermediate} receives the address of
@code{store} as an argument.  If @code{intermediate} calls @code{store},
the arguments given to @code{store} are used to store into @code{array}.
@code{store} also accesses @code{hack}'s local variable @code{addition}.

It is safe for @code{intermediate} to call @code{store} because
@code{hack}'s stack frame, with its arguments and local variables,
continues to exist during the call to @code{intermediate}.

Calling the nested function through its address after the containing
function has exited is asking for trouble.  If it is called after a
containing scope level has exited, and if it refers to some of the
variables that are no longer in scope, it will refer to memory
containing junk or other data.  It's not wise to take the risk.

The GNU C Compiler implements taking the address of a nested function
using a technique called @dfn{trampolines}.  This technique was
described in @cite{Lexical Closures for C@t{++}} (Thomas M. Breuel,
USENIX C@t{++} Conference Proceedings, October 17--21, 1988).

A nested function can jump to a label inherited from a containing
function, provided the label was explicitly declared in the containing
function (@pxref{Local Labels}).  Such a jump returns instantly to the
containing function, exiting the nested function that did the
@code{goto} and any intermediate function invocations as well.  Here
is an example:

@example
@group
bar (int *array, int offset, int size)
@{
  /* @r{Explicitly declare the label @code{failure}.} */
  __label__ failure;
  int access (int *array, int index)
    @{
      if (index > size)
        /* @r{Exit this function,}
           @r{and return to @code{bar}.} */
        goto failure;
      return array[index + offset];
    @}
@end group

@group
  int i;
  @r{@dots{}}
  for (i = 0; i < size; i++)
    @r{@dots{}} access (array, i) @r{@dots{}}
  @r{@dots{}}
  return 0;

 /* @r{Control comes here from @code{access}
    if it does the @code{goto}.}  */
 failure:
  return -1;
@}
@end group
@end example

To declare the nested function before its definition, use
@code{auto} (which is otherwise meaningless for function declarations;
@pxref{auto and register}).  For example,

@example
bar (int *array, int offset, int size)
@{
  auto int access (int *, int);
  @r{@dots{}}
  @r{@dots{}} access (array, i) @r{@dots{}}
  @r{@dots{}}
  int access (int *array, int index)
    @{
      @r{@dots{}}
    @}
  @r{@dots{}}
@}
@end example

@node Inline Function Definitions
@subsection Inline Function Definitions
@cindex inline function definitions
@cindex function definitions, inline
@findex inline

To declare a function inline, use the @code{inline} keyword in its
definition.  Here's code to define functions to access two fields
in a structure, and inlines them so that there is no cost to accessing
them by calling the functions.

@example
struct list
@{
  struct list *first, *second;
@};

inline struct list *
list_first (struct list *p)
@{
  return p->first;
@}

inline struct list *
list_second (struct list *p)
@{
  return p->second;
@}
@end example

Optimized compilation can substitute the inline function's body for
any call to it.  This is called @emph{inlining} the function.  It
makes the code that contains the call run faster, significantly so if
the inline function is small.

Here's a function that uses @code{list_second}:

@example
int
pairlist_length (struct list *l)
@{
  int length = 0;
  while (l)
    @{
      length++;
      l = list_second (l);
    @}
  return length;
@}
@end example

Substituting the code of @code{list_second} into the definition of
@code{pairlist_length} results in this code, in effect:

@example
int
pairlist_length (struct list *l)
@{
  int length = 0;
  while (l)
    @{
      length++;
      l = l->second;
    @}
  return length;
@}
@end example

Since the definition of @code{list_second} does not say @code{extern}
or @code{static}, that definition is used only for inlining.  It
doesn't generate code that can be called at run time.  If not all the
calls to the function are inlined, there must be a definition of the
same function name in another module for them to call.

@cindex inline functions, omission of
@c @opindex fkeep-inline-functions
Adding @code{static} to an inline function definition means the
function definition is limited to this compilation module.  Also, it
generates run-time code if necessary for the sake of any calls that
were not inlined.  If all calls are inlined then the function
definition does not generate run-time code, but you can force
generation of run-time code with the option
@option{-fkeep-inline-functions}.

@cindex extern inline function
Specifying @code{extern} along with @code{inline} means the function is
external and generates run-time code to be called from other
separately compiled modules, as well as inlined.  You can define the
function as @code{inline} without @code{extern} in other modules so as
to inline calls to the same function in those modules.

Why are some calls not inlined?  First of all, inlining is an
optimization, so non-optimized compilation does not inline.

Some calls cannot be inlined for technical reasons.  Also, certain
usages in a function definition can make it unsuitable for inline
substitution.  Among these usages are: variadic functions, use of
@code{alloca}, use of computed goto (@pxref{Labels as Values}), and
use of nonlocal goto.  The option @option{-Winline} requests a warning
when a function marked @code{inline} is unsuitable to be inlined.  The
warning explains what obstacle makes it unsuitable.

Just because a call @emph{can} be inlined does not mean it
@emph{should} be inlined.  The GNU C compiler weighs costs and
benefits to decide whether inlining a particular call is advantageous.

You can force inlining of all calls to a given function that can be
inlined, even in a non-optimized compilation. by specifying the
@samp{always_inline} attribute for the function, like this:

@example
/* @r{Prototype.}  */
inline void foo (const char) __attribute__((always_inline));
@end example

@noindent
This is a GNU C extension.  @xref{Attributes}.

A function call may be inlined even if not declared @code{inline} in
special cases where the compiler can determine this is correct and
desirable.  For instance, when a static function is called only once,
it will very likely be inlined.  With @option{-flto}, link-time
optimization, any function might be inlined.  To absolutely prevent
inlining of a specific function, specify
@code{__attribute__((__noinline__))} in the function's definition.

@node Obsolete Definitions
@section Obsolete Function Features

These features of function definitions are still used in old
programs, but you shouldn't write code this way today.
If you are just learning C, you can skip this section.

@menu
* Old GNU Inlining::                 An older inlining technique.
* Old-Style Function Definitions::   Original K&R style functions.
@end menu

@node Old GNU Inlining
@subsection Older GNU C Inlining

The GNU C spec for inline functions, before GCC version 5, defined
@code{extern inline} on a function definition to mean to inline calls
to it but @emph{not} generate code for the function that could be
called at run time.  By contrast, @code{inline} without @code{extern}
specified to generate run-time code for the function.  In effect, ISO
incompatibly flipped the meanings of these two cases.  We changed GCC
in version 5 to adopt the ISO specification.

Many programs still use these cases with the previous GNU C meanings.
You can specify use of those meanings with the option
@option{-fgnu89-inline}.  You can also specify this for a single
function with @code{__attribute__ ((gnu_inline))}.  Here's an example:

@example
inline __attribute__ ((gnu_inline))
int
inc (int *a)
@{
  (*a)++;
@}
@end example

@node Old-Style Function Definitions
@subsection Old-Style Function Definitions
@cindex old-style function definitions
@cindex function definitions, old-style
@cindex K&R-style function definitions

The syntax of C traditionally allows omitting the data type in a
function declaration if it specifies a storage class or a qualifier.
Then the type defaults to @code{int}.  For example:

@example
static foo (double x);
@end example

@noindent
defaults the return type to @code{int}.  This is bad practice; if you
see it, fix it.

An @dfn{old-style} (or ``K&R'') function definition is the way
function definitions were written in the 1980s.  It looks like this:

@example
@var{rettype}
@var{function} (@var{parmnames})
  @var{parm_declarations}
@{
  @var{body}
@}
@end example

In @var{parmnames}, only the parameter names are listed, separated by
commas.  Then @var{parm_declarations} declares their data types; these
declarations look just like variable declarations.  If a parameter is
listed in @var{parmnames} but has no declaration, it is implicitly
declared @code{int}.

There is no reason to write a definition this way nowadays, but they
can still be seen in older GNU programs.

An old-style variadic function definition looks like this:

@example
#include <varargs.h>

int
add_multiple_values (va_alist)
    va_dcl
@{
  int argcount;
  int counter, total = 0;

  /* @r{Declare a variable of type @code{va_list}.} */
  va_list argptr;

  /* @r{Initialize that variable.} */
  va_start (argptr);

  /* @r{Get the first argument (fixed).} */
  argcount = va_arg (int);

  for (counter = 0; counter < argcount; counter++)
    @{
      /* @r{Get the next additional argument.} */
      total += va_arg (argptr, int);
    @}

  /* @r{End use of the @code{argptr} variable.} */
  va_end (argptr);

  return total;
@}
@end example

Note that the old-style variadic function definition has no fixed
parameter variables; all arguments must be obtained with
@code{va_arg}.

@node Compatible Types
@chapter Compatible Types
@cindex compatible types
@cindex types, compatible

Declaring a function or variable twice is valid in C only if the two
declarations specify @dfn{compatible} types.  In addition, some
operations on pointers require operands to have compatible target
types.

In C, two different primitive types are never compatible.  Likewise for
the defined types @code{struct}, @code{union} and @code{enum}: two
separately defined types are incompatible unless they are defined
exactly the same way.

However, there are a few cases where different types can be
compatible:

@itemize @bullet
@item
Every enumeration type is compatible with some integer type.  In GNU
C, the choice of integer type depends on the largest enumeration
value.

@c ??? Which one, in GCC?
@c ??? ... it varies, depending on the enum values. Testing on
@c ??? fencepost, it appears to use a 4-byte signed integer first,
@c ??? then moves on to an 8-byte signed integer. These details
@c ??? might be platform-dependent, as the C standard says that even
@c ??? char could be used as an enum type, but it's at least true
@c ??? that GCC chooses a type that is at least large enough to
@c ??? hold the largest enum value.

@item
Array types are compatible if the element types are compatible
and the sizes (when specified) match.

@item
Pointer types are compatible if the pointer target types are
compatible.

@item
Function types that specify argument types are compatible if the
return types are compatible and the argument types are compatible,
argument by argument.  In addition, they must all agree in whether
they use @code{...} to allow additional arguments.

@item
Function types that don't specify argument types are compatible if the
return types are.

@item
Function types that specify the argument types are compatible with
function types that omit them, if the return types are compatible and
the specified argument types are unaltered by the argument promotions
(@pxref{Argument Promotions}).
@end itemize

In order for types to be compatible, they must agree in their type
qualifiers.  Thus, @code{const int} and @code{int} are incompatible.
It follows that @code{const int *} and @code{int *} are incompatible
too (they are pointers to types that are not compatible).

If two types are compatible ignoring the qualifiers, we call them
@dfn{nearly compatible}.  (If they are array types, we ignore
qualifiers on the element types.@footnote{This is a GNU C extension.})
Comparison of pointers is valid if the pointers' target types are
nearly compatible.  Likewise, the two branches of a conditional
expression may be pointers to nearly compatible target types.

If two types are compatible ignoring the qualifiers, and the first
type has all the qualifiers of the second type, we say the first is
@dfn{upward compatible} with the second.  Assignment of pointers
requires the assigned pointer's target type to be upward compatible
with the right operand (the new value)'s target type.

@node Type Conversions
@chapter Type Conversions
@cindex type conversions
@cindex conversions, type

C converts between data types automatically when that seems clearly
necessary.  In addition, you can convert explicitly with a @dfn{cast}.

@menu
* Explicit Type Conversion::     Casting a value from one type to another.
* Assignment Type Conversions::  Automatic conversion by assignment operation.
* Argument Promotions::          Automatic conversion of function parameters.
* Operand Promotions::           Automatic conversion of arithmetic operands.
* Common Type::                  When operand types differ, which one is used?
@end menu

@node Explicit Type Conversion
@section Explicit Type Conversion
@cindex cast
@cindex explicit type conversion

You can do explicit conversions using the unary @dfn{cast} operator,
which is written as a type designator (@pxref{Type Designators}) in
parentheses.  For example, @code{(int)} is the operator to cast to
type @code{int}.  Here's an example of using it:

@example
@{
  double d = 5.5;

  printf ("Floating point value: %f\n", d);
  printf ("Rounded to integer: %d\n", (int) d);
@}
@end example

Using @code{(int) d} passes an @code{int} value as argument to
@code{printf}, so you can print it with @samp{%d}.  Using just
@code{d} without the cast would pass the value as @code{double}.
That won't work at all with @samp{%d}; the results would be gibberish.

To divide one integer by another without rounding,
cast either of the integers to @code{double} first:

@example
(double) @var{dividend} / @var{divisor}
@var{dividend} / (double) @var{divisor}
@end example

It is enough to cast one of them, because that forces the common type
to @code{double} so the other will be converted automatically.

The valid cast conversions are:

@itemize @bullet
@item
One numerical type to another.

@item
One pointer type to another.
(Converting between pointers that point to functions
and pointers that point to data is not standard C.)

@item
A pointer type to an integer type.

@item
An integer type to a pointer type.

@item
To a union type, from the type of any alternative in the union
(@pxref{Unions}).  (This is a GNU extension.)

@item
Anything, to @code{void}.
@end itemize

@node Assignment Type Conversions
@section Assignment Type Conversions
@cindex assignment type conversions

Certain type conversions occur automatically in assignments
and certain other contexts.  These are the conversions
assignments can do:

@itemize @bullet
@item
Converting any numeric type to any other numeric type.

@item
Converting @code{void *} to any other pointer type
(except pointer-to-function types).

@item
Converting any other pointer type to @code{void *}.
(except pointer-to-function types).

@item
Converting 0 (a null pointer constant) to any pointer type.

@item
Converting any pointer type to @code{bool}.  (The result is
1 if the pointer is not null.)

@item
Converting between pointer types when the left-hand target type is
upward compatible with the right-hand target type.  @xref{Compatible
Types}.
@end itemize

These type conversions occur automatically in certain contexts,
which are:

@itemize @bullet
@item
An assignment converts the type of the right-hand expression
to the type wanted by the left-hand expression.  For example,

@example
double i;
i = 5;
@end example

@noindent
converts 5 to @code{double}.

@item
A function call, when the function specifies the type for that
argument, converts the argument value to that type.  For example,

@example
void foo (double);
foo (5);
@end example

@noindent
converts 5 to @code{double}.

@item
A @code{return} statement converts the specified value to the type
that the function is declared to return.  For example,

@example
double
foo ()
@{
  return 5;
@}
@end example

@noindent
also converts 5 to @code{double}.
@end itemize

In all three contexts, if the conversion is impossible, that
constitutes an error.

@node Argument Promotions
@section Argument Promotions
@cindex argument promotions
@cindex promotion of arguments

When a function's definition or declaration does not specify the type
of an argument, that argument is passed without conversion in whatever
type it has, with these exceptions:

@itemize @bullet
@item
Some narrow numeric values are @dfn{promoted} to a wider type.  If the
expression is a narrow integer, such as @code{char} or @code{short},
the call converts it automatically to @code{int} (@pxref{Integer
Types}).@footnote{On an embedded controller where @code{char}
or @code{short} is the same width as @code{int}, @code{unsigned char}
or @code{unsigned short} promotes to @code{unsigned int}, but that
never occurs in GNU C on real computers.}

In this example, the expression @code{c} is passed as an @code{int}:

@example
char c = '$';

printf ("Character c is '%c'\n", c);
@end example

@item
If the expression
has type @code{float}, the call converts it automatically to
@code{double}.

@item
An array as argument is converted to a pointer to its zeroth element.

@item
A function name as argument is converted to a pointer to that function.
@end itemize

@node Operand Promotions
@section Operand Promotions
@cindex operand promotions

The operands in arithmetic operations undergo type conversion automatically.
These @dfn{operand promotions} are the same as the argument promotions
except without converting @code{float} to @code{double}.  In other words,
the operand promotions convert

@itemize @bullet
@item
@code{char} or @code{short} (whether signed or not) to @code{int}.

@item
an array to a pointer to its zeroth element, and

@item
a function name to a pointer to that function.
@end itemize

@node Common Type
@section Common Type
@cindex common type

Arithmetic binary operators (except the shift operators) convert their
operands to the @dfn{common type} before operating on them.
Conditional expressions also convert the two possible results to their
common type.  Here are the rules for determining the common type.

If one of the numbers has a floating-point type and the other is an
integer, the common type is that floating-point type.  For instance,

@example
5.6 * 2   @result{} 11.2 /* @r{a @code{double} value} */
@end example

If both are floating point, the type with the larger range is the
common type.

If both are integers but of different widths, the common type
is the wider of the two.

If they are integer types of the same width, the common type is
unsigned if either operand is unsigned, and it's @code{long} if either
operand is @code{long}.  It's @code{long long} if either operand is
@code{long long}.

These rules apply to addition, subtraction, multiplication, division,
remainder, comparisons, and bitwise operations.  They also apply to
the two branches of a conditional expression, and to the arithmetic
done in a modifying assignment operation.

@node Scope
@chapter Scope
@cindex scope
@cindex block scope
@cindex function scope
@cindex function prototype scope

Each definition or declaration of an identifier is visible
in certain parts of the program, which is typically less than the whole
of the program.  The parts where it is visible are called its @dfn{scope}.

Normally, declarations made at the top-level in the source---that is,
not within any blocks and function definitions---are visible for the
entire contents of the source file after that point.  This is called
@dfn{file scope} (@pxref{File-Scope Variables}).

Declarations made within blocks of code, including within function
definitions, are visible only within those blocks.  This is called
@dfn{block scope}.  Here is an example:

@example
@group
void
foo (void)
@{
  int x = 42;
@}
@end group
@end example

@noindent
In this example, the variable @code{x} has block scope; it is visible
only within the @code{foo} function definition block.  Thus, other
blocks could have their own variables, also named @code{x}, without
any conflict between those variables.

A variable declared inside a subblock has a scope limited to
that subblock,

@example
@group
void
foo (void)
@{
  @{
    int x = 42;
  @}
  // @r{@code{x} is out of scope here.}
@}
@end group
@end example

If a variable declared within a block has the same name as a variable
declared outside of that block, the definition within the block
takes precedence during its scope:

@example
@group
int x = 42;

void
foo (void)
@{
  int x = 17;
  printf ("%d\n", x);
@}
@end group
@end example

@noindent
This prints 17, the value of the variable @code{x} declared in the
function body block, rather than the value of the variable @code{x} at
file scope.  We say that the inner declaration of @code{x}
@dfn{shadows} the outer declaration, for the extent of the inner
declaration's scope.

A declaration with block scope can be shadowed by another declaration
with the same name in a subblock.

@example
@group
void
foo (void)
@{
  char *x = "foo";
  @{
    int x = 42;
    @r{@dots{}}
    exit (x / 6);
  @}
@}
@end group
@end example

A function parameter's scope is the entire function body, but it can
be shadowed.  For example:

@example
@group
int x = 42;

void
foo (int x)
@{
  printf ("%d\n", x);
@}
@end group
@end example

@noindent
This prints the value of @code{x} the function parameter, rather than
the value of the file-scope variable @code{x}.

Labels (@pxref{goto Statement}) have @dfn{function} scope: each label
is visible for the whole of the containing function body, both before
and after the label declaration:

@example
@group
void
foo (void)
@{
  @r{@dots{}}
  goto bar;
  @r{@dots{}}
  @{  // @r{Subblock does not affect labels.}
    bar:
    @r{@dots{}}
  @}
  goto bar;
@}
@end group
@end example

Except for labels, a declared identifier is not
visible to code before its declaration.  For example:

@example
@group
int x = 5;
int y = x + 10;
@end group
@end example

@noindent
will work, but:

@example
@group
int x = y + 10;
int y = 5;
@end group
@end example

@noindent
cannot refer to the variable @code{y} before its declaration.

@include cpp.texi

@node    Integers in Depth
@chapter Integers in Depth

This chapter explains the machine-level details of integer types: how
they are represented as bits in memory, and the range of possible
values for each integer type.

@menu
* Integer Representations::      How integer values appear in memory.
* Maximum and Minimum Values::   Value ranges of integer types.
@end menu

@node Integer Representations
@section Integer Representations

@cindex integer representations
@cindex representation of integers

Modern computers store integer values as binary (base-2) numbers that
occupy a single unit of storage, typically either as an 8-bit
@code{char}, a 16-bit @code{short int}, a 32-bit @code{int}, or
possibly, a 64-bit @code{long long int}.  Whether a @code{long int} is
a 32-bit or a 64-bit value is system dependent.@footnote{In theory,
any of these types could have some other size, bit it's not worth even
a minute to cater to that possibility.  It never happens on
GNU/Linux.}

@cindex @code{CHAR_BIT}
The macro @code{CHAR_BIT}, defined in @file{limits.h}, gives the number
of bits in type @code{char}.  On any real operating system, the value
is 8.

The fixed sizes of numeric types necessarily limit their @dfn{range
of values}, and the particular encoding of integers decides what that
range is.

@cindex two's-complement representation
For unsigned integers, the entire space is used to represent a
nonnegative value.  Signed integers are stored using
@dfn{two's-complement representation}: a signed integer with @var{n}
bits has a range from @math{-2@sup{(@var{n} - 1)}} to @minus{}1 to 0
to 1 to @math{+2@sup{(@var{n} - 1)} - 1}, inclusive.  The leftmost, or
high-order, bit is called the @dfn{sign bit}.

In two's-complement representation, there is only one value that means
zero, and the most negative number lacks a positive counterpart.  As a
result, negating that number causes overflow; in practice, its result
is that number back again.  We will revisit that peculiarity shortly.

For example, a two's-complement signed 8-bit integer can represent all
decimal numbers from @minus{}128 to +127.  Negating @minus{}128 ought
to give +128, but that value won't fit in 8 bits, so the operation
yields @minus{}128.

Decades ago, there were computers that used other representations for
signed integers, but they are long gone and not worth any effort to
support.  The GNU C language does not support them.

@c ??? Is this duplicate?

When an arithmetic operation produces a value that is too big to
represent, the operation is said to @dfn{overflow}.  In C, integer
overflow does not interrupt the control flow or signal an error.
What it does depends on signedness.

For unsigned arithmetic, the result of an operation that overflows is
the @var{n} low-order bits of the correct value.  If the correct value
is representable in @var{n} bits, that is always the result;
thus we often say that ``integer arithmetic is exact,'' omitting the
crucial qualifying phrase ``as long as the exact result is
representable.''

In principle, a C program should be written so that overflow never
occurs for signed integers, but in GNU C you can specify various ways
of handling such overflow (@pxref{Integer Overflow}).

Integer representations are best understood by looking at a table for
a tiny integer size; here are the possible values for an integer with
three bits:

@multitable @columnfractions .25 .25 .25 .25
@headitem Unsigned @tab Signed @tab Bits @tab 2s Complement
@item 0 @tab 0 @tab 000 @tab 000 (0)
@item 1 @tab 1 @tab 001 @tab 111 (-1)
@item 2 @tab 2 @tab 010 @tab 110 (-2)
@item 3 @tab 3 @tab 011 @tab 101 (-3)
@item 4 @tab -4 @tab 100 @tab 100 (-4)
@item 5 @tab -3 @tab 101 @tab 011 (3)
@item 6 @tab -2 @tab 110 @tab 010 (2)
@item 7 @tab -1 @tab 111 @tab 001 (1)
@end multitable

The parenthesized decimal numbers in the last column represent the
signed meanings of the two's-complement of the line's value.  Recall
that, in two's-complement encoding, the high-order bit is 0 when
the number is nonnegative.

We can now understand the peculiar behavior of negation of the
most negative two's-complement integer: start with 0b100,
invert the bits to get 0b011, and add 1: we get
0b100, the value we started with.

We can also see overflow behavior in two's-complement:

@example
3 + 1 = 0b011 + 0b001 = 0b100 = (-4)
3 + 2 = 0b011 + 0b010 = 0b101 = (-3)
3 + 3 = 0b011 + 0b011 = 0b110 = (-2)
@end example

@noindent
A sum of two nonnegative signed values that overflows has a 1 in the
sign bit, so the exact positive result is truncated to a negative
value.

@c =====================================================================

@node Maximum and Minimum Values
@section Maximum and Minimum Values
@cindex maximum integer values
@cindex minimum integer values
@cindex integer ranges
@cindex ranges of integer types
@findex INT_MAX
@findex UINT_MAX
@findex SHRT_MAX
@findex LONG_MAX
@findex LLONG_MAX
@findex USHRT_MAX
@findex ULONG_MAX
@findex ULLONG_MAX
@findex CHAR_MAX
@findex SCHAR_MAX
@findex UCHAR_MAX

For each primitive integer type, there is a standard macro defined in
@file{limits.h} that gives the largest value that type can hold.  For
instance, for type @code{int}, the maximum value is @code{INT_MAX}.
On a 32-bit computer, that is equal to 2,147,483,647.  The
maximum value for @code{unsigned int} is @code{UINT_MAX}, which on a
32-bit computer is equal to 4,294,967,295.  Likewise, there are
@code{SHRT_MAX}, @code{LONG_MAX}, and @code{LLONG_MAX}, and
corresponding unsigned limits @code{USHRT_MAX}, @code{ULONG_MAX}, and
@code{ULLONG_MAX}.

Since there are three ways to specify a @code{char} type, there are
also three limits: @code{CHAR_MAX}, @code{SCHAR_MAX}, and
@code{UCHAR_MAX}.

@findex INT_MIN
For each type that is or might be signed, there is another symbol that
gives the minimum value it can hold.  (Just replace @code{MAX} with
@code{MIN} in the names listed above.)  There is no minimum limit
symbol for types specified with @code{unsigned} because the
minimum for them is universally zero.

@code{INT_MIN} is not the negative of @code{INT_MAX}.  In
two's-complement representation, the most negative number is 1 less
than the negative of the most positive number.  Thus, @code{INT_MIN}
on a 32-bit computer has the value @minus{}2,147,483,648.  You can't
actually write the value that way in C, since it would overflow.
That's a good reason to use @code{INT_MIN} to specify
that value.  Its definition is written to avoid overflow.

@include fp.texi

@node Compilation
@chapter Compilation
@cindex object file
@cindex compilation module
@cindex make rules
@cindex link

Early in the manual we explained how to compile a simple C program
that consists of a single source file (@pxref{Compile Example}).
However, we handle only short programs that way.  A typical C program
consists of many source files, each of which is usually a separate
@dfn{compilation module}---meaning that it has to be compiled
separately.  (The source files that are not separate compilation
modules are those that are used via @code{#include}; see @ref{Header
Files}.)

To compile a multi-module program, you compile each of the program's
compilation modules, making an @dfn{object file} for that module.  The
last step is to @dfn{link} the many object files together into a
single executable for the whole program.

For the full details of how to compile C programs (and other
languages' programs) with GCC, see @ref{Top,,, gcc, Using the GNU
Compiler Collection}.  On the Web, all is available through
@url{https://gcc.gnu.org/onlinedocs/}.  Here we give only a simple
introduction.

These commands compile two compilation modules, @file{foo.c} and
@file{bar.c}, running the compiler for each module:

@example
gcc -c -O -g foo.c
gcc -c -O -g bar.c
@end example

@noindent
In these commands, @option{-g} says to generate debugging information,
@option{-O} says to do some optimization, and @option{-c} says to put
the compiled code for that module into a corresponding object file and
go no further.  The object file for @file{foo.c} is automatically
called @file{foo.o}, and so on.

If you wish, you can specify the additional compilation options.  For
instance, @option{-Wformat -Wparenthesis -Wstrict-prototypes} request
additional warnings.

@cindex linking object files
After you compile all the program's modules, you link the object files
into a combined executable, like this:

@example
gcc -o foo foo.o bar.o
@end example

@noindent
In this command, @option{-o foo} species the file name for the
executable file, and the other arguments are the object files to link.
Always specify the executable file name in a command that generates
one.

One reason to divide a large program into multiple compilation modules
is to control how each module can access the internals of the others.
When a module declares a function or variable @code{extern}, other
modules can access it.  The other functions and variables defined in a
module can't be accessed from outside that module.

The other reason for using multiple modules is so that changing one
source file does not require recompiling all of them in order to try
the modified program.  It is sufficient to recompile the source file
that you changed, then link them all again.  Dividing a large program
into many substantial modules in this way typically makes
recompilation much faster.

Normally we don't run any of these commands directly.  Instead we
write a set of @dfn{make rules} for the program, then use the
@command{make} program to recompile only the source files that need to
be recompiled, by following those rules.  @xref{Top, The GNU Make
Manual, , make, The GNU Make Manual}.

@node Directing Compilation
@chapter Directing Compilation

This chapter describes C constructs that don't alter the program's
meaning @emph{as such}, but rather direct the compiler how to treat
some aspects of the program.

@menu
* Pragmas::                      Controlling compilation of some constructs.
* Static Assertions::            Compile-time tests for conditions.
@end menu

@node Pragmas
@section Pragmas

A @dfn{pragma} is an annotation in a program that gives direction to
the compiler.

@menu
* Pragma Basics::         Pragma syntax and usage.
* Severity Pragmas::      Settings for compile-time pragma output.
* Optimization Pragmas::  Controlling optimizations.
@end menu

@c See also @ref{Macro Pragmas}, which save and restore macro definitions.

@node Pragma Basics
@subsection Pragma Basics

C defines two syntactical forms for pragmas, the line form and the
token form.  You can write any pragma in either form, with the same
meaning.

The line form is a line in the source code, like this:

@example
#pragma @var{line}
@end example

@noindent
The line pragma has no effect on the parsing of the lines around it.
This form has the drawback that it can't be generated by a macro expansion.

The token form is a series of tokens; it can appear anywhere in the
program between the other tokens.

@example
_Pragma (@var{stringconstant})
@end example

@noindent
The pragma has no effect on the syntax of the tokens that surround it;
thus, here's a pragma in the middle of an @code{if} statement:

@example
if _Pragma ("hello") (x > 1)
@end example

@noindent
However, that's an unclear thing to do; for the sake of
understandability, it is better to put a pragma on a line by itself
and not embedded in the middle of another construct.

Both forms of pragma have a textual argument.  In a line pragma, the
text is the rest of the line.  The textual argument to @code{_Pragma}
uses the same syntax as a C string constant: surround the text with
two @samp{"} characters, and add a backslash before each @samp{"} or
@samp{\} character in it.

With either syntax, the textual argument specifies what to do.
It begins with one or several words that specify the operation.
If the compiler does not recognize them, it ignores the pragma.

Here are the pragma operations supported in GNU C@.

@c ??? Verify font for []
@table @code
@item #pragma GCC dependency "@var{file}" [@var{message}]
@itemx _Pragma ("GCC dependency \"@var{file}\" [@var{message}]")
Declares that the current source file depends on @var{file}, so GNU C
compares the file times and gives a warning if @var{file} is newer
than the current source file.

This directive searches for @var{file} the way @code{#include}
searches for a non-system header file.

If @var{message} is given, the warning message includes that text.

Examples:

@example
#pragma GCC dependency "parse.y"
_pragma ("GCC dependency \"/usr/include/time.h\" \
rerun fixincludes")
@end example

@item #pragma GCC poison @var{identifiers}
@itemx _Pragma ("GCC poison @var{identifiers}")
Poisons the identifiers listed in @var{identifiers}.

This is useful to make sure all mention of @var{identifiers} has been
deleted from the program and that no reference to them creeps back in.
If any of those identifiers appears anywhere in the source after the
directive, it causes a compilation error.  For example,

@example
#pragma GCC poison printf sprintf fprintf
sprintf(some_string, "hello");
@end example

@noindent
generates an error.

If a poisoned identifier appears as part of the expansion of a macro
that was defined before the identifier was poisoned, it will @emph{not}
cause an error.  Thus, system headers that define macros that use
the identifier will not cause errors.

For example,

@example
#define strrchr rindex
_Pragma ("GCC poison rindex")
strrchr(some_string, 'h');
@end example

@noindent
does not cause a compilation error.

@item #pragma GCC system_header
@itemx _Pragma ("GCC system_header")
Specify treating the rest of the current source file as if it came
from a system header file.  @xref{System Headers, System Headers,
System Headers, gcc, Using the GNU Compiler Collection}.

@item #pragma GCC warning @var{message}
@itemx _Pragma ("GCC warning @var{message}")
Equivalent to @code{#warning}.  Its advantage is that the
@code{_Pragma} form can be included in a macro definition.

@item #pragma GCC error @var{message}
@itemx _Pragma ("GCC error @var{message}")
Equivalent to @code{#error}.  Its advantage is that the
@code{_Pragma} form can be included in a macro definition.

@item #pragma message @var{message}
@itemx _Pragma ("message @var{message}")
Similar to @samp{GCC warning} and @samp{GCC error}, this simply prints an
informational message, and could be used to include additional warning
or error text without triggering more warnings or errors.  (Note that
unlike @samp{GCC warning} and @samp{GCC error}, @samp{message} does not include
@samp{GCC} as part of the pragma.)
@end table

@node Severity Pragmas
@subsection Severity Pragmas

These pragmas control the severity of classes of diagnostics.
You can specify the class of diagnostic with the GCC option that causes
those diagnostics to be generated.

@table @code
@item #pragma GCC diagnostic error @var{option}
@itemx _Pragma ("GCC diagnostic error @var{option}")
For code following this pragma, treat diagnostics of the variety
specified by @var{option} as errors.  For example:

@example
_Pragma ("GCC diagnostic error -Wformat")
@end example

@noindent
specifies to treat diagnostics enabled by the @var{-Wformat} option
as errors rather than warnings.

@item #pragma GCC diagnostic warning @var{option}
@itemx _Pragma ("GCC diagnostic warning @var{option}")
For code following this pragma, treat diagnostics of the variety
specified by @var{option} as warnings.  This overrides the
@var{-Werror} option which says to treat warnings as errors.

@item #pragma GCC diagnostic ignore @var{option}
@itemx _Pragma ("GCC diagnostic ignore @var{option}")
For code following this pragma, refrain from reporting any diagnostics
of the variety specified by @var{option}.

@item #pragma GCC diagnostic push
@itemx _Pragma ("GCC diagnostic push")
@itemx #pragma GCC diagnostic pop
@itemx _Pragma ("GCC diagnostic pop")
These pragmas maintain a stack of states for severity settings.
@samp{GCC diagnostic push} saves the current settings on the stack,
and @samp{GCC diagnostic pop} pops the last stack item and restores
the current settings from that.

@samp{GCC diagnostic pop} when the severity setting stack is empty
restores the settings to what they were at the start of compilation.

Here is an example:

@example
_Pragma ("GCC diagnostic error -Wformat")

/* @r{@option{-Wformat} messages treated as errors. } */

_Pragma ("GCC diagnostic push")
_Pragma ("GCC diagnostic warning -Wformat")

/* @r{@option{-Wformat} messages treated as warnings. } */

_Pragma ("GCC diagnostic push")
_Pragma ("GCC diagnostic ignored -Wformat")

/* @r{@option{-Wformat} messages suppressed. } */

_Pragma ("GCC diagnostic pop")

/* @r{@option{-Wformat} messages treated as warnings again. } */

_Pragma ("GCC diagnostic pop")

/* @r{@option{-Wformat} messages treated as errors again. } */

/* @r{This is an excess @samp{pop} that matches no @samp{push}. } */
_Pragma ("GCC diagnostic pop")

/* @r{@option{-Wformat} messages treated once again}
   @r{as specified by the GCC command-line options.}  */
@end example
@end table

@node Optimization Pragmas
@subsection Optimization Pragmas

These pragmas enable a particular optimization for specific function
definitions.  The settings take effect at the end of a function
definition, so the clean place to use these pragmas is between
function definitions.

@table @code
@item #pragma GCC optimize @var{optimization}
@itemx _Pragma ("GCC optimize @var{optimization}")
These pragmas enable the optimization @var{optimization} for the
following functions.  For example,

@example
_Pragma ("GCC optimize -fforward-propagate")
@end example

@noindent
says to apply the @samp{forward-propagate} optimization to all
following function definitions.  Specifying optimizations for
individual functions, rather than for the entire program, is rare but
can be useful for getting around a bug in the compiler.

If @var{optimization} does not correspond to a defined optimization
option, the pragma is erroneous.  To turn off an optimization, use the
corresponding @samp{-fno-} option, such as
@samp{-fno-forward-propagate}.

@item #pragma GCC target @var{optimizations}
@itemx _Pragma ("GCC target @var{optimizations}")
The pragma @samp{GCC target} is similar to @samp{GCC optimize} but is
used for platform-specific optimizations.  Thus,

@example
_Pragma ("GCC target popcnt")
@end example

@noindent
activates the optimization @samp{popcnt} for all
following function definitions.  This optimization is supported
on a few common targets but not on others.

@item #pragma GCC push_options
@itemx _Pragma ("GCC push_options")
The @samp{push_options} pragma saves on a stack the current settings
specified with the @samp{target} and @samp{optimize} pragmas.

@item #pragma GCC pop_options
@itemx _Pragma ("GCC pop_options")
The @samp{pop_options} pragma pops saved settings from that stack.

Here's an example of using this stack.

@example
_Pragma ("GCC push_options")
_Pragma ("GCC optimize forward-propagate")

/* @r{Functions to compile}
   @r{with the @code{forward-propagate} optimization.} */

_Pragma ("GCC pop_options")
/* @r{Ends enablement of @code{forward-propagate}.} */
@end example

@item #pragma GCC reset_options
@itemx _Pragma ("GCC reset_options")
Clears all pragma-defined @samp{target} and @samp{optimize}
optimization settings.
@end table

@node Static Assertions
@section Static Assertions
@cindex static assertions
@findex _Static_assert

You can add compiler-time tests for necessary conditions into your
code using @code{_Static_assert}.  This can be useful, for example, to
check that the compilation target platform supports the type sizes
that the code expects.  For example,

@example
_Static_assert ((sizeof (long int) >= 8),
    "long int needs to be at least 8 bytes");
@end example

@noindent
reports a compile-time error if compiled on a system with long
integers smaller than 8 bytes, with @samp{long int needs to be at
least 8 bytes} as the error message.

Since calls @code{_Static_assert} are processed at compile time, the
expression must be computable at compile time and the error message
must be a literal string.  The expression can refer to the sizes of
variables, but can't refer to their values.  For example, the
following static assertion is invalid for two reasons:

@example
char *error_message
  = "long int needs to be at least 8 bytes";
int size_of_long_int = sizeof (long int);

_Static_assert (size_of_long_int == 8, error_message);
@end example

@noindent
The expression @code{size_of_long_int == 8} isn't computable at
compile time, and the error message isn't a literal string.

You can, though, use preprocessor definition values with
@code{_Static_assert}:

@example
#define LONG_INT_ERROR_MESSAGE "long int needs to be \
at least 8 bytes"

_Static_assert ((sizeof (long int) == 8),
  LONG_INT_ERROR_MESSAGE);
@end example

Static assertions are permitted wherever a statement or declaration is
permitted, including at top level in the file, and also inside the
definition of a type.

@strong{Note:} The @code{==} used instead of @code{>=} probably makes
the program wrong but not invalid.

@example
union y
@{
  int i;
  int *ptr;
  _Static_assert (sizeof (int *) == sizeof (int),
		  "Pointer and int not same size");
@};
@end example

@node Type Alignment
@appendix Type Alignment
@cindex type alignment
@cindex alignment of type
@findex _Alignof
@findex __alignof__

Code for device drivers and other communication with low-level
hardware sometimes needs to be concerned with the alignment of
data objects in memory.

Each data type has a required @dfn{alignment}, always a power of 2,
that says at which memory addresses an object of that type can validly
start.  A valid address for the type must be a multiple of its
alignment.  If a type's alignment is 1, that means it can validly
start at any address.  If a type's alignment is 2, that means it can
only start at an even address.  If a type's alignment is 4, that means
it can only start at an address that is a multiple of 4.

The alignment of a type (except @code{char}) can vary depending on the
kind of computer in use.  To refer to the alignment of a type in a C
program, use @code{_Alignof}, whose syntax parallels that of
@code{sizeof}.  Like @code{sizeof}, @code{_Alignof} is a compile-time
operation, and it doesn't compute the value of the expression used
as its argument.

Nominally, each integer and floating-point type has an alignment equal to
the largest power of 2 that divides its size.  Thus, @code{int} with
size 4 has a nominal alignment of 4, and @code{long long int} with
size 8 has a nominal alignment of 8.

However, each kind of computer generally has a maximum alignment, and
no type needs more alignment than that.  If the computer's maximum
alignment is 4 (which is common), then no type's alignment is more
than 4.

The size of any type is always a multiple of its alignment; that way,
in an array whose elements have that type, all the elements are
properly aligned if the first one is.

These rules apply to all real computers today, but some embedded
controllers have odd exceptions.  We don't have references to cite for
them.
@c We can't cite a nonfree manual as documentation.

Ordinary C code guarantees that every object of a given type is in
fact aligned as that type requires.

If the operand of @code{_Alignof} is a structure field, the value
is the alignment it requires.  It may have a greater alignment by
coincidence, due to the other fields, but @code{_Alignof} is not
concerned about that.  @xref{Structures}.

Older versions of GNU C used the keyword @code{__alignof__} for this,
but now that the feature has been standardized, it is better
to use the standard keyword @code{_Alignof}.

@findex _Alignas
@findex __aligned__
You can explicitly specify an alignment requirement for a particular
variable or structure field by adding @code{_Alignas
(@var{alignment})} to the declaration, where @var{alignment} is a
power of 2 or a type name.  For instance:

@example
char _Alignas (8) x;
@end example

@noindent
or

@example
char _Alignas (double) x;
@end example

@noindent
specifies that @code{x} must start on an address that is a multiple of
8.  However, if @var{alignment} exceeds the maximum alignment for the
machine, that maximum is how much alignment @code{x} will get.

The older GNU C syntax for this feature looked like
@code{__attribute__ ((__aligned__ (@var{alignment})))} to the
declaration, and was added after the variable.  For instance:

@example
char x __attribute__ ((__aligned__ 8));
@end example

@xref{Attributes}.

@node Aliasing
@appendix Aliasing
@cindex aliasing (of storage)
@cindex pointer type conversion
@cindex type conversion, pointer

We have already presented examples of casting a @code{void *} pointer
to another pointer type, and casting another pointer type to
@code{void *}.

One common kind of pointer cast is guaranteed safe: casting the value
returned by @code{malloc} and related functions (@pxref{Dynamic Memory
Allocation}).  It is safe because these functions do not save the
pointer anywhere else; the only way the program will access the newly
allocated memory is via the pointer just returned.

In fact, C allows casting any pointer type to any other pointer type.
Using this to access the same place in memory using two
different data types is called @dfn{aliasing}.

Aliasing is necessary in some programs that do sophisticated memory
management, such as GNU Emacs, but most C programs don't need to do
aliasing.  When it isn't needed, @strong{stay away from it!}  To do
aliasing correctly requires following the rules stated below.
Otherwise, the aliasing may result in malfunctions when the program
runs.

The rest of this appendix explains the pitfalls and rules of aliasing.

@menu
* Aliasing Alignment::   Memory alignment considerations for
                           casting between pointer types.
* Aliasing Length::      Type size considerations for
                           casting between pointer types.
* Aliasing Type Rules::  Even when type alignment and size matches,
                           aliasing can still have surprising results.

@end menu

@node Aliasing Alignment
@appendixsection Aliasing and Alignment

In order for a type-converted pointer to be valid, it must have the
alignment that the new pointer type requires.  For instance, on most
computers, @code{int} has alignment 4; the address of an @code{int}
must be a multiple of 4.  However, @code{char} has alignment 1, so the
address of a @code{char} is usually not a multiple of 4.  Taking the
address of such a @code{char} and casting it to @code{int *} probably
results in an invalid pointer.  Trying to dereference it may cause a
@code{SIGBUS} signal, depending on the platform in use (@pxref{Signals}).

@example
foo ()
@{
  char i[4];
  int *p = (int *) &i[1]; /* @r{Misaligned pointer!} */
  return *p;              /* @r{Crash!} */
@}
@end example

This requirement is never a problem when casting the return value
of @code{malloc} because that function always returns a pointer
with as much alignment as any type can require.

@node Aliasing Length
@appendixsection Aliasing and Length

When converting a pointer to a different pointer type, make sure the
object it really points to is at least as long as the target of the
converted pointer.  For instance, suppose @code{p} has type @code{int
*} and it's cast as follows:

@example
int *p;

struct
  @{
    double d, e, f;
  @} foo;

struct foo *q = (struct foo *)p;

q->f = 5.14159;
@end example

@noindent
the value @code{q->f} will run past the end of the @code{int} that
@code{p} points to.  If @code{p} was initialized to the start of an
array of type @code{int[6]}, the object is long enough for three
@code{double}s.  But if @code{p} points to something shorter,
@code{q->f} will run on beyond the end of that, overlaying some other
data.  Storing that will garble that other data.  Or it could extend
past the end of memory space and cause a @code{SIGSEGV} signal
(@pxref{Signals}).

@node Aliasing Type Rules
@appendixsection Type Rules for Aliasing

C code that converts a pointer to a different pointer type can use the
pointers to access the same memory locations with two different data
types.  If the same address is accessed with different types in a
single control thread, optimization can make the code do surprising
things (in effect, make it malfunction).

Here's a concrete example where aliasing that can change the code's
behavior when it is optimized.  We assume that @code{float} is 4 bytes
long, like @code{int}, and so is every pointer.  Thus, the structures
@code{struct a} and @code{struct b} are both 8 bytes.

@example
#include <stdio.h>
struct a @{ int size; char *data; @};
struct b @{ float size; char *data; @};

void sub (struct a *p, struct b *q)
@{
  int x;
  p->size = 0;
  q->size = 1;
  x = p->size;
  printf("x       =%d\n", x);
  printf("p->size =%d\n", (int)p->size);
  printf("q->size =%d\n", (int)q->size);
@}

int main(void)
@{
  struct a foo;
  struct a *p = &foo;
  struct b *q = (struct b *) &foo;

  sub (p, q);
@}
@end example

This code works as intended when compiled without optimization.  All
the operations are carried out sequentially as written.  The code
sets @code{x} to @code{p->size}, but what it actually gets is the
bits of the floating point number 1, as type @code{int}.

However, when optimizing, the compiler is allowed to assume
(mistakenly, here) that @code{q} does not point to the same storage as
@code{p}, because their data types are not allowed to alias.

From this assumption, the compiler can deduce (falsely, here) that the
assignment into @code{q->size} has no effect on the value of
@code{p->size}, which must therefore still be 0.  Thus, @code{x} will
be set to 0.

GNU C, following the C standard, @emph{defines} this optimization as
legitimate.  Code that misbehaves when optimized following these rules
is, by definition, incorrect C code.

The rules for storage aliasing in C are based on the two data types:
the type of the object, and the type it is accessed through.  The
rules permit accessing part of a storage object of type @var{t} using
only these types:

@itemize @bullet
@item
@var{t}.

@item
A type compatible with @var{t}.  @xref{Compatible Types}.

@item
A signed or unsigned version of one of the above.

@item
A qualified version of one of the above.
@xref{Type Qualifiers}.

@item
An array, structure (@pxref{Structures}), or union type
(@code{Unions}) that contains one of the above, either directly as a
field or through multiple levels of fields.  If @var{t} is
@code{double}, this would include @code{struct s @{ union @{ double
d[2]; int i[4]; @} u; int i; @};} because there's a @code{double}
inside it somewhere.
@c For structures, shouldn't it be the first field?

@item
A character type.
@end itemize

What do these rules say about the example in this subsection?

For @code{foo.size} (equivalently, @code{a->size}), @var{t} is
@code{int}.  The type @code{float} is not allowed as an aliasing type
by those rules, so @code{b->size} is not supposed to alias with
elements of @code{a}.  Based on that assumption, GNU C makes a
permitted optimization that was not, in this case, consistent with
what the programmer intended the program to do.

Whether GCC actually performs type-based aliasing analysis depends on
the details of the code.  GCC has other ways to determine (in some cases)
whether objects alias, and if it gets a reliable answer that way, it won't
fall back on type-based heuristics.

@c @opindex -fno-strict-aliasing
The importance of knowing the type-based aliasing rules is not so as
to ensure that the optimization is done where it would be safe, but so
as to ensure it is @emph{not} done in a way that would break the
program.  You can turn off type-based aliasing analysis by giving GCC
the option @option{-fno-strict-aliasing}.

@node Digraphs
@appendix Digraphs
@cindex digraphs

C accepts aliases for certain characters.  Apparently in the 1990s
some computer systems had trouble inputting these characters, or
trouble displaying them.  These digraphs almost never appear in C
programs nowadays, but we mention them for completeness.

@table @samp
@item <:
An alias for @samp{[}.
@item :>
An alias for @samp{]}.
@item <%
An alias for @samp{@{}.
@item %>
An alias for @samp{@}}.
@item %:
An alias for @samp{#},
used for preprocessing directives (@pxref{Directives}) and
macros (@pxref{Macros}).
@end table

@node Attributes
@appendix Attributes in Declarations
@cindex attributes
@findex __attribute__

You can specify certain additional requirements in a declaration, to
get fine-grained control over code generation, and helpful
informational messages during compilation.  We use a few attributes in
code examples throughout this manual, including

@table @code
@item aligned
The @code{aligned} attribute specifies a minimum alignment for a
variable or structure field, measured in bytes:

@example
int foo __attribute__ ((aligned (8))) = 0;
@end example

@noindent
This directs GNU C to allocate @code{foo} at an address that is a
multiple of 8 bytes.  However, you can't force an alignment bigger
than the computer's maximum meaningful alignment.

@item packed
The @code{packed} attribute specifies to compact the fields of a
structure by not leaving gaps between fields.  For example,

@example
struct __attribute__ ((packed)) bar
@{
  char a;
  int b;
@};
@end example

@noindent
allocates the integer field @code{b} at byte 1 in the structure,
immediately after the character field @code{a}.  The packed structure
is just 5 bytes long (assuming @code{int} is 4 bytes) and its
alignment is 1, that of @code{char}.

@item deprecated
Applicable to both variables and functions, the @code{deprecated}
attribute tells the compiler to issue a warning if the variable or
function is ever used in the source file.

@example
int old_foo __attribute__ ((deprecated));

int old_quux () __attribute__ ((deprecated));
@end example

@item __noinline__
The @code{__noinline__} attribute, in a function's declaration or
definition, specifies never to inline calls to that function.  All
calls to that function, in a compilation unit where it has this
attribute, will be compiled to invoke the separately compiled
function.  @xref{Inline Function Definitions}.

@item __noclone__
The @code{__noclone__} attribute, in a function's declaration or
definition, specifies never to clone that function.  Thus, there will
be only one compiled version of the function.  @xref{Label Value
Caveats}, for more information about cloning.

@item always_inline
The @code{always_inline} attribute, in a function's declaration or
definition, specifies to inline all calls to that function (unless
something about the function makes inlining impossible).  This applies
to all calls to that function in a compilation unit where it has this
attribute.  @xref{Inline Function Definitions}.

@item gnu_inline
The @code{gnu_inline} attribute, in a function's declaration or
definition, specifies to handle the @code{inline} keyword the way GNU
C originally implemented it, many years before ISO C said anything
about inlining.  @xref{Inline Function Definitions}.
@end table

For full documentation of attributes, see the GCC manual.
@xref{Attribute Syntax, Attribute Syntax, System Headers, gcc, Using
the GNU Compiler Collection}.

@node Signals
@appendix Signals
@cindex signal
@cindex handler (for signal)
@cindex @code{SIGSEGV}
@cindex @code{SIGFPE}
@cindex @code{SIGBUS}

Some program operations bring about an error condition called a
@dfn{signal}.  These signals terminate the program, by default.

There are various different kinds of signals, each with a name.  We
have seen several such error conditions through this manual:

@table @code
@item SIGSEGV
This signal is generated when a program tries to read or write outside
the memory that is allocated for it, or to write memory that can only
be read.  The name is an abbreviation for ``segmentation violation''.

@item SIGFPE
This signal indicates a fatal arithmetic error.  The name is an
abbreviation for ``floating-point exception'', but covers all types of
arithmetic errors, including division by zero and overflow.

@item SIGBUS
This signal is generated when an invalid pointer is dereferenced,
typically the result of dereferencing an uninitialized pointer.  It is
similar to @code{SIGSEGV}, except that @code{SIGSEGV} indicates
invalid access to valid memory, while @code{SIGBUS} indicates an
attempt to access an invalid address.
@end table

These kinds of signal allow the program to specify a function as a
@dfn{signal handler}.  When a signal has a handler, it doesn't
terminate the program; instead it calls the handler.

There are many other kinds of signal; here we list only those that
come from run-time errors in C operations.  The rest have to do with
the functioning of the operating system.  The GNU C Library Reference
Manual gives more explanation about signals (@pxref{Program Signal
Handling, The GNU C Library, , libc, The GNU C Library Reference
Manual}).

@node GNU Free Documentation License
@appendix GNU Free Documentation License

@include fdl.texi

@node GNU General Public License
@appendix GNU General Public License

@include gpl.texi

@node Symbol Index
@unnumbered Index of Symbols and Keywords

@printindex fn

@node Concept Index
@unnumbered Concept Index

@printindex cp

@bye