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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<!--Converted with LaTeX2HTML 99.2beta8 (1.46)
original version by: Nikos Drakos, CBLU, University of Leeds
* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>22. Trivial Introduction to C</TITLE>
<META NAME="description" CONTENT="22. Trivial Introduction to C">
<META NAME="keywords" CONTENT="rute">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="LaTeX2HTML v99.2beta8">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
<LINK REL="STYLESHEET" HREF="rute.css">
<LINK REL="next" HREF="node26.html">
<LINK REL="previous" HREF="node24.html">
<LINK REL="up" HREF="rute.html">
<LINK REL="next" HREF="node26.html">
</HEAD>
<BODY BGCOLOR=#FFFFFF >
<TABLE width="100%" border="0" cellspacing="0" cellpadding="0">
<TR><TD align=left bgcolor="#000000">
<FONT COLOR=white>
<A HREF="http://www.icon.co.za/~psheer/rute-purchase.html"><FONT COLOR=white>Purchase</FONT></A>
</FONT>
</TD><TD align=center bgcolor="#000000">
<FONT COLOR=white>
Copyright © 2002 Paul Sheer. <A HREF="copying.html"><FONT COLOR=white>Click here for copying permissions.</FONT></A>
</FONT>
</TD><TD align=right bgcolor="#000000">
<FONT COLOR=white>
<A HREF="http://www.icon.co.za/~psheer/rute-home.html"><FONT COLOR=white>Home</FONT></A>
</FONT>
</TD></TR>
<TR><TD colspan=2 align=left bgcolor="#ECEBF4">
<IMG SRC="va-btn-small-light-60.png">
</TD><TD align=right bgcolor="#ECEBF4">
<IMG SRC="sflogo2-steel-60.png">
</TD></TR>
</TABLE><BR>
<!--Navigation Panel-->
<A NAME="tex2html2197"
HREF="node26.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A>
<A NAME="tex2html2193"
HREF="rute.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A>
<A NAME="tex2html2187"
HREF="node24.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
<A NAME="tex2html2195"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html2198"
HREF="node26.html">23. Shared Libraries</A>
<B> Up:</B> <A NAME="tex2html2194"
HREF="rute.html">rute</A>
<B> Previous:</B> <A NAME="tex2html2188"
HREF="node24.html">21. System Services and</A>
  <B> <A NAME="tex2html2196"
HREF="node1.html">Contents</A></B>
<BR>
<BR>
<!--End of Navigation Panel-->
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
<UL>
<LI><A NAME="tex2html2199"
HREF="#SECTION002510000000000000000">22.1 <B>C</B> Fundamentals</A>
<UL>
<LI><A NAME="tex2html2200"
HREF="#SECTION002511000000000000000">22.1.1 The simplest <B>C</B> program</A>
<LI><A NAME="tex2html2201"
HREF="#SECTION002512000000000000000">22.1.2 Variables and types</A>
<LI><A NAME="tex2html2202"
HREF="#SECTION002513000000000000000">22.1.3 Functions</A>
<LI><A NAME="tex2html2203"
HREF="#SECTION002514000000000000000">22.1.4 <TT>
<FONT COLOR="#0000ff">for</FONT></TT>, <TT>
<FONT COLOR="#0000ff">while</FONT></TT>, <TT>
<FONT COLOR="#0000ff">if</FONT></TT>, and <TT>
<FONT COLOR="#0000ff">switch</FONT></TT> statements</A>
<LI><A NAME="tex2html2204"
HREF="#SECTION002515000000000000000">22.1.5 Strings, arrays, and memory allocation</A>
<LI><A NAME="tex2html2205"
HREF="#SECTION002516000000000000000">22.1.6 String operations</A>
<LI><A NAME="tex2html2206"
HREF="#SECTION002517000000000000000">22.1.7 File operations</A>
<LI><A NAME="tex2html2207"
HREF="#SECTION002518000000000000000">22.1.8 Reading command-line arguments inside <B>C</B> programs</A>
<LI><A NAME="tex2html2208"
HREF="#SECTION002519000000000000000">22.1.9 A more complicated example</A>
<LI><A NAME="tex2html2209"
HREF="#SECTION0025110000000000000000">22.1.10 <TT>
<FONT COLOR="#0000ff">#include</FONT></TT> statements and prototypes</A>
<LI><A NAME="tex2html2210"
HREF="#SECTION0025111000000000000000">22.1.11 <B>C</B> comments</A>
<LI><A NAME="tex2html2211"
HREF="#SECTION0025112000000000000000">22.1.12 <TT>
<FONT COLOR="#0000ff">#define</FONT></TT> and <TT>
<FONT COLOR="#0000ff">#if</FONT></TT> -- <B>C</B> macros</A>
</UL>
<LI><A NAME="tex2html2212"
HREF="#SECTION002520000000000000000">22.2 Debugging with <TT>
<FONT COLOR="#0000ff">gdb</FONT></TT> and <TT>
<FONT COLOR="#0000ff">strace</FONT></TT></A>
<UL>
<LI><A NAME="tex2html2213"
HREF="#SECTION002521000000000000000">22.2.1 <TT>
<FONT COLOR="#0000ff">gdb</FONT></TT></A>
<LI><A NAME="tex2html2214"
HREF="#SECTION002522000000000000000">22.2.2 Examining <TT>
<FONT COLOR="#0000ff">core</FONT></TT> files</A>
<LI><A NAME="tex2html2215"
HREF="#SECTION002523000000000000000">22.2.3 <TT>
<FONT COLOR="#0000ff">strace</FONT></TT></A>
</UL>
<LI><A NAME="tex2html2216"
HREF="#SECTION002530000000000000000">22.3 <B>C</B> Libraries</A>
<LI><A NAME="tex2html2217"
HREF="#SECTION002540000000000000000">22.4 <B>C</B> Projects -- <TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT>s</A>
<UL>
<LI><A NAME="tex2html2218"
HREF="#SECTION002541000000000000000">22.4.1 Completing our example <TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT></A>
<LI><A NAME="tex2html2219"
HREF="#SECTION002542000000000000000">22.4.2 Putting it all together</A>
</UL></UL>
<!--End of Table of Child-Links-->
<HR>
<H1><A NAME="SECTION002500000000000000000">
22. Trivial Introduction to <B>C</B></A>
</H1>
<P>
<A NAME="chap:trivintroc"></A>
<P>
<B>C</B> was invented for the purpose
of writing an operating system that
could be recompiled (ported)
to different hardware platforms (different CPUs).
Because the operating system is written in <B>C</B>, this
language is
the first choice for writing any kind of application that has to
communicate efficiently with the operating system.
<P>
Many people who don't program very well in <B>C</B> think of <B>C</B> as
an arbitrary language out of many. This point should be made at
once: <B>C</B> is the fundamental basis of all computing in the world
today. U<SMALL>NIX</SMALL>, Microsoft Windows, office suites, web browsers
and device drivers are all written in <B>C</B>. Ninety-nine percent of your time spent
at a computer is probably spent using an application written
in <B>C</B>. About 70% of all ``open source'' software is written in <B>C</B>,
and the remaining 30% written in languages whose
compilers or interpreters
are written in <B>C</B>. <FONT COLOR="#ffa500">[C++ is also quite popular. It is, however, not
as fundamental to computing, although it is more suitable in
many situations.]</FONT>
<P>
Further, there is no replacement for <B>C</B>. Since it fulfills its purpose
almost flawlessly, there will never be a need to replace it.
<I>Other languages may fulfill other purposes, but <B>C</B> fulfills
its purpose most adequately.</I> For instance, all future operating
systems will probably be written in <B>C</B> for a long time to come.
<P>
It is for these reasons that your knowledge of U<SMALL>NIX</SMALL> will never be
complete until you can program in <B>C</B>. On the other hand, just because
you can program in <B>C</B> does not mean that you <I>should</I>. Good <B>C</B>
programming is a fine art which many veteran <B>C</B> programmers
never manage to master, even after many years. <I>It is essential
to join a Free software project to properly master an effective style
of <B>C</B> development.</I>
<P>
<H1><A NAME="SECTION002510000000000000000">
22.1 <B>C</B> Fundamentals</A>
</H1>
<P>
We start with a simple <B>C</B> program
and then add fundamental elements to it.
Before going too far, you may wish to review <TT>
<FONT COLOR="#0000ff">bash</FONT></TT>
functions in Section <A HREF="node10.html#sec:functions">7.7</A>.
<P>
<H2><A NAME="SECTION002511000000000000000">
22.1.1 The simplest <B>C</B> program</A>
</H2>
<P>
A simple <B>C</B> program is:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> printf ("Hello World!\n");</code><br>
<code> return 3;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Save this program in a file <TT>
<FONT COLOR="#0000ff">hello.c</FONT></TT>. We will now
compile the program. <FONT COLOR="#ffa500">[<I>Compiling</I> is the process of turning <B>C</B>
code into <I>assembler instructions</I>. Assembler instructions are
the program code that your 80<I>?</I>86/SPARC/RS6000 CPU
understands directly. The resulting binary executable is fast because
it is executed natively by
your processor--it is the very chip that
you see on your motherboard that does fetch <TT>
<FONT COLOR="#0000ff">Hello</FONT></TT> byte for byte
from memory and executes each instruction. This is what is meant by
<I>million instructions per second</I> (MIPS). The
<I>megahertz</I> of the machine quoted by hardware vendors is
<I>very</I> roughly the number of MIPS. Interpreted languages (like
shell scripts) are much slower because the code itself is written in
something not understandable to the CPU. The <TT>
<FONT COLOR="#0000ff">/bin/bash</FONT></TT>
program has to <I>interpret</I> the shell program. <TT>
<FONT COLOR="#0000ff">/bin/bash</FONT></TT>
itself is written in <B>C</B>, but the overhead of interpretation makes
scripting languages many orders of magnitude slower than compiled
languages. Shell scripts do not need to be compiled.]</FONT>Run the command
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>gcc -Wall -o hello hello.c</code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">-o hello</FONT></TT> option tells
<TT>
<FONT COLOR="#0000ff">gcc</FONT></TT> <FONT COLOR="#ffa500">[GNU <B>C</B> Compiler.
<TT>
<FONT COLOR="#0000ff">cc</FONT></TT> on other U<SMALL>NIX</SMALL>
systems.]</FONT> to produce the binary file <TT>
<FONT COLOR="#0000ff">hello</FONT></TT> instead of the
default binary file named
<TT>
<FONT COLOR="#0000ff">a.out</FONT></TT>. <FONT COLOR="#ffa500">[Called <TT>
<FONT COLOR="#0000ff">a.out</FONT></TT> for historical reasons.]</FONT>The <TT>
<FONT COLOR="#0000ff">-Wall</FONT></TT> option means to report <TT>
<FONT COLOR="#0000ff">all</FONT></TT>
<TT>
<FONT COLOR="#0000ff">W</FONT></TT>arnings during the compilation. This is not
strictly necessary but is most helpful
for correcting possible errors in your programs. More compiler options
are discussed on page <A HREF="node27.html#page:compileroptions"><IMG ALIGN="BOTTOM" BORDER="1" ALT="[*]" SRC="crossref.png"></A>.
<P>
Then, run the program with
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>./hello</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Previously you should have familiarized yourself with
<TT>
<FONT COLOR="#0000ff">bash</FONT></TT> functions. In
<B>C</B> <I>all</I> code is inside a function.
The first function to be called (by the operating system) is the
<TT>
<FONT COLOR="#0000ff">main</FONT></TT> function.
<P>
Type <TT>
<FONT COLOR="#0000ff">echo $?</FONT></TT> to see the return
code of the program. You will
see it is <TT>
<FONT COLOR="#0000ff">3</FONT></TT>, the return value of the <TT>
<FONT COLOR="#0000ff">main</FONT></TT>
function.
<P>
Other things to note are the <TT>
<FONT COLOR="#0000ff">"</FONT></TT> on either side of the string
to be printed. Quotes are required around string literals. Inside
a string literal, the <TT>
<FONT COLOR="#0000ff">\n</FONT></TT> <I>escape sequence</I>
indicates a newline character. <TT>
<FONT COLOR="#0000ff">ascii</FONT></TT>(7) shows some
other escape sequences. You can also see a proliferation of
<TT>
<FONT COLOR="#0000ff">;</FONT></TT> everywhere in a <B>C</B> program. Every statement in <B>C</B> is
terminated by a <TT>
<FONT COLOR="#0000ff">;</FONT></TT> unlike statements in shell scripts where
a <TT>
<FONT COLOR="#0000ff">;</FONT></TT> is optional.
<P>
Now try:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> printf ("number %d, number %d\n", 1 + 2, 10);</code><br>
<code> exit (3);</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
<TT>
<FONT COLOR="#0000ff">printf</FONT></TT> can be thought of as the command to send output
to the terminal. It is also what is known as a <I>standard <B>C</B>
library function</I>. In other words, it is specified that a <B>C</B>
implementation should always have the <TT>
<FONT COLOR="#0000ff">printf</FONT></TT> function
and that it should behave in a certain way.
<P>
The <TT>
<FONT COLOR="#0000ff">%d</FONT></TT> specifies that a <TT>
<FONT COLOR="#0000ff">d</FONT></TT><I>ecimal</I> should
go in at that point in the text. The number to be substituted
will be the first <I>argument</I> to the <TT>
<FONT COLOR="#0000ff">printf</FONT></TT> function
after the string literal--that is, the <TT>
<FONT COLOR="#0000ff">1 + 2</FONT></TT>. The
next <TT>
<FONT COLOR="#0000ff">%d</FONT></TT> is substituted with the second argument--that is,
the <TT>
<FONT COLOR="#0000ff">10</FONT></TT>. The <TT>
<FONT COLOR="#0000ff">%d</FONT></TT> is known as a <I>format
specifier</I>. It essentially <I>converts</I> an integer number into
a decimal representation. See <TT>
<FONT COLOR="#0000ff">printf</FONT></TT>(3) for more details.
<P>
<H2><A NAME="SECTION002512000000000000000">
22.1.2 Variables and types</A>
</H2>
<P>
With <TT>
<FONT COLOR="#0000ff">bash</FONT></TT>, you could use a variable anywhere, anytime, and the
variable would just be blank if it had never been assigned a value. In <B>C</B>,
however, you have to explicitly tell the compiler what variables you are
going to need before each block of code. You do this with a variable declaration:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> int x;</code><br>
<code> int y;</code><br>
<code> x = 10;</code><br>
<code> y = 2:</code><br>
<code> printf ("number %d, number %d\n", 1 + y, x);</code><br>
<code> exit (3);</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">int x</FONT></TT> is a variable declaration. It tells the
program to reserve space for one <TT>
<FONT COLOR="#0000ff">int</FONT></TT><I>eger</I>
variable
that it will later refer to as <TT>
<FONT COLOR="#0000ff">x</FONT></TT>. <TT>
<FONT COLOR="#0000ff">int</FONT></TT> is the
<I>type</I> of the variable. <TT>
<FONT COLOR="#0000ff">x = 10</FONT></TT> assigned a value of 10
to the variable. There are types for each kind of number
you would like to work with, and format specifiers to convert them
for printing:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> char a;</code><br>
<code> short b;</code><br>
<code> int c;</code><br>
<code> long d;</code><br>
<code> float e;</code><br>
<code> double f;</code><br>
<code> long double g;</code><br>
<code> a = 'A';</code><br>
<code> b = 10;</code><br>
<code> c = 10000000;</code><br>
<code> d = 10000000;</code><br>
<code> e = 3.14159;</code><br>
<code> f = 10e300;</code><br>
<code> g = 10e300;</code><br>
<code> printf ("%c, %hd, %d, %ld, %f, %f, %Lf\n", a, b, c, d, e, f, g);</code><br>
<code> exit (3);</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
<P>
You will notice that <TT>
<FONT COLOR="#0000ff">%f</FONT></TT> is used for both
<TT>
<FONT COLOR="#0000ff">float</FONT></TT><I>s</I> <I>and</I>
<TT>
<FONT COLOR="#0000ff">double</FONT></TT><I>s</I>. The reason is that a <TT>
<FONT COLOR="#0000ff">float</FONT></TT> is always converted
to a <TT>
<FONT COLOR="#0000ff">double</FONT></TT> before an operation like this. Also try replacing
<TT>
<FONT COLOR="#0000ff">%f</FONT></TT> with <TT>
<FONT COLOR="#0000ff">%e</FONT></TT> to print in
exponential notation--that is,
less significant digits.
<P>
<H2><A NAME="SECTION002513000000000000000">
22.1.3 Functions</A>
</H2>
<P>
Functions are implemented as follows:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>void mutiply_and_print (int x, int y)</code><br>
<code>{</code><br>
<code> printf ("%d * %d = %d\n", x, y, x * y);</code><br>
<code>}</code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> mutiply_and_print (30, 5);</code><br>
<code> mutiply_and_print (12, 3);</code><br>
<code> exit (3);</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Here we have a non-main function <I>called</I> by the
<TT>
<FONT COLOR="#0000ff">main</FONT></TT> function. The function is first <I>declared</I>
with
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>void mutiply_and_print (int x, int y)</code><br>
</FONT></TD></TR></TABLE><P>
<P>
This declaration states the return value of the function
(<TT>
<FONT COLOR="#0000ff">void</FONT></TT> for no return value), the function name
(<TT>
<FONT COLOR="#0000ff">mutiply_and_print</FONT></TT>), and then the
<I>arguments</I> that
are going to be passed to the function. The numbers passed to
the function are given their own names, <TT>
<FONT COLOR="#0000ff">x</FONT></TT> and <TT>
<FONT COLOR="#0000ff">y</FONT></TT>,
and are converted to the type of <TT>
<FONT COLOR="#0000ff">x</FONT></TT> and <TT>
<FONT COLOR="#0000ff">y</FONT></TT> before
being passed to the function--in this case, <TT>
<FONT COLOR="#0000ff">int</FONT></TT> and
<TT>
<FONT COLOR="#0000ff">int</FONT></TT>. The actual <B>C</B> code that comprises the function
goes between curly braces <TT>
<FONT COLOR="#0000ff">{</FONT></TT> and
<TT>
<FONT COLOR="#0000ff">}</FONT></TT>.
<P>
In other words, the above function is equivalent to:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>void mutiply_and_print ()</code><br>
<code>{</code><br>
<code> int x;</code><br>
<code> int y;</code><br>
<code> x = <first-number-passed></code><br>
<code> y = <second-number-passed></code><br>
<code> printf ("%d * %d = %d\n", x, y, x * y);</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H2><A NAME="SECTION002514000000000000000">
22.1.4 <TT>
<FONT COLOR="#0000ff">for</FONT></TT>, <TT>
<FONT COLOR="#0000ff">while</FONT></TT>, <TT>
<FONT COLOR="#0000ff">if</FONT></TT>, and <TT>
<FONT COLOR="#0000ff">switch</FONT></TT> statements</A>
</H2>
<P>
As with shell scripting, we have
the <TT>
<FONT COLOR="#0000ff">for</FONT></TT>, <TT>
<FONT COLOR="#0000ff">while</FONT></TT>, and
<TT>
<FONT COLOR="#0000ff">if</FONT></TT> statements:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>25</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>30</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>35</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>40</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>45</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> int x;</code><br>
<code> </code><br>
<code> x = 10;</code><br>
<code> </code><br>
<code> if (x == 10) {</code><br>
<code> printf ("x is exactly 10\n");</code><br>
<code> x++;</code><br>
<code> } else if (x == 20) {</code><br>
<code> printf ("x is equal to 20\n");</code><br>
<code> } else {</code><br>
<code> printf ("No, x is not equal to 10 or 20\n");</code><br>
<code> }</code><br>
<code> </code><br>
<code> if (x > 10) {</code><br>
<code> printf ("Yes, x is more than 10\n");</code><br>
<code> }</code><br>
<code> </code><br>
<code> while (x > 0) {</code><br>
<code> printf ("x is %d\n", x);</code><br>
<code> x = x - 1;</code><br>
<code> }</code><br>
<code> </code><br>
<code> for (x = 0; x < 10; x++) {</code><br>
<code> printf ("x is %d\n", x);</code><br>
<code> }</code><br>
<code> </code><br>
<code> switch (x) {</code><br>
<code> case 9:</code><br>
<code> printf ("x is nine\n");</code><br>
<code> break;</code><br>
<code> case 10:</code><br>
<code> printf ("x is ten\n");</code><br>
<code> break;</code><br>
<code> case 11:</code><br>
<code> printf ("x is eleven\n");</code><br>
<code> break;</code><br>
<code> default:</code><br>
<code> printf ("x is huh?\n");</code><br>
<code> break;</code><br>
<code> }</code><br>
<code> </code><br>
<code> return 0;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
It is easy to see the format that these statements take, although they
are vastly different from shell scripts. <B>C</B> code works in
<I>statement blocks</I> between
curly braces, in the same way that shell scripts have
<TT>
<FONT COLOR="#0000ff">do</FONT></TT>'s and <TT>
<FONT COLOR="#0000ff">done</FONT></TT>'s.
<P>
Note that with most programming languages when we want to
add <TT>
<FONT COLOR="#0000ff">1</FONT></TT> to a variable we have to write, say, <TT>
<FONT COLOR="#0000ff">x = x + 1</FONT></TT>.
In <B>C</B>, the abbreviation <TT>
<FONT COLOR="#0000ff">x++</FONT></TT> is used, meaning to
<I>increment</I> a variable by <TT>
<FONT COLOR="#0000ff">1</FONT></TT>.
<P>
The <TT>
<FONT COLOR="#0000ff">for</FONT></TT> loop takes three statements between <TT>
<FONT COLOR="#0000ff">(</FONT></TT>
... <TT>
<FONT COLOR="#0000ff">)</FONT></TT>: a statement to start things off, a comparison, and
a statement to be executed on each completion of the statement block.
The statement block after the <TT>
<FONT COLOR="#0000ff">for</FONT></TT> is repeatedly executed until
the comparison is untrue.
<P>
The <TT>
<FONT COLOR="#0000ff">switch</FONT></TT> statement is like
<TT>
<FONT COLOR="#0000ff">case</FONT></TT> in shell scripts.
<TT>
<FONT COLOR="#0000ff">switch</FONT></TT> considers the argument inside its <TT>
<FONT COLOR="#0000ff">(</FONT></TT> ...
<TT>
<FONT COLOR="#0000ff">)</FONT></TT> and decides which <TT>
<FONT COLOR="#0000ff">case</FONT></TT> line to jump to. In this
example it will obviously be <TT>
<FONT COLOR="#0000ff">printf ("x is ten\n");</FONT></TT> because
<TT>
<FONT COLOR="#0000ff">x</FONT></TT> was 10 when the previous <TT>
<FONT COLOR="#0000ff">for</FONT></TT> loop exited.
The <TT>
<FONT COLOR="#0000ff">break</FONT></TT> tokens mean that we are through with the <TT>
<FONT COLOR="#0000ff">switch</FONT></TT>
statement and that execution should continue from Line 46.
<P>
Note that in <B>C</B> the comparison <TT>
<FONT COLOR="#0000ff">==</FONT></TT> is used instead of <TT>
<FONT COLOR="#0000ff">=</FONT></TT>.
The symbol <TT>
<FONT COLOR="#0000ff">=</FONT></TT> means to assign a value to a variable, whereas <TT>
<FONT COLOR="#0000ff">==</FONT></TT>
is an <I>equality operator</I>.
<P>
<H2><A NAME="SECTION002515000000000000000">
22.1.5 Strings, arrays, and memory allocation</A>
</H2>
<P>
You can define a list of numbers with:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>int y[10];</code><br>
</FONT></TD></TR></TABLE><P>
This list is called an <I>array</I>:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> int x;</code><br>
<code> int y[10];</code><br>
<code> for (x = 0; x < 10; x++) {</code><br>
<code> y[x] = x * 2;</code><br>
<code> }</code><br>
<code> for (x = 0; x < 10; x++) {</code><br>
<code> printf ("item %d is %d\n", x, y[x]);</code><br>
<code> }</code><br>
<code> return 0;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
<P>
If an array is of type <TT>
<FONT COLOR="#0000ff">char</FONT></TT><I>acter</I>,
then it is called a <I>string</I>:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> int x;</code><br>
<code> char y[11];</code><br>
<code> for (x = 0; x < 10; x++) {</code><br>
<code> y[x] = 65 + x * 2;</code><br>
<code> }</code><br>
<code> for (x = 0; x < 10; x++) {</code><br>
<code> printf ("item %d is %d\n", x, y[x]);</code><br>
<code> }</code><br>
<code> y[10] = 0;</code><br>
<code> printf ("string is %s\n", y);</code><br>
<code> return 0;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
Note that a string has to be <I>null-terminated</I>. This means
that the last character must be a zero. The code <TT>
<FONT COLOR="#0000ff">y[10] = 0</FONT></TT>
sets the 11th item in the array to zero. This also means that
strings need to be one <TT>
<FONT COLOR="#0000ff">char</FONT></TT> longer than you would think.
<P>
(Note that the first item in the array is <TT>
<FONT COLOR="#0000ff">y[0]</FONT></TT>, not <TT>
<FONT COLOR="#0000ff">y[1]</FONT></TT>,
as with some other programming languages.)
<P>
In the preceding example, the line <TT>
<FONT COLOR="#0000ff">char y[11]</FONT></TT> reserved 11
bytes for the string. But what if you want a string of 100,000
bytes? <B>C</B> allows you to request memory
from the kernel.
This is called <I>allocate memory</I>. Any non-trivial
program will allocate memory for itself and there is no other
way of getting large blocks of memory for your program
to use. Try:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> int x;</code><br>
<code> char *y;</code><br>
<code> y = malloc (11);</code><br>
<code> printf ("%ld\n", y);</code><br>
<code> for (x = 0; x < 10; x++) {</code><br>
<code> y[x] = 65 + x * 2;</code><br>
<code> }</code><br>
<code> y[10] = 0;</code><br>
<code> printf ("string is %s\n", y);</code><br>
<code> free (y);</code><br>
<code> return 0;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
<P>
The declaration <TT>
<FONT COLOR="#0000ff">char *y</FONT></TT> means to
declare a variable (a number) called <TT>
<FONT COLOR="#0000ff">y</FONT></TT> that <I>points</I>
to a memory location. The <TT>
<FONT COLOR="#0000ff">*</FONT></TT>
(<I>asterisk</I>) in this context
means <I>pointer</I>. For example, if you have a machine with perhaps 256
megabytes of RAM + swap, then <TT>
<FONT COLOR="#0000ff">y</FONT></TT> potentially has a range of
this much. The numerical value of <TT>
<FONT COLOR="#0000ff">y</FONT></TT> is also printed
with <TT>
<FONT COLOR="#0000ff">printf ("%ld\n", y);</FONT></TT>, but is of no
interest to the programmer.
<P>
When you have finished using memory you must give it back to the operating
system by using <TT>
<FONT COLOR="#0000ff">free</FONT></TT>. Programs that don't <TT>
<FONT COLOR="#0000ff">free</FONT></TT>
all the memory they allocate are said to
<I>leak</I> memory.
<P>
Allocating memory often requires you to perform a calculation to
determine the amount of memory required. In the above case we
are allocating the space of 11 <TT>
<FONT COLOR="#0000ff">char</FONT></TT>s. Since each
<TT>
<FONT COLOR="#0000ff">char</FONT></TT> is really a single byte, this presents no problem. But
what if we were allocating 11 <TT>
<FONT COLOR="#0000ff">int</FONT></TT>s? An <TT>
<FONT COLOR="#0000ff">int</FONT></TT> on a
PC is 32 bits--four bytes. To determine the size of a type,
we use the <TT>
<FONT COLOR="#0000ff">sizeof</FONT></TT> keyword:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> int a;</code><br>
<code> int b;</code><br>
<code> int c;</code><br>
<code> int d;</code><br>
<code> int e;</code><br>
<code> int f;</code><br>
<code> int g;</code><br>
<code> a = sizeof (char);</code><br>
<code> b = sizeof (short);</code><br>
<code> c = sizeof (int);</code><br>
<code> d = sizeof (long);</code><br>
<code> e = sizeof (float);</code><br>
<code> f = sizeof (double);</code><br>
<code> g = sizeof (long double);</code><br>
<code> printf ("%d, %d, %d, %d, %d, %d, %d\n", a, b, c, d, e, f, g);</code><br>
<code> return 0;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
Here you can see the number of bytes required by all of these
types.
Now we can easily allocate arrays of things other than <TT>
<FONT COLOR="#0000ff">char</FONT></TT>.
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> int x;</code><br>
<code> int *y;</code><br>
<code> y = malloc (10 * sizeof (int));</code><br>
<code> printf ("%ld\n", y);</code><br>
<code> for (x = 0; x < 10; x++) {</code><br>
<code> y[x] = 65 + x * 2;</code><br>
<code> }</code><br>
<code> for (x = 0; x < 10; x++) {</code><br>
<code> printf ("%d\n", y[x]);</code><br>
<code> }</code><br>
<code> free (y);</code><br>
<code> return 0;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
On many machines an <TT>
<FONT COLOR="#0000ff">int</FONT></TT> is four bytes (32 bits), but you
should never assume this. <I>Always use the <TT>
<FONT COLOR="#0000ff">sizeof</FONT></TT>
keyword to allocate memory.</I>
<P>
<H2><A NAME="SECTION002516000000000000000">
22.1.6 String operations</A>
</H2>
<P>
<B>C</B> programs probably do more string manipulation than
anything else. Here is a program that divides a sentence
into words:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>25</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>30</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>35</code></font><code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code>#include <string.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> int length_of_word;</code><br>
<code> int i;</code><br>
<code> int length_of_sentence;</code><br>
<code> char p[256];</code><br>
<code> char *q;</code><br>
<code> </code><br>
<code> strcpy (p, "hello there, my name is fred.");</code><br>
<code> </code><br>
<code> length_of_sentence = strlen (p);</code><br>
<code> </code><br>
<code> length_of_word = 0;</code><br>
<code> </code><br>
<code> for (i = 0; i <= length_of_sentence; i++) {</code><br>
<code> if (p[i] == ' ' || i == length_of_sentence) {</code><br>
<code> q = malloc (length_of_word + 1);</code><br>
<code> if (q == 0) {</code><br>
<code> perror ("malloc failed");</code><br>
<code> abort ();</code><br>
<code> }</code><br>
<code> strncpy (q, p + i - length_of_word, length_of_word);</code><br>
<code> q[length_of_word] = 0;</code><br>
<code> printf ("word: %s\n", q);</code><br>
<code> free (q);</code><br>
<code> length_of_word = 0;</code><br>
<code> } else {</code><br>
<code> length_of_word = length_of_word + 1;</code><br>
<code> }</code><br>
<code> }</code><br>
<code> return 0;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
Here we introduce three more
<I>standard <B>C</B> library functions</I>.
<TT>
<FONT COLOR="#0000ff">strcpy</FONT></TT> stands for
<TT>
<FONT COLOR="#0000ff">str</FONT></TT><I>ing</I><TT>
<FONT COLOR="#0000ff">c</FONT></TT><I>o</I><TT>
<FONT COLOR="#0000ff">py</FONT></TT>. It copies
bytes from one place to another sequentially, until it reaches a zero
byte (i.e., the end of string). Line 13 of this program
copies text <I>into</I> the <TT>
<FONT COLOR="#0000ff">char</FONT></TT>acter array <TT>
<FONT COLOR="#0000ff">p</FONT></TT>, which
is called the <I>target</I> of the copy.
<P>
<TT>
<FONT COLOR="#0000ff">strlen</FONT></TT> stands for <TT>
<FONT COLOR="#0000ff">str</FONT></TT><I>ing</I><TT>
<FONT COLOR="#0000ff">len</FONT></TT><I>gth</I>.
It determines the length of a string, which is just a count of
the number of <TT>
<FONT COLOR="#0000ff">char</FONT></TT>acters up to the null character.
<P>
We need to loop over the length of the sentence. The variable
<TT>
<FONT COLOR="#0000ff">i</FONT></TT> indicates the current position in the sentence.
<P>
Line 20 says that if
we find a character 32 (denoted by <TT>
<FONT COLOR="#0000ff">' '</FONT></TT>), we know we have
reached a word boundary. We also know that the end of the
sentence is a word boundary even though there may not be a
space there. The token <TT>
<FONT COLOR="#0000ff">||</FONT></TT>
means <B>OR</B>. At
this point we can allocate memory for the
current word and copy the word into that memory. The
<TT>
<FONT COLOR="#0000ff">strncpy</FONT></TT> function is useful for this. It copies
a string, but only up to a limit of <TT>
<FONT COLOR="#0000ff">length_of_word</FONT></TT>
characters (the last argument). Like <TT>
<FONT COLOR="#0000ff">strcpy</FONT></TT>, the first
argument is the target, and the second argument is the
place to copy from.
<P>
To calculate the position of the start of the last word, we use
<TT>
<FONT COLOR="#0000ff">p + i - length_of_word</FONT></TT>. This means that we are adding
<TT>
<FONT COLOR="#0000ff">i</FONT></TT> to the memory location <TT>
<FONT COLOR="#0000ff">p</FONT></TT> and then going back
<TT>
<FONT COLOR="#0000ff">length_of_word</FONT></TT> counts thereby pointing
<TT>
<FONT COLOR="#0000ff">strncpy</FONT></TT> to the exact position.
<P>
Finally, we null-terminate the string on Line 27. We can then print
<TT>
<FONT COLOR="#0000ff">q</FONT></TT>, <TT>
<FONT COLOR="#0000ff">free</FONT></TT> the used memory,
and begin with the next word.
<P>
For a complete list of string operations, see <TT>
<FONT COLOR="#0000ff">string</FONT></TT>(3).
<P>
<H2><A NAME="SECTION002517000000000000000">
22.1.7 File operations</A>
</H2>
<P>
Under most programming languages, file operations involve
three steps: <I>opening</I> a file, <I>reading</I> or
<I>writing</I> to the file, and then <I>closing</I> the
file. You use the command <TT>
<FONT COLOR="#0000ff">fopen</FONT></TT> to tell the operating
system that you are ready to begin working with a file:
<P>
The following program opens a file and spits it
out on the terminal:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code>#include <string.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> int c;</code><br>
<code> FILE *f;</code><br>
<code> </code><br>
<code> f = fopen ("mytest.c", "r");</code><br>
<code> if (f == 0) {</code><br>
<code> perror ("fopen");</code><br>
<code> return 1;</code><br>
<code> }</code><br>
<code> for (;;) {</code><br>
<code> c = fgetc (f);</code><br>
<code> if (c == -1)</code><br>
<code> break;</code><br>
<code> printf ("%c", c);</code><br>
<code> }</code><br>
<code> fclose (f);</code><br>
<code> return 0;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
<P>
A new type is presented here: <TT>
<FONT COLOR="#0000ff">FILE *</FONT></TT>. It is a file
operations variable that must be <I>initialized</I> with
<TT>
<FONT COLOR="#0000ff">fopen</FONT></TT> before it can be used. The <TT>
<FONT COLOR="#0000ff">fopen</FONT></TT>
function takes two arguments: the first is the name of the file, and
the second is a string explaining <I>how</I> we want to open the
file--in this case <TT>
<FONT COLOR="#0000ff">"r"</FONT></TT> means <TT>
<FONT COLOR="#0000ff">r</FONT></TT><I>eading</I>
from the start of the file. Other options are <TT>
<FONT COLOR="#0000ff">"w"</FONT></TT> for
<TT>
<FONT COLOR="#0000ff">w</FONT></TT><I>riting</I> and several more described in <TT>
<FONT COLOR="#0000ff">fopen</FONT></TT>(3).
<P>
If the return value of <TT>
<FONT COLOR="#0000ff">fopen</FONT></TT> is zero, it means that
<TT>
<FONT COLOR="#0000ff">fopen</FONT></TT> has failed. The <TT>
<FONT COLOR="#0000ff">perror</FONT></TT> function then prints a
textual error message (for example, <TT>
<FONT COLOR="#0000ff">No such file or directory</FONT></TT>).
It is essential to check the return value of all
library calls in this way. These checks will
constitute about one third of your <B>C</B> program.
<P>
The command <TT>
<FONT COLOR="#0000ff">fgetc</FONT></TT> <I>gets</I> a character from the file.
It retrieves consecutive bytes from the file until it reaches the
end of the file, when it returns a <TT>
<FONT COLOR="#0000ff">-1</FONT></TT>. The <TT>
<FONT COLOR="#0000ff">break</FONT></TT>
statement says to immediately terminate the <TT>
<FONT COLOR="#0000ff">for</FONT></TT> loop,
whereupon execution will continue from line 21. <TT>
<FONT COLOR="#0000ff">break</FONT></TT>
statements can appear inside <TT>
<FONT COLOR="#0000ff">while</FONT></TT> loops as well.
<P>
You will notice that the <TT>
<FONT COLOR="#0000ff">for</FONT></TT> statement is empty. This is
allowable <B>C</B> code and means to loop forever.
<P>
Some other file functions are <TT>
<FONT COLOR="#0000ff">fread</FONT></TT>, <TT>
<FONT COLOR="#0000ff">fwrite</FONT></TT>,
<TT>
<FONT COLOR="#0000ff">fputc</FONT></TT>, <TT>
<FONT COLOR="#0000ff">fprintf</FONT></TT>, and <TT>
<FONT COLOR="#0000ff">fseek</FONT></TT>. See <TT>
<FONT COLOR="#0000ff">fwrite</FONT></TT>(3),
<TT>
<FONT COLOR="#0000ff">fputc</FONT></TT>(3), <TT>
<FONT COLOR="#0000ff">fprintf</FONT></TT>(3), and <TT>
<FONT COLOR="#0000ff">fseek</FONT></TT>(3).
<P>
<H2><A NAME="SECTION002518000000000000000">
22.1.8 Reading command-line arguments inside <B>C</B> programs</A>
</H2>
<P>
Up until now, you are probably wondering what the
<TT>
<FONT COLOR="#0000ff">(int argc, char *argv[])</FONT></TT> are for. These are
the command-line arguments passed to the program by
the shell. <TT>
<FONT COLOR="#0000ff">argc</FONT></TT> is the total number of command-line
arguments, and <TT>
<FONT COLOR="#0000ff">argv</FONT></TT> is an array of strings of
each argument. Printing them out is easy:
<P>
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code>#include <string.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> int i;</code><br>
<code> for (i = 0; i < argc; i++) {</code><br>
<code> printf ("argument %d is %s\n", i, argv[i]);</code><br>
<code> }</code><br>
<code> return 0;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H2><A NAME="SECTION002519000000000000000">
22.1.9 A more complicated example</A>
</H2>
<P>
Here we put this altogether in a program that reads in lots of files and
dumps them as words. Here are some new notations you will encounter:
<TT>
<FONT COLOR="#0000ff">!=</FONT></TT> is the inverse of <TT>
<FONT COLOR="#0000ff">==</FONT></TT> and tests if
<I>not-equal-to</I>; <TT>
<FONT COLOR="#0000ff">realloc</FONT></TT> <I>reallocates</I>
memory--it resizes an old block of memory so that any
bytes of the old block are preserved; <TT>
<FONT COLOR="#0000ff">\n</FONT></TT>,
<TT>
<FONT COLOR="#0000ff">\t</FONT></TT> mean the newline character, 10, or the
tab character, 9, respectively (see <TT>
<FONT COLOR="#0000ff">ascii</FONT></TT>(7)).
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red size="-1">
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>25</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>30</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>35</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>40</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>45</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>50</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>55</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>60</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>65</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>70</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-2"><code>75</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue size="-1">
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code>#include <string.h></code><br>
<code> </code><br>
<code>void word_dump (char *filename)</code><br>
<code>{</code><br>
<code> int length_of_word;</code><br>
<code> int amount_allocated;</code><br>
<code> char *q;</code><br>
<code> FILE *f;</code><br>
<code> int c;</code><br>
<code> </code><br>
<code> c = 0;</code><br>
<code> </code><br>
<code> f = fopen (filename, "r");</code><br>
<code> if (f == 0) {</code><br>
<code> perror ("fopen failed");</code><br>
<code> exit (1);</code><br>
<code> }</code><br>
<code> </code><br>
<code> length_of_word = 0;</code><br>
<code> </code><br>
<code> amount_allocated = 256;</code><br>
<code> q = malloc (amount_allocated);</code><br>
<code> if (q == 0) {</code><br>
<code> perror ("malloc failed");</code><br>
<code> abort ();</code><br>
<code> }</code><br>
<code> </code><br>
<code> while (c != -1) {</code><br>
<code> if (length_of_word >= amount_allocated) {</code><br>
<code> amount_allocated = amount_allocated * 2;</code><br>
<code> q = realloc (q, amount_allocated);</code><br>
<code> if (q == 0) {</code><br>
<code> perror ("realloc failed");</code><br>
<code> abort ();</code><br>
<code> }</code><br>
<code> }</code><br>
<code> </code><br>
<code> c = fgetc (f);</code><br>
<code> q[length_of_word] = c;</code><br>
<code> </code><br>
<code> if (c == -1 || c == ' ' || c == '\n' || c == '\t') {</code><br>
<code> if (length_of_word > 0) {</code><br>
<code> q[length_of_word] = 0;</code><br>
<code> printf ("%s\n", q);</code><br>
<code> }</code><br>
<code> amount_allocated = 256;</code><br>
<code> q = realloc (q, amount_allocated);</code><br>
<code> if (q == 0) {</code><br>
<code> perror ("realloc failed");</code><br>
<code> abort ();</code><br>
<code> }</code><br>
<code> length_of_word = 0;</code><br>
<code> } else {</code><br>
<code> length_of_word = length_of_word + 1;</code><br>
<code> }</code><br>
<code> }</code><br>
<code> </code><br>
<code> fclose (f);</code><br>
<code>}</code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> int i;</code><br>
<code> </code><br>
<code> if (argc < 2) {</code><br>
<code> printf ("Usage:\n\twordsplit <filename> ...\n");</code><br>
<code> exit (1);</code><br>
<code> }</code><br>
<code> </code><br>
<code> for (i = 1; i < argc; i++) {</code><br>
<code> word_dump (argv[i]);</code><br>
<code> }</code><br>
<code> </code><br>
<code> return 0;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
<P>
This program is more complicated than you might immediately
expect. Reading in a file where we are <I>sure</I> that a word will
never exceed 30 characters is simple. But what if we have a file
that contains some words that are 100,000 characters long? GNU
programs are expected to behave correctly under these
circumstances.
<P>
To cope with normal as well as extreme circumstances, we
start off assuming that a word will never be more than 256
characters. If it appears that the word is growing over 256
characters, we <TT>
<FONT COLOR="#0000ff">realloc</FONT></TT><I>ate</I> the memory space to
double its size (lines 32 amd 33). When we start with a new word, we can free up
memory again, so we <TT>
<FONT COLOR="#0000ff">realloc</FONT></TT> back to 256 again (lines 48 and 49). In this
way we are using the minimum amount of memory at each point in time.
<P>
We have hence created a program that can work efficiently with a
100-gigabyte file just as easily as with a 100-byte file. <I>This
is part of the art of <B>C</B> programming.</I>
<P>
Experienced <B>C</B> programmers may actually scoff at the above
listing because it really isn't as ``minimalistic'' as is absolutely
possible. In fact, it is a truly excellent listing for the following
reasons:
<UL>
<LI>The program is easy to understand.
</LI>
<LI>The program uses an efficient algorithm (albeit not optimal).
</LI>
<LI>The program contains no arbitrary limits that would cause
unexpected behavior in extreme circumstances.
</LI>
<LI>The program uses no nonstandard <B>C</B> functions or notations
that would prohibit it compiling successfully on other systems.
It is therefore <I>portable</I>.
</LI>
</UL>
<I>Readability in <B>C</B> is your first
priority--it is imperative
that what you do is <I>obvious</I> to anyone reading the code.</I>
<P>
<H2><A NAME="SECTION0025110000000000000000">
22.1.10 <TT>
<FONT COLOR="#0000ff">#include</FONT></TT> statements and prototypes</A>
</H2>
<P>
At the start of each program will be one or more <TT>
<FONT COLOR="#0000ff">#include</FONT></TT>
statements. These tell the compiler to read in another <B>C</B> program.
Now, ``raw'' <B>C</B> does not have a whole lot in the way of protecting
against errors: for example, the <TT>
<FONT COLOR="#0000ff">strcpy</FONT></TT> function could
just as well be used with one, three, or four arguments, and the
<B>C</B> program would still compile. It would, however, wreak havoc
with the internal memory and cause the program to crash. These
other <TT>
<FONT COLOR="#0000ff">.h</FONT></TT> <B>C</B> programs are called <I>header</I> files.
They contain templates for how functions are meant to be called.
Every function you might like to use is contained in one or another
template file. The templates are called <I>function
prototypes</I>. <FONT COLOR="#ffa500">[C++ has something called ``templates.'' This is
a special C++ term having nothing to do with the discussion here.]</FONT>
<P>
A function prototype is written the same as the function itself,
but without the code. A function
prototype for <TT>
<FONT COLOR="#0000ff">word_dump</FONT></TT>
would simply be:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>void word_dump (char *filename);</code><br>
</FONT></TD></TR></TABLE><P>
The trailing <TT>
<FONT COLOR="#0000ff">;</FONT></TT> is essential and distinguishes a function
prototype from a function.
<P>
After a function prototype is defined, any attempt to use the function in a
way other than intended--say, passing it to few arguments or
arguments of the wrong type--will be met with fierce
opposition from <TT>
<FONT COLOR="#0000ff">gcc</FONT></TT>.
<P>
You will notice that the <TT>
<FONT COLOR="#0000ff">#include <string.h></FONT></TT> appeared
when we started using <TT>
<FONT COLOR="#0000ff">str</FONT></TT>ing operations. Recompiling
these programs without the <TT>
<FONT COLOR="#0000ff">#include <string.h></FONT></TT> line
gives the warning message
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>mytest.c:21: warning: implicit declaration of function `strncpy'</code><br>
</FONT></TD></TR></TABLE><P>
which is quite to the point.
<P>
The function prototypes
give a clear definition of how every
function is to be used. Man pages will always first state
the function prototype so that you are clear on what arguments
are to be passed and what types they should have.
<P>
<H2><A NAME="SECTION0025111000000000000000">
22.1.11 <B>C</B> comments</A>
</H2>
<P>
A <B>C</B> comment is denoted with
<TT>
<FONT COLOR="#0000ff">/* <comment lines> */</FONT></TT> and can
span multiple lines.
Anything between the <TT>
<FONT COLOR="#0000ff">/*</FONT></TT> and <TT>
<FONT COLOR="#0000ff">*/</FONT></TT> is ignored. Every function
should be commented, and all nonobvious code should be
commented. It is a good maxim that a program that <I>needs</I>
lots of comments to explain it is <I>badly written</I>. Also, never
comment the obvious, and explain <I>why</I> you do things rather
that <I>what</I> you are doing. It is advisable <I>not</I> to
make pretty graphics between each function, so rather:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>/* returns -1 on error, takes a positive integer */</code><br>
<code>int sqr (int x)</code><br>
<code>{</code><br>
<code> <...></code><br>
</FONT></TD></TR></TABLE><P>
than
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>/***************************----SQR----******************************</code><br>
<code> * x = argument to make the square of *</code><br>
<code> * return value = *</code><br>
<code> * -1 (on error) *</code><br>
<code> * square of x (on success) *</code><br>
<code> ********************************************************************/</code><br>
<code>int sqr (int x)</code><br>
<code>{</code><br>
<code> <...></code><br>
</FONT></TD></TR></TABLE><P>
which is liable to cause nausea. In C++, the additional comment
<TT>
<FONT COLOR="#0000ff">//</FONT></TT> is allowed, whereby everything between the
<TT>
<FONT COLOR="#0000ff">//</FONT></TT> and the end of the line is ignored. It is accepted under <TT>
<FONT COLOR="#0000ff">gcc</FONT></TT>,
but should not be used unless you really are programming in C++. In addition,
programmers often ``comment out'' lines by placing a <TT>
<FONT COLOR="#0000ff">#if 0</FONT></TT> ...
<TT>
<FONT COLOR="#0000ff">#endif</FONT></TT> around them, which really does exactly the same
thing as a comment (see Section <A HREF="node25.html#sec:cdefine">22.1.12</A>) but allows
you to have comments within comments. For example
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code> int x;</code><br>
<code> x = 10;</code><br>
<code>#if 0</code><br>
<code> printf ("debug: x is %d\n", x); /* print debug information */</code><br>
<code>#endif</code><br>
<code> y = x + 10;</code><br>
<code> <...></code><br>
</FONT></TD></TR></TABLE><P>
comments out Line 4.
<P>
<H2><A NAME="SECTION0025112000000000000000">
22.1.12 <TT>
<FONT COLOR="#0000ff">#define</FONT></TT> and <TT>
<FONT COLOR="#0000ff">#if</FONT></TT> -- <B>C</B> macros</A>
</H2>
<P>
<A NAME="sec:cdefine"></A>
<P>
Anything starting with a <TT>
<FONT COLOR="#0000ff">#</FONT></TT> is not actually <B>C</B>, but a
<B>C</B> <I>preprocessor directive</I>. A <B>C</B> program is first
run through a <I>preprocessor</I> that removes all spurious
junk, like comments, <TT>
<FONT COLOR="#0000ff">#include</FONT></TT> statements, and anything
else beginning with a <TT>
<FONT COLOR="#0000ff">#</FONT></TT>. You can make <B>C</B> programs
much more readable by defining <I>macros</I> instead
of literal values. For instance,
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#define START_BUFFER_SIZE 256</code><br>
</FONT></TD></TR></TABLE><P>
in our example program, <TT>
<FONT COLOR="#0000ff">#define</FONT></TT><I>s</I> the text
<TT>
<FONT COLOR="#0000ff">START_BUFFER_SIZE</FONT></TT> to be the text <TT>
<FONT COLOR="#0000ff">256</FONT></TT>.
Thereafter, wherever in the <B>C</B> program we have a
<TT>
<FONT COLOR="#0000ff">START_BUFFER_SIZE</FONT></TT>, the text <TT>
<FONT COLOR="#0000ff">256</FONT></TT> will be seen by
the compiler, and we can use <TT>
<FONT COLOR="#0000ff">START_BUFFER_SIZE</FONT></TT>
instead. This is a much <I>cleaner</I> way of programming
because, if, say, we would like to change the <TT>
<FONT COLOR="#0000ff">256</FONT></TT> to some
other value, we only need to change it in one place.
<TT>
<FONT COLOR="#0000ff">START_BUFFER_SIZE</FONT></TT> is also more meaningful than a
number, making the program more readable.
<P>
Whenever you have a <I>literal constant</I> like <TT>
<FONT COLOR="#0000ff">256</FONT></TT>,
you should replace it with a macro defined near the top of
your program.
<P>
You can also check for the existence of macros with the
<TT>
<FONT COLOR="#0000ff">#ifdef</FONT></TT> and
<TT>
<FONT COLOR="#0000ff">#ifndef</FONT></TT> directive. <TT>
<FONT COLOR="#0000ff">#</FONT></TT> directives are
really a programming language all on their own:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>25</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>/* Set START_BUFFER_SIZE to fine-tune performance before compiling: */</code><br>
<code>#define START_BUFFER_SIZE 256</code><br>
<code>/* #define START_BUFFER_SIZE 128 */</code><br>
<code>/* #define START_BUFFER_SIZE 1024 */</code><br>
<code>/* #define START_BUFFER_SIZE 16384 */</code><br>
<code> </code><br>
<code>#ifndef START_BUFFER_SIZE</code><br>
<code>#error This code did not define START_BUFFER_SIZE. Please edit</code><br>
<code>#endif</code><br>
<code> </code><br>
<code>#if START_BUFFER_SIZE <= 0</code><br>
<code>#error Wooow! START_BUFFER_SIZE must be greater than zero</code><br>
<code>#endif</code><br>
<code> </code><br>
<code>#if START_BUFFER_SIZE < 16</code><br>
<code>#warning START_BUFFER_SIZE to small, program may be inefficient</code><br>
<code>#elif START_BUFFER_SIZE > 65536</code><br>
<code>#warning START_BUFFER_SIZE to large, program may be inefficient</code><br>
<code>#else</code><br>
<code>/* START_BUFFER_SIZE is ok, do not report */</code><br>
<code>#endif</code><br>
<code> </code><br>
<code>void word_dump (char *filename)</code><br>
<code>{</code><br>
<code> <...></code><br>
<code> amount_allocated = START_BUFFER_SIZE;</code><br>
<code> q = malloc (amount_allocated);</code><br>
<code> <...></code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H1><A NAME="SECTION002520000000000000000">
22.2 Debugging with <TT>
<FONT COLOR="#0000ff">gdb</FONT></TT> and <TT>
<FONT COLOR="#0000ff">strace</FONT></TT></A>
</H1>
<P>
Programming errors, or <I>bugs</I>, can be
found by inspecting program execution. Some developers claim
that the need for such inspection implies a sloppy development
process. Nonetheless it is instructive to learn <B>C</B> by
actually watching a program work.
<P>
<H2><A NAME="SECTION002521000000000000000">
22.2.1 <TT>
<FONT COLOR="#0000ff">gdb</FONT></TT></A>
</H2>
<P>
The GNU debugger, <TT>
<FONT COLOR="#0000ff">gdb</FONT></TT>,
is a replacement for the standard
U<SMALL>NIX</SMALL> debugger,
<TT>
<FONT COLOR="#0000ff">db</FONT></TT>. To debug a program means to step through its execution
line-by-line, in order to find programming errors as they happen. Use the
command <TT>
<FONT COLOR="#0000ff">gcc -Wall -g -O0 -o wordsplit wordsplit.c</FONT></TT> to recompile your
program above.
The <TT>
<FONT COLOR="#0000ff">-g</FONT></TT> option enables debugging support in the resulting executable
and the <TT>
<FONT COLOR="#0000ff">-O0</FONT></TT> option disables compiler optimization
(which sometimes causes confusing behavior).
For the following example, create a test file <TT>
<FONT COLOR="#0000ff">readme.txt</FONT></TT> with some plain text
inside it. You can then run <TT>
<FONT COLOR="#0000ff">gdb -q wordsplit</FONT></TT>. The
standard <TT>
<FONT COLOR="#0000ff">gdb</FONT></TT> prompt will appear,
which indicates the start of a <I>debugging session</I>:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) </code><br>
</FONT></TD></TR></TABLE><P>
At the prompt, many one letter commands are available to
control program execution. The first of these is <TT>
<FONT COLOR="#0000ff">r</FONT></TT><I>un</I>
which executes the program as though it had been started
from a regular shell:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) <font color="navy"><B>r</B></font></code><br>
<code>Starting program: /homes/src/wordsplit/wordsplit </code><br>
<code>Usage:</code><br>
<code> wordsplit <filename> ...</code><br>
<code> </code><br>
<code>Program exited with code 01.</code><br>
</FONT></TD></TR></TABLE><P>
Obviously, we will want to set some trial command-line arguments.
This is done with the special command, <TT>
<FONT COLOR="#0000ff">set args</FONT></TT>:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) <font color="navy"><B>set args readme.txt readme2.txt</B></font></code><br>
</FONT></TD></TR></TABLE><P>
<P>
The <TT>
<FONT COLOR="#0000ff">b</FONT></TT><I>reak</I> command
is used like <TT>
<FONT COLOR="#0000ff">b [[<file>:]<line>|<function>]</FONT></TT>,
and sets a <I>break point</I> at a function or line number:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) <font color="navy"><B>b main</B></font></code><br>
<code>Breakpoint 1 at 0x8048796: file wordsplit.c, line 67.</code><br>
</FONT></TD></TR></TABLE><P>
A break point will interrupt execution of the program. In this case
the program will stop when it enters the
<TT>
<FONT COLOR="#0000ff">main</FONT></TT> function (i.e., right at the start).
Now we can <TT>
<FONT COLOR="#0000ff">r</FONT></TT>un the program again:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) <font color="navy"><B>r</B></font></code><br>
<code>Starting program: /home/src/wordsplit/wordsplit readme.txt readme2.txt</code><br>
<code> </code><br>
<code>Breakpoint 1, main (argc=3, argv=0xbffff804) at wordsplit.c:67</code><br>
<code>67 if (argc < 2) {</code><br>
<code>(gdb) </code><br>
</FONT></TD></TR></TABLE><P>
As specified, the program stops at the beginning of the <TT>
<FONT COLOR="#0000ff">main</FONT></TT>
function at line 67.
<P>
If you are interested in viewing the contents of
a variable, you can use the <TT>
<FONT COLOR="#0000ff">p</FONT></TT>rint command:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) <font color="navy"><B>p argc</B></font></code><br>
<code>$1 = 3</code><br>
<code>(gdb) <font color="navy"><B>p argv[1]</B></font></code><br>
<code>$2 = 0xbffff988 "readme.txt"</code><br>
</FONT></TD></TR></TABLE><P>
which tells us the value of <TT>
<FONT COLOR="#0000ff">argc</FONT></TT> and <TT>
<FONT COLOR="#0000ff">argv[1]</FONT></TT>.
The <TT>
<FONT COLOR="#0000ff">l</FONT></TT><I>ist</I> command
displays the lines about the current line:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) <font color="navy"><B>l</B></font></code><br>
<code>63 int main (int argc, char *argv[])</code><br>
<code>64 {</code><br>
<code>65 int i;</code><br>
<code>66 </code><br>
<code>67 if (argc < 2) {</code><br>
<code>68 printf ("Usage:\n\twordsplit <filename> ...\n");</code><br>
<code>69 exit (1);</code><br>
<code>70 }</code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">l</FONT></TT><I>ist</I> command can also take an optional file and line number
(or even a function name):
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) <font color="navy"><B>l wordsplit.c:1</B></font></code><br>
<code>1 #include <stdlib.h></code><br>
<code>2 #include <stdio.h></code><br>
<code>3 #include <string.h></code><br>
<code>4 </code><br>
<code>5 void word_dump (char *filename)</code><br>
<code>6 {</code><br>
<code>7 int length_of_word;</code><br>
<code>8 int amount_allocated;</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Next, we can try setting a break point at an arbitrary line and then
using the <TT>
<FONT COLOR="#0000ff">c</FONT></TT><I>ontinue</I>
command to proceed with program execution:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) <font color="navy"><B>b wordsplit.c:48</B></font></code><br>
<code>Breakpoint 2 at 0x804873e: file wordsplit.c, line 48.</code><br>
<code>(gdb) <font color="navy"><B>c</B></font></code><br>
<code>Continuing.</code><br>
<code>Zaphod</code><br>
<code> </code><br>
<code>Breakpoint 2, word_dump (filename=0xbffff988 "readme.txt") at wordsplit.c:48</code><br>
<code>48 amount_allocated = 256;</code><br>
</FONT></TD></TR></TABLE><P>
Execution obediently stops at line 48. At this point it is useful to
run a <TT>
<FONT COLOR="#0000ff">b</FONT></TT><I>ack</I><TT>
<FONT COLOR="#0000ff">t</FONT></TT><I>race</I>.
This prints out the current
<I>stack</I> which shows the functions that were called to get to the
current line. This output allows you to <I>trace</I> the history of
execution.
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) <font color="navy"><B>bt</B></font></code><br>
<code>#0 word_dump (filename=0xbffff988 "readme.txt") at wordsplit.c:48</code><br>
<code>#1 0x80487e0 in main (argc=3, argv=0xbffff814) at wordsplit.c:73</code><br>
<code>#2 0x4003db65 in __libc_start_main (main=0x8048790 <main>, argc=3, ubp_av=0xbf</code><br>
<code>fff814, init=0x8048420 <_init>, </code><br>
<code> fini=0x804883c <_fini>, rtld_fini=0x4000df24 <_dl_fini>, stack_end=0xbffff8</code><br>
<code>0c) at ../sysdeps/generic/libc-start.c:111</code><br>
</FONT></TD></TR></TABLE><P>
<P>
The <TT>
<FONT COLOR="#0000ff">clear</FONT></TT> command then deletes the break point at the
current line:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) <font color="navy"><B>clear</B></font></code><br>
<code>Deleted breakpoint 2 </code><br>
</FONT></TD></TR></TABLE><P>
<P>
The most important commands for debugging are the <TT>
<FONT COLOR="#0000ff">n</FONT></TT><I>ext</I>
and <TT>
<FONT COLOR="#0000ff">s</FONT></TT><I>tep</I> commands. The <TT>
<FONT COLOR="#0000ff">n</FONT></TT> command simply executes one line of
<B>C</B> code:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) <font color="navy"><B>n</B></font></code><br>
<code>49 q = realloc (q, amount_allocated);</code><br>
<code>(gdb) <font color="navy"><B>n</B></font></code><br>
<code>50 if (q == 0) {</code><br>
<code>(gdb) <font color="navy"><B>n</B></font></code><br>
<code>54 length_of_word = 0;</code><br>
</FONT></TD></TR></TABLE><P>
This activity is called <I>stepping</I> through your program. The <TT>
<FONT COLOR="#0000ff">s</FONT></TT>
command is identical to <TT>
<FONT COLOR="#0000ff">n</FONT></TT> except that it dives into functions instead
of running them as single line. To see the difference, step over line 73
first with <TT>
<FONT COLOR="#0000ff">n</FONT></TT>, and then with <TT>
<FONT COLOR="#0000ff">s</FONT></TT>, as follows:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>25</code></font><code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) <font color="navy"><B>set args readme.txt readme2.txt</B></font></code><br>
<code>(gdb) <font color="navy"><B>b main</B></font></code><br>
<code>Breakpoint 1 at 0x8048796: file wordsplit.c, line 67.</code><br>
<code>(gdb) <font color="navy"><B>r</B></font></code><br>
<code>Starting program: /home/src/wordsplit/wordsplit readme.txt readme2.txt</code><br>
<code> </code><br>
<code>Breakpoint 1, main (argc=3, argv=0xbffff814) at wordsplit.c:67</code><br>
<code>67 if (argc < 2) {</code><br>
<code>(gdb) <font color="navy"><B>n</B></font></code><br>
<code>72 for (i = 1; i < argc; i++) {</code><br>
<code>(gdb) <font color="navy"><B>n</B></font></code><br>
<code>73 word_dump (argv[i]);</code><br>
<code>(gdb) <font color="navy"><B>n</B></font></code><br>
<code>Zaphod</code><br>
<code>has</code><br>
<code>two</code><br>
<code>heads</code><br>
<code>72 for (i = 1; i < argc; i++) {</code><br>
<code>(gdb) <font color="navy"><B>s</B></font></code><br>
<code>73 word_dump (argv[i]);</code><br>
<code>(gdb) <font color="navy"><B>s</B></font></code><br>
<code>word_dump (filename=0xbffff993 "readme2.txt") at wordsplit.c:13</code><br>
<code>13 c = 0;</code><br>
<code>(gdb) <font color="navy"><B>s</B></font></code><br>
<code>15 f = fopen (filename, "r");</code><br>
<code>(gdb) </code><br>
</FONT></TD></TR></TABLE><P>
<P>
An interesting feature of <TT>
<FONT COLOR="#0000ff">gdb</FONT></TT> is its ability to
attach onto running programs. Try the following sequence of
commands:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>[root@cericon]# <font color="navy"><B>lpd</B></font></code><br>
<code>[root@cericon]# <font color="navy"><B>ps awx | grep lpd</B></font></code><br>
<code>28157 ? S 0:00 lpd Waiting</code><br>
<code>28160 pts/6 S 0:00 grep lpd</code><br>
<code>[root@cericon]# <font color="navy"><B>gdb -q /usr/sbin/lpd</B></font></code><br>
<code>(no debugging symbols found)...</code><br>
<code>(gdb) <font color="navy"><B>attach 28157</B></font></code><br>
<code>Attaching to program: /usr/sbin/lpd, Pid 28157</code><br>
<code>0x40178bfe in __select () from /lib/libc.so.6</code><br>
<code>(gdb) </code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">lpd</FONT></TT> daemon was not compiled with debugging support,
but the point is still made: you can halt and debug <I>any</I> running
process on the system. Try running a <TT>
<FONT COLOR="#0000ff">bt</FONT></TT> for fun. Now release
the process with
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>(gdb) <font color="navy"><B>detach</B></font></code><br>
<code>Detaching from program: /usr/sbin/lpd, Pid 28157</code><br>
</FONT></TD></TR></TABLE><P>
<P>
The debugger provides copious amounts of online help.
The <TT>
<FONT COLOR="#0000ff">help</FONT></TT> command can
be run to explain further.
The <TT>
<FONT COLOR="#0000ff">gdb</FONT></TT> <TT>
<FONT COLOR="#0000ff">info</FONT></TT> pages also elaborate on an
enormous number of display features and tracing
features not covered here.
<P>
<H2><A NAME="SECTION002522000000000000000">
22.2.2 Examining <TT>
<FONT COLOR="#0000ff">core</FONT></TT> files</A>
</H2>
<P>
If your program has a segmentation violation
(``segfault'') then a <TT>
<FONT COLOR="#0000ff">core</FONT></TT> file will be
written to the current directory. This is known as a <I>core dump</I>.
A core dump is caused by a bug in the program--its response to a
<TT>
<FONT COLOR="#0000ff">SIGSEGV</FONT></TT> signal sent to the program because
it tried to access an area of memory outside of its allowed range.
These files can be examined using <TT>
<FONT COLOR="#0000ff">gdb</FONT></TT> to (usually) reveal where the problem
occurred. Simply run <TT>
<FONT COLOR="#0000ff">gdb <executable> ./core</FONT></TT> and then type <TT>
<FONT COLOR="#0000ff">bt</FONT></TT> (or any <TT>
<FONT COLOR="#0000ff">gdb</FONT></TT> command)
at the <TT>
<FONT COLOR="#0000ff">gdb</FONT></TT> prompt. Typing <TT>
<FONT COLOR="#0000ff">file ./core</FONT></TT> will reveal something like
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red size="-1">
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue size="-1">
<code>/root/core: ELF 32-bit LSB core file of '<executable>' (signal 11), Intel 80386, version 1</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H2><A NAME="SECTION002523000000000000000">
22.2.3 <TT>
<FONT COLOR="#0000ff">strace</FONT></TT></A>
</H2>
<P>
The <TT>
<FONT COLOR="#0000ff">strace</FONT></TT> command prints every
<I>system call</I> performed
by a program. A system call is a function call made <I>by</I> a <B>C</B>
library function to the L<SMALL>INUX</SMALL> kernel. Try
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>strace ls</code><br>
<code>strace ./wordsplit</code><br>
</FONT></TD></TR></TABLE><P>
<P>
If a program has not been compiled with debugging support, the
only way to inspect its execution may be with the <TT>
<FONT COLOR="#0000ff">strace</FONT></TT>
command. In any case, the command can provide valuable information
about where a program is failing and is useful for diagnosing errors.
<P>
<H1><A NAME="SECTION002530000000000000000">
22.3 <B>C</B> Libraries</A>
</H1>
<P>
We made reference to the Standard <B>C</B> library. The <B>C</B>
language on its own does almost nothing; everything useful
is an external function. External functions are grouped
into libraries. The Standard <B>C</B> library is the file
<TT>
<FONT COLOR="#0000ff">/lib/libc.so.6</FONT></TT>. To list all the <B>C</B> library functions, run:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>nm /lib/libc.so.6</code><br>
<code>nm /lib/libc.so.6 | grep ' T ' | cut -f3 -d' ' | grep -v '^_' | sort -u | less</code><br>
</FONT></TD></TR></TABLE><P>
many of these have <TT>
<FONT COLOR="#0000ff">man</FONT></TT> pages, but some will have
no documentation and require you to read the comments inside
the header files (which are often most explanatory).
It is better not to use functions unless
you are sure that they are
<I>standard</I> functions in the
sense that they are common to other systems.
<P>
To create your own library is simple. Let's say we have two
files that contain several functions that we would like to
compile into a library. The files are <TT>
<FONT COLOR="#0000ff">simple_math_sqrt.c</FONT></TT>
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>static int abs_error (int a, int b)</code><br>
<code>{</code><br>
<code> if (a > b)</code><br>
<code> return a - b;</code><br>
<code> return b - a;</code><br>
<code>}</code><br>
<code> </code><br>
<code>int simple_math_isqrt (int x)</code><br>
<code>{</code><br>
<code> int result;</code><br>
<code> if (x < 0) {</code><br>
<code> fprintf (stderr, </code><br>
<code> "simple_math_sqrt: taking the sqrt of a negative number\n");</code><br>
<code> abort ();</code><br>
<code> }</code><br>
<code> result = 2;</code><br>
<code> while (abs_error (result * result, x) > 1) {</code><br>
<code> result = (x / result + result) / 2;</code><br>
<code> }</code><br>
<code> return result;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
and <TT>
<FONT COLOR="#0000ff">simple_math_pow.c</FONT></TT>
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>int simple_math_ipow (int x, int y)</code><br>
<code>{</code><br>
<code> int result;</code><br>
<code> if (x == 1 || y == 0)</code><br>
<code> return 1;</code><br>
<code> if (x == 0 && y < 0) {</code><br>
<code> fprintf (stderr,</code><br>
<code> "simple_math_pow: raising zero to a negative power\n");</code><br>
<code> abort ();</code><br>
<code> }</code><br>
<code> if (y < 0)</code><br>
<code> return 0;</code><br>
<code> result = 1;</code><br>
<code> while (y > 0) {</code><br>
<code> result = result * x;</code><br>
<code> y = y - 1;</code><br>
<code> }</code><br>
<code> return result;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
<P>
We would like to call the library <TT>
<FONT COLOR="#0000ff">simple_math</FONT></TT>.
It is good practice to name all the functions in the library
<TT>
<FONT COLOR="#0000ff">simple_math_</FONT></TT><I>??????</I>. The function <TT>
<FONT COLOR="#0000ff">abs_error</FONT></TT>
is not going to be used outside of the file <TT>
<FONT COLOR="#0000ff">simple_math_sqrt.c</FONT></TT>
and so we put the keyword <TT>
<FONT COLOR="#0000ff">static</FONT></TT> in front of it, meaning
that it is a <I>local</I> function.
<P>
We can compile the code with:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>gcc -Wall -c simple_math_sqrt.c</code><br>
<code>gcc -Wall -c simple_math_pow.c</code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">-c</FONT></TT> option means <I>compile only</I>. The
code is not turned into an executable. The generated
files are <TT>
<FONT COLOR="#0000ff">simple_math_sqrt.o</FONT></TT> and <TT>
<FONT COLOR="#0000ff">simple_math_pow.o</FONT></TT>.
These are called <TT>
<FONT COLOR="#0000ff">o</FONT></TT><I>bject</I> files.
<P>
We now need to <I>archive</I> these files into
a library. We do this with the <TT>
<FONT COLOR="#0000ff">ar</FONT></TT> command (a predecessor
of <TT>
<FONT COLOR="#0000ff">tar</FONT></TT>):
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>ar libsimple_math.a simple_math_sqrt.o simple_math_pow.o</code><br>
<code>ranlib libsimple_math.a</code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">ranlib</FONT></TT> command indexes the archive.
<P>
The library can now be used. Create a file <TT>
<FONT COLOR="#0000ff">mytest.c</FONT></TT>:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code> </code><br>
<code>int main (int argc, char *argv[])</code><br>
<code>{</code><br>
<code> printf ("%d\n", simple_math_ipow (4, 3));</code><br>
<code> printf ("%d\n", simple_math_isqrt (50));</code><br>
<code> return 0;</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
and run
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>gcc -Wall -c mytest.c</code><br>
<code>gcc -o mytest mytest.o -L. -lsimple_math</code><br>
</FONT></TD></TR></TABLE><P>
The first command compiles the file <TT>
<FONT COLOR="#0000ff">mytest.c</FONT></TT> into
<TT>
<FONT COLOR="#0000ff">mytest.o</FONT></TT>, and the second function is called
<I>linking</I> the program, which assimilates <TT>
<FONT COLOR="#0000ff">mytest.o</FONT></TT>
and the libraries into a single executable. The option
<TT>
<FONT COLOR="#0000ff">L.</FONT></TT> means to look in the current directory
for any libraries (usually only <TT>
<FONT COLOR="#0000ff">/lib</FONT></TT>
and <TT>
<FONT COLOR="#0000ff">/usr/lib</FONT></TT> are
searched). The option <TT>
<FONT COLOR="#0000ff">-lsimple_math</FONT></TT> means to assimilate the
library <TT>
<FONT COLOR="#0000ff">libsimple_math.a</FONT></TT> (<TT>
<FONT COLOR="#0000ff">lib</FONT></TT> and <TT>
<FONT COLOR="#0000ff">.a</FONT></TT> are added
automatically). This operation is called <I>static</I> <FONT COLOR="#ffa500">[Nothing
to do with the ``<TT>
<FONT COLOR="#0000ff">static</FONT></TT>'' keyword.]</FONT> linking
because it happens before the program is run and includes all object
files into the executable<A NAME="page:staticlinking"></A>.
<P>
As an aside, note that it is often the case that many static
libraries are linked into the same program. Here order is important: the
library with the least dependencies should come last, or you will get
so-called <I>symbol referencing errors</I>.
<P>
We can also create a header file <TT>
<FONT COLOR="#0000ff">simple_math.h</FONT></TT> for using the
library.
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>/* calculates the integer square root, aborts on error */</code><br>
<code>int simple_math_isqrt (int x);</code><br>
<code> </code><br>
<code>/* calculates the integer power, aborts on error */</code><br>
<code>int simple_math_ipow (int x, int y);</code><br>
</FONT></TD></TR></TABLE><P>
Add the line <TT>
<FONT COLOR="#0000ff">#include "simple_math.h"</FONT></TT> to
the top of <TT>
<FONT COLOR="#0000ff">mytest.c</FONT></TT>:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#include <stdlib.h></code><br>
<code>#include <stdio.h></code><br>
<code>#include "simple_math.h"</code><br>
</FONT></TD></TR></TABLE><P>
This addition gets rid of
the <TT>
<FONT COLOR="#0000ff">implicit declaration of function</FONT></TT>
warning messages.
Usually <TT>
<FONT COLOR="#0000ff">#include <simple_math.h></FONT></TT> would be used,
but here, this is a header file in the current directory--our
<I>own</I> header file--and this is where
we use <TT>
<FONT COLOR="#0000ff">"simple_math.h"</FONT></TT> instead of <TT>
<FONT COLOR="#0000ff"><simple_math.h></FONT></TT>.
<P>
<H1><A NAME="SECTION002540000000000000000">
22.4 <B>C</B> Projects -- <TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT>s</A>
</H1>
<P>
What if you make
a small change to one of the files
(as you are likely to do very often when developing)?
You could script the process of compiling and linking,
but the script would build everything, and not just the
changed file. What we really need is a utility that
only recompiles object files whose sources have changed:
<TT>
<FONT COLOR="#0000ff">make</FONT></TT> is such a utility.
<P>
<TT>
<FONT COLOR="#0000ff">make</FONT></TT> is a program that looks inside a
<TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT>
in the current directory then does a lot of compiling and linking.
<TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT>s contain lists of rules and <I>dependencies</I>
describing how to build a program.
<P>
Inside a <TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT> you need to state a list of
<I>what-depends-on-what</I> dependencies that <TT>
<FONT COLOR="#0000ff">make</FONT></TT>
can work through, as well as the shell commands needed to
achieve each goal.
<P>
<H2><A NAME="SECTION002541000000000000000">
22.4.1 Completing our example <TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT></A>
</H2>
<P>
Our first (last?) <I>dependency</I> in the process of completing
the compilation is that <TT>
<FONT COLOR="#0000ff">mytest</FONT></TT> <I>depends on</I>
both the library, <TT>
<FONT COLOR="#0000ff">libsimple_math.a</FONT></TT>, and
the object file, <TT>
<FONT COLOR="#0000ff">mytest.o</FONT></TT>. In <TT>
<FONT COLOR="#0000ff">make</FONT></TT> terms
we create a <TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT> line that looks like:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>mytest: libsimple_math.a mytest.o</code><br>
</FONT></TD></TR></TABLE><P>
meaning simply that the files <TT>
<FONT COLOR="#0000ff">libsimple_math.a mytest.o</FONT></TT>
must exist and be updated before <TT>
<FONT COLOR="#0000ff">mytest</FONT></TT>.
<TT>
<FONT COLOR="#0000ff">mytest:</FONT></TT> is called a <TT>
<FONT COLOR="#0000ff">make</FONT></TT> <I>target</I>.
Beneath this line, we also need to state
how to build <TT>
<FONT COLOR="#0000ff">mytest</FONT></TT>:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code> gcc -Wall -o $@ mytest.o -L. -lsimple_math</code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">$@</FONT></TT> means the name of the target
itself, which is just substituted with <TT>
<FONT COLOR="#0000ff">mytest</FONT></TT>. <I>Note that the
space before the <TT>
<FONT COLOR="#0000ff">gcc</FONT></TT> is a tab
character and not 8 space characters.</I>
<P>
The next dependency is that <TT>
<FONT COLOR="#0000ff">libsimple_math.a</FONT></TT>
depends on <TT>
<FONT COLOR="#0000ff">simple_math_sqrt.o simple_math_pow.o</FONT></TT>.
Once again we have a dependency, along with a shell
script to build the target. The full <TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT>
<I>rule</I> is:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>libsimple_math.a: simple_math_sqrt.o simple_math_pow.o</code><br>
<code> rm -f $@</code><br>
<code> ar rc $@ simple_math_sqrt.o simple_math_pow.o</code><br>
<code> ranlib $@</code><br>
</FONT></TD></TR></TABLE><P>
<I>Note again that the left margin consists of a single tab character and not spaces.</I>
<P>
The final dependency is that the files <TT>
<FONT COLOR="#0000ff">simple_math_sqrt.o</FONT></TT> and <TT>
<FONT COLOR="#0000ff">simple_math_pow.o</FONT></TT>
depend on the files <TT>
<FONT COLOR="#0000ff">simple_math_sqrt.c</FONT></TT> and <TT>
<FONT COLOR="#0000ff">simple_math_pow.c</FONT></TT>.
This requires two <TT>
<FONT COLOR="#0000ff">make</FONT></TT> target rules, but <TT>
<FONT COLOR="#0000ff">make</FONT></TT> has a short
way of stating such a rule in the case of many <B>C</B> source files,
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>.c.o:</code><br>
<code> gcc -Wall -c -o $*.o $<</code><br>
</FONT></TD></TR></TABLE><P>
which means that any <TT>
<FONT COLOR="#0000ff">.o</FONT></TT> files needed
can be built from a <TT>
<FONT COLOR="#0000ff">.c</FONT></TT> file of a similar name by means
of the command <TT>
<FONT COLOR="#0000ff">gcc -Wall -c -o $*.o $<</FONT></TT>, where <TT>
<FONT COLOR="#0000ff">$*.o</FONT></TT>
means the name of the object file and <TT>
<FONT COLOR="#0000ff">$<</FONT></TT> means
the name of the file that <TT>
<FONT COLOR="#0000ff">$*.o</FONT></TT> depends on, one at
a time.
<P>
<H2><A NAME="SECTION002542000000000000000">
22.4.2 Putting it all together</A>
</H2>
<P>
<TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT>s can, in fact, have their rules
put in any order, so it's best to state the most obvious
rules first for readability.
<P>
There is also a rule you should always state at the
outset:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>all: libsimple_math.a mytest</code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">all:</FONT></TT> target is the rule that
<TT>
<FONT COLOR="#0000ff">make</FONT></TT> tries to satisfy when <TT>
<FONT COLOR="#0000ff">make</FONT></TT> is
run with no command-line arguments. This just
means that <TT>
<FONT COLOR="#0000ff">libsimple_math.a</FONT></TT> and <TT>
<FONT COLOR="#0000ff">mytest</FONT></TT>
are the last two files to be built, that is, they are
the top-level dependencies.
<P>
<TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT>s also have their own form of environment
variables, like shell scripts. You can see that we have
used the text <TT>
<FONT COLOR="#0000ff">simple_math</FONT></TT> in three of our rules.
It makes sense to define a <I>macro</I> for this so that
we can easily change to a different library name.
<P>
Our final <TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT> is<A NAME="page:libmakefile"></A>:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code># Comments start with a # (hash) character like shell scripts.</code><br>
<code># Makefile to build libsimple_math.a and mytest program.</code><br>
<code># Paul Sheer <psheer@cranzgot.co.za> Sun Mar 19 15:56:08 2000</code><br>
<code> </code><br>
<code>OBJS = simple_math_sqrt.o simple_math_pow.o</code><br>
<code>LIBNAME = simple_math</code><br>
<code>CFLAGS = -Wall</code><br>
<code> </code><br>
<code>all: lib$(LIBNAME).a mytest</code><br>
<code> </code><br>
<code>mytest: lib$(LIBNAME).a mytest.o</code><br>
<code> gcc $(CFLAGS) -o $@ mytest.o -L. -l${LIBNAME}</code><br>
<code> </code><br>
<code>lib$(LIBNAME).a: $(OBJS)</code><br>
<code> rm -f $@</code><br>
<code> ar rc $@ $(OBJS)</code><br>
<code> ranlib $@</code><br>
<code> </code><br>
<code>.c.o:</code><br>
<code> gcc $(CFLAGS) -c -o $*.o $<</code><br>
<code> </code><br>
<code>clean:</code><br>
<code> rm -f *.o *.a mytest</code><br>
</FONT></TD></TR></TABLE><P>
<P>
We can now easily type
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>make</code><br>
</FONT></TD></TR></TABLE><P>
in the current directory to
cause everything to be built.
<P>
You can see we have added an additional disconnected
target <TT>
<FONT COLOR="#0000ff">clean:</FONT></TT>. Targets can be run explictly on the command-line
like this:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>make clean</code><br>
</FONT></TD></TR></TABLE><P>
which removes all built files.
<P>
<TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT>s have far more uses than just building <B>C</B>
programs. Anything that needs to be built from
sources can employ a <TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT> to make things
easier.
<P>
<HR>
<!--Navigation Panel-->
<A NAME="tex2html2197"
HREF="node26.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A>
<A NAME="tex2html2193"
HREF="rute.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A>
<A NAME="tex2html2187"
HREF="node24.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
<A NAME="tex2html2195"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html2198"
HREF="node26.html">23. Shared Libraries</A>
<B> Up:</B> <A NAME="tex2html2194"
HREF="rute.html">rute</A>
<B> Previous:</B> <A NAME="tex2html2188"
HREF="node24.html">21. System Services and</A>
  <B> <A NAME="tex2html2196"
HREF="node1.html">Contents</A></B>
<!--End of Navigation Panel-->
</BODY>
</HTML>
|