The C Programming Language(中文版)


w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! Ѣ᠔Փ⫼ⱘ㋏㒳DŽ䖭䞠ВϔϾ⡍⅞ⱘ՟ᄤDŽ೼ UNIX ᪡԰㋏㒳Ёˈއབԩ䖤㸠䖭Ͼ⿟ᑣপ } printf("hello, world\n"); { main() #include ೼ C 䇁㿔Ёˈ៥Ӏৃҹ⫼ϟ߫⿟ᑣᠧॄߎ“hello, worldā˖ ࠄᶤϾഄᮍDŽᥠᦵњ䖭ѯ᪡԰㒚㡖ҹৢˈ݊ᅗџᚙህ↨䕗ᆍᯧњDŽ ᖙ乏㓪ݭ⿟ᑣ᭛ᴀˈ✊ৢ៤ࡳഄ䖤㸠㓪䆥ˈᑊࡴ䕑ǃ䖤㸠ˈ᳔ৢ䕧ߎܜ⦄䖭ϾⳂⱘˈ៥Ӏ佪 ሑㅵ䖭Ͼ㒗дᕜㅔऩˈԚᇍѢ߱ᄺ䇁㿔ⱘҎᴹ䇈ˈᅗҡ✊ৃ㛑៤Ўϔ໻䱰⹡ˈ಴Ў㽕ᅲ hello, world 䇋ᠧॄߎϟ߫ݙᆍ 㓪ݭⱘ㄀ϔϾ⿟ᑣ޴Т䛑ᰃⳌৠⱘˈे˖ ᄺдϔ䮼ᮄ⿟ᑣ䆒䅵䇁㿔ⱘᚳϔ䗨ᕘህᰃՓ⫼ᅗ㓪ݭ⿟ᑣDŽᇍѢ᠔᳝䇁㿔ⱘ߱ᄺ㗙ᴹ䇈ˈ 1.1. ܹ䮼 ᶊDŽ дDŽ᮴䆎ᰃ㒣偠Єᆠⱘ⿟ᑣਬ䖬ᰃ߱ᄺ㗙ˈ䛑ৃҹᡞᴀゴ԰Ўৢ㓁৘ゴ䆺㒚䆆㾷ⱘݙᆍⱘḚ 㒗ܙ㋴DŽ߱ᄺ㗙ᑨ㓪ݭϔѯ㉏Ԑⱘᇣ⿟ᑣ԰Ўᴀゴݙᆍⱘ㸹ܗ㸠⿟ᑣ䆒䅵᠔䳔㽕ⱘϔѯ෎ᴀ ᮴䆎ᰃ߽䖬ᰃᓞˈϔϾ㒣偠Єᆠⱘ⿟ᑣਬᑨ䆹ৃҹҢᴀゴҟ㒡ⱘݙᆍЁ᥼ⶹҪӀ㞾Ꮕ䖯 ᅗⱘ䋳䴶ᕅડDŽ ѯݙᆍ೼ৢ㓁Ⳍ݇ゴ㡖䖬ᖙ乏ݡ⃵䆆䗄DŽ៥ӀᏠᳯ䖭⾡䞡໡㒭䇏㗙ᏺᴹⱘᐂࡽᬜᵰ䖰䖰䍙䖛 ࡯ᇚ䖭ѯ䯂乬ⱘᕅડ䰡ࠄ᳔ԢˈԚ䯂乬㚃ᅮ䖬ᰃᄬ೼DŽ঺ϔϾϡ䎇П໘೼Ѣˈᴀゴ᠔䆆ⱘᶤ ᑊ≵᳝⫼ࠄ C 䇁㿔ⱘ᠔᳝ᔎ໻ࡳ㛑ˈ಴ℸˈ䖭ѯ՟ᄤг䆌ᑊϡㅔ⋕ǃ㊒⚐DŽ㱑✊៥ӀᏆ㒣ሑ ᗻⱘᅠᭈ䇈ᯢˈᑊϨˈ⬅Ѣ↨䕗ㅔ⬹ˈৃ㛑ӮՓ䇏㗙ѻ⫳ϔѯ䇃㾷˗ݡ㗙ˈ⬅Ѣ᠔Вⱘ՟ᄤ 䖭⾡䆆㾷ᮍᓣг᳝㔎⚍DŽᑨᔧᦤ䇋⊼ᛣⱘᰃˈ೼ᴀゴⱘݙᆍЁ᮴⊩ᡒࠄӏԩ⡍ᅮ䇁㿔⡍ 䅼䆎DŽخㄝˈᴀゴᇚ᱖ϡ ᑧޚѯ䞡㽕⡍ᗻˈ↨བᣛ䩜ǃ㒧ᵘǃC 䇁㿔ЁकߚЄᆠⱘ䖤ㅫヺ䲚ড়ǃ䚼ߚ᥻ࠊ⌕䇁হҹঞᷛ ᐌ䞣ǃㅫᴃ䖤ㅫǃ᥻ࠊ⌕ǃߑ᭄ǃ෎ᴀ䕧ܹˋ䕧ߎㄝDŽ㗠ᇍѢ㓪ݭ䕗໻ൟ⿟ᑣ᠔⍝ঞࠄⱘϔ ᰃᏠᳯ䇏㗙㛑ሑᖿഄ㓪ݭߎ᳝⫼ⱘ⿟ᑣˈЎℸˈᴀゴᇚ䞡⚍ҟ㒡ϔѯ෎ᴀὖᗉˈ↨བব䞣Ϣ ໛ᅠᭈǃ䆺㒚ഄ䅼䆎 C 䇁㿔Ёⱘϔѯᡔᴃ˄ᔧ✊ˈ䖭䞠᠔Вⱘ᠔᳝՟ᄤ䛑ᰃℷ⹂ⱘ˅DŽ៥Ӏޚ 䅼䆎DŽ಴ℸˈᴀゴϡخ೼ℸ᱖ᯊϡ໮ˈމ㋴ˈ㟇Ѣ݊Ёⱘ݋ԧ㒚㡖ǃ㾘߭ҹঞϔѯ՟໪ᚙܗ ὖ㽕ഄҟ㒡 C 䇁㿔ˈЏ㽕ᰃ䗮䖛ᅲ䰙ⱘ⿟ᑣᓩܹ C 䇁㿔ⱘ෎ᴀܜ೼ᴀкⱘᓔ㆛ˈ៥Ӏ佪 ㄀1ゴᇐ㿔 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ⱘ㒗д˅ˈेՓ䕧ߎᠧॄᅠ៤ৢгخⱘᎺッ㸠佪ᓔྟDŽབᵰএᥝᄫヺІЁⱘ\n˄䖭ᰃϾؐᕫϔ ೼ C 䇁㿔Ёˈᄫヺᑣ߫\n 㸼⼎ᤶ㸠ヺˈ೼ᠧॄЁ䘛ࠄᅗᯊˈ䕧ߎᠧॄᇚᤶ㸠ˈҢϟϔ㸠 ᄫヺІDŽⳂࠡ៥ӀҙՓ⫼ᄫヺІ԰Ў printf ঞ݊ᅗߑ᭄ⱘখ᭄DŽ ⫼ঠᓩোᣀ䍋ᴹⱘᄫヺᑣ߫⿄ЎᄫヺІ៪ᄫヺІᐌ䞣ˈབ"hello, world\n"ህᰃϔϾ ᅗᠧॄঠᓩোЁ䯈ⱘᄫヺІDŽ world\n"DŽ԰Ўখ᭄䇗⫼ printf ߑ᭄DŽprintf ᰃϔϾ⫼Ѣᠧॄ䕧ߎⱘᑧߑ᭄ˈ೼ℸ໘ˈ 䇗⫼ߑ᭄ᯊˈা䳔㽕Փ⫼ߑ᭄ৡࡴϞ⫼೚ᣀোᣀ䍋ᴹⱘখ᭄㸼ेৃDŽϞ䴶䖭ᴵ䇁হᇚ"hello, printf("hello, world\n"); ߑ᭄Ёⱘ䇁হ⫼ϔᇍ㢅ᣀো{}ᣀ䍋ᴹDŽᴀ՟Ёⱘ main ߑ᭄ҙࣙ৿ϟ䴶ϔᴵ䇁হ˖ ℸ⫼ぎখ᭄㸼()㸼⼎DŽ 㸼DŽߑ᭄ৡৢ䴶ⱘϔᇍ೚ᣀোᇚখ᭄߫㸼ᣀ䍋ᴹDŽ೼ᴀ՟Ёˈmain ߑ᭄ϡ䳔㽕ӏԩখ᭄ˈ಴ ߑ᭄П䯈䖯㸠᭄᥂Ѹᤶⱘϔ⾡ᮍ⊩ᰃ䇗⫼ߑ᭄৥㹿䇗⫼ߑ᭄ᦤկϔϾؐ˄⿄Ўϵ೴˅߫ ᑧ䖯㸠䆺㒚ҟ㒡DŽޚ৿䖭ϔ㸠䇁হDŽ៥Ӏᇚ೼㄀ 7 ゴ੠䰘ᔩ B Ёᇍᷛ 䕧ܹˋ䕧ߎᑧⱘֵᙃDŽ䆌໮ C 䇁㿔⑤⿟ᑣⱘᓔྟ໘䛑ࣙޚ⫼Ѣਞ䆝㓪䆥఼೼ᴀ⿟ᑣЁࣙ৿ᷛ #include 㞾Ꮕ㓪ݭⱘˈгৃҹᴹ㞾Ѣߑ᭄ᑧDŽϞ䗄⿟ᑣ↉Ёⱘ㄀ϔ㸠䇁হ main ߑ᭄䗮ᐌӮ䇗⫼݊ᅗߑ᭄ᴹᐂࡽᅠ៤ᶤѯᎹ԰ˈ㹿䇗⫼ⱘߑ᭄ৃҹᰃ⿟ᑣ䆒䅵Ҏਬ Ͼԡ㕂ࣙ৿ϔϾ main ߑ᭄DŽ Ͼ⡍⅞ⱘߑ᭄ৡ——↣Ͼ⿟ᑣ䛑Ң main ߑ᭄ⱘ䍋⚍ᓔྟᠻ㸠ˈ䖭ᛣੇⴔ↣Ͼ⿟ᑣ䛑ᖙ乏೼ᶤ ϟˈߑ᭄ⱘੑৡ≵᳝䰤ࠊˈԚ main ᰃϔމᕜ㉏ԐDŽ೼ᴀ՟Ёˈߑ᭄ⱘৡᄫЎ mainDŽ䗮ᐌᚙ ؐDŽC 䇁㿔Ёⱘߑ᭄㉏ԐѢ Fortran 䇁㿔Ёⱘᄤ⿟ᑣ੠ߑ᭄ˈϢ Pascal 䇁㿔Ёⱘ䖛⿟੠ߑ᭄г 䅵ㅫ䖛⿟ЁՓ⫼ⱘټⱘDŽߑ᭄Ёࣙ৿ϔѯ䇁হˈҹᣛᅮ᠔㽕ᠻ㸠ⱘ䅵ㅫ᪡԰˗ব䞣߭⫼Ѣᄬ ѯ䇈ᯢDŽϔϾ C 䇁㿔⿟ᑣˈ᮴䆎݊໻ᇣབԩˈ䛑ᰃ⬅ߑ᭄੠ব䞣㒘៤خϟ䴶ᇍ⿟ᑣᴀ䑿 ㄀ϔϾ C 䇁㿔⿟ᑣ \n ӝζܰ໻׸ ೴ printf ྻມವᆓ׸༝ॹĢۆ೴Ըဈ࣏ۆ main ࣳۜᄯܝ೴ԅဴࡳ՛΄ࣳၽۆ main ೴ē൑Ϣࠄ೓ϵ೴ᄔۆՇ࿌੠น main ԅ γᅹ࣏ԅ໰๯ۃͧ { ;(" printf("hello, world\n { main() #include ೼݊ᅗ᪡԰㋏㒳Ёˈ㓪䆥ǃࡴ䕑ǃ䖤㸠ㄝ㾘߭Ӯ᳝᠔ϡৠDŽ hello, world ेৃ䖤㸠 a.outˈᠧॄߎϟֵ߫ᙃ˖ a.out ᠻ㸠᭛ӊ a.outDŽ✊ৢˈ៥Ӏ䕧ܹ˖ བᵰ⑤⿟ᑣ≵᳝ҔМ䫭䇃˄՟བⓣᥝᄫヺ៪ᣐ䫭ᄫヺ˅ˈ㓪䆥䖛⿟ᇚ乎߽䖯㸠ˈᑊ⫳៤ϔϾৃ cc hello.c ৢݡ䗮䖛ϟ߫ੑҸ䖯㸠㓪䆥˖ ᖙ乏೼ᶤϾ᭛ӊЁᓎゟ䖭Ͼ⑤⿟ᑣˈᑊҹ“.cā԰Ў᭛ӊⱘᠽሩৡˈ՟བ hello.cˈ✊ܜ佪 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ℸ⿟ᑣЁҡ✊াࣙᣀϔϾৡЎ main ⱘߑ᭄ᅮНDŽᅗ↨ࠡ䴶ᠧॄ“hello, worldāⱘ⿟ᑣ 300 148 280 137 260 126 240 115 220 104 200 93 180 82 160 71 140 60 120 48 100 37 80 26 60 15 40 4 20 •6 1 •17 ៥ӀᴹⳟϟϔϾ⿟ᑣˈՓ⫼݀ᓣć=(5/9)(̧•32)ᠧॄϟ߫ढ⇣⏽ᑺϢᨘ⇣⏽ᑺᇍ✻㸼˖ 1.2. ব䞣Ϣㅫᴃ㸼䖒ᓣ DŽމᄫヺᑣ߫Ё᳾᳒߫ߎⱘᶤϔϾᄫヺ˅ᯊˈ㾖ᆳϔϟӮߎ⦄ҔМᚙ Ͼᅲ偠ˈᔧ printf ߑ᭄ⱘখ᭄ᄫヺІЁࣙ৿\c˄݊Ё c ᰃϞ䴶ⱘ䕀Нخ 㒗д 1•2 ݙᆍˈⳟⳟӮᕫࠄҔМߎ䫭ֵᙃDŽ 㒗д 1•1 ೼Դ㞾Ꮕⱘ㋏㒳Ё䖤㸠“hello, world ā⿟ᑣDŽݡ᳝ᛣএᥝ⿟ᑣЁⱘ䚼ߚ ߫ⱘᅠᭈ߫㸼DŽ ⼎ࠊ㸼ヺ˗\b 㸼⼎ಲ䗔ヺ˗\"㸼⼎ঠᓩো˗\\㸼⼎ড᭰ᴴヺᴀ䑿DŽ2.3 㡖ᇚ㒭ߎ䕀Нᄫヺᑣ ⱘᴎࠊDŽ䰸ℸП໪ˈC 䇁㿔ᦤկⱘ䕀Нᄫヺᑣ߫䖬ࣙᣀ˖\t 㸼ܙᄫヺᦤկњϔ⾡䗮⫼ⱘৃᠽ 䇋⊼ᛣˈ\n াҷ㸼ϔϾᄫヺDŽ㉏ԐѢ\n ⱘ䕀Нᄫヺᑣ߫Ў㸼⼎᮴⊩䕧ܹⱘᄫヺ៪ϡৃ㾕 䖭↉⿟ᑣϢࠡ䴶ⱘ⿟ᑣⱘ䕧ߎⳌৠDŽ } printf("\n"); printf("world"); printf("hello, "); { main() #include ߎ㸠DŽϞ䴶㒭ߎⱘ㄀ϔϾ⿟ᑣгৃҹᬍݭ៤ϟ߫ᔶᓣ˖ printf ߑ᭄∌䖰ϡӮ㞾ࡼᤶ㸠ˈ䖭ḋ៥Ӏৃҹ໮⃵䇗⫼䆹ߑ᭄ҹߚ䰊↉ᕫࠄϔϾ䭓ⱘ䕧 C 㓪䆥఼ᇚӮѻ⫳ϔᴵ䫭䇃ֵᙃDŽ "); printf("hello, world བ˖ ϡӮᤶ㸠DŽ೼ printf ߑ᭄ⱘখ᭄Ёˈা㛑⫼\n 㸼⼎ᤶ㸠ヺDŽབᵰ⫼⿟ᑣⱘᤶ㸠ҷ᳓\nˈ՟ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ೼Ϟ䴶ⱘ⏽ᑺ䕀ᤶ⿟ᑣЁˈ᳔ᓔྟᠻ㸠ⱘ䅵ㅫᰃϟ߫ 4 Ͼ䌟ؐ䇁হ˖ ߿ҟ㒡DŽ ᵘǃ㘨ড়ˈᣛ৥䖭ѯ㉏ൟⱘᣛ䩜ҹঞ䖨ಲ䖭ѯ㉏ൟؐⱘߑᬭDŽ៥Ӏᇚ೼ৢ㓁Ⳍᑨⱘゴ㡖Ёߚ Ѣ݋ԧⱘᴎ఼DŽ঺໪ˈ䖬ᄬ೼䖭ѯ෎ᴀ᭄᥂㉏ൟⱘ᭄㒘ǃ㒧އ䖭ѯ᭄᥂㉏ൟᇍ䈵ⱘ໻ᇣгপ double ঠ㊒ᑺ⍂⚍ൟ long 䭓ᭈൟ short ⷁᭈൟ char ᄫヺ——ϔϾᄫ㡖 䰸 int Ϣ float ㉏ൟП໪ˈC 䇁催䖬ᦤկњ݊ᅗϔѯ෎ᴀ᭄᥂㉏ൟˈ՟བ˖ ൟ䗮ᐌᰃ 32 ԡˈᅗ㟇ᇥ᳝ 6 ԡ᳝ᬜ᭄ᄫˈপؐ㣗ೈϔ㠀೼ 10•38̚1038 П䯈DŽ 䗮ᐌЎ 16 ԡˈ݊পؐ㣗ೈ೼•32768̚32767 П䯈ˈг᳝⫼ 32 ԡ㸼⼎ⱘ int ㉏ൟDŽfloat ㉏ Ѣ݋ԧⱘᴎ఼DŽᇍѢ int ㉏ൟˈއৃҹᏺ᳝ᇣ᭄䚼ߚⱘ᭄˅DŽint Ϣ float ㉏ൟⱘপؐ㣗ೈপ ݊Ёˈ㉏ൟ int 㸼⼎݊ৢ᠔߫ব䞣Ўᭈ᭄ˈϢПⳌᇍᑨⱘˈfloat 㸼⼎᠔߫ব䞣Ў⍂⚍᭄˄ेˈ int lower, upper, step; int fahr, celsius; 䇁হПࠡDŽໄᯢ⫼Ѣ䇈ᯢব䞣ⱘሲᗻˈᅗ⬅ϔϾ㉏ൟৡ੠ϔϾব䞣㸼㒘៤ˈ՟བ˖ ໄᯢৢՓ⫼DŽໄᯢ䗮ᐌᬒ೼ߑ᭄䍋ྟ໘ˈ೼ӏԩৃᠻ㸠ܜ೼ C 䇁㿔Ёˈ᠔᳝ব䞣䛑ᖙ乏 ࠊ㸼ヺ៪ᤶ㸠ヺП໘ˈ䛑ৃҹՓ⫼⊼䞞DŽ 䆌ߎ⦄ぎḐǃܕ㓪䆥఼ᗑ⬹DŽ⊼䞞ৃҹ㞾⬅ഄ䖤⫼೼⿟ᑣЁˈՓᕫ⿟ᑣ᳈ᯧѢ⧚㾷DŽ⿟ᑣЁ ҔМ⫼ⱘDŽࣙ৿೼/*Ϣ*/П䯈ⱘᄫヺᑣ߫ᇚ㹿خ⿄Ў⊼䞞ˈℸ໘ˈᅗㅔऩഄ㾷䞞ˈ䆹⿟ᑣᰃ ᠧॄढ⇣⏽ᑺϢᨘ⇣⏽ᑺᇍ✻㸼 */ /*ᔧ fahr=0ˈ20ˈ… ˈ300 ᯊˈߚ߿ ݊Ёⱘϸ㸠˖ } } fahr = fahr + step; printf("%d\t%d\n", fahr, celsius); celsius = 5 * (fahr•32) / 9; while (fahr <= upper) { fahr = lower; step = 20; /* ℹ䭓 */ upper = 300; /* ⏽ᑺ㸼ⱘϞ䰤 */ lower = 0; /* ⏽ᑺ㸼ⱘϟ䰤 */ int lower, upper, step; int fahr, celsius; { main() ᠧॄढ⇣⏽ᑺϢᨘ⇣⏽ᑺᇍ✻㸼 */ /* ᔧ fahr=0ˈ20ˈ… ˈ300 ᯊˈߚ߿ #include ᓣǃᕾ⦃ҹঞḐᓣ࣪䕧ߎDŽ䆹⿟ᑣབϟ᠔⼎˖ 䭓ϔѯˈԚᑊϡ໡ᴖDŽ䖭Ͼ⿟ᑣЁᓩܹњϔѯᮄⱘὖᗉˈࣙᣀ⊼䞞ǃໄᯢǃব䞣ǃㅫᴃ㸼䖒 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! Ⳃ੠㉏ൟϞ䛑ᖙ乏ऍ䜡ˈ৺߭ᇚߎ⦄䫭䇃ⱘ㒧ᵰDŽ printf ߑ᭄ⱘ㄀ϔϾখ᭄Ёⱘ৘Ͼ%ߚ߿ᇍᑨѢ㄀ѠϾǃ㄀ϝϾǃ……খ᭄ˈᅗӀ೼᭄ ⫼ѢᠧॄϸϾᭈ᭄ fahr Ϣ celsius ⱘؐˈᑊ೼ϸ㗙П䯈⬭ϔϾࠊ㸼ヺⱘぎ䯈˄\t˅DŽ printf(" %d\t%d\n", fahr, celsius); བˈ%d ᣛᅮϔϾᭈൟখ᭄ˈ಴ℸ䇁হ 㸼⼎݊ᅗⱘখ᭄˄㄀ѠϾǃ㄀ϝϾǃ……খ᭄˅Пϔ䖯㸠᳓ᤶⱘԡ㕂ˈᑊᣛᅮᠧॄḐᓣDŽ՟ 䆺㒚ҟ㒡DŽ䆹ߑ᭄ⱘ㄀ϔϾখ᭄ᰃᕙᠧॄⱘᄫヺІˈ݊Ёⱘ↣Ͼⱒߚো˄%˅خ㄀ 7 ゴᇚᇍℸ Ң䆹՟ᄤЁгৃҹⳟߎ printf ߑ᭄ⱘϔѯࡳ㛑DŽprintf ᰃϔϾ䗮⫼䕧ߎḐᓣ࣪ߑ᭄ˈ ᰃᭈ᭄ˈ5 / 9 Ⳍ䰸ৢ㒣៾প᠔ᕫⱘ㒧ᵰЎ 0ˈ಴ℸ䖭ḋ∖ᕫⱘ᠔᳝ᨘ⇣⏽ᑺ䛑ᇚЎ 0DŽ ݊ᅗ䇁㿔Ёˈᭈ᭄䰸⊩᪡԰ᇚᠻ㸠㟡ԡˈ㒧ᵰЁⱘӏԩᇣ᭄䚼ߚ䛑Ӯ㹿㟡ᓗDŽ⬅Ѣ 5 ੠ 9 䛑 Ь 5 ✊ৢݡ䰸ҹ 9 㗠ϡᰃⳈ᥹ݭ៤ 5 / 9ˈ݊ॳ಴ᰃ೼ C 䇁㿔ঞ䆌໮ܜП᠔ҹᡞ㸼䖒ᓣݭ៤ ⫼Ѣ䅵ㅫϢᣛᅮढ⇣⏽ᑺⳌᇍᑨⱘᨘ⇣⏽ᑺؐˈᑊᇚ㒧ᵰ䌟ؐ㒭ব䞣 celsiusDŽ೼䆹䇁হЁˈ celsius = 5 * (fahr • 32) / 9˖ ೼䆹⿟ᑣЁˈ㒱໻䚼ߚᎹ԰䛑ᰃ೼ᕾ⦃ԧЁᅠ៤ⱘDŽᕾ⦃ԧЁⱘ䌟ؐ䇁হ ៤ϔⳈՓ⫼䖭⾡亢ḐⱘདдᛃDŽݏḐЁ䗝ᢽњϔ⾡ˈ䇏㗙ৃҹ䗝ᢽ䗖ড়㞾Ꮕⱘϔ⾡亢Ḑˈᑊ 㒧ড়݇㋏᳈⏙ἮᯢњDŽⳌ↨㗠㿔ˈ㢅ᣀোⱘԡ㕂ህϡ䙷М䞡㽕њDŽ៥ӀҢ↨䕗⌕㸠ⱘϔѯ亢 ៥Ӏᓎ䆂↣㸠াкݭϔᴵ䇁হˈᑊ೼䖤ㅫヺϸ䖍৘ࡴϞϔϾぎḐᄫヺˈ䖭ḋৃҹՓᕫ䖤ㅫⱘ ⿟ᑣⱘ໪㾖ᔶᓣˈԚℷ⹂ⱘ㓽䖯ҹঞֱ⬭䗖ᔧぎḐⱘ⿟ᑣ䆒䅵亢Ḑᇍ⿟ᑣⱘᯧ䇏ᗻ䴲ᐌ䞡㽕DŽ ߎᕾ⦃䇁হЁࣙ৿ાѯ䇁হDŽ䖭⾡㓽䖯ᮍᓣさߎњ⿟ᑣⱘ䘏䕥㒧ᵘDŽሑㅵ C 㓪䆥఼ᑊϡ݇ᖗ ϟˈ៥Ӏᘏᰃᡞ⬅ while ᥻ࠊⱘ䇁হ㓽䖯ϔϾࠊ㸼ԡˈ䖭ḋህৃҹᕜᆍᯧഄⳟމ೼䖭ϸ⾡ᚙ i = 2 * i; while (i < j) ᑣ˅ˈгৃҹᰃϡ⫼㢅ᣀোࣙᣀⱘऩᴵ䇁হˈ՟བ˖ while 䇁হⱘᕾ⦃ԧৃҹᰃ⫼㢅ᣀোᣀ䍋ᴹⱘϔᴵ៪໮ᴵ䇁হ˄བϞ䴶ⱘ⏽ᑺ䕀ᤶ⿟ ᳝݊ᅗ䇁হˈ಴ℸᭈϾ⿟ᑣⱘᠻ㸠㒜ℶDŽ ᕾ⦃㒧ᴳˈᑊ㒻㓁ᠻ㸠䎳೼ while ᕾ⦃䇁হПৢⱘϟϔᴵ䇁হDŽ೼ᴀ⿟ᑣЁˈᕾ⦃䇁হৢ≵ fahr>upper)ᯊˈ)؛ᴵӊˈབᵰЎⳳˈ߭ݡ⃵ᠻ㸠ᕾ⦃ԧ˗ᔧ೚ᣀোЁⱘᴵӊ⌟䆩㒧ᵰЎ (fahr<=upper)ˈ߭ᠻ㸠ᕾ⦃ԧ˄ᣀ೼㢅ᣀোЁⱘ 3 ᴵ䇁হ˅˗✊ৢݡ䞡ᮄ⌟䆩೚ᣀোЁⱘ 䆩೚ᣀোЁⱘᴵӊ˗བᵰᴵӊЎⳳ⌟ܜwhile ᕾ⦃䇁হⱘᠻ㸠ᮍᓣᰃ䖭ḋⱘ˖佪 } ... while (fahr <= upper) { ⦃䇁হⱘ⫼䗨˖ ⏽ᑺ䕀ᤶ㸼Ёⱘ৘㸠䅵ㅫᮍᓣⳌৠˈ಴ℸৃҹ⫼ᕾ⦃䇁হ䞡໡䕧ߎ৘㸠DŽ䖭ᰃ while ᕾ ᅗӀЎব䞣䆒㕂߱ؐDŽ৘ᴵ䇁হഛҹߚো㒧ᴳDŽ fahr = lower; step = 20; upper = 300; lower = 0; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ൟˈ䕀ᤶ݀ᓣⱘ㸼䗄ᮍᓣг᳈㞾✊ϔѯDŽ೼ࠡϔϾ⿟ᑣЁˈП᠔ҹϡ㛑Փ⫼ 5 / 9 ⱘᔶᓣˈ 䖭Ͼ⿟ᑣϢࠡϔϾ⿟ᑣ෎ᴀⳌৠˈϡৠⱘᰃˈᅗᡞ fahr Ϣ celsius ໄᯢЎ float ㉏ } } fahr = fahr + step; printf("%3.0f %6.1f\n", fahr, celsius); celsius = (5.0/9.0) * (fahr•32.0); while (fahr <= upper) { fahr = lower; step = 20; /* step size */ upper = 300; /* upper limit */ lower = 0; /* lower limit of temperatuire scale */ float lower, upper, step; float fahr, celsius; { main() for fahr = 0, 20, ..., 300; floating•point version */ /* print Fahrenheit•Celsius table #include ϟ䴶ᰃ䆹⿟ᑣⱘজϔ⾡⠜ᴀ 䗖ᔧׂᬍDŽخ᳈㊒⹂ⱘ㒧ᵰˈᑨ䆹⫼⍂⚍ㅫᴃ䖤ㅫҷ᳓Ϟ䴶ⱘᭈൟㅫᴃ䖤ㅫDŽ䖭ህ䳔㽕ᇍ⿟ᑣ ᑺؐϡ໾㊒⹂ˈ՟བˈϢ 0̧ᇍᑨⱘ㊒⹂ⱘᨘ⇣⏽ᑺᑨ䆹Ў•17.8ćˈ㗠ϡᰃ•17ćDŽЎњᕫࠄ ঺ϔϾ䕗ЎϹ䞡ⱘ䯂乬ᰃˈ⬅Ѣ៥ӀՓ⫼ⱘᰃᭈൟㅫᴃ䖤ㅫˈ಴ℸ㒣䅵ㅫᕫࠄⱘᨘ⇣⏽ ... 100 37 80 26 60 15 40 4 20 •6 0 •17 䕧ߎⱘ㒧ᵰབϟ᠔⼎˖ ᠧॄ fahr Ϣ celsius ⱘؐˈ䖭ḋˈfahr ⱘؐऴ 3 Ͼ᭄ᄫᆑˈcelsius ⱘؐऴ 6 Ͼ᭄ᄫᆑˈ printf(" %3d %6d\n", fahr, celsius); %d Ёᣛᯢᠧॄᆑᑺˈ߭ᠧॄⱘ᭄ᄫӮ೼ᠧॄऎඳݙেᇍ唤DŽ՟བˈৃҹ⫼䇁হ བᵰ೼ printf 䇁হⱘ㄀ϔϾখ᭄ⱘ˖އ᠔ҹ䕧ߎⱘ㒧ᵰϡᰃᕜ㕢㾖DŽ䖭Ͼ䯂乬↨䕗ᆍᯧ㾷 Ϟ䗄ⱘ⏽ᑺ䕀ᤶ⿟ᑣᄬ೼ϸϾ䯂乬DŽ↨䕗ㅔऩⱘ䯂乬ᰃˈ⬅Ѣ䕧ߎⱘ᭄ϡᰃেᇍ唤ⱘˈ ᭄ܹ᥂㗠ϡᰃݭ䕧ߎ᭄᥂DŽ 䯙䇏 7.4 㡖Ёᇍ scanf ߑ᭄ⱘ䅼䆎䚼ߚˈscanf ߑ᭄㉏ԐѢ printf ߑ᭄ˈԚᅗ⫼Ѣ䇏䕧ܜ ᳈໮ⱘҟ㒡ˈᑊϨˈ⡍߿ᇚḐᓣ࣪䕧ܹ᥼ৢࠄ㄀ 7 ゴ䆆㾷DŽབᵰ䇏㗙ᛇњ㾷᭄᥂䕧ܹˈৃҹ خЎњᇚ䞡⚍ᬒࠄ䆆䗄 C 䇁㿔ᴀ䑿Ϟˈ៥Ӏ೼㄀ 7 ゴПࠡⱘ৘ゴЁᇚϡݡᇍ䕧ܹˋ䕧ߎ 㓪䆥఼੠ᑧᴹ䇈ˈ䆹ߑ᭄ⱘሲᗻ䛑ᰃⳌৠⱘDŽ ⱘޚᅮНњ printf ߑ᭄ⱘ㸠Ўˈ಴ℸˈᇍ↣Ͼヺড়䆹ᷛޚ䗮ᐌ䛑ৃҹՓ⫼DŽԚᰃˈANSI ᷛ ᑣߑ᭄೼ C 䇁㿔⿟ᑣЁޚᑧߑ᭄ЁϔϾ᳝⫼ⱘߑ᭄㗠Ꮖˈ䖭ѯᷛޚࡳ㛑DŽprintf ҙҙᰃᷛ 乎֓ᣛߎˈprintf ߑ᭄ᑊϡᰃ C 䇁㿔ᴀ䑿ⱘϔ䚼ߚˈC 䇁㿔ᴀ䑿ᑊ≵᳝ᅮН䕧ܹˋ䕧ߎ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! #include ⏽ᑺ䕀ᤶ⿟ᑣⱘࡳ㛑˖ ᇍѢᶤϾ⡍ᅮӏࡵ៥Ӏৃҹ䞛⫼໮⾡ᮍ⊩ᴹ㓪ݭ⿟ᑣDŽϟ䴶䖭↉ҷⷕгৃҹᅲ⦄ࠡ䴶ⱘ 1.3. for䇁হ 㒗д 1•4 㓪ݭϔϾ⿟ᑣᠧॄᨘ⇣⏽ᑺ䕀ᤶЎⳌᑨढ⇣⏽ᑺⱘ䕀ᤶ㸼DŽ 㒗д 1•3 ׂᬍ⏽ᑺ䕀ᤶ⿟ᑣˈՓП㛑೼䕀ᤶ㸼ⱘ乊䚼ᠧॄϔϾᷛ乬DŽ 㸼⼎ᄫヺ˗%s 㸼⼎ᄫヺІ˗%%㸼⼎ⱒߚো˄%˅ᴀ䑿DŽ ℸ໪ˈprintf ߑ᭄䖬ᬃᣕϟ߫Ḑᓣ䇈ᯢ˖%o 㸼⼎ܿ䖯ࠊ᭄˗%x 㸼⼎क݁䖯ࠊ᭄˗%c %6.2f ᣝ✻⍂⚍᭄ᠧॄˈ㟇ᇥ 6 Ͼᄫヺᆑˈᇣ᭄⚍ৢ᳝ϸԡᇣ᭄ %.2f ᣝ✻⍂⚍᭄ᠧॄˈᇣ᭄⚍ৢ᳝ϸԡᇣ᭄ %6f ᣝ✻⍂⚍᭄ᠧॄˈ㟇ᇥ 6 Ͼᄫヺᆑ %f ᣝ✻⍂⚍᭄ᠧॄ %6d ᣝ✻क䖯ࠊᭈൟ᭄ᠧॄˈ㟇ᇥ 6 Ͼᄫヺᆑ %d ᣝ✻क䖯ࠊᭈൟ᭄ᠧॄ ॄ䆹᭄DŽ ᣛᅮᕙᠧॄⱘ⍂⚍᭄ⱘᇣ᭄⚍ৢ᳝ϸԡᇣ᭄ˈԚᆑᑺ≵᳝䰤ࠊ˗%f ߭ҙҙ㽕∖ᣝ✻⍂⚍᭄ᠧ Ḑᓣ䇈ᯢৃҹⳕ⬹ᆑᑺϢ㊒ᑺˈ՟བˈ%6f 㸼⼎ᕙᠧॄⱘ⍂⚍᭄㟇ᇥ᳝ 6 Ͼᄫヺᆑ˗%.2f ... 40 4.4 20 •6.7 0 •17.8 ᭄⚍ৢ䴶᳝ 1 ԡ᭄ᄫDŽ݊䕧ߎབϟ᠔⼎˖ ᏺᇣ᭄⚍੠ᇣ᭄䚼ߚ˗%6.1f 㸼ᯢ঺ϔϾᕙᠧॄⱘ᭄˄celsius˅㟇ᇥऴ 6 ϾᄫヺᆑˈϨᇣ printf Ёⱘ䕀ᤶ䇈ᯢ%3.0f 㸼ᯢᕙᠧॄⱘ⍂⚍᭄˄े fahr˅㟇ᇥऴ 3 ϾᄫヺᆑˈϨϡ ᭄DŽ ᡞ int ㉏ൟⱘ᪡԰᭄䕀ᤶЎ float ㉏ൟⱘ᪡԰ܜг䛑ᰃᣝ✻䖭⾡ᮍᓣᠻ㸠ⱘˈे೼䖤ㅫПࠡ while (fahr <= upper) Ϣᴵӊ⌟䆩䇁হ fahr = lower; ㄀ 2 ゴᇚ䆺㒚ҟ㒡ᡞᭈൟ᭄䕀ᤶЎ⍂⚍ൟ᭄ⱘ㾘߭DŽ೼䖭䞠䳔㽕⊼ᛣˈ䌟ؐ䇁হ ҹᔎ䇗݊⍂⚍ᗻ䋼ˈ֓Ѣ䯙䇏DŽ ϡ䖛ˈेՓ⍂⚍ᐌ䞣পⱘᰃᭈൟؐˈ೼кݭᯊ᳔ད䖬ᰃЎᅗࡴϞϔϾᰒᓣⱘᇣ᭄⚍ˈ䖭ḋৃ ⚍ൟDŽ՟བˈ೼㸼䖒ᓣ fahr – 32 Ёˈ32 ೼䖤ㅫ䖛⿟Ёᇚ㹿㞾ࡼ䕀ᤶЎ⍂⚍᭄ݡখϢ䖤ㅫDŽ ㅫヺ᳝ϔϾ⍂⚍ൟ᪡԰᭄੠ϔϾᭈൟ᪡԰᭄ˈ߭೼ᓔྟ䖤ㅫПࠡᭈൟ᪡԰᭄ᇚӮ㹿䕀ᤶЎ⍂ བᵰᶤϾㅫᴃ䖤ㅫヺⱘ᠔᳝᪡԰᭄ഛЎᭈൟˈ߭ᠻ㸠ᭈൟ䖤ㅫDŽԚᰃˈབᵰᶤϾㅫᴃ䖤 ᯢ䆹ᐌ᭄ᰃϔϾ⍂⚍᭄ˈ಴ℸˈ5.0 / 9.0 ᰃϸϾ⍂⚍᭄Ⳍ䰸ˈ㒧ᵰᇚϡ㹿㟡ԡDŽ ᰃ಴Ўᣝᭈൟ䰸⊩ⱘ䅵ㅫ㾘߭ˈᅗӀⳌ䰸ᑊ㟡ԡৢᕫࠄⱘ㒧ᵰЎ 0DŽԚᰃˈᐌ᭄Ёⱘᇣ᭄⚍㸼 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! Ҹৃҹᡞヺোৡ˄៪⿄Ўヺোᐌ䞣˅ᅮНЎϔϾ⡍ᅮⱘᄫヺІ˖ ⿟ᑣⱘׂᬍবᕫ᳈ࡴೄ䲒DŽ໘⧚䖭⾡ᑏ᭄ⱘϔ⾡ᮍ⊩ᰃ䌟ќᅗӀ᳝ᛣНⱘৡᄫDŽ#define ᣛ ⱘĀᑏ᭄āᑊϡᰃϔϾདдᛃˈᅗӀ޴Т᮴⊩৥ҹৢ䯙䇏䆹⿟ᑣⱘҎᦤկҔМֵᙃˈ㗠ϨՓ ೼㒧ᴳ䅼䆎⏽ᑺ䕀ᤶ⿟ᑣࠡˈ៥Ӏݡᴹⳟϔϟヺোᐌ䞣DŽ೼⿟ᑣЁՓ⫼ 300ǃ20 ㄝ㉏Ԑ 1.4. ヺোᐌ䞣 ᑺ䕀ᤶ㸼DŽ 㒗д 1•5 ׂᬍ⏽ᑺ䕀ᤶ⿟ᑣˈ㽕∖ҹ䗚ᑣ˄ेᣝ✻Ң 300 ᑺࠄ 0 ᑺⱘ乎ᑣ˅ᠧॄ⏽ DŽޥᇚᕾ⦃᥻ࠊ䇁হ䲚Ёᬒ೼ϔ䍋ˈϨ↨ while 䇁হ᳈㋻ ⾡᳈⏙᱄DŽfor 䇁হ↨䕗䗖ড়߱ྟ࣪੠๲ࡴℹ䭓䛑ᰃऩᴵ䇁হᑊϨ䘏䕥Ⳍ݇ⱘᚙᔶˈ಴Ўᅗ ೼ᅲ䰙㓪⿟䖛⿟Ёˈৃҹ䗝ᢽ whi1e Ϣ for Ёⱘӏᛣϔ⾡ᕾ⦃䇁হˈЏ㽕㽕ⳟՓ⫼ાϔ ߚ˄㄀ϝ䚼ߚ˅䛑ৃҹᰃӏԩ㸼䖒ᓣDŽ ⫼㢅ᣀোᣀ䍋ᴹⱘϔ㒘䇁হDŽ߱ྟ࣪䚼ߚ˄㄀ϔ䚼ߚ˅ǃᴵӊ䚼ߚ˄㄀Ѡ䚼ߚ˅Ϣ๲ࡴℹ䭓䚼 ᕾ⦃ᇚ㒜ℶᠻ㸠DŽϢ while 䇁হϔḋˈfor ᕾ⦃䇁হⱘᕾ⦃ԧৃҹা᳝ϔᴵ䇁হˈгৃҹᰃ faise˅ˈ˄؛ҹᇚᕾ⦃ব䞣 fahr ๲ࡴϔϾℹ䭓ˈᑊݡ⃵ᇍᴵӊ∖ؐDŽབᵰ䅵ㅫᕫࠄⱘᴵӊؐЎ fahr = fahr + 20 ᕾ⦃ԧ˄ᴀ՟Ёⱘᕾ⦃ԧҙࣙ৿ϔϾ printf ߑ᭄䇗⫼䇁হ˅DŽℸৢᇚᠻ㸠㄀ϝ䚼ߚ ᰃ᥻ࠊᕾ⦃ⱘ⌟䆩៪ᴵӊ䚼ߚDŽᕾ⦃᥻ࠊᇚᇍ䆹ᴵӊ∖ؐˈབᵰ㒧ᵰؐЎⳳ˄true˅ˈ߭ᠻ㸠 fahr <= 300 ᰃ߱ྟ࣪䚼ߚˈҙ೼䖯ܹᕾ⦃ࠡᠻ㸠ϔ⃵DŽ㄀Ѡ䚼ߚ fahr = 0 П䯈⫼ߚো䱨ᓔDŽ㄀ϔ䚼ߚ while 䇁হ↨䕗ˈህӮথ⦄ for 䇁হⱘ᪡԰᳈Ⳉ㾖ϔѯDŽ೚ᣀোЁ݅ࣙ৿ 3 Ͼ䚼ߚˈ৘䚼ߚ for 䇁হᰃϔ⾡ᕾ⦃䇁হˈᅗᰃᇍ while 䇁হⱘ᥼ᑓDŽབᵰᇚ for 䇁হϢࠡ䴶ҟ㒡ⱘ ᰃϢ%6.1f ऍ䜡ⱘ⍂⚍ؐˈ᠔ҹৃҹ೼ℸ໘Փ⫼ӏԩ⍂⚍㸼䖒ᓣDŽ ؐⱘӏԩഎড়ˈ䛑ৃҹՓ⫼䆹㉏ൟⱘ᳈໡ᴖⱘ㸼䖒ᓣDŽ಴Ў printf ߑ᭄ⱘ㄀ϝϾখ᭄ᖙ乏 䆌Փ⫼ᶤ⾡㉏ൟব䞣ܕҹϞ޴⚍ᬍ䖯Ёⱘ᳔ৢϔ⚍ᰃ C 䇁㿔ЁϔϾ䗮⫼㾘߭ⱘᅲ՟˖೼ খ᭄ˈᅗϡݡᰃϔϾऩ⣀ⱘ䌟ؐ䇁হDŽ ⱘϟ䰤ǃϞ䰤੠ℹ䭓䛑ᰃᐌ䞣ˈ㗠䅵ㅫᨘ⇣⏽ᑺⱘ㸼䖒ᓣ⦄೼ব៤њ printf ߑ᭄ⱘ㄀ϝϾ এᥝњ໻䚼ߚব䞣ˈ㗠াՓ⫼њϔϾ int ㉏ൟⱘব䞣 fahrDŽ೼ᮄᓩܹⱘ for 䇁হЁˈ⏽ᑺ 䖭Ͼ⿟ᑣϢϞ㡖Ёҟ㒡ⱘ⿟ᑣᠻ㸠㒧ᵰⳌৠˈԚ⿟ᑣᴀ䑿ै᳝᠔ϡৠDŽ᳔Џ㽕ⱘᬍ䖯೼Ѣᅗ } printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr•32)); for (fahr = 0; fahr <= 300; fahr = fahr + 20) int fahr; { main() ᠧॄढ⇣⏽ᑺ—ᨘ⇣⏽ᑺᇍ✻㸼*/*/ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䖭ϸϾߑ᭄ৃҹѸ᳓䇗⫼ˈ䕧ߎⱘ⃵ᑣϢ䇗⫼ⱘ⃵ᑣϔ㟈DŽ ᇚᡞᭈൟব䞣 c ⱘݙᆍҹᄫヺⱘᔶᓣᠧॄߎᴹˈ䗮ᐌᰃᰒ⼎೼ሣᐩϞDŽputchar Ϣ printf putchar() ↣⃵䇗⫼ putchar ߑ᭄ᯊᇚᠧॄϔϾᄫヺDŽ՟བˈ䇁হ ӊ䕧ܹᄫヺⱘᮍ⊩ˈ៥Ӏᇚ೼㄀ 7 ゴЁ䅼䆎DŽ Пৢˈব䞣 c Ёᇚࣙ৿䕧ܹ⌕ЁⱘϟϔϾᄫヺDŽ䖭⾡ᄫヺ䗮ᐌᰃ䗮䖛䬂Ⲭ䕧ܹⱘDŽ݇ѢҢ᭛ c = getchar() гህᰃ䇈ˈ೼ᠻ㸠䇁হ ߑ᭄DŽ↣⃵䇗⫼ᯊˈgetchar ߑ᭄Ң᭛ᴀ⌕Ё䇏ܹϟϔϾ䕧ܹᄫヺˈᑊᇚ݊԰Ў㒧ᵰؐ䖨ಲDŽ ᑧᦤկњϔ⃵䇏ˋݭϔϾᄫヺⱘߑ᭄ˈ݊Ё᳔ㅔऩⱘᰃ getchar ੠ putchar ϸϾޚᷛ ᑧⱘ C 䇁㿔⿟ᑣਬϡᖙ݇ᖗ೼⿟ᑣП໪䖭ѯ㸠ᰃབԩ㸼⼎ⱘDŽޚൟDŽՓ⫼ᷛ ᑧ䋳䋷Փ↣Ͼ䕧ܹˋ䕧ߎ⌕䛑㛑໳䙉ᅜ䖭ϔ῵ޚϾ៪໮Ͼᄫヺ㒘៤ˈ㸠᳿ᰃϔϾᤶ㸠ヺDŽᷛ 䕧ߎ䛑ᰃᣝ✻ᄫヺ⌕ⱘᮍᓣ໘⧚DŽ᭛ᴀ⌕ᰃ⬅໮㸠ᄫヺᵘ៤ⱘᄫヺᑣ߫ˈ㗠↣㸠ᄫヺ߭⬅ 0 ᑧᦤկⱘ䕧ܹˋ䕧ߎ῵ൟ䴲ᐌㅔऩDŽ᮴䆎᭛ᴀҢԩ໘䕧ܹˈ䕧ߎࠄԩ໘ˈ݊䕧ܹˋޚᷛ ᴀ㗠ᏆDŽ⠜ܙ䞠᠔䅼䆎ⱘ⿟ᑣॳൟⱘᠽ ᥹ϟᴹ៥Ӏⳟϔ㒘Ϣᄫヺൟ᭄᥂໘⧚᳝݇ⱘ⿟ᑣDŽ䇏㗙ᇚӮথ⦄ˈ䆌໮⿟ᑣাϡ䖛ᰃ䖭 1.5. ᄫヺ䕧ܹˋ䕧ߎ #define ᣛҸ㸠ⱘ᳿ሒ≵᳝ߚোDŽ ᐌ䞣ৡ䗮ᐌ⫼໻ݭᄫ↡ᣐݭˈ䖭ḋৃҹᕜᆍᯧϢ⫼ᇣݭᄫ↡ᣐݭⱘব䞣ৡⳌऎ߿DŽ⊼ᛣˈ ݊ЁˈLOWERǃUPPER Ϣ STEP 䛑ᰃヺোᐌ䞣ˈ㗠䴲ব䞣ˈ಴ℸϡ䳔㽕ߎ⦄೼ໄᯢЁDŽヺো } printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr•32)); for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP) int fahr; { main() /* print Fahrenheit•Celsius table */ #define STEP 20 /* step size */ #define UPPER 300 /* upper limit */ #define LOWER 0 /* lower limit of table */ #include Ӏ䛑ᰃҹᄫ↡ᠧ༈ⱘᄫ↡੠᭄ᄫᑣ߫˗᳓ᤶ᭛ᴀৃҹᰃӏԩᄫヺᑣ߫ˈ㗠ϡҙ䰤Ѣ᭄ᄫDŽ ݊ᅗৡᄫⱘϔ䚼ߚ˅䛑ᇚ⫼Ⳍᑨⱘ᳓ᤶ᭛ᴀ᳓ᤶDŽ݊ЁˈৡᄫϢ᱂䗮ব䞣ৡⱘᔶᓣⳌৠ˖ᅗ ೼䆹ᅮНПৢˈ⿟ᑣЁߎ⦄ⱘ᠔᳝೼#define ЁᅮНⱘৡᄫ˄᮶≵᳝⫼ᓩোᓩ䍋ᴹˈгϡᰃ define ৡᄫ ᳓ᤶ᭛ᴀ# w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! #include 䆩䚼ߚЁˈϞ䗄ᄫヺ໡ࠊ⿟ᑣ֓ৃҹᬍݭ៤ϟ߫ᔶᓣ˖ 䌟ؐৃҹ԰Ў᳈໻ⱘ㸼䖒ᓣⱘϔ䚼ߚߎ⦄DŽབᵰᇚЎ c 䌟ؐⱘ᪡԰ᬒ೼ while ᕾ⦃䇁হⱘ⌟ П㉏ⱘ䌟ؐ᪡԰ᰃϔϾ㸼䖒ᓣˈᑊϨ݋᳝ϔϾؐˈे䌟ؐৢᎺ䖍ব䞣ֱᄬⱘؐDŽгህᰃ䇈ˈ c = getchar() 䇁㿔Ёˈ㉏ԐѢ ᇍѢ㒣偠↨䕗Єᆠⱘ C 䇁㿔⿟ᑣਬˈৃҹᡞ䖭Ͼᄫヺ໡ࠊ⿟ᑣ㓪ݭᕫ᳈㊒⚐ϔѯDŽ೼ C ӏԩ⡍ᅮⱘ᭄ؐDŽ ӏԩ char ㉏ൟⱘؐ䛑ϡⳌৠेৃDŽ䖭䞠Փ⫼ヺোᐌ䞣ˈৃҹ⹂ֱ⿟ᑣϡ䳔㽕ձ䌪Ѣ݊ᇍᑨⱘ EOF ᅮН೼༈᭛ӊЁˈᰃϾᭈൟ᭄ˈ݊݋ԧ᭄ؐᰃҔМᑊϡ䞡㽕ˈা㽕ᅗϢ ᭛ӊ㒧ᴳヺ EOFDŽ಴ℸˈ៥Ӏᇚ c ໄᯢ៤ int ㉏ൟDŽټӏԩৃ㛑ⱘᄫヺ໪䖬㽕㛑ᄬټ䰸њ㛑ᄬ ᬒ getchar ߑ᭄䖨ಲⱘӏԩؐDŽ䖭䞠П᠔ҹϡᡞ c ໄᯢ៤ char ㉏ൟˈᰃ಴Ўᅗᖙ乏䎇໳໻ˈ ؐ⿄Ў EOF˄end of fileˈ᭛ӊ㒧ᴳ˅DŽ៥Ӏ೼ໄᯢব䞣 c ⱘᯊ׭ˈᖙ乏䅽ᅗ໻ࠄ䎇ҹᄬ ೼≵᳝䕧ܹᯊˈgetchar ߑ᭄ᇚ䖨ಲϔϾ⡍⅞ؐˈ䖭Ͼ⡍⅞ؐϢӏԩᅲ䰙ᄫヺ䛑ϡৠDŽ䖭Ͼ ᮍ⊩ᰃ˖އབԩऎߚ᭛ӊЁ᳝ᬜ᭄᥂Ϣ䕧ܹ㒧ᴳヺⱘ䯂乬DŽC 䇁㿔䞛পⱘ㾷އ䖭䞠䳔㽕㾷 ヺൟ᭄᥂DŽ಴Ўᶤѯ┰೼ⱘ䞡㽕ॳ಴ˈ៥Ӏ೼ℸՓ⫼ int ㉏ൟDŽ ᄫټ䖭⾡ᄫヺൟ᭄᥂ˈᔧ✊ӏԩᭈൟ˄int˅гৃҹ⫼ѢᄬټⱘDŽchar ㉏ൟϧ䮼⫼Ѣᄬټᄬ ᄫヺ೼䬂Ⲭǃሣᐩ៪݊ᅗⱘӏԩഄᮍ᮴䆎ҹҔМᔶᓣ㸼⦄ˈᅗ೼ᴎ఼ݙ䚼䛑ᰃҹԡ῵ᓣ ݊Ёˈ݇㋏䖤ㅫヺ!=㸼⼎ĀϡㄝѢāDŽ } } c = getchar(); putchar(c); while (c != EOF) { c = getchar(); int c; { main() /* copy input to output; 1st version */ #include ᇚϞ䗄෎ᴀᗱᛇ䕀ᤶЎ C 䇁㿔⿟ᑣЎ˖ ՠຏྡྷّᆓ׸ ՠదԅᆓ׸ذ೛ѻ while (؞ᆓ׸Ϣಾำߑࠒ೯ᄗವ׸) ՠྡྷّᆓ׸ བϟ˖ ᭄䞣᚞Ҏⱘ᳝⫼ⱘҷⷕDŽ᳔ㅔऩⱘ՟ᄤህᰃᡞ䕧ܹϔ⃵ϔϾᄫヺഄ໡ࠊࠄ䕧ߎˈ݊෎ᴀᗱᛇ ϟ㓪ݭߎމࡽѢ getchar Ϣ putchar ߑ᭄ˈৃҹ೼ϡњ㾷݊ᅗ䕧ܹˋ䕧ߎⶹ䆚ⱘᚙ׳ ᭛ӊ໡ࠊ .1.5.1 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ݊Ёˈ䇁হ } printf("%ld\n", nc); ++nc; while (getchar() != EOF) nc = 0; long nc; { main() /* count characters in input; 1st version */ #include ϟ߫⿟ᑣ⫼Ѣᇍᄫヺ䖯㸠䅵᭄ˈᅗϢϞ䴶ⱘ໡ࠊ⿟ᑣ㉏ԐDŽ 1.5.2. ᄫヺ䅵᭄ 㒗д 1•7 㓪ݭϔϾᠧॄ EOF ؐⱘ⿟ᑣDŽ 㒗д 1•6 偠䆕㸼䖒ᓣ getchar() != EOF ⱘؐᰃ 0 䖬ᰃ 1DŽ 䖭ᑊϡᰃ៥Ӏ᠔Ꮰᳯⱘ㒧ᵰ˄᳈䆺㒚ⱘݙᆍˈ䇋খ㾕㄀ 2 ゴⱘⳌ݇䚼ߚ˅DŽ Ѣ䇗⫼ getchar ߑ᭄ᯊᰃ৺⺄ࠄ᭛ӊ㒧ᴳᷛᖫ˅ˈއ䆹䇁হᠻ㸠ৢˈc ⱘؐᇚ㹿㕂Ў 0 ៪ 1˄প c = (getchar() != EOF) ㄝӋѢ䇁হ c = getchar() != EOF ᪡԰Пࠡᠻ㸠DŽ಴ℸ䇁হ ϟ݇㋏⌟䆩!=ᇚ೼䌟ؐ=މ㑻㽕催ˈ䖭ḋˈ೼ϡՓ⫼೚ᣀোⱘᚙܜ㑻↨䌟ؐ䖤ㅫヺ=ⱘӬܜӬ ᇍ while 䇁হⱘᴵӊ䚼ߚᴹ䇈ˈ䌟ؐ㸼䖒ᓣϸ䖍ⱘ೚ᣀোϡ㛑ⳕ⬹DŽϡㄝѢ䖤ㅫヺ!=ⱘ DŽ˅މ䖭⾡ᚙܡ㛑Ӯᕜ䲒⧚㾷ˈᑨሑ䞣䙓 ៥Ӏ㒣ᐌӮⳟࠄ䖭⾡亢ḐDŽ˄ϡ䖛ˈབᵰ៥Ӏ䖛໮ഄՓ⫼䖭⾡㉏ൟⱘ໡ᴖ䇁হˈ㓪ݭⱘ⿟ᑣৃ DŽдᛃ䖭⾡亢Ḑৢˈ䇏㗙ህӮথ⦄ᣝ✻䖭⾡ᮍᓣ㓪ݭⱘ⿟ᑣ᳈ᯧ䯙䇏DŽޥᭈϾ⿟ᑣⳟ䍋ᴹ᳈㋻ ҹϞ䖭↉⿟ᑣᇚ䕧ܹ䲚Ё࣪ˈgetchar ߑ᭄೼⿟ᑣЁাߎ⦄њϔ⃵ˈ䖭ḋህ㓽ⷁњ⿟ᑣˈ ᭄ᠻ㸠㒧ᴳDŽ ᠻ㸠 while 䇁হDŽᔧࠄ䖒䕧ܹⱘ㒧ሒԡ㕂ᯊˈwhile ᕾ⦃䇁হ㒜ℶᠻ㸠ˈҢ㗠ᭈϾ main ߑ 㒧ᴳᷛᖫDŽབᵰ䆹ᄫヺϡᰃ᭛ӊ㒧ᴳᷛᖫˈ߭ᠻ㸠 while 䇁হԧˈᑊᠧॄ䆹ᄫヺDŽ䱣ৢ䞡໡ 䇏ϔϾᄫヺᑊᇚ݊䌟ؐ㒭 cˈ✊ৢ⌟䆩䆹ᄫヺᰃ৺Ў᭛ӊܜ೼䆹⿟ᑣЁˈwhile ᕾ⦃䇁হ佪 } putchar(c); while ((c = getchar()) != EOF) int c; { main() copy input to output; 2nd version */ */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! #include ҹ㸠ᑣ߫ⱘᔶᓣߎ⦄ˈ↣ϔ㸠ഛҹᤶ㸠ヺ㒧ᴳDŽ಴ℸˈ㒳䅵㸠᭄ㄝӋѢ㒳䅵ᤶ㸠ヺⱘϾ᭄DŽ ᑧֱ䆕䕧ܹ᭛ᴀ⌕ޚ᥹ϟᴹⱘ䖭Ͼ⿟ᑣ⫼Ѣ㒳䅵䕧ܹЁⱘ㸠᭄DŽ៥Ӏ೼Ϟ䴶ᦤࠄ䖛ˈᷛ 1.5.3. 㸠䅵᭄ ߎ⦄䖍⬠ᴵӊᯊˈwhile 䇁হϢ for 䇁হ᳝ࡽѢ⹂ֱ⿟ᑣᠻ㸠ড়⧚ⱘ᪡԰DŽ DŽ೼ߎ⦄ 0 䭓ᑺⱘ䕧ܹᯊˈ⿟ᑣⱘ໘⧚ᑨ䆹♉⌏ϔѯˈ೼މ㛑ߎ⦄ᕾ⦃ԧϔ⃵䛑ϡᠻ㸠ⱘᚙ Пϔህᰃ೼ᠻ㸠ᕾ⦃ԧПࠡህᇍᴵӊ䖯㸠⌟䆩ˈབᵰᴵӊϡ⒵䎇ˈ߭ϡᠻ㸠ᕾ⦃ԧˈ䖭ህৃ ⿟ᑣⱘᠻ㸠㒧ᵰᇚЎ 0ˈ䖭гᰃℷ⹂ⱘ㒧ᵰDŽ䖭ϔ⚍ᕜ䞡㽕DŽwhi1e 䇁হϢ for 䇁হⱘӬ⚍ ˈ؛㄀ϔ⃵䇗⫼ getchar ߑ᭄ⱘি׭ˈwhile 䇁হ៪ for 䇁হЁⱘᴵӊ⌟䆩ҢϔᓔྟህЎ བᵰ䕧ܹЁϡࣙ৿ᄫヺˈ䙷Мˈ೼˖މ೼㒧ᴳ䅼䆎ᄫヺ䅵᭄⿟ᑣПࠡˈ៥Ӏ㗗㰥ҹϟᚙ ᬒ೼ϔ㸠ᰃЎњ᳈ࡴ䝦ⳂDŽ ⫼ऩ⣀ⱘߚোҷ᳓DŽऩ⣀ⱘߚো⿄Ўぎ䇁হˈᅗℷད㛑⒵䎇 for 䇁হⱘ䖭ϔ㽕∖DŽᡞᅗऩ⣀ ߚϢ๲ࡴℹ䭓䚼ߚᅠ៤њDŽԚ C 䇁㿔ⱘ䇁⊩㾘߭㽕∖ for ᕾ⦃䇁হᖙ乏᳝ϔϾᕾ⦃ԧˈ಴ℸ ೼䆹⿟ᑣ↉Ёˈfor ᕾ⦃䇁হⱘᕾ⦃ԧᰃぎⱘˈ䖭ᰃ಴Ў᠔᳝Ꮉ԰䛑೼⌟䆩˄ᴵӊ˅䚼 ᇣ᭄䚼ߚˈ಴ℸᇣ᭄䚼ߚⱘԡ᭄Ў 0DŽ ᇍѢ float Ϣ double ㉏ൟDŽprintf ߑ᭄䛑Փ⫼%f 䖯㸠䇈ᯢDŽ%.0f ᔎࠊϡᠧॄᇣ᭄⚍੠ } printf("%.0f\n", nc); ; for (nc = 0; gechar() != EOF; ++nc) double nc; { main() /* count characters in input; 2nd version */ #include ⦃䇁হˈ㗠⫼ for ᕾ⦃䇁হᴹሩ⼎㓪ݭℸᕾ⦃ⱘ঺ϔ⾡ᮍ⊩˖ Փ⫼ double˄ঠ㊒ᑺ⍂⚍᭄˅㉏ൟৃҹ໘⧚᳈໻ⱘ᭄ᄫDŽ៥Ӏ೼䖭䞠ϡՓ⫼ while ᕾ ᭄ᰃ long ᭈൟDŽ ᔧᇣⱘ䕧ܹ䛑ৃ㛑Փ int ㉏ൟⱘ䅵᭄ব䞣⑶ߎDŽ䕀ᤶ䇈ᯢ%ld ਞ䆝 printf ߑ᭄݊ᇍᑨⱘখ ⱘ䭓ᑺ˄᳔໻ؐЎ 32767˅ˈ䖭ḋˈⳌܗऩټԚ೼ϔѯᴎ఼Ϟˈint ㉏ൟⱘؐৃ㛑া᳝ 16 ԡᄬ DŽ೼ᶤѯᴎ఼Ϟ int Ϣ long ㉏ൟⱘ䭓ᑺⳌৠˈܗऩټᭈൟ᭄˄䭓ᭈൟ˅㟇ᇥ㽕ऴ⫼ 32 ԡᄬ 䆹ᄫヺ䅵᭄⿟ᑣՓ⫼ long ㉏ൟⱘব䞣ᄬᬒ䅵᭄ؐˈ㗠≵᳝Փ⫼ int ㉏ൟⱘব䞣DŽlong Ⳃࠡˈ៥ӀাՓ⫼ࠡ㓔ᔶᓣDŽ ㄀ 2 ゴЁᇚⳟࠄˈ䖭ϸ⾡ᔶᓣ೼㸼䖒ᓣЁ݋᳝ϡৠⱘؐˈԚ++nc Ϣ nc++䛑Փ nc ⱘؐ๲ࡴ 1DŽ ϸϾ䖤ㅫヺ᮶ৃҹ԰Ўࠡ㓔䖤ㅫヺ˄བ++nc˅ˈгৃҹ԰Ўৢ㓔䖤ㅫヺ˄བ nc++˅DŽ៥Ӏ೼ 䖤ㅫヺ••DŽ++Ϣ••䖭ޣ䇁হ++nc ᳈㊒⚐ϔѯˈϨ䗮ᐌᬜ⥛г᳈催DŽϢ䆹䖤ㅫヺⳌᑨⱘᰃ㞾 ᓩܹњϔϾᮄⱘ䖤ㅫヺ++ˈ݊ࡳ㛑ᰃᠻ㸠ࡴ 1 ᪡԰DŽৃҹ⫼䇁হ nc = nc + 1 ҷ᳓ᅗˈԚ nc;++ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! #define OUT 0 /* outside a word */ #define IN 1 /* inside a word */ #include Ё wc ⿟ᑣⱘ偼ᑆ䚼ߚ˖ ᆑᵒˈᅗᰃӏԩ݊Ёϡࣙ৿ぎḐǃࠊ㸼ヺ៪ᤶ㸠ヺⱘᄫヺᑣ߫DŽϟ䴶䖭↉⿟ᑣᰃ UNIX ㋏㒳 ៥Ӏᇚҟ㒡ⱘ㄀ 4 Ͼᅲ⫼⿟ᑣ⫼Ѣ㒳䅵㸠᭄ǃऩ䆡᭄Ϣᄫヺ᭄DŽ䖭䞠ᇍऩ䆡ⱘᅮН↨䕗 1.5.4. ऩ䆡䅵᭄ ᳓ᤶЎ\bˈᡞড᭰ᴴ᳓ᣝЎ\\DŽ䖭ḋৃҹᇚࠊ㸼ヺ੠ಲ䗔ヺҹৃ㾕ⱘᮍᓣᰒ⼎ߎᴹDŽ 㒗д 1•10 㓪ݭϔϾᇚ䕧ܹ໡ࠊࠄ䕧ߎⱘ⿟ᑣˈᑊᇚ݊Ёⱘࠊ㸼ヺ᳓ᤶЎ\tˈᡞಲ䗔ヺ ᳓DŽ 㒗д 1•9 㓪ݭϔϾᇚ䕧ܹ໡ࠊࠄ䕧ߎⱘ⿟ᑣˈᑊᇚ݊Ё䖲㓁ⱘ໮ϾぎḐ⫼ϔϾぎḐҷ 㒗д 1•8 㓪ݭϔϾ㒳䅵ぎḐǃࠊ㸼ヺϢᤶ㸠ヺϾ᭄ⱘ⿟ᑣDŽ ៥Ӏᇚ೼㄀ 2 ゴ䖯ϔℹ䅼䆎DŽ ᭈൟ᭄㗠Ꮖ˗㗠"\n"ᰃϔϾҙࣙ৿ϔϾᄫヺⱘᄫヺІᐌ䞣DŽ᳝݇ᄫヺІϢᄫヺП䯈ⱘ݇㋏ˈ ೼ ASCII ᄫヺ䲚Ё݊ؐЎ 10DŽ៥Ӏᑨᔧ⊼ᛣࠄˈ'\n'ᰃऩϾᄫヺˈ೼㸼䖒ᓣЁᅗϡ䖛ᰃϔϾ ᄫヺІᐌ䞣ЁՓ⫼ⱘ䕀Нᄫヺᑣ߫гᰃড়⊩ⱘᄫヺᐌ䞣ˈ↨བˈ'\n'ҷ㸼ᤶ㸠ヺⱘؐˈ ЎDŽ'A'ⱘᛣН᳈⏙ἮˈϨϢ⡍ᅮⱘᄫヺ䲚᮴݇DŽ ೼ ASCII ᄫヺ䲚Ё݊ؐЎ 65˄ेᄫヺ A ⱘݙ䚼㸼⼎ؐЎ 65˅DŽᔧ✊ˈ⫼'A'㽕↨⫼ 65 དˈ಴ ПЎᄫヺᐌ䞣DŽԚᰃˈᅗাϡ䖛ᰃᇣⱘᭈൟ᭄ⱘ঺ϔ⾡ݭ⊩㗠ᏆDŽ՟བˈ'A'ᰃϔϾᄫヺᐌ䞣˗ ऩᓩোЁⱘᄫヺ㸼⼎ϔϾᭈൟؐˈ䆹ؐㄝѢℸᄫヺ೼ᴎ఼ᄫヺ䲚Ёᇍᑨⱘ᭄ؐˈ៥Ӏ⿄ ড়⊩ⱘ㸼䖒ᓣˈ಴ℸ㋏㒳ϡӮ㒭ߎ䄺ਞֵᙃDŽ 㗙᳝ᯊӮ䫭䇃ഄݭ៤ऩㄝѢো=DŽ೼㄀ 2 ゴ៥ӀᇚⳟࠄˈेՓ䖭ḋ䇃⫼њˈ݊㒧ᵰ䗮ᐌҡ✊ᰃ ݇㋏ˈҹ⼎ऎߚDŽ䖭䞠ᦤ䝦⊼ᛣˈ೼㸼⼎ĀㄝѢā䘏䕥݇㋏ⱘᯊ׭˄ᑨ䆹⫼==˅ˈ C 䇁㿔߱ᄺ Ёⱘ.EQ.˅DŽ⬅Ѣ C 䇁㿔ᇚऩㄝѢো=԰Ў䌟ؐ䖤ㅫヺˈ಴ℸՓ⫼ঠㄝѢো==㸼⼎Ⳍㄝⱘ䘏䕥 ঠㄝѢো==ᰃ C 䇁㿔Ё㸼⼎ĀㄝѢā݇㋏ⱘ䖤ㅫヺ˄㉏ԐѢ Pascal ЁⱘऩㄝѢো=ঞ Fortran 䖭䞠ݡ⃵⫼㓽䖯ᮍᓣ㸼ᯢ䇁হП䯈ⱘ᥻ࠊ݇㋏DŽ 䆩೚ᣀোЁⱘᴵӊˈབᵰ䆹ᴵӊЎⳳˈ߭ᠻ㸠݊ৢⱘ䇁হ˄៪ᣀ೼㢅ᣀোЁⱘϔ㒘䇁হ˅DŽ⌟ܜ ೼䆹⿟ᑣЁˈwhile ᕾ⦃䇁হⱘᕾ⦃ԧᰃϔϾ if 䇁হˈᅗ᥻ࠊ㞾๲䇁হ++nlDŽif 䇁হ } printf("%d\n", nl); ++nl; if (c == '\n') while ((c = getchar()) != EOF) nl = 0; int c, nl; { main() count lines in input */ */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䇁হ 2 else 䇁হ 1 if (㸼䗄ᓣ) ԰DŽ݊ϔ㠀ᔶᓣЎ˖ ᯊ᠔㽕ᠻ㸠ⱘࡼ؛䖭↉⿟ᑣЁ䖬ࣙᣀϔϾ else 䚼ߚˈᅗᣛᅮᔧ if 䇁হЁⱘᴵӊ䚼ߚЎ ᳝ᖙ㽕њˈϡЙ៥ӀᇚӮⳟࠄ䖭⾡՟ᄤDŽ ህخϟ䖭ḋމϡᖙᠻ㸠ৢ䴶ϸϾ⌟䆩DŽ೼䖭䞠ˈ䖭ϔ⚍ᑊϡ⡍߿䞡㽕ˈԚ೼ᶤѯ᳈໡ᴖⱘᚙ ህゟे㒜ℶDŽབᵰ c ᰃぎḐˈ߭≵᳝ᖙ㽕ݡ⌟䆩ᅗᰃ৺Ўᤶ㸠ヺ៪ࠊ㸼ヺˈ䖭ḋህؐ∖ˈ؛ ⬅&&៪||䖲᥹ⱘ㸼䖒ᓣ⬅Ꮊ㟇ে∖ؐˈᑊֱ䆕೼∖ؐ䖛⿟Ёা㽕㛑໳߸ᮁ᳔㒜ⱘ㒧ᵰЎⳳ៪ 㑻DŽܜࠊ㸼ヺⱘৃ㾕㸼⼎ᔶᓣ˅DŽⳌᑨഄˈ䖤ㅫヺ&&ҷ㸼 AND˄䘏䕥Ϣ˅ˈᅗҙ↨||催ϔϾӬ ⱘᛣНᰃĀབᵰ c ᰃぎḐˈ៪ c ᰃᤶ㸠ヺˈ៪ c ᰃࠊ㸼ヺā˄ࠡ䴶䆆䖛ˈ䕀Нᄫヺᑣ߫\t ᰃ if (c == ' ' || c== '\n' || c == '\t') 䖤ㅫヺ||ҷ㸼 OR˄䘏䕥៪˅ˈ᠔ҹϟ߫䇁হ n1 = (nw = (nc = 0)); ೼ݐ᳝ؐϢ䌟ؐϸ⾡ࡳ㛑ⱘ㸼䖒ᓣЁˈ䌟ؐ㒧ড়⃵ᑣᰃ⬅ে㟇ᎺDŽ᠔ҹϞ䴶䖭ᴵ䇁হㄝৠѢ ᇚᡞ݊Ёⱘ 3 Ͼব䞣 nlǃnw Ϣ nc 䛑䆒㕂Ў 0DŽ䖭⾡⫼⊩ᕜᐌ㾕ˈԚ㽕⊼ᛣ䖭ḋϔϾџᅲ˖ nl = nw = nc = 0; ϟ߫䇁হ བᵰ⿟ᑣЁⱘᑏ᭄䛑ҹヺোᐌ䞣ⱘᔶᓣߎ⦄ˈᇍ⿟ᑣ䖯㸠໻䞣ׂᬍህӮⳌᇍᆍᯧᕫ໮DŽ ಴ℸ㗠๲ࡴⱘϔ⚍Ꮉ԰䞣Ϣᦤ催⿟ᑣৃ䇏ᗻᏺᴹⱘད໘Ⳍ↨ᰃؐᕫⱘDŽ䇏㗙гӮথ⦄ˈˈخ г䆌ⳟϡߎ᳝ҔМӬ࢓ˈԚ೼䕗໻ⱘ⿟ᑣЁˈབᵰҢϔᓔྟህ䖭ḋ⫣خ⾡䕗ᇣⱘ⿟ᑣЁˈ䖭 Ӏ೼䖭䞠Փ⫼њヺোᐌ䞣 IN Ϣ OUTˈ㗠≵᳝Փ⫼݊ᇍᑨⱘ᭄ؐ 1 Ϣ 0ˈ䖭ḋ⿟ᑣ᳈ᯧ䇏DŽ೼ 䆄ᔩ⿟ᑣᔧࠡᰃ৺ℷԡѢϔϾऩ䆡ПЁˈᅗⱘ߱ؐᰃĀϡ೼ऩ䆡Ёāˈे߱ؐ㹿䌟Ў OUTDŽ៥ ⿟ᑣᠻ㸠ᯊˈ↣ᔧ䘛ࠄऩ䆡ⱘ㄀ϔϾᄫヺˈᅗህ԰ЎϔϾᮄऩ䆡ࡴҹ㒳䅵DŽstate ব䞣 } printf("%d %d %d\n", nl, nw, nc); } } ++nw; state = IN; else if (state == OUT) { state = OUT; if (c == ' ' || c == '\n' || c = '\t') ++nl; if (c == '\n') ++nc; while ((c = getchar()) != EOF) { nl = nw = nc = 0; state = OUT; int c, nl, nw, nc, state; { main() count lines, words, and characters in input */ */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᇚব䞣 ndigit ໄᯢЎ⬅ 10 Ͼᭈൟ᭄ᵘ៤ⱘ᭄㒘DŽ೼ C 䇁㿔Ёˈ᭄㒘ϟᷛᘏᰃҢ 0 ᓔྟˈ಴ int ndigit[10]ˈ 䆹⿟ᑣЁⱘໄᯢ䇁হ digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345 ᔧᡞ䖭↉⿟ᑣᴀ䑿԰Ў䕧ܹᯊˈ䕧ߎ㒧ᵰЎ˖ } nwhite, nother); printf(", white space = %d, other = %d\n", printf(" %d", ndigit[i]); for (i = 0; i < 10; ++i) printf("digits ="); ++nother; else ++nwhite; else if (c == ' ' || c == '\n' || c == '\t') ++ndigit[c•'0']; if (c >= '0' && c <= '9') while ((c = getchar()) != EOF) ndigit[i] = 0; for (i = 0; i < 10; ++i) nwhite = nother = 0; int ndigit[10]; int c, i, nwhite, nother; { main() /* count digits, white space, others */ #include Փ⫼ 10 Ͼ⣀ゟⱘব䞣᳈ᮍ֓DŽϟ䴶ᰃ䆹⿟ᑣⱘϔ⾡⠜ᴀ˖ ᠔᳝ⱘ䕧ܹᄫヺৃҹߚ៤ 12 ㉏ˈ಴ℸৃҹ⫼ϔϾ᭄㒘ᄬᬒ৘Ͼ᭄ᄫߎ⦄ⱘ⃵᭄ˈ䖭ḋ↨ 䆹⿟ᑣ䅼䆎 C 䇁㿔໮ᮍ䴶ⱘ䯂乬DŽ ヺঞᤶ㸠ヺ˅ҹঞ᠔᳝݊ᅗᄫヺߎ⦄ⱘ⃵᭄DŽ䖭Ͼ⿟ᑣⱘᅲ⫼ᛣНᑊϡ໻ˈԚ៥Ӏৃҹ䗮䖛 ೼䖭䚼ߚݙᆍЁˈ៥Ӏᴹ㓪ݭϔϾ⿟ᑣˈҹ㒳䅵৘Ͼ᭄ᄫǃぎⱑヺ˄ࣙᣀぎḐヺǃࠊ㸼 1.6. ᭄㒘 㒗д 1•12 㓪ݭϔϾ⿟ᑣˈҹ↣㸠ϔϾऩ䆡ⱘᔶᓣᠧॄ݊䕧ܹDŽ ᳔ܹৃ㛑থ⦄䖭㉏䫭䇃ਸ਼’ ໛བԩ⌟䆩ऩ䆡䅵᭄⿟ᑣ˛བᵰ⿟ᑣЁᄬ೼ᶤ⾡䫭䇃ˈ䙷МҔМḋⱘ䕧ޚ㒗д 1•11 Դ োݙⱘϸᴵ䇁হDŽ ߫DŽ೼ऩ䆡䅵᭄⿟ᑣЁˈelse Пৢⱘ䇁হҡᰃϔϾ if 䇁হˈ䆹 if 䇁হ᥻ࠊњࣙ৿೼㢅ᣀ 䇁হ 1ˈ৺߭ᠻ㸠䇁হ 2DŽ䖭ϸᴵ䇁হ䛑᮶ৃҹᰃऩᴵ䇁হˈгৃҹᰃᣀ೼㢅ᣀোݙⱘ䇁হᑣ Ёˈif•else Ёⱘϸᴵ䇁হ᳝Ϩҙ᳝ϔᴵ䇁হ㹿ᠻ㸠DŽབᵰ㸼䖒ᓣⱘؐЎⳳˈ߭ᠻ㸠݊ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! DŽ៥Ӏᇚ೼ 3.4 㡖㒭މ㋴Ⳍऍ䜡ⱘᚙܗᅮᶤϾᭈൟ៪ᄫヺ㸼䖒ᓣᰃ৺ϢϔϾᐌ䞣䲚ড়ЁⱘᶤϾ ㄀ 3 ゴᇚ䅼䆎ⱘ switch 䇁হᦤկњ㓪ݭ໮䏃ߚᬃ⿟ᑣⱘ঺ϔ⾡ᮍᓣˈᅗ⡍߿䗖ড়Ѣ߸ 䴶ⱘে䖍⬠DŽ ৺߭ˈབᵰ↣Ͼ if 䛑↨ࠡϔϾ else ৥䞠㓽䖯ϔѯ䎱⾏ˈ䙷М䕗䭓ⱘ߸ᅮᑣ߫ህৃ㛑䍙ߎ义 ህ⿟ᑣ䆒䅵亢Ḑ㗠㿔ˈ៥Ӏᓎ䆂䇏㗙䞛⫼Ϟ䴶᠔⼎ⱘ㓽䖯Ḑᓣҹԧ⦄䆹㒧ᵘⱘሖ⃵݇㋏ˈ 䇁হ else if (ᴵӊ) ࡼ԰DŽ೼㄀ϔϾ if Ϣ᳔ৢϔϾ else П䯈ৃҹ᳝ 0 Ͼ៪໮Ͼϟ߫ᔶᓣⱘ䇁হᑣ߫˖ ㉏ԐѢࠡ䴶ⱘऩ䆡䅵᭄⿟ᑣˈབᵰ≵᳔᳝ৢϔϾ else ঞᇍᑨⱘ䇁হˈ䆹䇁হԧᇚϡᠻ㸠ӏԩ ᑆᴵ䇁হ˅DŽབᵰ᠔᳝ᴵӊ䛑ϡ⒵䎇ˈ߭ᠻ㸠ԡѢ᳔ৢϔϾ else Пৢⱘ䇁হ˄བᵰ᳝ⱘ䆱˅DŽ 䖭䚼ߚ䇁হᠻ㸠ᅠ៤ৢˈᭈϾ䇁হԧᠻ㸠㒧ᴳ˄݊Ёⱘӏԩ䇁হ䛑ৃҹᰃᣀ೼㢅ᣀোЁⱘ㢹 ೼䖭⾡ᮍᓣЁˈ৘ᴵӊҢࠡᕔৢձ⃵∖ؐˈⳈࠄ⒵䎇ᶤϾᴵӊˈ✊ৢᠻ㸠ᇍᑨⱘ䇁হ䚼ߚDŽ 䇁হ n else ... ... 䇁হ 2 else if (ᴵӊ 1) 䇁হ 1 if (ᴵӊ 1) ⿟ᑣЁ㒣ᐌՓ⫼ϟ߫ᮍᓣ㸼⼎໮䏃߸ᅮ˖ ++nother; else ++nwhite; else if (c == ' ' || c == '\n' || c == '\t') ++ndigit[c•'0']; if (c >= '0' && c <= '9') ߸ᮁϔϾᄫヺᰃ᭄ᄫǃぎⱑヺ䖬ᰃ݊ᅗᄫヺⱘࡳ㛑ৃҹ⬅ϟ߫䇁হᑣ߫ᅠ៤˖ ᷛDŽ ᔧ᭄㒘 ndigit ⱘড়⊩ϟܙ೼ c Ёⱘᄫヺᰃ'0'̚'9'ˈ݊ؐᇚЎ 0̚9ˈ಴ℸৃҹټབᵰᄬ ᮶㞾✊জᮍ֓ˈ՟བˈc • '0'ᰃϔϾᭈൟ㸼䖒ᓣˈخㄝӋѢ int ㉏ൟⱘব䞣੠ᐌ䞣DŽ䖭ḋ ⬅ᅮНৃⶹˈchar ㉏ൟⱘᄫヺᰃᇣᭈൟˈ಴ℸ char ㉏ൟⱘব䞣੠ᐌ䞣೼ㅫᴃ㸼䖒ᓣЁ 䲚䛑ᰃ䖭ḋⱘDŽ ᠡৃ㸠DŽᑌ䖤ⱘᰃˈ᠔᳝ⱘᄫヺ⫣خ⾡া᳝ᔧ'0'ǃ'1'ǃĂǃ'9'݋᳝䖲㓁䗦๲ⱘؐᯊˈ䖭 c• '0' ⫼Ѣ߸ᮁ c Ёⱘᄫヺᰃ৺Ў᭄ᄫDŽབᵰᅗᰃ᭄ᄫˈ䙷М䆹᭄ᄫᇍᑨⱘ᭄ؐᰃ if (c >= '0' && c <= '9') Ѣ᭄ᄫⱘᄫヺ㸼⼎ሲᗻDŽ՟བˈ⌟䆩䇁হއ䆹⿟ᑣⱘᠻ㸠প ᭄㒘ϟᷛৃҹᰃӏԩᭈൟ㸼䖒ᓣˈࣙᣀᭈൟব䞣˄བ i˅ҹঞᭈൟᐌ䞣DŽ ࣪੠ᠧॄ᭄㒘ⱘϸϾ for ᕾ⦃䇁হড᯴ߎᴹDŽ ㋴ߚ߿Ў ndigit[0]ǃndiglt[1]ǃĂǃndigit[9]ˈ䖭ৃҹ䗮䖛߱ྟܗℸ䆹᭄㒘ⱘ 10 Ͼ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䖨ಲؐ㉏ൟ ߑ᭄ৡ(0 Ͼ៪໮Ͼখ᭄ໄᯢ) ߑ᭄ᅮНⱘϔ㠀ᔶᓣЎ˖ } return p; p = p * base; for (i = 1; i <= n; ++i) p = 1; int i, p; { int power(int base, int n) /* power: raise base to n•th power; n >= 0 */ } return 0; printf("%d %d %d\n", i, power(2,i), power(•3,i)); for (i = 0; i < 10; ++i) int i; { main() /* test power function */ int power(int m, int n); #include 㒧ᵘDŽ ϟ䴶ᰃߑ᭄ power(m, n) ⱘᅮНঞ䇗⫼ᅗⱘЏ⿟ᑣˈ䖭ḋ៥ӀৃҹⳟࠄϔϾᅠᭈⱘ⿟ᑣ ᑧЁᦤկњϔϾ䅵ㅫ xy ⱘߑ᭄ pow(x, y)DŽ˅ޚ⃵ᐖˈԚ䖭ᇍѢ䇈ᯢ䯂乬Ꮖ䎇໳њDŽ˄ᷛ 5)ᴹ䇈ˈ݊㒧ᵰؐЎ 32DŽ䆹ߑ᭄ᑊ䴲ϔϾᅲ⫼ⱘ∖ᐖߑ᭄ˈᅗা㛑໘⧚䕗ᇣⱘᭈ᭄ⱘℷᭈ᭄ ⱘᮍ⊩DŽpower(m, n) ߑ᭄⫼Ѣ䅵ㅫᭈ᭄ mⱘ n⃵ᐖˈ݊Ё nᰃℷᭈ᭄DŽᇍߑ᭄䇗⫼ power(2, ㉏ԐѢ**ⱘ∖ᐖ䖤ㅫヺˈ៥Ӏ⦄೼䗮䖛㓪ݭϔϾ∖ᐖⱘߑ᭄ power(m, n)ᴹ䇈ᯢߑ᭄ᅮН Fortran 䇁㿔ϔḋᦤկ ڣЁᦤկⱘߑ᭄DŽ⦄೼ˈ䅽៥Ӏ㞾Ꮕࡼ᠟ᴹ㓪ݭϔѯߑ᭄DŽC 䇁㿔≵᳝ ࠄⳂࠡЎℶˈ៥Ӏ᠔Փ⫼ⱘߑ᭄˄བ printfǃgetchar ੠ putchar ㄝ˅䛑ᰃߑ᭄ᑧ ҹՓҷⷕ↉᳈⏙᱄ᯧ䇏DŽৃخϔ⃵ⱘⷁߑ᭄ˈ䖭ḋ ህ໳њDŽ೼ C 䇁㿔Ёৃҹㅔऩǃᮍ֓ǃ催ᬜഄՓ⫼ߑ᭄DŽ៥Ӏ㒣ᐌӮⳟࠄ೼ᅮНৢҙ䇗⫼њ ⱘDŽՓ⫼䆒䅵ℷ⹂ⱘߑ᭄ˈ⿟ᑣਬ᮴䳔㗗㰥ࡳ㛑ᰃབԩᅲ⦄ⱘˈ㗠া䳔ⶹ䘧ᅗ݋᳝ાѯࡳ㛑 ߑ᭄DŽߑ᭄Ў䅵ㅫⱘᇕ㺙ᦤկњϔ⾡ㅔ֓ⱘᮍ⊩ˈℸৢՓ⫼ߑ᭄ᯊϡ䳔㽕㗗㰥ᅗᰃབԩᅲ⦄ C 䇁㿔Ёⱘߑ᭄ㄝӋѢ Fortran 䇁㿔Ёⱘᄤ⿟ᑣ៪ߑ᭄ˈгㄝӋѢ Pascal 䇁㿔Ёⱘ䖛⿟៪ 1.7. ߑ᭄ 㒗д 1•14 㓪ݭϔϾ⿟ᑣˈᠧॄ䕧ܹЁ৘Ͼᄫヺߎ⦄乥ᑺⱘⳈᮍ೒DŽ 㒬ࠊˈൖⳈᮍ৥ⱘⳈᮍ೒߭㽕ೄ䲒ѯDŽ 㒗д 1•13 㓪ݭϔϾ⿟ᑣˈᠧॄ䕧ܹЁऩ䆡䭓ᑺⱘⳈᮍ೒DŽ∈ᑇᮍ৥ⱘⳈᮍ೒↨䕗ᆍᯧ ߎ⫼ switch 䇁হ㓪ݭⱘ䆹⿟ᑣⱘ঺ϔϾ⠜ᴀˈϢℸ䖯㸠↨䕗DŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ḋϞ䴶ⱘߑ᭄ॳൟгৃҹݭ៤ҹϟᔶᓣ ߑ᭄ॳൟϢߑ᭄ໄᯢЁখ᭄ৡϡ㽕∖ⳌৠDŽџᅲϞˈߑ᭄ॳൟЁⱘখ᭄ৡᰃৃ䗝ⱘˈ䖭 ߎ⦄䫭䇃DŽ ൟˈᅗᖙ乏Ϣ power ߑ᭄ⱘᅮН੠⫼⊩ϔ㟈DŽབᵰߑ᭄ⱘᅮНǃ⫼⊩Ϣߑ᭄ॳൟϡϔ㟈ˈᇚ 㸼ᯢ power ߑ᭄᳝ϸϾ int ㉏ൟⱘখ᭄ˈᑊ䖨ಲϔϾ int ㉏ൟⱘؐDŽ䖭⾡ໄᯢ⿄Ўߑ᭄ॳ int power(int m, int n); ߎ⦄೼ main ߑ᭄Пࠡⱘໄᯢ䇁হ ҹᦤ䝦໻ᆊ⊼ᛣˈ⿟ᑣ䖬㽕৥݊ᠻ㸠⦃๗䖨ಲ⢊ᗕDŽ ⱘ main ߑ᭄䛑ⳕ⬹њ return 䇁হˈԚ៥Ӏᇚ೼ҹৢⱘ main ߑ᭄Ёࣙ৿ return 䇁হˈ ៪ߎ䫭㒧ᴳᴵӊDŽЎㅔ⋕䍋㾕ˈࠡ䴶މؐЎ 0 㸼⼎ℷᐌ㒜ℶˈ䖨ಲؐЎ䴲 0 㸼⼎ߎ⦄ᓖᐌᚙ ಴ℸгৃҹ৥݊䇗⫼㗙䖨ಲϔϾؐˈ䆹䇗⫼㗙ᅲ䰙Ϟህᰃ⿟ᑣⱘᠻ㸠⦃๗DŽϔ㠀ᴹ䇈ˈ䖨ಲ 䇏㗙ৃ㛑Ꮖ㒣⊼ᛣࠄˈmain ߑ᭄ⱘ᳿ሒ᳝ϔϾ return 䇁হDŽ⬅Ѣ main ᴀ䑿гᰃߑ᭄ˈ ⬹ߑ᭄䖨ಲⱘؐDŽ ⫼ⱘؐDŽ䖭ㄝৠѢ೼ࠄ䖒ߑ᭄ⱘে㒜㒧㢅ᣀোᯊˈߑ᭄ህĀࠄ䖒њሑ༈āDŽЏ䇗ߑ᭄гৃҹᗑ ߑ᭄ϡϔᅮ䛑᳝䖨ಲؐDŽϡᏺ㸼䖒ᓣⱘ return 䇁হᇚᡞ᥻ࠊᴗ䖨ಲ㒭䇗⫼㗙ˈԚϡ䖨ಲ᳝ return 㸼䖒ᓣ; 䴶ৃҹ䎳ӏԩ㸼䖒ᓣˈᔶᓣЎ˖ power ߑ᭄䅵ㅫ᠔ᕫⱘ㒧ᵰ䗮䖛 return 䇁হ䖨ಲ㒭 main ߑ᭄DŽ݇䬂ᄫ return ⱘৢ ᓣখ᭄ᇍᑨⱘؐ⿄Ўᅲ䰙খ᭄DŽ ៥Ӏ䗮ᐌᡞߑ᭄ᅮНЁ೚ᣀোݙ߫㸼Ёߎ⦄ⱘব䞣⿄Ўᔶᓣখ᭄ˈ㗠ᡞߑ᭄䇗⫼ЁϢᔶ さDŽব䞣 i Ϣ p гᰃ䖭ḋ˖power ߑ᭄Ёⱘ i Ϣ main ߑ᭄Ёⱘ i ᮴݇DŽކӮᓩ䍋 ߑ᭄ݙ䚼᳝ᬜˈᇍ݊ᅗӏԩߑ᭄䛑ᰃϡৃ㾕ⱘ˖݊ᅗߑ᭄ৃҹՓ⫼ϢПⳌৠⱘখ᭄ৡᄫ㗠ϡ ໄᯢখ᭄ⱘ㉏ൟǃৡᄫҹঞ䆹ߑ᭄䖨ಲ㒧ᵰⱘ㉏ൟDŽpower ߑ᭄ⱘখ᭄Փ⫼ⱘৡᄫা೼ power int power(int base, int n) power ߑ᭄ⱘ㄀ϔ㸠䇁হ ϡᰃ᠔᳝ߑ᭄ⱘ㒧ᵰ䛑ᰃᭈൟؐˈ៥Ӏᇚ೼㄀ 4 ゴЁ䅼䆎˅DŽ ߑ᭄䖨ಲϔϾḐᓣ࣪ⱘᭈ᭄ᑊᠧॄDŽ೼㸼䖒ᓣЁˈpower(2, i) ৠ 2 ੠ i ϔḋ䛑ᰃᭈ᭄˄ᑊ ↣⃵䇗⫼ᯊˈmain ߑ᭄৥ power ߑ᭄Ӵ䗦ϸϾখ᭄˗೼䇗⫼ᠻ㸠ᅠ៤ᯊˈpower ߑ᭄৥ main printf("%d %d %d\n", i, power(2, i), power(•i, 3)); main ߑ᭄೼ϟ߫䇁হЁ䇗⫼њϸ⃵ power ߑ᭄˖ ϸϾߑ᭄ᬒ೼ৠϔ᭛ӊЁˈ䖭ḋࠡ䴶᠔ᄺⱘ᳝݇䖤㸠 C 䇁㿔⿟ᑣⱘⶹ䆚ҡ✊᳝ᬜDŽ ᅮᇚ main ੠ power 䖭؛ᅮⱘDŽ៥Ӏ᱖Ϩއ԰ˈԚ䖭ᰃ᪡԰㋏㒳ⱘॳ಴ˈᑊϡᰃ䇁㿔ⱘሲᗻ ᳈໮ⱘᎹخ೼໮Ͼ᭛ӊЁDŽབᵰ⑤⿟ᑣߚᬷ೼໮Ͼ᭛ӊЁˈ䙷Мˈ೼㓪䆥੠ࡴ䕑ᯊˈህ䳔㽕 ߑ᭄ᅮНৃҹҹӏᛣ⃵ᑣߎ⦄೼ϔϾ⑤᭛ӊ៪໮Ͼ⑤᭛ӊЁˈԚৠϔߑ᭄ϡ㛑ߚࡆᄬᬒ } 䇁হᑣ߫ ໄᯢ䚼ߚ } w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᗻ䋼˖ ㅔ⋕DŽջབˈϟ䴶ⱘ䖭Ͼ power ߑ᭄߽⫼њ䖭ϔޥ乱໪Փ⫼ⱘব䞣᳈ᇥDŽ䖭ḋ⿟ᑣৃҹ᳈㋻ Ӵؐ䇗⫼ⱘ߽໻ѢᓞDŽ೼㹿䇗⫼ߑ᭄Ёˈখ᭄ৃҹⳟ԰ᰃ֓Ѣ߱ྟ࣪ⱘሔ䚼ব䞣ˈ಴ℸ 㛑ׂᬍ݊⾕᳝ⱘЈᯊࡃᴀⱘؐDŽ ᳔Џ㽕ⱘऎ߿೼Ѣˈ೼ C 䇁㿔Ёˈ㹿䇗⫼ߑ᭄ϡ㛑Ⳉ᥹ׂᬍЏ䇗ߑ᭄Ёব䞣ⱘؐˈ㗠া ߑ᭄ᖙ乏䆓䯂ॳྟখ᭄ˈ㗠ϡᰃ䆓䯂খ᭄ⱘᴀഄࡃᴀDŽ Fortran ㄝ䇁㿔ᰃĀ䗮䖛ᓩ⫼䇗⫼āˈPascal ߭䞛⫼ var খ᭄ⱘᮍᓣˈ೼䖭ѯ䇁㿔Ёˈ㹿䇗⫼ⱘ খ᭄ؐᄬᬒ೼Јᯊব䞣Ёˈ㗠ϡᰃᄬᬒ೼ॳᴹⱘব䞣ЁDŽ䖭Ϣ݊ᅗᶤѯ䇁㿔ᰃϡৠⱘˈ↨བˈ 䰠⫳DŽ೼ C 䇁㿔Ёˈ᠔᳝ߑ᭄খ᭄䛑ᰃĀ䗮䖛ؐāӴ䗦ⱘDŽгህᰃ䇈ˈӴ䗦㒭㹿䇗⫼ߑ᭄ⱘ дᛃ݊ᅗ䇁㿔˄⡍߿ᰃ Fortran 䇁㿔˅ⱘ⿟ᑣਬৃ㛑Ӯᇍ C 䇁㿔ⱘߑ᭄খ᭄Ӵ䗦ᮍᓣᛳࠄ 1.8. খ᭄——Ӵؐ䇗⫼ 㒗д 1•15 䞡ᮄ㓪ݭ 1.2 㡖Ёⱘ⏽ᑺ䕀ᤶ⿟ᑣˈՓ⫼ߑ᭄ᅲ⦄⏽ᑺ䕀ᤶ䅵ㅫDŽ ៥Ӏ䖬ᰃᔎ⚜ᓎ䆂䇏㗙˖೼Փ⫼ᮄᓣⱘ㓪䆥఼ᯊˈ᳔དՓ⫼ᮄᓣⱘߑ᭄ॳൟໄᯢᮍᓣDŽ ൟᮍ䴶ⱘ䫭䇃DŽANSI C ҡ✊ᬃᣕᮻᓣⱘߑ᭄ໄᯢϢᅮНˈ䖭ḋ㟇ᇥৃҹ᳝ϔϾ䖛⏵䰊↉DŽԚ ೼ ANSI C ЁᅮНⱘߑ᭄ॳൟ䇁⊩Ёˈ㓪䆥఼ৃҹᕜᆍᯧẔ⌟ߎߑ᭄䇗⫼Ёখ᭄᭄Ⳃ੠㉏ 䚼ⳕ⬹DŽ ᅮ䖨ಲ int ㉏ൟⱘؐˈ಴ℸᭈϾߑ᭄ⱘໄᯢৃҹܼ؛ϟᇚ㹿މџᅲϞˈpower ߑ᭄೼咬䅸ᚙ 䆌ࣙ৿খ᭄߫㸼ˈ䖭ḋ㓪䆥఼ህ᮴⊩೼ℸᯊẔᶹ power ߑ᭄䇗⫼ⱘড়⊩ᗻDŽܕߑ᭄ໄᯢЁϡ int power(); ೼ C 䇁㿔ⱘ᳔߱ᅮНЁˈৃҹ೼⿟ᑣⱘᓔ༈ᣝ✻ϟ䴶䖭⾡ᔶᓣໄᯢ power ߑ᭄˖ ൟˈ߭咬䅸Ў int ㉏ൟDŽߑ᭄ԧϢ ANSI C ЁᔶᓣⳌৠDŽ ݊Ёˈখ᭄ৡ೼೚ᣀোݙᣛᅮˈখ᭄㉏ൟ೼Ꮊ㢅ᣀোПࠡໄᯢDŽབᵰ≵᳝ໄᯢᶤϾখ᭄ⱘ㉏ } return p; p = p * base; for (i = 1; i <= n; ++i) p = 1; int i, p; { int base, n; power(base, n) /* (old•style version) */ /* power: raise base to n•th power; n >= 0 */ ᣝ✻ C 䇁㿔ⱘ᳔߱ᅮНˈpower ߑ᭄ᑨ䆹ݭ៤ϟ߫ᔶᓣ˖ ಲ乒ϔϟˈANSI C ৠ䕗ᮽ⠜ᴀ C 䇁㿔П䯈ⱘ᳔໻ऎ߿೼Ѣߑ᭄ⱘໄᯢϢᅮНᮍᓣⱘϡৠDŽ ৡDŽ Ԛᰃˈড়䗖ⱘখ᭄ৡ㛑໳䍋ࠄᕜདⱘ䇈ᯢᗻ԰⫼ˈ಴ℸ៥Ӏ೼ߑ᭄ॳൟЁᘏᰃᣛᯢখ᭄ int power(int, int); w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! #define MAXLINE 1000 /* maximum input line length */ #include ݭⱘ⿟ᑣ˖ ᳔ৢˈ៥Ӏ䳔㽕೼Џߑ᭄ main Ё᥻ࠊ getline ੠ copy 䖭ϸϾߑ᭄DŽҹϟ֓ᰃ៥Ӏ㓪 䇈ˈ៥Ӏ䳔㽕⫼঺ϔϾߑ᭄ copy ᡞᮄ㸠໡ࠊࠄϔϾᅝܼⱘԡ㕂DŽ ᔧথ⦄ᶤϾᮄ䇏ܹⱘ㸠↨ҹࠡ䇏ܹⱘ᳔䭓㸠䖬㽕䭓ᯊˈህ䳔㽕ᡞ䆹㸠ֱᄬ䍋ᴹDŽгህᰃ ᷛᖫ᭛ӊ㒧ᴳⱘ䖨ಲؐDŽ↣ϔ㸠㟇ᇥࣙᣀϔϾᄫヺˈাࣙ৿ᤶ㸠ヺⱘ㸠ˈ݊䭓ᑺЎ 1DŽ ᯊ䖨ಲ䆹㸠ⱘ䭓ᑺˈ㗠೼䘛ࠄ᭛ӊ㒧ᴳヺᯊ䖨ಲ 0DŽ⬅Ѣ 0 ϡᰃ᳝ᬜⱘ㸠䭓ᑺˈ಴ℸৃҹ԰Ў getline ߑ᭄ᑨ䆹೼䇏ࠄ᭛ӊ᳿ሒᯊ䖨ಲϔϾֵো˗᳈Ў᳝⫼ⱘ䆒䅵ᰃᅗ㛑໳೼䇏ܹ᭛ᴀ㸠 ゟⱘߑ᭄ getlineˈᅗ䇏প䕧ܹⱘϟϔ㸠DŽ៥Ӏሑ䞣ֱᣕ䆹ߑ᭄೼݊ᅗഎৄг᳝⫼DŽ㟇ᇥ ៥Ӏ㓪ݭϔϾ⣀ˈܜ಴Ў䖭⾡ߦߚᮍᓣ↨䕗ড়⧚ˈ᠔ҹৃҹᣝ✻䖭⾡ᮍᓣ㓪ݭ⿟ᑣDŽ佪 ⱘ㸠ǃֱᄬ䆹㸠ˈ݊ԭ䚼ߚ߭᥻ࠊ䖭ϔ䖛⿟DŽ ҢϞ䴶ⱘḚᶊЁᕜᆍᯧⳟߎˈ⿟ᑣᕜ㞾✊ഄߚ៤њ㢹ᑆ⠛ᮁˈߚ߿⫼Ѣ䇏ܹᮄ㸠ǃ⌟䆩䇏ܹ ᠧॄ᳔䭓ⱘ㸠 ֱᄬ䆹㸠ⱘ䭓ᑺ ֱᄬ䆹㸠Ў᳔䭓㸠 if (䆹㸠↨Ꮖ໘⧚ⱘ᳔䭓㸠䖬㽕䭓) while (䖬᳝᳾໘⧚ⱘ㸠) ㅫ⊩ⱘ෎ᴀḚᶊ䴲ᐌㅔऩ˖ ҹঞ᪡԰ᄫヺ᭄㒘ⱘߑ᭄ⱘ⫼⊩DŽ䆹⿟ᑣ䇏ܹϔ㒘᭛ᴀ㸠ˈᑊᡞ᳔䭓ⱘ᭛ᴀ㸠ᠧॄߎᴹDŽ䆹 ᄫヺ᭄㒘ᰃ C 䇁㿔Ё᳔ᐌ⫼ⱘ᭄㒘㉏ൟDŽϟ䴶៥Ӏ䗮䖛㓪ݭϔϾ⿟ᑣˈᴹ䇈ᯢᄫヺ᭄㒘 1.9. ᄫヺ᭄㒘 ㋶ⱘؐDŽ䖭ᰃϟϔ㡖ᇚ㽕䅼䆎ⱘ䯂乬DŽܗ䆓䯂៪ׂᬍ᭄㒘 ㋴ᴀ䑿DŽ೼㹿䇗⫼ߑ᭄Ёˈৃҹ䗮䖛᭄㒘ϟᷛܗ㋴ⱘԡ㕂៪ഄഔ——ᅗᑊϡ໡ࠊ᭄㒘ܗ䍋ྟ ህ᳝᠔ϡৠњDŽᔧᡞ᭄㒘ৡ⫼԰খ᭄ᯊˈӴ䗦㒭ߑ᭄ⱘؐᰃ᭄㒘މབᵰᰃ᭄㒘খ᭄ˈᚙ ߭䳔㽕ᇚᇍᑨⱘখ᭄ໄᯢЎᣛ䩜㉏ൟˈᑊ䗮䖛ᅗ䯈᥹䆓䯂ব䞣DŽ៥Ӏᇚ೼㄀ 5 ゴЁ䅼䆎ᣛ䩜DŽ ߑ᭄ᦤկᕙ䆒㕂ؐⱘব䞣ⱘഄഔ˄Ңᡔᴃ㾦ᑺⳟˈഄഔህᰃᣛ৥ব䞣ⱘᣛ䩜˅ˈ㗠㹿䇗⫼ߑ᭄ ϟˈ䇗⫼㗙䳔㽕৥㹿䇗⫼މᖙ㽕ᯊˈгৃҹ䅽ߑ᭄㛑໳ׂᬍЏ䇗ߑ᭄Ёⱘব䞣DŽ䖭⾡ᚙ ᭄ؐDŽ ϡ䳔㽕乱໪ᓩܹব䞣 i˗power ߑ᭄ݙ䚼ᇍ n ⱘӏԩ᪡԰ϡӮᕅડࠄ䇗⫼ߑ᭄Ё n ⱘॳྟখ Ⳉࠄ݊ؐЎ 0ˈ䖭ḋህˈޣ݊Ёˈখ᭄ n ⫼԰Јᯊব䞣ˈᑊ䗮䖛䱣ৢᠻ㸠ⱘ for ᕾ⦃䇁হ䗦 } return p; p = p * base; for (p = 1; n > 0; ••n) int p; { int power(int base, int n) power: raise base to n•th power; n >= 0; version 2 */ */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ぎ䯈DŽ೼ getline ߑ᭄Ё≵᳝ᖙ㽕ᣛᯢ᭄㒘 s ⱘ䭓ᑺˈ䖭ᰃ಴Ў䆹᭄㒘ټᇣⱘⳂⱘᰃ⬭ߎᄬ ໄᯢⱘˈᅗᡞ㄀ϔϾখ᭄ s ໄᯢЎ᭄㒘ˈᡞ㄀ѠϾখ᭄ lim ໄᯢЎᭈൟˈໄᯢЁᦤկ᭄㒘໻ int getline(char s[], int lim) ϸϾখ᭄ᰃ䗮䖛⿟ᑣ㸠 main Ϣ getline П䯈䗮䖛ϔᇍখ᭄ঞϔϾ䖨ಲؐ䖯㸠᭄᥂ѸᤶDŽ೼ getline ߑ᭄Ёˈ ᭛ӊЁDŽ ᅮᅗӀ䛑ᄬᬒ೼ৠϔϾ؛⿟ᑣⱘᓔྟᇍ getline ੠ copy 䖭ϸϾߑ᭄䖯㸠њໄᯢˈ䖭䞠 } ++i; while ((to[i] = from[i]) != '\0') i = 0; int i; { void copy(char to[], char from[]) /* copy: copy 'from' into 'to'; assume to is big enough */ } return i; s[i] = '\0'; } ++i; s[i] = c; if (c == '\n') { s[i] = c; for (i=0; i < lim•1 && (c=getchar())!=EOF && c!='\n'; ++i) int c, i; { int getline(char s[],int lim) /* getline: read a line into s, return length */ } return 0; printf("%s", longest); if (max > 0) /* there was a line */ } copy(longest, line); max = len; if (len > max) { while ((len = getline(line, MAXLINE)) > 0) max = 0; char longest[MAXLINE]; /* longest line saved here */ char line[MAXLINE]; /* current input line */ int max; /* maximum length seen so far */ int len; /* current line length */ { main() /* print the longest input line */ void copy(char to[], char from[]); int getline(char line[], int maxline); w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᭄Ёⱘ↣Ͼሔ䚼ব䞣া೼ߑ᭄㹿䇗⫼ᯊᄬ೼ˈ೼ߑ᭄ᠻ㸠ᅠ↩䗔ߎᯊ⍜༅DŽ䖭гᰃ݊ᅗ䇁㿔 ৠḋབℸDŽ՟བˈgetline ߑ᭄Ёໄᯢⱘব䞣 i Ϣ copy ߑ᭄Ёໄᯢⱘব䞣 i ≵᳝݇㋏DŽߑ ѢᅗӀᰃ೼ main ߑ᭄Ёໄᯢⱘˈ಴ℸ݊ᅗߑ᭄ϡ㛑Ⳉ᥹䆓䯂ᅗӀDŽ݊ᅗߑ᭄Ёໄᯢⱘব䞣г main ߑ᭄Ёⱘব䞣˄བ lineǃlongest ㄝ˅ᰃ main ߑ᭄ⱘ⾕㞾ব䞣៪ሔ䚼ব䞣DŽ⬅ 1.10. ໪䚼ব䞣Ϣ԰⫼ඳ ϔϾ䕧ܹ㸠Ёⱘᄫヺ乎ᑣDŽצ㓪ݭϔϾ⿟ᑣˈ↣⃵乴 䖛ᴹDŽՓ⫼䆹ߑ᭄צ㒗д 1•19 㓪ݭߑ᭄ reverse(s)ˈᇚᄫヺІ s Ёⱘᄫヺ乎ᑣ乴 㸠DŽ 㒗д 1•18 㓪ݭϔϾ⿟ᑣˈߴ䰸↣Ͼ䕧ܹ㸠᳿ሒⱘぎḐঞࠊ㸼ヺˈᑊߴ䰸ᅠܼᰃぎḐⱘ 㒗д 1•17 㓪ݭϔϾ⿟ᑣˈᠧॄ䭓ᑺ໻Ѣ 80 Ͼᄫヺⱘ᠔᳝䕧ܹ㸠DŽ 㸠ⱘ䭓ᑺˈᑊሑৃ㛑໮ഄᠧॄ᭛ᴀDŽ 㒗д 1•16 ׂᬍᠧॄ᳔䭓᭛ᴀ㸠ⱘ⿟ᑣⱘЏ⿟ᑣ mainˈՓПৃҹᠧॄӏᛣ䭓ᑺⱘ䕧ܹ 䳔㽕䖯㸠䫭䇃ẔᶹDŽ ৺⑶ߎDŽ঺ϔᮍ䴶ˈ䇗⫼ copy ߑ᭄ⱘ⿟ᑣⶹ䘧˄гৃҹᡒߎ˅ᄫヺІⱘ䭓ᑺˈ಴ℸ䆹ߑ᭄ϡ ⶹ䘧䕧ܹ㸠ⱘ䭓ᑺˈ಴ℸ getline ߑ᭄䳔㽕Ẕᶹᰃܜ䇗⫼ getline ߑ᭄ⱘ⿟ᑣ᮴⊩乘 ໘⧚DŽЎњㅔ࣪⿟ᑣˈ៥Ӏ೼䖭䞠ϡ㗗㰥䖭Ͼ䯂乬DŽމ 䖛⌟䆩㸠ⱘ䭓ᑺҹঞẔᶹ䖨ಲⱘ᳔ৢϔϾᄫヺᴹ߸ᅮᔧࠡ㸠ᰃ৺໾䭓ˈ✊ৢݡḍ᥂݋ԧⱘᚙ ℶ䇏ᄫヺDŽmain ߑ᭄ৃҹ䗮ذⱘᠻ㸠ᰃᅝܼⱘˈ᮴䆎ᰃ৺ࠄ䖒ᤶ㸠ヺᄫヺˈᔧ᭄㒘⒵ᯊᅗᇚ 䆌ⱘ᳔໻ؐᯊˈmain ߑ᭄ᑨ䆹བԩ໘⧚ˈgetline ߑ᭄ܕ乬DŽ՟བˈᔧ䇏ܹⱘ㸠䭓ᑺ໻Ѣ ؐᕫϔᦤⱘᰃˈेՓᰃϞ䗄䖭ḋᕜᇣⱘ⿟ᑣˈ೼Ӵ䗦খ᭄ᯊгӮ䘛ࠄϔѯ咏⚺ⱘ䆒䅵䯂 ぎᄫヺ'\0'ϡᰃ᱂䗮᭛ᴀⱘϔ䚼ߚDŽ˅ ⱘᅲ⦄ℷᰃձ䌪Ѣ䕧ܹখ᭄⬅'\0'㒧ᴳ䖭ϔџᅲˈᅗᇚ'\0'ᣋ䋱ࠄ䕧ߎখ᭄ЁDŽ˄гህᰃ䇈ˈ printf ߑ᭄ЁⱘḐᓣ㾘㣗%s 㾘ᅮˈᇍᑨⱘখ᭄ᖙ乏ᰃҹ䖭⾡ᔶᓣ㸼⼎ⱘᄫヺІDŽcopy ߑ᭄ ᑊҹ'\0'ᷛᖫᄫヺІⱘ㒧ᴳDŽ ᄫヺІⱘ৘Ͼᄫヺˈټ㋴ߚ߿ᄬܗ᭄㒘ⱘ৘ˈټⱘᄫヺІᐌ䞣ᯊˈᅗᇚҹᄫヺ᭄㒘ⱘᔶᓣᄬ "hello\0" ᄫヺІⱘ㒧ᴳDŽ䖭ϔ㑺ᅮᏆ㹿 C 䇁㿔䞛⫼˖ᔧ೼ C 䇁㿔⿟ᑣЁߎ⦄㉏ԐѢ getline ߑ᭄ᡞᄫヺ'\0'˄ेぎᄫヺˈ݊ؐЎ 0˅ᦦܹࠄᅗ߯ᓎⱘ᭄㒘ⱘ᳿ሒˈҹᷛ䆄 ߑ᭄ⱘ䖨ಲؐ㉏ൟЎ voidˈᅗᰒᓣ䇈ᯢ䆹ߑ᭄ϡ䖨ಲӏԩؐDŽ ᳝ѯߑ᭄䖨ಲ᳝⫼ⱘؐˈ㗠᳝ѯߑ᭄˄བ copy˅ҙ⫼Ѣᠻ㸠ϔѯࡼ԰ˈᑊϡ䖨ಲؐDŽcopy ⱘ咬䅸䖨ಲؐ㉏ൟЎ intˈ಴ℸ䖭䞠ⱘ int ৃҹⳕ⬹DŽ 䇁হᇚؐ䖨ಲ㒭݊䇗⫼㗙DŽϞ䗄⿟ᑣ㸠гໄᯢњ getline ᭄ⱘ䖨ಲؐ㉏ൟЎ intDŽ⬅Ѣߑ᭄ ⱘ໻ᇣᰃ೼ main ߑ᭄Ё䆒㕂ⱘDŽབৠ power ߑ᭄ϔḋˈgetline ߑ᭄Փ⫼њϔϾ return w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! } ++i; line[i] = c; if (c == '\n') { line[i] = c; && (c=getchar)) != EOF && c != '\n'; ++i) for (i = 0; i < MAXLINE • 1 extern char line[]; int c, i; { int getline(void) /* getline: specialized version */ } return 0; printf("%s", longest); if (max > 0) /* there was a line */ } copy(); max = len; if (len > max) { while ((len = getline()) > 0) max = 0; extern char longest[]; extern int max; int len; { main() /* print longest input line; specialized version */ void copy(void); int getline(void); 䳔㽕ׂᬍ䖭 3 Ͼߑ᭄ⱘ䇗⫼ǃໄᯢϢߑ᭄ԧDŽ 䞣ˈ៥ӀᬍݭϞ䗄ᠧॄ᳔䭓᭛ᴀ㸠ⱘ⿟ᑣˈᡞ lineǃlongest Ϣ max ໄᯢ៤໪䚼ব䞣DŽ䖭 ᯢᯊৃҹ⫼ extern 䇁হᰒᓣໄᯢˈгৃҹ䗮䖛Ϟϟ᭛䱤ᓣໄᯢDŽЎњ᳈䆺㒚ഄ䅼䆎໪䚼ব DŽ೼↣Ͼ䳔㽕䆓䯂໪䚼ব䞣ⱘߑ᭄Ёˈᖙ乏ໄᯢⳌᑨⱘ໪䚼ব䞣ˈℸᯊ䇈ᯢ݊㉏ൟDŽໄܗऩ ټ໪䚼ব䞣ᖙ乏ᅮН೼᠔᳝ߑ᭄П໪ˈϨা㛑ᅮНϔ⃵ˈᅮНৢ㓪䆥⿟ᑣᇚЎᅗߚ䜡ᄬ 䖭ѯব䞣ҡᇚֱᣕॳᴹⱘؐϡবDŽ 㗠ϡᰃ೼ߑ᭄䇗⫼ᯊѻ⫳ǃ೼ߑ᭄ᠻ㸠ᅠ↩ᯊ⍜༅DŽेՓ೼ᇍ໪䚼ব䞣䌟ؐⱘߑ᭄䖨ಲৢˈ ҹ䗮䖛໪䚼ব䞣Ѹᤶ᭄᥂ˈ㗠ϡᖙՓ⫼খ᭄㸼DŽݡ㗙ˈ໪䚼ব䞣೼⿟ᑣᠻ㸠ᳳ䯈ϔⳈᄬ೼ˈ ᳔໪ሖ⿟ᑣഫໄᯢⱘব䞣䴲ᐌ㉏Ԑ˅DŽ⬅Ѣ໪䚼ব䞣ৃҹ೼ܼሔ㣗ೈݙ䆓䯂ˈ಴ℸˈߑ᭄䯈ৃ 䗮䖛ব䞣ৡ䆓䯂䖭⾡㉏ൟⱘব䞣˄䖭ᴎࠊৠ Fortran 䇁㿔Ёⱘ COMMON ব䞣៪ Pascal 䇁㿔Ё 䰸㞾ࡼব䞣໪ˈ䖬ৃҹᅮНԡѢ᠔᳝ߑ᭄໪䚼ⱘব䞣ˈгህᰃ䇈ˈ೼᠔᳝ߑ᭄Ё䛑ৃҹ ߭݊Ёᄬᬒⱘᰃ᮴ᬜؐDŽ ֱ⬭ࠡ⃵䇗⫼ᯊⱘ䌟ؐˈϨ೼↣⃵䖯ܹߑ᭄ᯊ䛑㽕ᰒᓣЎ݊䌟ؐDŽབᵰ㞾ࡼব䞣≵᳝䌟ؐˈ ⬅Ѣ㞾ࡼব䞣া೼ߑ᭄䇗⫼ᠻ㸠ᳳ䯈ᄬ೼ˈ಴ℸˈ೼ߑ᭄ⱘϸ⃵䇗⫼П䯈ˈ㞾ࡼব䞣ϡ ㉏ˈ䖭⾡㉏ൟⱘሔ䚼ব䞣೼໮⃵ߑ᭄䇗⫼П䯈ֱᣕؐϡবDŽ˅ټゴᇚ䅼䆎 static ᄬ 䗮ᐌᡞ䖭㉏ব䞣⿄Ў㞾ࡼব᳔ⱘॳ಴DŽҹৢ៥ӀՓ⫼Ā㞾ࡼব䞣āҷ㸼Āሔ䚼ব䞣āDŽ˄㄀ 4 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 㒉ⱘব䞣ৡⳈ᥹ݭܹњߑ᭄ˈҢ㗠Փ䖭ϸϾ᳝⫼ⱘߑ᭄༅এњ䗮⫼ᗻDŽ དˈॳ಴᳝ϸᮍ䴶ˈ݊ϔ֓ᰃՓ⫼њ໪䚼ব䞣˗঺ϔᮍ䴶ˈ㄀ 2 Ͼ⠜ᴀЁⱘߑ᭄ᇚᅗӀ᠔᪡ ᬍজবᕫकߚೄ䲒DŽ៥Ӏࠡ䴶㓪ݭⱘᠧॄ᳔䭓᭛ᴀ㸠ⱘ⿟ᑣⱘ㄀ 2 Ͼ⠜ᴀህϡབ㄀ 1 Ͼ⠜ᴀ ⿟ᑣЁⱘ᭄᥂݇㋏῵㊞ϡ⏙——໪䚼ব䞣ⱘؐৃ㛑Ӯ㹿ᛣ໪ഄ៪ϡ㒣ᛣഄׂᬍˈ㗠⿟ᑣⱘׂ Փ⫼໪䚼ব䞣ⱘᯊ׭ˈᅗӀгᰃᄬ೼ⱘDŽ䖛ߚձ䌪໪䚼ব䞣Ӯᇐ㟈ϔᅮⱘ亢䰽ˈ಴ЎᅗӮՓ ৃҹㅔ᭄࣪᥂ⱘ䗮ֵ——খ᭄㸼বⷁњˈϨ೼䳔㽕ᯊᘏৃҹ䆓䯂䖭ѯব䞣DŽԚᰃˈेՓ೼ϡ 乎֓ᦤϔϟˈ⦄೼䍞ᴹ䍞໮ⱘҎᡞ⫼ࠄⱘ᠔᳝ϰ㽓䛑԰Ў໪䚼ব䞣Փ⫼ˈ಴ЎԐТ䖭ḋ DŽܗऩټᯢব䞣ⱘᗻ䋼ˈԚᑊϡߚ䜡ᄬ 㗠Āໄᯢāᣛⱘᰃ䇈ˈܗऩټ˄declaration˅䖭ϸϾ䆡DŽĀᅮНā㸼⼎߯ᓎব䞣៪ߚ䜡ᄬ 䇏㗙ᑨ䆹⊼ᛣࠄˈ䖭㡖Ё៥Ӏ೼䇜䆎໪䚼ব䞣ᯊ䇼ᜢഄՓ⫼њᅮН˄define˅Ϣໄᯢ ϔℹ䅼䆎DŽ ANSI C Ёˈབᵰ㽕ໄᯢぎখ᭄㸼ˈ߭ᖙ乏Փ⫼݇䬂ᄫ void 䖯㸠ᰒᓣໄᯢDŽ㄀ 4 ゴᇚᇍℸ䖯 ANSI C 䇁㿔ᡞぎখ᭄㸼ⳟ៤㗕⠜ᴀ C 䇁㿔ⱘໄᯢᮍᓣˈᑊϨᇍখ᭄㸼ϡݡ䖯㸠ӏԩẔᶹDŽ೼ ӊᓔྟ໘ᅗӀⱘॳൟᑨ䆹ᰃ getline()Ϣ copy()DŽԚЎњϢ㗕⠜ᴀⱘ C 䇁㿔⿟ᑣݐᆍˈ ೼Ϟ䗄⡍߿⠜ᴀЁˈ⬅Ѣ getline Ϣ copy ߑ᭄䛑ϡᏺখ᭄ˈ಴ℸҢ䘏䕥Ϟ䆆ˈ೼⑤᭛ ໄᯢⱘDŽ᳈䆺㒚ⱘֵᙃᇚ೼㄀ 4 ゴЁ䅼䆎ˈ㄀ 7 ゴঞ䰘ᔩ B ᇚ䅼䆎ߑ᭄ᑧDŽ ᑧЁⱘߑ᭄ህᰃ೼㉏ԐѢⱘ༈᭛ӊЁޚৡ.h 㑺ᅮЎ༈᭛ӊৡⱘᠽሩৡDŽ՟བˈᷛ Ў༈᭛ӊ˅ˈᑊ೼↣Ͼ⑤᭛ӊⱘᓔ༈Փ⫼#include 䇁হᡞ᠔㽕⫼ⱘ༈᭛ӊࣙ৿䖯ᴹDŽৢ㓔 НП䯈ⱘ㘨㋏DŽҎӀ䗮ᐌᡞব䞣੠ߑ᭄ⱘ extern ໄᯢᬒ೼ϔϾऩ⣀ⱘ᭛ӊЁ˄дᛃϞ⿄П ᭛ӊЁՓ⫼ˈ䙷М೼᭛ӊ file2 Ϣ file3 Ёህ䳔㽕Փ⫼ extern ໄᯢᴹᓎゟ䆹ব䞣Ϣ݊ᅮ བᵰ⿟ᑣࣙ৿೼໮Ͼ⑤᭛ӊЁˈ㗠ᶤϾব䞣೼ file1 ᭛ӊЁᅮНǃ೼ file2 ੠ file3 ᓔྟ໘ˈ䖭ḋህৃҹⳕ⬹ extern ໄᯢDŽ Ёˈ᠔᳝໪䚼ব䞣ⱘᅮН䛑ᬒ೼⑤᭛ӊⱘ⫣خЁⱘ޴Ͼ extern ໄᯢ䛑ᰃ໮ԭⱘDŽ೼䗮ᐌⱘ ߑ᭄Пࠡˈ䙷М೼䙷Ͼߑ᭄Ёህ≵᳝ᖙ㽕Փ⫼ extern ໄᯢDŽ಴ℸˈmainǃgetline ঞ copy ϟৃҹⳕ⬹ extern ໄᯢDŽ೼⑤᭛ӊЁˈབᵰ໪䚼ব䞣ⱘᅮНߎ⦄೼Փ⫼ᅗⱘމᶤѯᚙ ᮍ䴶Ϣ᱂䗮ব䞣ⱘໄᯢⳌৠDŽ ЁՓ⫼ extern ㉏ൟⱘໄᯢDŽ䖭⾡㉏ൟⱘໄᯢ䰸њ೼ࠡ䴶ࡴњϔϾ݇䬂ᄫ extern ໪ˈ݊ᅗ ߑ᭄೼Փ⫼໪䚼ব䞣Пࠡˈᖙ乏㽕ⶹ䘧໪䚼ব䞣ⱘৡᄫDŽ㽕䖒ࠄ䆹Ⳃⱘˈϔ⾡ᮍᓣᰃ೼ߑ᭄ НϢሔ䚼ব䞣ⱘᅮНᰃⳌৠⱘˈԚ⬅ѢᅗӀԡѢ৘ߑ᭄ⱘ໪䚼ˈ಴ℸ䖭ѯব䞣ᰃ໪䚼ব䞣DŽ DŽҢ䇁⊩㾦ᑺⳟˈ໪䚼ব䞣ⱘᅮܗऩټњ৘໪䚼ব䞣ⱘ㉏ൟˈ䖭ḋ㓪䆥⿟ᑣᇚЎᅗӀߚ䜡ᄬ ೼䆹՟ᄤЁˈࠡ޴㸠ᅮНњ mainǃgetline Ϣ copy ߑ᭄Փ⫼ⱘ޴Ͼ໪䚼ব䞣ˈໄᯢ } ++i; while ((longest[i] = line[i]) != '\0') i = 0; extern char line[], longest[]; int i; { void copy(void) /* copy: specialized version */ } return i; line[i] = '\0'; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᛇᡞ䆹⿟ᑣ㓪ݭ៤ᅠܼ䗮⫼ⱘ⿟ᑣˈ䲒ᑺӮ↨䕗໻DŽ˅ ᣀোϡ䜡ᇍㄝDŽ㽕ℷ⹂໘⧚ᓩো˄ࣙᣀऩᓩো੠ঠᓩো˅ǃ䕀Нᄫヺᑣ߫Ϣ⊼䞞DŽ˄བᵰ䇏㗙 㒗д 1•24 㓪ݭϔϾ⿟ᑣˈᶹᡒ C 䇁㿔⿟ᑣЁⱘ෎ᴀ䇁⊩䫭䇃ˈབ೚ᣀোǃᮍᣀোǃ㢅 䆌ጠ༫DŽܕᄫヺᐌ䞣DŽ೼ C 䇁㿔Ёˈ⊼䞞ϡ 㒗д 1•23 㓪ݭϔϾߴ䰸 C 䇁㿔⿟ᑣЁ᠔᳝ⱘ⊼䞞䇁হDŽ㽕ℷ⹂໘⧚ᏺᓩোⱘᄫヺІϢ DŽމ೼ᣛᅮⱘ߫ࠡ≵᳝ぎḐ៪ࠊ㸼ヺᯊⱘᚙ ೼䕧ܹ㸠ⱘ㄀ n ߫Пࠡⱘ᳔ৢϔϾ䴲ぎḐПৢDŽ㽕ֱ䆕⿟ᑣ㛑໳ᱎ㛑ഄ໘⧚䕧ܹ㸠ᕜ䭓ҹঞ 㒗д 1•22 㓪ݭϔϾ⿟ᑣˈᡞ䕗䭓ⱘ䕧ܹ㸠Āᡬā៤ⷁϔѯⱘϸ㸠៪໮㸠ˈᡬ㸠ⱘԡ㕂 Ͼࠊ㸼ヺ៪㗙ϔϾぎḐ䛑ৃҹࠄ䖒ϟϔϾࠊ㸼ヺ㒜ℶԡᯊˈ䗝⫼ાϔ⾡᳓ᤶᄫヺ↨䕗ད˛ ⳌৠDŽᔧՓ⫼ϔމ䆒ࠊ㸼ヺ㒜ℶԡⱘԡ㕂Ϣ㒗д 1•20 ⱘ detab ⿟ᑣⱘᚙ؛П䯈ⱘ䯈䱨ϡবDŽ 㒗д 1•21 㓪ݭ⿟ᑣ entabˈᇚぎḐІ᳓ᤶЎ᳔ᇥ᭄䞣ⱘࠊ㸼ヺ੠ぎḐˈԚ㽕ֱᣕऩ䆡 Ͼࠊ㸼ヺ㒜ℶԡDŽn ᑨ䆹԰Ўব䞣䖬ᰃヺোᐌ䞣ਸ਼˛ 䆒ࠊ㸼ヺ㒜ℶԡⱘԡ㕂ᰃ೎ᅮⱘˈ↨བ↣䱨 n ߫ህӮߎ⦄ϔ؛ϟϔϾࠊ㸼ヺ㒜ℶԡⱘഄᮍDŽ ⒵ࠄܙ㒗д 1•20 㓪ݭ⿟ᑣ detabˈᇚ䕧ܹЁⱘࠊ㸼ヺ᳓ᤶ៤䗖ᔧ᭄ⳂⱘぎḐˈՓぎḐ ϟ䴶ⱘ޴Ͼ㒗д↨ᴀゴࠡ䴶㓪ݭⱘ⿟ᑣ㽕໡ᴖϔѯDŽ ㋴ˈ៥ӀᏆ㒣㛑໳㓪ݭߎⳌᔧ㾘῵ⱘ᳝⫼ⱘ⿟ᑣDŽᓎ䆂䇏㗙㢅ϔѯᯊ䯈㓪ݭ⿟ᑣ԰Ў㒗дDŽ ܗࡽѢ䖭ѯᇥ䞣ⱘ䇁㿔׳ࠄⳂࠡЎℶˈ៥ӀᏆ㒣ᇍ C 䇁㿔ⱘӴ㒳Ḍᖗ䚼ߚ䖯㸠њҟ㒡DŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! short int sh; ⫼Ѣ䰤ᅮᭈൟ˖ ℸ໪ˈ䖬ৃҹ೼䖭ѯ෎ᴀ᭄᥂㉏ൟⱘࠡ䴶ࡴϞϔѯ䰤ᅮヺDŽshort Ϣ long ϸϾ䰤ᅮヺ double ঠ㊒ᑺ⍂⚍ൟ float ऩ㊒ᑺ⍂⚍ൟ int ᭈൟˈ䗮ᐌড᯴њ᠔⫼ᴎ఼Ёᭈ᭄ⱘ᳔㞾✊䭓ᑺ char ᄫヺൟˈऴ⫼ϔϾᄫ㡖ˈৃҹᄬᬒᴀഄᄫヺ䲚ЁⱘϔϾᄫヺ C 䇁㿔াᦤկњϟ߫޴⾡෎ᴀ᭄᥂㉏ൟ˖ 2.2. ᭄᥂㉏ൟঞ䭓ᑺ ϔ㠀Փ⫼䕗ⷁⱘব䞣ৡ˄ᇸ݊ᰃᕾ⦃᥻ࠊব䞣˅ˈ໪䚼ব䞣Փ⫼䕗䭓ⱘৡᄫDŽ ϡᆍᯧᓩ䍋⏋⎚DŽሔ䚼ব䞣خ䗝ᢽⱘব䞣ৡ㽕㛑໳ሑ䞣Ңᄫ䴶Ϟ㸼䖒ব䞣ⱘ⫼䗨ˈ䖭ḋ ᄫЁⱘᄫヺ䛑ᖙ乏ᇣݭDŽع᠔᳝݇ ব䞣ৡDŽخ⫼ԐѢ ifǃelseǃintǃfloat ㄝ݇䬂ᄫᰃֱ⬭㒭䇁㿔ᴀ䑿Փ⫼ⱘˈϡ㛑ᡞᅗӀ ҙֱ䆕ࠡ 6 ϾᄫヺⱘᚳϔᗻˈᑊϨϡऎߚ໻ᇣݭDŽ㉏ޚ੠∛㓪⿟ᑣⱘDŽᇍѢ໪䚼ৡˈANSI ᷛ ᇣѢ 31ˈ䖭ᰃ಴Ў∛㓪⿟ᑣ੠ࡴ䕑⿟ᑣৃ㛑ӮՓ⫼䖭ѯ໪䚼ৡˈ㗠䇁㿔ᴀ䑿ᰃ᮴⊩᥻ࠊࡴ䕑 ᇍѢݙ䚼ৡ㗠㿔ˈ㟇ᇥࠡ 31 Ͼᄫヺᰃ᳝ᬜⱘDŽߑ᭄ৡϢ໪䚼ব䞣ৡࣙ৿ⱘᄫヺ᭄Ⳃৃ㛑 Ӵ㒳ⱘ C 䇁㿔⫼⊩Ёˈব䞣ৡՓ⫼ᇣݭᄫ↡ˈヺোᐌ䞣ৡܼ䚼Փ⫼໻ݭᄫ↡DŽ ৡϡ㽕ҹϟߦ㒓ᓔ༈DŽ໻ݭᄫ↡Ϣᇣݭᄫ↡ᰃ᳝ऎ߿ⱘˈ᠔ҹˈx ϢX ᰃϸϾϡৠⱘৡᄫDŽ೼ ᐌ⫼Ѣੑৡ䕗䭓ⱘব䞣ৡˈҹᦤ催݊ৃ䇏ᗻDŽ⬅Ѣ՟⿟ⱘৡᄫ䗮ᐌҹϟߦ㒓ᓔ༈ˈ಴ℸব䞣 ᰃᄫ↡ˈ䗮خᄫᰃ⬅ᄫ↡੠᭄ᄫ㒘៤ⱘᑣ߫ˈԚ݊㄀ϔϾᄫヺᖙ乏Ўᄫ↡DŽϟߦ㒓“_ā㹿ⳟ ᇍব䞣ⱘੑৡϢヺোᐌ䞣ⱘੑৡᄬ೼ϔѯ䰤ࠊᴵӊˈ䖭ϔ⚍៥Ӏ೼㄀ 1 ゴ≵᳝䇈ᯢDŽৡ 2.1. ব䞣ৡ ҹ䗖ড়Ѣ᳈໮ⱘ᭄᥂㉏ൟDŽˈܙᔎࠊ䕀ᤶ㾘߭䖯㸠њᠽ 䖬ᇍㅫᴃ㉏ൟП䯈ⱘ㞾ࡼޚᇍ䈵ৃҹໄᯢЎ const˄ᐌ䞣˅㉏ൟˈ㸼ᯢ݊ؐϡ㛑ׂᬍDŽ䆹ᷛ ヺІᐌ䞣ৃҹ೼㓪䆥ᯊ䖲᥹DŽANSI C 䖬ᬃᣕᵮВ㉏ൟˈ䆹䇁㿔⡍ᗻ㒣䖛њ䭓ᳳⱘথሩᠡᔶ៤DŽ ヺᐌ䞣DŽ⍂⚍䖤ㅫৃҹҹऩ㊒ᑺ䖯㸠ˈ䖬ৃҹՓ⫼᳈催㊒ᑺⱘ long double ㉏ൟ䖤ㅫDŽᄫ signed˄ᏺヺো˅੠ unsigned˄᮴ヺো˅ϸ⾡ᔶᓣˈϨৃҹ㸼⼎᮴ヺোᐌ䞣Ϣक݁䖯ࠊᄫ њ䆌໮ᇣⱘׂᬍϢ๲㸹DŽ᠔᳝ᭈൟ䛑ࣙᣀخᇍ䇁㿔ⱘ෎ᴀ㉏ൟϢ㸼䖒ᓣޚANSI ᷛ ᆍDŽ ᅮ䆹ᇍ䈵ৃপؐⱘ䲚ড়ҹঞৃҹᇍ䆹ᇍ䈵ᠻ㸠ⱘ᪡԰DŽᴀゴᇚ䆺㒚䆆䗄䖭ѯݙއᇍ䈵ⱘ㉏ൟ ᣛᅮব䞣ⱘ߱ؐDŽ䖤ㅫヺᣛᅮᇚ㽕䖯㸠ⱘ᪡԰DŽ㸼䖒ᓣ߭ᡞব䞣Ϣᐌ䞣㒘ড়䍋ᴹ⫳៤ᮄⱘؐDŽ ব䞣੠ᐌ䞣ᰃ⿟ᑣ໘⧚ⱘϸ⾡෎ᴀ᭄᥂ᇍ䈵DŽໄᯢ䇁হ䇈ᯢব䞣ⱘৡᄫঞ㉏ൟˈгৃҹ ㄀2ゴ㉏ൟǃ䖤ㅫヺϢ㸼䖒ᓣ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᅗᭈ᭄ϔḋখϢ᭄ؐ䖤ㅫˈ݊ڣгৃҹ ᖗ䆹ᄫヺᇍᑨⱘ݋ԧؐˈ๲ࡴњ⿟ᑣⱘᯧ䇏ᗻDŽᄫヺᐌ䞣ϔ㠀⫼ᴹϢ݊ᅗᄫヺ䖯㸠↨䕗ˈԚ ≵᳝݇㋏DŽབᵰ⫼ᄫヺ'0'ҷ᳓䖭ϾϢ݋ԧᄫヺ䲚᳝݇ⱘؐ˄↨བ 48˅ˈ䙷Мˈ⿟ᑣህ᮴䳔݇ 䲚Ёⱘ᭄ؐህᰃᄫヺᐌ䞣ⱘؐDŽ՟བˈ೼ ASCII ᄫヺ䲚Ёˈᄫヺ'0'ⱘؐЎ 48ˈᅗϢ᭄ؐ 0 ϔϾᄫヺᐌ䞣ᰃϔϾᭈ᭄ˈкݭᯊᇚϔϾᄫヺᣀ೼ऩᓩোЁˈབˈ'x'DŽᄫヺ೼ᴎ఼ᄫヺ unsigned long ㉏ൟ˄᮴ヺো䭓ᭈൟ˅ⱘᐌ䞣ˈ݊ؐㄝѢक䖯ࠊ᭄ 15DŽ ҹՓ⫼ৢ㓔 L 㸼⼎ long ㉏ൟˈՓ⫼ৢ㓔 U 㸼⼎ unsigned ㉏ൟDŽ՟བˈ0XFUL ᰃϔϾ ៤ܿ䖯ࠊᔶᓣ 037ˈгৃҹݭ៤क݁䖯ࠊᔶᓣ 0x1f ៪ 0X1FDŽܿ䖯ࠊϢक݁䖯ࠊⱘᐌ䞣гৃ ⼎ᅗЎܿ䖯ࠊᔶᓣ˗ࠡ㓔Ў 0x ៪ 0Xˈ߭㸼⼎ᅗЎक݁䖯ࠊᔶᓣDŽ՟བˈक䖯ࠊ᭄ 31 ৃҹݭ ᭈൟ᭄䰸њ⫼क䖯ࠊ㸼⼎໪ˈ䖬ৃҹ⫼ܿ䖯ࠊ៪क݁䖯ࠊ㸼⼎DŽᏺࠡ㓔 0 ⱘᭈൟᐌ䞣㸼 ⼎ long double ㉏ൟDŽ ≵᳝ৢ㓔ⱘ⍂⚍᭄ᐌ䞣Ў double ㉏ൟDŽৢ㓔 f ៪ F 㸼⼎ float ㉏ൟˈ㗠ৢ㓔 l ៪ L ߭㸼 ⍂⚍᭄ᐌ䞣Ёࣙ৿ϔϾᇣ᭄⚍˄བ 123.4˅៪ϔϾᣛ᭄˄བ 1e•2˅ˈгৃҹϸ㗙䛑᳝DŽ ⧚DŽ᮴ヺোᐌ䞣ҹᄫ↡ u ៪ U 㒧ሒDŽৢ㓔 ul ៪ UL 㸼ᯢᰃ unsigned long ㉏ൟDŽ 123456789LDŽབᵰϔϾᭈ᭄໾໻ҹ㟇Ѣ᮴⊩⫼ int ㉏ൟ㸼⼎ᯊˈгᇚ㹿ᔧ԰ long ㉏ൟ໘ ㉏ԐѢ 1234 ⱘᭈ᭄ᐌ䞣ሲѢ int ㉏ൟDŽlong ㉏ൟⱘᐌ䞣ҹᄫ↡ l ៪ L 㒧ሒˈབ 2.3. ᐌ䞣 ⦄DŽৢϔ⾡ᮍ⊩ⱘᅲ⦄䕗ೄ䲒ϔѯˈ಴Ў㽕⹂ᅮ৘⾡⍂⚍㉏ൟⱘপؐ㣗ೈDŽ ༈᭛ӊЁⱘⳌᑨؐҹঞⳈ᥹䅵ㅫϸ⾡ᮍᓣᅲޚint Ϣ long ㉏ൟব䞣ⱘপؐ㣗ೈDŽ䞛⫼ᠧॄᷛ 㒗д 2•1 㓪ݭϔϾ⿟ᑣҹ⹂ᅮߚ߿⬅ signed ঞ unsigned 䰤ᅮⱘ charǃshortǃ ӊϢЁᡒࠄˈ䖭ѯݙᆍᇚ೼䰘ᔩ B Ё䅼䆎DŽ ༈᭛ޚ᳝݇䖭ѯ㉏ൟ䭓ᑺᅮНⱘヺোᐌ䞣ҹঞ݊ᅗϢᴎ఼੠㓪䆥఼᳝݇ⱘሲᗻৃҹ೼ᷛ ৠⱘ䭓ᑺDŽ ⦄DŽfloatǃdouble Ϣ long double ㉏ൟৃҹ㸼⼎Ⳍৠⱘ䭓ᑺˈгৃҹ㸼⼎ϸ⾡៪ϝ⾡ϡ Ѣ݋ԧⱘᅲއlong double ㉏ൟ㸼⼎催㊒ᑺⱘ⍂⚍᭄DŽৠᭈൟϔḋˈ⍂⚍ൟⱘ䭓ᑺгপ Ѣ݋ԧᴎ఼ˈԚৃᠧॄᄫヺᘏᰃℷؐDŽއᰃ৺ᏺヺো߭প ব䞣ⱘপؐ㣗ೈ߭Ў•128̚127˄೼䞛⫼ᇍѠⱘ㸹ⷕⱘᴎ఼Ϟ˅DŽϡᏺ䰤ᅮヺⱘ char ㉏ൟᇍ䈵 䈵ऴ⫼ 8 ԡˈ䙷М unsigned char ㉏ൟব䞣ⱘপؐ㣗ೈЎ 0̚255ˈ㗠 signed char ㉏ൟ ⱘ᭄ᘏᰃℷؐ៪ 0ˈᑊ䙉ᅜㅫᴃ῵ 2n ᅮᕟˈ݊Ё n ᰃ䆹㉏ൟऴ⫼ⱘԡ᭄DŽ՟བˈབᵰ char ᇍ ㉏ൟ䰤ᅮヺ signed Ϣ unsigned ৃ⫼Ѣ䰤ᅮ char ㉏ൟ៪ӏԩᭈൟDŽunsigned ㉏ൟ ㉏ൟϡᕫ䭓Ѣ int ㉏ൟˈ㗠 int ㉏ൟϡᕫ䭓Ѣ long ㉏ൟDŽ Ԛ㽕䙉ᕾϟ߫䰤ࠊ˖short Ϣ int ㉏ൟ㟇ᇥЎ 16 ԡ ˈ㗠 long ㉏ൟ㟇ᇥЎ 32 ԡˈᑊϨ short ԡˈint ㉏ൟৃҹЎ 16 ԡ៪ 32 ԡDŽ৘㓪䆥఼ৃҹḍ᥂⹀ӊ⡍ᗻ㞾Џ䗝ᢽড়䗖ⱘ㉏ൟ䭓ᑺˈ int 䗮ᐌҷ㸼⡍ᅮᴎ఼Ёᭈ᭄ⱘ㞾✊䭓ᑺDŽshort ㉏ൟ䗮ᐌЎ 16 ԡˈ1ong ㉏ൟ䗮ᐌЎ 32 short Ϣ long ϸϾ䰤ᅮヺⱘᓩܹৃҹЎ៥Ӏᦤկ⒵䎇ᅲ䰙䳔㽕ⱘϡৠ䭓ᑺⱘᭈൟ᭄DŽ DŽخ೼Ϟ䗄䖭⾡㉏ൟⱘໄᯢЁˈ݇䬂ᄫ int ৃҹⳕ⬹DŽ䗮ᐌᕜ໮Ҏгдᛃ䖭М long int counter; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! "hello," " world" ヺІᐌ䞣䖲᥹䍋ᴹˈ՟བˈϟ߫ᔶᓣ˖ ヺᑣ߫ৠḋгৃҹ⫼೼ᄫヺІЁDŽ೼ᄫヺІЁՓ⫼\"㸼⼎ঠᓩোᄫヺDŽ㓪䆥ᯊৃҹᇚ໮Ͼᄫ 䛑ᰃᄫヺІDŽঠᓩোϡᰃᄫヺІⱘϔ䚼ߚˈᅗা⫼Ѣ䰤ᅮᄫヺІDŽᄫヺᐌ䞣ЁՓ⫼ⱘ䕀Нᄫ "" /* ぎᄫヺІ */ ៪ "I am a string" ՟བ˖ ᄫヺІᐌ䞣гিᄫヺІᄫ䴶ؐˈᰃ⫼ঠᓩোᣀ䍋ᴹⱘ 0 Ͼ៪໮Ͼᄫヺ㒘៤ⱘᄫヺᑣ߫DŽ int days[31+28+LEAP+31+30+31+30+31+31+30+31+30+31]; #define LEAP 1 /* in leap years */ ៪ char line[MAXLINE+1]; #define MAXLINE 1000 ᅗৃҹߎ⦄೼ᐌ䞣ৃҹߎ⦄ⱘӏԩԡ㕂ˈ՟བ˖ ᐌ䞣㸼䖒ᓣᰃҙҙাࣙ৿ᐌ䞣ⱘ㸼䖒ᓣDŽ䖭⾡㸼䖒ᓣ೼㓪䆥ᯊ∖ؐˈ㗠ϡ೼䖤㸠ᯊ∖ؐDŽ ҹᔎ䇗ᶤѯ㸼䖒ᓣⱘᄫヺሲᗻˈԚ᭄݊ᄫؐЎ 0DŽ ᄫヺᐌ䞣'\0'㸼⼎ؐЎ 0 ⱘᄫヺˈгህᰃぎᄫヺ˄null˅DŽ៥Ӏ䗮ᐌ⫼'\0'ⱘᔶᓣҷ᳓ 0ˈ \v 㒉৥ࠊ㸼ヺ \t ῾৥ࠊ㸼ヺ \xhh क݁䖯ࠊ᭄ \r ಲ䔺ヺ \ooo ܿ䖯ࠊ᭄ \n ᤶ㸠ヺ \" ঠᓩো \f ᤶ义ヺ \' ऩᓩো \b ಲ䗔ヺ \? 䯂ো \a ડ䪗ヺ \\ ড᭰ᴴ ANSI C 䇁㿔Ёⱘܼ䚼䕀Нᄫヺᑣ߫བϟ᠔⼎˖ #define BELL '\x7' /* ASCII bell character */ #define VTAB '\xb' /* ASCII vertical tab */ Ϟ䗄䇁হгৃҹ⫼क݁䖯ࠊⱘᔶᓣкݭЎ˖ #define BELL '\007' /* ASCII bell character */ #define VTAB '\013' /* ASCII vertical tab */ ߫ᔶᓣкݭ䇁হ˖ 㸼⼎ˈ݊Ёˈhh ᰃϔϾ៪໮Ͼक݁䖯ࠊ᭄ᄫ˄0…9ˈa…fˈA…F˅DŽ಴ℸˈ៥Ӏৃҹᣝ✻ϟ '\xhh' ৃҹ⫼ 㸼⼎ӏᛣⱘᄫ㡖໻ᇣⱘԡ῵ᓣDŽ݊Ёˈooo ҷ㸼 1̚3 Ͼܿ䖯ࠊ᭄ᄫ˄0…7˅DŽ䖭⾡ԡ῵ᓣ䖬 '\ooo' ϸϾᄫヺˈԚা㸼⼎ϔϾᄫヺDŽ঺໪ˈ៥Ӏৃҹ⫼ڣヺᑣ߫ⳟ䍋ᴹ ᶤѯᄫヺৃҹ䗮䖛䕀Нᄫヺᑣ߫˄՟བˈᤶ㸠ヺ\n˅㸼⼎Ўᄫヺ੠ᄫヺІᐌ䞣DŽ䕀Нᄫ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᅮϔ⾡ব䞣㉏ൟˈৢ䴶᠔ᏺⱘব䞣㸼ৃҹࣙ৿ϔϾ៪໮Ͼ䆹㉏ൟⱘব䞣DŽ՟བ˖ ໄᯢৢՓ⫼ˈሑㅵᶤѯব䞣ৃҹ䗮䖛Ϟϟ᭛䱤ᓣഄໄᯢDŽϔϾໄᯢᣛܜ᠔᳝ব䞣䛑ᖙ乏 2.4. ໄᯢ #define ᳈݋Ӭ࢓DŽℸ໪ˈ䇗䆩⿟ᑣৃҹҹヺোᔶᓣᠧॄߎᵮВব䞣ⱘؐDŽ ⱘؐᰃ৺Ў䆹ᵮВⱘ᳝ᬜؐDŽϡ䖛ˈᵮВব䞣ᦤկ䖭⾡Ẕᶹˈ಴ℸᵮВ↨ټൟⱘব䞣Ёᄬ ᅗⱘӬ࢓೼Ѣᐌ䞣ؐৃҹ㞾ࡼ⫳៤DŽሑㅵৃҹໄᯢ enum ㉏ൟⱘব䞣ˈԚ㓪䆥఼ϡẔᶹ䖭⾡㉏ ᵮВЎᓎゟᐌ䞣ؐϢৡᄫП䯈ⱘ݇㘨ᦤկњϔ⾡߽֓ⱘᮍᓣDŽⳌᇍѢ#define 䇁হᴹ䇈ˈ ϡৠᵮВЁⱘৡᄫᖙ乏ѦϡⳌৠDŽৠϔᵮВЁϡৠⱘৡᄫৃҹ݋᳝ⳌৠⱘؐDŽ /* FEB ⱘؐЎ 2ˈMAR ⱘؐЎ 3ˈձℸ㉏᥼ */ JUL, AUG, SEP, OCT, NOV, DEC }; enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, NEWLINE = '\n', VTAB = '\v', RETURN = '\r' }; enum escapes { BELL = '\a', BACKSPACE = '\b', TAB = '\t', খⳟϟ䴶ϸϾ՟ᄤЁⱘ㄀ѠϾ՟ᄤ˖ ᵰাᣛᅮњ䚼ߚᵮВৡⱘؐˈ䙷М᳾ᣛᅮؐⱘᵮВৡⱘؐᇚձⴔ᳔ৢϔϾᣛᅮؐ৥ৢ䗦๲ˈ ϟˈenum ㉏ൟЁ㄀ϔϾᵮВৡⱘؐЎ 0ˈ㄀ѠϾЎ 1ˈձℸ㉏᥼DŽབމ೼≵᳝ᰒᓣ䇈ᯢⱘᚙ enum boolean { NO, YES }; ᵮВᐌ䞣ᰃ঺໪ϔ⾡㉏ൟⱘᐌ䞣DŽᵮВᰃϔϾᐌ䞣ᭈൟؐⱘ߫㸼ˈ՟བ˖ ϔϾᄫヺ˄ेᄫ↡ x˅ҹঞϔϾ㒧ᴳヺ'\0'ⱘᄫヺ᭄㒘DŽ ࠡ㗙ᰃϔϾᭈ᭄ˈ݊ؐᰃᄫ↡ x ೼ᴎ఼ᄫヺ䲚Ёᇍᑨⱘ᭄ؐ˄ݙ䚼㸼⼎ؐ˅˗ৢ㗙ᰃϔϾࣙ৿ ៥Ӏᑨ䆹᧲⏙Ἦᄫヺᐌ䞣Ϣҙࣙ৿ϔϾᄫヺⱘᄫヺІП䯈ⱘऎ߿˖'x'Ϣ"x"ᰃϡৠⱘDŽ ༈᭛ӊЁໄᯢњ strlen ੠݊ᅗᄫヺІߑ᭄DŽޚᷛ } return i; ++i; while (s[i] != '\0') int i; { int strlen(char s[]) /* strlen: return length of s */ ϟ䴶ᰃ៥Ӏ䆒䅵ⱘ strlen ߑ᭄ⱘϔϾ⠜ᴀ˖ ᑧߑ᭄ strlen(s)ৃҹ䖨ಲᄫヺІখ᭄ s ⱘ䭓ᑺˈԚ䭓ᑺϡࣙᣀ᳿ሒⱘ'\0'DŽޚІⱘ䭓ᑺDŽᷛ ᮍ⊩г䇈ᯢˈC 䇁㿔ᇍᄫヺІⱘ䭓ᑺ≵᳝䰤ࠊˈԚ⿟ᑣᖙ乏ᠿᦣᅠᭈϾᄫヺІৢᠡ㛑⹂ᅮᄫヺ ᭄↨ᣀ೼ঠᓩোЁⱘᄫヺ᭄໮ϔϾDŽ䖭⾡㸼⼎ܗऩټᄫヺІⱘ⠽⧚ᄬټІⱘ㒧ሒˈ಴ℸDŽᄬ Ңᡔᴃ㾦ᑺⳟˈᄫヺІᐌ䞣ህᰃᄫヺ᭄㒘DŽᄫヺІⱘݙ䚼㸼⼎Փ⫼ϔϾぎᄫヺ'\0'԰Ў ᄫヺІᐌ䞣ⱘ䖲᥹Ўᇚ䕗䭓ⱘᄫヺІߚᬷ೼㢹ᑆϾ⑤᭛ӊ㸠ЁᦤկњᬃᣕDŽ "hello, world" ㄝӋѢ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! printf("%d is not a leap year\n", year); else printf("%d is a leap year\n", year); if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ৃҹ⫼ϟ߫䇁হ߸ᮁ䯄ᑈ˖ ᭈ䰸Ԛϡ㛑㹿 100 ᭈ䰸ˈ䙷М䖭ϔᑈህᰃ䯄ᑈˈℸ໪ˈ㛑㹿 400 ᭈ䰸ⱘᑈӑгᰃ䯄ᑈDŽ಴ℸˈ ⱘ㒧ᵰᰃ x 䰸ҹ y ⱘԭ᭄ˈᔧ x 㛑㹿 y ᭈ䰸ᯊˈ݊ؐЎ 0DŽ՟བˈབᵰᶤϔᑈⱘᑈӑ㛑㹿 4 x % y ߚDŽ㸼䖒ᓣ ㅫᴃ䖤ㅫヺࣙᣀ˖+ǃ•ǃ*ǃ/ǃ%˄প῵䖤ㅫヺ˅DŽᭈ᭄䰸⊩Ӯ៾ᮁ㒧ᵰЁⱘᇣ᭄䚼ܗѠ 2.5. ㅫᴃ䖤ㅫヺ Ѣ݋ԧⱘᅲ⦄DŽއབᵰ䆩೒ׂᬍ const 䰤ᅮヺ䰤ᅮⱘؐˈ݊㒧ᵰপ int strlen(const char[]); ㋴ⱘؐ˖ܗconst 䰤ᅮヺгৃ䜡ড়᭄㒘খ᭄Փ⫼ˈᅗ㸼ᯢߑ᭄ϡ㛑ׂᬍ᭄㒘 const char msg[] = "warning: "; const double e = 2.71828182845905; ㋴ⱘؐ䛑ϡ㛑㹿ׂᬍ˖ܗ᭄㒘㗠㿔ˈconst 䰤ᅮヺᣛᅮ᭄㒘᠔᳝ ӏԩব䞣ⱘໄᯢ䛑ৃҹՓ⫼ const 䰤ᅮヺ䰤ᅮDŽ䆹䰤ᅮヺᣛᅮব䞣ⱘؐϡ㛑㹿ׂᬍDŽᇍ 䞣Ϣ䴭ᗕব䞣ᇚ㹿߱ྟ࣪Ў 0DŽ᳾㒣ᰒᓣ߱ྟ࣪ⱘ㞾ࡼব䞣ⱘؐЎ᳾ᅮНؐ˄े᮴ᬜؐ˅DŽ ϟˈ໪䚼বމ࣪ⱘ㞾ࡼব䞣䛑ᇚ㹿߱ྟ࣪ϔ⃵ˈ݊߱ྟ࣪㸼䖒ᓣৃҹᰃӏԩ㸼䖒ᓣDŽ咬䅸ᚙ ᠻ㸠Пࠡ䖯㸠ˈᑊϨ߱ྟ࣪㸼䖒ᓣᖙ乏Ўᐌ䞣㸼䖒ᓣDŽ↣⃵䖯ܹߑ᭄៪⿟ᑣഫᯊˈᰒᓣ߱ྟ བᵰব䞣ϡᰃ㞾ࡼব䞣ˈ߭া㛑䖯㸠ϔ⃵߱ྟ࣪᪡԰ˈҢὖᗉϞ䆆ˈᑨ䆹ᰃ೼⿟ᑣᓔྟ float eps = 1.0e•5; int limit = MAXLINE + 1; int i = 0; char esc = '\\'; ᔧᇍব䞣䖯㸠߱ྟ࣪ⱘ߱ྟ࣪㸼䖒ᓣDŽ՟བ˖ܙঞϔϾ㸼䖒ᓣˈ䆹㸼䖒ᓣህ 䖬ৃҹ೼ໄᯢⱘৠᯊᇍব䞣䖯㸠߱ྟ࣪DŽ೼ໄᯢЁˈབᵰব䞣ৡⱘৢ䴶㋻䎳ϔϾㄝোҹ ׂᬍDŽ ᣝ✻䖭⾡ᔶᓣкݭҷⷕ䳔㽕ऴ⫼䕗໮ⱘぎ䯈ˈԚ֓Ѣ৥৘ໄᯢ䇁হЁ⏏ࡴ⊼䞞ˈг֓Ѣҹৢ cbar line[1000]; char c; int step; int upper; int lower; Ӌഄݭ៤ϟ߫ᔶᓣ˖ ϔϾໄᯢ䇁হЁⱘ໮Ͼব䞣ৃҹᢚᓔ೼໮Ͼໄᯢ䇁হЁໄᯢDŽϞ䴶ⱘϸϾໄᯢ䇁হгৃҹㄝ char c, 1ine[1000]; int lower, upper, step; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! Ѣϟ߫㉏Ԑⱘ㒧ᵘЁ˖ 䘏䕥䴲䖤ㅫヺ!ⱘ԰⫼ᰃᇚ䴲 0 ᪡԰᭄䕀ᤶЎ 0ˈᇚ᪡԰᭄ 0 䕀ᤶЎ 1DŽ䆹䖤ㅫヺ䗮ᐌ⫼ ߭㒧ᵰؐЎ᭄ؐ 0DŽˈ؛བᵰЎ ḍ᥂ᅮНˈ೼݇㋏㸼䖒ᓣ៪䘏䕥㸼䖒ᓣЁˈབᵰ݇㋏Ўⳳˈ߭㸼䖒ᓣⱘ㒧ᵰؐЎ᭄ؐ 1˗ Ϣ'\n'䖯㸠↨䕗DŽ ᡞߑ᭄䖨ಲؐ䌟ؐ㒭 cˈ✊ৢݡᇚ cܜ˖Ёˈህ䳔㽕Փ⫼೚ᣀোˈ䖭ḋᠡ㛑䖒ࠄ乘ᳳⱘⳂⱘ (c = getchar()) != '’\n' 㸼䖒ᓣ 㑻ˈ಴ℸˈ೼ܜ㑻催Ѣ䌟ؐ䖤ㅫヺⱘӬܜህϡ䳔㽕঺໪ࡴ೚ᣀোDŽԚᰃˈ⬅Ѣ䖤ㅫヺ!=ⱘӬ i >= < <= ݇㋏䖤ㅫヺࣙᣀϟ߫޴Ͼ䖤ㅫヺ˖ 2.6. ݇㋏䖤ㅫヺϢ䘏䕥䖤ㅫヺ 㑻੠㒧ড়ᕟDŽܜᴀゴ᳿ሒⱘ㸼 2•1 ᅠᭈᘏ㒧њ᠔᳝䖤ㅫヺⱘӬ ߭DŽ 㑻ԢDŽㅫᴃ䖤ㅫヺ䞛⫼ҢᎺࠄেⱘ㒧ড়㾘ܜ䖤ㅫヺ+੠•ⱘӬܗ㑻জ↨ϔܜㅫヺ*ǃ/੠%ⱘӬ 㑻Ԣˈ㗠䖤ܜ㑻↨䖤ㅫヺ*ǃ/੠%ⱘӬܜ㑻ˈᅗӀⱘӬܜ䖤ㅫヺ+੠•݋᳝ⳌৠⱘӬܗѠ ḋⱘDŽ ᰃϔމѢ݋ԧᴎ఼ⱘᅲ⦄ˈ䖭੠໘⧚Ϟ⑶៪ϟ⑶ⱘᚙއপⱘᮍ৥ҹঞপ῵䖤ㅫ㒧ᵰⱘヺোপ ϟˈᭈ᭄䰸⊩៾މপ῵䖤ㅫヺ%ϡ㛑ᑨ⫼Ѣ float ៪ double ㉏ൟDŽ೼᳝䋳᪡԰᭄ⱘᚙ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! } return c; else return c + 'a' • 'A'; if (c >= 'A' && c <= 'Z') { int lower(int c) /* lower: convert c to lower case; ASCII only */ ᯴ᇘࠄᇍᑨⱘᇣݭᄫ↡DŽབᵰᕙ䕀ᤶⱘᄫヺϡᰃ໻ݭᄫ↡ˈlower ߑ᭄ᇚ䖨ಲᄫヺᴀ䑿DŽ ߑ᭄ lower ᰃᇚ char ㉏ൟ䕀ᤶЎ int ㉏ൟⱘ঺ϔϾ՟ᄤˈᅗᇚ ASCII ᄫヺ䲚Ёⱘᄫヺ ؐᰃϔϾ䖲㓁ⱘ䗦๲ᑣ߫DŽ ⱘᄫヺ᠔ᇍᑨⱘ᭄ᄫؐˈ䖭ᰃ಴Ў'0'ǃ'l'ㄝ೼ᄫヺ䲚Ёᇍᑨⱘ᭄ټ㛑໳䅵ㅫߎ s[i]Ёᄬ s[i] • '0' ៥Ӏ೼㄀ 1 ゴ䆆䖛ˈ㸼䖒ᓣ } return n; n = 10 * n + (s[i] • '0'); for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i) n = 0; int i, n; { int atoi(char s[]) /* atoi: convert s to integer */ ᭄ᄫ䕀ᤶЎⳌᑨⱘ᭄ؐ˖ 䖭ህЎᅲ⦄ᶤѯᄫヺ䕀ᤶᦤկњᕜ໻ⱘ♉⌏ᗻˈ↨བˈϟ䴶ⱘߑ᭄ atoi ህᰃϔ՟ˈᅗᇚϔІ ⬅Ѣ char ㉏ൟህᰃ䕗ᇣⱘᭈൟˈ಴ℸ೼ㅫᴃ㸼䖒ᓣЁৃҹ㞾⬅Փ⫼ char ㉏ൟⱘব䞣ˈ ؐ䌟㒭䕗ⷁⱘᭈൟব䞣ˈᡞ⍂⚍ൟؐ䌟ؐ㒭ᭈൟব䞣ˈㄝㄝˈԚ䖭ѯ㸼䖒ᓣᑊϡ䴲⊩DŽ ЎϟᷛDŽ䩜ᇍৃ㛑ᇐ㟈ֵᙃ϶༅ⱘ㸼䖒ᓣˈ㓪䆥఼ৃ㛑Ӯ㒭ߎ䄺ਞֵᙃˈ↨བᡞ䕗䭓ⱘᭈൟ 䆌ᡞ float ㉏ൟⱘ㸼䖒ᓣ԰ܕ䆌Փ⫼᮴ᛣНⱘ㸼䖒ᓣˈ՟བˈϡܕⱘব䞣 f Ў⍂⚍ൟ˅DŽϡ ϶༅ֵᙃⱘ䕀ᤶˈ՟བˈ೼䅵ㅫ㸼䖒ᓣ f+i ᯊˈᇚᭈൟব䞣 i ⱘؐ㞾ࡼ䕀ᤶЎ⍂⚍ൟ˄䖭䞠 ㉏ൟDŽϔ㠀ᴹ䇈ˈ㞾ࡼ䕀ᤶᰃᣛᡞĀ↨䕗じⱘā᪡԰᭄䕀ᤶЎĀ↨䕗ᆑⱘā᪡԰᭄ˈᑊϨϡ ᔧϔϾ䖤ㅫヺⱘ޴Ͼ᪡԰᭄㉏ൟϡৠᯊˈህ䳔㽕䗮䖛ϔѯ㾘߭ᡞᅗӀ䕀ᤶЎᶤ⾡݅ৠⱘ 2.7. ㉏ൟ䕀ᤶ ⦃䇁হDŽ 㒗д 2•2 ೼ϡՓ⫼䖤ㅫヺ&&៪||ⱘᴵӊϟ㓪ݭϔϾϢϞ䴶ⱘ for ᕾ⦃䇁হㄝӋⱘᕾ ᰃ᳝ᬜⱘā˅ˈԚᇍѢϔѯ᳈໡ᴖⱘ㒧ᵘৃ㛑Ӯ䲒Ѣ⧚㾷DŽ ᔧ✊ˈᕜ䲒䆘߸Ϟ䗄ϸ⾡ᔶᓣા⾡᳈དDŽ㉏ԐѢ!valid ⱘ⫼⊩䇏䍋ᴹ᳈Ⳉ㾖ϔѯ˄Āབᵰϡ if (valid == 0) ϔ㠀ϡ䞛⫼ϟ߫ᔶᓣ˖ if (!valid) w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! · བᵰ݊ЁϔϾ᪡԰᭄ⱘ㉏ൟЎ longˈ߭ᇚ঺ϔϾ᪡԰᭄г䕀ᤶЎ long ㉏ൟDŽ · ᇚ char Ϣ short ㉏ൟⱘ᪡԰᭄䕀ᤶЎ int ㉏ൟ˗ · བᵰ݊ЁϔϾ᪡԰᭄ⱘ㉏ൟЎ floatˈ߭ᇚ঺ϔϾ᪡԰᭄䕀ᤶЎ float ㉏ൟ˗ · བᵰ݊ЁϔϾ᪡԰᭄ⱘ㉏ൟЎ doubleˈ߭ᇚ঺ϔϾ᪡԰᭄䕀ᤶЎ double ㉏ൟ˗ double ㉏ൟ˗ · བᵰ݊ЁϔϾ᪡԰᭄ⱘ㉏ൟЎ long doubleˈ߭ᇚ঺ϔϾ᪡԰᭄䕀ᤶЎ 1ong 䖭ѯ䴲ℷᓣⱘ㾘߭ህৃҹњ˖ 㡖䆺㒚ഄ߫ߎњ䖭ѯ䕀ᤶ㾘߭DŽԚᰃˈབᵰ≵᳝ unsigned ㉏ൟⱘ᪡԰᭄ˈ߭া㽕Փ⫼ϟ䴶 㽕ᡞĀ䕗Ԣāⱘ㉏ൟᦤछЎĀ䕗催āⱘ㉏ൟˈ䖤ㅫⱘ㒧ᵰЎ䕗催ⱘ㉏ൟDŽ䰘ᔩ A.6ܜ䖤ㅫПࠡ 䖤ㅫヺˈ↨བ+៪*˅ⱘϸϾ᪡԰᭄݋᳝ϡৠⱘ㉏ൟˈ䙷М೼䖯㸠ܗϾ᪡԰᭄ⱘ䖤ㅫヺ⿄ЎѠ 䖤ㅫヺ˄݋᳝ϸܗϟӮ䖯㸠䱤ᓣⱘㅫᴃ㉏ൟ䕀ᤶDŽϔ㠀ᴹ䇈ˈབᵰѠމC 䇁㿔Ёˈᕜ໮ᚙ ⴔĀ䴲 0”ˈ䖭Ѡ㗙П䯈≵᳝ऎ߿DŽ ᵰЎⳳᯊৃ㛑䖨ಲӏᛣⱘ䴲 0 ؐDŽ೼ ifǃwhileǃfor ㄝ䇁হⱘ⌟䆩䚼ߚЁˈĀⳳāህᛣੇ ᴹ䇈ˈᔧ c Ў᭄ᄫᯊˈd ⱘؐЎ lˈ৺߭ d ⱘؐЎ 0DŽԚᰃˈᶤѯߑ᭄˄↨བ isdigit˅೼㒧 d = c >= '0' && c <= '9' ᯊˈ㸼䖒ᓣⱘؐЎ 0DŽ಴ℸˈᇍѢ䌟ؐ䇁হ؛ؐЎ 1˗ᔧ߸ᅮ㒧ᵰЎ ᔧ݇㋏㸼䖒ᓣ˄བ i>j˅ҹঞ⬅&&ǃ||䖲᥹ⱘ䘏䕥㸼䖒ᓣⱘ߸ᅮ㒧ᵰЎⳳᯊˈ㸼䖒ᓣⱘ ᳔དᣛᅮ signed ៪ unsigned 䰤ᅮヺDŽ 䴲ᄫヺ᭄᥂ˈټ఼Ϟৃ㛑ᰃℷⱘDŽЎњֱ䆕⿟ᑣⱘৃ⿏ỡᗻˈབᵰ㽕೼ char ㉏ൟⱘব䞣Ёᄬ ೼ᄫヺব䞣Ёⱘԡ῵ᓣ೼ᶤѯᴎ఼Ёৃ㛑ᰃ䋳ⱘˈ㗠೼঺ϔѯᴎټᄫヺᘏᰃℷؐDŽԚᰃˈᄬ ᠧॄᄫヺ䲚ЁⱘᄫヺϡӮᰃ䋳ؐˈ಴ℸˈ೼㸼䖒ᓣЁ䖭ѯޚC 䇁㿔ⱘᅮНֱ䆕њᴎ఼ⱘᷛ ⱘᎺ䖍⏏ࡴ 0ˈ䖭ḋᇐ㟈ⱘ䕀ᤶ㒧ᵰؐᘏᰃℷؐDŽ 㸠Āヺোᠽሩā˅DŽ㗠೼঺ϔѯᴎ఼Ёˈᡞ char ㉏ൟؐ䕀ᤶЎ int ㉏ൟᯊˈ೼ char ㉏ൟؐ ఼㒧ᵘП䯈ⱘऎ߿DŽ೼ᶤѯᴎ఼Ёˈབᵰ char ㉏ൟؐⱘ᳔ᎺϔԡЎ 1ˈ߭䕀ᤶЎ䋳ᭈ᭄˄䖯 ൟⱘؐᯊˈ݊㒧ᵰ᳝≵᳝ৃ㛑Ў䋳ᭈ᭄˛ᇍѢϡৠⱘᴎ఼ˈ݊㒧ᵰгϡৠˈ䖭ড᯴њϡৠᴎ োব䞣˄signed˅䖬ᰃᏺヺোব䞣˄unsigned˅DŽᔧᡞϔϾ char ㉏ൟⱘؐ䕀ᤶЎ int ㉏ ᇚᄫヺ㉏ൟ䕀ᤶЎᭈൟᯊˈ៥Ӏ䳔㽕⊼ᛣϔ⚍DŽC 䇁㿔≵᳝ᣛᅮ char ㉏ൟⱘব䞣ᰃ᮴ヺ ᳓ҷDŽ೼ᴀкⱘৢ㓁ݙᆍЁˈ៥ӀᇚՓ⫼ЁᅮНⱘߑ᭄DŽ isdigit(c) ᑧЁⱘߑ᭄ޚৃҹ⫼䆹ᷛ c >= '0' && c <= '9' Ϟ䗄 lower ߑ᭄DŽ㉏Ԑഄˈ⌟䆩䇁হ tolower(c)ߑ᭄ᇚ c 䕀ᤶЎᇣݭᔶᓣ˄བᵰ c Ў໻ݭᔶᓣⱘ䆱˅ˈৃҹՓ⫼ tolower ᳓ҷ ༈᭛ӊᅮНњϔ㒘Ϣᄫヺ䲚᮴݇ⱘ⌟䆩੠䕀ᤶߑ᭄DŽ՟བˈޚ䰘ᔩ B ҟ㒡ⱘᷛ Ёህϡҙ䰤Ѣ䕀ᤶᄫ↡ⱘ໻ᇣݭDŽ ᄫ↡DŽԚᰃˈৢ䴶ϔ⚍ᇍ EBCDIC ᄫヺ䲚ᰃϡ៤ゟⱘˈ಴ℸ䖭ϔߑ᭄԰⫼೼ EBCDIC ᄫヺ䲚 Ў᭄ᄫؐᴹ䇈݋᳝೎ᅮⱘ䯈䱨ˈᑊϨ↣Ͼᄫ↡㸼䛑ᰃ䖲㓁ⱘ——гህᰃ䇈ˈ೼ A̚Z П䯈া᳝ Ϟ䗄䖭Ͼߑ᭄ᰃЎ ASCII ᄫヺ䲚䆒䅵ⱘDŽ೼ ASCII ᄫヺ䲚Ёˈ໻ݭᄫ↡Ϣᇍᑨⱘᇣݭᄫ↡԰ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 㑻䖯㸠њᘏ㒧DŽܜ㑻ˈ㸼 2•1 ᇍ䖤ㅫヺӬܜৠⱘӬ 䖤ㅫヺ݋᳝ⳌܗϾᣛᅮ㉏ൟⱘ n ⱘؐˈn ᴀ䑿ⱘؐᑊ≵᳝ᬍবDŽᔎࠊ㉏ൟ䕀ᤶ䖤ㅫヺϢ݊ᅗϔ ᇚ݊䕀ᤶЎ double ㉏ൟDŽ⊼ᛣˈᔎࠊ㉏ൟ䕀ᤶাᰃ⫳៤ϔܜ೼ᡞ n Ӵ䗦㒭ߑ᭄ sqrt Пࠡ sqrt((double) n) n ᰃᭈ᭄ˈৃҹՓ⫼ double ㉏ൟˈབᵰ໘⧚ϡᔧˈ㒧ᵰৃ㛑Ӯ᮴ᛣН˄sqrt ೼Ёໄᯢ˅DŽ಴ℸˈབᵰ ᣛᅮⱘ㉏ൟⱘᶤϾব䞣ˈ✊ৢݡ⫼䆹ব䞣᳓ᤶϞ䗄ᭈᴵ䇁হDŽ՟བˈᑧߑ᭄ sqrt ⱘখ᭄Ў 㹿䌟ؐ㒭㉏ൟৡܜ⹂৿Н˖೼Ϟ䗄䇁হЁˈ㸼䖒ᓣ佪ޚ៥Ӏৃҹ䖭ḋᴹ⧚㾷ᔎࠊ㉏ൟ䕀ᤶⱘ (㉏ൟৡ) 㸼䖒ᓣ ൟ䕀ᤶDŽ೼ϟ߫䇁হЁˈ㸼䖒ᓣᇚᣝ✻Ϟ䗄䕀ᤶ㾘߭㹿䕀ᤶЎ㉏ൟৡᣛᅮⱘ㉏ൟ˖ 䖤ㅫヺᔎࠊ䖯㸠ᰒᓣ㉏ܗ᳔ৢˈ೼ӏԩ㸼䖒ᓣЁ䛑ৃҹՓ⫼ϔϾ⿄Ўᔎࠊ㉏ൟ䕀ᤶⱘϔ Ў int ៪ double ㉏ൟDŽ double ㉏ൟDŽ಴ℸˈेՓ䇗⫼ߑ᭄ⱘখ᭄Ў char ៪ float ㉏ൟˈ៥Ӏгᡞߑ᭄খ᭄ໄᯢ ϟˈchar Ϣ short ㉏ൟ䛑ᇚ㹿䕀ᤶЎ int ㉏ൟˈfloat ㉏ൟᇚ㹿䕀ᤶЎމ᳝ߑ᭄ॳൟⱘᚙ ⬅Ѣߑ᭄䇗⫼ⱘখ᭄ᰃ㸼䖒ᓣˈ᠔ҹ೼ᡞখ᭄Ӵ䗦㒭ߑ᭄ᯊгৃ㛑䖯㸠㉏ൟ䕀ᤶDŽ೼≵ Ѣ݋ԧⱘᅲ⦄DŽއЎ float ㉏ൟᯊˈᰃ䖯㸠ಯ㟡Ѩܹ䖬ᰃ៾পপ ൟ䕀ᤶDŽᔧᡞ float ㉏ൟ䕀ᤶЎ int ㉏ൟᯊˈᇣ᭄䚼ߚᇚ㹿៾পᥝ˗ᔧᡞ double ㉏ൟ䕀ᤶ བᵰ x ᰃ float ㉏ൟˈi ᰃ int ㉏ൟˈ䙷М䇁হ x = i Ϣ i = x ೼ᠻ㸠ᯊ䛑㽕䖯㸠㉏ ϔϟˈ߭ᠻ㸠ৢৃ㛑Ӯ϶༅ֵᙃDŽצ䇁হⱘ⃵ᑣ乴 ᠻ㸠ৢˈc ⱘؐᇚֱᣕϡবDŽ᮴䆎ᰃ৺䖯㸠ヺোᠽሩˈ䆹㒧䆎䛑៤ゟDŽԚᰃˈབᵰᡞϸϾ䌟ؐ c = i; i = c; char c; int i; ߫⿟ᑣ↉˖ ᔧᡞ䕗䭓ⱘᭈ᭄䕀ᤶЎ䕗ⷁⱘᭈ᭄៪ char ㉏ൟᯊˈ䍙ߎⱘ催ԡ䚼ߚᇚ㹿϶ᓗDŽ಴ℸˈϟ ࠡ䴶ᦤࠄ䖛ˈ᮴䆎ᰃ৺䖯㸠ヺোᠽሩˈᄫヺൟব䞣䛑ᇚ㹿䕀ᤶЎᭈൟব䞣DŽ ⱘ㉏ൟे䌟ؐ㸼䖒ᓣ㒧ᵰⱘ㉏ൟDŽ 䌟ؐᯊг㽕䖯㸠㉏ൟ䕀ᤶDŽ䌟ؐ䖤ㅫヺে䖍ⱘؐ䳔㽕䕀ᤶЎᎺ䖍ব䞣ⱘ㉏ൟˈᎺ䖍ব䞣 unslgned long ㉏ൟˈ಴㗠៤ЎϔϾ↨䕗໻ⱘℷ᭄DŽ int ㉏ൟⱘ 1U ᇚ㹿ᦤछЎ signed long ㉏ൟ˗Ԛ•1L > 1ULˈ䖭ᰃ಴Ў 1L ᇚ㹿ᦤछЎ ᅮ int ㉏ൟऴ 16 ԡˈlong ㉏ൟऴ 32 ԡˈ䙷Мˈ•1L < 1U ˈ䖭ᰃ಴Ў unsighed؛ˈ໻ᇣDŽ՟བ Ѣᴎ఼Ёϡৠᭈ᭄㉏ൟⱘއヺোؐϢ᮴ヺোؐП䯈ⱘ↨䕗䖤ㅫᰃϢᴎ఼Ⳍ݇ⱘˈ಴ЎᅗӀপ ᔧ㸼䖒ᓣЁࣙ৿ unsigned ㉏ൟⱘ᪡԰᭄ᯊˈ䕀ᤶ㾘߭㽕໡ᴖϔѯDŽЏ㽕ॳ಴೼Ѣˈᏺ ఼ᠻ㸠ᯊ䯈˄ঠ㊒ᑺㅫᴃ䖤ㅫ⡍߿䌍ᯊ˅DŽ ぎ䯈ˈ᳝ᯊгЎњ㡖ⳕᴎټⱘব䞣DŽՓ⫼ float ㉏ൟЏ㽕ᰃЎњ೼Փ⫼䕗໻ⱘ᭄㒘ᯊ㡖ⳕᄬ ༈᭛ӊЁᅮНⱘߑ᭄˅Փ⫼ঠ㊒ᑺ㉏ൟޚН᳝᠔ϡৠDŽϔ㠀ᴹ䇈ˈ᭄ᄺߑ᭄˄བᷛ ᛣˈ㸼䖒ᓣЁ float ㉏ൟⱘ᪡԰᭄ϡӮ㞾ࡼ䕀ᤶЎ double ㉏ൟˈ䖭ϔ⚍Ϣ᳔߱ⱘᅮ⊼ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ব䞣ˈ㉏ԐѢ㸼䖒ᓣ(i+j)++ᰃ䴲⊩ⱘDŽ 䖤ㅫヺা㛑԰⫼Ѣޣᇚ x ⱘؐ㕂Ў 6DŽ䖭ϸᴵ䇁হᠻ㸠ᅠ៤ৢˈব䞣 n ⱘؐ䛑ᰃ 6DŽ㞾๲Ϣ㞾 x = ++n; ᠻ㸠ৢⱘ㒧ᵰᰃᇚ x ⱘؐ㕂Ў 5ˈ㗠 x = n++; ᇍѢՓ⫼ব䞣 n ⱘؐⱘϞϟ᭛ᴹ䇈ˈ++n ੠ n++ⱘᬜᵰᰃϡৠⱘDŽབᵰ n ⱘؐЎ 5ˈ䙷М Փ⫼ব䞣 n ⱘؐˈ✊ৢݡᇚ n ⱘؐ䗦๲ 1DŽгህᰃ䇈ˈܜ⫼ব䞣 n ⱘؐˈ㗠㸼䖒ᓣ n++߭ᰃ ᇚ n ⱘؐ䗦๲ 1ˈ✊ৢݡՓܜ ᰃᇚব䞣 n ⱘؐࡴ 1DŽԚᰃˈᅗӀП䯈᳝ϔ⚍ϡৠDŽ㸼䖒ᓣ++n ϟˈ݊ᬜᵰ䛑މ䴶ˈབ++n˅DŽгৃҹ⫼԰ৢ㓔䖤ㅫヺ˄⫼೼ব䞣ৢ䴶ˈབ n++˅DŽ೼䖭ϸ⾡ᚙ ++Ϣ••䖭ϸϾ䖤ㅫヺ⡍⅞ⱘഄᮍЏ㽕㸼⦄೼˖ᅗӀ᮶ৃҹ⫼԰ࠡ㓔䖤ㅫヺ˄⫼೼ব䞣ࠡ ++nl; if (c = '\n') 1DŽ៥Ӏ㒣ᐌՓ⫼++䖤ㅫヺ䗦๲ব䞣ⱘؐˈབϟ᠔⼎˖ ޣ䖤ㅫヺՓ݊᪡԰᭄䗦ޣ㞾 ⱘ⡍⅞䖤ㅫヺDŽ㞾๲䖤ㅫヺ++Փ݊᪡԰᭄䗦๲ 1ˈޣC 䇁㿔ᦤկњϸϾ⫼Ѣব䞣䗦๲Ϣ䗦 䖤ㅫヺޣ2.8. 㞾๲䖤ㅫヺϢ㞾 䆌ࣙ৿ⱘ᭄ᄫࣙᣀ˖0̚9ǃa̚f ҹঞ A̚FDŽܕ៪ 0X˅䕀ᤶЎϢПㄝӋⱘᭈൟؐDŽᄫヺІЁ 㒗д 2•3 㓪ݭߑ᭄ htoi(s)ˈᡞ⬅क݁䖯ࠊ᭄ᄫ㒘៤ⱘᄫヺІ˄ࣙ৿ৃ䗝ⱘࠡ㓔 0x } next = seed; { void srand(unsigned int seed) /* srand: set seed for rand() */ } return (unsigned int)(next/65536) % 32768; next = next * 1103515245 + 12345; { int rand(void) /* rand: return pseudo•random integer on 0..32767 */ unsigned long int next = 1; ߑ᭄ srandDŽࠡϔϾߑ᭄ rand Փ⫼њᔎࠊ㉏ൟ䕀ᤶDŽ ᑧЁࣙ৿ϔϾৃ⿏ỡⱘᅲ⦄Ӿ䱣ᴎ᭄থ⫳఼ⱘߑ᭄ rand ҹঞϔϾ߱ྟ࣪⾡ᄤ᭄ⱘޚᷛ ϡ䳔㽕Փ⫼ᔎࠊ㉏ൟ䕀ᤶ䖤ㅫヺህৃҹ㞾ࡼᇚᭈ᭄ 2 ᔎࠊ䕀ᤶЎ double ㉏ൟⱘؐ 2.0DŽ root2 = sqrt(2); ϟ߫ߑ᭄䇗⫼˖ double sqrt(double); 㸠㞾ࡼᔎࠊ䕀ᤶDŽ՟བˈᇍѢ sqrt ⱘߑ᭄ॳൟ ϟˈখ᭄ᰃ䗮䖛ߑ᭄ॳൟໄᯢⱘDŽ䖭ḋˈᔧߑ᭄㹿䇗⫼ᯊˈໄᯢᇚᇍখ᭄䖯މ೼䗮ᐌᚙ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ೼ᇚ t Ёⱘᄫヺ䗤Ͼᣋ䋱ࠄ s ⱘሒ䚼ᯊˈব䞣 i ੠ j Փ⫼ⱘ䛑ᰃৢ㓔䖤ㅫヺ++ˈҢ㗠ֱ䆕೼ } ; while ((s[i++] = t[j++]) != '\0') /* copy t */ i++; while (s[i] != '\0') /* find end of s */ i = j = 0; int i, j; { void strcat(char s[], char t[]) /* strcat: concatenate t to end of s; s must be big enough */ ᑧЁⱘ䆹ߑ᭄䖨ಲϔϾᣛ৥ᮄᄫヺІⱘᣛ䩜˅˖ޚݭⱘ䖭Ͼߑ᭄≵᳝ӏԩ䖨ಲؐ˄ᷛ ᅮᄫヺІ s Ё᳝䎇໳ⱘぎ䯈ֱᄬ䖭ϸϾᄫヺІ䖲᥹ⱘ㒧ᵰDŽϟ䴶㓪؛ ⱘሒ䚼DŽߑ᭄ strcat ߑ᭄ strcat(s, t)ˈᅗᇚᄫヺІ t 䖲᥹ࠄᄫヺІ sޚ៥Ӏݡᴹⳟ㄀ϝϾ՟ᄤDŽ㗗㰥ᷛ s[i++] = c; if (c == '\n') ⫼ϟ䴶䖭⾡᳈ㅔ⋕ⱘᔶᓣҷ᳓˖ } ++i; s[i] = c; if (c == '\n') { ⱘ if 䇁হ˖ ៥Ӏ೼㄀ 1 ゴЁ㓪ݭⱘߑ᭄ getline ᰃ㉏Ԑ㒧ᵘⱘ঺໪ϔϾ՟ᄤDŽ៥Ӏৃҹᇚ䆹ߑ᭄Ё } j++; s[j] = s[i]; if (s[i] != c) { ໛໘⧚ϟϔϾᄫヺDŽ݊Ёⱘ if 䇁হᅠܼㄝӋѢϟ߫䇁হ˖ޚ๲ࡴ 1ˈҹ ↣ᔧߎ⦄ϔϾϡᰃ c ⱘᄫヺᯊˈ䆹ߑ᭄ᡞᅗᣋ䋱ࠄ᭄㒘ЁϟᷛЎ j ⱘԡ㕂ˈ䱣ৢᠡᇚ j ⱘؐ } s[j] = '\0'; s[j++] = s[i]; if (s[i] != c) for (i = j = 0; s[i] != '\0'; i++) int i, j; { void squeeze(char s[], int c) /* squeeze: delete all c from s */ Ёߎ⦄ⱘ᠔᳝ᄫヺ c˖ ϟ䳔㽕䜠ᚙ㗗㰥DŽ՟བˈ㗗㰥ϟ䴶ⱘߑ᭄ squeeze(s, c)ˈᅗߴ䰸ᄫヺІ sމԚ೼ᶤѯᚙ nl++; if (c == '\n') ՟བ˖ ϟˈࠡ㓔ᮍᓣ੠ৢ㓔ᮍᓣⱘᬜᵰⳌৠDŽމ೼ϡ䳔㽕Փ⫼ӏԩ݋ԧؐϨҙ䳔㽕䗦๲ব䞣ⱘᚙ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᅮ x ᰃ 16 ԡⱘ᭄ؐDŽ䖭⾡ৃ⿏ỡⱘᔶᓣᑊ≵᳝๲ࡴ乱໪ᓔ䫔ˈ಴؛ⱘ㸼䖒ᓣ㽕དˈৠЎৢ㗙 ᇚᡞxⱘ᳔ৢ6ԡ䆒㕂Ў 0DŽ⊼ᛣˈ㸼䖒ᓣ x & ~077 Ϣᴎ఼ᄫ䭓᮴݇ˈᅗ↨ᔶᓣЎ x & 0177700 x = x & ~077 Ў 1DŽ՟བ˖ 䖤ㅫヺ~⫼Ѣ∖ᭈ᭄ⱘѠ䖯ࠊডⷕˈेߚ߿ᇚ᪡԰᭄৘Ѡ䖯ࠊԡϞⱘ 1 বЎ 0ˈ0 বܗϔ ฿㸹˄ेĀ䘏䕥⿏ԡā˅DŽ ᇍᎺ䖍ぎߎⱘ䚼ߚ⫼ヺোԡ฿㸹˄ेĀㅫᴃ⿏ԡā˅ˈ㗠঺ϔѯᴎ఼߭ᇍᎺ䖍ぎߎⱘ䚼ߚ⫼ 0 ԡᯊˈᎺ䖍ぎߎⱘ䚼ߚᇚ⫼ 0 ฿㸹˗ᔧᇍ signed ㉏ൟⱘᏺヺোؐ䖯㸠ে⿏ᯊˈᶤѯᴎ఼ᇚ 2 ԡ⫼ 0 ฿㸹ˈ䆹㸼䖒ᓣㄝӋѢᇍᎺ᪡԰᭄Ьҹ 4DŽ೼ᇍ unsigned ㉏ൟⱘ᮴ヺোؐ䖯㸠ে⿏ ᅮ˄ে᪡԰᭄ⱘؐᖙ乏ᰃ䴲䋳ؐ˅DŽ಴ℸˈ㸼䖒ᓣ x << 2 ᇚᡞ x ⱘؐᎺ⿏ 2 ԡˈে䖍ぎߎⱘ ⿏ԡ䖤ㅫヺ<<Ϣ>>ߚ߿⫼Ѣᇚ䖤ㅫⱘᎺ᪡԰᭄Ꮊ⿏Ϣে⿏ˈ⿏ࡼⱘԡ᭄߭⬅ে᪡԰᭄ᣛ ⳳؐDŽ՟བˈབᵰ x ⱘؐЎ 1ˈY ⱘؐЎ 2ˈ䙷Мˈx & y ⱘ㒧ᵰЎ 0ˈ㗠 x && y ⱘؐЎ 1DŽ ៥Ӏᖙ乏ᇚԡ䖤ㅫヺ&ǃ|ৠ䘏䕥䖤ㅫヺ&&ǃ||ऎߚᓔᴹˈৢ㗙⫼ѢҢᎺ㟇ে∖㸼䖒ᓣⱘ 0DŽ ᣝԡᓖ៪䖤ㅫヺ^ᔧϸϾ᪡԰᭄ⱘᇍᑨԡϡⳌৠᯊᇚ䆹ԡ䆒㕂Ў 1ˈ৺߭ˈᇚ䆹ԡ䆒㕂Ў 䆹䇁হᇚ x ЁᇍᑨѢ SET_ON ЁЎ 1 ⱘ䙷ѯѠ䖯ࠊԡ㕂Ў 1DŽ x = x | SET_ON˗ ᣝԡ៪䖤ㅫヺ|ᐌ⫼ѢᇚᶤѯѠ䖯ࠊԡ㕂Ў 1ˈ՟བ˖ 䆹䇁হᇚ n Ё䰸 7 ϾԢѠ䖯ࠊԡ໪ⱘ݊ᅗ৘ԡഛ㕂Ў 0DŽ n = n & 0177˖ ᣝԡϢ䖤ㅫヺ&㒣ᐌ⫼Ѣሣ㬑ᶤѯѠ䖯ࠊԡˈ՟བ˖ 䖤ㅫヺ˅ܗ~ ᣝԡ∖ড˄ϔ >> ে⿏ << Ꮊ⿏ ^ ᣝԡᓖ៪˄XOR˅ | ᣝԡ៪˄OR˅ & ᣝԡϢ˄AND˅ ো៪᮴ヺো charǃshortǃintǃlong ㉏ൟ˖ C 䇁㿔ᦤկњ 6 Ͼԡ᪡԰䖤ㅫヺDŽ䖭ѯ䖤ㅫヺা㛑԰⫼Ѣᭈൟ᪡԰᭄ˈेা㛑԰⫼Ѣᏺヺ 2.9. ᣝԡ䖤ㅫヺ ݋᳝ৠḋⱘࡳ㛑ˈԚᅗ䖨ಲⱘᰃᣛ৥䆹ԡ㕂ⱘᣛ䩜DŽ˅ ᑧߑ᭄ strpbrkޚߎ⦄ⱘԡ㕂԰Ў㒧ᵰ䖨ಲDŽབᵰ s1 Ёϡࣙ৿ s2 Ёⱘᄫヺˈ߭䖨ಲ•1DŽ˄ᷛ 㒗д 2•5 㓪ݭߑ᭄ any(s1, s2) ˈᇚᄫヺІ s2 Ёⱘӏϔᄫヺ೼ᄫヺІ s1 Ё㄀ϔ⃵ 䛑ߴ䰸DŽ 㒗д 2•4 squeeze(s1, s2)ˈᇚᄫヺІ s1 ЁӏԩϢᄫヺІ s2 Ёᄫヺऍ䜡ⱘᄫヺ ᕾ⦃䖛⿟Ё i Ϣ j ഛᣛ৥ϟϔϾԡ㕂DŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᣀোᰃᖙϡৃᇥⱘˈ՟བˈ ᅗӀⱘऎ߿೼Ѣˈࠡϔ⾡ᔶᓣ expr1 া䅵ㅫϔ⃵DŽ⊼ᛣˈ೼㄀Ѡ⾡ᔶᓣЁˈexpr2 ϸ䖍ⱘ೚ expr1 = (expr1) op (expr2) ㄝӋѢ˖ expr1 op= expr2 བᵰ expr1 ੠ expr2 ᰃ㸼䖒ᓣˈ䙷М + • * / % << >> & ^ | ヺ op=ˈ݊Ёˈop ৃҹᰃϟ䴶䖭ѯ䖤ㅫヺПϔ˖ 䖤ㅫヺ˄े᳝ᎺǃেϸϾ᪡԰᭄ⱘ䖤ㅫヺˈ↨བ+˅䛑᳝ϔϾⳌᑨⱘ䌟ؐ䖤ㅫܗ໻໮᭄Ѡ ݊Ёⱘ䖤ㅫヺ+=⿄Ў䌟ؐ䖤ㅫヺDŽ i += 2 ߭ৃҹᇚ䖭⾡㸼䖒ᓣ㓽ݭЎϟ߫ᔶᓣ˖ i = i+2 ೼䌟ؐ㸼䖒ᓣЁˈབᵰ㸼䖒ᓣᎺ䖍ⱘব䞣䞡໡ߎ⦄೼㸼䖒ᓣⱘে䖍ˈབ˖ 2.10. 䌟ؐ䖤ㅫヺϢ㸼䖒ᓣ ⿏ߎⱘԡᇚҢ᳔Ꮊッ⿏ܹ˅n˄Ѡ䖯ࠊ˅ԡৢ᠔ᕫࠄⱘؐDŽ 㒗д 2•8 㓪ݭϔϾߑ᭄ rightrot(x, n)ˈ䆹ߑ᭄䖨ಲᇚ x ᕾ⦃ে⿏˄ेҢ᳔েッ ԡֱᣕϡবDŽ ᵰؐ˖ᇚ x ЁҢ㄀ p ԡᓔྟⱘ n Ͼ˄Ѡ䖯ࠊ˅ԡ∖ড˄ेˈ1 ব៤ 0ˈ0 ব៤ 1˅ˈ x ⱘ݊ԭ৘ 㒗д 2•7 㓪ݭϔϾߑ᭄ invert(x, p, n)ˈ䆹ߑ᭄䖨ಲᇍ x ᠻ㸠ϟ߫᪡԰ৢⱘ㒧 ৘ԡֱᣕϡবDŽ ⱘ㒧ᵰؐ˖ᇚ x ЁҢ㄀ p ԡᓔྟⱘ n Ͼ˄Ѡ䖯ࠊ˅ԡ䆒㕂Ў y Ё᳔ে䖍 n ԡⱘؐˈx ⱘ݊ԭ 㒗д 2•6 㓪ݭϔϾߑ᭄ setbits(x, p, n, y)ˈ䆹ߑ᭄䖨ಲᇍ x ᠻ㸠ϟ߫᪡԰ৢ ডˈ䖭ḋህᓎゟњ᳔ে䖍 n ԡܼЎ 1 ⱘሣ㬑ⷕDŽ 䞠Փ⫼䇁হ~0 << n ᇚ~0 Ꮊ⿏ n ԡˈᑊᇚ᳔ে䖍ⱘ n ԡ⫼ 0 ฿㸹DŽݡՓ⫼~䖤ㅫᇍᅗᣝԡপ ݊Ёˈ㸼䖒ᓣ m << (p+1•n) ᇚᳳᳯ㦋ᕫⱘᄫ↉⿏ԡࠄᄫⱘ᳔েッDŽ~0 ⱘ᠔᳝ԡ䛑Ў 1ˈ䖭 } return (x >> (p+1•n)) & ~(~0 << n); { unsigned getbits(unsigned x, int p, int n) /* getbits: get n bits from position p */ ՟བˈgetbits(x, 4, 3)䖨ಲ x Ё㄀ 4ǃ3ǃ2 ϝԡⱘؐDŽ ᅮ᳔ে䖍ⱘϔԡᰃ㄀ 0 ԡˈn Ϣ p 䛑ᰃড়⧚ⱘℷؐDŽ؛᭄㄀ p ԡᓔྟ৥ে᭄ n ԡⱘᄫ↉DŽ䖭䞠 Ўњ䖯ϔℹ䇈ᯢᶤѯԡ䖤ㅫヺˈ៥Ӏᴹⳟߑ᭄ getbits(x, p, n) ˈᅗ䖨ಲ x ЁҢে䖍 Ўˈ~077 ᰃᐌ䞣㸼䖒ᓣˈৃҹ೼㓪䆥ᯊ∖ؐDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! else z = a; if (a > b) ϟ䴶䖭㒘䇁হ˖ 2.11. ᴵӊ㸼䖒ᓣ ⱘ䘧⧚DŽ⫼䖭ϔᮍ⊩䞡ݭ bitcount ߑ᭄ˈҹࡴᖿ݊ᠻ㸠䗳ᑺDŽخϔϾѠ䖯ࠊԡDŽ䇋㾷䞞䖭ḋ 㒗д 2•9 ೼∖ᇍѠⱘ㸹ⷕᯊˈ㸼䖒ᓣ x &= (x – 1)ৃҹߴ䰸 x Ё᳔ে䖍ؐЎ 1 ⱘ ៤ৢⱘؐˈ ೼᠔᳝ⱘ䖭㉏㸼䖒ᓣЁˈ䌟ؐ㸼䖒ᓣⱘ㉏ൟᰃᅗⱘᎺ᪡԰᭄ⱘ㉏ൟˈ݊ؐᰃ䌟ؐ᪡԰ᅠ ݊ᅗ䌟ؐ䖤ㅫヺ˄བ+=ǃ•=ㄝ˅гৃҹ⫼೼㸼䖒ᓣЁˈሑㅵ䖭⾡⫼⊩↨䕗ᇥ㾕DŽ while ((c = getchar()) !=EOF) ՟ᄤ˖ ҢϞ䗄՟ᄤЁ៥Ӏৃҹⳟߎˈ䌟ؐ䇁হ݋᳝ؐˈϨৃҹ⫼೼㸼䖒ᓣЁDŽϟ䴶ᰃ᳔ᐌ㾕ⱘϔϾ ѻ⫳催ᬜҷⷕDŽ ᰃ৺ᅠܼϔḋˈг᮴乏Ўϸ㗙ЎҔМϡϔḋ㗠⭥ᚥϡ㾷ˈᑊϨˈ䌟ؐ䖤ㅫヺ䖬᳝ࡽѢ㓪䆥఼ 䌟ؐ䖤ㅫヺՓ⿟ᑣҷⷕ᳈ᯧѢ⧚㾷ˈҷⷕⱘ䯙䇏㗙ϡᖙ✲䌍㢺ᖗഄএẔᶹϸϾ䭓㸼䖒ᓣ yyval[yypv[p3+p4] + yypv[p1+p2]] += 2 i Ёāˈ಴ℸˈ㸼䖒ᓣ i += 2 ↨ i = i + 2 ᳈㞾✊ˈ঺໪ˈᇍѢ໡ᴖⱘ㸼䖒ᓣˈ՟བ˖ ᐌӮ䇈Āᡞ 2 ࡴࠄ i Ϟā៪Āᡞ i ๲ࡴ 2āˈ㗠ϡӮ䇈Āপ i ⱘؐˈࡴϞ 2ˈݡᡞ㒧ᵰᬒಲࠄ 䰸њㅔ⋕໪ˈ䌟ؐ䖤ㅫヺ䖬᳝ϔϾӬ⚍˖㸼⼎ᮍᓣϢҎӀⱘᗱ㓈дᛃ↨䕗᥹䖥DŽ៥Ӏ䗮 ߎⱘԡ䛑⫼ 0˄㗠ϡᰃヺোԡ˅฿㸹DŽ 䖭䞠ᇚ x ໄᯢЎ᮴ヺো㉏ൟᰃЎњֱ䆕ᇚ x ে⿏ᯊˈ᮴䆎䆹⿟ᑣ೼ҔМᴎ఼Ϟ䖤㸠ˈᎺ䖍ぎ } return b; b++; if (x & 01) for (b = 0; x != 0; x >>= 1) int b; { int bitcount(unsigned x) /* bitcount: count 1 bits in x */ ៥Ӏ䖭䞠В՟䇈ᯢDŽϟ䴶ⱘߑ᭄ bitcount 㒳䅵݊ᭈൟখ᭄ⱘؐЎ 1 ⱘѠ䖯ࠊԡⱘϾ᭄DŽ x = x * y + 1 㗠ϡᰃ x = x * (y + 1) ⱘ৿Нᰃ˖ x *= y + 1 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䖤ㅫヺ 㒧ড়ᗻ 㑻Ϣ㒧ড়ᗻܜ㸼 2•1 䖤ㅫヺⱘӬ 䅼䆎䖤ㅫヺ*˄䗮䖛ᣛ䩜䯈᥹䆓䯂˅Ϣ&˄ᇍ䈵ഄഔ˅ˈ㄀ 3 ゴᇚ䅼䆎䗫ো䖤ㅫヺDŽ ⫼Ѣ䆓䯂㒧ᵘ៤ਬˈ㄀ 6 ゴᇚ䅼䆎䖭ϸϾ䖤ㅫヺҹঞ sizeof(ᇍ䈵䭓ᑺ)䖤ㅫヺDŽ㄀ 5 ゴᇚ 䖤ㅫヺ+ǃ•催DŽ䖤ㅫヺ( ) 㸼⼎ߑ᭄䇗⫼DŽ䖤ㅫヺ•>੠.ܗ㑻䛑↨Ѡܜ㑻ˈᅗӀⱘӬܜৠⱘӬ 㑻䗤㸠䰡ԢDŽ՟བˈ*ǃ/Ϣ%ϝ㗙݋᳝Ⳍܜ㑻ˈ৘㸠䯈ҢϞᕔϟӬܜⱘ৘䖤ㅫヺ݋᳝ⳌৠⱘӬ 㑻Ϣ㒧ড়ᗻˈ݊Ёⱘϔѯ㾘߭៥Ӏ䖬≵᳝䆆䗄DŽৠϔ㸠Ёܜ㸼 2•1 ᘏ㒧њ᠔᳝䖤ㅫヺⱘӬ 㑻Ϣ∖ؐ⃵ᑣܜ2.12. 䖤ㅫヺӬ Ёⱘ if•else 㒧ᵘDŽ 㒗д 2•10 䞡ᮄ㓪ݭᇚ໻ݭᄫ↡䕀ᤶЎᇣݭᄫ↡ⱘߑ᭄ lowerˈᑊ⫼ᴵӊ㸼䖒ᓣ᳓ҷ݊ printf("You have %d item%s.\n", n, n==1 ? "" : "s"); ϔѯDŽϟ䴶ᰃ঺ϔϾ↨䕗དⱘ՟ᄤ˖ ޥϾぎḐDŽ㓪ݭ䖭ḋⱘҷⷕৃ㛑䳔㽕ϔѯᡔᎻˈԚ↨⫼ㄝӋⱘ if•else 㒧ᵘ㓪ݭⱘҷⷕ㽕㋻ ㋴ৢ䛑㽕ᠧॄϔܗ㋴Пৢ䛑㽕ᠧॄϔϾᤶ㸠ヺˈ᠔᳝݊ᅗܗ㋴Пৢҹঞ೼㄀ n Ͼܗ೼↣ 10 Ͼ printf("%6d%c", a[i], (i%10==9 !! i==n•1) ? '\n' : ' '); for (i = 0; i < n; i++) ϔ㸠˅˖ ㋴ˈ↣߫П䯈⫼ϔϾぎḐ䱨ᓔˈ↣㸠⫼ϔϾᤶ㸠ヺ㒧ᴳ˄ࣙᣀ᳔ৢܗ㋴ˈ↣㸠ᠧॄ 10 ϾܗϾ 䞛⫼ᴵӊ㸼䖒ᓣৃҹ㓪ݭߎᕜㅔ⋕ⱘҷⷕDŽ՟བˈϟ䴶ⱘ䖭Ͼᕾ⦃䇁হᠧॄϔϾ᭄㒘ⱘ n ߚ᳈ᯧѢ䯙䇏DŽ 㑻䴲ᐌԢˈҙ催Ѣ䌟ؐ䖤ㅫヺDŽԚ៥Ӏ䖬ᰃᓎ䆂Փ⫼೚ᣀোˈ಴Ў䖭ৃҹՓ㸼䖒ᓣⱘᴵӊ䚼 ܜᴵӊ㸼䖒ᓣЁ㄀ϔϾ㸼䖒ᓣϸ䖍ⱘ೚ᣀোᑊϡᰃᖙ乏ⱘˈ䖭ᰃ಴Ўᴵӊ䖤ㅫヺ?:ⱘӬ ᰃ float ㉏ൟˈϢ n ᰃ৺Ўℷؐ᮴݇DŽ (n > 0) ? f : n ՟བˈབᵰ f Ў float ㉏ൟˈn Ў int ㉏ൟˈ䙷М㸼䖒ᓣ ᅮDŽއഄᮍ˗བᵰ expr2 Ϣ expr3 ⱘ㉏ൟϡৠˈ㒧ᵰⱘ㉏ൟᇚ⬅ᴀゴࠡ䴶䅼䆎ⱘ䕀ᤶ㾘߭ ᑨ䆹⊼ᛣˈᴵӊ㸼䖒ᓣᅲ䰙Ϟህᰃϔ⾡㸼䖒ᓣˈᅗৃҹ⫼೼݊ᅗ㸼䖒ᓣৃҹՓ⫼ⱘӏԩ z = (a > b) ? a : b; /* z = max(a, b) */ 㛑᳝ϔϾ㸼䖒ᓣ㹿䅵ㅫDŽ಴ℸˈҹϞ䇁হৃҹᬍݭЎ˖ 㸼䖒ᓣⱘؐˈ৺߭䅵ㅫ expr3 ⱘؐˈᑊҹ䆹ؐ԰Ўᴵӊ㸼䖒ᓣⱘؐDŽexpr2 Ϣ expr3 Ёা 䅵ㅫ expr1ˈབᵰ݊ؐϡㄝѢ 0˄Ўⳳ˅ˈ߭䅵ㅫ expr2 ⱘؐˈᑊҹ䆹ؐ԰ЎᴵӊܜЁˈ佪 expr1 ? expr2 : expr3 կњ঺໪ϔ⾡ᮍ⊩㓪ݭ䖭↉⿟ᑣঞ㉏Ԑⱘҷⷕ↉ˈ೼㸼䖒ᓣ 䖤ㅫヺ“? :ā˅ᦤܗ⫼Ѣ∖ a ੠ b Ёⱘ᳔໻ؐˈᑊᇚ㒧ᵰֱᄬࠄ z ЁDŽᴵӊ㸼䖒ᓣ˄Փ⫼ϝ z = b; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᯢ⹂㾘ᅮњ᠔᳝ᇍখ᭄ⱘࡃ԰⫼䛑ᖙ乏೼ߑ᭄䇗⫼Пࠡ⫳ᬜˈԚ䖭ᇍࠡ䴶ҟ㒡ⱘ printfޚᷛ ᅮˈ಴Ў᳔Շⱘ∖ؐ乎ᑣৠᴎ఼㒧ᵘ᳝ᕜ໻݇㋏DŽ˄ANSI Cއ԰⫼˄ᇍব䞣䌟ؐ˅ˈᇚ⬅㓪䆥఼ ᇍ໻໮᭄䖭㉏䯂乬᳝ᛣ᳾԰݋ԧ㾘ᅮDŽ㸼䖒ᓣԩᯊӮѻ⫳䖭⾡ࡃޚѻ⫳ϡৠⱘ㒧ᵰDŽC 䇁㿔ᷛ 㓪䆥఼ⱘ㾷䞞ৃ㛑ϡৠˈᑊ಴ℸމ䯂乬ᰃ˖᭄㒘ϟᷛ i ᰃᓩ⫼ᮻؐ䖬ᰃᓩ⫼ᮄؐ˛ᇍ䖭⾡ᚙ a[i] = i++; ˖މব䞣㹿ׂᬍⱘ乎ᑣП䯈ᄬ೼ⴔᖂ཭ⱘձ䌪݇㋏ˈϟ߫䇁হህᰃϔϾ݌ൟⱘҸҎϡᛝᖿⱘᚙ ∖ؐⱘৠᯊˈׂᬍњᶤѯব䞣ⱘؐDŽ೼᳝ࡃ԰⫼ᕅડⱘ㸼䖒ᓣЁˈ݊ᠻ㸠㒧ᵰৠ㸼䖒ᓣЁⱘ 䖤ㅫヺ䛑᳝ৃ㛑ѻ⫳Āࡃ԰⫼”——೼ᇍ㸼䖒ᓣޣߑ᭄䇗⫼ǃጠ༫䌟ؐ䇁হǃ㞾๲Ϣ㞾 printf("%d %d\n", n, power(2, n)); ++n; ⱘࡲ⊩ᰃᡞ䆹䇁হᬍݭ៤ϟ߫ᔶᓣ˖އৢᠻ㸠DŽ㾷 Ѣ n ⱘ㞾๲䖤ㅫ೼ power 䇗⫼Пࠡ䖬ᰃПއ೼ϡৠⱘ㓪䆥఼Ёৃ㛑Ӯѻ⫳ϡৠⱘ㒧ᵰˈ䖭প printf("%d %d\n", ++n, power(2, n)); /* 䫭 */ ㉏ԐഄˈC 䇁㿔г≵᳝ᣛᅮߑ᭄৘খ᭄ⱘ∖ؐ乎ᑣDŽ಴ℸˈϟ߫䇁হ ⡍ᅮⱘ䅵ㅫ乎ᑣˈৃҹᡞЁ䯈㒧ᵰֱᄬ೼Јᯊব䞣ЁDŽ њ঺ϔϾߑ᭄᠔Փ⫼ⱘব䞣ˈ䙷М x ⱘ㒧ᵰৃ㛑Ӯձ䌪Ѣ䖭ϸϾߑ᭄ⱘ䅵ㅫ乎ᑣDŽЎњֱ䆕 ⱘ䇁হЁˈf()ৃҹ೼ g()Пࠡ䅵ㅫˈгৃҹ೼ g()Пৢ䅵ㅫDŽ಴ℸˈབᵰߑ᭄ f ៪ g ᬍব x = f() + g(); ੠,䖤ㅫヺ䰸໪˅DŽ՟བˈ೼ᔶབ ৠ໻໮᭄䇁㿔ϔḋˈC 䇁㿔≵᳝ᣛᅮৠϔ䖤ㅫヺЁ໮Ͼ᪡԰᭄ⱘ䅵ㅫ乎ᑣ˄&&ǃ||ǃ?: ᖙ乏⫼೚ᣀোᣀ䍋ᴹᠡ㛑ᕫࠄℷ⹂㒧ᵰDŽ if ((x & MASK) == 0) ... 㑻↨䖤ㅫヺ==Ϣ!=ⱘԢDŽ䖭ᛣੇⴔˈԡ⌟䆩㸼䖒ᓣˈབܜ⊼ᛣˈԡ䖤ㅫヺ&ǃ^Ϣ|ⱘӬ dغމԅညຕ ူ Αອ࿫ԅ֝၍ၮസ׸cc ူ ᅟġྡྷ၍ၮസ׸cc , Ңে㟇Ꮊ = += •= *= /= %= &= ^= |= <<= >>= Ңে㟇Ꮊ ?: ҢᎺ㟇ে || ҢᎺ㟇ে && ҢᎺ㟇ে | ҢᎺ㟇ে ^ ҢᎺ㟇ে & ҢᎺ㟇ে == != ҢᎺ㟇ে < <= > >= ҢᎺ㟇ে << >> ҢᎺ㟇ে + • ҢᎺ㟇ে * / % ҢᎺ㟇ে ! ~ ++ •• + • * (type) sizeof Ңে㟇Ꮊ ҢᎺ㟇ে . <• [] () w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ⱘˈህ᳔དϡ㽕ᇱ䆩䖤⫼ᶤ⾡⡍⅞ⱘᅲ⦄ᮍᓣDŽއབԩ㾷 Ԛᰃˈབᵰϡⶹ䘧䖭ѯ䯂乬೼৘⾡ᴎ఼Ϟᰃˈܡ亢ḐDŽᕜ㞾✊ˈ᳝ᖙ㽕њ㾷ાѯ䯂乬䳔㽕䙓 ೼ӏԩϔ⾡㓪⿟䇁㿔Ёˈབᵰҷⷕⱘᠻ㸠㒧ᵰϢ∖ؐ乎ᑣⳌ݇ˈ߭䛑ᰃϡདⱘ⿟ᑣ䆒䅵 ߑ᭄䇗⫼≵᳝ҔМᐂࡽDŽ˅ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ⱘᮍ⊩ᰃᇚ↣Ͼ else Ϣ᳔䖥ⱘࠡϔϾ≵᳝ else 䜡ᇍⱘ if 䖯㸠ऍ䜡DŽއߚᇚᇐ㟈℻НDŽ㾷 ಴Ў if•else 䇁হⱘ else 䚼ߚᰃৃ䗝ⱘˈ᠔ҹ೼ጠ༫ⱘ if 䇁হЁⳕ⬹ᅗⱘ else 䚼 ϟৃ㛑Ӯ৿Нϡ⏙DŽމϟ䖭⾡ᔶᓣᰃ㞾✊⏙᱄ⱘˈԚг᳝ѯᚙމᶤѯᚙ if (㸼䖒ᓣ !0) ᴹҷ᳓ if (㸼䖒ᓣ) ⱘ՟ᄤᰃ⫼བϟݭ⊩ ⬅Ѣ if 䇁হাᰃㅔऩ⌟䆩㸼䖒ᓣⱘ᭄ؐˈ಴ℸৃҹᇍᶤѯҷⷕⱘ㓪ݭ䖯㸠ㅔ࣪DŽ᳔ᯢᰒ ߭ᠻ㸠䇁হ 2DŽ 㸼䖒ᓣⱘؐЎ 0˅ˈᑊϨ䆹䇁হࣙ৿ else 䚼ߚˈे˄؛Ў䴲 0˅ˈ߭ᠻ㸠䇁হ 1˗བᵰ݊ؐЎ 䅵ㅫ㸼䖒ᓣⱘؐˈབᵰ݊ؐЎⳳ˄े㸼䖒ᓣⱘؐܜˈ݊Ё else 䚼ߚᰃৃ䗝ⱘDŽ䆹䇁হᠻ㸠ᯊ 䇁হ 2 else 䇁হ 1 if {㸼䖒ᓣ} if•else 䇁হ⫼Ѣᴵӊ߸ᅮˈ݊䇁⊩བϟ᠔⼎˖ 3.2. if•else䇁হ 䛑ৃҹໄᯢব䞣ˈヿ 4 ゴᇚᇍℸ䖯㸠䅼䆎DŽ˅ে㢅ᣀো⫼Ѣ㒧ᴳ⿟ᑣഫˈ݊ৢϡ䳔㽕ߚোDŽ ifǃelseˈwhile Ϣ for Пৢ㹿㢅ᣀোᣀԣⱘ໮ᴵ䇁হгᰃ㉏Ԑⱘ՟ᄤDŽ˄೼ӏԩ⿟ᑣഫЁ ⿟ᑣഫ˅ˈ໡ড়䇁হ೼䇁⊩ϞㄝӋѢऩᴵ䇁হDŽߑ᭄ԧЁ㹿㢅ᣀোᣀ䍋ᴹⱘ䇁হ֓ᰃᯢᰒϔ՟DŽ ⫼ϔᇍ㢅ᣀো“{āϢ“}āᡞϔ㒘ໄᯢ੠䇁হᣀ೼ϔ䍋ህᵘ៤њϔϾ໡ড়䇁হ˄гি԰ ೼ C 䇁㿔Ёˈߚোᰃ䇁হ㒧ᴳヺˈ㗠 Pascal ㄝ䇁㿔ैᡞߚো⫼԰䇁হП䯈ⱘߚ䱨ヺDŽ printf(...); i++; x = 0; 䇁হDŽ՟བ˖ ೼ x = 0ǃi++៪ printf(...)䖭ḋⱘ㸼䖒ᓣПৢࡴϞϔϾߚো˄;˅ˈᅗӀህব៤њ 3.1. 䇁হϢ⿟ᑣഫ Փ⫼њϔѯ᳔ᐌ⫼ⱘ᥻ࠊ⌕㒧ᵘDŽᴀゴᇚ᳈䆺㒚ഄ䆆䗄᥻ࠊ⌕䇁হDŽ ⿟ᑣ䇁㿔Ёⱘ᥻ࠊ⌕䇁হ⫼Ѣ᥻ࠊ৘䅵ㅫ᪡԰ᠻ㸠ⱘ⃵ᑣDŽ೼ࠡ䴶ⱘ՟ᄤЁˈ៥Ӏ᳒㒣 ㄀3ゴ᥻ࠊ⌕ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䇁হ else 䇁হ else if (㸼䖒ᓣ) 䇁হ else if (㸼䖒ᓣ) 䇁হ else if (㸼䖒ᓣ) 䇁হ if (㸼䖒ᓣ) ೼ C 䇁㿔Ё៥ӀӮ㒣ᐌ⫼ࠄϟ߫㒧ᵘ˖ 3.3. else•if䇁ᯀ 䖭㉏ⱘ㸼䖒ᓣ䇁হᘏᰃҹߚো㒧ᴳⱘDŽ z=a;”“ڣЁˈz = a ৢ᳝ϔϾߚোDŽ䖭ᰃ಴ЎˈҢ䇁⊩Ϟ䆆ˈ䎳೼ if ৢ䴶ⱘᑨ䆹ᰃϔᴵ䇁হˈ㗠 z = b; else z = a; if (a > b) 乎֓ᦤ䝦䇏㗙⊼ᛣˈ೼䇁হ ϟՓ⫼㢅ᣀোDŽމሖⱘ if 䜡ᇍDŽ䖭⾡䫭䇃ᕜ䲒থ⦄ˈ಴ℸ៥Ӏᓎ䆂೼᳝ if 䇁হጠ༫ⱘᚙ ⿟ᑣⱘ㓽䖯㒧ᵘᯢ⹂ഄ㸼ᯢњ䆒䅵ᛣ೒ˈԚ㓪䆥఼᮴⊩㦋ᕫ䖭ϔֵᙃˈᅗӮᇚ else 䚼ߚϢݙ printf("error •• n is negative\n"); else /* WRONG */ } return i; printf("..."); if (s[i] > 0) { for (i = 0; i < n; i++) if (n > 0) ϟᇸЎ᳝ᆇ˖މ℻Нᗻ೼ϟ䴶䖭⾡ᚙ z = b; else } z = a; if (a > b) if (n > 0) { ᛣ೒ˈ߭ᖙ乏Փ⫼㢅ᣀোᔎࠊᅲ⦄ℷ⹂ⱘऍ䜡݇㋏˖ else 䚼ߚϢݙሖⱘ if ऍ䜡ˈ៥Ӏ䗮䖛⿟ᑣⱘ㓽䖯㒧ᵘгৃҹⳟߎᴹDŽབᵰ䖭ϡヺড়៥Ӏⱘ z = b; else z = a; if (a > b) if (n > 0) ՟བˈ೼ϟ߫䇁হЁ˖ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䜡ˈᑊᠻ㸠Ⳍᑨⱘߚᬃࡼ԰DŽ switch 䇁হᰃϔ⾡໮䏃߸ᅮ䇁হˈᅗ⌟䆩㸼䖒ᓣᰃ৺Ϣϔѯᐌ䞣ᭈ᭄ؐЁⱘᶤϔϾؐऍ 3.4. switch䇁হ ϔ⃵⌟䆩DŽ↨䕗ϸ⾡⠜ᴀߑ᭄ⱘ䖤㸠ᯊ䯈DŽ া㽕ϔ⃵ህ䎇໳˄ҷӋᰃᇚ᳈໮ⱘ⌟䆩೼ᕾ⦃໪ᠻ㸠˅DŽ䞡ݭ䆹ߑ᭄ˈՓᕫ೼ᕾ⦃ݙ䚼াᠻ㸠 㒗д 3•1 ೼Ϟ䴶᳝݇ᡬञᶹᡒⱘ՟ᄤЁˈwhile ᕾ⦃䇁হݙ݅ᠻ㸠њϸ⃵⌟䆩ˈ݊ᅲ else•if 㒧ᵘᠻ㸠䖭⾡߸ᅮᕜ㞾✊DŽ ㋴ v[mid]DŽՓ⫼ܗ䆹ߑ᭄ⱘ෎ᴀ߸ᅮᰃ˖೼↣ϔℹ߸ᮁ x ᇣѢǃ໻Ѣ䖬ᰃㄝѢЁ䯈 } return •1; /* no match */ } return mid; else /* found match */ low = mid + 1; else if (x > v[mid]) high = mid + 1; if (x < v[mid]) mid = (low+high)/2; while (low <= high) { high = n • 1; low = 0; int low, high, mid; { int binsearch(int x, int v[], int n) /* binsearch: find x in v[0] <= v[1] <= ... <= v[n•1] */ ᶹᡒ㣗ೈЎぎDŽ ㋴䖯㸠↨䕗DŽ䖭Ͼ䖛⿟ϔⳈ䖯㸠ϟএˈⳈࠄᡒࠄᣛᅮⱘؐ៪ܗℹ䛑ᰃᇚ x Ϣ᠔䗝䚼ߚⱘЁ䯈 ϟˈϟϔމؐˈ߭೼䆹᭄㒘ⱘࠡञ䚼ߚᶹᡒ˗৺߭ˈ೼䆹᭄㒘ⱘৢञ䚼ߚᶹᡒDŽ೼䖭ϸ⾡ᚙ ㋴ⱘܗ㋴䖯㸠↨䕗DŽབᵰ x ᇣѢЁ䯈ܗᇚ䕧ܹؐ x Ϣ᭄㒘 v ⱘЁ䯈ܜ೼ᡬञᶹᡒᯊˈ佪 x ೼ v Ёⱘԡ㕂˄ҟѢ 0̚n•1 П䯈ⱘϔϾᭈ᭄˅˗৺߭ˈ䆹ߑ᭄䖨ಲ•1DŽ ㋴ᖙ乏ҹछᑣᥦ߫DŽབᵰ v Ёࣙ৿ xˈ߭䆹ߑ᭄䖨ಲܗЁᰃ৺ᄬ೼ᶤϾ⡍ᅮⱘؐ xDŽ᭄㒘 v ⱘ 䖭䞠䗮䖛ϔϾᡬञᶹᡒߑ᭄䇈ᯢϝ䏃߸ᅮ⿟ᑣⱘ⫼⊩DŽ䆹ߑ᭄⫼Ѣ߸ᅮᏆᥦᑣⱘ᭄㒘 v 䚼ߚⳕ⬹ᥝ˗䆹䚼ߚгৃҹ⫼ᴹẔᶹ䫭䇃ˈҹᤩ㦋Āϡৃ㛑āⱘᴵӊDŽ 䇁হ else ᡞ䆹㒧ᵘ᳿ሒⱘ ϟˈৃҹމᠻ㸠ᰒᓣⱘ᪡԰ˈ䖭⾡ᚙމᴵӊ䛑ϡ⒵䎇ᯊⱘᚙᔶDŽ᳝ᯊ׭ᑊϡ䳔㽕䩜ᇍ咬䅸ᚙ гህᰃᔧϞ䴶৘ˈމ៪咬䅸ᚙމ᳔ৢϔϾ else 䚼ߚ⫼Ѣ໘⧚ĀϞ䗄ᴵӊഛϡ៤ゟāⱘᚙ ߫ⱘᠻ㸠DŽৠḋˈ݊Ё৘䇁হ᮶ৃҹᰃऩᴵ䇁হˈгৃҹᰃ⫼㢅ᣀোᣀԣⱘ໡ড়䇁হDŽ 䖒ᓣᇚ㹿ձ⃵∖ؐˈϔᮺᶤϾ㸼䖒ᓣ㒧ᵰЎⳳˈ߭ᠻ㸠ϢПⳌ݇ⱘ䇁হˈᑊ㒜ℶᭈϾ䇁হᑣ ಴ℸ៥Ӏ೼䖭䞠ऩ⣀䇈ᯢϔϟDŽ䖭⾡ if 䇁হᑣ߫ᰃ㓪ݭ໮䏃߸ᅮ᳔ᐌ⫼ⱘᮍ⊩DŽ݊Ёⱘ৘㸼 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ϟЎњ䰆ℶⳈ᥹䖯ܹϟϔϾߚᬃᠻ㸠ˈމ៤ϔϾӏࡵˈབϞ՟Ёᇍ᭄ᄫⱘ໘⧚DŽԚᰃˈℷᐌᚙ ᳝Ӭ⚍г᳝㔎⚍DŽདⱘϔ䴶ᰃᅗৃҹᡞ㢹ᑆϾߚᬃ㒘ড়೼ϔ䍋ᅠ⫣خձ⃵ᠻ㸠৘ߚᬃⱘ 䖯ϔℹҟ㒡DŽخӀ⿡ৢ䖬ᇚ হDŽbreak 䇁হ䖬ৃᔎࠊ᥻ࠊҢ whileǃfor Ϣ do ᕾ⦃䇁হЁゟे䗔ߎˈᇍѢ䖭ϔ⚍ˈ៥ 䰸䴲೼⿟ᑣЁᰒᓣഄ䏇䕀DŽ䏇ߎ switch 䇁হ᳔ᐌ⫼ⱘᮍ⊩ᰃՓ⫼ break 䇁হϢ return 䇁 ⱘ԰⫼াᰃϔϾᷛোˈ಴ℸˈᶤϾߚᬃЁⱘҷⷕᠻ㸠ᅠৢˈ⿟ᑣᇚ䖯ܹϟϔߚᬃ㒻㓁ᠻ㸠ˈ break 䇁হᇚᇐ㟈⿟ᑣⱘᠻ㸠ゟेҢ switch 䇁হЁ䗔ߎDŽ೼ switch 䇁হЁˈcase } return 0; nwhite, nother); printf(", white space = %d, other = %d\n", printf(" %d", ndigit[i]); for (i = 0; i < 10; i++) printf("digits ="); } } break; nother++; default: break; nwhite++; case '\t': case '\n': case ' ': break; ndigit[c•'0']++; case '5': case '6': case '7': case '8': case '9': case '0': case '1': case '2': case '3': case '4': switch (c) { while ((c = getchar()) != EOF) { ndigit[i] = 0; for (i = 0; i < 10; i++) nwhite = nother = 0; int c, i, nwhite, nother, ndigit[10]; { main() /* count digits, white space, others */ #include ヺঞ݊ᅗ᠔᳝ᄫヺߎ⦄ⱘ⃵᭄DŽϟ䴶៥Ӏ⫼ switch 䇁হᬍݭ䆹⿟ᑣབϟ˖ ៥Ӏ೼㄀ 1 ゴЁ᳒⫼ if…else if…else 㒧ᵘ㓪ݭ䖛ϔϾ⿟ᑣҹ㒳䅵৘Ͼ᭄ᄫǃぎⱑ ᑣᰃӏᛣⱘDŽ ᬃϢ㸼䖒ᓣⱘؐऍ䜡ˈ߭䆹 switch 䇁হϡᠻ㸠ӏԩࡼ԰DŽ৘ߚᬃঞ default ߚᬃⱘᥦ߫⃵ 㸠ᷛ䆄Ў default ⱘߚᬃDŽdefault ߚᬃᰃৃ䗝ⱘDŽབᵰ≵᳝ default ߚᬃг≵᳝݊ᅗߚ ߭Ң䆹ߚᬃᓔྟᠻ㸠DŽ৘ߚᬃ㸼䖒ᓣᖙ乏ѦϡⳌৠDŽབᵰ≵᳝ાϔߚᬃ㛑ऍ䜡㸼䖒ᓣˈ߭ᠻ ↣ϔϾߚᬃ䛑⬅ϔϾ៪໮Ͼᭈ᭄ؐᐌ䞣៪ᐌ䞣㸼䖒ᓣᷛ䆄DŽབᵰᶤϾߚᬃϢ㸼䖒ᓣⱘؐऍ䜡ˈ default: 䇁হᑣ߫ case ᐌ䞣㸼䖒ᓣ: 䇁হᑣ߫ case ᐌ䞣㸼䖒ᓣ: 䇁হᑣ߫ switch (㸼䖒ᓣ) { w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! Ѣ⿟ᑣ䆒䅵Ҏਬⱘއ೼䆒䅵⿟ᑣᯊࠄᑩ䗝⫼ while ᕾ⦃䇁হ䖬ᰃ for ᕾ⦃䇁হˈЏ㽕প 㛑㒜ℶᠻ㸠DŽ ࡽ݊ᅗ᠟↉˄བ break 䇁হ៪ return 䇁হ˅ᠡ׳ᰃϔϾĀ᮴䰤āᕾ⦃䇁হˈ䖭⾡䇁হ䳔㽕 } ... for (;;) { for ᕾ⦃䇁হ˖ њ while ᕾ⦃䇁হDŽབᵰⳕ⬹⌟䆩ᴵӊˈे㸼䖒ᓣ 2ˈ߭䅸Ў݊ؐ∌䖰ᰃⳳؐˈ಴ℸˈϟ߫ ߚ䛑ৃҹⳕ⬹ˈԚߚোᖙ乏ֱ⬭DŽབᵰ೼ for 䇁হЁⳕ⬹㸼䖒ᓣ 1 Ϣ㸼䖒ᓣ 3ˈᅗህ䗔࣪៤ Ϣ㸼䖒ᓣ 3 ᰃ䌟ؐ㸼䖒ᓣ៪ߑ᭄䇗⫼ˈ㸼䖒ᓣ 2 ᰃ݇㋏㸼䖒ᓣDŽ䖭 3 Ͼ㒘៤䚼ߚЁⱘӏԩ䚼 ᰃˈ㸼䖒ᓣ 1މҢ䇁⊩㾦ᑺⳟˈfor ᕾ⦃䇁হⱘ 3 Ͼ㒘៤䚼ߚ䛑ᰃ㸼䖒ᓣDŽ᳔ᐌ㾕ⱘᚙ Ӏᇚ೼ 3.7 㡖Ёҟ㒡 continue 䇁হDŽ Ԛᔧ while ៪ for ᕾ⦃䇁হЁࣙ৿ continue 䇁হᯊˈϞ䗄Ѡ㗙П䯈ህϡϔᅮㄝӋњDŽ៥ } 㸼䖒ᓣ 3; 䇁হ while (㸼䖒ᓣ 2) { 㸼䖒ᓣ 1; ᅗㄝӋѢϟ߫ while 䇁হ˖ 䇁হ for (㸼䖒ᓣ 1; 㸼䖒ᓣ 2; 㸼䖒ᓣ 3) for ᕾ⦃䇁হ; ϔⳈ䖯㸠ϟএˈⳈࠄ䆹㸼䖒ᓣⱘؐЎ 0 Ўℶˈ䱣ৢ㒻㓁ᠻ㸠䇁হৢ䴶ⱘ䚼ߚDŽ 㸼䖒ᓣⱘؐDŽབᵰ݊ؐ䴲 0ˈ߭ᠻ㸠䇁হˈᑊݡ⃵∖䆹㸼䖒ᓣⱘؐDŽ䖭ϔᕾ⦃䖛⿟∖ܜЁˈ佪 䇁হ while (㸼䖒ᓣ) ៥Ӏ೼ࠡ䴶Ꮖ㒣Փ⫼䖛 while Ϣ for ᕾ⦃䇁হDŽ೼ while ᕾ⦃䇁হ 3.5. whileᕾ⦃Ϣ for ᕾ⦃ ᅲ䰙ᄫヺDŽ ∖Փ⫼ swich 䇁হDŽݡ㓪ݭϔϾ݋᳝Ⳍডࡳ㛑ⱘߑ᭄ˈ೼໡ࠊ䖛⿟Ёᇚ䕀Нᄫヺᑣ߫䕀ᤶЎ 䖛⿟Ёᇚᤶ㸠ヺǃࠊ㸼ヺㄝϡৃ㾕ᄫヺߚ߿䕀ᤶЎ\nǃ\t ㄝⳌᑨⱘৃ㾕ⱘ䕀Нᄫヺᑣ߫DŽ㽕 㒗д 3•2 㓪ݭϔϾߑ᭄ escape(s, t)ˈᇚᄫヺІ t ໡ࠊࠄᄫヺІ s Ёˈᑊ೼໡ࠊ ࡴ݊ᅗߚᬃᯊˈ䖭⾡䰆㣗᥾ᮑӮ䰡Ԣ⢃䫭䇃ⱘৃ㛑ᗻDŽ ೼䘏䕥Ϟ≵᳝ᖙ㽕ˈԚᔧ៥Ӏ䳔㽕৥䆹 switch 䇁হৢ⏏خ䴶гࡴϞϔϾ break 䇁হDŽ䖭ḋ ԰Ўϔ⾡㡃དⱘ⿟ᑣ䆒䅵亢Ḑˈ೼ switch 䇁হ᳔ৢϔϾߚᬃ˄े default ߚᬃ˅ⱘৢ 䞞DŽ ϟᑨ䆹ࡴϞ䗖ᔧⱘ⿟ᑣ⊼މϔϾߚᬃⳈ᥹䖯ܹϟϔϾߚᬃᠻ㸠䖭⾡⫼⊩ˈ೼ϡᕫϡՓ⫼ⱘᚙ ᇥҢޣ໪ˈᑨሑ䞣މ೼⿟ᑣׂᬍᯊᕜᆍᯧߎ䫭DŽ䰸њϔϾ䅵ㅫ䳔㽕໮Ͼᷛোⱘᚙخ䖭ḋˈܼ عᑊϡ⫣خϾߚᬃৢᖙ乏ҹϔϾ break 䇁হ㒧ᴳDŽҢϔϾߚᬃⳈ᥹䖯ܹϟϔϾߚᬃᠻ㸠ⱘ↣ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ㋴DŽ䖭ḋৃҹᖿܗ䕗Ⳍ䚏ⱘ↨ܜㅔऩѸᤶᥦᑣㅫ⊩䙷ḋڣ㋴ˈ㗠ϡᰃܗ䕗䎱⾏䖰ⱘ↨ܜ˖ᰃ ᭄㒘䖯㸠ᥦᑣⱘ Shell ᥦᑣㅫ⊩DŽShell ᥦᑣㅫ⊩ᰃ D. L. ShellѢ 1959 ᑈথᯢⱘˈ݊෎ᴀᗱᛇ ᡞᕾ⦃᥻ࠊ䚼ߚ䲚Ё೼ϔ䍋ˈᇍѢ໮䞡ጠ༫ᕾ⦃ˈӬ࢓᳈ЎᯢᰒDŽϟ䴶ⱘߑ᭄ᰃᇍᭈൟ ⱘ䆺㒚ֵᙃˈ䇋খ㾕䰘ᔩ B.5 㡖DŽ ᑧЁᦤկњϔϾ᳈ᅠ୘ⱘߑ᭄ strtolˈᅗᇚᄫヺІ䕀ᤶЎ䭓ᭈൟ᭄DŽ᳝݇ߑ᭄ strtolޚᷛ } return sign * n; n = 10 * n + (s[i] • '0'); for (n = 0; isdigit(s[i]); i++) i++; if (s[i] == '+' || s[i] == '•') /* skip sign */ sign = (s[i] == '•') ? •1 : 1; ; for (i = 0; isspace(s[i]); i++) /* skip white space */ int i, n, sign; { int atoi(char s[]) /* atoi: convert s to integer; version 2 */ #include 㛑䕀ᤶЎ᭄ᄫⱘᄫヺᯊˈᭈϾ໘⧚䖛⿟㒜ℶDŽ ໛DŽᔧ䘛ࠄ㄀ϔϾϡޚདخ݊Ёⱘ↣ϔℹ䛑ᇍ䕧᭄ܹ᥂䖯㸠Ⳍᑨⱘ໘⧚ˈᑊЎϟϔℹⱘᠻ㸠 ēωᄓ໻ᅧܰד௜ჼ೴Ϧ ē႕ՠ௜׸ۜܥပ׸ۜԅڴఢ ڶē႕ණܥပࣂ͂׸ԅڴఢ ϟ䴶ᰃ⿟ᑣⱘ㒧ᵘˈҢЁৃҹⳟߎ䕧ܹⱘḐᓣ˖ োDŽ˄㄀ 4 ゴᇚҟ㒡ߑ᭄ atofˈᅗ⫼Ѣᇍ⍂⚍᭄ᠻ㸠ৠḋⱘ䕀ᤶDŽ˅˅•˄ޣ៪ ⱘߑ᭄↨㄀ 2 ゴЁⱘ atoi ߑ᭄᳈䗮⫼ˈᅗৃҹ໘⧚ৃ䗝ⱘࠡᇐぎⱑヺҹঞϔϾৃ䗝ⱘࡴ˄+˅ ԰ЎϔϾ䕗໻ⱘ՟ᄤˈ៥Ӏᴹ䞡ᮄ㓪ݭᇚᄫヺІ䕀ᤶЎᇍᑨ᭄ؐⱘߑ᭄ atoiDŽ䖭䞠㓪ݭ ᰃϔ⾡ϡདⱘ⿟ᑣ䆒䅵亢Ḑˈ䆹䚼ߚᬒ㕂ᕾ⦃᥻ࠊ䖤ㅫ᳈ড়䗖DŽ 䖯㸠ᕾ⦃᥻ࠊDŽሑㅵབℸˈ⡉ᔎഄᡞϔѯ᮴݇ⱘ䅵ㅫᬒࠄ for 䇁হⱘ߱ྟ࣪੠ব䞣䗦๲䚼ߚ ֱ⬭DŽ಴Ў for 䇁হⱘ৘㒘៤䚼ߚৃҹᰃӏԩ㸼䖒ᓣˈ᠔ҹ for 䇁হᑊϡ䰤Ѣ䗮䖛ㅫᴃ㑻᭄ ⱘᕾ⦃ব䞣੠Ϟ䰤೼ᕾ⦃ԧݙৃҹׂᬍˈᑊϨᔧᕾ⦃಴ᶤ⾡ॳ಴㒜ℶৢᕾ⦃ব䞣 i ⱘؐҡ✊ ⹂ˈ಴Ў೼ C 䇁㿔Ёˈfor ᕾ⦃䇁হޚPascal 䇁㿔Ёⱘ for ᕾ⦃DŽԚᰃˈ䖭⾡㉏↨ᑊϡᅠܼ ㋴ⱘϔ⾡дᛃᗻ⫼⊩ˈᅗ㉏ԐѢ Fortran 䇁㿔Ёⱘ DO ᕾ⦃៪ܗ䖭ᰃ C 䇁㿔໘⧚᭄㒘ࠡ n Ͼ ... for (i = 0; i < n; i++) ǃ᳈⏙᱄DŽ䗮䖛ϟ߫䇁হৃҹᕜᯢᰒഄⳟߎ䖭ϔ⚍˖ޥࠊ䇁হ䲚Ёᬒ೼ᕾ⦃ⱘᓔ༈ˈ㒧ᵘ᳈㋻ བᵰ䇁হЁ䳔㽕ᠻ㸠ㅔऩⱘ߱ྟ࣪੠ব䞣䗦๲ˈՓ⫼ for 䇁হ᳈ড়䗖ϔѯˈᅗᇚᕾ⦃᥻ ಴Ў݊Ё≵᳝߱ྟ࣪៪䞡ᮄ߱ྟ࣪ⱘ᪡԰ˈ᠔ҹՓ⫼ whi1e ᕾ⦃䇁হ᳈㞾✊ϔѯDŽ ; /* skip white space characters */ while ((c = getchar()) == ' ' || c == '\n' || c = '\t') དDŽ՟བˈ೼ϟ߫䇁হЁ˖أϾҎ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 㒗д 3•3 㓪ݭߑ᭄ expand(s1, s2)ˈᇚᄫヺІ s1 Ё㉏ԐѢ a•z ϔ㉏ⱘ䗳䆄ヺো c = s[i], s[i] = s[j], s[j] = c; for (i = 0, j = strlen(s)•1; i < j; i++, j••) ㋴ⱘѸᤶ䖛⿟֓ৃҹⳟ៤ᰃϔϾऩℹ᪡԰DŽܗˈ㋴ⱘѸᤶˈ䖭ḋܗ䗖⫼Ѣ reverse ߑ᭄Ё ᭄ݙⱘ for 䇁হˈᇍѢ䳔㽕೼ऩϾ㸼䖒ᓣЁ䖯㸠໮ℹ䅵ㅫⱘᅣᴹ䇈гᕜ䗖ড়DŽ䗫ো㸼䖒ᓣ䖬 ᑨ䆹ᜢ⫼䗫ো䖤ㅫヺDŽ䗫ো䖤ㅫヺ᳔䗖⫼Ѣ݇㋏㋻ᆚⱘ㒧ᵘЁˈ↨བϞ䴶ⱘ reverse ߑ 䖭ѯ䗫োᑊϡֱ䆕৘㸼䖒ᓣᣝҢᎺ㟇েⱘ乎ᑣ∖ؐDŽ ϟⱘ䗫োᑊϡᰃ䗫ো䖤ㅫヺˈ↨བߚ䱨ߑ᭄খ᭄ⱘ䗫োˈߚ䱨ໄᯢЁব䞣ⱘ䗫োㄝˈމᶤѯᚙ } } s[j] = c; s[i] = s[j]; c = s[i]; for (i = 0, j = strlen(s)•1; i < j; i++, j••) { int c, i, j; { void reverse(char s[]) /* reverse: reverse string s in place */ #include 㕂ᄫヺІ s Ё৘Ͼᄫヺⱘԡ㕂DŽצ བৠᯊ໘⧚ϸϾᕾ⦃᥻ࠊবᰩDŽ៥Ӏৃҹ䗮䖛ϟ䴶ⱘߑ᭄ reverse(s)ᴹВ՟DŽ䆹ߑ᭄⫼Ѣ ݊㒧ᵰⱘ㉏ൟ੠ؐDŽ䖭ḋˈ೼ for ᕾ⦃䇁হЁˈৃҹᇚ໮Ͼ㸼䖒ᓣᬒ೼৘Ͼ䇁হ៤ߚЁˈ↨ োߚ䱨ⱘϔᇍ㸼䖒ᓣᇚᣝ✻ҢᎺࠄেⱘ乎ᑣ䖯㸠∖ؐˈ㸼䖒ᓣে䖍ⱘ᪡԰᭄ⱘ㉏ൟ੠ؐेЎ 㑻᳔Ԣⱘ䖤ㅫヺˈ೼ for 䇁হЁ㒣ᐌӮ⫼ࠄᅗDŽ㹿䗫ܜ䗫ো䖤ㅫヺ“,āгᰃ C 䇁㿔Ӭ ህ䇈ᯢ for 䇁হ݋᳝ᕜᔎⱘ䗮⫼ᗻDŽ ⊼ᛣˈेՓ᳔໪ሖ for ᕾ⦃ⱘ᥻ࠊব䞣ϡᰃㅫᴃ㑻᭄ˈfor 䇁হⱘкݭᔶᓣҡ✊≵᳝বˈ䖭 ㋴᳔㒜䛑ӮԡѢℷ⹂ⱘᥦᑣԡ㕂ϞDŽܗࠄ 1ˈ಴ℸ᠔᳝ޣӀѦᤶ䖛ᴹDŽ⬅Ѣ gap ⱘ᳔ؐ㒜㽕䗦 ㋴䗚ᑣᯊᡞᅗܗ㋴ˈᔧ䖭ϸϾܗࡼԡ㕂DŽ᳔ݙሖⱘ for 䇁হ⫼Ѣ↨䕗৘ᇍⳌ䎱 gap Ͼԡ㕂ⱘ ㋴䯈⿏ܗ䎱⾏ˈҢ n/2 ᓔྟˈ䗤ℹ䖯㸠ᇍᡬˈⳈࠄ䎱⾏Ў 0DŽЁ䯈ሖⱘ for ᕾ⦃䇁হ⫼Ѣ೼ ㋴П䯈ⱘܗ䆹ߑ᭄Ёࣙ৿ϔϾϝ䞡ጠ༫ⱘ for ᕾ⦃䇁হDŽ᳔໪ሖⱘ for 䇁হ᥻ࠊϸϾ㹿↨䕗 } } v[j+gap] = temp; v[j] = v[j+gap]; temp = v[j]; for (j=i•gap; j>=0 && v[j]>v[j+gap]; j•=gap) { for (i = gap; i < n; i++) for (gap = n/2; gap > 0; gap /= 2) int gap, i, j, temp; { void shellsort(int v[], int n) /* shellsort: sort v[0]...v[n•1] into increasing order */ ㋴ⱘѦᤶDŽܗᇥЎ 1ˈ䖭ᯊᥦᑣব៤њⳌ䚏 ޣᇥˈⳈࠄޣ㋴П䯈ⱘ䎱⾏䗤ℹܗ䕏ৢ㓁ⱘᎹ԰DŽ㹿↨䕗ⱘޣҢ㗠ˈމᇥ໻䞣ⱘ᮴ᑣᚙޣ䗳 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 㒗д 3•5 㓪ݭߑ᭄ itob(n, s, b) ˈᇚᭈ᭄ n 䕀ᤶЎҹ b Ўᑩⱘ᭄ˈᑊᇚ䕀ᤶ㒧ᵰ ⱘؐDŽ DŽ䇋㾷䞞݊ॳ಴DŽׂᬍ䆹ߑ᭄ˈՓᅗ೼ӏԩᴎ఼Ϟ䖤㸠ᯊ䛑㛑ᠧॄߎℷ⹂މn ㄝѢ•2 ᄫ䭓•1 ⱘᚙ 㒗д 3•4 ೼᭄ⱘᇍѠⱘ㸹ⷕ㸼⼎Ёˈ៥Ӏ㓪ݭⱘ itoa ߑ᭄ϡ㛑໘⧚᳔໻ⱘ䋳᭄ˈे 䚼ߚ䇃䅸Ўᰃ঺Ͼ while ᕾ⦃ⱘᓔྟDŽ 㤝⥛ⱘ䇏㗙ᇚ whileܡҹ䙓ৃخㅵ≵᳝ᖙ㽕ˈԚ៥Ӏҡ✊⫼㢅ᣀোᇚ䆹䇁হᣀ䍋ᴹњˈ䖭ḋ ؐЎ 0ˈг㟇ᇥ㽕ᡞϔϾᄫヺᬒࠄ᭄㒘 s ЁDŽ݊Ёⱘ do•while 䇁হԧЁা᳝ϔᴵ䇁হˈሑ 䖭䞠᳝ᖙ㽕Փ⫼ do•while 䇁হˈ㟇ᇥՓ⫼ do•while 䇁হӮᮍ֓ϔѯˈ಴ЎेՓ n ⱘ } reverse(s); s[i] = '\0'; s[i++] = '•'; if (sign < 0) } while ((n /= 10) > 0); /* delete it */ s[i++] = n % 10 + '0'; /* get next digit */ do { /* generate digits in reverse order */ i = 0; n = •n; /* make n positive */ if ((sign = n) < 0) /* record sign */ int i, sign; { void itoa(int n, char s[]) /* itoa: convert n to characters in s */ 㕂DŽצ㽕⫳៤ডᑣⱘᄫヺІˈ✊ৢݡᡞ䆹ᄫヺІܜӀ佪 ⱘˈ಴ℸˈ៥צ᭄Ё⫳៤᭄ᄫⱘᮍ⊩ᇚ᭄ᄫ䕀ᤶЎᄫヺІˈ߭⫳៤ⱘᄫヺІⱘ⃵ᑣℷདᰃ乴 ⱘ㽕໡ᴖϔѯDŽབᵰᣝ✻ atoi ߑڣ᭄ⱘ䗚ߑ᭄ˈᅗᡞ᭄ᄫ䕀ᤶЎᄫヺІDŽ䖭ϾᎹ԰↨᳔߱ᛇ ᕾ⦃䇁হ᳝ᯊ䖬ᰃᕜ᳝⫼ⱘˈϟ䴶៥Ӏ䗮䖛ߑ᭄ itoa ᴹ䇈ᯢ䖭ϔ⚍DŽitoa ߑ᭄ᰃ atoi ߑ 㒣偠㸼ᯢˈdo•while ᕾ⦃↨ while ᕾ⦃੠ for ᕾ⦃⫼ᕫᇥᕫ໮DŽሑㅵབℸˈdo•while ໪ˈdo•while ᕾ⦃Ϣ Pascal 䇁㿔ⱘ repeat•until 䇁হㄝӋDŽ ߭ᕾ⦃㒜ℶDŽ䰸њᴵӊ⌟䆩ⱘ䇁Нϡৠˈ؛߭ݡ⃵ᠻ㸠䇁হˈձℸ㉏᥼DŽᔧ㸼䖒ᓣⱘؐবЎ ᠻ㸠ᕾ⦃ԧЁⱘ䇁হ䚼ߚˈ✊ৢݡ∖㸼䖒ᓣⱘؐDŽབᵰ㸼䖒ᓣⱘؐЎⳳˈܜˈ೼䖭ϔ㒧ᵘЁ while (㸼䖒ᓣ); 䇁হ do do•while ᕾ⦃ⱘ䇁⊩ᔶᓣབϟ˖ 䖭ḋᕾ⦃ԧ㟇ᇥ㹿ᠻ㸠ϔ⃵DŽ 䆩DŽϢℸⳌডˈC 䇁㿔Ёⱘ㄀ϝ⾡ᕾ⦃——do•while ᕾ⦃߭೼ᕾ⦃ԧᠻ㸠ৢ⌟䆩㒜ℶᴵӊˈ ៥Ӏ೼㄀ 1 ゴЁ᳒㒣䆆䖛ˈwhile Ϣ for 䖭ϸ⾡ᕾ⦃೼ᕾ⦃ԧᠻ㸠ࠡᇍ㒜ℶᴵӊ䖯㸠⌟ 3.6. do•whileᕾ⦃ DŽ԰Ўࠡᇐ੠ሒ䱣ⱘ•ᄫヺॳḋᥦॄDŽމҹ໘⧚ a•b•cǃa•z0•9 Ϣ•a•z ㄝ㉏Ԑⱘᚙ ೼ᄫヺІ s2 ЁᠽሩЎㄝӋⱘᅠᭈ߫㸼 abc…xyzDŽ䆹ߑ᭄ৃҹ໘⧚໻ᇣݭᄫ↡੠᭄ᄫˈᑊৃ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᳈⏅DŽ ӮՓ⿟ᑣⱘጠ༫خ䖛ᴹ៪㗙㓽䖯঺ϔሖᕾ⦃ˈ䖭ḋצcontinue 䇁হˈ߭ৃ㛑䳔㽕ᡞ⌟䆩乴 ϟˈབᵰϡՓ⫼މᔧᕾ⦃ⱘৢ䴶䚼ߚ↨䕗໡ᴖᯊˈᐌᐌӮ⫼ࠄ continue 䇁হDŽ䖭⾡ᚙ ... /* do positive elements */ continue; if (a[i] < 0) /* skip negative elements */ for (i = 0; i < n; i++) ໘⧚DŽ ㋴ⱘؐЎ䋳ˈ߭䏇䖛ϡܗ㋴DŽབᵰᶤϾܗ՟བˈϟ䴶䖭↉⿟ᑣ⫼Ѣ໘⧚᭄㒘 a Ёⱘ䴲䋳 ⦃ࣙ৿ⱘ switch 䇁হЁⱘ continue 䇁হˈᇚᇐ㟈䖯ܹϟϔ⃵ᕾ⦃DŽ 䕀⿏ࠄ䗦๲ᕾ⦃ব䞣䚼ߚDŽcontinue 䇁হা⫼Ѣᕾ⦃䇁হˈϡ⫼Ѣ switch 䇁হDŽᶤϾᕾ 䇁হЁˈcontinue 䇁হⱘᠻ㸠ᛣੇⴔゟेᠻ㸠⌟䆩䚼ߚ˗೼ for ᕾ⦃Ёˈ߭ᛣੇⴔՓ᥻ࠊ হ⫼ѢՓ forǃwhile ៪ do•while 䇁হᓔྟϟϔ⃵ᕾ⦃ⱘᠻ㸠DŽ೼ while Ϣ do•while continue 䇁হϢ break 䇁হᰃⳌ݇㘨ⱘˈԚᅗ≵᳝ break 䇁হᐌ⫼DŽcontinue 䇁 ៪ҙࣙ৿ぎⱑヺˈ䆹ߑ᭄гᰃℷ⹂ⱘDŽ বЎ䋳᭄ᯊ˄ेᭈϾᄫヺІ䛑㹿ᠿᦣᅠᯊ˅ˈᕾ⦃㒜ℶᠻ㸠DŽ䇏㗙ৃҹ偠䆕ˈेՓᄫヺІЎぎ ᰃぎḐヺǃࠊ㸼ヺҹঞᤶ㸠ヺⱘᄫヺDŽᔧᡒࠄヺড়ᴵӊⱘ㄀ϔϾᄫヺˈ៪ᔧᕾ⦃᥻ࠊব䞣 n strlen ߑ᭄䖨ಲᄫヺІⱘ䭓ᑺDŽfor ᕾ⦃ҢᄫヺІⱘ᳿ሒᓔྟডᮍ৥ᠿᦣᇏᡒ㄀ϔϾϡ } return n; s[n+1] = '\0'; break; if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n') for (n = strlen(s)•1; n >= 0; n••) int n; { int trim(char s[]) /* trim: remove trailing blanks, tabs, newlines */ Ў䴲ぎḐヺǃ䴲ࠊ㸼ヺǃ䴲ᤶ㸠ヺᯊˈህՓ⫼ break 䇁হҢᕾ⦃Ё䗔ߎDŽ ϟ䴶ⱘߑ᭄ trim ⫼Ѣߴ䰸ᄫヺІሒ䚼ⱘぎḐヺǃࠊ㸼ヺϢᤶ㸠ヺDŽᔧথ⦄᳔ে䖍ⱘᄫヺ 䇁হ㛑Փ⿟ᑣҢ switch 䇁হ៪᳔ݙሖᕾ⦃Ёゟे䏇ߎDŽ forǃwhile Ϣ do•while ㄝᕾ⦃Ёᦤࠡ䗔ߎˈህབৠҢ switch 䇁হЁᦤࠡ䗔ߎϔḋDŽbreak ϡ䗮䖛ᕾ⦃༈䚼៪ሒ䚼ⱘᴵӊ⌟䆩㗠䏇ߎᕾ⦃ˈ᳝ᯊᰃᕜᮍ֓ⱘDŽbreak 䇁হৃ⫼ѢҢ 3.7. break䇁হϢ continue 䇁হ ϔᅮⱘぎḐDŽܙ฿᠔ᕫ㒧ᵰⱘᎺ䖍 ᄫ↉ᆑᑺDŽЎњֱ䆕䕀ᤶৢ᠔ᕫⱘ㒧ᵰ㟇ᇥ݋᳝㄀ϝϾখ᭄ᣛᅮⱘ᳔ᇣᆑᑺˈ೼ᖙ㽕ᯊᑨ೼ 㒗д 3•6 ׂᬍ itoa ߑ᭄ˈՓᕫ䆹ߑ᭄ৃҹ᥹ᬊϝϾখ᭄DŽ݊Ёˈ㄀ϝϾখ᭄Ў᳔ᇣ ֱᄬ೼ s ЁDŽ ҹᄫヺⱘᔶᓣֱᄬࠄᄫヺІ s ЁDŽ՟བˈitob(n, s, 16)ᡞᭈ᭄ n Ḑᓣ࣪៤क݁䖯ࠊᭈ᭄ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ... /* didn't find any common element */ else ... /* got one: a[i•1] == b[j•1] */ if (found) found = 1; if (a[i] == b[j]) for (j = 0; j < m && !found; j++) for (i = 0; i < n && !found; i++) found = 0; ᓣ˖ ㋴ⱘ⿟ᑣ↉ᬍݭ៤ϟ߫ᔶܗ乱໪ⱘ䞡໡⌟䆩៪ব䞣DŽ՟བˈৃᇚϞ䴶߸ᅮᰃ৺݋᳝Ⳍৠ᭄㒘 ᠔᳝Փ⫼њ goto 䇁হⱘ⿟ᑣҷⷕ䛑㛑ᬍݭ៤ϡᏺ goto 䇁হⱘ⿟ᑣˈԚৃ㛑Ӯ๲ࡴϔѯ ... /* got one: a[i] == b[j] */ found: ... /* didn't find any common element */ goto found; if (a[i] == b[j]) for (j = 0; j < m; j++) for (i = 0; i < n; i++) ᮍ⊩ᰃ˖އ㛑ⱘ㾷 ㋴ⱘ䯂乬DŽϔ⾡ৃܗ៥Ӏᴹⳟ঺໪ϔϾ՟ᄤDŽ㗗㰥߸ᅮϸϾ᭄㒘 a Ϣ b Ёᰃ৺݋᳝Ⳍৠ goto 䇁হ᠔೼ߑ᭄ⱘӏԩ䇁হⱘࠡ䴶DŽᷛোⱘ԰⫼ඳᰃᭈϾߑ᭄DŽ ᷛোⱘੑৡৠব䞣ੑৡⱘᔶᓣⳌৠˈᷛোⱘৢ䴶㽕㋻䎳ϔϾݦোDŽᷛোৃҹԡѢᇍᑨⱘ ↨䕗ᮍ֓DŽ ೼䆹՟ᄤЁˈབᵰ䫭䇃໘⧚ҷⷕᕜ䞡㽕ˈᑊϨ䫭䇃ৃ㛑ߎ⦄೼໮ϾഄᮍˈՓ⫼ goto 䇁হᇚӮ /* clean up the mess */ error: ... } goto error; if (disaster) ... for ( ... ) { for ( ... ) ࠄⳂⱘⱘˈᅗা㛑Ң᳔ݙሖᕾ⦃䗔ߎࠄϞϔ㑻ⱘᕾ⦃DŽϟ䴶ᰃՓ⫼ goto 䇁হⱘϔϾ՟ᄤ˖ ϟՓ⫼ break 䇁হᰃϡ㛑䖒މⱘ㒧ᵘЁⱘ໘⧚䖛⿟ˈ՟བϔ⃵䏇ߎϸሖ៪໮ሖᕾ⦃DŽ䖭⾡ᚙ Ԛᰃˈ೼ᶤѯഎড়ϟ goto 䇁হ䖬ᰃ⫼ᕫⴔⱘDŽ᳔ᐌ㾕ⱘ⫼⊩ᰃ㒜ℶ⿟ᑣ೼ᶤѯ⏅ᑺጠ༫ ⫼ goto 䇁হDŽ হᰃ≵᳝ᖙ㽕ⱘˈᅲ䏉ЁϡՓ⫼ goto 䇁হгৃҹᕜᆍᯧഄݭߎҷⷕDŽ㟇ℸˈᴀкЁ䖬≵᳝Փ C 䇁㿔ᦤկњৃ䱣ᛣⒹ⫼ⱘ goto 䇁হҹঞᷛ䆄䏇䕀ԡ㕂ⱘᷛোDŽҢ⧚䆎Ϟ䆆ˈgoto 䇁 goto䇁হϢᷛো .3.8 ໻໮᭄ᚙމϟˈՓ⫼ goto 䇁হⱘ⿟ᑣ↉↨ϡՓ⫼ goto 䇁হⱘ⿟ᑣ↉㽕䲒ҹ⧚㾷੠㓈ᡸˈ ᇥ᭄ᚙމ䰸໪ˈ↨བ៥Ӏࠡ䴶᠔Вⱘ޴Ͼ՟ᄤDŽሑㅵ䆹䯂乬ᑊϡ໾Ϲ䞡ˈԚ៥Ӏ䖬ᰃᓎ䆂ሑ ৃ㛑ᇥഄՓ⫼ goto 䇁হDŽ Click to buy NOW! PDF•XC H ANGE w w w . d o c u • t r a c k.com w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䚼ߚ䆒䅵៤ϔϾ⣀ゟⱘߑ᭄DŽߚ߿໘⧚ 3 Ͼᇣⱘ䚼ߚ↨໘⧚ϔϾ໻ⱘᭈԧ᳈ᆍᯧˈ಴Ў䖭ḋ ᰃˈ߽⫼݊㒧ᵘᡞ↣ϔ⫣خሑㅵ៥Ӏৃҹᡞ᠔᳝ⱘҷⷕ䛑ᬒ೼Џ⿟ᑣ main ЁˈԚ᳈དⱘ ᠧॄ䆹㸠 if (䆹㸠ࣙ৿ᣛᅮⱘ῵ᓣ) whiel (䖬᳝᳾໘⧚ⱘ㸠) 䆹ӏࡵৃҹᯢ⹂ഄߦߚ៤ϟ߫ 3 䚼ߚ˖ Re•mould it nearer to the Heart's Desire! Would not we shatter it to bits •• and then Ah Love! could you and I with Fate conspire ⿟ᑣᠻ㸠ৢ䕧ߎϟ߫㒧ᵰ˖ Re•mould it nearer to the Heart's Desire! Would not we shatter it to bits •• and then To grasp this sorry Scheme of Things entire, Ah Love! could you and I with Fate conspire ᴹ˄䖭ᰃ UNIX ⿟ᑣ grep ⱘ⡍՟˅՟བˈ೼ϟ߫ϔ㒘᭛ᴀ㸠Ёᶹᡒࣙ৿ᄫヺІ“ouldāⱘ㸠˖ ៥Ӏᴹ䆒䅵ᑊ㓪ݭϔϾ⿟ᑣˈᅗᇚ䕧ܹЁࣙ৿⡍ᅮĀ῵ᓣā៪ᄫヺІⱘ৘㸠ᠧॄߎܜ佪 4.1. ߑ᭄ⱘ෎ᴀⶹ䆚 䗮䖛ᅣখ᭄߯ᓎᏺᓩোⱘᄫヺІⱘᮍ⊩˅ˈᇍᅣᠽሩ䖛⿟ⱘ᥻ࠊ᳈ϹḐDŽ C 䇁㿔乘໘⧚ⱘࡳ㛑гᕫࠄњ๲ᔎDŽᮄⱘ乘໘⧚఼ࣙ৿ϔ㒘᳈ᅠᭈⱘᴵӊ㓪䆥ᣛҸ˄ϔ⾡ ྟ࣪ⱘ䗖⫼㣗ೈг᳈ࡴᑓ⊯њˈ㞾ࡼ᭄㒘Ϣ㒧ᵘ䛑ৃҹ䖯㸠߱ྟ࣪DŽ 䖯ϔℹᯢ⹂њৡᄫⱘ԰⫼ඳ㾘߭ˈ⡍߿㽕∖↣Ͼ໪䚼ᇍ䈵া㛑᳝ϔϾᅮНDŽ߱ޚANSI ᷛ 䕀ᤶDŽ ⱘ C 䇁㿔⠜ᴀ᳈໮ⱘ䫭䇃DŽᑊϨˈབᵰখ᭄ໄᯢᕫᔧˈ⿟ᑣৃҹ㞾ࡼഄ䖯㸠䗖ᔧⱘᔎࠊ㉏ൟ њׂᬍDŽ෎Ѣ䆹ॳ಴ˈ㓪䆥఼ህ᳝ৃ㛑Ẕ⌟ߎ↨ҹࠡخᇍߑ᭄ᅮНⱘ䇁⊩гޚ䗖ᑨˈANSI ᷛ 䆌೼ໄᯢߑ᭄ᯊໄᯢখ᭄ⱘ㉏ൟDŽЎњՓߑ᭄ⱘໄᯢϢᅮНⳌܕ᳒㒣䆆䖛ˈⳂࠡ C 䇁㿔Ꮖ㒣 ⱘ᳔ᯢᰒⱘׂᬍᰃߑ᭄ໄᯢϢߑ᭄ᅮН䖭ϸᮍ䴶DŽ㄀ 1 ゴЁ៥Ӏخᇍ C 䇁㿔᠔ޚANSI ᷛ 䖭ϔ䖛⿟ˈ಴Ў㓪䆥Ϣࡴ䕑ⱘ݋ԧᅲ⦄㒚㡖೼৘Ͼ㓪䆥㋏㒳ЁᑊϡⳌৠDŽ Ͼ᭛ӊৃҹऩ⣀㓪䆥ˈᑊৃҹϢᑧЁᏆ㓪䆥䖛ⱘߑ᭄ϔ䍋ࡴ䕑DŽ៥Ӏ೼䖭䞠ϡᠧㅫ䆺㒚䅼䆎 ߑ᭄㒘៤ˈ㗠ϡᰃ⬅ᇥ䞣䕗໻ⱘߑ᭄㒘៤DŽϔϾ⿟ᑣৃҹֱᄬ೼ϔϾ៪㗙໮Ͼ⑤᭛ӊЁDŽ৘ C 䇁㿔೼䆒䅵Ё㗗㰥њߑ᭄ⱘ催ᬜᗻϢᯧ⫼ᗻ䖭ϸϾ಴㋴DŽC 䇁㿔⿟ᑣϔ㠀䛑⬅䆌໮ᇣⱘ ԧ᪡԰㒚㡖䱤㮣䍋ᴹˈҢ㗠ՓᭈϾ⿟ᑣ㒧ᵘ᳈ࡴ⏙᱄ˈᑊ䰡Ԣׂᬍ⿟ᑣⱘ䲒ᑺDŽ ᵘ䗴⿟ᑣˈ㗠ϡ䳔㽕䞡ᮄ㓪ݭϔѯҷⷕDŽϔϾ䆒䅵ᕫᔧⱘߑ᭄ৃҹᡞ⿟ᑣЁϡ䳔㽕њ㾷ⱘ݋ ߑ᭄ৃҹᡞ໻ⱘ䅵ㅫӏࡵߚ㾷៤㢹ᑆϾ䕗ᇣⱘӏࡵˈ⿟ᑣ䆒䅵Ҏਬৃҹ෎Ѣߑ᭄䖯ϔℹ ㄀4ゴߑ᭄Ϣ⿟ᑣ㒧ᵘ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! } return i; s[i] = '\0'; s[i++] = c; if (c == '\n') s[i++] = c; while (••lim > 0 && (c=getchar()) != EOF && c != '\n') i = 0; int c, i; { int getline(char s[], int lim) /* getline: get line into s, return length */ } return found; } found++; printf("%s", line); if (strindex(line, pattern) >= 0) { while (getline(line, MAXLINE) > 0) int found = 0; char line[MAXLINE]; { main() /* find all lines matching pattern */ char pattern[] = "ould"; /* pattern to search for */ int strindex(char source[], char searchfor[]); int getline(char line[], int max) #define MAXLINE 1000 /* maximum input line length */ #include 㗙ৃᇚᅗϢ㄀ 1 ゴЁⱘ⠜ᴀ䖯㸠↨䕗ˈ៪䆌ӮᕫࠄϔѯਃথDŽ ⿟ᑣ䖤㸠ᯊᇚ῵ᓣ԰Ўখ᭄Ӵ䗦㒭ߑ᭄DŽ݊Ёˈgetline ߑ᭄䕗ࠡ䴶ⱘ⠜ᴀг⿡᳝ϡৠˈ䇏 ᰃϔ⾡᳔䗮⫼ⱘᴎࠊDŽ៥Ӏ೼䖭䞠াㅔऩ䅼䆎ᄫヺ᭄㒘ⱘ߱ྟ࣪ᮍ⊩ˈ㄀ 5 ゴᇚҟ㒡བԩ೼ ᑣˈ䇏㗙ৃҹᶹⳟ৘䚼ߚᰃᗢḋ㒘ড়೼ϔ䍋ⱘDŽ៥Ӏ⦄೼ᶹᡒⱘ῵ᓣᰃᄫヺІᄫ䴶ؐˈᅗϡ ᅠ៤䖭ḋⱘ䆒䅵ৢˈ㓪ݭᭈϾ⿟ᑣⱘ㒚㡖ህⳈ៾њᔧњDŽϟ䴶߫ߎⱘህᰃϔϾᅠᭈⱘ⿟ ⱘᑧߑ᭄ strstr ⱘࡳ㛑㉏ԐѢ strindex ߑ᭄ˈԚ䆹ᑧߑ᭄䖨ಲⱘᰃᣛ䩜㗠ϡᰃϟᷛؐDŽ˅ ᑧЁᦤկޚᴖⱘ῵ᓣऍ䜡ˈা䳔᳓ᤶ strindex ߑ᭄ेৃˈ⿟ᑣⱘ݊ԭ䚼ߚৃֱᣕϡবDŽ˄ᷛ DŽབᵰҹৢ䳔㽕䖯㸠᳈໡މ䖭ḋⱘ䋳᭄㸼⼎༅䋹ⱘᚙ 1•ڣ⫼ؐাৃ㛑Ў 0 ៪ℷ᭄ˈ಴ℸৃҹ 䍋ྟԡ㕂៪㋶ᓩDŽᔧ s ϡࣙ৿ t ᯊˈ䖨ಲؐЎ•1DŽ⬅Ѣ C 䇁㿔᭄㒘ⱘϟᷛҢ 0 ᓔྟˈϟᷛⱘ ៥Ӏ㓪ݭߑ᭄ strindex(s, t) ᅲ⦄䆹ⳂᷛDŽ䆹ߑ᭄䖨ಲᄫヺІ t ೼ᄫヺІ s Ёߎ⦄ⱘ Ͼ߸ᅮĀ䆹㸠ࣙ৿ᣛᅮⱘ῵ᓣāⱘߑ᭄DŽ ߑ᭄ᅲ⦄Āᠧॄ䆹㸠āˈ䖭Ͼߑ᭄ᰃ⦄៤ⱘˈ߿ҎᏆ㒣ᦤկњDŽгህᰃ䇈ˈ៥Ӏা䳔㽕㓪ݭϔ ៥Ӏ⫼ߑ᭄ getline ᅲ⦄Ā䖬᳝᳾໘⧚ⱘ㸠āˈ䆹ߑ᭄Ꮖ೼㄀ 1 ゴЁҟ㒡䖛˗⫼ printf гৃҹ೼݊ᅗ⿟ᑣЁՓ⫼DŽ ᇥњϡᖙ㽕ⱘⳌѦᕅડⱘᴎӮˈᑊϨˈ䖭ѯߑ᭄ޣҹᡞϡⳌ݇ⱘ㒚㡖䱤㮣೼ߑ᭄ЁˈҢ㗠ৃ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᴹ㓪䆥䖭 3 Ͼ᭛ӊˈᑊᡞ⫳៤ⱘⳂᷛҷⷕߚ߿ᄬᬒ೼᭛ӊ main.oǃgetline.o Ϣ cc main.c getline.c strindex.c ᬒ೼ৡЎ main.cǃgetline.c Ϣ strindex.c ⱘ 3 Ͼ᭛ӊЁˈ߭ৃҹՓ⫼ੑҸ ᅮ᳝ 3 Ͼߑ᭄ߚ߿ᄬ؛೼ UNIX ㋏㒳ЁˈৃҹՓ⫼㄀ 1 ゴЁᦤࠄ䖛ⱘ cc ੑҸᠻ㸠䖭ϔӏࡵDŽ ೼ϡৠⱘ㋏㒳Ёˈֱᄬ೼໮Ͼ⑤НӊЁⱘ C 䇁㿔⿟ᑣⱘ㓪䆥Ϣࡴ䕑ᴎࠊᰃϡৠⱘDŽ՟བˈ ೼䇗⫼䆹⿟ᑣⱘ⦃๗ЁՓ⫼DŽ ೼Ϟ䴶ⱘ῵ᓣᶹᡒ⿟ᑣЁˈЏ⿟ᑣ main 䖨ಲњϔϾ⢊ᗕˈेऍ䜡ⱘ᭄ⳂDŽ䆹䖨ಲؐৃҹ ϟˈབᵰߑ᭄≵᳝៤ࡳഄ䖨ಲϔϾؐˈ߭ᅗⱘĀؐā㚃ᅮᰃ᮴⫼ⱘDŽމ೼ӏԩᚙ DŽܚ䖨ಲؐˈ㗠Ң঺ϔϾഄᮍ䖨ಲᯊ≵᳝䖨ಲؐˈ䆹ߑ᭄ᑊϡ䴲⊩ˈԚৃ㛑ᰃϔ⾡ߎ䯂乬ⱘᕕ 㗠㒧ᴳᠻ㸠ᯊˈ᥻ࠊৠḋгӮ䖨ಲ㒭䇗⫼㗙˄ϡ䖨ಲؐ˅DŽབᵰᶤϾߑ᭄ҢϔϾഄᮍ䖨ಲᯊ᳝ 䇁হⱘৢ䴶≵᳝㸼䖒ᓣᯊˈߑ᭄ᇚϡ৥䇗⫼㗙䖨ಲؐDŽᔧ㹿䇗⫼ߑ᭄ᠻ㸠ࠄ᳔ৢⱘে㢅ᣀো 䇗⫼ߑ᭄ৃҹᗑ⬹䖨ಲؐDŽᑊϨˈreturn 䇁হⱘৢ䴶гϡϔᅮ䳔㽕㸼䖒ᓣDŽᔧ return োᰃৃ䗝ⱘDŽ ೼ᖙ㽕ᯊˈ㸼䖒ᓣᇚ㹿䕀ᤶЎߑ᭄ⱘ䖨ಲؐ㉏ൟDŽ㸼䖒ᓣϸ䖍䗮ᐌࡴϔᇍ೚ᣀোˈℸ໘ⱘᣀ return 㸼䖒ᓣ˗ 㹿䇗⫼ߑ᭄䗮䖛 return 䇁হ৥䇗⫼㗙䖨ಲؐˈreturn 䇁হⱘৢ䴶ৃҹ䎳ӏԩ㸼䖒ᓣ˖ ߚ⾏ࠄ໮Ͼ᭛ӊЁˈ⑤⿟ᑣህৃҹߚ៤໮Ͼ᭛ӊDŽ ؐҹঞ໪䚼ব䞣䖯㸠DŽߑ᭄೼⑤᭛ӊЁߎ⦄ⱘ⃵ᑣৃҹᰃӏᛣⱘDŽা㽕ֱ䆕↣ϔϾߑ᭄ϡ㹿 ⿟ᑣৃҹⳟ៤ᰃব䞣ᅮН੠ߑ᭄ᅮНⱘ䲚ৄDŽߑ᭄П䯈ⱘ䗮ֵৃҹ䗮䖛খ᭄ǃߑ᭄䖨ಲ 䅸Ў int ㉏ൟDŽ ҷⷕ˅DŽབᵰߑ᭄ᅮНЁⳕ⬹њ䖨ಲؐ㉏ൟˈ߭咬ܙ฿⿟ᑣᓔথᳳ䯈⫼ҹֱ⬭ԡ㕂˄⬭ᕙҹৢ 䆹ߑ᭄ϡᠻ㸠ӏԩ᪡԰гϡ䖨ಲӏԩؐDŽ䖭⾡ϡᠻ㸠ӏԩ᪡԰ⱘߑ᭄᳝ᯊᕜ᳝⫼ˈᅗৃҹ೼ dummy() {} ߑ᭄ᅮНЁⱘ৘ᵘ៤䚼ߚ䛑ৃҹⳕ⬹DŽ᳔ㅔऩⱘߑ᭄བϟ᠔⼎˖ } ಒੜۤဴࡳ { ೴ੜ(ϵ೴ಒੜζ)ۆ ᄔफ໸ݍֲ ˖ߑ᭄ⱘᅮНᔶᓣབϟ } return •1; } return i; if (k > 0 && t[k] == '\0') ; for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++) for (i = 0; s[i] != '\0'; i++) { int i, j, k; { int strindex(char s[], char t[]) strindex: return index of t in s, •1 if none */ */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䆹Ⳃⱘˈϔ⾡ᮍ⊩ᰃ೼䇗⫼ߑ᭄Ёᰒᓣໄᯢ atof ߑ᭄DŽϟ䴶᠔⼎ⱘ෎ᴀ䅵ㅫ఼⿟ᑣ˄ҙ䗖⫼ ݊⃵ˈ䇗⫼ߑ᭄ᖙ乏ⶹ䘧 atof ߑ᭄䖨ಲⱘᰃ䴲ᭈൟؐˈ䖭ϔ⚍гᰃᕜ䞡㽕ⱘDŽЎњ䖒ࠄ } return sign * val / power; } power *= 10; val = 10.0 * val + (s[i] • '0'); for (power = 1.0; isdigit(s[i]); i++) { i++; if (s[i] == '.') val = 10.0 * val + (s[i] • '0'); for (val = 0.0; isdigit(s[i]); i++) i++; if (s[i] == '+' || s[i] == '•') sign = (s[i] == '•') ? •1 : 1; ; for (i = 0; isspace(s[i]); i++) /* skip white space */ int i, sign; double val, power; { double atof(char s[]) /* atof: convert string s to double */ #include ؐⱘ㉏ൟৡᑨᬒ೼ߑ᭄ৡᄫПࠡˈབϟ᠔⼎˖ Ѣ atof ߑ᭄ⱘ䖨ಲؐ㉏ൟϡᰃ intˈ಴ℸ䆹ߑ᭄ᖙ乏ໄᯢ䖨ಲؐⱘ㉏ൟDŽ䖨ಲ⬅ˈܜ佪 㛑ⱘ atof ߑ᭄ˈ೼༈᭛ӊЁໄᯢDŽ ᑧЁࣙ৿㉏Ԑࡳޚ㓪ݭⱘ⠜ᴀᑊϡᰃϔϾ催䋼䞣ⱘ䕧ܹ䕀ᤶߑ᭄ˈᅗऴ⫼њ䖛໮ⱘぎ䯈DŽᷛ DŽ៥Ӏ䖭䞠މ᭄䳔㽕໘⧚ৃ䗝ⱘヺো੠ᇣ᭄⚍ˈᑊ㽕㗗㰥ৃ㛑㔎ᇥᭈ᭄䚼ߚ៪ᇣ᭄䚼ߚⱘᚙ atof ߑ᭄ᰃ atoi ߑ᭄ⱘᠽሩˈ㄀ 2 ゴϢ㄀ 3 ゴᏆ䅼䆎њ atoi ߑ᭄ⱘ޴Ͼ⠜ᴀDŽatof ߑ atof(s)ᴹ䇈ᯢߑ᭄䖨ಲ䴲ᭈൟؐⱘᮍ⊩DŽ䆹ߑ᭄ᡞᄫヺІ s 䕀ᤶЎⳌᑨⱘঠ㊒ᑺ⍂⚍᭄DŽ ㄝߑ᭄˅䖨ಲⱘᰃ double ㉏ൟⱘؐˈᶤѯϧ⫼ߑ᭄߭䖨ಲ݊ᅗ㉏ൟⱘؐDŽ៥Ӏ䗮䖛ߑ᭄ བᶤϾߑ᭄ᖙ乏䖨ಲ݊ᅗ㉏ൟⱘؐˈ䆹ᗢМࡲਸ਼˛䆌໮᭄ؐߑ᭄˄བ sqrtǃsin Ϣ cos؛ ࠄⳂࠡЎℶˈ៥Ӏ᠔䅼䆎ⱘߑ᭄䛑ᰃϡ䖨ಲӏԩؐ˄void˅៪া䖨ಲ int ㉏ൟؐⱘߑ᭄DŽ 4.2. 䖨ಲ䴲ᭈൟؐⱘߑ᭄ བᵰ s Ёϡࣙ৿ tˈ߭䖨ಲ•1DŽ 㒗д 4•1 㓪ݭߑ᭄ strindex(s, t)ˈᅗ䖨ಲᄫヺІ t ೼ s Ё᳔ে䖍ߎ⦄ⱘԡ㕂DŽ ⑤᭛ӊϢⳂᷛ᭛ӊDŽ strindex.o ϔ䍋ࡴ䕑ࠄৃᠻ㸠᭛ӊЁDŽcc ੑҸՓ⫼“.cāϢ“.oā䖭ϸ⾡ᠽሩৡᴹऎߚ ᇍ main.c ᭛ӊ䞡ᮄ㓪䆥ˈᑊᇚ㓪䆥ⱘ㒧ᵰϢҹࠡᏆ㓪䆥䖛ⱘⳂᷛ᭛ӊ getline.o ੠ cc main.c getline.o strindex.o 䫭䇃˄↨བ᭛ӊ main.c Ёᄬ೼䫭䇃˅ˈ߭ৃҹ䗮䖛ੑҸ strindex.o Ёˈ✊ৢݡᡞ䖭 3 Ͼ᭛ӊϔ䍋ࡴ䕑ࠄৃᠻ㸠᭛ӊ a.out ЁDŽབᵰ⑤⿟ᑣЁᄬ೼ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! double atof(char s[]); { int atoi(char s[]) /* atoi: convert string s to integer using atof */ ᤶЎ int ㉏ൟ˅˖ ೼ℷ⹂䖯㸠ໄᯢⱘߑ᭄ atof ⱘ෎⸔Ϟˈ៥Ӏৃҹ߽⫼ᅗ㓪ݭߎߑ᭄ atoi˄ᇚᄫヺІ䕀 ᯢDŽ ᰃϡᦤ׵ⱘDŽབᵰߑ᭄ᏺ᳝খ᭄ˈ߭㽕ໄᯢᅗӀ˗བᵰ≵᳝খ᭄ˈ߭Փ⫼ void 䖯㸠ໄخ䖭М ⱘ䖭⾡⡍⅞໘⧚ᰃЎњՓᮄⱘ㓪䆥఼㛑㓪䆥↨䕗㗕ⱘ C 䇁㿔⿟ᑣDŽϡ䖛ˈ೼ᮄ㓪ݭⱘ⿟ᑣЁ 䆒ˈᑊӮ݇䯁᠔᳝ⱘখ᭄ẔᶹDŽᇍぎখ᭄㸼؛䙷М㓪䆥⿟ᑣгϡӮᇍߑ᭄ atof ⱘখ᭄԰ӏԩ double atof˄ ˅˗ 䆒DŽᑊϨˈབᵰߑ᭄ໄᯢЁϡࣙ৿খ᭄ˈ՟བ˖؛ᇍ݊খ᭄԰ӏԩ ᅮЎ int ㉏ൟˈԚϞϟ᭛ᑊϡ؛ϟ᭛ህӮ䅸Ў䆹ৡᄫᰃϔϾߑ᭄ৡᄫˈ䆹ߑ᭄ⱘ䖨ಲؐᇚ㹿 ࠡ≵᳝ໄᯢ䖛ⱘϔϾৡᄫߎ⦄೼ᶤϾ㸼䖒ᓣЁˈᑊϨ݊ৢ㋻䎳ϔϾᎺ೚ᣀোˈ䙷МϞܜབᵰ sum += atof(line) བ˖ ݊ЁⱘϔϾॳ಴ᰃˈབᵰ≵᳝ߑ᭄ॳൟˈ߭ߑ᭄ᇚ೼㄀ϔ⃵ߎ⦄ⱘ㸼䖒ᓣЁ㹿䱤ᓣໄᯢˈ՟ ḍ᥂ࠡ䴶᳝݇ߑ᭄ⱘໄᯢབԩϢᅮНֱᣕϔ㟈ⱘ䅼䆎ˈথ⫳ϡऍ䜡⦄䈵ԐТᕜҸҎৗ᚞DŽ 㗠 main ߑ᭄ैᇚ䖨ಲؐᣝ✻ int ㉏ൟ໘⧚ˈ᳔ৢⱘ㒧ᵰؐ↿᮴ᛣНDŽ ⾡ৃ㛑ᗻ᳈໻˅ˈ䖭⾡ϡऍ䜡ⱘ䫭䇃ህ᮴⊩Ẕ⌟ߎᴹˈatof ߑ᭄ᇚ䖨ಲ double ㉏ൟⱘؐˈ ᭛ӊЁˈᑊϨ㉏ൟϡϔ㟈ˈ㓪䆥఼ህӮẔ⌟ࠄ䆹䫭䇃DŽԚᰃˈབᵰ atof ߑ᭄ᰃऩ⣀㓪䆥ⱘ˄䖭 ߑ᭄ atof ⱘໄᯢϢᅮНᖙ乏ϔ㟈DŽབᵰ atof ߑ᭄Ϣ䇗⫼ᅗⱘЏߑ᭄ main ᬒ೼ৠϔ⑤ double ㉏ൟⱘؐDŽ 㸼ᯢ sum ᰃϔϾ double ㉏ൟⱘব䞣ˈatof ߑ᭄ᏺ᳝Ͼ char[]㉏ൟⱘখ᭄ˈϨ䖨ಲϔϾ double sum, atof(char []); ݊Ёˈໄᯢ䇁হ } return 0; printf("\t%g\n", sum += atof(line)); while (getline(line, MAXLINE) > 0) sum = 0; int getline(char line[], int max); char line[MAXLINE]; double sum, atof(char []); { main() /* rudimentary calculator */ #define MAXLINE 100 #include ᇍᅗӀ∖੠ˈ೼↣⃵䕧ܹᅠ៤ৢᡞ䖭ѯ᭄ⱘ㌃䅵ᘏ੠ᠧॄߎᴹ˖ Ѣᬃ⼼㈓䅵ㅫ˅Ё᳝㉏ԐⱘໄᯢDŽ䆹⿟ᑣ೼↣㸠Ё䇏পϔϾ᭄˄᭄ⱘࠡ䴶ৃ㛑᳝ℷ䋳ো˅ˈᑊ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ೼䗚⊶݄㸼⼎⊩Ёˈ᠔᳝䖤ㅫヺ䛑䎳೼᪡԰᭄ⱘৢ䴶DŽ↨བˈϟ߫Ё㓔㸼䖒ᓣ˖ ㄝ䇁㿔гՓ⫼њ䗚⊶݄㸼⼎⊩˅DŽ 䗚⊶݄㸼⼎⊩ҷ᳓᱂䗮ⱘЁ䕡㸼⼎⊩˄䗚⊶݄㸼⼎⊩⫼೼ᶤѯ㹪⦡䅵ㅫ఼ЁˈForth Ϣ Postscript ǃЬ˄*˅ǃ䰸˄/˅ಯ߭䖤ㅫࡳ㛑ⱘ䅵ㅫ఼⿟ᑣDŽЎњ᳈ᆍᯧᅲ⦄ˈ៥Ӏ೼䅵ㅫ఼ЁՓ⫼˅•˄ ޣϟ䴶៥Ӏ䗮䖛ϔϾ᳈໡ᴖⱘ՟ᄤᴹ䇈ᯢ䖭ϔ⚍DŽ៥ӀⱘⳂᷛᰃ㓪ݭϔϾ݋᳝ࡴ˄+˅ǃ ᮍᓣ֓ᰃᡞ䖭ѯ݅ѿ᭄᥂ᅮНЎ໪䚼ব䞣ˈ㗠ϡᰃ԰Ўߑ᭄খ᭄Ӵ䗦DŽ ϟ᳔ᮍ֓ⱘމ಴ℸˈབᵰϸϾߑ᭄ᖙ乏݅ѿᶤѯ᭄᥂ˈ㗠䖭ϸϾߑ᭄Ѧϡ䇗⫼ᇍᮍˈ䖭⾡ᚙ ⍜༅DŽ㗠໪䚼ব䞣ᰃ∌Йᄬ೼ⱘˈᅗӀⱘؐ೼ϔ⃵ߑ᭄䇗⫼ࠄϟϔ⃵ߑ᭄䇗⫼П䯈ֱᣕϡবDŽ ব䞣া㛑೼ߑ᭄ݙ䚼Փ⫼ˈҢ݊᠔೼ⱘߑ᭄㹿䇗⫼ᯊব䞣ᓔྟᄬ೼ˈ೼ߑ᭄䗔ߎᯊব䞣гᇚ ໪䚼ব䞣ⱘ⫼䗨䖬㸼⦄೼ᅗӀϢݙ䚼ব䞣Ⳍ↨݋᳝᳈໻ⱘ԰⫼ඳ੠᳈䭓ⱘ⫳ᄬᳳDŽ㞾ࡼ ᵘѻ⫳ϡ㡃ⱘᕅડˈ㗠Ϩৃ㛑Ӯᇐ㟈⿟ᑣЁ৘Ͼߑ᭄П䯈݋᳝໾໮ⱘ᭄᥂㘨㋏DŽ ᖙ乏䴲ᐌ䇼ᜢˈ಴Ў䖭⾡ᮍᓣৃ㛑ᇍ⿟ᑣ㒧خ᳝ᬜDŽԚᰃˈ៥Ӏ೼㄀ 1 ゴЁᏆ㒣ᣛߎˈ䖭ḋ བᵰߑ᭄П䯈䳔㽕݊ѿ໻䞣ⱘব䞣ˈՓ⫼໪䚼ব䞣㽕↨Փ⫼ϔϾᕜ䭓ⱘখ᭄㸼᳈ᮍ֓ǃ 䗮䖛ᶤ⾡ᮍᓣ䖯㸠ໄᯢDŽ ߑ᭄খ᭄Ϣ䖨ಲؐⱘᮍᓣDŽӏԩߑ᭄䛑ৃҹ䗮䖛ৡᄫ䆓䯂ϔϾ໪䚼ব䞣ˈᔧ✊䖭Ͼৡᄫ䳔㽕 ಴Ў໪䚼ব䞣ৃҹ೼ܼሔ㣗ೈݙ䆓䯂ˈ䖭ህЎߑ᭄П䯈ⱘ᭄᥂Ѹᤶᦤկњϔ⾡ৃҹҷ᳓ ೼ᶤϔϾ⑤᭛ӊЁՓ⫼ⱘ໪䚼ব䞣Ϣߑ᭄DŽ COMMON ഫ៪ Pascal 䇁㿔Ё೼᳔໪ሖ⿟ᑣഫЁໄᯢⱘব䞣DŽ៥Ӏᇚ೼ৢ䴶ҟ㒡བԩᅮНা㛑 Ёᡞ䖭ϔᗻ䋼⿄Ў໪䚼䫒᥹˅DŽ೼䖭ϾᛣНϞˈ໪䚼ব䞣㉏ԐѢ Fortran 䇁㿔ⱘޚ䈵˄ᷛ ໪䚼ব䞣ⱘ᠔᳝ᓩ⫼˄ेՓ䖭⾡ᓩ⫼ᴹ㞾Ѣऩ⣀㓪䆥ⱘϡৠߑ᭄˅ᅲ䰙Ϟ䛑ᰃᓩ⫼ৠϔϾᇍ ϟˈ໪䚼ব䞣Ϣߑ᭄݋᳝ϟ߫ᗻ䋼˖䗮䖛ৠϔϾৡᄫᇍމ಴ℸߑ᭄ᴀ䑿ᰃĀ໪䚼ⱘāDŽ咬䅸ᚙ 䆌೼ϔϾߑ᭄ЁᅮН݊ᅗߑ᭄ˈܕН೼ߑ᭄П໪ˈ಴ℸৃҹ೼䆌໮ߑ᭄ЁՓ⫼DŽ⬅Ѣ C 䇁㿔ϡ external Ϣ internal Ⳍᇍⱘˈinternal ⫼Ѣᦣ䗄ᅮН೼ߑ᭄ݙ䚼ⱘߑ᭄খ᭄ঞব䞣DŽ໪䚼ব䞣ᅮ C 䇁㿔⿟ᑣৃҹⳟ៤⬅ϔ㋏߫ⱘ໪䚼ᇍ䈵ᵘ៤ˈ䖭ѯ໪䚼ᇍ䈵ৃ㛑ᰃব䞣៪ߑ᭄DŽᔶᆍ䆡 4.3. ໪䚼ব䞣 ⱘ⾥ᄺ㸼⼎⊩ˈ݊Ёˈ⍂⚍᭄ৢ䴶ৃ㛑Ӯ㋻䎳ϔϾ e ៪ E ҹঞϔϾᣛ᭄˄ৃ㛑᳝ℷ䋳ো˅DŽ 123.45e•6 Փᅗৃҹ໘⧚ᔶབˈܙ㒗д 4•2 ᇍ atof ߑ᭄䖯㸠ᠽ ㉏ൟ䕀ᤶⱘᮍ⊩ᰒᓣ㸼ᯢњ᠔㽕ᠻ㸠ⱘ䕀ᤶ᪡԰ˈ಴ℸৃҹ䰆ℶ᳝݇ⱘ䄺ਞֵᙃDŽ 䖭⾡᪡԰ৃ㛑Ӯ϶༅ֵᙃˈᶤѯ㓪䆥఼ৃ㛑Ӯᇍℸ㒭ߎ䄺ਞֵᙃDŽ೼䆹ߑ᭄Ёˈ⬅Ѣ䞛⫼њ ᠔ҹˈreturn 䇁হЁⱘ atof ߑ᭄ⱘ double ㉏ൟؐᇚ㹿㞾ࡼ䕀ᤶЎ int ㉏ൟؐDŽԚᰃˈ ݊Ёˈ㸼䖒ᓣⱘؐ೼䖨ಲПࠡᇚ㹿䕀ᤶЎߑ᭄ⱘ㉏ൟDŽ಴Ўߑ᭄ atoi ⱘ䖨ಲؐЎ int ㉏ൟˈ return(㸼䖒ᓣ); 䇋⊼ᛣ݊Ёⱘໄᯢ੠ return 䇁হⱘ㒧ᵘDŽ೼ϟ߫ᔶᓣⱘ return 䇁হЁ˖ } return (int) atof(s); w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! int getop(char s[]) { ... } double pop(void) { ... } void push( double f) { ... } push Ϣ pop ᠔Փ⫼ⱘ໪䚼ব䞣 main() { ... } main Փ⫼ⱘߑ᭄ໄᯢ #define... /* ϔѯ define ᅮН */ #include... /* ϔѯࣙ৿༈᭛ӊ */ ߫ᔶᓣ˖ ᡞϞ䴶䖭↉䆱䕀ᤶ៤ҷⷕᕜᆍᯧDŽབᵰᡞ䆹⿟ᑣᬒ೼ϔϾ⑤᭛ӊЁˈ⿟ᑣৃ㛑㉏ԐѢϟ 䆓䯂ˈ㗠ϡ㛑㹿 main ߑ᭄䆓䯂DŽ 㸠य़ܹϢᔍߎ᪡԰DŽ಴ℸˈৃҹᡞᷜঞⳌֵ݇ᙃᬒ೼໪䚼ব䞣Ёˈᑊাկ push Ϣ pop ߑ᭄ 㒭ᇍᅗᠻ㸠य़ܹ៪ᔍߎ᪡԰ⱘߑ᭄DŽԚᰃˈmain ߑ᭄ϡ䳔㽕њ㾷᥻ࠊᷜⱘব䞣ֵᙃˈᅗা䖯 ՟⿟ৃҹⳈ᥹䆓䯂ᅗ˛ϔ⾡ৃ㛑ᰃᡞᅗᬒ೼Џߑ᭄ main Ёˈᡞᷜঞ݊ᔧࠡԡ㕂԰Ўখ᭄Ӵ䗦 гህᰃ䇈ˈાѯ˛ܓࠄⳂࠡЎℶˈ៥Ӏ䖬≵᳝䅼䆎䆒䅵ЁⱘϔϾ䞡㽕䯂乬˖ᡞᷜᬒ೼ા ঺໪䖬䳔㽕ϔϾऩ⣀ⱘߑ᭄ᴹপϟϔϾ䕧ܹ䖤ㅫヺ៪᪡԰᭄DŽ ᰒᕫᕜ䭓њˈ᳔དᡞᅗӀ䆒䅵៤⣀ゟⱘߑ᭄ˈ㗠ϡ㽕ᡞᅗӀ԰Ў⿟ᑣЁ䞡໡ⱘҷⷕ↉Փ⫼DŽ ᷜⱘय़ܹϢᔍߎ᪡԰↨䕗ㅔऩˈԚᰃˈབᵰᡞ䫭䇃Ẕ⌟Ϣᘶ໡᪡԰䛑ࡴ䖯ᴹˈ䆹⿟ᑣህ ѻӐ else Ӱѻωӕ࿦ႼՄԅᄔ else if (ಾܰ໻׸) གదӾႼᄯڴߜࠒ ᄓ໻ၮസ Ӱѻൎ༓೴ંԅЀᆴ೴ else if (ಾၮസ׸) ߜ؞೴གదӾႼᄯ if (ಾ೴) while (ຏྡྷّၮസ׸ݧЀᆴ೴Ϣಾำߑࠒ೯ᄗವ׸) ᪡԰˖ 䖭ḋˈ䆹⿟ᑣⱘ㒧ᵘህᵘ៤ϔϾᕾ⦃ˈ↣⃵ᕾ⦃ᇍϔϾ䖤ㅫヺঞⳌᑨⱘ᪡԰᭄ᠻ㸠ϔ⃵ ⱘؐᔍߎᑊᠧॄDŽ ᳔ৢˈҢᷜЁপߎᷜ乊ⱘ•1 ੠ 9ˈᑊᡞᅗӀⱘ⿃•9 य़ܹࠄᷜ乊DŽࠄ䖒䕧ܹ㸠ⱘ᳿ሒᯊˈᡞᷜ乊 ࠄᷜЁˈݡ⫼ϸ㗙ПᏂ•1 পҷᅗӀ˗✊ৢˈᇚ 4 ੠ 5 य़ܹࠄᷜЁˈݡ⫼ϸ㗙П੠ 9 পҷᅗӀDŽ ᡞ 1 ੠ 2 य़ܹܜ԰᭄ˈᑊᡞ䖤ㅫ㒧ᵰݡय़ܹࠄᷜЁDŽ՟བˈᇍϞ䴶ⱘ䗚⊶݄㸼䖒ᓣᴹ䇈ˈ佪 䖤ㅫヺᴹ䇈ᰃϸϾ᪡԰᭄˅ˈᡞ䆹䖤ㅫヺ԰⫼Ѣᔍߎⱘ᪡ܗᷜЁᔍߎⳌᑨ᭄Ⳃⱘ᪡԰᭄˄ᇍѠ 䅵ㅫ఼⿟ᑣⱘᅲ⦄ᕜㅔऩDŽ↣Ͼ᪡԰᭄䛑㹿ձ⃵य़ܹࠄᷜЁ˗ᔧϔϾ䖤ㅫヺࠄ䖒ᯊˈҢ 䗚⊶݄㸼⼎⊩Ёϡ䳔㽕೚ᣀোˈা㽕ⶹ䘧↣Ͼ䖤ㅫヺ䳔㽕޴Ͼ᪡԰᭄ህϡӮᓩ䍋℻НDŽ 1 2 • 4 5 + * 䞛⫼䗚⊶݄㸼⼎⊩㸼⼎Ў˖ (5 + 4) * (2 – 1) 㹿 getop 䇗⫼ⱘߑ᭄ ៥Ӏ೼ৢ䴶䚼ߚᇚ䅼䆎བԩᡞ䆹⿟ᑣߚࡆ៤ϸϾ៪໮Ͼ⑤᭛ӊDŽ main ߑ᭄ࣙᣀϔϾᕜ໻ⱘ switch ᕾ⦃ˈ䆹ᕾ⦃ḍ᥂䖤ㅫヺ៪᪡԰᭄ⱘ㉏ൟ᥻ࠊ⿟ᑣⱘ 䕀⿏DŽ䖭䞠ⱘ switch 䇁হⱘ⫼⊩↨ 3.4 㡖Ёⱘ՟ᄤ᳈Ў݌ൟDŽ #include #include /* for atof() */ #define MAXOP 100 /* max size of operand or operator */ #define NUMBER '0' /* signal that a number was found */ int getop(char []); void push(double); double pop(void); /* reverse Polish calculator */ main() { int type; double op2; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '•': op2 = pop(); push(pop() • op2); break; case '/': op2 = pop(); if (op2 != 0.0) push(pop() / op2); else printf("error: zero divisor\n"); break; case '\n': printf("\t%.8g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } return 0; } ಴Ў+Ϣ*ϸϾ䖤ㅫヺ⒵䎇Ѹᤶᕟˈ಴ℸˈ᪡԰᭄ⱘᔍߎ⃵ᑣ᮴݇㋻㽕DŽԚᰃˈ•Ϣ/ϸϾ Click to buy NOW! PDF•XC H ANGE w w w . d o c u • t r a c k.com w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! i = 0; return c; /* not a number */ if (!isdigit(c) && c != '.') s[1] = '\0'; ; while ((s[0] = c = getch()) == ' ' || c == '\t') int i, c; { int getop(char s[]) /* getop: get next character or numeric operand */ void ungetch(int); int getch(void); #include ᴹњDŽ ᡞ䖭ѯ᭄ᄫᄫヺІᬊ䲚䍋ᴹ˄݊Ёৃ㛑ࣙ৿ᇣ᭄⚍˅ˈᑊ䖨ಲ NUMBERˈҹᷛ䆚᭄Ꮖ㒣ᬊ䲚䍋 ↨䕗ᆍᯧDŽᅗ䳔㽕䏇䖛ぎḐϢࠊ㸼ヺDŽབᵰϟϔϾᄫヺϡᰃ᭄ᄫ៪ᇣ᭄⚍ˈ߭䖨ಲ˗৺߭ˈ ϟ䴶៥Ӏᴹⳟ getop ߑ᭄ⱘᅲ⦄DŽ䆹ߑ᭄㦋পϟϔϾ䖤ㅫヺ៪᪡԰᭄DŽ䆹ӏࡵᅲ⦄䍋ᴹ 䩜ˈ಴ℸˈᇍ main ߑ᭄㗠㿔㽕ᇚᅗӀ䱤㮣䍋ᴹDŽ ݅ѿⱘᷜ੠ᷜ乊ᣛ䩜ᅮН೼䖭ϸϾߑ᭄ⱘ໪䚼DŽԚᰃˈmain ߑ᭄ᴀ䑿ᑊ≵᳝ᓩ⫼ᷜ៪ᷜ乊ᣛ བᵰব䞣ᅮН೼ӏԩߑ᭄ⱘ໪䚼ˈ߭ᰃ໪䚼ব䞣DŽ಴ℸˈ៥Ӏᡞ push ੠ pop ߑ᭄ᖙ乏 } } return 0.0; printf("error: stack empty\n"); else { return val[••sp]; if (sp > 0) { double pop(void) /* pop: pop and return top value from stack */ } printf("error: stack full, can't push %g\n", f); else val[sp++] = f; if (sp < MAXVAL) { void push(double f) /* push: push f onto value stack */ double val[MAXVAL]; /* value stack */ int sp = 0; /* next free stack position */ #define MAXVAL 100 /* maximum depth of val stack */ ㄀ϔϾؐᔍߎࠄϔϾЈᯊব䞣ЁDŽ ˈ main ߑ᭄Ёϔḋᡞ ڣЁᑊ≵᳝ᅮНϸϾ pop 䇗⫼ⱘ∖ؐ⃵ᑣDŽЎњֱ䆕ℷ⹂ⱘ⃵ᑣˈᖙ乏 push(pop() • pop()); /* WRONG */ 䖤ㅫヺⱘᎺে᪡԰᭄ᖙ乏ࡴҹऎߚDŽ೼ߑ᭄䇗⫼ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! њᦤկϔ⾡᳈䗮⫼ⱘᮍ⊩ˈ៥Ӏ೼䖭䞠Փ⫼њϔϾ᭄㒘㗠ϡᰃϔϾᄫヺDŽ ᑧЁᦤկњ ungetcˈᅗᇚϔϾᄫヺय़ಲࠄᷜЁˈ៥Ӏᇚ೼㄀ 7 ゴЁ䅼䆎䆹ߑ᭄DŽЎޚᷛ } buf[bufp++] = c; else printf("ungetch: too many characters\n"); if (bufp >= BUFSIZE) { void ungetch(int c) /* push character back on input */ } return (bufp > 0) ? buf[••bufp] : getchar(); { int getch(void) /* get a (possibly pushed•back) character */ int bufp = 0; /* next free position in buf */ char buf[BUFSIZE]; /* buffer for ungetch */ #define BUFSIZE 100 ߑ᭄ঞ݊݅ѿব䞣˖ ᣕؐϡবˈ಴ℸᅗӀᖙ乏ᰃ䖭ϸϾߑ᭄ⱘ໪䚼ব䞣DŽৃҹᣝ✻ϟ߫ᮍᓣ㓪ݭ getchǃungetch ऎϢϟᷛব䞣ᰃկ getch Ϣ ungetch ߑ᭄݅ѿⱘˈϨ೼ϸ⃵䇗⫼П䯈ᖙ乏ֱކ⬅Ѣ㓧 ऎЁᔧࠡᄫヺⱘԡ㕂DŽކԣ㓧 ᯊˈgetch ߑ᭄䇗⫼ getchar ߑ᭄Ⳉ᥹Ң䕧ܹЁ䇏ᄫヺDŽ䖭䞠䖬䳔㽕๲ࡴϔϾϟᷛব䞣ᴹ䆄 ऎЎぎކऎЁ䇏পᄫヺ˗ᔧ㓧ކऎϡぎᯊˈgetch ߑ᭄ህҢ㓧ކऎ˄ᄫヺ᭄㒘˅Ёˈᔧ䆹㓧 ކ䖭ϸϾߑ᭄П䯈ⱘणৠᎹ԰гᕜㅔऩDŽungetch ߑ᭄ᡞ㽕य़ಲⱘᄫヺᬒࠄϔϾ݅ѿ㓧 䖨ಲ ungetch ߑ᭄ᬒಲⱘ䙷ϾᄫヺDŽܜⱘ䕧ܹПࠡ 㗠 ungetch ߑ᭄߭⫼Ѣᡞᄫヺᬒಲࠄ䕧ܹЁˈ䖭ḋˈℸৢ೼䇗⫼ getch ߑ᭄ᯊˈ೼䇏ܹᮄ ѦⳌण԰ⱘߑ᭄ᴹ↨䕗ᮍ֓ഄ῵ᢳডপᄫヺ᪡԰DŽgetch ߑ᭄⫼Ѣ䇏ܹϟϔϾᕙ໘⧚ⱘᄫヺˈ ᳝䇏ܹ䆹ᄫヺϔḋDŽ៥Ӏৃҹ㓪ݭϔᇍ≴ڣህᡞᅗय़ಲࠄ䕧ܹЁˈᇍҷⷕ݊ԭ䚼ߚ㗠㿔ህད DŽ↣ᔧ⿟ᑣ໮䇏ܹϔϾᄫヺᯊˈއབᵰ㛑Āড䇏āϡ䳔㽕ⱘᄫヺˈ䆹䯂乬ህৃҹᕫࠄ㾷 ⱘ᭄DŽ 㛑⹂ᅮⱘDŽ⬅Ѣ⿟ᑣ㽕䍙ࠡ䇏ܹϔϾᄫヺˈ䖭ḋህᇐ㟈᳔ৢ᳝ϔϾᄫヺϡሲѢᔧࠡ᠔㽕䇏ܹ ᰃϔ՟˖೼ⳟࠄ㄀ϔϾ䴲᭄ᄫᄫヺПࠡˈᏆ㒣䇏ܹⱘ᭄ⱘᅠᭈᗻᰃϡ֓މড়៤ϔϾ᭄ᄫⱘᚙ ⿟ᑣϡ㛑⹂ᅮᅗᏆ㒣䇏ܹⱘ䕧ܹᰃ৺䎇໳ˈ䰸䴲䍙ࠡ໮䇏ܹϔѯ䕧ܹDŽ䇏ܹϔѯᄫヺҹ˖މ 䖭↉⿟ᑣЁⱘ getch Ϣ ungetch ϸϾߑ᭄᳝ҔМ⫼䗨ਸ਼˛⿟ᑣЁ㒣ᐌӮߎ⦄䖭ḋⱘᚙ } return NUMBER; ungetch(c); if (c != EOF) s[i] = '\0'; ; while (isdigit(s[++i] = c = getch())) if (c == '.') /* collect fraction part */ ; while (isdigit(s[++i] = c = getch())) if (isdigit(c)) /* collect integer part */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! void push(double f) { ... } double val[MAXVAL]; int sp = 0; main() { ... } ব䞣ˈབϟ᠔⼎˖ ᴳDŽ՟བˈབᵰ mainǃspǃvalǃpush Ϣ pop ᰃձ⃵ᅮН೼ᶤϾ᭛ӊЁⱘ 5 Ͼߑ᭄៪໪䚼 ໪䚼ব䞣៪ߑ᭄ⱘ԰⫼ඳҢໄᯢᅗⱘഄᮍᓔྟˈࠄ݊᠔೼ⱘ˄ᕙ㓪䆥ⱘ˅᭛ӊⱘ᳿ሒ㒧 ≵᳝ӏԩ݇㋏DŽߑ᭄ⱘখ᭄гᰃ䖭ḋⱘˈᅲ䰙Ϟৃҹᇚᅗⳟ԰ᰃሔ䚼ব䞣DŽ 䇈ˈ݊԰⫼ඳᰃໄᯢ䆹ব䞣ৡⱘߑ᭄DŽϡৠߑ᭄Ёໄᯢⱘ݋᳝Ⳍৠৡᄫⱘ৘Ͼሔ䚼ব䞣П䯈 ৡᄫⱘᆴဈ့ᣛⱘᰃ⿟ᑣЁৃҹՓ⫼䆹ৡᄫⱘ䚼ߚDŽᇍѢ೼ߑ᭄ᓔ༈ໄᯢⱘ㞾ࡼব䞣ᴹ ᑣЁ䘛ࠄⱘ㉏Ԑ䯂乬DŽ ᑺᴹⳟˈ䅵ㅫ఼⿟ᑣ↨䕗ᇣˈϡؐᕫߚ៤޴Ͼ᭛ӊᄬᬒˈԚ䗮䖛ᅗৃҹᕜདഄ䇈ᯢ䕗໻ⱘ⿟ Ўњ䅼䆎䖭ѯ䯂乬ˈ៥Ӏ䞡ᮄ㒘㒛ࠡ䴶ⱘ䅵ㅫ఼⿟ᑣˈᇚᅗߚᬷࠄ໮Ͼ᭛ӊЁDŽҢᅲ䏉ⱘ㾦 · བԩ߱ྟ࣪໪䚼ব䞣˛ · བԩ㒘㒛⿟ᑣЁⱘໄᯢᠡ㛑⹂ֱা᳝ϔӑࡃᴀ˛ · བԩᅝᥦໄᯢⱘԡ㕂ᠡ㛑⹂ֱ⿟ᑣ೼ࡴ䕑ᯊ৘䚼ߚ㛑ℷ⹂䖲᥹˛ · བԩ䖯㸠ໄᯢᠡ㛑⹂ֱব䞣೼㓪䆥ᯊ㹿ℷ⹂ໄᯢ˛ Ꮖ㓪䆥䖛ⱘߑ᭄ৃҹҢᑧЁ䖯㸠ࡴ䕑DŽ䖭䞠៥Ӏᛳ݈䍷ⱘ䯂乬᳝˖ܜॳ ᵘ៤ C 䇁㿔⿟ᑣⱘߑ᭄Ϣ໪䚼ব䞣ৃҹߚᓔ䖯㸠㓪䆥DŽϔϾ⿟ᑣৃҹᄬᬒ೼޴Ͼ᭛ӊЁˈ 4.4. ԰⫼ඳ㾘߭ getch Ϣ ungetch ߑ᭄DŽ䇋䖤⫼䖭ϔᮍ⊩ׂᬍ䅵ㅫ఼⿟ᑣDŽ ϟৃҹϡՓ⫼މ㒗д 4•10 ঺ϔ⾡ᮍ⊩ᰃ䗮䖛 getline ߑ᭄䇏ܹᭈϾ䕧ܹ㸠ˈ䖭⾡ᚙ EOF ᯊᑨ䆹བԩ໘⧚˛䇋ᅲ⦄Դⱘ䆒䅵ᮍḜDŽ 㒗д 4•9 ҹϞҟ㒡ⱘ getch Ϣ ungetch ߑ᭄ϡ㛑ℷ⹂ഄ໘⧚य़ಲⱘ EOFDŽ㗗㰥य़ಲ ᅮ᳔໮াय़ಲϔϾᄫヺDŽ䇋Ⳍᑨഄׂᬍ getch Ϣ ungetch 䖭ϸϾߑ᭄DŽ؛ 㒗д 4•8 䳔㽕Փ⫼ buf ੠ bufp ৫˛ᅗ㛑৺ҙՓ⫼ ungetch ߑ᭄˛ 㒗д 4•7 㓪ݭϔϾߑ᭄ ungets(s)ˈᇚᭈϾᄫヺІ s य़ಲࠄ䕧ܹЁDŽungets ߑ᭄ ব䞣ᕜᆍᯧ˅DŽ๲ࡴϔϾব䞣ᄬᬒ᳔䖥ᠧॄⱘؐDŽ 㒗д 4•6 㒭䅵ㅫ఼⿟ᑣ๲ࡴ໘⧚ব䞣ⱘੑҸ˄ᦤկ 26 Ͼ݋᳝ऩϾ㣅᭛ᄫ↡ব䞣ৡⱘ ᭄ⱘ䆺㒚ֵᙃˈখ㾕䰘ᔩ B.4 㡖Ёⱘ༈᭛ӊDŽ 㒗д 4•5 㒭䅵ㅫ఼⿟ᑣ๲ࡴ䆓䯂 sinǃexp Ϣ pow ㄝᑧߑ᭄ⱘ᪡԰DŽ᳝݇䖭ѯᑧߑ ㋴ⱘؐDŽ঺໪๲ࡴϔϾੑҸ⫼Ѣ⏙ぎᷜDŽܗ㋴˗Ѹᤶᷜ乊ϸϾܗ໡ࠊᷜ乊 ㋴˗ܗϟᠧॄᷜ乊މ㋴ⱘᚙܗ㒗д 4•4 ೼ᷜ᪡԰Ё⏏ࡴ޴ϾੑҸˈߚ߿⫼Ѣ೼ϡᔍߎ DŽމপ῵˄%˅䖤ㅫヺˈᑊ⊼ᛣ㗗㰥䋳᭄ⱘᚙ ህ↨䕗ㅔऩњDŽ೼䆹⿟ᑣЁࡴܹܙ㒗д 4•3 ೼᳝њ෎ᴀḚᶊৢˈᇍ䅵ㅫ఼⿟ᑣ䖯㸠ᠽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! Փ⫼ǃৢᅮНব䞣 sp Ϣ valˈг䳔㽕ᣝ✻䖭⾡ᮍᓣᴹ㒘㒛᭛ӊDŽ ܜӀ䗖⫼Ѣ䆹᭛ӊЁⱘ᠔᳝ߑ᭄DŽᇍѢ file1ˈ䖭ḋϔ㒘ໄᯢህ໳њDŽབᵰ㽕೼ৠϔϾ᭛ӊЁ ⬅Ѣ᭛ӊ file1 Ёⱘ extern ໄᯢϡҙᬒ೼ߑ᭄ᅮНⱘ໪䴶ˈ㗠Ϩ䖬ᬒ೼ᅗӀⱘࠡ䴶ˈ಴ℸᅗ double val[MAXVAL]; int sp = 0; ೼᭛ӊ file2 Ё˖ double pop(void) { ... } void push(double f) { ... } extern double val[]; extern int sp; ೼᭛ӊ file1 Ё˖ ᅮā೼ϔ䍋˖ ߱ྟ࣪˄䗮ᐌϡ໻ৃ㛑䖭ḋ㒘㒛⿟ᑣ˅ˈ߭䳔㽕䗮䖛ϟ䴶䖭ѯᅮНϢໄᯢᡞ䖭ѯߑ᭄੠ব䞣Ā㒥 ᅮߑ᭄ push Ϣ pop ᅮН೼ϔϾ᭛ӊЁˈ㗠ব䞣 val Ϣ sp ೼঺ϔϾ᭛ӊЁᅮНᑊ㹿؛ ໪䚼ব䞣ⱘ߱ྟ࣪া㛑ߎ⦄೼݊ᅮНЁDŽ 㒘ⱘ䭓ᑺDŽ extern ໄᯢ˅DŽ໪䚼ব䞣ⱘᅮНЁᖙ乏ᣛᅮ᭄㒘ⱘ䭓ᑺˈԚ extern ໄᯢ߭ϡϔᅮ㽕ᣛᅮ᭄ ৃҹ䗮䖛 extern ໄᯢᴹ䆓䯂ᅗ˄ᅮН໪䚼ব䞣ⱘ⑤᭛ӊЁгৃҹࣙ৿ᇍ䆹໪䚼ব䞣ⱘ ೼ϔϾ⑤⿟ᑣⱘ᠔᳝⑤᭛ӊЁˈϔϾ໪䚼ব䞣া㛑೼ᶤϾ᭛ӊЁᅮНϔ⃵ˈ㗠݊ᅗ᭛ӊ DŽܗऩ ټব䞣 val˄䆹᭄㒘ⱘ䭓ᑺ೼݊ᅗഄᮍ⹂ᅮ˅ˈԚ䖭ϸϾໄᯢᑊ≵᳝ᓎゟব䞣៪ЎᅗӀߚ䜡ᄬ Ў⑤᭛ӊⱘ݊ԭ䚼ߚໄᯢњϔϾ int ㉏ൟⱘ໪䚼ব䞣 sp ҹঞϔϾ double ᭄㒘㉏ൟⱘ໪䚼 extern double val[]; extern int sp; Ў䆹⑤᭛ӊЁ݊ԭ䚼ߚⱘໄᯢDŽ㗠ϟ䴶ⱘϸ㸠䇁হ˖ ৠᯊ䖭ϸᴵ䇁হ䖬ৃҹ԰ˈܗऩټ䙷М䖭ϸᴵ䇁হᇚᅮН໪䚼ব䞣 sp Ϣ valˈᑊЎПߚ䜡ᄬ double val[MAXVAL]; int sp; ໪䚼˖ ఼ⱘߚ䜡DŽབᵰᇚϟ߫䇁হᬒ೼᠔᳝ߑ᭄ⱘټব䞣ⱘ㉏ൟ˅ˈ㗠ব䞣ᅮН䰸ℸҹ໪䖬ᇚᓩ䍋ᄬ ᇚ໪䚼ব䞣ⱘໄᯢϢᅮНϹḐऎߚᓔᴹᕜ䞡㽕DŽব䞣ໄᯢ⫼Ѣ䇈ᯢব䞣ⱘሲᗻ˄Џ㽕ᰃ ⫼ϡ೼ৠϔϾ⑤᭛ӊЁˈ߭ᖙ乏೼Ⳍᑨⱘব䞣ໄᯢЁᔎࠊᗻഄՓ⫼݇䬂ᄫ externDŽ ঺ϔᮍ䴶ˈབᵰ㽕೼໪䚼ব䞣ⱘᅮНПࠡՓ⫼䆹ব䞣ˈ៪㗙໪䚼ব䞣ⱘᅮНϢব䞣ⱘՓ Ԛᰃˈ䖭ϸϾব䞣ৡϡ㛑⫼೼ main ߑ᭄Ёˈpush Ϣ pop ߑ᭄гϡ㛑⫼೼ main ߑ᭄ЁDŽ 䙷Мˈ೼ push Ϣ pop 䖭ϸϾߑ᭄Ёϡ䳔䖯㸠ӏԩໄᯢህৃҹ䗮䖛ৡᄫ䆓䯂ব䞣 sp Ϣ valˈ double pop(void) { ... } w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! Փ⫼᳈໮ⱘ༈᭛ӊˈ៥Ӏ䳔㽕㊒ᖗഄ㒘㒛ᅗӀDŽ ᶤѯЁㄝ㾘῵ⱘ⿟ᑣˈ᳔དা⫼ϔϾ༈᭛ӊᄬᬒ⿟ᑣЁ৘䚼ߚ݅ѿⱘᇍ䈵DŽ䕗໻ⱘ⿟ᑣ䳔㽕 ⱘֵᙃ˗঺ϔᮍ䴶ᰃ⦄ᅲЁ㓈ᡸ䕗໮ⱘ༈᭛ӊ↨䕗ೄ䲒DŽ៥Ӏৃҹᕫߎ䖭ḋϔϾ㒧䆎˖ᇍѢ ៥Ӏᇍϟ䴶ϸϾ಴㋴䖯㸠њᡬ㹋˖ϔᮍ䴶ᰃ៥Ӏᳳᳯ↣Ͼ᭛ӊা㛑䆓䯂ᅗᅠ៤ӏࡵ᠔䳔 ˄#include ᣛҸᇚ೼ 4.11 㡖Ёҟ㒡˅DŽ䖭ḋߚࡆৢˈ⿟ᑣⱘᔶᓣབϟ᠔⼎˖ 䚼ߚᬒ೼༈᭛ӊ calc.h Ёˈ೼䳔㽕Փ⫼䆹༈᭛ӊᯊ䗮䖛#include ᣛҸᇚᅗࣙ৿䖯ᴹ Ё೼ϔ䍋ˈ䖭ḋህা䳔㽕ϔϾࡃᴀˈᬍ䖯⿟ᑣᯊгᆍᯧֱ䆕⿟ᑣⱘℷ⹂ᗻDŽ៥Ӏᡞ䖭ѯ݀݅ ℸ໪ˈ䖬ᖙ乏㗗㰥ᅮН੠ໄᯢ೼䖭ѯ᭛ӊП䯈ⱘ݅ѿ䯂乬DŽ៥Ӏሑৃ㛑ᡞ݅ѿⱘ䚼ߚ䲚 П᠔ҹߚࡆ៤໮Ͼ᭛ӊˈЏ㽕ᰃ㗗㰥೼ᅲ䰙ⱘ⿟ᑣЁˈᅗӀߚ߿ᴹ㞾Ѣऩ⣀㓪䆥ⱘᑧDŽ ߑ᭄ᬒ೼㄀ϝϾ᭛ӊ getop.c Ё ˗ᇚ getch Ϣ ungetch ߑ᭄ᬒ೼㄀ಯϾ᭛ӊ getch.c ЁDŽ Ё˗ᇚ push Ϣ pop ߑ᭄ҹঞᅗӀՓ⫼ⱘ໪䚼ব䞣ᬒ೼㄀ѠϾ᭛ӊ stack.c Ё˗ᇚ getop 䖬ᰃ᳝ᖙ㽕ⱘDŽ៥Ӏ䖭ḋߚࡆ˖ᇚЏߑ᭄ main ऩ⣀ᬒ೼᭛ӊ main.cخ៤䚼ߚᕜ䭓ˈ䖭М DŽབᵰ䆹⿟ᑣⱘ৘㒘މϟ䴶៥Ӏᴹ㗗㰥ᡞϞ䗄ⱘ䅵ㅫ఼⿟ᑣߚࡆࠄ㢹ᑆϾ⑤᭛ӊЁⱘᚙ ༈᭛ӊ .4.5 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! f(register unsigned m, register long n) ⱘ՟ᄤ˖މregister ໄᯢা䗖⫼Ѣ㞾ࡼব䞣ҹঞߑ᭄ⱘᔶᓣখ᭄DŽϟ䴶ᰃৢϔ⾡ᚙ register char c; register int x; register ໄᯢⱘᔶᓣབϟ᠔⼎˖ ᗑ⬹ℸ䗝乍DŽ register ব䞣ᬒ೼ᴎ఼ⱘᆘᄬ఼Ёˈ䖭ḋৃҹՓ⿟ᑣ᳈ᇣǃᠻ㸠䗳ᑺ᳈ᖿDŽԚ㓪䆥఼ৃҹ register ໄᯢਞ䆝㓪䆥఼ˈᅗ᠔ໄᯢⱘব䞣೼⿟ᑣЁՓ⫼乥⥛䕗催DŽ݊ᗱᛇᰃˈᇚ 4.7. ᆘᄬ఼ব䞣 䆹䯂乬DŽއstatic ㉏ൟⱘݙ䚼ব䞣㾷 㒗д 4•11 ׂᬍ getop ߑ᭄ˈՓ݊ϡᖙՓ⫼ ungetch ߑ᭄DŽᦤ⼎˖ৃҹՓ⫼ϔϾ 䞣DŽ ぎ䯈ⱘবټহ䆱䇈ˈstatic ㉏ൟⱘݙ䚼ব䞣ᰃϔ⾡া㛑೼ᶤϾ⡍ᅮߑ᭄ЁՓ⫼ԚϔⳈऴ᥂ᄬ 㞾ࡼব䞣䙷ḋˈ䱣ⴔ᠔೼ߑ᭄ⱘ㹿䇗⫼੠䗔ߎ㗠ᄬ೼੠⍜༅DŽᤶڣ䇗⫼ˈᅗϔⳈᄬ೼ˈ㗠ϡ ߑ᭄ⱘሔ䚼ব䞣ˈা㛑೼䆹ߑ᭄ЁՓ⫼ˈԚᅗϢ㞾ࡼব䞣ϡৠⱘᰃˈϡㅵ݊᠔೼ߑ᭄ᰃ৺㹿 static гৃ⫼Ѣໄᯢݙ䚼ব䞣DŽstatic ㉏ൟⱘݙ䚼ব䞣ৠ㞾ࡼব䞣ϔḋˈᰃᶤϾ⡍ᅮ ㉏ൟˈ߭䆹ߑ᭄ৡ䰸њᇍ䆹ߑ᭄ໄᯢ᠔೼ⱘ᭛ӊৃ㾕໪ˈ݊ᅗ᭛ӊ䛑᮴⊩䆓䯂DŽ ৡᄫᰃܼሔৃ䆓䯂ⱘˈᇍᭈϾ⿟ᑣⱘ৘Ͼ䚼ߚ㗠㿔䛑ৃ㾕DŽԚᰃˈབᵰᡞߑ᭄ໄᯢЎ static ϟˈߑ᭄މ໪䚼ⱘ static ໄᯢ䗮ᐌ໮⫼Ѣব䞣ˈᔧ✊ˈᅗгৃ⫼Ѣໄᯢߑ᭄DŽ䗮ᐌᚙ ᷜ᪡԰ⱘ push Ϣ pop ߑ᭄Փ⫼ⱘব䞣DŽ さDŽৠḋˈৃҹ䗮䖛ᡞব䞣 sp Ϣ val ໄᯢЎ䴭ᗕ㉏ൟ䱤㮣䖭ϸϾ⬅ᠻ㸠ކⱘⳌৠⱘৡᄫⳌ 䙷М݊ᅗߑ᭄ህϡ㛑䆓䯂ব䞣 buf Ϣ bufpˈ಴ℸ䖭ϸϾৡᄫϡӮ੠ৠϔ⿟ᑣЁⱘ݊ᅗ᭛ӊЁ void ungetch(int c) { ... } int getch(void) { ... } static int bufp = 0; /* next free position in buf */ static char buf[BUFSIZE]; /* buffer for ungetch */ བᵰᡞϞ䗄ϸϾߑ᭄੠ϸϾব䞣ᬒ೼ϔϾ᭛ӊЁ㓪䆥ˈབϟ᠔⼎˖ ҹ೼ℷᐌⱘᇍ䈵ໄᯢПࠡࡴϞ݇䬂ᄫ static ԰Ўࠡ㓔DŽৃˈټ㽕ᇚᇍ䈵ᣛᅮЎ䴭ᗕᄬ ᑨ䆹㹿 getch Ϣ ungetch ߑ᭄ⱘ䇗⫼㗙᠔䆓䯂DŽ 㒧ᵘ䳔㽕݅ѿ buf Ϣ bufp ϸϾব䞣ˈ䖭ḋ buf Ϣ bufp ᖙ乏ᰃ໪䚼ব䞣ˈԚ䖭ϸϾᇍ䈵ϡ 䗮䖛 static 䰤ᅮ໪䚼ᇍ䈵ˈৃҹ䖒ࠄ䱤㮣໪䚼ᇍ䈵ⱘⳂⱘˈ↨བˈgetch•ungetch ໡ড় ໄᯢ䰤ᅮ໪䚼ব䞣Ϣߑ᭄ˈৃҹᇚ݊ৢໄᯢⱘᇍ䈵ⱘ԰⫼ඳ䰤ᅮЎ㹿㓪䆥⑤᭛ӊⱘ࠽ԭ䚼ߚDŽ 䞣 buf Ϣ bufpˈᅗӀҙկ݊᠔೼ⱘ⑤᭛ӊЁⱘߑ᭄Փ⫼ˈ݊ᅗߑ᭄ϡ㛑䆓䯂DŽ⫼ static ᶤѯব䞣ˈ↨བ᭛ӊ stack.c ЁᅮНⱘব䞣 sp Ϣ val ҹঞ᭛ӊ getch.c ЁᅮНⱘব 䴭ᗕব䞣 .4.6 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ࠡ䴶៥Ӏ໮⃵ᦤࠄ䖛߱ྟ࣪ⱘὖᗉˈϡ䖛ྟ㒜≵᳝䆺㒚䅼䆎DŽᴀ㡖ᇚᇍࠡ䴶䅼䆎ⱘ৘⾡ 4.9. ߱ྟ࣪ ৺߭ˈᕜৃ㛑ᓩ䍋⏋х੠䫭䇃DŽ ˈމߎ⦄ব䞣ৡ䱤㮣໪䚼԰⫼ඳЁⳌৠৡᄫⱘᚙܡ೼ϔϾདⱘ⿟ᑣ䆒䅵亢ḐЁˈᑨ䆹䙓 ໪䚼ব䞣DŽ䖭↉ҷⷕЁⱘব䞣 y гᰃབℸDŽ ߑ᭄ f ݙⱘব䞣 x ᓩ⫼ⱘᰃߑ᭄ⱘখ᭄ˈ㉏ൟЎ double˗䴶೼ߑ᭄ f ໪ˈx ᰃ int ㉏ൟⱘ } double y; { f(double x) int y; int x; 㞾ࡼব䞣˄ࣙᣀᔶᓣখ᭄˅гৃҹ䱤㮣ৠৡⱘ໪䚼ব䞣Ϣߑ᭄DŽ೼ϟ䴶ⱘໄᯢЁ˖ ᑣഫᯊ㹿߱ྟ࣪ϔ⃵DŽ ᑣഫᯊˈ೼⿟ᑣഫݙໄᯢҹঞ߱ྟ࣪ⱘ㞾ࡼব䞣䛑ᇚ㹿߱ྟ࣪DŽ䴭ᗕব䞣া೼㄀ϔ⃵䖯ܹ⿟ ব䞣 i ⱘ԰⫼ඳᰃ if 䇁হⱘĀⳳāߚᬃˈ䖭Ͼ i Ϣ䆹⿟ᑣഫ໪ໄᯢⱘ i ᮴݇DŽ↣⃵䖯ܹ⿟ } ... for (i = 0; i < n; i++) int i; /* declare a new i */ if (n > 0) { ᣀোऍ䜡ⱘে㢅ᣀোߎ⦄ПࠡϔⳈᄬ೼DŽ՟བˈ೼ϟ䴶ⱘ⿟ᑣ↉Ё˖ ⾡ᮍᓣໄᯢⱘব䞣ৃҹ䱤㮣⿟ᑣഫ໪ϢПৠৡⱘব䞣ˈᅗӀП䯈≵᳝ӏԩ݇㋏ˈᑊ೼ϢᎺ㢅 ೼ߑ᭄ᓔྟⱘ㢅ᣀোПৢˈ䖬ৃҹ㋻䎳೼ӏԩ݊ᅗᷛ䆚໡ড়䇁হᓔྟⱘᎺ㢅ᣀোПৢDŽҹ䖭 ᰃˈ೼ߑ᭄Ёৃҹҹ⿟ᑣഫ㒧ᵘⱘᔶᓣᅮНব䞣DŽব䞣ⱘໄᯢ˄ࣙᣀ߱ྟ࣪˅䰸њৃҹ㋻䎳 䆌೼ߑ᭄ЁᅮНߑ᭄DŽԚܕC 䇁㿔ᑊϡᰃ Pascal ㄝ䇁㿔ᛣНϞⱘ⿟ᑣഫ㒧ᵘⱘ䇁㿔ˈᅗϡ 4.8. ⿟ᑣഫ㒧ᵘ ᰃϡৠⱘDŽ ֵᙃˈ៥Ӏᇚ೼㄀ 5 ゴЁ䅼䆎˅DŽ೼ϡৠⱘᴎ఼Ёˈᇍᆘᄬ఼ব䞣ⱘ᭄Ⳃ੠㉏ൟⱘ݋ԧ䰤ࠊг ᆘᄬ఼ব䞣ᅲ䰙Ϟᰃϡᰃᄬᬒ೼ᆘᄬ఼Ёˈᅗⱘഄഔ䛑ᰃϡ㛑䆓䯂ⱘ˄᳝݇䖭ϔ⚍᳈䆺㒚ⱘ ᑊ≵᳝ҔМᆇ໘ˈ䖭ᰃ಴Ў㓪䆥఼ৃҹᗑ⬹䖛䞣ⱘ៪ϡᬃᣕⱘᆘᄬ఼ব䞣ໄᯢDŽ঺໪ˈ᮴䆎 䆌ᶤѯ㉏ൟⱘব䞣DŽԚᰃˈ䖛䞣ⱘᆘᄬ఼ໄᯢܕা᳝ᕜᇥⱘব䞣ৃҹֱᄬ೼ᆘᄬ఼ЁˈϨা ᇍᆘᄬ఼ব䞣ⱘՓ⫼Ӯ᳝ϔѯ䰤ࠊDŽ↣Ͼߑ᭄Ёމᅲ䰙Փ⫼ᯊˈᑩሖ⹀ӊ⦃๗ⱘᅲ䰙ᚙ } ... register int i; } w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ߱ྟ࣪㸼䖒ᓣᑣ߫DŽ՟བ˖ ᄫヺ᭄㒘ⱘ߱ྟ࣪↨䕗⡍⅞˖ৃҹ⫼ϔϾᄫヺІᴹҷ᳓⫼㢅ᣀোᣀ䍋ᴹᑊ⫼䗫োߚ䱨ⱘ ㋴DŽܗ᥹߱ྟ࣪ৢ䴶ⱘ᭄㒘 ㋴㗠Ⳉܗ㋴ˈгϡ㛑䏇䖛ࠡ䴶ⱘ᭄㒘ܗ䇃ⱘDŽϡ㛑ϔ⃵ᇚϔϾ߱ྟ࣪㸼䖒ᓣᣛᅮ㒭໮Ͼ᭄㒘 ㋴᭄໮ˈ߭ᰃ䫭ܗ㋴ᇚ㹿߱ྟ࣪Ў 0ˈབᵰ߱ྟ࣪㸼䖒ᓣⱘϾ᭄↨᭄㒘ܗ≵᳝߱ྟ࣪㸼䖒ᓣⱘ ㋶᭄ᇥˈ߭ᇍ໪䚼ব䞣ǃ䴭ᗕব䞣੠㞾ࡼব䞣ᴹ䇈ˈܗབᵰ߱ྟ࣪㸼䖒ᓣⱘϾ᭄↨᭄㒘 ᭄㒘ⱘ䭓ᑺЎ 12DŽ ᔧⳕ⬹᭄㒘ⱘ䭓ᑺᯊˈ㓪䆥఼ᇚᡞ㢅ᣀোЁ߱ྟ࣪㸼䖒ᓣⱘϾ᭄԰Ў᭄㒘ⱘ䭓ᑺˈ೼ᴀ՟Ё int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 㒘 daysˈ݊ব䞣ⱘᅮНབϟ˖ োᣀ䍋ᴹˈ৘߱ྟ࣪㸼䖒ᓣП䯈䗮䖛䗫োߚ䱨DŽ՟བˈབᵰ㽕⫼ϔᑈЁ৘᳜ⱘ໽᭄߱ྟ᭄࣪ ᭄㒘ⱘ߱ྟ࣪ৃҹ೼ໄᯢⱘৢ䴶㋻䎳ϔϾ߱ྟ࣪㸼䖒ᓣ߫㸼ˈ߱ྟ࣪㸼䖒ᓣ߫㸼⫼㢅ᣀ ᰒᓣⱘ䌟ؐ䇁হDŽ ᛃDŽ㗗㰥ࠄব䞣ໄᯢЁⱘ߱ྟ࣪㸼䖒ᓣᆍᯧ㹿Ҏᗑ⬹ˈϨ䎱Փ⫼ⱘԡ㕂䕗䖰ˈ៥Ӏϔ㠀Փ⫼ ᅲ䰙Ϟˈ㞾ࡼব䞣ⱘ߱ྟ࣪ㄝᬜѢㅔݭⱘ䌟ؐ䇁হDŽおコ䞛⫼ાϔ⾡ᔶᓣˈ䖬ᕫⳟϾҎⱘд high = n • 1; low = 0; int low, high, mid; ҷ᳓ॳᴹⱘᔶᓣ˖ } ... int mid; int high = n • 1; int low = 0; { int binsearch(int x, int v[], int n) ߱ྟ࣪ৃҹ䞛⫼ϟ߫ᔶᓣ˖ ৿ӏᛣ೼ℸ㸼䖒ᓣПࠡᏆ㒣ᅮНⱘؐˈࣙᣀߑ᭄䇗⫼ˈ៥Ӏ೼ 3.3 㡖Ёҟ㒡ⱘᡬञᶹᡒ⿟ᑣⱘ ᇍѢ㞾ࡼব䞣Ϣᆘᄬ఼ব䞣ᴹ䇈ˈ߱ྟ࣪㸼䖒ᓣৃҹϡᰃᐌ䞣㸼䖒ᓣ˖㸼䖒ᓣЁৃҹࣙ ᑣഫᯊ䛑ᇚ㹿߱ྟ࣪DŽ Ϟ䆆ᰃ೼⿟ᑣᓔྟᠻ㸠ࠡ䖯㸠߱ྟ࣪˅DŽᇍѢ㞾ࡼব䞣Ϣᆘᄬ఼ব䞣ˈ߭೼↣⃵䖯ܹߑ᭄៪⿟ ᇍѢ໪䚼ব䞣Ϣ䴭ᗕব䞣ᴹ䇈ˈ߱ྟ࣪㸼䖒ᓣᖙ乏ᰃᐌ䞣㸼䖒ᓣˈϨা߱ྟ࣪ϔ⃵˄Ңὖᗉ long day = 1000L * 60L * 60L * 24L; /* milliseconds/day */ char squota = '\''; int x = 1; ᅮНᷛ䞣ব䞣ᯊˈৃҹ೼ব䞣ৡৢ㋻䎳ϔϾㄝো੠ϔϾ㸼䖒ᓣᴹ߱ྟ࣪ব䞣˖ ᄬ఼ব䞣ⱘ߱ؐ߭≵᳝ᅮН˄े߱ؐЎ᮴⫼ⱘֵᙃ˅DŽ ϟˈ໪䚼ব䞣੠䴭ᗕব䞣䛑ᇚ㹿߱ྟ࣪Ў 0ˈ㗠㞾ࡼব䞣੠ᆘމ೼ϡ䖯㸠ᰒᓣ߱ྟ࣪ⱘᚙ ϔϾᘏ㒧DŽخ㉏ⱘ߱ྟ࣪㾘߭ټᄬ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! void swap(int v[], int i, int j); int i, last; { void qsort(int v[], int left, int right) /* qsort: sort v[left]...v[right] into increasing order */ ㋴DŽܗ೼↣⃵ߦߚᄤ䲚ᯊˈ䆹ㅫ⊩ᘏᰃ䗝প৘Ͼᄤ᭄㒘ⱘЁ䯈 Ңᠻ㸠䗳ᑺᴹ䆆ˈϟ߫⠜ᴀⱘᖿ䗳ᥦᑣߑ᭄ৃ㛑ϡᰃ᳔ᖿⱘˈԚᅗᰃ᳔ㅔऩⱘㅫ⊩ПϔDŽ ᑣˈ㒜ℶ䗦ᔦDŽ ㋴᭄ᇣѢ 2 ᯊˈ䖭Ͼᄤ䲚ህϡ䳔㽕ݡ⃵ᥦܗḋϸϾᄤ䲚䗦ᔦᠻ㸠䖭ϔ䖛⿟ˈᔧᶤϾᄤ䲚Ёⱘ ㋴DŽᇍ䖭ܗ㋴䛑໻Ѣ៪ㄝѢ䆹ܗ㋶ˈ঺ϔϾᄤ䲚Ёⱘ᠔᳝ܗ㋴䛑ᇣѢ䆹ܗϔϾᄤ䲚Ёⱘ᠔᳝ ㋴ߦߚЎϸϾᄤ䲚ˈܗ㋴Ў⬠ᇚ݊ԭܗ㋴ˈҹ䆹ܗᯢⱘDŽᇍѢϔϾ㒭ᅮⱘ᭄㒘ˈҢЁ䗝ᢽϔϾ ঺໪ϔϾ㛑䕗ད䇈ᯢ䗦ᔦⱘ՟ᄤᰃᖿ䗳ᥦᑣDŽᖿ䗳ᥦᑣㅫ⊩ᰃ C. A. R. Hoare Ѣ 1962 ᑈথ 2ˈ✊ৢݡ䖨ಲࠄ㄀ϔ⃵䇗⫼DŽ䖨ಲࠄ㄀ϔ⃵䇗⫼ᯊᇚᠧ 3ˈ䱣П㒧ᴳߑ᭄ⱘᠻ㸠DŽ ᠧॄܜᇚᠧॄ 1ˈ✊ৢݡ䖨ಲࠄ㄀Ѡ⃵䇗⫼DŽҢ㄀ϝ⃵䇗⫼䖨ಲৢⱘ㄀Ѡ⃵䇗⫼ৠḋгᇚܜ佪 printd ⱘ㄀Ѡ⃵䇗⫼ˈৢ㗙জᡞ 1 Ӵ䗦㒧 printd ⱘ㄀ϝ⃵䇗⫼DŽ㄀ϝ⃵䇗⫼ printd ᯊ 䞣䲚ড়DŽ಴ℸˈ䇗⫼ printd(123)ᯊˈ㄀ϔ⃵䇗⫼ printd ⱘখ᭄ n=123DŽᅗᡞ 12 Ӵ䗦㒭 ߑ᭄䗦ᔦ䇗⫼㞾䑿ᯊˈ↣⃵䇗⫼䛑ӮᕫࠄϔϾϢҹࠡⱘ㞾ࡼব䞣䲚ড়ϡৠⱘᮄⱘ㞾ࡼব } putchar(n % 10 + '0'); printd(n / 10); if (n / 10) } n = •n; putchar('•'); if (n < 0) { { void printd(int n) /* printd: print n in decimal */ #include 䖭䞠㓪ݭⱘߑ᭄ϡ㛑໘⧚᳔໻ⱘ䋳᭄DŽ 䇗⫼ᅗ㞾䑿ᠧॄࠡ䴶ⱘ˄催ԡ˅᭄ᄫˈ✊ৢݡᠧॄৢ䴶ⱘ᭄ᄫDŽܜ⫼䗦ᔦˈߑ᭄ printd 佪 ҹⳌডⱘ⃵ᑣᠧॄᅗӀˈ䖭⾡ᮍᓣϢ 3.6 㡖Ё itoa ߑ᭄ⱘ໘⧚ᮍᓣⳌԐDŽ঺ϔ⾡ᮍ⊩߭ᰃՓ ࠄϔϾ᭄㒘Ёˈ✊ৢݡټ䆹䯂乬᳝ϸ⾡ᮍ⊩DŽϔ⾡ᮍ⊩ᰃᇚ⫳៤ⱘ৘Ͼ᭄ᄫձ⃵ᄬއ㾷 ᅗӀᖙ乏ҹϢℸⳌডⱘ⃵ᑣᠧॄDŽ Ѣ催ԡ᭄ᄫ⫳៤ˈԚܜDŽࠡ䴶䆆䖛ˈ᭄ᄫᰃҹডᑣ⫳៤ⱘ˖Ԣԡ᭄ᄫމ԰ЎᄫヺІᠧॄⱘᚙ C 䇁㿔Ёⱘߑ᭄ৃҹ䗦ᔦ䇗⫼ˈेߑ᭄ৃҹⳈ᥹៪䯈᥹䇗⫼㞾䑿DŽ៥Ӏ㗗㰥ϔϟᇚϔϾ᭄ 4.10. 䗦ᔦ ϟˈ᭄㒘ⱘ䭓ᑺᰃ 5˄4 ϾᄫヺࡴϞϔϾᄫヺІ㒧ᴳヺ'\0'˅DŽމ䖭⾡ᚙ char pattern[] = { 'o', 'u', 'l', 'd'}; ᅗৠϟ䴶ⱘໄᯢᰃㄝӋⱘ˖ char pattern[] = "ould "; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! #include "ำߑ੠" ೼⑤᭛ӊЁˈӏԩᔶབ˖ ᭛ӊࣙ৿ᣛҸ˄े#include ᣛҸ˅Փᕫ໘⧚໻䞣ⱘ#define ᣛҸҹঞໄᯢ᳈ࡴᮍ֓DŽ 4.11.1. ᭛ӊࣙ৿ 㒡乘໘⧚఼ⱘ݊ᅗϔѯ⡍ᗻˈབᴵӊ㓪䆥Ϣᏺখ᭄ⱘᅣDŽ ӊⱘݙᆍࣙ৿䖯ᔧࠡ᭛ӊЁ˅੠#define ᣛҸ˄⫼ӏᛣᄫヺᑣ߫᳓ҷϔϾᷛ䆄˅DŽᴀ㡖䖬ᇚҟ ⱘ㄀ϔϾℹ偸DŽϸϾ᳔ᐌ⫼ⱘ乘໘⧚఼ᣛҸᰃ˖#include ᣛҸ˄⫼Ѣ೼㓪䆥ᳳ䯈ᡞᣛᅮ᭛ C 䇁㿔䗮䖛乘໘⧚఼ᦤկњϔѯ䇁㿔ࡳ㛑DŽҢὖᗉϞ䆆ˈ乘໘⧚఼ᰃ㓪䆥䖛⿟Ёऩ⣀ᠻ㸠 4.11. C乘໘⧚఼ 㕂DŽצ 㒗д 4•13 㓪ݭϔϾ䗦ᔦ⠜ᴀⱘ reverse(s)ߑ᭄ˈҹᇚᄫヺІ s 䇗⫼ᡞᭈ᭄䕀ᤶЎᄫヺІDŽ 㒗д 4•12 䖤⫼ printd ߑ᭄ⱘ䆒䅵ᗱᛇ㓪ݭϔϾ䗦ᔦ⠜ᴀⱘ itoa ߑ᭄ˈे䗮䖛䗦ᔦ 䕗དⱘ՟ᄤDŽ Ϣ⧚㾷DŽ೼ᦣ䗄ᷥㄝ䗦ᔦᅮНⱘ᭄᥂㒧ᵘᯊՓ⫼䗦ᔦᇸ݊ᮍ֓DŽ៥Ӏᇚ೼ 6.5 㡖Ёҟ㒡ϔϾ↨ ᑊϨ↨Ⳍᑨⱘ䴲䗦ᔦҷⷕ᳈ᯧѢ㓪ݭˈޥⱘᷜDŽ䗦ᔦⱘᠻ㸠䗳ᑺᑊϡᖿˈԚ䗦ᔦҷⷕ↨䕗㋻ ໘⧚ؐټ఼ⱘᓔ䫔ˈ಴Ў䗦ᔦ䇗⫼䖛⿟Ёᖙ乏೼ᶤϾഄᮍ㓈ᡸϔϾᄬټ䗦ᔦᑊϡ㡖ⳕᄬ ᑧЁᦤկњϔϾ qsort ߑ᭄ˈᅗৃ⫼Ѣᇍӏԩ㉏ൟⱘᇍ䈵ᥦᑣDŽޚᷛ } v[j] = temp; v[i] = v[j]; temp = v[i]; int temp; { void swap(int v[], int i, int j) /* swap: interchange v[i] and v[j] */ Փ⫼ 3 ⃵DŽ ㋴Ѹᤶ᪡԰ᬒ೼ϔϾऩ⣀ⱘߑ᭄ swap Ёˈᰃ಴Ўᅗ೼ qsort ߑ᭄Ё㽕ܗ䖭䞠П᠔ҹᇚ᭄㒘 } qsort(v, last+1, right); qsort(v, left, last•1); swap(v, left, last); /* restore partition elem */ swap(v, ++last, i); if (v[i] < v[left]) for (i = left + 1; i <= right; i++) /* partition */ last = left; /* to v[0] */ swap(v, left, (left + right)/2); /* move partition elem */ return; /* fewer than two elements */ if (left >= right) /* do nothing if array contains */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 㗠᮴䳔䩜ᇍϡৠⱘ᭄᥂㉏ൟ䳔㽕ᅮНϡৠⱘ max ߑ᭄DŽ བᵰᇍ৘⾡㉏ൟⱘখ᭄ⱘ໘⧚ᰃϔ㟈ⱘˈ߭ৃҹᇚৠϔϾᅣᅮНᑨ⫼Ѣӏԩ᭄᥂㉏ൟˈ x = ((p+q) > (r+s) ? (p+q) : (r+s)); ᇚ㹿᳓ᤶЎϟ߫ᔶᓣ˖ x = max(p+q, r+s); ℸЎ A ៪ B˅ⱘ↣⃵ߎ⦄䛑ᇚ㹿᳓ᤶ៤ᇍᑨⱘᅲ䰙খ᭄DŽ಴ℸˈ䇁হ˖ ᰃߑ᭄䆡⫼ˈԚᅣ䇗⫼Ⳉ᥹ᇚ᳓ᤶ᭛ᴀᦦܹࠄҷⷕЁDŽᔶᓣখ᭄˄೼ڣՓ⫼ᅣ max ⳟ䍋ᴹᕜ #define max(A, B) ((A) > (B) ? (A) : (B)) НᅮНњϔϾᅣ max˖ ᅣᅮНгৃҹᏺখ᭄ˈ䖭ḋৃҹᇍϡৠⱘᅣ䇗⫼Փ⫼ϡৠⱘ᳓ᤶ᭛ᴀDŽ՟བˈϟ߫ᅣᅮ 䆹䇁হЎ᮴䰤ᕾ⦃ᅮНњϔϾᮄৡᄫ foreverDŽ #define forever for (;;) /* infinite loop */ ᳓ᤶ᭛ᴀৃҹᰃӏᛣⱘˈ՟བ˖ ᰃϔϾ䗮䖛#define ᣛҸᅮН䖛ⱘৡᄫˈ߭೼ printf("YES")៪ YESMAN Ёᇚϡᠻ㸠᳓ᤶDŽ ⫼ࠡ䴶ߎ⦄ⱘᅣᅮНDŽ᳓ᤶাᇍ䆄ো䖯㸠ˈᇍᣀ೼ᓩোЁⱘᄫヺІϡ䍋԰⫼DŽ՟བˈབᵰ YES ᅮНⱘৡᄫⱘ԰⫼ඳҢ݊ᅮН⚍ᓔྟˈࠄ㹿㓪䆥ⱘ⑤᭛ӊⱘ᳿ሒ໘㒧ᴳDŽᅣᅮНЁгৃҹՓ Ͼ䕗䭓ⱘᅣᅮНߚ៤㢹ᑆ㸠ˈ䖭ᯊ䳔㽕೼ᕙ㓁ⱘ㸠᳿ሒࡴϞϔϾড᭰ᴴヺ\DŽ#define ᣛҸ #define ᣛҸऴϔ㸠ˈ᳓ᤶ᭛ᴀᰃ#define ᣛҸ㸠ሒ䚼ⱘ᠔᳝࠽ԭ䚼ߚݙᆍˈԚгৃҹᡞϔ ϟˈމ#define ᣛҸЁⱘৡᄫϢব䞣ৡⱘੑৡᮍᓣⳌৠˈ᳓ᤶ᭛ᴀৃҹᰃӏᛣᄫヺІDŽ䗮ᐌᚙ 䖭ᰃϔ⾡᳔ㅔऩⱘᅣ᳓ᤶ——ৢ㓁᠔᳝ߎ⦄ৡᄫ䆄োⱘഄᮍ䛑ᇚ㹿᳓ᤶЎ඙ܰำ·DŽ #define ੠ᆓ ඙ܰำ· ᅣᅮНⱘᔶᓣབϟ˖ 4.11.2. ᅣ᳓ᤶ ᶤϾࣙ৿᭛ӊⱘݙᆍথ⫳њব࣪ˈ䙷М᠔᳝ձ䌪Ѣ䆹ࣙ৿᭛ӊⱘ⑤᭛ӊ䛑ᖙ乏䞡ᮄ㓪䆥DŽ ߎ⦄ϔѯϡᖙ㽕ⱘ䫭䇃DŽᕜ㞾✊ˈབᵰܡ⑤᭛ӊ䛑݋᳝ⳌৠⱘᅮНϢব䞣ໄᯢˈ䖭ḋৃҹ䙓 ೼໻ⱘ⿟ᑣЁˈ#include ᣛҸᰃᇚ᠔᳝ໄᯢᤚ㒥೼ϔ䍋ⱘ䕗དⱘᮍ⊩DŽᅗֱ䆕᠔᳝ⱘ ѯݙᆍ≵᳝ᖙ㽕ऩ⣀ᄬᬒ೼᭛ӊЁ˗䆓䯂༈᭛ӊⱘ㒚㡖ৠ݋ԧⱘᅲ⦄᳝݇DŽ˅ extern ໄᯢˈ៪Ң༈᭛ӊЁ䆓䯂ᑧߑ᭄ⱘߑ᭄ॳൟໄᯢˈ↨བDŽ˄ϹḐഄ䇈ˈ䖭 ⑤᭛ӊⱘᓔྟ໘䗮ᐌ䛑Ӯ᳝໮Ͼ#include ᣛҸˈᅗӀ⫼ҹࣙ৿ᐌ㾕ⱘ#define 䇁হ੠ #include ᣛҸDŽ ߭ᇚḍ᥂Ⳍᑨⱘ㾘߭ᶹᡒ䆹᭛ӊˈ䖭Ͼ㾘߭ৠ݋ԧⱘᅲ⦄᳝݇DŽ㹿ࣙ৿ⱘ᭛ӊᴀ䑿гৃࣙ৿ ԡ㕂ᶹᡒ䆹᭛ӊ˗བᵰ೼䆹ԡ㕂≵᳝ᡒࠄ᭛ӊˈ៪㗙བᵰ᭛ӊৡᰃ⫼ᇪᣀো<Ϣ>ᣀ䍋ᴹⱘˈ ⱘ㸠䛑ᇚ㹿᳓ᤶЎ⬅᭛ӊৡᣛᅮⱘ᭛ӊⱘݙᆍDŽབᵰ᭛ӊৡ⫼ᓩোᓩ䍋ᴹˈ߭೼⑤᭛ӊ᠔೼ #include <ำߑ੠> ៪ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! Դ᳝᠔ᐂࡽDŽ˅ 㒗д 4•14 ᅮНᅣ swap(t, x, y ˅ҹѸᤶ t ㉏ൟⱘϸϾখ᭄DŽ˄Փ⫼⿟ᑣഫ㒧ᵘӮᇍ ##ⱘጠ༫Փ⫼㾘߭↨䕗䲒ҹᥠᦵˈ䆺㒚㒚㡖䇋খ䯙䰘ᔩ ADŽ ಴ℸˈᅣ䇗⫼ paste(name, 1)ⱘ㒧ᵰᇚᓎゟ䆄ো name1DŽ #define paste(front, back) front ## back ᠿᦣDŽ՟བˈϟ䴶ᅮНⱘᅣ paste ⫼Ѣ䖲᥹ϸϾখ᭄ ##Ⳍ䚏ˈ߭䆹খ᭄ᇚ㹿ᅲ䰙খ᭄᳓ᤶˈ##Ϣࠡৢⱘぎⱑヺᇚ㹿ߴ䰸ˈᑊᇍ᳓ᤶৢⱘ㒧ᵰ䞡ᮄ 乘໘⧚఼䖤ㅫヺ##Ўᅣᠽሩᦤկњϔ⾡䖲᥹ᅲ䰙খ᭄ⱘ᠟↉DŽབᵰ᳓ᤶ᭛ᴀЁⱘখ᭄Ϣ ড়⊩ⱘᄫヺІᐌ䞣DŽ ೼ᅲ䰙খ᭄Ёˈ↣Ͼঠᓩো"ᇚ㹿᳓ᤶЎ\"ˈড᭰ᴴ\ᇚ㹿᳓ᤶЎ\\ˈ಴ℸ᳓ᤶৢⱘᄫヺІᰃ printf("x/y = &g\n", x/y); ݊ЁⱘᄫヺІ㹿䖲᥹䍋ᴹњˈ䖭ḋˈ䆹ᅣ䇗⫼ⱘᬜᵰㄝӋѢ printf("x/y" " = &g\n", x/y); 䇗⫼䆹ᅣᯊˈ䆹ᅣᇚ㹿ᠽሩЎ˖ dprint(x/y) Փ⫼䇁হ #define dprint(expr) printf(#expr " = %g\n", expr) ㅫ㒧ড়䍋ᴹ㓪ݭϔϾ䇗䆩ᠧॄᅣ˖ 㒧ᵰᇚ㹿ᠽሩЎ⬅ᅲ䰙খ᭄᳓ᤶ䆹খ᭄ⱘᏺᓩোⱘᄫヺІDŽ՟བˈৃҹᇚᅗϢᄫヺІ䖲᥹䖤 ᔶᓣখ᭄ϡ㛑⫼ᏺᓩোⱘᄫヺІ᳓ᤶDŽԚᰃˈབᵰ೼᳓ᤶ᭛ᴀЁˈখ᭄ৡҹ#԰Ўࠡ㓔߭ int getchar(void) { ... } #undef getchar ᰃᅣ䇗⫼˖ ҹֱ䆕ৢ㓁ⱘ䇗⫼ᰃߑ᭄䇗⫼ˈ㗠ϡৃخৃҹ䗮䖛#undef ᣛҸপ⍜ৡᄫⱘᅣᅮНˈ䖭ḋ 䫔DŽ༈᭛ӊЁᅮНⱘߑ᭄гᐌᐌᰃ䗮䖛ᅣᅲ⦄ⱘDŽ ໘⧚ᄫヺᯊ䇗⫼ߑ᭄᠔䳔ⱘ䖤㸠ᯊᓔܡputchar ߑ᭄೼ᅲ䰙Ёᐌᐌ㹿ᅮНЎᅣˈ䖭ḋৃҹ䙓 Ԛᰃˈᅣ䖬ᰃᕜ᳝ӋؐⱘDŽ༈᭛ӊЁ᳝ϔϾᕜᅲ⫼ⱘ՟ᄤ˖getchar Ϣ ਸ਼˛މᔧ⫼ squrare(z+1)䇗⫼䆹ᅣᅮНᯊӮߎ⦄ҔМᚙ #define square(x) x * x /* WRONG */ ℷ⹂ᗻDŽ㗗㰥ϟ߫ᅣᅮН˖ ᅗᇚᇍ↣Ͼখ᭄ᠻ㸠ϸ⃵㞾๲᪡԰DŽৠᯊ䖬ᖙ乏⊼ᛣˈ㽕䗖ᔧՓ⫼೚ᣀোҹֱ䆕䅵ㅫ⃵ᑣⱘ max(i++, j++) /* WRONG */ DŽ՟བ˖މⱘᚙ ໡䅵ㅫϸ⃵ˈབᵰ㸼䖒ᓣᄬ೼ࡃ԰⫼˄↨བ৿᳝㞾๲䖤ㅫヺ៪䕧ܹˋ䕧ߎ˅ˈ߭Ӯߎ⦄ϡℷ⹂ Ҩ㒚㗗㰥ϔϟ max ⱘሩᓔᓣˈህӮথ⦄ᅗᄬ೼ϔѯ㔎䱋DŽ݊Ёˈ԰Ўখ᭄ⱘ㸼䖒ᓣ㽕䞡 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! #endif /* hdr.h ᭛ӊⱘݙᆍᬒ೼䖭䞠 */ #define EDR #ifndef HDR ᅮНDŽϞ䴶᳝݇#if ⱘ㄀ϔϾ՟ᄤৃҹᬍݭЎϟ߫ᔶᓣ˖ C 䇁㿔ϧ䮼ᅮНњϸϾ乘໘⧚䇁হ#ifdef Ϣ#ifndefˈᅗӀ⫼ᴹ⌟䆩ᶤϾৡᄫᰃ৺Ꮖ㒣 #include HDR #endif #define HDR "default.h" #else #define HDR "msdos.h" #elif SYSTEM == MSDOS #define HDR "bsd.h" #elif SYSTEM == BSD #define HDR "sysv.h" #if SYSTEM == SYSV ᴀⱘ༈᭛ӊ˖ 䆩㋏㒳ব䞣 SYSTEMˈ✊ৢḍ᥂䆹ব䞣ⱘؐ⹂ᅮࣙ৿ાϾ⠜⌟ܜϟ䴶ⱘ䖭↉乘໘⧚ҷⷕ佪 ӊࣙ৿䖯ᴹˈ⫼᠋ϡᖙ㗗㰥੠໘⧚༈᭛ӊП䯈ⱘ৘⾡ձ䌪݇㋏DŽ བᵰ໮Ͼ༈᭛ӊ㛑໳ϔ㟈ഄՓ⫼䖭⾡ᮍᓣˈ䙷Мˈ↣Ͼ༈᭛ӊ䛑ৃҹᇚᅗ᠔ձ䌪ⱘӏԩ༈᭛ ໮⃵䞡໡ࣙ৿ৠϔ᭛ӊDŽܡ㒣ᅮНˈ䖭ḋᇚⳈ᥹䏇䕀ࠄ#endif ໘DŽ㉏Ԑⱘᮍᓣгৃҹ⫼ᴹ䙓 ㄀ϔ⃵ࣙ৿༈᭛ӊ hdr.h ᯊˈᇚᅮНৡᄫ HDR˗ℸৢݡ⃵ࣙ৿䆹༈᭛ӊᯊˈӮথ⦄䆹ৡᄫᏆ #endif /* hdr.h ᭛ӊⱘݙᆍᬒ೼䖭䞠 */ #define HDR #if !defined(HDR) ⱘᴵӊ䇁হЁ˖ ՟བˈЎњֱ䆕 hdr.h ᭛ӊⱘݙᆍা㹿ࣙ৿ϔ⃵ˈৃҹᇚ䆹᭛ӊⱘݙᆍࣙ৿೼ϟ߫ᔶᓣ Ў 0DŽ defined(ৡᄫ)ˈ䆹㸼䖒ᓣⱘؐ䙉ᕾϟ߫㾘߭˖ᔧৡᄫᏆ㒣ᅮНᯊˈ݊ؐЎ 1˗৺߭ˈ݊ؐ #else 䇁হЎℶ˄乘໘⧚఼䇁হ#elif ㉏ԐѢ else if˅DŽ೼#if 䇁হЁৃҹՓ⫼㸼䖒ᓣ 䞣˅䖯㸠∖ؐˈ㢹䆹㸼䖒ᓣⱘؐϡㄝѢ 0ˈ߭ࣙ৿݊ৢⱘ৘㸠ˈⳈࠄ䘛ࠄ#endifǃ#elif ៪ #if 䇁হᇍ݊Ёⱘᐌ䞣ᭈൟ㸼䖒ᓣ˄݊Ёϡ㛑ࣙ৿ sizeofǃ㉏ൟ䕀ᤶ䖤ㅫヺ៪ enum ᐌ ϔ⾡᠟↉DŽ Ё䖯㸠䅵ㅫDŽ䖭⾡ᮍᓣЎ೼㓪䆥䖛⿟Ёḍ᥂䅵ㅫ᠔ᕫⱘᴵӊؐ䗝ᢽᗻഄࣙ৿ϡৠҷⷕᦤկњ 䖬ৃҹՓ⫼ᴵӊ䇁হᇍ乘໘⧚ᴀ䑿䖯㸠᥻ࠊˈ䖭⾡ᴵӊ䇁হⱘؐᰃ೼乘໘⧚ᠻ㸠ⱘ䖛⿟ ᴵӊࣙ৿ .4.11.3 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! int *ip; /* ip is a pointer to int */ int x = 1, y = 2, z[10]; ԩ೼⿟ᑣЁໄᯢᣛ䩜ҹঞབԩՓ⫼䖤ㅫヺ&੠*˖ ᅮ x Ϣ y ᰃᭈ᭄ˈ㗠 ip ᰃᣛ৥ int ㉏ൟⱘᣛ䩜ˈϟ䴶ⱘҷⷕ↉䇈ᯢњབ؛䈵DŽ៥Ӏ೼䖭䞠 䖤ㅫヺ*ᰃ䯈᥹ᇏഔ៪䯈᥹ᓩ⫼䖤ㅫヺDŽᔧᅗ԰⫼Ѣᣛ䩜ᯊˈᇚ䆓䯂ᣛ䩜᠔ᣛ৥ⱘᇍܗϔ ㋴DŽᅗϡ㛑԰⫼Ѣ㸼䖒ᓣǃᐌ䞣៪ register ㉏ൟⱘব䞣DŽܗⱘᇍ䈵ˈेব䞣Ϣ᭄㒘 ᇚᡞ c ⱘഄഔ䌟ؐ㒭ব䞣 pˈ៥Ӏ⿄ p ЎĀᣛ৥”c ⱘᣛ䩜DŽഄഔ䖤ㅫヺ&া㛑ᑨ⫼ѢݙᄬЁ p = &c; 䖤ㅫヺ&ৃ⫼ѢপϔϾᇍ䈵ⱘഄഔˈ಴ℸˈϟ߫䇁হ˖ܗϔ ೒ 5•1 ᵰ c ⱘ㉏ൟᰃ charˈᑊϨ p ᰃᣛ৥ c ⱘᣛ䩜ˈ߭ৃ⫼೒ 5•1 㸼⼎ᅗӀП䯈ⱘ݇㋏˖ 䗮ᐌᰃϸϾ៪ 4 Ͼᄫ㡖˅DŽ಴ℸˈབ˄ܗऩټ㉏ൟⱘ᭄᥂DŽᣛ䩜ᰃ㛑໳ᄬᬒϔϾഄഔⱘϔ㒘ᄬ ϔϾ long˄䭓ᭈൟ˅ټᄬৃܗऩټϔϾ short˄ⷁᭈൟ˅㉏ൟⱘ᭄᥂ˈ㗠 4 ϾⳌ䚏ⱘᄫ㡖ᄬټ ᄬৃܗऩټϟˈᴎ఼ⱘϔϾᄫ㡖ৃҹᄬᬒϔϾ char ㉏ൟⱘ᭄᥂ˈϸϾⳌ䚏ⱘᄫ㡖ᄬމ䗮ᐌᚙ ҹऩϾ䖯㸠᪡㒉ˈгৃҹҹ䖲㓁៤㒘ⱘᮍᓣ᪡㒉DŽৃܗऩټ䖛ѯᄬˈܗऩټ㓁㓪ো៪㓪ഔⱘᄬ ៥Ӏ䗮䖛ϔϾㅔऩⱘ⼎ᛣ೒ᴹ䇈ᯢݙᄬᰃབԩ㒘㒛ⱘDŽ䗮ᐌⱘᴎ఼䛑᳝ϔ㋏߫䖲ˈܜ佪 5.1. ᣛ䩜Ϣഄഔ ⱘᣛ䩜˅ҷ᳓ char *԰Ў䗮⫼ᣛ䩜ⱘ㉏ൟDŽ 㒣㹿ᕜ໮Ӭ⾔ⱘ⿟ᑣ䆒䅵Ҏਬ੠㓪䆥఼᠔䞛㒇DŽℸ໪ˈANSI C Փ⫼㉏ൟ void * ˄ᣛ৥ void ANSI C ⱘϔϾ᳔䞡㽕ⱘব࣪ᰃˈᅗᯢ⹂ഄࠊᅮњ᪡㒉ᣛ䩜ⱘ㾘߭DŽџᅲϞˈ䖭ѯ㾘߭Ꮖ Ӏᇚሑ࡯䇈ᯢ䖭ϔ⚍DŽ 䇃ⱘഄᮍDŽԚᰃˈབᵰ䇼ᜢഄՓ⫼ᣛ䩜ˈ֓ৃҹ߽⫼ᅗݭߎㅔऩǃ⏙᱄ⱘ⿟ᑣDŽ೼ᴀゴЁ៥ ᣛ䩜੠ goto 䇁হϔḋˈӮᇐ㟈⿟ᑣ䲒ҹ⧚㾷DŽབᵰՓ⫼㗙㉫ᖗˈᣛ䩜ᕜᆍᯧህᣛ৥њ䫭 П䯈ⱘ݇㋏ˈᑊ᥶䅼བԩ߽⫼䖭⾡݇㋏DŽ ⱘҷⷕDŽᣛ䩜Ϣ᭄㒘П䯈ⱘ݇㋏कߚᆚߛˈ៥Ӏᇚ೼ᴀゴЁ䅼䆎ᅗӀޥҹ⫳៤᳈催ᬜǃ᳈㋻ 䩜ᐌᐌᰃ㸼䖒ᶤϾ䅵ㅫⱘᚳϔ䗨ᕘˈ঺ϔϾॳ಴ᰃˈৠ݊ᅗᮍ⊩↨䕗䍋ᴹˈՓ⫼ᣛ䩜䗮ᐌৃ ᣛ䩜ᰃϔ⾡ֱᄬব䞣ഄഔⱘব䞣DŽ೼ C 䇁㿔Ёˈᣛ䩜ⱘՓ⫼䴲ᐌᑓ⊯ˈॳ಴Пϔᰃˈᣛ ㄀5ゴᣛ䩜Ϣ᭄㒘 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᇚᡞ ip Ёⱘؐᣋ䋱ࠄ iq Ёˈ䖭ḋˈᣛ䩜 iq гᇚᣛ৥ ip ᣛ৥ⱘᇍ䈵DŽ iq = ip ⱘᮍ⊩Փ⫼DŽ՟བˈབᵰ iq ᰃ঺ϔϾᣛ৥ᭈൟⱘᣛ䩜ˈ䙷М䇁হ ᳔ৢ䇈ᯢϔ⚍ˈ⬅Ѣᣛ䩜гᰃব䞣ˈ᠔ҹ೼⿟ᑣЁৃҹⳈ᥹Փ⫼ˈ㗠ϡᖙ䗮䖛䯈᥹ᓩ⫼ 㟇Ꮊⱘ㒧ড়乎ᑣDŽ 䖤ㅫヺ䙉ᕾҢেܗ㗠ϡᰃᇍ ip ᣛ৥ⱘᇍ䈵䖯㸠ࡴ 1 䖤ㅫˈ䖭ᰃ಴Ўˈ㉏ԐѢ*੠++䖭ḋⱘϔ 䇁হⱘᠻ㸠㒧ᵰDŽ䇁হ(*ip)++Ёⱘ೚ᣀোᰃᖙ䳔ⱘˈ৺߭ˈ䆹㸼䖒ᓣᇚᇍ ip 䖯㸠ࡴ 1 䖤ㅫˈ (*ip)++ ៪ ++*ip ߭ᇚ ip ᣛ৥ⱘᇍ䈵ⱘؐࡴ 1ˈᅗㄝৠѢ *ip += 1 ᇚᡞ*ip ᣛ৥ⱘᇍ䈵ⱘؐপߎᑊࡴ 1ˈ✊ৢݡᇚ㒧ᵰ䌟ؐ㒭 yˈ㗠ϟ߫䌟ؐ䇁হ˖ y = *ip + 1 㑻催ˈ಴ℸˈ䌟ؐ䇁হܜ㑻↨ㅫᴃ䖤ㅫヺⱘӬܜ䖤ㅫヺ*੠&ⱘӬܗϔ ᇚᡞ*ip ⱘؐ๲ࡴ 10DŽ *ip = *ip + 10; 䇁হ བᵰᣛ䩜 ip ᣛ৥ᭈൟব䞣ˈ䙷М೼ x ৃҹߎ⦄ⱘӏԩϞϟ᭛Ё䛑ৃҹՓ⫼*ipˈ಴ℸˈ Ԛᅗϡ㛑䯈᥹ᓩ⫼݊㞾䑿DŽ៥Ӏᇚ೼ 5.11 㡖Ё䆺㒚䅼䆎䆹䯂乬˅DŽ ᰃᣛ৥ void ㉏ൟⱘᣛ䩜ৃҹᄬᬒᣛ৥ӏԩ㉏ൟⱘᣛ䩜ˈމ⾡⡍ᅮⱘ᭄᥂㉏ൟDŽ˄ϔϾ՟໪ᚙ ៥Ӏᑨ䆹⊼ᛣˈᣛ䩜া㛑ᣛ৥ᶤ⾡⡍ᅮ㉏ൟⱘᇍ䈵ˈгህᰃ䇈ˈ↣Ͼᣛ䩜䛑ᖙ乏ᣛ৥ᶤ ㉏ൟⱘᣛ䩜DŽ 㸼ᯢˈ೼㸼䖒ᓣЁˈ*dp ੠ atof(s)ⱘؐ䛑ᰃ double ㉏ൟˈϨ atof ⱘখ᭄ᰃϔϾᣛ৥ char double *dpˈatof(char *); ՟བˈໄᯢ 䇁⊩Ϣໄᯢ䆹ব䞣᠔೼㸼䖒ᓣⱘ䇁⊩㉏ԐDŽৠḋⱘॳ಴ˈᇍߑ᭄ⱘໄᯢгৃҹ䞛⫼䖭⾡ᮍᓣDŽ 䖭ḋໄᯢᰃЎњ֓Ѣ䆄ᖚDŽ䆹ໄᯢ䇁হ㸼ᯢ㸼䖒ᓣ*ip ⱘ㒧ᵰᰃ int ㉏ൟDŽ䖭⾡ໄᯢব䞣ⱘ int *ip˗ བϟ᠔⼎˖ ব䞣 xǃy Ϣ z ⱘໄᯢᮍᓣ៥ӀᏆ㒣೼ࠡ䴶ⱘゴ㡖Ё㾕ࠄ䖛DŽ៥Ӏᴹⳟᣛ䩜 ip ⱘໄᯢˈ ip = &z[0]; /* ip now points to z[0] */ *ip = 0; /* x is now 0 */ y = *ip; /* y is now 1 */ ip = &x; /* ip now points to x */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ៥Ӏ䗮䖛೒ 5•2 䖯㸠䇈ᯢDŽ } *py = temp; *px = *py; temp = *px; int temp; { void swap(int *px, int *py) /* interchange *px and *py */ খ᭄䛑ໄᯢЎᣛ䩜ˈᑊϨ䗮䖛䖭ѯᣛ䩜ᴹ䯈᥹䆓䯂ᅗӀᣛ৥ⱘ᪡԰᭄DŽ 䖤ㅫヺ&⫼ᴹপব䞣ⱘഄഔˈ䖭ḋ&a ህᰃϔϾᣛ৥ব䞣 a ⱘᣛ䩜DŽswap ߑ᭄ⱘ᠔᳝ܗ⬅Ѣϔ swap(&a, &b); 䇗⫼ߑ᭄ˈे˖ 䙷Мˈབԩᅲ⦄៥ӀⱘⳂᷛਸ਼ˈৃҹՓЏ䇗⿟ᑣᇚᣛ৥᠔㽕Ѹᤶⱘব䞣ⱘᣛ䩜Ӵ䗦㒭㹿 খ᭄ a ੠ b ⱘؐDŽ䆹ߑ᭄ҙҙѸᤶњ a ੠ b ⱘࡃᴀⱘؐDŽ 䖭ᰃ಴Ўˈ⬅Ѣখ᭄Ӵ䗦䞛⫼Ӵؐᮍᓣˈ಴ℸϞ䗄ⱘ swap ߑ᭄ϡӮᕅડࠄ䇗⫼ᅗⱘ՟⿟Ёⱘ swap(a, b); ߭ϟ߫䇁হ᮴⊩䖒ࠄ䆹ⳂⱘDŽ } y = temp; x = y; temp = x; int temp; { void swap(int x, int y) /* WRONG */ ㋴DŽԚᰃˈབᵰᇚ swap ߑ᭄ᅮНЎϟ߫ᔶᓣ˖ܗⱘצ ᬍЏ䇗ߑ᭄Ёব᳔ⱘؐDŽ՟བˈᥦᑣߑ᭄ৃ㛑ӮՓ⫼ϔϾৡЎ swap ⱘߑ᭄ᴹѸᤶϸϾ⃵ᑣ乴 ⬅Ѣ C 䇁㿔ᰃҹӴؐⱘᮍᓣᇚখ᭄ؐӴ䗦㒭㹿䇗⫼ߑ᭄DŽ಴ℸˈ㹿䇗⫼ߑ᭄ϡ㛑Ⳉ᥹ׂ ᣛ䩜Ϣߑ᭄খ᭄ .5.2 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ; while (isspace(c = getch())) /* skip white space */ int c, sign; { int getint(int *pn) /* getint: get next integer from input into *pn */ void ungetch(int); int getch(void); #include 䕧ܹЁࣙ৿ϔϾ᳝ᛣНⱘ᭄ᄫᯊ䖨ಲϔϾℷؐDŽ 䆹⠜ᴀⱘ getint ߑ᭄೼ࠄ䖒᭛ӊ㒧ሒᯊ䖨ಲ EOFˈᔧϟϔϾ䕧ܹϡᰃ᭄ᄫᯊ䖨ಲ 0ˈᔧ ᇚ᮴⊩ᡞ䕀ᤶᕫࠄⱘᭈ᭄Ӵಲ㒭䇗⫼㗙DŽ ؐᇚ๲ࡴ 1DŽ䇋⊼ᛣˈ䖭䞠ᖙ乏ᇚ array[n]ⱘഄഔӴ䗦㒭ߑ᭄ getintˈ৺߭ߑ᭄ getint ㋴ array[n]ˈৠᯊˈn ⱘܗ↣⃵䇗⫼ getint ᯊˈ䕧ܹ⌕ЁⱘϟϔϾᭈ᭄ᇚ㹿䌟ؐ㒭᭄㒘 for (n = 0; n < SIZE && getint(&array[n]) != EOF; n++) int n, array[SIZE], getint(int *); ϟ䴶ⱘᕾ⦃䇁হ䇗⫼ getint ߑ᭄㒭ϔϾᭈൟ᭄㒘䌟ؐ˖ њ䖭⾡ᮍ⊩ˈ݋ԧ㒚㡖䇋খ㾕 7.4 㡖DŽ 䕀ᤶৢᕫࠄⱘᭈ᭄ᑊӴಲ㒭Џ䇗ߑ᭄DŽߑ᭄ scanf ⱘᅲ⦄ህ䞛⫼ټᯊˈՓ⫼ϔϾᣛ䩜খ᭄ᄬ ৃҹ䖭ḋ䆒䅵䆹ߑ᭄˖ᇚᷛ䆚ᰃ৺ࠄ䖒᭛ӊ㒧ሒⱘ⢊ᗕ԰Ў getint ߑ᭄ⱘ䖨ಲؐˈৠ ৃ⫼ϔϾ䕧ܹⱘᭈ᭄㸼⼎DŽ ᴳᷛ䆄DŽ䖭ѯؐᖙ乏䗮䖛ϡৠⱘᮍᓣ䖨ಲDŽEOF˄᭛ӊ㒧ᴳᷛ䆄˅ৃҹ⫼ӏԩؐ㸼⼎ˈᔧ✊г ᕫࠄϔϾᭈ᭄DŽgetint 䳔㽕䖨ಲ䕀ᤶৢᕫࠄⱘᭈ᭄ˈᑊϨˈ೼ࠄ䖒䕧ܹ㒧ሒᯊ㽕䖨ಲ᭛ӊ㒧 ߑ᭄ getint ᥹ফ㞾⬅Ḑᓣⱘ䕧ܹˈᑊᠻ㸠䕀ᤶˈᇚ䕧ܹⱘᄫヺ⌕ߚ㾷៤ᭈ᭄ˈϨ↣⃵䇗⫼ ᣛ䩜খ᭄Փᕫ㹿䇗⫼ߑ᭄㛑໳䆓䯂੠ׂᬍЏ䇗ߑ᭄Ёᇍ䈵ⱘؐDŽ៥Ӏᴹⳟ䖭ḋϔϾ՟ᄤ˖ ೒ 5•2 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ߭䇈ᯢᅗᰃϔϾᣛ৥ᭈൟᇍ䈵ⱘᣛ䩜ˈ䙷Мˈ䌟ؐ䇁হ int *pa; ㋴DŽབᵰ pa ⱘໄᯢЎܗa[i]㸼⼎䆹᭄㒘ⱘ㄀ i Ͼ ೒ 5•3 ೼Ⳍ䚏ⱘݙᄬऎඳЁˈৡᄫߚ߿Ў a[0]ǃa[1]ǃĂǃa[9]˄খ㾕೒ 5•3˅DŽټᇍ䈵ᄬ ᅮНњϔϾ䭓ᑺЎ 10 ⱘ᭄㒘 aDŽᤶহ䆱䇈ˈᅗᅮНњϔϾ⬅ 10 Ͼᇍ䈵㒘៤ⱘ䲚ড়ˈ䖭 10 Ͼ int a[10]; ໄᯢ 䍋ᴹ⿡ᖂೄ䲒ϔѯDŽ ᣛ䩜㓪ݭⱘ⿟ᑣ↨⫼᭄㒘ϟᷛ㓪ݭⱘ⿟ᑣᠻ㸠䗳ᑺᖿˈԚ঺ϔᮍ䴶ˈ⫼ᣛ䩜ᅲ⦄ⱘ⿟ᑣ⧚㾷 䅼䆎ᣛ䩜Ϣ᭄㒘DŽ䗮䖛᭄㒘ϟᷛ᠔㛑ᅠ៤ⱘӏԩ᪡԰䛑ৃҹ䗮䖛ᣛ䩜ᴹᅲ⦄DŽϔ㠀ᴹ䇈ˈ⫼ ೼ C 䇁㿔Ёˈᣛ䩜੠᭄㒘П䯈ⱘ݇㋏कߚᆚߛˈ಴ℸˈ೼᥹ϟᴹⱘ䚼ߚЁˈ៥Ӏᇚৠᯊ 5.3. ᣛ䩜Ϣ᭄㒘 getfloat ߑ᭄ⱘ䖨ಲؐᑨ䆹ᰃҔМ㉏ൟ˛ 㒗д 5•2 ῵ӓߑ᭄ getint ⱘᅲ⦄ᮍ⊩ˈ㓪ݭϔϾ䇏প⍂⚍᭄ⱘߑ᭄ getfloatDŽ ヺো㾚Ў᭄ᄫ 0 ⱘ᳝ᬜ㸼䖒ᮍᓣDŽׂᬍ䆹ߑ᭄ˈᇚ䖭⾡ᔶᓣⱘ+៪•ヺো䞡ᮄݭಲࠄ䕧ܹ⌕ЁDŽ 㒗д 5•1 ೼Ϟ䴶ⱘ՟ᄤЁˈབᵰヺো+៪•ⱘৢ䴶㋻䎳ⱘϡᰃ᭄ᄫˈgetint ߑ᭄ᇚᡞ ᮄݭಲࠄ䕧ܹЁDŽ ࡽ䖭ϸϾߑ᭄ˈߑ᭄ getint ᖙ乏䇏ܹⱘϔϾ໮ԭᄫヺህৃҹ䞡׳ˈ˅ϸϾߑ᭄˄খ㾕 4.3 㡖 ೼ getintߑ᭄Ёˈ*pnྟ㒜԰ЎϔϾ᱂䗮ⱘᭈൟব䞣Փ⫼DŽ݊Ё䖬Փ⫼њgetch੠ungetch } return c; ungetch(c); if (c != EOF) *pn *= sign; *pn = 10 * *pn + (c • '0'); for (*pn = 0; isdigit(c), c = getch()) c = getch(); if (c == '+' || c == '•') sign = (c == '•') ? •1 : 1; } return 0; ungetch(c); /* it is not a number */ if (!isdigit(c) && c != EOF && c != '+' && c != '•') { w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ߿ᮑࡴഄഔ䖤ㅫヺ&ˈ֓ৃҹᕫߎ䖭ḋⱘ㒧䆎˖&a[i]੠ a+i ⱘ৿НгᰃⳌৠⱘDŽa+i ᰃ a ᓣˈ✊ৢݡ䖯㸠∖ؐˈ಴ℸ೼⿟ᑣЁ䖭ϸ⾡ᔶᓣᰃㄝӋⱘDŽབᵰᇍ䖭ϸ⾡ㄝӋⱘ㸼⼎ᔶᓣߚ ᇚ݊䕀ᤶЎ*(a+i)ⱘᔶܜ㋴ a[i]ⱘؐᯊˈC 䇁㿔ᅲ䰙Ϟܗৃ㛑Ӯ㾝ᕫᕜ༛ᗾDŽ೼䅵ㅫ᭄㒘 ㋴ a[i]ⱘᓩ⫼гৃҹݭ៤*(a+i)䖭⾡ᔶᓣDŽᇍ㄀ϔ⃵᥹㾺䖭⾡ݭ⊩ⱘҎᴹ䇈ˈܗᇍ᭄㒘 pa = a; 䌟ؐ䇁হ pa=&a[0]гৃҹݭ៤ϟ߫ᔶᓣ˖ ㋴ⱘഄഔˈ᠔ҹˈܗৢˈpa ੠ a ݋᳝ⳌৠⱘؐDŽ಴Ў᭄㒘ৡ᠔ҷ㸼ⱘህᰃ䆹᭄㒘᳔ᓔྟⱘϔϾ pa = &a[0]˗ ㋴ⱘഄഔDŽᠻ㸠䌟ؐ䇁হܗ䆹᭄㒘㄀ 0 Ͼ ϟᷛ੠ᣛ䩜䖤ㅫП䯈݋᳝ᆚߛⱘᇍᑨ݇㋏DŽḍ᥂ᅮНˈ᭄㒘㉏ൟⱘব䞣៪㸼䖒ᓣⱘؐᰃ Ͼᇍ䈵DŽ pa+1 ᣛ৥ pa ᠔ᣛ৥ⱘᇍ䈵ⱘϟϔϾᇍ䈵DŽⳌᑨഄˈpa+i ᣛ৥ pa ᠔ᣛ৥ⱘᇍ䈵Пৢⱘ㄀ i ㋴ⱘ㉏ൟ៪᭄㒘䭓ᑺᰃҔМˈϞ䴶ⱘ㒧䆎䛑៤ゟDŽĀᣛ䩜ࡴ 1āህᛣੇⴔˈܗ᮴䆎᭄㒘 a Ё ೒ 5•5 㾕೒ 5•5˅DŽ ㋴ a[i]ⱘݙᆍ˄খܗ㋴ a[i]ⱘഄഔˈ*(pa+i)ᓩ⫼ⱘᰃ᭄㒘ܗa[1]ⱘݙᆍˈpa+i ᰃ᭄㒘 ㋴ܗ㋴DŽ಴ℸˈབᵰᣛ䩜 pa ᣛ৥ a[0]ˈ䙷М*(pa+1)ᓩ⫼ⱘᰃ᭄㒘ܗ㋴Пࠡⱘ㄀ i Ͼܗ ㋴ˈ㗠 pa•i ᇚᣛ৥ pa ᠔ᣛ৥᭄㒘ܗ㋴Пৢⱘ㄀ i Ͼܗ㋴ˈpa+i ᇚᣛ৥ pa ᠔ᣛ৥᭄㒘ܗ ㋴ˈ䙷Мˈḍ᥂ᣛ䩜䖤ㅫⱘᅮНˈpa+1 ᇚᣛ৥ϟϔϾܗབᵰ pa ᣛ৥᭄㒘ЁⱘᶤϾ⡍ᅮ ㋴ a[0]Ёⱘݙᆍ໡ࠊࠄব䞣 x ЁDŽܗᇚᡞ᭄㒘 x = *pa; 䖭ḋˈ䌟ؐ䇁হ ೒ 5•4 㾕೒ 5•4˅DŽ ㋴ a[0]ⱘഄഔ˄খܗ㋴ˈгህᰃ䇈ˈpa ⱘؐЎ᭄㒘ܗ߭ৃҹᇚᣛ䩜 pa ᣛ৥᭄㒘 a ⱘ㄀ 0 Ͼ pa = &a[0]; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䛑ᇚᡞ䍋ྟѢ a[2]ⱘᄤ᭄㒘ⱘഄഔӴ䗦㒭ߑ᭄ fDŽ೼ߑ᭄ f Ёˈখ᭄ⱘໄᯢᔶᓣৃҹЎ f(a+2) Ϣ f(&a[2]) ᭄DŽ՟བˈབᵰ a ᰃϔϾ᭄㒘ˈ䙷Мϟ䴶ϸϾߑ᭄䇗⫼ гৃҹᇚᣛ৥ᄤ᭄㒘䍋ྟԡ㕂ⱘᣛ䩜Ӵ䗦㒭ߑ᭄ˈ䖭ḋˈህᇚ᭄㒘ⱘϔ䚼ߚӴ䗦㒭њߑ ᭄㒘੠ᣛ䩜䖭ϸ⾡㸼⼎ᮍ⊩DŽ 䱣ৢḍ᥂Ⳍᑨⱘᮍᓣ᪡԰䆹খ᭄DŽЎњⳈ㾖Ϩᙄᔧഄᦣ䗄ߑ᭄ˈ೼ߑ᭄Ё⫮㟇ৃҹৠᯊՓ⫼ ߸ᅮᰃᣝ✻᭄㒘໘⧚䖬ᰃᣝ✻ᣛ䩜໘⧚ˈމᣛ䩜DŽབᵰᇚ᭄㒘ৡӴ䗦㒭ߑ᭄ˈߑ᭄ৃҹḍ᥂ᚙ ᰃㄝӋⱘDŽ៥Ӏ䗮ᐌ᳈дᛃѢՓ⫼ৢϔ⾡ᔶᓣˈ಴Ўᅗ↨ࠡ㗙᳈Ⳉ㾖ഄ㸼ᯢњ䆹খ᭄ᰃϔϾ char *s; ੠ char s[]; ೼ߑ᭄ᅮНЁˈᔶᓣখ᭄ 䛑ৃҹℷ⹂ഄᠻ㸠DŽ strlen(ptr); /* char *ptr; */ strlen(array); /* char array[100]; */ strlen("hello, world"); /* string constant */ ㉏ԐѢϟ䴶䖭ḋⱘߑ᭄䇗⫼˖ ᭄ⱘ䇗⫼㗙ЁⱘᄫヺІˈᅗҙᇍ䆹ᣛ䩜೼ strlen ߑ᭄Ёⱘ⾕᳝ࡃᴀ䖯㸠㞾๲䖤ㅫDŽ಴ℸˈ ಴Ў s ᰃϔϾᣛ䩜ˈ᠔ҹᇍ݊ᠻ㸠㞾๲䖤ㅫᰃড়⊩ⱘDŽᠻ㸠 s++䖤ㅫϡӮᕅડࠄ strlen ߑ } return n; n++; for (n = 0; *s != '\0', s++) int n; { int strlen(char *s) /* strlen: return length of string s */ ⱘ䭓ᑺDŽ ⱘব䞣DŽ៥Ӏৃҹ߽⫼䆹⡍ᗻ㓪ݭ strlen ߑ᭄ⱘ঺ϔϾ⠜ᴀˈ䆹ߑ᭄⫼Ѣ䅵ㅫϔϾᄫヺІ ഄഔؐټ᭄Ёˈ䆹খ᭄ᰃϔϾሔ䚼ব䞣ˈ಴ℸˈ᭄㒘ৡখ᭄ᖙ乏ᰃϔϾᣛ䩜ˈгህᰃϔϾᄬ ㋶ⱘഄഔDŽ೼㹿䇗⫼ߑܗᔧᡞ᭄㒘ৡӴ䗦㒭ϔϾߑ᭄ᯊˈᅲ䰙ϞӴ䗦ⱘᰃ䆹᭄㒘㄀ϔϾ ᓣⱘ䇁হᰃ䴲⊩ⱘDŽ 䇁㿔Ёˈ䇁হ pa=a ੠ pa++䛑ᰃড়⊩ⱘDŽԚ᭄㒘ৡϡᰃব䞣ˈ಴ℸˈ㉏ԐѢ a=pa ੠ a++ᔶ Ԛᰃˈ៥Ӏᖙ乏䆄ԣˈ᭄㒘ৡ੠ᣛ䩜П䯈᳝ϔϾϡৠП໘ˈᣛ䩜ᰃϔϾব䞣ˈ಴ℸˈ೼ C 䞣ᅲ⦄DŽ⿏أ䗮䖛ᣛ䩜੠ ϟᷛDŽpa[i]Ϣ*(pa+i)ᰃㄝӋⱘDŽㅔ㗠㿔ПˈϔϾ䗮䖛᭄㒘੠ϟᷛᅲ⦄ⱘ㸼䖒ᓣৃㄝӋഄ ㋴ⱘഄഔDŽⳌᑨഄˈབᵰ pa ᰃϾᣛ䩜ˈ䙷Мˈ೼㸼䖒ᓣЁгৃҹ೼ᅗⱘৢ䴶ࡴܗПৢ㄀ i Ͼ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! allocp += n; if (allocbuf + ALLOCSIZE • allocp >= n) { /* it fits */ { char *alloc(int n) /* return pointer to n characters */ static char *allocp = allocbuf; /* next free position */ static char allocbuf[ALLOCSIZE]; /* storage for alloc */ #define ALLOCSIZE 10000 /* size of available space */ allocp ⱘؐ䆒㕂Ў p˄খ㾕೒ 5•6˅DŽ 䯆ぎ䯈ϡ໳ˈ߭ alloc 䖨ಲ 0DŽབᵰ p ೼ allocbuf ⱘ䖍⬠Пݙˈ߭ afree(p)ҙҙাᰃᇚ ⱘᔧࠡؐ˄ेぎ䯆ഫⱘᓔྟԡ㕂˅ˈ✊ৢᇚ allocp ࡴ n ҹՓᅗᣛ৥ϟϔϾぎ䯆ऎඳDŽབᵰぎ allocbuf ᭄㒘Ё᳝≵᳝䎇໳ⱘ࠽ԭぎ䯈DŽབᵰ᳝䎇໳ⱘぎ䯆ぎ䯈ˈ߭ alloc 䖨ಲ allocp DŽᔧ䇗⫼ alloc ⬇䇋 n Ͼᄫヺⱘぎ䯈ᯊˈalloc Ẕᶹܗallocbuf ЁⱘϟϔϾぎ䯆ऩ гᰃ៥Ӏ䳔㽕њ㾷ⱘֵᙃDŽ៥ӀՓ⫼ᣛ䩜 allocp ᣛ৥މ⢊⫼allocbuf Ёⱘぎ䯈Փ ᕫDŽ ഫⱘᣛ䩜㦋ټҹ≵᳝ৡᄫˈᅗৃҹ䗮䖛䇗⫼ malloc ߑ᭄៪৥᪡԰㋏㒳⬇䇋ϔϾᣛ৥᮴ৡᄬ ⱘ⑤᭛ӊЁᇚ䆹᭄㒘ໄᯢЎ static ㉏ൟˈՓᕫᅗᇍ໪ϡৃ㾕DŽᅲ䰙ᅲ⦄ᯊˈ䆹᭄㒘⫮㟇ৃ 䩜㗠ϡᰃ᭄㒘ϟᷛˈ಴ℸˈ݊ᅗߑ᭄᮴䳔ⶹ䘧䆹᭄㒘ⱘৡᄫˈ䖭ḋˈৃҹ೼ࣙ৿ alloc ੠ afree ᭄㒘ᰃ alloc ੠ afree ϸϾߑ᭄⾕᳝ⱘ᭄㒘DŽ⬅Ѣߑ᭄ alloc ੠ afree ໘⧚ⱘᇍ䈵ᰃᣛ ᳔ᆍᯧⱘᅲ⦄ᮍ⊩ᰃ䅽 alloc ߑ᭄ᇍϔϾ໻ᄫヺ᭄㒘 allocbuf Ёⱘぎ䯈䖯㸠ߚ䜡DŽ䆹 䰤ࠊˈ៥Ӏᇚ೼ 8.7 㡖Ё䇈ᯢབԩᅲ⦄䖭ѯߑ᭄DŽ ᑧЁᦤկњ݋᳝㉏Ԑࡳ㛑ⱘߑ᭄ malloc ੠ freeˈᅗӀ≵᳝Ϟ䗄ޚぎ䯈ⱘㅵ⧚DŽᷛټ䖯㸠ᄬ ߎⱘ߫㸼˅ܜ䇗⫼ alloc ߑ᭄ⱘ⃵ᑣⳌডDŽᤶহ䆱䇈ˈalloc Ϣ afree ҹᷜⱘᮍᓣ˄ेৢ䖯 ֓ҹৢ䞡⫼DŽП᠔ҹ䇈䖭ϸϾߑ᭄ᰃĀϡᅠ୘ⱘāˈᰃ಴Ўᇍ afree ߑ᭄ⱘ䇗⫼⃵ᑣᖙ乏Ϣ ぎ䯈ˈҹټᄫヺᑣ߫DŽ㄀ѠϾߑ᭄ afree(p)䞞ᬒᏆߚ䜡ⱘᄬټ᭄ⱘ䇗⫼㗙ৃ߽⫼䆹ᣛ䩜ᄬ ⱘᣛ䩜ˈalloc ߑܗऩټϾߑ᭄㒘៤DŽ㄀ϔϾߑ᭄ alloc(n)䖨ಲϔϾᣛ৥ n Ͼ䖲㓁ᄫヺᄬ ߚ䜡⿟ᑣDŽᅗ⬅ϸټϔ䍋ᰃ䆹䇁㿔ⱘϔ໻Ӭ⚍DŽЎњ䇈ᯢ䖭ϔ⚍ˈ៥ӀᴹⳟϔϾϡᅠ୘ⱘᄬ C 䇁㿔Ёⱘഄഔㅫᴃ䖤ㅫᮍ⊩ᰃϔ㟈Ϩ᳝㾘ᕟⱘˈᇚᣛ䩜ǃ᭄㒘੠ഄഔⱘㅫᴃ䖤ㅫ䲚៤೼ ㋴DŽ䖭㉏䖤ㅫᰃᣛ䩜៪ഄഔㅫᴃ䖤ㅫЁ᳔ㅔऩⱘᔶᓣDŽܗϾ ㋴Пৢⱘ㄀ iܗ㋴ˈ㗠 p+=i ᇚᇍ p 䖯㸠ࡴ i ⱘ๲䞣䖤ㅫˈՓ݊ᣛ৥ᣛ䩜 p ᔧࠡ᠔ᣛ৥ⱘܗ ㋴ⱘᣛ䩜ˈ䙷М p++ᇚᇍ p 䖯㸠㞾๲䖤ㅫᑊᣛ৥ϟϔϾܗབᵰ p ᰃϔϾᣛ৥᭄㒘ЁᶤϾ 5.4. ഄഔㅫᴃ䖤ㅫ ᔧ✊ˈᓩ⫼᭄㒘䖍⬠П໪ⱘᇍ䈵ᰃ䴲⊩ⱘDŽ ㋴DŽܗp[•1]ǃp[•2]䖭ḋⱘ㸼䖒ᓣ೼䇁⊩Ϟ䛑ᰃড়⊩ⱘˈᅗӀߚ߿ᓩ⫼ԡѢ p[0]ПࠡⱘϸϾ ㋴DŽ㉏ԐѢܗ㋴Пࠡⱘܗ㋴ᄬ೼ˈгৃҹ䗮䖛ϟᷛ䆓䯂᭄㒘㄀ϔϾܗབᵰ⹂ֵⳌᑨⱘ ㋴DŽܗᇍѢߑ᭄ f ᴹ䇈ˈᅗᑊϡ݇ᖗ᠔ᓩ⫼ⱘᰃ৺াᰃϔϾ᳈໻᭄㒘ⱘ䚼ߚ f(int *arr) { ... } ៪ f(int arr[]) { ... } w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ㉏ԐѢ 㒣ᐌӮ⫼ࠄ NULLDŽ ༈᭛ӊЁDŽ៥Ӏ೼ৢ䴶䚼ߚޚ0 ᰃᣛ䩜ⱘϔϾ⡍⅞ؐDŽヺোᐌ䞣 NULL ᅮН೼ᷛ ҹ੠ᐌ䞣 0 䖯㸠↨䕗DŽ⿟ᑣЁ㒣ᐌ⫼ヺোᐌ䞣 NULL ҷ᳓ᐌ䞣 0ˈ䖭ḋ֓Ѣ᳈⏙᱄ഄ䇈ᯢᐌ䞣 ᣛ䩜Ϣᭈ᭄П䯈ϡ㛑ⳌѦ䕀ᤶˈԚ 0 ᰃᚳϔⱘ՟໪˖ᐌ䞣 0 ৃҹ䌟ؐ㒭ᣛ䩜ˈᣛ䩜гৃ 0 㸼⼎≵᳝䎇໳ⱘぎ䯆ぎ䯈ৃկߚ䜡DŽ 0 ∌䖰ϡᰃ᳝ᬜⱘ᭄᥂ഄഔˈ಴ℸˈ䖨ಲؐ 0 ৃ⫼ᴹ㸼⼎থ⫳њᓖᐌџӊDŽ೼ᴀ՟Ёˈ䖨ಲؐ ⊩⒵䎇ˈalloc ᖙ乏䖨ಲᶤ⾡ᔶᓣⱘֵোҹ䇈ᯢ≵᳝䎇໳ⱘぎ䯆ぎ䯈ৃկߚ䜡DŽC 䇁㿔ֱ䆕ˈ alloc ᇚ䖨ಲϔϾᣛ৥᠔䳔໻ᇣⱘᄫヺഫ佪ഄഔⱘᣛ䩜˄⊼ᛣߑ᭄ᴀ䑿ⱘໄᯢ˅DŽབᵰ⬇䇋᮴ ぎ䯈ⱘ⬇䇋ৃҹ⒵䎇ˈټぎ䯈ৢ allocp ⱘᮄؐ㟇໮↨ allocbuf ⱘሒッഄഔ໻ 1DŽབᵰᄬټ ぎ䯈䇋∖DŽབᵰぎ䯆ぎ䯈䎇໳ˈ߭ߚ䜡ᄬټẔᶹᰃ৺᳝䎇໳ⱘぎ䯆ぎ䯈ҹ⒵䎇 n Ͼᄫヺⱘᄬ if (allocbuf + ALLOCSIZE • allocp >= n) { /* it fits */ ϟ߫ if ⌟䆩䇁হ˖ ㋴ⱘഄഔDŽܗ䖭ᰃ಴Ў䆹᭄㒘ৡᅲ䰙Ϟህᰃ᭄㒘㄀ 0 Ͼ static char* allocp = &allocbuf[0]; ᑣᠻ㸠ᯊⱘϟϔϾぎ䯆ԡ㕂DŽϞ䗄䇁হгৃҹݭ៤ϟ߫ᔶᓣ˖ ᇚ allocp ᅮНЎᄫヺ㉏ൟᣛ䩜ˈᑊᇚᅗ߱ྟ࣪Ў allocbuf ⱘ䍋ྟഄഔˈ䆹䍋ྟഄഔᰃ⿟ static char* allocp = allocbuf; ᅮНⱘ݋᳝䗖ᔧ㉏ൟⱘ᭄᥂ⱘഄഔDŽ՟བˈໄᯢ ࣪ؐা㛑ᰃ 0 ៪㗙ᰃ㸼⼎ഄഔⱘ㸼䖒ᓣˈᇍৢ㗙ᴹ䇈ˈ㸼䖒ᓣ᠔ҷ㸼ⱘഄഔᖙ乏ᰃ೼ℸࠡᏆ ϟˈৠ݊ᅗ㉏ൟⱘব䞣ϔḋˈᣛ䩜гৃҹ߱ྟ࣪DŽ䗮ᐌˈᇍᣛ䩜᳝ᛣНⱘ߱ྟމϔ㠀ᚙ ೒ 5•6 } allocp = p; if (p >= allocbuf && p < allocbuf + ALLOCSIZE) { void afree(char *p) /* free storage pointed to by p */ } return 0; } else /* not enough room */ return allocp • n; /* old p */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ৥ⱘᇍ䈵ⱘ䭓ᑺDŽ ᕫࠄϔϾ䗖⫼Ѣ⍂⚍㉏ൟ㗠䴲ᄫヺൟⱘݙᄬߚ䜡ߑ᭄DŽ᠔᳝ⱘᣛ䩜䖤ㅫ䛑Ӯ㞾ࡼ㗗㰥ᅗ᠔ᣛ ഔDŽ಴ℸˈা䳔㽕ᇚ alloc ੠ afree ߑ᭄Ё᠔᳝ⱘ char ㉏ൟ᳓ᤶЎ float ㉏ൟˈህৃҹ ㉏ൟˈᑊϨ p ᰃϔϾᣛ৥⍂⚍㉏ൟⱘᣛ䩜ˈ䙷М೼ᠻ㸠 p++ৢˈp ᇚᣛ৥ϟϔϾ⍂⚍᭄ⱘഄ ぎ䯈ⱘ⍂⚍ټᣛ䩜ⱘㅫᴃ䖤ㅫ݋᳝ϔ㟈ᗻ˖བᵰ໘⧚ⱘ᭄᥂㉏ൟᰃ↨ᄫヺൟऴ᥂᳈໮ᄬ ⱘߑ᭄⠜ᴀⳌऍ䜡DŽSize_t ᰃ⬅䖤ㅫヺ sizeof 䖨ಲⱘ᮴ヺোᭈൟDŽ˅ ᑧЁޚؐDŽԚᰃˈ៥Ӏ೼䖭䞠Փ⫼ size_t ԰Ўߑ᭄ strlen ⱘ䖨ಲؐ㉏ൟˈ䖭ḋৃҹϢᷛ ᳔໻㣗ೈDŽ༈᭛ӊЁᅮНⱘ㉏ൟ ptrdiff_t 䎇ҹ㸼⼎ϸϾᣛ䩜П䯈ⱘᏺヺোᏂ Ꮖ㒣Ẕᶹ䖛ⱘᄫヺ᭄ˈेᄫヺІⱘ䭓ᑺDŽ˄ᄫヺІЁⱘᄫヺ᭄᳝ৃ㛑䍙䖛 int ㉏ൟ᠔㛑㸼⼎ⱘ Ѣ p ᰃᣛ৥ᄫヺⱘᣛ䩜ˈ᠔ҹ↣ᠻ㸠ϔ⃵ p++ˈp ህᇚᣛ৥ϟϔϾᄫヺⱘഄഔˈp•s ߭㸼⼎ ᕾ⦃䇁হᇚձ⃵ẔᶹᄫヺІЁⱘ↣ϾᄫヺˈⳈࠄ䘛ࠄᷛ䆚ᄫヺ᭄㒘㒧ሒⱘᄫヺ'\0' ЎℶDŽ⬅ ೼Ϟ䗄⿟ᑣ↉ⱘໄᯢЁˈᣛ䩜 p 㹿߱ྟ࣪Ўᣛ৥ sˈेᣛ৥䆹ᄫヺІⱘ㄀ϔϾᄫヺDŽwhi1e } return p • s; p++; while (*p != '\0') char *p = s; { int strlen(char *s) /* strlen: return length of string s */ ⠜ᴀˈབϟ᠔⼎˖ ㋴ⱘ᭄ⳂDŽ៥Ӏ⬅ℸৃҹ㓪ݭߎߑ᭄ strlen ⱘ঺ϔϾܗ㋶П䯈ⱘܗህᰃԡѢ p ੠ q ᣛ৥ⱘ ㋶ˈϨ p=ⱘ݇㋏↨䕗䖤ㅫDŽབᵰ p ᣛ৥ⱘ᭄㒘 㸠↨䕗䖤ㅫDŽ՟བˈབᵰᣛ䩜 p ੠ q ᣛ৥ৠϔϾ᭄㒘ⱘ៤ਬˈ䙷МᅗӀП䯈ህৃҹ䖯㸠㉏Ԑ ϟᇍᣛ䩜ৃҹ䖯މ೼ᶤѯᚙˈܜⱘᴵӊ⌟䆩䇁হ㸼ᯢᣛ䩜ㅫᴃ䖤ㅫ᳝ҹϟ޴Ͼ䞡㽕⡍⚍DŽ佪 if (p >= allocbuf && p < allocbuf + ALLOCSIZE) ҹঞ if (allocbuf + ALLOCSIZE • allocp >= n) { /* it fits */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ೒ 5•7 ׂᬍᄫヺІⱘݙᆍˈ㒧ᵰᰃ≵᳝ᅮНⱘ˄খ㾕೒ 5•7˅DŽ ᰃϔϾᣛ䩜ˈ݊߱ؐᣛ৥ϔϾᄫヺІᐌ䞣ˈПৢᅗৃҹ㹿ׂᬍҹᣛ৥݊ᅗഄഔˈԚབᵰ䆩೒ ԡ㕂DŽ঺ϔᮍ䴶ˈpmessageټ㒘ЁⱘऩϾᄫヺৃҹ䖯㸠ׂᬍˈԚ amessage ྟ㒜ᣛ৥ৠϔϾᄬ Ϟ䗄ໄᯢЁˈamessage ᰃϔϾҙҙ䎇ҹᄬᬒ߱ྟ࣪ᄫヺІҹঞぎᄫヺ'\0'ⱘϔ㓈᭄㒘DŽ᭄ char *pmessage = "now is the time"; /*ᅮНϔϾᣛ䩜 */ char amessage[] = "nw is the time"; /*ᅮНϔϾ᭄㒘 */ ϟ䴶ϸϾᅮНП䯈᳝ᕜ໻ⱘᏂ߿˖ ᰃ⍝ঞࠄᣛ䩜ⱘ᪡԰DŽC 䇁㿔≵᳝ᦤկᇚᭈϾᄫヺІ԰ЎϔϾᭈԧ䖯㸠໘⧚ⱘ䖤ㅫヺDŽ ᇚᡞϔϾᣛ৥䆹ᄫヺ᭄㒘ⱘᣛ䩜䌟ؐ㒭 pmessageDŽ䆹䖛⿟ᑊ≵᳝䖯㸠ᄫヺІⱘ໡ࠊˈ㗠া pmessage ="now is the time"; 䙷Мˈ䇁হ char *pmessage; ᅮᣛ䩜 pmessage ⱘໄᯢབϟ˖؛䰸њ԰Ўߑ᭄খ᭄໪ˈᄫヺІᐌ䞣䖬᳝݊ᅗ⫼⊩DŽ ㋴ⱘᣛ䩜䆓䯂DŽܗ䗮䖛ϔϾᣛ৥݊㄀ϔϾ 䗄䇁হЁˈprintf ᥹ফⱘᰃϔϾᣛ৥ᄫヺ᭄㒘㄀ϔϾᄫヺⱘᣛ䩜DŽгህᰃ䇈ˈᄫヺІᐌ䞣ৃ ᔧ㉏ԐѢ䖭ḋⱘϔϾᄫヺІߎ⦄೼⿟ᑣЁᯊˈᅲ䰙Ϟᰃ䗮䖛ᄫヺᣛ䩜䆓䯂䆹ᄫヺІⱘDŽ೼Ϟ princf("hello, world\n"}; ᄫヺІᐌ䞣᳔ᐌ㾕ⱘ⫼⊩г䆌ᰃ԰Ўߑ᭄খ᭄ˈ՟བ˖ ᭄г಴ℸ↨ঠᓩোݙⱘᄫヺ᭄໻ 1DŽܗऩټᄫヺ᭄㒘ⱘ㒧ሒDŽᄫヺІᐌ䞣ऴ᥂ⱘᄬ ೼ᄫヺІⱘݙ䚼㸼⼎Ёˈᄫヺ᭄㒘ҹぎᄫヺ'\0'㒧ሒˈ᠔ҹˈ⿟ᑣৃҹ䗮䖛Ẕᶹぎᄫヺᡒࠄ "I am a string" ᆓ׸ґШ२ᰃϔϾᄫヺ᭄㒘ˈ՟བ˖ 5.5. ᄫヺᣛ䩜Ϣߑ᭄ 䰸໪˅DŽމ㉏ൟⱘᚙ ৥ϔ⾡㉏ൟᇍ䈵ⱘᣛ䩜䌟ؐ㒭ᣛ৥঺ϔ⾡㉏ൟᇍ䈵ⱘᣛ䩜ⱘ䖤ㅫ˄ϸϾᣛ䩜Пϔᰃ void * ៪ሣ㬑䖤ㅫ˗ᣛ䩜ৠ float ៪ double ㉏ൟП䯈ⱘࡴ⊩䖤ㅫ˗ϡ㒣ᔎࠊ㉏ൟ䕀ᤶ㗠Ⳉ᥹ᇚᣛ 䕗䖤ㅫDŽ݊ᅗ᠔᳝ᔶᓣⱘᣛ䩜䖤ㅫ䛑ᰃ䴲⊩ⱘˈ՟བϸϾᣛ䩜䯈ⱘࡴ⊩ǃЬ⊩ǃ䰸⊩ǃ⿏ԡ ៪↨䕗䖤ㅫ˗ᇚᣛ䩜䌟ؐЎ 0 ៪ᣛ䩜Ϣ 0 П䯈ⱘ↨⫣ޣ㋴ⱘϸϾᣛ䩜䯈ⱘܗᣛ৥Ⳍৠ᭄㒘Ё 䖤ㅫ˗⫣ޣ᳝ᬜⱘᣛ䩜䖤ㅫࣙᣀⳌৠ㉏ൟᣛ䩜П䯈ⱘ䌟ؐ䖤ㅫ˗ᣛ䩜ৠᭈ᭄П䯈ⱘࡴ⊩៪ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! void strcpy(char *s, char *t) /* strcpy: copy t to s; pointer version 3 */ ᮁ㸼䖒ᓣⱘؐᰃ৺Ў 0 ेৃDŽ಴ℸˈ䆹ߑ᭄ৃ䖯ϔℹݭ៤ϟ߫ᔶᓣ˖ Ўњ᳈䖯ϔℹഄ㊒⚐⿟ᑣˈ៥Ӏ⊼ᛣࠄˈ㸼䖒ᓣৠ'\0'ⱘ↨䕗ᰃ໮ԭⱘˈ಴Ўা䳔㽕߸ ৥ⱘԡ㕂ˈⳈࠄ䘛ࠄ㒧ᴳヺ'\0'Ўℶ˄ৠᯊг໡ࠊ䆹㒧ᴳヺ˅ˈ ᄫヺ'\0'䖯㸠↨䕗䖤ㅫˈҹ᥻ࠊᕾ⦃ⱘᠻ㸠DŽ᳔ৢⱘ㒧ᵰᰃձ⃵ᇚ t ᣛ৥ⱘᄫヺ໡ࠊࠄ s ᣛ ࠄњᣛ䩜 s ᣛ৥ⱘᮻԡ㕂DŽ䆹ᄫヺؐৠᯊг⫼ᴹ੠ぎټ೼ s ᠻ㸠㞾๲䖤ㅫПࠡˈᄫヺህ㹿ᄬ 䖤ㅫПࠡ t ᠔ᣛ৥ⱘᄫヺDŽৢ㓔䖤ㅫヺ++㸼⼎೼䇏প䆹ᄫヺПৢᠡᬍব t ⱘؐDŽৠḋⱘ䘧⧚ˈ ೼䆹⠜ᴀЁˈs ੠ t ⱘ㞾๲䖤ㅫᬒࠄњᕾ⦃ⱘ⌟䆩䚼ߚЁDŽ㸼䖒ᓣ*t++ⱘؐᰃᠻ㸠㞾๲ } ; while ((*s++ = *t++) != '\0') { void strcpy(char *s, char *t) /* strcpy: copy t to s; pointer version 2 */ ݭ៤ϟ߫ᔶᓣ˖ ᅲ䰙Ϟˈstrcpy ߑ᭄ᑊϡӮᣝ✻Ϟ䴶ⱘ䖭ѯᮍᓣ㓪ݭDŽ㒣偠Єᆠⱘ⿟ᑣਬ᳈୰⃶ᇚᅗ㓪 Ⳉࠄᇚ t Ёⱘ㒧ᴳヺ'\0'໡ࠊࠄ s ЎℶDŽ ੠ t ᰃᮍ֓ഄ䖯㸠њ߱ྟ࣪ⱘᣛ䩜ˈᕾ⦃↣ᠻ㸠ϔ⃵ˈᅗӀህ⊓ⴔⳌᑨⱘ᭄㒘ࠡ䖯ϔϾᄫヺˈ ಴Ўখ᭄ᰃ䗮䖛ؐӴ䗦ⱘˈ᠔ҹ೼ strcpy ߑ᭄ЁৃҹҹӏԩᮍᓣՓ⫼খ᭄ s ੠ tDŽ೼ℸˈs } } t++; s++; while ((*s = *t) != '\0') { i = 0; int i; { void strcpy(char *s, char *t) /* strcpy: copy t to s; pointer version */ Ўњ䖯㸠↨䕗ˈϟ䴶ᰃ⫼ᣛ䩜ᮍ⊩ᅲ⦄ⱘ strcpy ߑ᭄˖ } i++; while ((s[i] = t[i]) != '\0') i = 0; int i; { void strcpy(char *s, char *t) /* strcpy: copy t to s; array subscript version */ ᮍ⊩ᅲ⦄ⱘˈབϟ᠔⼎˖ ヺDŽЎњ䖯㸠ᄫヺⱘ໡ࠊˈ䖭䞠Փ⫼њϔϾᕾ⦃䇁হDŽstrcpy ߑ᭄ⱘ㄀ 1 Ͼ⠜ᴀᰃ䗮䖛᭄㒘 s ᣛ৥ⱘԡ㕂DŽབᵰՓ⫼䇁হ s=t ᅲ⦄䆹ࡳ㛑ˈ݊ᅲ䋼Ϟাᰃᣋ䋱њᣛ䩜ˈ㗠ᑊ≵᳝໡ࠊᄫ ᴹⷨおᅗӀⱘϡৠᅲ⦄⠜ᴀDŽ㄀ϔϾߑ᭄ strcpy(s, t)ᡞᣛ䩜 t ᣛ৥ⱘᄫヺІ໡ࠊࠄᣛ䩜 ᑧЁϸϾ᳝⫼ⱘߑ᭄Ў՟ޚЎњ᳈䖯ϔℹഄ䅼䆎ᣛ䩜੠᭄㒘݊ᅗᮍ䴶ⱘ䯂乬ˈϟ䴶ҹᷛ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 㒗д 5•5 ᅲ⦄ᑧߑ᭄ strncpyǃstrncat ੠ strncmpˈᅗӀ᳔໮ᇍখ᭄ᄫヺІЁ 䖨ಲ 1˗৺߭䖨ಲ 0DŽ 㒗д 5•4 㓪ݭߑ᭄ strend(s, t)DŽབᵰᄫヺІ t ߎ⦄೼ᄫヺІ s ⱘሒ䚼ˈ䆹ߑ᭄ ⱘᄫヺІ໡ࠊࠄ s ᣛ৥ⱘᄫヺІⱘሒ䚼DŽ 㒗д 5•3 ⫼ᣛ䩜ᮍᓣᅲ⦄㄀ 2 ゴЁⱘߑ᭄ strcatDŽߑ᭄ strcat(s, t)ᇚ t ᣛ৥ І໘⧚ߑ᭄ⱘໄᯢDŽ ᑧЁ݊ᅗϔѯᄫヺޚ༈᭛ӊЁࣙ৿ᴀ㡖ᦤࠄⱘߑ᭄ⱘໄᯢˈ঺໪䖬ࣙᣀᷛ DŽ᳈䆺㒚ⱘֵᙃˈ䇋খ㾕 4.3 㡖DŽ⫣⫼ޚᰃ䖯ᷜ੠ߎᷜⱘᷛ ㋴ᔍߎࠄ val Ё */ܗval = *••p; /*ᇚᷜ乊 *p++ = val; /*ᇚ val य़ܹᷜ */ 䖤ㅫDŽџᅲϞˈϟ䴶ⱘϸϾ㸼䖒ᓣ˖ޣᇍ p ᠻ㸠㞾ܜ೼䇏পᣛ䩜 p ᣛ৥ⱘᄫヺПࠡ *••p 䖤ㅫヺ++੠••ᣝ✻݊ᅗᮍᓣ㒘ড়Փ⫼ˈԚ䖭ѯ⫼⊩ᑊϡ໮㾕DŽ՟བˈϟ߫㸼䖒ᓣ ⬅Ѣ++੠••᮶ৃҹ԰Ўࠡ㓔䖤ㅫヺˈгৃҹ԰Ўৢ㓔䖤ㅫヺˈ᠔ҹ䖬ৃҹᇚ䖤ㅫヺ*Ϣ } return *s • *t; return 0; if (*s == '\0') for ( ; *s == *t; s++, t++) { int strcmp(char *s, char *t) /* strcmp: return <0 if s0 if s>t */ ϟ䴶⫼ᰃᣛ䩜ᮍᓣᅲ⦄ⱘ strcmp ߑ᭄˖ } return s[i] • t[i]; return 0; if (s[i] == '\0') for (i = 0; s[i] == t[i]; i++) int i; { int strcmp(char *s, char *t) /* strcmp: return <0 if s0 if s>t */ ᰃ s ੠ t ⬅ࠡ৥ৢ䗤ᄫヺ↨䕗ᯊ䘛ࠄⱘ㄀ϔϾϡⳌㄝᄫヺ໘ⱘᄫヺⱘᏂؐDŽ ᑊϨḍ᥂ s ᣝ✻ᄫ݌乎ᑣᇣѢǃㄝѢ៪໻Ѣ t ⱘ㒧ᵰߚ߿䖨ಲ䋳ᭈ᭄ǃ0 ៪ℷᭈ᭄DŽ䆹䖨ಲؐ ៥Ӏⷨおⱘ㄀ѠϾߑ᭄ᰃᄫヺІ↨䕗ߑ᭄ strcmp(s, t)DŽ䆹ߑ᭄↨䕗ᄫヺІ s ੠ tˈ ᑧ˄˅Ёᦤկⱘߑ᭄ strcpy ᡞⳂᷛᄫヺІ԰Ўߑ᭄ؐ䖨ಲDŽޚᷛ 㿔⿟ᑣЁ㒣ᐌӮ䞛⫼䖭⾡ݭ⊩DŽ 䆹ߑ᭄߱ⳟ䍋ᴹϡ໾ᆍᯧ⧚㾷ˈԚ䖭⾡㸼⼎ᮍ⊩ᰃᕜ᳝ད໘ⱘˈ៥Ӏᑨ䆹ᥠᦵ䖭⾡ᮍ⊩ˈC 䇁 } ; while (*s++ = *t++) } w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ⫼Ѣ㸼⼎䴲⊩㸠᭄ⱘ᭄ؐˈ՟བ•1DŽ 㛑໘⧚᳝䰤᭄Ⳃⱘ䕧ܹ㸠ˈ᠔ҹ೼䕧ܹ㸠᭄䖛໮㗠䍙䖛䰤ᅮⱘ᳔໻㸠᭄ᯊˈ䆹ߑ᭄䖨ಲᶤϾ 㒘DŽᅗৠᯊ䖬ᖙ乏㒳䅵䕧ܹⱘ㸠᭄ˈ಴Ў೼ᥦᑣ੠ᠧॄᯊ㽕⫼ࠄ䖭ϔֵᙃDŽ⬅Ѣ䕧ܹߑ᭄া 䕧ܹߑ᭄ᖙ乏ᬊ䲚੠ֱᄬ↣Ͼ᭛ᴀ㸠ЁⱘᄫヺˈᑊᓎゟϔϾᣛ৥䖭ѯ᭛ᴀ㸠ⱘᣛ䩜ⱘ᭄ ঞ䕧ܹ੠䕧ߎߑ᭄DŽ 䇈ᯢˈ⦄೼Џ㽕㗗㰥᭄᥂㒧ᵘҹخ݊ᅗߑ᭄ⱘᠻ㸠DŽ݇Ѣᇍ᭛ᴀ㸠ᥦᑣ䖭ϔℹˈ៥Ӏ⿡ৢݡ ϟˈ᳔དᇚ⿟ᑣߦߚ៤㢹ᑆϾϢ䯂乬ⱘ㞾✊ߦߚⳌϔ㟈ⱘߑ᭄ˈᑊ䗮䖛Џߑ᭄᥻ࠊމ䗮ᐌᚙ ̟ұ༝ӕ࿦ำ·໻ ճำ·໻ࠩ໻૦༝ ՠ௜ൎပ೛ద໻ ᥦᑣ䖛⿟ࣙᣀϟ߫ 3 Ͼℹ偸˖ ⫳䯂乬DŽ ㅵ⧚੠Ꮌ໻ⱘᓔ䫔䖭ϸϾᄾټ䖭⾡ᅲ⦄ᮍ⊩⍜䰸њ಴⿏ࡼ᭛ᴀ㸠ᴀ䑿᠔ᏺᴹⱘ໡ᴖⱘᄬ ೒ 5•8 ᑨⱘᣛ䩜ˈ㗠ϡᰃ䖭ϸϾ᭛ᴀ㸠ᴀ䑿˄খ㾕೒ 5•8˅DŽ ⱘϸϾ᭛ᴀ㸠ᯊˈᅲ䰙ϞѸᤶⱘᰃᣛ䩜᭄㒘ЁϢ䖭ϸϾ᭛ᴀ㸠Ⳍᇍצⱘ↨䕗DŽᔧѸᤶ⃵ᑣ乴 ϔϾ᭄㒘ЁDŽ䖭ḋˈᇚᣛ৥ϸϾ᭛ᴀ㸠ⱘᣛ䩜Ӵ䗦㒭ߑ᭄ strcmp ህৃᅲ⦄ᇍ䖭ϸϾ᭛ᴀ㸠 ೼ټ㒘Ёˈ䙷М↣Ͼ᭛ᴀ㸠ৃ䗮䖛ᣛ৥ᅗⱘ㄀ϔϾᄫヺⱘᣛ䩜ᴹ䆓䯂DŽ䖭ѯᣛ䩜ᴀ䑿ৃҹᄬ ೼ϔϾ䭓ᄫヺ᭄ټ៥Ӏᓩܹᣛ䩜᭄㒘໘⧚䖭⾡䯂乬DŽབᵰᕙᥦᑣⱘ᭛ᴀ㸠佪ሒⳌ䖲ഄᄬ ԰DŽ៥Ӏ䳔㽕ϔϾ㛑໳催ᬜǃᮍ֓ഄ໘⧚ৃব䭓ᑺ᭛ᴀ㸠ⱘ᭄᥂㸼⼎ᮍ⊩DŽ ⧚ⱘᰃ䭓ᑺϡϔⱘ᭛ᴀ㸠DŽᑊϨϢᭈ᭄ϡৠⱘᰃˈᅗӀϡ㛑೼ऩϾ䖤ㅫЁᅠ៤↨䕗៪⿏ࡼ᪡ ㄀ 4 ゴЁ⫼ᖿ䗳ᥦᑣㅫ⊩ᇍᅗ䖯㸠њᬍ䖯DŽ䖭ѯᥦᑣㅫ⊩೼ℸҡ✊ᰃ᳝ᬜⱘˈԚᰃˈ⦄೼໘ ㋴䖯㸠ᥦᑣⱘ shell ᥦᑣߑ᭄ˈᑊ೼ܗ៥Ӏ೼㄀ 3 ゴЁ᳒ᦣ䗄䖛ϔϾ⫼Ѣᇍᭈൟ᭄㒘Ёⱘ ᑣDŽ UNIX ⿟ᑣ sort ⱘϔϾㅔ࣪⠜ᴀ䇈ᯢ䖭ϔ⚍DŽ䆹⿟ᑣᣝᄫ↡乎ᑣᇍ⬅᭛ᴀ㸠㒘៤ⱘ䲚ড়䖯㸠ᥦ ೼᭄㒘ЁDŽϟ䴶䗮䖛㓪ݭټᅗব䞣ϔḋᄬ݊ڣ⬅Ѣᣛ䩜ᴀ䑿гᰃব䞣ˈ᠔ҹᅗӀгৃҹ 5.6. ᣛ䩜᭄㒘ҹঞᣛ৥ᣛ䩜ⱘᣛ䩜 3 ゴ ˅ˈ strindexǃgetop˄㄀ 4 ゴ˅ㄝㄝDŽ getline˄㄀ 1ǃ4 ゴ ˅ˈ atoiǃitoa ҹঞᅗӀⱘবԧᔶᓣ˄㄀ 2ǃ3ǃ4 ゴ ˅ˈ reverse˄㄀ 㒗д 5•6 䞛⫼ᣛ䩜㗠䴲᭄㒘㋶ᓩᮍᓣᬍݭࠡ䴶ゴ㡖੠㒗дЁⱘᶤѯ⿟ᑣˈ՟བ ЁDŽ᳈䆺㒚ⱘ䇈ᯢ䇋খ㾕䰘ᔩ BDŽ ⱘࠡ n Ͼᄫヺ䖯㸠᪡԰DŽ՟བˈߑ᭄ strncpy(s, t, n)ᇚ t Ё᳔໮ࠡ n Ͼᄫヺ໡ࠊࠄ s 䕧ߎߑ᭄া䳔㽕ᣝ✻ᣛ䩜᭄㒘Ёⱘ⃵ᑣձ⃵ᠧॄ䖭ѯ᭛ᴀ㸠ेৃDŽ #include #include #define MAXLINES 5000 /* max #lines to be sorted */ char *lineptr[MAXLINES]; /* pointers to text lines */ int readlines(char *lineptr[], int nlines); void writelines(char *lineptr[], int nlines); void qsort(char *lineptr[], int left, int right); /* sort input lines */ main() { int nlines; /* number of input lines read */ if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { qsort(lineptr, 0, nlines•1); writelines(lineptr, nlines); return 0; } else { printf("error: input too big to sort\n"); return 1; } } #define MAXLEN 1000 /* max length of any input line */ int getline(char *, int); char *alloc(int); /* readlines: read input lines */ int readlines(char *lineptr[], int maxlines) { int len, nlines; char *p, line[MAXLEN]; nlines = 0; while ((len = getline(line, MAXLEN)) > 0) if (nlines >= maxlines || p = alloc(len) == NULL) return •1; else { line[len•1] = '\0'; /* delete newline */ strcpy(p, line); lineptr[nlines++] = p; } return nlines; } /* writelines: write output lines */ void writelines(char *lineptr[], int nlines) { int i; for (i = 0; i < nlines; i++) printf("%s\n", lineptr[i]); Click to buy NOW! PDF•XC H ANGE w w w . d o c u • t r a c k.com w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! char *temp; { void swap(char *v[], int i, int j) /* swap: interchange v[i] and v[j] */ ϔѯᕜᇣⱘᬍࡼ˖خৠḋˈswap ߑ᭄гা䳔㽕 } qsort(v, last+1, right); qsort(v, left, last•1); swap(v, left, last); swap(v, ++last, i); if (strcmp(v[i], v[left]) < 0) for (i = left+1; i <= right; i++) last = left; swap(v, left, (left + right)/2); return; /* fewer than two elements */ if (left >= right) /* do nothing if array contains */ void swap(char *v[], int i, int j); int i, last; { void qsort(char *v[], int left, int right) /* qsort: sort v[left]...v[right] into increasing order */ ӏԩᬍࡼDŽ خ⃵ˈ䳔㽕䇗⫼ strcmp ߑ᭄ᅠ៤᭛ᴀ㸠ⱘ↨䕗䖤ㅫDŽԚᥦᑣㅫ⊩೼䖭䞠ҡ✊᳝ᬜˈϡ䳔㽕 䳔㽕ׂᬍ䆹ߑ᭄ⱘໄᯢ䚼ߚ˗݊ˈܜϔѯᇣᬍࡼ˖佪خ೼䖭䞠䳔㽕ᇍ㄀ 4 ゴⱘᖿ䗳ᥦᑣߑ᭄ ೼ᯢ⹂њ䕧ܹ੠䕧ߎߑ᭄ⱘᅲ⦄ᮍ⊩Пৢˈϟ䴶֓ৃҹⴔ᠟㗗㰥᭛ᴀ㸠ⱘᥦᑣ䯂乬њDŽ 䖤ㅫDŽޣϔ㸠ˈৠᯊᇍ nlines 䖯㸠㞾 ᕾ⦃ᓔྟᠻ㸠ᯊˈ*lineptr ᣛ৥㄀ϔ㸠ˈ↣ᠻ㸠ϔ⃵㞾๲䖤ㅫ䛑Փᕫ lineptr ᣛ৥ϟ ˄⊼ᛣ䖭䞠ⱘ᭄㒘ব䞣 lineptr ৃҹᬍবؐ˅ } printf("%s\n", *lineptr++); while (nlines•• > 0) { void writelines(char *lineptr[], int nlines) /* writelines: write output lines */ ⫼ˈ䖭ḋˈwritelines ߑ᭄ৃҹᬍݭЎ˖ ⬅Ѣ 1ineptr ᴀ䑿ᰃϔϾ᭄㒘ৡˈ಴ℸˈৃᣝ✻ࠡ䴶՟ᄤЁⳌৠⱘᮍ⊩ᇚ݊԰Ўᣛ䩜Փ 䩜ᣛ৥ⱘ㄀ i Ͼ᭛ᴀ㸠ⱘ佪ᄫヺDŽ ৥ᄫヺ㉏ൟᇍ䈵ⱘᣛ䩜DŽгህᰃ䇈ˈlineptr[i]ᰃϔϾᄫヺᣛ䩜ˈ㗠*lineptr[i]ᰃ䆹ᣛ ㋴ᰃϔϾᣛܗ㋴ⱘϔ㓈᭄㒘ˈ݊Ё᭄㒘ⱘ↣Ͼܗᅗ㸼⼎ 1ineptr ᰃϔϾ݋᳝ MAXLINES Ͼ char *lineptr[MAXLINES]; ೼䆹՟ᄤЁˈᣛ䩜᭄㒘 1ineptr ⱘໄᯢᰃᮄߎ⦄ⱘ䞡㽕ὖᗉ˖ ᳝݇ߑ᭄ getline ⱘ䆺㒚ֵᙃখ㾕 1.9 㡖DŽ { w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! *pmonth = i; yearday •= daytab[leap][i]; for (i = 1; yearday > daytab[leap][i]; i++) leap = year%4 == 0 && year%100 != 0 || year%400 == 0; int i, leap; { void month_day(int year, int yearday, int *pmonth, int *pday) /* month_day: set month, day from day of year */ } return day; day += daytab[leap][i]; for (i = 1; i < month; i++) leap = year%4 == 0 && year%100 != 0 || year%400 == 0; int i, leap; { int day_of_year(int year, int month, int day) /* day_of_year: set day of year from month & day */ }; {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, static char daytab[2][13] = { ᮁ 2 ᳜᳝໮ᇥ໽᳈ᆍᯧDŽ䆹᭄㒘ҹঞᠻ㸠᮹ᳳ䕀ᤶⱘߑ᭄བϟ᠔⼎˖ ↣Ͼ᳜ⱘ໽᭄ϡৠˈ᠔ҹˈᇚ䖭ѯ໽᭄ߚ߿ᄬᬒ೼ϔϾѠ㓈᭄㒘ⱘϸ㸠Ё↨೼䅵ㅫ䖛⿟Ё߸ 䖭ѯߑ᭄䛑㽕⫼ࠄϔᓴ䆄ᔩ↣᳜໽᭄ⱘ㸼˄བ“9 ᳜᳝ 30 ໽āㄝ˅DŽᇍ䯄ᑈ੠䴲䯄ᑈᴹ䇈ˈ ᇚᡞ m ⱘؐ䆒㕂Ў 2ˈᡞ d ⱘؐ䆒㕂Ў 29˄2 ᳜ 29 ᮹ ˅DŽ month_day(1988, 60, &m, &d); ؐˈ᠔ҹ೼ߑ᭄ month_day Ёˈ᳜੠᮹䖭ϸϾখ᭄Փ⫼ᣛ䩜ⱘᔶᓣDŽ՟བˈϟ߫䇁হ˖ ϔᑈЁ㄀޴໽ⱘ㸼⼎ᔶᓣˈߑ᭄ month_day ߭ᠻ㸠Ⳍডⱘ䕀ᤶDŽ಴ЎৢϔϾߑ᭄㽕䖨ಲϸϾ Нϟ߫ϸϾߑ᭄ҹ䖯㸠᮹ᳳ䕀ᤶ˖ߑ᭄ day_of_year ᇚᶤ᳜ᶤ᮹ⱘ᮹ᳳ㸼⼎ᔶᓣ䕀ᤶЎᶤ ⼎ᔶᓣˈডПѺ✊DŽ՟བˈ3 ᳜ 1 ᮹ᰃ䴲䯄ᑈⱘ㄀ 60 ໽ˈᰃ䯄ᑈⱘ㄀ 61 ໽DŽ೼䖭䞠ˈ៥Ӏᅮ ៥Ӏ㗗㰥ϔϾ᮹ᳳ䕀ᤶⱘ䯂乬ˈᡞᶤ᳜ᶤ᮹䖭⾡᮹ᳳ㸼⼎ᔶᓣ䕀ᤶЎᶤᑈЁ㄀޴໽ⱘ㸼 㡖ᇚᇍ໮㓈᭄㒘ⱘ⡍ᗻ䖯㸠ҟ㒡DŽ ᣛ䩜᭄㒘Փ⫼ᕫ䙷ḋᑓ⊯DŽᴀڣC 䇁㿔ᦤկњ㉏ԐѢⶽ䰉ⱘ໮㓈᭄㒘ˈԚᅲ䰙ϞᅗӀᑊϡ 5.7. ໮㓈᭄㒘 ぎ䯈ЁDŽ䆹ߑ᭄ⱘ䖤㸠䗳ᑺ↨ᬍݭࠡᖿ໮ᇥ˛ټࠄ䇗⫼ alloc ߚ䜡ⱘᄬټ㒘Ёˈ㗠ϡᰃᄬ ࠄ⬅ main ߑ᭄ᦤկⱘϔϾ᭄ټ㒗д 5•7 䞡ݭߑ᭄ readlinesˈᇚ䕧ܹⱘ᭛ᴀ㸠ᄬ ㋴П䯈ৃҹѦⳌ໡ࠊDŽܗtemp Ϣ v ⱘӏᛣ ㋴䛑ᰃᄫヺᣛ䩜ˈᑊϨ temp гᖙ乏ᰃᄫヺᣛ䩜ˈ಴ℸܗ಴Ў v˄߿ৡЎ lineptr˅ⱘ᠔᳝ } v[j] = temp; v[i] = v[j]; temp = v[i]; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䆹䯂乬DŽއ㒗д 5•8 ߑ᭄ day_of_year ੠ month_day Ё≵᳝䖯㸠䫭䇃Ẕᶹˈ䇋㾷 ៥Ӏᇚ೼ 5.12 㡖Ё䖯ϔℹ䅼䆎᳈໡ᴖⱘໄᯢDŽ ϔ㠀ᴹ䇈ˈ䰸᭄㒘ⱘ㄀ϔ㓈˄ϟᷛ˅ৃҹϡᣛᅮ໻ᇣ໪ˈ݊ԭ৘㓈䛑ᖙ乏ᯢ⹂ᣛᅮ໻ᇣDŽ ㋴䛑ᰃϔϾᣛ৥ᭈൟᇍ䈵ⱘᣛ䩜DŽܗ㋴ˈ݊Ё↣Ͼܗ䖭ⳌᔧѢໄᯢњϔϾ᭄㒘ˈ䆹᭄㒘᳝ 13 Ͼ int *daytab[13] 㑻ˈ᠔ҹϞ䗄ໄᯢЁᖙ乏Փ⫼೚ᣀোDŽབᵰএᥝᣀোˈ߭ໄᯢব៤ܜ㑻催Ѣ*ⱘӬܜӬ ㋴ⱘϔ㓈᭄㒘DŽ಴Ўᮍᣀো[]ⱘܗ䖭⾡ໄᯢᔶᓣ㸼ᯢখ᭄ᰃϔϾᣛ䩜ˈᅗᣛ৥݋᳝ 13 Ͼᭈൟ f(int (*daytab)[13]) { ... } ಴Ў᭄㒘ⱘ㸠᭄᮴݇㋻㽕ˈ᠔ҹˈ䆹ໄᯢ䖬ৃҹݭ៤ f(int daytab[][13]) { ... } гৃҹݭ៤ f(int daytab[2][13]) { ... } ℸˈབᵰᇚ᭄㒘 daytab ԰Ўখ᭄Ӵ䗦㒭ߑ᭄ fˈ䙷М f ⱘໄᯢᑨ䆹ݭ៤ϟ߫ᔶᓣ˖ ㋴ᵘ៤ⱘϔ㓈᭄㒘DŽ಴ܗ㒭ߑ᭄ⱘᰃϔϾᣛ৥ᕜ໮ᇍ䈵ⱘᣛ䩜ˈ݊Ё↣Ͼᇍ䈵ᰃ⬅ 13 Ͼᭈൟ ㋴ⱘϔ㓈᭄㒘DŽ೼䆹՟ᄤЁˈӴ䗦ܗ৥䞣ᵘ៤ⱘϔ㓈᭄㒘ˈ݊Ё↣Ͼ㸠৥䞣ᰃ݋᳝ 13 Ͼᭈൟ ᭄㒘ⱘ㸠᭄≵᳝໾໻݇㋏ˈ಴Ўࠡ䴶Ꮖ㒣䆆䖛ˈߑ᭄䇗⫼ᯊӴ䗦ⱘᰃϔϾᣛ䩜ˈᅗᣛ৥⬅㸠 བᵰᇚѠ㓈᭄㒘԰Ўখ᭄Ӵ䗦㒭ߑ᭄ˈ䙷М೼ߑ᭄ⱘখ᭄ໄᯢЁᖙ乏ᣛᯢ᭄㒘ⱘ᭄߫DŽ ⱘϟᷛ᳈ࡴⳈ㾖DŽ ぎ䯈ᑊϡᰃЏ㽕䯂乬ˈ᠔ҹ䖭⾡໘⧚ᮍᓣ↨೼⿟ᑣЁ䇗ᭈ᭄㒘ټ㗠ϡᰃ 0̚11DŽ⬅Ѣ೼䖭䞠ᄬ ㋴䆒㕂Ў 0ˈ䖭ḋˈ᳜ӑⱘؐЎ 1̚12ˈܗ߱ྟ࣪DŽ೼ᴀ՟Ёˈ៥Ӏᇚ᭄㒘 daytab ⱘ㄀ϔ߫ ᭄㒘ৃҹ⫼㢅ᣀোᣀ䍋ᴹⱘ߱ؐ㸼䖯㸠߱ྟ࣪ˈѠ㓈᭄㒘ⱘ↣ϔ㸠⬅Ⳍᑨⱘᄤ߫㸼䖯㸠 乎ᑣ䆓䯂᭄㒘ᯊˈ᳔ে䖍ⱘ᭄㒘ϟᷛ˄े߫˅ব࣪ᕫ᳔ᖿDŽټ಴ℸˈᔧᣝᄬ ˈټ㋴ᣝ㸠ᄬܗ䰸њ㸼⼎ᮍᓣⱘऎ߿໪ˈC 䇁㿔ЁѠ㓈᭄㒘ⱘՓ⫼ᮍᓣ੠݊ᅗ䇁㿔ϔḋDŽ᭄㒘 daytab[i,j] /* WRONG */ 㗠ϡ㛑ݭ៤ daytab[i][j] /* [row][col] */ ㋴гᰃϔϾϔ㓈᭄㒘DŽ಴ℸˈ᭄㒘ϟᷛᑨ䆹ݭ៤ܗϔ⾡⡍⅞ⱘϔ㓈᭄㒘ˈᅗⱘ↣Ͼ ࠄⳂࠡЎℶˈdaytab ᰃ៥Ӏ䘛ࠄⱘ㄀ϔϾѠ㓈᭄㒘DŽ೼ C 䇁㿔ЁˈѠ㓈᭄㒘ᅲ䰙Ϟᰃ char ㉏ൟⱘব䞣Ёᄬᬒ䕗ᇣⱘ䴲ᄫヺᭈ᭄гᰃড়⊩ⱘDŽ ㋴ໄᯢЎ char ㉏ൟˈᰃЎњ䇈ᯢ೼ܗϾߑ᭄䛑ৃҹՓ⫼䆹᭄㒘DŽ䖭䞠П᠔ҹᇚ daytab ⱘ ᭄㒘 daytab ᖙ乏೼ߑ᭄ day_of_year ੠ month_day ⱘ໪䚼䖯㸠ໄᯢˈ䖭ḋˈ䖭ϸ ᭄㒘 daytab ⱘϟᷛDŽخ⫼ ಴ℸˈ೼ᴀ՟Ёˈৃҹᇚ䘏䕥㸼䖒ᓣ leap ᯊ˅៪㗙 1˄Ўⳳᯊ˅DŽ؛៥Ӏ೼ࠡ䴶ⱘゴ㡖Ё᳒䆆䖛ˈ䘏䕥㸼䖒ᓣⱘㅫᴃ䖤ㅫؐাৃ㛑ᰃ 0˄Ў } pday = yearday;* w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! char *name[]={"Illegal manth", "Jan", "Feb", "Mar"}; ϔϾ↨䕗DŽϟ䴶ᰃᣛ䩜᭄㒘ⱘໄᯢ੠೒ᔶ࣪ᦣ䗄˄খ㾕೒ 5•9˅˖خᔶ࣪ᦣ䗄ˈ៥Ӏৃҹ DŽ㒧ড়ϟ䴶ⱘໄᯢ੠೒މ⫼໘ᰃᄬᬒ݋᳝ϡৠ䭓ᑺⱘᄫヺІˈ↨བߑ᭄ month_name Ёⱘᚙ ࡽѢᭈൟ䖯㸠䅼䆎ˈԚࠄⳂࠡЎℶˈᣛ䩜᭄㒘᳔乥㐕ⱘ׳ሑㅵ៥Ӏ೼Ϟ䴶ⱘ䅼䆎Ё䛑ᰃ ㋴ৃҹϡᣛ৥ӏԩ৥䞣DŽܗ㋴ⱘ৥䞣ˈ㗠ᶤѯܗᣛ৥݋᳝ 50 Ͼ ㋴ৃҹܗ㋴ⱘ৥䞣ˈᶤѯܗ㋴ৃҹᣛ৥݋᳝ 2 Ͼܗ㋴ⱘ৥䞣ˈᶤѯܗᖙ䛑ᣛ৥ϔϾ݋᳝ 20 Ͼ ㋴ϡܗ䯈DŽᣛ䩜᭄㒘ⱘϔϾ䞡㽕Ӭ⚍೼Ѣˈ᭄㒘ⱘ↣ϔ㸠䭓ᑺৃҹϡৠˈгህᰃ䇈ˈb ⱘ↣Ͼ ぎټぎ䯈ҹঞ 10 Ͼᣛ䩜ⱘᄬټ㋴ⱘ᭄㒘ˈ䙷М㓪䆥఼ህ㽕Ўᅗߚ䜡 200 Ͼ int ㉏ൟ䭓ᑺⱘᄬ ܗ㋴䛑ᣛ৥ϔϾ݋᳝ 20 Ͼܗᅮ b ⱘ↣Ͼ؛ⱘᮍᓣ䖯㸠ˈ↨བ䴭ᗕ߱ྟ࣪៪䗮䖛ҷⷕ߱ྟ࣪DŽ ᇍ b ᴹ䇈ˈ䆹ᅮНҙҙߚ䜡њ 10 Ͼᣛ䩜ˈᑊϨ≵᳝ᇍᅗӀ߱ྟ࣪ˈᅗӀⱘ߱ྟ࣪ᖙ乏ҹᰒᓣ ㋴ a[row][col]ⱘԡ㕂DŽԚᰃˈܗ݀ᓣ 20×row+col˄݊Ёˈrow 㸼⼎㸠ˈcol 㸼⼎߫˅䅵ㅫᕫࠄ ぎ䯈ˈᑊϨ䗮䖛ᐌ㾘ⱘⶽ䰉ϟᷛ䅵ㅫټⳳℷⱘѠ㓈᭄㒘ˈᅗߚ䜡њ 200 Ͼ int ㉏ൟ䭓ᑺⱘᄬ 䙷МˈҢ䇁⊩㾦ᑺ䆆ˈa[3][4]੠ b[3][4]䛑ᰃᇍϔϾ int ᇍ䈵ⱘড়⊩ᓩ⫼DŽԚ a ᰃϔϾ int *b[10]; int a[10][20]; བ᳝ϟ䴶ϸϾᅮН˖؛Ёⱘ nameDŽ ᇍѢ C 䇁㿔ⱘ߱ᄺ㗙ᴹ䇈ˈᕜᆍᯧ⏋⎚Ѡ㓈᭄㒘Ϣᣛ䩜᭄㒘П䯈ⱘऎ߿ˈ↨བϞ䴶՟ᄤ 5.9. ᣛ䩜Ϣ໮㓈᭄㒘 ⹂᭄ᄫ฿᭄ܹ㒘ⱘ䭓ᑺDŽޚ㸠㒳䅵ˈᑊᇚ䖭ϔ name[i]ЁDŽ⬅ѢϞ䗄ໄᯢЁ≵᳝ᣛᯢ name ⱘ䭓ᑺˈ಴ℸˈ㓪䆥఼㓪䆥ᯊᇚᇍ߱ؐϾ᭄䖯 ೼ټ఼ЁⱘᶤϾԡ㕂ˈᣛ৥ᅗⱘᣛ䩜ᄬټ೼ᄬټ㋴DŽ㄀ i ϾᄫヺІⱘ᠔᳝ᄫヺᄬܗԡ㕂ⱘ ヺᣛ䩜DŽname ᭄㒘ⱘ߱ྟ࣪䗮䖛ϔϾᄫヺІ߫㸼ᅲ⦄ˈ߫㸼Ёⱘ↣ϾᄫヺІ䌟ؐ㒭᭄㒘Ⳍᑨ ㋴Ўᄫܗ݊Ёˈname ⱘໄᯢϢᥦᑣ՟ᄤЁ lineptr ⱘໄᯢⳌৠˈᰃϔϾϔ㓈᭄㒘ˈ᭄㒘ⱘ }; "October", "November", "December" "July", "August", "September", "April", "May", "June", "January", "February", "March", "Illegal month", static char *name[] = { { char *month_name(int n) /* month_name: return name of n•th month */ ᣛ䩜᭄㒘ⱘ߱ྟ࣪䇁⊩੠ࠡ䴶᠔䆆ⱘ݊ᅗ㉏ൟᇍ䈵ⱘ߱ྟ࣪䇁⊩㉏Ԑ˖ ࣪䆹ৡᄫ᭄㒘DŽ ㋴ⱘᣛ䩜DŽᴀ㡖ᇚ䇈ᯢབԩ߱ྟܗϾ⾕᳝ⱘᄫヺІ᭄㒘ˈᔧᅗ㹿䇗⫼ᯊˈ䖨ಲϔϾᣛ৥ℷ⹂ ᄫヺІⱘᣛ䩜DŽ䖭ᰃݙ䚼 static ㉏ൟ᭄㒘ⱘϔ⾡⧚ᛇⱘᑨ⫼DŽmonth_name ߑ᭄Ёࣙ৿ϔ 㗗㰥䖭ḋϔϾ䯂乬˖㓪ݭϔϾߑ᭄ month_name(n)ˈᅗ䖨ಲϔϾᣛ৥㄀ n Ͼ᳜ৡᄫⱘ ᣛ䩜᭄㒘ⱘ߱ྟ࣪ .5.8 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! argv[argc]ⱘؐᖙ乏Ўϔぎᣛ䩜(খ㾕೒ 5•11)DŽ 㽕∖ޚϾৃ䗝খ᭄Ў argv[1]ˈ㗠᳔ৢϔϾৃ䗝খ᭄Ў argv[argc•1]DŽ঺໪ˈANSI ᷛ argv[0]ǃargv[1]੠ argv[2]ⱘؐߚ߿Ў“echoāǃ “ hello,āˈҹঞ“worldāDŽ㄀ϔ བᵰ argc ⱘؐЎ 1ˈ߭䇈ᯢ⿟ᑣৡৢ䴶≵᳝ੑҸ㸠খ᭄DŽ೼Ϟ䴶ⱘ՟ᄤЁˈargc ⱘؐЎ 3ˈ ᣝ✻ C 䇁㿔ⱘ㑺ᅮˈargv[0]ⱘؐᰃਃࡼ䆹⿟ᑣⱘ⿟ᑣৡˈ಴ℸ argc ⱘؐ㟇ᇥЎ 1DŽ hello, world ᇚᠧॄϟ߫䕧ߎ˖ echo hello, world ᭄П䯈⫼ぎḐ䱨ᓔDŽгህᰃ䇈ˈੑҸ ᳔ㅔऩⱘ՟ᄤᰃ⿟ᑣ echoˈᅗᇚੑҸ㸠খ᭄ಲᰒ೼ሣᐩϞⱘϔ㸠Ёˈ݊ЁੑҸ㸠Ё৘খ 㒘ⱘᣛ䩜ˈ݊Ё↣ϾᄫヺІᇍᑨϔϾখ᭄DŽ៥Ӏ䗮ᐌ⫼໮㑻ᣛ䩜໘⧚䖭ѯᄫヺІDŽ ⿟ᑣᯊੑҸ㸠Ёখ᭄ⱘ᭄Ⳃ˗㄀ѠϾখ᭄˄⿄Ў argvˈ⫼Ѣখ᭄৥䞣˅ᰃϔϾᣛ৥ᄫヺІ᭄ main ᯊˈᅗᏺ᳝ϸϾখ᭄DŽ㄀ϔϾখ᭄˄дᛃϞ⿄Ў argcˈ⫼Ѣখ᭄䅵᭄˅ⱘؐ㸼⼎䖤㸠 ೼ᬃᣕ C 䇁㿔ⱘ⦃๗Ёˈৃҹ೼⿟ᑣᓔྟᠻ㸠ᯊᇚੑҸ㸠খ᭄Ӵ䗦㒭⿟ᑣDŽ䇗⫼Џߑ᭄ 5.10. ੑҸ㸠খ᭄ 㒗д 5•9 ⫼ᣛ䩜ᮍᓣҷ᳓᭄㒘ϟᷛᮍᓣᬍݭߑ᭄ day_of_year ੠ month_dayDŽ ೒ 5•10 char aname[][15] = { "Illegal month", "Jan", "Feb", "Mar" }; ϟ䴶ᰃѠ㓈᭄㒘ⱘໄᯢ੠೒ᔶ࣪ᦣ䗄˄খ㾕೒ 5•10˅˖ ೒ 5•9 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! char line[MAXLINE]; { main(int argc, char *argv[]) /* find: print lines that match pattern from 1st arg */ int getline(char *line, int max); #define MAXLINE 1000 #include #include ⿟ᑣ grep ⱘᅲ⦄ᮍ⊩ᬍݭ῵ᓣᶹᡒ⿟ᑣˈ䗮䖛ੑҸ㸠ⱘ㄀ϔϾখ᭄ᣛᅮᕙऍ䜡ⱘ῵ᓣDŽ ᮍ⊩ᰒ✊ϡ㛑ҸҎ⒵ᛣDŽϟ䴶៥Ӏᴹᬜӓ UNIXއЁˈ៥Ӏᇚᶹᡒ῵ᓣݙ㕂ࠄ⿟ᑣЁњˈ䖭⾡㾷 ៥Ӏᴹⳟ㄀ѠϾ՟ᄤDŽ೼䆹՟ᄤЁˈ៥Ӏᇚ๲ᔎ 4.1 㡖Ё῵ᓣᶹᡒ⿟ᑣⱘࡳ㛑DŽ೼ 4.1 㡖 䖭ህ䇈ᯢˈprintf ⱘḐᓣ࣪খ᭄гৃҹᰃ㸼䖒ᓣDŽ printf((argc > 1) ? "%s " : "%s”, *++argv); гৃҹᇚ printf 䇁হݭ៤ϟ߫ᔶᓣ˖ ៤њ᠔᳝খ᭄ⱘᠧॄDŽ 䖤ㅫˈᔧᅗব៤ 0 ᯊˈህᅠޣ᭄ˈ*argv ህᰃᣛ৥䙷Ͼখ᭄ⱘᣛ䩜DŽϢℸৠᯊˈargc ᠻ㸠㞾 ᅗ೼᳔ᓔྟᣛ৥ argv[1]㗠䴲 argv[0]DŽ↣ᠻ㸠ϔ⃵㞾๲䖤ㅫˈህՓᕫ argv ᣛ৥ϟϔϾখ ಴Ў argv ᰃϔϾᣛ৥খ᭄ᄫヺІ᭄㒘䍋ྟԡ㕂ⱘᣛ䩜ˈ᠔ҹˈ㞾๲䖤ㅫ˄++argv˅ᇚՓᕫ } return 0; printf("\n"); printf("%s%s", *++argv, (argc > 1) ? " " : ""); while (••argc > 0) { main(int argc, char *argv[]) /* echo command•line arguments; 2nd version */ #include ᅲ⦄ⱘˈ݊Ё argv ᰃϔϾᣛ৥ char ㉏ൟⱘᣛ䩜ⱘᣛ䩜˖ 䖤ㅫⱘ෎⸔Ϟޣখ᭄DŽecho ⿟ᑣⱘ㄀ѠϾ⠜ᴀᰃ೼ᇍ argv 䖯㸠㞾๲䖤ㅫǃᇍ argc 䖯㸠㞾 ಴Ў argv ᰃϔϾᣛ৥ᣛ䩜᭄㒘ⱘᣛ䩜ˈ᠔ҹˈৃҹ䗮䖛ᣛ䩜㗠䴲᭄㒘ϟᷛⱘᮍᓣ໘⧚ੑҸ㸠 } return 0; printf("\n"); printf("%s%s", argv[i], (i < argc•1) ? " " : ""); for (i = 1; i < argc; i++) int i; { main(int argc, char *argv[]) /* echo command•line arguments; 1st version */ #include ⿟ᑣ echo ⱘ㄀ϔϾ⠜ᴀᇚ argv ⳟ៤ᰃϔϾᄫヺᣛ䩜᭄㒘˖ ೒ 5•11 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! number = 1; case 'n': break; except = 1; case 'x': switch (c) { while (c = *++argv[0]) while (••argc > 0 && (*++argv)[0] == '•') int c, except = 0, number = 0, found = 0; long lineno = 0; char line[MAXLINE]; { main(int argc, char *argv[]) /* find: print lines that match pattern from 1st arg */ int getline(char *line, int max); #define MAXLINE 1000 #include #include ᬍݭৢⱘ῵ᓣᶹᡒ⿟ᑣབϟ᠔⼎˖ find •nx ῵ᓣ ᮴݇DŽℸ໪ˈབᵰৃ䗝খ᭄㛑໳㒘ড়Փ⫼ˈᇚӮ㒭Փ⫼㗙ᏺᴹ᳈໻ⱘᮍ֓ˈ↨བ˖ 䆌ҹӏᛣ⃵ᑣߎ⦄ˈৠᯊˈ⿟ᑣⱘ݊ԭ䚼ߚᑨ䆹ϢੑҸ㸠Ёখ᭄ⱘ᭄Ⳃܕৃ䗝খ᭄ᑨ䆹 ᇚᠧॄ᠔᳝Ϣ῵ᓣϡऍ䜡ⱘ㸠ˈᑊ೼↣Ͼᠧॄ㸠ⱘࠡ䴶ࡴϞ㸠োDŽ find •x •n ῵ᓣ োā˅㸼⼎ᠧॄ㸠োˈ䙷Мϟ߫ੑҸ˖ ᅮ⫼•x˄ҷ㸼Ā䰸……П໪ā˅㸼⼎ᠧॄ᠔᳝Ϣ῵ᓣϡऍ䜡ⱘ᭛ᴀ㸠ˈ⫼•n˄ҷ㸼Ā㸠؛᭄DŽ UNIX ㋏㒳Ёⱘ C 䇁㿔⿟ᑣ᳝ϔϾ݀݅ⱘ㑺ᅮ˖ҹ䋳োᓔ༈ⱘখ᭄㸼⼎ϔϾৃ䗝ᷛᖫ៪খ 㸠ࠡ䴶ࡴϞⳌᑨⱘ㸠োāDŽ ᭄DŽ݊ЁϔϾখ᭄㸼⼎Āᠧॄ䰸ऍ䜡῵ᓣП໪ⱘ᠔᳝㸠āˈ঺ϔϾখ᭄㸼⼎Ā↣Ͼᠧॄⱘ᭛ᴀ 䆌⿟ᑣᏺϸϾৃ䗝খܕᅮ؛Ўњ᳈䖯ϔℹഄ㾷䞞ᣛ䩜㒧ᵘˈ៥Ӏᴹᬍ䖯῵ᓣᶹᡒ⿟ᑣDŽ ӊЁDŽ ԡ㕂˗བᵰᄫヺІ t ≵᳝೼ᄫヺІ s Ёߎ⦄ˈߑ᭄䖨ಲ NULL˄ぎᣛ䩜˅DŽ䆹ߑ᭄ໄᯢ೼༈᭛ ᑧߑ᭄ strstr(s, t) 䖨ಲϔϾᣛ䩜ˈ䆹ᣛ䩜ᣛ৥ᄫヺІ t ೼ᄫヺІ s Ё㄀ϔ⃵ߎ⦄ⱘޚᷛ } return found; } found++; printf("%s", line); if (strstr(line, argv[1]) != NULL) { while (getline(line, MAXLINE) > 0) else printf("Usage: find pattern\n"); if (argc != 2) int found = 0; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᇚᠧॄ݊䕧ܹⱘ᳔ৢ n 㸠DŽ᮴䆎䕧ܹ៪ n ⱘؐᰃ৺ড়⧚ˈ䆹⿟ᑣ䛑ᑨ䆹㛑ℷᐌ䖤㸠DŽ㓪 tail •n 10ˈԚৃ䗮䖛ϔϾৃ䗝খ᭄ᬍব n ⱘؐˈ಴ℸˈੑҸ ϟˈn ⱘؐЎމ㒗д 5•13 㓪ݭ⿟ᑣ tailˈᇚ݊䕧ܹЁⱘ᳔ৢ n 㸠ᠧॄߎᴹDŽ咬䅸ᚙ ℶDŽ䗝ᢽ˄ᇍՓ⫼㗙㗠㿔˅↨䕗ᮍ֓ⱘ咬䅸㸠ЎDŽذ㸼⼎ࠊ㸼ヺҢ㄀ m ߫ᓔྟˈ↣䱨 n ߫ entab –m +n ҹ᥹ফϟ߫㓽ݭⱘੑҸ˖ˈܙϔѯᠽخ㒗д 5•12 ᇍ⿟ᑣ entab ੠ detab ⱘࡳ㛑 ℶԡ䆒㕂DŽذℶԡDŽབᵰਃࡼ⿟ᑣᯊϡᏺখ᭄ˈ߭Փ⫼咬䅸ⱘࠊ㸼ヺذЎখ᭄ⱘࠊ㸼ヺ 㒗д 5•11 ׂᬍ⿟ᑣ entab ੠ decab˄㄀ 1 ゴ㒗дЁ㓪ݭⱘߑ᭄˅ˈՓᅗӀ᥹ফϔ㒘԰ ᇚ䅵ㅫ㸼䖒ᓣ 2 × (3 + 4)ⱘؐDŽ expr 2 3 4 + * ヺ៪᪡԰᭄⫼ϔϾऩ⣀ⱘখ᭄㸼⼎DŽ՟བˈੑҸ 㒗д 5•10 㓪ݭ⿟ᑣ exprˈҹ䅵ㅫҢੑҸ㸠䕧ܹⱘ䗚⊶݄㸼䖒ᓣⱘؐˈ݊Ё↣Ͼ䖤ㅫ ℹᴹ⧚㾷ˈ䖭ḋӮ᳈Ⳉ㾖ϔѯDŽ ҹᇚᅗӀߚЎϸℹ៪ϝৃˈމᕜᇥ᳝ҎՓ⫼↨䖭᳈໡ᴖⱘᣛ䩜㸼䖒ᓣDŽབᵰ䘛ࠄ䖭⾡ᚙ ᇍᣛ䩜 argv[0]䖯㸠њ㞾๲䖤ㅫDŽ 㸼䖒ᓣ*++argv[0]ˈ݊Ⳃⱘᰃ䘡ग़ϔϾ⡍ᅮⱘখ᭄ІDŽ೼ݙሖᕾ⦃Ёˈ㸼䖒ᓣ*++argv[0] argv[0])DŽᅲ䰙Ϟˈ៥Ӏ೼ݙሖᕾ⦃ЁህՓ⫼њ)++*خোˈ৺߭㓪䆥఼ᇚӮᡞ䆹㸼䖒ᓣᔧ 㑻↨*੠++催ˈ᠔ҹ೼Ϟ䗄㸼䖒ᓣЁᖙ乏Փ⫼೚ᣀܜ**++argv˅DŽ಴Ў[]Ϣ᪡԰᭄ⱘ㒧ড়Ӭ ϔϾᣛ৥খ᭄ᄫヺІⱘᣛᓩˈ಴ℸ(*++argv)[0]ᰃᅗⱘ㄀ϔϾᄫヺ˄঺ϔ⾡᳝ᬜᔶᓣᰃ ⱘ㄀ϔϾখ᭄DŽ಴ℸˈ䖭ᯊ argc ⱘؐᑨЎ 1ˈ㗠*argv ᑨ䆹ᣛ৥῵ᓣDŽ⊼ᛣˈ*++argv ᰃ བᵰ≵᳝䫭䇃ˈ߭ argc ⱘؐ㸼⼎䖬≵᳝໘⧚ⱘখ᭄᭄Ⳃˈ㗠 argv ߭ᣛ৥䖭ѯ᳾໘⧚খ᭄Ё 䖤ㅫˈargv ᠻ㸠㞾๲䖤ㅫDŽᕾ⦃䇁হ㒧ᴳᯊˈޣ೼໘⧚↣Ͼৃ䗝খ᭄Пࠡˈargc ᠻ㸠㞾 } return found; } } found++; printf("%s", line); printf("%ld:", lineno); if (number) if ((strstr(line, *argv) != NULL) != except) { lineno++; while (getline(line, MAXLINE) > 0) { else printf("Usage: find •x •n pattern\n"); if (argc != 1) } break; found = •1; argc = 0; printf("find: illegal option %c\n", c); default: break; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ೼䇗⫼ߑ᭄ qsort ⱘ䇁হЁˈstrcmp ੠ numcmp ᰃߑ᭄ⱘഄഔDŽ಴ЎᅗӀᰃߑ᭄ˈ᠔ } } return 1; printf("input too big to sort\n"); } else { return 0; writelines(lineptr, nlines); (int (*)(void*,void*))(numeric ? numcmp : strcmp)); qsort((void**) lineptr, 0, nlines•1, if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { numeric = 1; if (argc > 1 && strcmp(argv[1], "•n") == 0) int numeric = 0; /* 1 if numeric sort */ int nlines; /* number of input lines read */ { main(int argc, char *argv[]) /* sort input lines */ int numcmp(char *, char *); int (*comp)(void *, void *)); void qsort(void *lineptr[], int left, int right, void writelines(char *lineptr[], int nlines); int readlines(char *lineptr[], int nlines); char *lineptr[MAXLINES]; /* pointers to text lines */ #define MAXLINES 5000 /* max #lines to be sorted */ #include #include ߎ䫭໘⧚ᑊϡᰃ䯂乬ⱘ䞡⚍ˈ៥ӀᇚЏ㽕㗗㰥ᣛ৥ߑ᭄ⱘᣛ䩜䯂乬DŽ ᭄೼ main ПࠡໄᯢˈᑊϨˈᣛ৥ᙄᔧߑ᭄ⱘᣛ䩜ᇚ㹿Ӵ䗦㒭 qsort ߑ᭄DŽ೼䖭䞠ˈখ᭄ⱘ ҹ᭄ؐЎ෎⸔ᴹ↨䕗ϸϾ䕧ܹ㸠ˈᑊ䖨ಲϢ strcmp ৠḋⱘ↨䕗㒧ᵰⱘߑ᭄ numcmpDŽ䖭ѯߑ ៥Ӏ೼ࠡ䴶䆆䖛ˈߑ᭄ strcmp ᣝᄫ݌乎ᑣ↨䕗ϸϾ䕧ܹ㸠DŽ೼䖭䞠ˈ៥Ӏ䖬䳔㽕ϔϾ ᥦᑣDŽ䖭ህᰃ៥Ӏⱘᮄ⠜ᴀᥦᑣߑ᭄᠔䞛⫼ⱘᮍ⊩DŽޚ⦄ᣝ✻ϡৠⱘᷛ ⊩Ϣ↨䕗ǃѸᤶ᪡԰᮴݇ˈ಴ℸˈ䗮䖛೼ᥦᑣㅫ⊩Ё䇗⫼ϡৠⱘ↨䕗੠Ѹᤶߑ᭄ˈ֓ৃҹᅲ ᤶ᪡԰ǃϔϾ⫼Ѣ↨䕗੠Ѹᤶᇍ䈵Ⳉࠄ᠔᳝ᇍ䈵䛑ᣝℷ⹂⃵ᑣᥦ߫ⱘᥦᑣㅫ⊩DŽ⬅Ѣᥦᑣㅫ ᇍ䈵⃵ᑣⱘѸצᥦᑣ⿟ᑣ䗮ᐌࣙᣀ 3 䚼ߚ˖߸ᮁӏԩϸϾᇍ䈵П䯈⃵ᑣⱘ↨䕗᪡԰ǃ乴 ؐ໻ᇣ㗠䴲ᄫ݌乎ᑣᇍ䕧ܹ㸠䖯㸠ᥦᑣDŽ ϟˈ䆹ߑ᭄ᇚᣝ᭄މ⫼⊩ˈ៥Ӏ᥹ϟᴹᇚׂᬍᴀゴࠡ䴶ⱘᥦᑣߑ᭄ˈ೼㒭ᅮৃ䗝খ᭄•n ⱘᚙ 䌟ؐǃᄬᬒ೼᭄㒘ЁǃӴ䗦㒭ߑ᭄ҹঞ԰Ўߑ᭄ⱘ䖨ಲؐㄝㄝDŽЎњ䇈ᯢᣛ৥ߑ᭄ⱘᣛ䩜ⱘ ೼ C 䇁㿔Ёˈߑ᭄ᴀ䑿ϡᰃব䞣ˈԚৃҹᅮНᣛ৥ߑ᭄ⱘᣛ䩜DŽ䖭⾡㉏ൟⱘᣛ䩜ৃҹ㹿 5.11. ᣛ৥ߑ᭄ⱘᣛ䩜 ḋˈ㗠ϡ䞛⫼೎ᅮ䭓ᑺⱘѠ㓈᭄㒘DŽ ᮍᓣϔټᮍᓣᑨ䆹ৠ 5.6 㡖Ёᥦᑣ⿟ᑣⱘᄬټぎ䯈˗䕧ܹ㸠ⱘᄬټߚഄ߽⫼ᄬܙݭⱘ⿟ᑣ㽕 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! #include 䕗ϸϾᄫヺІˈԚᅗ䗮䖛䇗⫼ atof 䅵ㅫᄫヺІᇍᑨⱘ᭄ؐˈ✊ৢ೼ℸ෎⸔Ϟ䖯㸠↨䕗˖ ៥Ӏ೼ࠡ䴶䆆䖛ߑ᭄ strcmpˈऴ⫼Ѣ↨䕗ϸϾᄫヺІDŽ䖭䞠ҟ㒡ⱘߑ᭄ numcmp гᰃ↨ ໻ⱘᏂ߿DŽ ߭㸼ᯢ comp ᰃϔϾߑ᭄ˈ䆹ߑ᭄䖨ಲϔϾᣛ৥ int ㉏ൟⱘᣛ䩜ˈ䖭ৠ៥Ӏⱘᴀᛣᰒ✊᳝ᕜ int *comp(void *, void *) /* WRONG */ ៤ϟ䴶ⱘᔶᓣ˖ ݊Ёⱘ೚ᣀোᰃᖙ乏ⱘˈ䖭ḋᠡ㛑໳ֱ䆕݊Ёⱘ৘Ͼ䚼ߚℷ⹂㒧ড়DŽབᵰ≵᳝ᣀোˈ՟བݭ (*comp)(v[i], v[left]) 䇁হᰃᇍ䆹ߑ᭄䖯㸠䇗⫼˖ comp ⱘՓ⫼੠݊ໄᯢᰃϔ㟈ⱘˈcomp ᰃϔϾᣛ৥ߑ᭄ⱘᣛ䩜ˈ*comp ҷ㸼ϔϾߑ᭄DŽϟ߫ if ((*comp)(v[i], v[left]) < 0) ೼ϟ߫䇁হЁ˖ intDŽ ᅗ㸼ᯢ comp ᰃϔϾᣛ৥ߑ᭄ⱘᣛ䩜ˈ䆹ߑ᭄݋᳝ϸϾ void * ㉏ൟⱘখ᭄ˈ݊䖨ಲؐ㉏ൟЎ int (*comp)(void *, void *) ៥ӀҨ㒚ⷨおϔϟ݊ЁⱘໄᯢDŽqsort ߑ᭄ⱘ㄀ಯϾখ᭄ໄᯢབϟ˖ } qsort(v, last+1, right, comp); qsort(v, left, last•1, comp); swap(v, left, last); swap(v, ++last, i); if ((*comp)(v[i], v[left]) < 0) for (i = left+1; i <= right; i++) last = left; swap(v, left, (left + right)/2); return; /* fewer than two elements */ if (left >= right) /* do nothing if array contains */ void swap(void *v[], int, int); int i, last; { int (*comp)(void *, void *)) void qsort(void *v[], int left, int right, /* qsort: sort v[left]...v[right] into increasing order */ ડࠄ᭄᥂ⱘᅲ䰙㸼⼎ˈԚ㽕⹂ֱ㓪䆥఼ϡӮ᡹䫭DŽ ᭄ᔎࠊ䕀ᤶЎ void * ㉏ൟDŽ↨䕗ߑ᭄ⱘখ᭄г㽕ᠻ㸠䖭⾡㉏ൟⱘ䕀ᤶDŽ䖭⾡䕀ᤶ䗮ᐌϡӮᕅ *㉏ൟˈᑊϨ೼ᇚᅗ䕀ᤶಲॳᴹⱘ㉏ൟᯊϡӮ϶༅ֵᙃˈ᠔ҹˈ䇗⫼ qsort ߑ᭄ᯊৃҹᇚখ Ёˈᣛ䩜᭄㒘খ᭄ⱘ㉏ൟЎ䗮⫼ᣛ䩜㉏ൟ void *DŽ⬅Ѣӏԩ㉏ൟⱘᣛ䩜䛑ৃҹ䕀ᤶЎ void ॳൟৃҹⳟߎˈᅗⱘখ᭄㸼ࣙᣀϔϾᣛ䩜᭄㒘ǃϸϾᭈ᭄੠ϔϾ᳝ϸϾᣛ䩜খ᭄ⱘߑ᭄DŽ݊ ᬍݭৢⱘ qsort ߑ᭄㛑໳໘⧚ӏԩ᭄᥂㉏ൟˈ㗠ϡҙҙ䰤ѢᄫヺІDŽҢߑ᭄ qsort ⱘ ҹࠡ䴶ϡ䳔㽕ࡴϞপഄഔ䖤ㅫヺ&ˈৠḋⱘॳ಴ˈ᭄㒘ৡࠡ䴶гϡ䳔㽕&䖤ㅫヺDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ҹঞ int *f(); /* f: function returning pointer to int */ ໮ⱘ೚ᣀোDŽ៥Ӏᴹⳟϟ䴶᠔⼎ⱘϸϾໄᯢ˖ 䕗໡ᴖˈ߭ᆍᯧ䅽Ҏ⏋⎚ˈॳ಴೼ѢˈC 䇁㿔ⱘໄᯢϡ㛑ҢᎺ㟇ে䯙䇏ˈ㗠ϨՓ⫼њ໾↨މᚙ ᰃᕜ᳝ᬜⱘˈԚᰃˈབᵰ⫣خC 䇁㿔ⱘˈމ㿔ⱘ䇁⊩࡯೒Փໄᯢ੠Փ⫼Ⳍϔ㟈DŽᇍѢㅔऩⱘᚙ C 䇁㿔ᐌᐌ಴Ўໄᯢⱘ䇁⊩䯂乬㗠ফࠄҎӀⱘᡍ䆘ˈ⡍߿ᰃ⍝ঞࠄߑ᭄ᣛ䩜ⱘ䇁⊩DŽC 䇁 5.12. ໡ᴖໄᯢ 䗝乍ˈ㗠ᇍ义ⷕᥦᑣᯊՓ⫼њ•n 䗝 乍 DŽ˅ Ͼᄫ↉ᣝ✻ϔϾऩ⣀ⱘ䗝乍䲚ড়䖯㸠ᥦᑣDŽ˄೼ᇍᴀк㋶ᓩ䖯㸠ᥦᑣᯊˈ㋶ᓩᴵⳂՓ⫼њ•df 㒗д 5•17 ๲ࡴᄫ↉໘⧚ࡳ㛑ˈҹՓᕫᥦᑣ⿟ᑣৃҹḍ᥂㸠ݙⱘϡৠᄫ↉䖯㸠ᥦᑣˈ↣ 䕗DŽ㽕ֱ䆕䆹䗝乍ৃҹ੠•f 㒘ড়೼ϔ䍋Փ⫼DŽ 㒗д 5•16 ๲ࡴ䗝乍•d˄ҷ㸼Ⳃᔩ乎ᑣ˅DŽ䆹䗝乍㸼ᯢˈাᇍᄫ↡ǃ᭄ᄫ੠ぎḐ䖯㸠↨ ੠ A ᯊ䅸ЎᅗӀⳌㄝDŽ 㒗д 5•15 ๲ࡴ䗝乍•fˈՓᕫᥦᑣ䖛⿟ϡ㗗㰥ᄫ↡໻ᇣݭП䯈ⱘऎ߿DŽ՟བˈ↨䕗 a ᑣDŽ㽕ֱ䆕•r ੠•n 㛑໳㒘ড়೼ϔ䍋Փ⫼DŽ ᮍᓣᥦ˅ޣ㒗д 5•14 ׂᬍᥦᑣ⿟ᑣˈՓᅗ㛑໘⧚•r ᷛ䆄DŽ䆹ᷛ䆄㸼ᯢˈҹ䗚ᑣ˄䗦 䖬ৃҹᇚ݊ᅗϔѯ䗝乍๲ࡴࠄᥦᑣ⿟ᑣЁˈ᳝ѯৃҹ԰Ў䕗䲒ⱘ㒗дDŽ } v[j] = temp; v[i] = v[j]; temp = v[i]; void *temp; { void swap(void *v[], int i, int j;) ㉏ൟDŽ ѸᤶϸϾᣛᓩⱘ swap ߑ᭄੠ᴀゴࠡ䴶᠔䗄ⱘ swap ߑ᭄ⳌৠˈԚᅗⱘখ᭄ໄᯢЎ void * } return 0; else return 1; else if (v1 > v2) return •1; if (v1 < v2) v2 = atof(s2); v1 = atof(s1); double v1, v2; { int numcmp(char *s1, char *s2) numcmp: compare s1 and s2 numerically */ */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ߚᵤᷥᴹ䇈ᯢߚᵤⱘ䖛⿟˄݊Ё direct•dcl 㓽ݭЎ dir•dcl˅DŽ ݡ᥹ⴔˈ(*pfa[])()㹿䆚߿ЎϔϾ direct•dclˈ಴ℸгᰃϔϾ dclDŽৃҹ⫼೒ 5•12 ᠔⼎ⱘ䇁⊩ ᰃϔϾ direct•dclDŽ᥹ⴔˈ*pfa[]㹿䆚߿ЎϔϾ dclˈ಴ℸˈ߸ᅮ(*pfa[])ᰃϔϾ direct•dclDŽ ᣝ✻䆹䇁⊩ߚᵤˈpfa ᇚ㹿䆚߿ЎϔϾ nameˈҢ㗠㹿䅸ЎᰃϔϾ direct•dclDŽѢᰃˈpfa[]г (*pfa[])() 䆹䇁⊩ৃ⫼ᴹᇍ C 䇁㿔ⱘໄᯢ䖯㸠ߚᵤDŽ՟བˈ㗗㰥ϟ䴶ⱘໄᯢヺ˖ ৃ䗝䭓ᑺⱘ direct•dclDŽ ϔᇍ೚ᣀোᣀ䍋ᴹⱘ dclǃৢ䴶䎳᳝ϔᇍ೚ᣀোⱘ direct•dclǃৢ䴶䎳᳝⫼ᮍᣀোᣀ䍋ᴹⱘ㸼⼎ ㅔ㗠㿔Пˈໄᯢヺ dcl ህᰃࠡ䴶ৃ㛑ᏺ᳝໮Ͼ*ⱘ direct•dclDŽdirect•dcl ৃҹᰃ nameǃ⬅ direct•dcl[optional size] direct•dcl() (dcl) direct•dcl name dcl: optional *'s direct•dcl ᦣ䗄DŽϟ䴶ᰃ݊ㅔ࣪ⱘ䇁⊩ᔶᓣ˖ ⿟ᑣ dcl ᰃ෎Ѣໄᯢヺⱘ䇁⊩㓪ݭⱘDŽ䰘ᔩ A ҹঞ 8.5 㡖ᇚᇍໄᯢヺⱘ䇁⊩䖯㸠䆺㒚ⱘ pointer to array[5] of char x: array[3] of pointer to function returning char (*(*x[3])())[5] pointer to function returning char x: function returning pointer to array[] of char (*(*x())[])() comp: pointer to function returning void void (*comp)() comp: function returning pointer to void void *comp() daytab: array[13] of pointer to int int *daytab[13] daytab: pointer to array[13] of int int (*daytab)[13] argv: pointer to char char **argv ㄀ϔϾ⿟ᑣ dcl ໡ᴖϔѯDŽᅗᇚ C 䇁㿔ⱘໄᯢ䕀ᤶЎ᭛ᄫᦣ䗄ˈ↨བ˖ ᤶDŽ᭛ᄫᦣ䗄ᰃҢᎺ㟇ে䯙䇏ⱘDŽ ⫼䖭⾡ᮍ⊩˖ϔϾ⿟ᑣ⫼Ѣᇚℷ⹂ⱘ C 䇁㿔ໄᯢ䕀ᤶЎ᭛ᄫᦣ䗄ˈ঺ϔϾ⿟ᑣᅠ៤Ⳍডⱘ䕀 偸ড়៤ˈ䖭⾡ᮍ⊩៥Ӏᇚ೼ 6.7 㡖Ё䅼䆎DŽ䖭䞠ҟ㒡঺ϔ⾡ᮍ⊩DŽ᥹ϟᴹ䆆䗄ⱘϸϾ⿟ᑣህՓ ᯢᰃᕜ䞡㽕ⱘDŽབԩ߯ᓎ໡ᴖⱘໄᯢਸ਼˛ϔ⾡↨䕗དⱘᮍ⊩ᰃˈՓ⫼ typedef 䗮䖛ㅔऩⱘℹ ሑㅵᅲ䰙Ёᕜᇥ⫼ࠄ䖛Ѣ໡ᴖⱘໄᯢˈԚᰃˈពᕫབԩ⧚㾷⫮㟇བԩՓ⫼䖭ѯ໡ᴖⱘໄ ᣀোҹֱℷ⹂ⱘ㒧ড়乎ᑣDŽ 㑻ԢѢ()ˈ᠔ҹˈໄᯢЁᖙ乏Փ⫼೚ܜᅗӀП䯈ⱘ৿НᏂ߿䇈ᯢ˖*ᰃϔϾࠡ㓔䖤ㅫヺˈ݊Ӭ int (*pf)(); /* pf: pointer to function returning int */ ೒ 5•12 ⿟ᑣ dcl ⱘḌᖗᰃϸϾߑ᭄˖dcl Ϣ dirdclˈᅗӀḍ᥂ໄᯢヺⱘ䇁⊩ᇍໄᯢ䖯㸠ߚᵤDŽ ಴Ў䇁⊩ᰃ䗦ᔦᅮНⱘˈ᠔ҹ೼䆚߿ϔϾໄᯢⱘ㒘៤䚼ߚᯊˈ䖭ϸϾߑ᭄ᰃⳌѦ䗦ᔦ䇗⫼ⱘDŽ ៥Ӏ⿄䆹⿟ᑣᰃϔϾ䗦ᔦϟ䰡䇁⊩ߚᵤ⿟ᑣDŽ /* dcl: parse a declarator */ void dcl(void) { int ns; for (ns = 0; gettoken() == '*'; ) /* count *'s */ ns++; dirdcl(); while (ns•• > 0) strcat(out, " pointer to"); } /* dirdcl: parse a direct declarator */ void dirdcl(void) { int type; if (tokentype == '(') { /* ( dcl ) */ dcl(); if (tokentype != ')') printf("error: missing )\n"); } else if (tokentype == NAME) /* variable name */ strcpy(name, token); else printf("error: expected name or (dcl)\n"); while ((type=gettoken()) == PARENS || type == BRACKETS) if (type == PARENS) strcat(out, " function returning"); Click to buy NOW! PDF•XC H ANGE w w w . d o c u • t r a c k.com w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ; while ((c = getch()) == ' ' || c == '\t') char *p = token; void ungetch(int); int c, getch(void); { int gettoken(void) /* return next token */ ヺDŽ ৃҹᰃϔϾৡᄫˈϔᇍ೚ᣀোˈৃ㛑ࣙ৿ϔϾ᭄ᄫⱘϔᇍᮍᣀোˈгৃҹᰃ݊ᅗӏԩऩϾᄫ ߑ᭄ gettoken ⫼ᴹ䏇䖛ぎḐϢࠊ㸼ヺˈҹᶹᡒ䕧ܹЁⱘϟϔϾ䆄োDŽĀ䆄োā˄token˅ } return 0; } printf("%s: %s %s\n", name, out, datatype); printf("syntax error\n"); if (tokentype != '\n') dcl(); /* parse rest of line */ out[0] = '\0'; strcpy(datatype, token); /* is the datatype */ while (gettoken() != EOF) { /* 1st token on line */ { main() /* convert declaration to words */ char out[1000]; char datatype[MAXTOKEN]; /* data type = char, int, etc. */ char name[MAXTOKEN]; /* identifier name */ char token[MAXTOKEN]; /* last token string */ int tokentype; /* type of last token */ int gettoken(void); void dirdcl(void); void dcl(void); enum { NAME, PARENS, BRACKETS }; #define MAXTOKEN 100 #include #include #include ϟ䴶ᰃ䆹⿟ᑣⱘܼሔব䞣੠Џ⿟ᑣ˖ 㒗дDŽخ⊩໘⧚᮴ᬜⱘໄᯢDŽ䖭ѯᮍ䴶ⱘᬍ䖯⬭㒭䇏㗙 DŽ⬅Ѣ≵᳝ᅠ໛ⱘߎ䫭໘⧚ˈ಴ℸᅗг᮴މ䖭ḋⱘ䰤ᅮヺDŽᅗϡ㛑໘⧚ᏺ᳝ϡᖙ㽕ぎḐⱘᚙ ⧚㉏ԐѢ char ៪ int 䖭ḋⱘㅔऩ᭄᥂㉏ൟˈ㗠᮴⊩໘⧚ߑ᭄Ёⱘখ᭄㉏ൟ៪㉏ԐѢ const ᕫሑ୘ሑ㕢ˈ᠔ҹᇍ dcl ᳝ᕜ໮䰤ࠊˈᅗা㛑໘خ䆹⿟ᑣⱘⳂⱘᮼ೼䇈ᯢ䯂乬ˈᑊϡᛇ } } strcat(out, " of"); strcat(out, token); strcat(out, " array"); else { w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! } else strcpy(out, temp); sprintf(temp, "%s %s", token, out); } else if (type == NAME) { strcpy(out, temp); sprintf(temp, "(*%s)", out); else if (type == '*') { strcat(out, token); if (type == PARENS || type == BRACKETS) while ((type = gettoken()) != '\n') strcpy(out, token); while (gettoken() != EOF) { char temp[MAXTOKEN]; int type; { main() /* undcl: convert word descriptions to declarations */ Փ⫼Ⳍৠⱘ໪䚼ব䞣DŽ ⬅Ѣᇍ䕧ܹⱘ䇁⊩䖯㸠њㅔ࣪ˈ᠔ҹৃҹ䞡⫼Ϟ䴶ᅮНⱘ gettoken ߑ᭄DŽundcl ੠ dcl char (*(*x())[])() ⿟ᑣ undcl ᇚᡞ䆹ᔶᓣ䕀ᤶЎ˖ x () * [] * () char 䩜ߚ߿ᣛ৥໮Ͼߑ᭄ˈ䖭ѯߑ᭄ⱘ䖨ಲؐЎ char ㉏ൟ˅ⱘᦣ䗄⫼ϟ߫ᔶᓣ㸼⼎˖ ㋴Ўᣛ䩜ˈ䖭ѯᣛܗϔϾߑ᭄ˈᅗ䖨ಲϔϾᣛ䩜ˈ䆹ᣛ䩜ᣛ৥ϔϾϔ㓈᭄㒘ˈ䆹ϔ㓈᭄㒘ⱘ ៥Ӏᇚ“x is a function returning a pointer to an array of pointers to functions returning charā˄ x ᰃ བᵰϡ೼Т⫳៤໮ԭⱘ೚ᣀোˈ঺ϔϾᮍ৥ⱘ䕀ᤶ㽕ᆍᯧϔѯDŽЎњㅔ࣪⿟ᑣⱘ䕧ܹˈ ᳝݇ߑ᭄ getch ੠ ungetch ⱘ䇈ᯢˈখ㾕㄀ 4 ゴDŽ } return tokentype = c; } else return tokentype = NAME; ungetch(c); *p = '\0'; *p++ = c; for (*p++ = c; isalnum(c = getch()); ) } else if (isalpha(c)) { return tokentype = BRACKETS; *p = '\0'; ; for (*p++ = c; (*p++ = getch()) != ']'; ) } else if (c == '[') { } return tokentype = '('; ungetch(c); } else { return tokentype = PARENS; strcpy(token, "()"); if ((c = getch()) == ')') { if (c == '(') { w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! খ᭄㉏ൟⱘໄᯢǃᏺ᳝㉏ԐѢ const 䰤ᅮヺⱘໄᯢㄝDŽ 㒗д 5•20 ᠽሩ dcl ⿟ᑣⱘࡳ㛑ˈՓᅗ㛑໳໘⧚ࣙ৿݊ᅗ៤ߚⱘໄᯢˈ՟བᏺ᳝ߑ᭄ ೚ᣀোDŽ 㒗д 5•19 ׂᬍ undcl ⿟ᑣˈՓᅗ೼ᡞ᭛ᄫᦣ䗄䕀ᤶЎໄᯢⱘ䖛⿟ЁϡӮ⫳៤໮ԭⱘ 㒗д 5•18 ׂᬍ dcl ⿟ᑣˈՓᅗ㛑໳໘⧚䕧ܹЁⱘ䫭䇃DŽ } return 0; } printf("invalid input at %s\n", token); w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 㒧ᵘЁᅮНⱘব䞣⿄ЎюၔDŽ㒧ᵘ៤ਬǃ㒧ᵘᷛ䆄੠᱂䗮ব䞣˄े䴲៤ਬ˅ৃҹ䞛⫼Ⳍ ೼ᅮНПৢˈ㒧ᵘᷛ䆄ህҷ㸼㢅ᣀোݙⱘໄᯢˈৃҹ⫼ᅗ԰Ў䆹ໄᯢⱘㅔݭᔶᓣDŽ struct ৢ䴶ⱘৡᄫᰃৃ䗝ⱘˈ⿄Ў㒧ᵘᷛ䆄˄䖭䞠ᰃ point˅DŽ㒧ᵘᷛ䆄⫼ѢЎ㒧ᵘੑৡˈ ݇䬂ᄫ struct ᓩܹ㒧ᵘໄᯢDŽ㒧ᵘໄᯢ⬅ࣙ৿೼㢅ᣀোݙⱘϔ㋏߫ໄᯢ㒘៤DŽ݇䬂ᄫ }; int y; int x; struct point { ៥Ӏৃҹ䞛⫼㒧ᵘᄬᬒ䖭ϸϾതᷛˈ݊ໄᯢབϟ˖ ೒ 6•1 ⼎ᅗˈϨ xǃy ⱘതᷛؐ䛑Ўᭈ᭄˄খ㾕೒ 6•1˅ ᅮ⫼ x Ϣ y തᷛ㸼؛ˈᴹᓎゟϔѯ䗖⫼Ѣ೒ᔶ乚ඳⱘ㒧ᵘDŽ⚍ᰃ᳔෎ᴀⱘᇍ䈵ܜ៥Ӏ佪 6.1. 㒧ᵘⱘ෎ᴀⶹ䆚 ᭄㒘⦄೼гৃҹ䖯㸠߱ྟ࣪DŽ Ёˈ㞾ࡼ㒧ᵘ੠ޚᠡᇍ݊ሲᗻ䖯㸠њ㊒⹂ᅮНDŽ೼ ANSI ᷛޚ䆥఼᠔ᬃᣕˈԚᰃˈⳈࠄ䖭ϔᷛ Ӵ䗦㒭ߑ᭄ˈߑ᭄гৃҹ䖨ಲ㒧ᵘ㉏ൟⱘ䖨ಲؐDŽ໮ᑈҹࠡˈ䖭ϔ᪡԰ህᏆ㒣㹿໻໮᭄ⱘ㓪 ೼㒧ᵘᮍ䴶᳔Џ㽕ⱘব࣪ᰃᅮНњ㒧ᵘⱘ䌟ؐ᪡԰——㒧ᵘৃҹᣋ䋱ǃ䌟ؐǃޚANSI ᷛ ⶽᔶ⬅ϸϾ⚍ᅮНˈㄝㄝDŽ DŽC 䇁㿔Ё᳈݌ൟⱘϔϾ՟ᄤᴹ㞾Ѣ೒ᔶ乚ඳ˖⚍⬅ϔᇍതᷛᅮНˈމᎹ䌘гৃ㛑ߎ⦄㉏Ԑⱘᚙ ⼒Ӯֱ䰽োǃᎹ䌘ㄝDŽ݊Ёⱘᶤѯሲᗻгৃҹᰃ㒧ᵘˈ՟བྦྷৡৃҹߚ៤޴䚼ߚˈഄഔ⫮㟇 Ꮉ䌘䆄ᔩᰃ⫼ᴹᦣ䗄㒧ᵘⱘϔϾӴ㒳՟ᄤDŽ↣Ͼ䲛ਬ⬅ϔ㒘ሲᗻᦣ䗄ˈབྦྷৡǃഄഔǃ ᰃ೼໻ൟⱘ⿟ᑣЁDŽ 㗠ϡᰃ৘㞾⣀ゟⱘᅲԧˈ಴ℸ㒧ᵘ᳝ࡽѢ㒘㒛໡ᴖⱘ᭄᥂ˈ⡍߿ܗ㒘Ⳍ݇ⱘব䞣ⳟ԰ϔϾऩ ব䞣㒘㒛೼ϔϾৡᄫПϟDŽ˄ᶤѯ䇁㿔ᇚ㒧ᵘ⿄ЎĀ䆄ᔩāˈ↨བ Pascal 䇁㿔DŽ˅⬅Ѣ㒧ᵘᇚϔ 㒧ᵘᰃϔϾ៪໮Ͼব䞣ⱘ䲚ড়ˈ䖭ѯব䞣ৃ㛑Ўϡৠⱘ㉏ൟˈЎњ໘⧚ⱘᮍ֓㗠ᇚ䖭ѯ ㄀6ゴ㒧ᵘ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ೒ 6•2 Нབϟ˖ 㒧ᵘৃҹጠ༫DŽ៥Ӏৃҹ⫼ᇍ㾦㒓ϞⱘϸϾ⚍ᴹᅮНⶽᔶ˄খ㾕೒ 6•2˅ˈⳌᑨⱘ㒧ᵘᅮ dist = sqrt((double)pt.x * pt.x + (double)pt.y * pt.y); double dist, sqrt(double); ៪㗙䗮䖛ϟ߫ҷⷕ䅵ㅫॳ⚍(0, 0)ࠄ⚍ pt ⱘ䎱⾏˖ printf("%d,%d", pt.x, pt.y); തᷛ˖ ݊Ёⱘ㒧ᵘ៤ਬ䖤ㅫヺ“.āᇚ㒧ᵘৡϢ៤ਬৡ䖲᥹䍋ᴹDŽ՟བˈৃ⫼ϟ߫䇁হᠧॄ⚍ pt ⱘ ੠.юၔٲࠒ ೼㸼䖒ᓣЁˈৃҹ䗮䖛ϟ߫ᔶᓣᓩ⫼ᶤϾ⡍ᅮ㒧ᵘЁⱘ៤ਬ˖ 㞾ࡼ㒧ᵘгৃҹ䗮䖛䌟ؐ߱ྟ࣪ˈ䖬ৃҹ䗮䖛䇗⫼䖨ಲⳌᑨ㉏ൟ㒧ᵘⱘߑ᭄䖯㸠߱ྟ࣪DŽ struct point maxpt = {320, 200}; 㸠DŽ߱ؐ㸼Ёৠ↣Ͼ៤ਬᇍᑨⱘ߱ؐᖙ乏ᰃᐌ䞣㸼䖒ᓣˈ՟བ˖ ᅮНњϔϾ struct point ㉏ൟⱘব䞣 ptDŽ㒧ᵘⱘ߱ྟ࣪ৃҹ೼ᅮНⱘৢ䴶Փ⫼߱ؐ㸼䖯 struct point pt; ᷛ䆄ᅮНDŽ՟བˈᇍѢϞ䴶㒭ߎⱘ㒧ᵘໄᯢ pointˈ䇁হ ⱘ῵ᵓ៪䕂ᒧDŽԚᰃˈབᵰ㒧ᵘໄᯢЁᏺ᳝ᷛ䆄ˈ䙷М೼ҹৢᅮН㒧ᵘᅲ՟ᯊ֓ৃҹՓ⫼䆹 ぎ䯈ˈᅗҙҙᦣ䗄њϔϾ㒧ᵘټབᵰ㒧ᵘໄᯢⱘৢ䴶ϡᏺব䞣㸼ˈ߭ϡ䳔㽕Ўᅗߚ䜡ᄬ 䯈DŽ ぎټ݋᳝㉏ԐⱘᛣНDŽ䖭ϸϾໄᯢ䛑ᇚ xǃy Ϣ z ໄᯢЎᣛᅮ㉏ൟⱘব䞣ˈᑊϨЎᅗӀߚ䜡ᄬ int x, y, z; Ң䇁⊩㾦ᑺᴹ䇈ˈ䖭⾡ᮍᓣⱘໄᯢϢໄᯢ struct { ... } x, y, z; 䞣㸼ˈ䖭Ϣ݊ᅗ෎ᴀ㉏ൟⱘব䞣ໄᯢᰃⳌৠⱘDŽ՟བ˖ struct ໄᯢᅮНњϔ⾡᭄᥂㉏ൟDŽ೼ᷛᖫ㒧ᵘ៤ਬ㸼㒧ᴳⱘে㢅ᣀোПৢৃҹ䎳ϔϾব ӮՓ⫼ⳌৠⱘৡᄫDŽ ᵘЁⱘ៤ਬৃҹՓ⫼ⳌৠⱘৡᄫˈԚᰃˈҢ㓪⿟亢Ḑᮍ䴶ᴹ䇈ˈ䗮ᐌা᳝ᆚߛⳌ݇ⱘᇍ䈵ᠡ さˈ಴Ў䗮䖛Ϟϟ᭛ߚᵤᘏৃҹᇍᅗӀ䖯㸠ऎߚDŽ঺໪ˈϡৠ㒧ކৠⱘৡᄫˈᅗӀП䯈ϡӮ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! { struct addpoint(struct point p1, struct point p2) /* addpoints: add two points */ ᥹ϟᴹ䳔㽕㓪ݭϔ㋏߫ⱘߑ᭄ᇍ⚍ᠻ㸠ㅫᴃ䖤ㅫDŽ՟བ˖ (screen.pt1.y + screen.pt2.y)/2); middle = makepoint((screen.pt1.x + screen.pt2.x)/2, screen.pt2 = makepoint(XMAX, YMAX); screen.pt1 = makepoint(0,0); struct point makepoint(int, int); struct point middle; struct rect screen; ᭄DŽ՟བ˖ ⦄೼ৃҹՓ⫼ makepoint ߑ᭄ࡼᗕഄ߱ྟ࣪ӏᛣ㒧ᵘˈгৃҹ৥ߑ᭄ᦤկ㒧ᵘ㉏ൟⱘখ さDŽџᅲϞˈՓ⫼䞡ৡৃҹᔎ䇗ϸ㗙П䯈ⱘ݇㋏DŽކ⊼ᛣˈখ᭄ৡ੠㒧ᵘ៤ਬৠৡϡӮᓩ䍋 } return temp; temp.y = y; temp.x = x; struct point temp; { struct point makepoint(int x, int y) /* makepoint: make a point from x and y components */ ᴹⳟϔϟߑ᭄ makepointˈᅗᏺ᳝ϸϾᭈൟখ᭄ˈᑊ䖨ಲϔϾ point ㉏ൟⱘ㒧ᵘ˖ܜ佪 ᵘⱘᣛ䩜DŽ䖭 3 ⾡ᮍ⊩৘᳝߽ᓞDŽ ⾡ৃ㛑ⱘᮍ⊩Ӵ䗦㒧ᵘ˖ϔᰃߚ߿Ӵ䗦৘Ͼ㒧ᵘ៤ਬˈѠᰃӴ䗦ᭈϾ㒧ᵘˈϝᰃӴ䗦ᣛ৥㒧 Ўњ᳈䖯ϔℹഄ⧚㾷㒧ᵘˈ៥Ӏ㓪ݭ޴Ͼᇍ⚍੠ⶽᔶ䖯㸠᪡԰ⱘߑ᭄DŽ㟇ᇥৃҹ䗮䖛 3 ⫼ϔϾᐌ䞣៤ਬؐ߫㸼߱ྟ࣪㒧ᵘˈ㞾ࡼ㒧ᵘгৃҹ䗮䖛䌟ؐ䖯㸠߱ྟ࣪DŽ ݊Ёˈ໡ࠊ੠䌟ؐࣙᣀ৥ߑ᭄Ӵ䗦খ᭄ҹঞҢߑ᭄䖨ಲؐDŽ㒧ᵘП䯈ϡৃҹ䖯㸠↨䕗DŽৃҹ 㒧ᵘⱘড়⊩᪡԰া᳝޴⾡˖԰ЎϔϾᭈԧ໡ࠊ੠䌟ؐˈ䗮䖛&䖤ㅫヺপഄഔˈ䆓䯂݊៤ਬDŽ 6.2. 㒧ᵘϢߑ᭄ ᓩ⫼ screen ⱘ៤ਬ pt1 ⱘ x തᷛDŽ screen.pt1.x ߭ৃҹ⫼䇁হ struct rect screen; 㒧ᵘ rect ࣙ৿ϸϾ point ㉏ൟⱘ៤ਬDŽབᵰᣝ✻ϟ߫ᮍᓣໄᯢ screen ব䞣˖ }; struct point pt2; struct point pt1; struct rect { w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! юၔٲp•>ࠒ ϔϾᣛ৥㒧ᵘⱘᣛ䩜ˈৃҹ⫼ ᅮ p ᰃ؛㒧ᵘᣛ䩜ⱘՓ⫼乥ᑺ䴲ᐌ催ˈЎњՓ⫼ᮍ֓ˈC 䇁㿔ᦤկњ঺ϔ⾡ㅔݭᮍᓣDŽ 催DŽ㸼䖒ᓣ*pp.x ⱘ৿НㄝӋѢ*(pp.x)ˈ಴Ў x ϡᰃᣛ䩜ˈ᠔ҹ䆹㸼䖒ᓣᰃ䴲⊩ⱘDŽ 㑻ܜ㑻↨“*āⱘӬܜ݊Ёˈ(*pp).x Ёⱘ೚ᣀোᰃᖙ䳔ⱘˈ಴Ў㒧ᵘ៤ਬ䖤ㅫヺ“.āⱘӬ printf("origin is (%d,%d)\n", (*pp).x, (*pp).y); pp = &origin; struct point origin, *pp; М*pp ेЎ䆹㒧ᵘˈ㗠(*pp).x ੠(*pp).y ߭ᰃ㒧ᵘ៤ਬDŽৃҹᣝ✻ϟ՟ЁⱘᮍᓣՓ⫼ pp˖ ᇚ pp ᅮНЎϔϾᣛ৥ struct point ㉏ൟᇍ䈵ⱘᣛ䩜DŽབᵰ pp ᣛ৥ϔϾ point 㒧ᵘˈ䙷 struct point *pp; ᵘᣛ䩜㉏ԐѢ᱂䗮ব䞣ᣛ䩜DŽໄᯢ བᵰӴ䗦㒭ߑ᭄ⱘ㒧ᵘᕜ໻ˈՓ⫼ᣛ䩜ᮍᓣⱘᬜ⥛䗮ᐌ↨໡ࠊᭈϾ㒧ᵘⱘᬜ⥛㽕催DŽ㒧 } return temp; temp.pt2.y = max(r.pt1.y, r.pt2.y); temp.pt2.x = max(r.pt1.x, r.pt2.x); temp.pt1.y = min(r.pt1.y, r.pt2.y); temp.pt1.x = min(r.pt1.x, r.pt2.x); struct rect temp; { struct rect canonrect(struct rect r) /* canonrect: canonicalize coordinates of rectangle */ #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) 㾘㣗ᔶᓣⱘⶽᔶ˖ ᔶᓣ㸼⼎ⱘˈ݊Ё pt1 ⱘതᷛᇣѢ pt2 ⱘതᷛDŽϟ߫ߑ᭄ᇚ䖨ಲϔϾޚ䆒ⶽᔶᰃ⫼ᷛ؛䖭䞠 } && p.y >= r.pt1.y && p.y < r.pt2.y; return p.x >= r.pt1.x && p.x < r.pt2.x { int ptinrect(struct point p, struct rect r) /* ptinrect: return 1 if p in r, 0 if not */ 䖭ḋϔϾ㑺ᅮ˖ⶽᔶࣙᣀ݊Ꮊջ䖍੠ᑩ䖍ˈԚϡࣙᣀ乊䖍੠েջ䖍DŽ ϟ䴶ᴹⳟ঺໪ϔϾ՟ᄤDŽߑ᭄ prinrect ߸ᮁϔϾ⚍ᰃ৺೼㒭ᅮⱘⶽᔶݙ䚼DŽ៥Ӏ䞛⫼ Ӵ䗦ⱘDŽ ᰃЎњᔎ䇗㒧ᵘ㉏ൟⱘখ᭄੠݊ᅗ㉏ൟⱘখ᭄ϔḋˈ䛑ᰃ䗮䖛ؐˈټՓ⫼ᰒᓣⱘЈᯊব䞣ᄬ ݊Ёˈߑ᭄ⱘখ᭄੠䖨ಲؐ䛑ᰃ㒧ᵘ㉏ൟDŽП᠔ҹⳈ᥹ᇚⳌࡴ᠔ᕫⱘ㒧ᵰ䌟ؐ㒭 p1ˈ㗠≵᳝ } return p1; p1.y += p2.y; p1.x += p2.x; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! int count; char *word; struct key { 䖭ḋⱘ໮Ͼব䞣ᇍ݅ৠᵘ៤ϔϾ᭄㒘DŽ៥Ӏᴹⳟϟ䴶ⱘໄᯢ˖ int cout; char *word; гህᰃ៥Ӏ䖭䞠᠔䇈ⱘ㒧ᵘ᭄㒘DŽ↣Ͼ݇䬂ᄫ乍ࣙᣀϔᇍব䞣˖ ៥Ӏ⊼ᛣࠄˈ䖭ϸϾ᭄㒘ⱘ໻ᇣⳌৠˈ㗗㰥ࠄ䆹⡍⚍ˈৃҹ䞛⫼঺ϔ⾡ϡৠⱘ㒘㒛ᮍᓣˈ int keycount[NKEYS]; char *keyword[NKEYS]; Փ⫼ϸϾ⣀ゟⱘ᭄㒘 keyword ੠ keycount ߚ߿ᄬᬒᅗӀˈབϟ᠔⼎ ϔϾᄫヺІ᭄㒘ᄬᬒ݇䬂ᄫৡˈϔϾᭈൟ᭄㒘ᄬᬒⳌᑨ݇䬂ᄫⱘߎ⦄⃵᭄DŽϔ⾡ᅲ⦄ᮍ⊩ᰃˈ 㗗㰥㓪ݭ䖭ḋϔϾ⿟ᑣˈᅗ⫼ᴹ㒳䅵䕧ܹЁ৘Ͼ C 䇁㿔݇䬂ᄫߎ⦄ⱘ⃵᭄DŽ៥Ӏ䳔㽕⫼ 6.3. 㒧ᵘ᭄㒘 䇏পᣛ䩜 str ᣛ৥ⱘᇍ䈵ⱘؐˈ✊ৢݡᇚ p ࡴ 1DŽܜ ⱘᇍ䈵ⱘؐࡴ 1˗*p++•>str str ᣛ৥ⱘᇍ䈵ⱘؐˈ✊ৢݡᇚ str ࡴ 1˄Ϣ*s++Ⳍৠ˅˗(*p•>str˅++ᇚᣛ䩜 str ᣛ৥ 䇏পᣛ䩜ܜ++ৠḋⱘ䘧⧚ˈ*p•>str 䇏পⱘᰃᣛ䩜 str ᠔ᣛ৥ⱘᇍ䈵ⱘؐ˗*p•>str ᇍ len ᠻ㸠᪡԰ˈ✊ৢݡᇚ p ࡴ 1˄䆹㸼䖒ᓣЁⱘᣀোৃҹⳕ⬹˅DŽܜ㗠(p++)•>len ߭ ᠻ㸠 p ⱘࡴ 1 ᪡԰ˈݡᇍ len ᠻ㸠᪡԰˗ܜҹՓ⫼ᣀোᬍব㒧ড়⃵ᑣDŽ՟བ˖(++p)•>len ᇚ ᇚ๲ࡴ len ⱘؐˈ㗠ϡᰃ๲ࡴ p ⱘؐˈ䖭ᰃ⬄Ўˈ݊Ёⱘ䱤৿ᣀো݇㋏ᰃ++(p•>len)DŽৃ ++p•>len 㸼䖒ᓣ } *p; char *str; int len; struct { 㒧ᵘໄᯢ 䇗⫼ⱘ“()āҹঞ⫼Ѣϟᷛⱘ“[]āˈ಴ℸˈᅗӀৠ᪡԰᭄П䯈ⱘ㒧ড়г᳔㋻ᆚDŽ՟བˈᇍѢ 㑻᳔催˖㒧ᵘ䖤ㅫヺ“.ā੠“•>āǃ⫼Ѣߑ᭄ܜ೼᠔᳝䖤ㅫヺЁˈϟ䴶 4 Ͼ䖤ㅫヺⱘӬ (rp•>pt1).x (r.pt1).x rp•>pt1.x r.pt1.x ҹϟ 4 Ͼ㸼䖒ᓣᰃㄝӋⱘ˖ struct rect r, *rp = &r; 䖤ㅫヺ.੠•>䛑ᰃҢᎺ㟇ে㒧ড়ⱘˈ᠔ҹˈᇍѢϟ䴶ⱘໄᯢ˖ printf("origin is (%d,%d)\n", pp•>x, pp•>y); 䖭⾡ᔶᓣᓩ⫼Ⳍᑨⱘ㒧ᵘ៤ਬDŽ䖭ḋˈህৃҹ⫼ϟ䴶ⱘᔶᓣᬍݭϞ䴶ⱘϔ㸠ҷⷕ˖ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! int getword(char *, int); #define MAXWORD 100 #include #include #include ೼ keytab ЁDŽټkeytab Ё䖯㸠ᶹᡒDŽ⊼ᛣˈ݇䬂ᄫ߫㸼ᖙ乏ᣝछᑣᄬ getword 䇏প䕧ܹˈ↣⃵䇏পϔϾऩ䆡DŽ↣Ͼऩ䆡ᇚ䗮䖛ᡬञᶹᡒߑ᭄˄খ㾕㄀ 3 ゴ˅೼ ᅮНњ keytabDŽЏ⿟ᑣড໡䇗⫼ߑ᭄ܜ೼㒳䅵݇䬂ᄫߎ⦄⃵᭄ⱘ⿟ᑣЁˈ៥Ӏ佪 ⱘ乍᭄DŽ ϟˈབᵰ߱ؐᄬ೼ᑊϨᮍᣀো[ ] Ё≵᭄᳝ؐˈ㓪䆥⿟ᑣᇚ䅵ㅫ᭄㒘 keytab Ёމ⬹DŽ䗮ᐌᚙ Ԛᰃˈབᵰ߱ؐᰃㅔऩব䞣៪ᄫヺІˈᑊϨ݊Ёⱘӏԩؐ䛑ϡЎぎˈ߭ݙሖⱘ㢅ᣀোৃҹⳕ ... { "case", 0 }, { "break", 0 }, { "auto", 0 }, ᵘ˅ⱘ߱ؐ䛑ᣀ೼㢅ᣀোݙˈབϟ᠔⼎˖ ᰃˈᇚ↣ϔ㸠˄े↣Ͼ㒧⫣خϢ㒧ᵘ៤ਬⳌᇍᑨˈ߱ؐг㽕ᣝ✻៤ᇍⱘᮍᓣ߫ߎDŽ᳈㊒⹂ⱘ }; "while", 0 "volatile", 0, "void", 0, "unsigned", 0, /* ... */ "default", 0, "continue", 0, "const", 0, "char", 0, "case", 0, "break", 0, "auto", 0, } keytab[] = { int count; char *word; struct key { ⊩㉏Ԑ——೼ᅮНⱘৢ䴶䗮䖛ϔϾ⫼೚ᣀোᣀ䍋ᴹⱘ߱ؐ㸼䖯㸠߱ྟ࣪ˈབϟ᠔⼎˖ া䳔㽕߱ྟ࣪ϔ⃵ˈ᠔᳝ⱘഄᮍ䛑ৃҹՓ⫼DŽ䖭⾡㒧ᵘⱘ߱ྟ࣪ᮍ⊩ৠࠡ䴶᠔䗄ⱘ߱ྟ࣪ᮍ ಴Ў㒧ᵘ keytab ࣙ৿ϔϾ೎ᅮⱘৡᄫ䲚ড়ˈ᠔ҹˈ᳔དᇚᅗໄᯢЎ໪䚼ব䞣ˈ䖭ḋˈ struct key keytab[NKEYS]; }; int count; char *word; struct key { ㋴䛑ᰃϔϾ㒧ᵘDŽϞ䗄ໄᯢгৃҹݭ៤ϟ߫ᔶᓣ˖ܗ᭄㒘 keytab ⱘ↣Ͼ ぎ䯈DŽټᅗໄᯢњϔϾ㒧ᵘ㉏ൟ keyˈᑊᅮНњ䆹㉏ൟⱘ㒧ᵘ᭄㒘 keytabˈৠᯊЎ݊ߚ䜡ᄬ keytab[NKEYS]; { w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ҹঞ sizeof ճເ ᑺDŽ㸼䖒ᓣ 䖤ㅫヺ sizeofˈᅗৃ⫼ᴹ䅵ㅫӏϔᇍ䈵ⱘ䭓ܗC 䇁㿔ᦤկњϔϾ㓪䆥ᯊ˄compile•time˅ϔ Keytab ⱘ䭓ᑺ/struct key ⱘ䭓ᑺ ᑺЬҹ乍᭄ˈ಴ℸˈৃҹᕫߎ乍᭄Ў˖ ಴Ў᭄㒘ⱘ䭓ᑺ೼㓪䆥ᯊᏆ㒣ᅠܼ⹂ᅮˈᅗㄝѢ᭄㒘乍ⱘ䭓ˈخԚᅲ䰙Ϟᑊϡ䳔㽕䖭ḋ ✊ৢᕾ⦃䘡ग़ keytabˈⳈࠄ䇏ࠄሒ䚼ⱘぎᣛ䩜ЎℶDŽ ࡲ⊩ᰃˈ೼߱ؐ㸼ⱘ㒧ሒ໘ࡴϞϔϾぎᣛ䩜ˈއᅝܼˈᔧ߫㸼ৃ㛑ব᳈ᯊᇸ݊བℸDŽϔ⾡㾷 NKEYS ҷ㸼 keytab Ё݇䬂ᄫⱘϾ᭄DŽሑㅵৃҹ᠟Ꮉ䅵ㅫˈԚ⬅ᴎ఼ᅲ⦄Ӯ᳈ㅔऩǃ᳈ Ͼऩ䆡ˈᑊᇚ݊໡ࠊࠄৡᄫЎ䆹ߑ᭄ⱘ㄀ϔϾখ᭄ⱘ᭄㒘ЁDŽ ߑ᭄ getword ᇚ೼⿡ৢҟ㒡ˈ䖭䞠া䳔㽕њ㾷ᅗⱘࡳ㛑ᰃ↣䇗⫼ϔ⃵䆹ߑ᭄ˈᇚ䇏ܹϔ } return •1; } return mid; else low = mid + 1; else if (cond > 0) high = mid • 1; if ((cond = strcmp(word, tab[mid].word)) < 0) mid = (low+high) / 2; while (low <= high) { high = n • 1; low = 0; int low, high, mid; int cond; { int binsearch(char *word, struct key tab[], int n) /* binsearch: find word in tab[0]...tab[n•1] */ } return 0; keytab[n].count, keytab[n].word); printf("%4d %s\n", if (keytab[n].count > 0) for (n = 0; n < NKEYS; n++) keytab[n].count++; if ((n = binsearch(word, keytab, NKEYS)) >= 0) if (isalpha(word[0])) while (getword(word, MAXWORD) != EOF) char word[MAXWORD]; int n; { main() /* count C keywords */ int binsearch(char *, struct key *, int); w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ༈᭛ӊЁDŽ ޚⱑヺˈisalpha ߑ᭄䆚߿ᄫ↡ˈisalnum ߑ᭄䆚߿ᄫ↡੠᭄ᄫDŽ᠔᳝䖭ѯߑ᭄䛑ᅮН೼ᷛ ಲࠄ䕧ܹЁˈҹ֓ϟϔ⃵䇗⫼Փ⫼DŽGetword 䖬Փ⫼њ݊ᅗϔѯߑ᭄˖isspace ߑ᭄䏇䖛ぎ ᄫⱘ䲚ড়ᯊˈ䇈ᯢ getword ໮䇏ܹњϔϾᄫヺDŽ䱣ৢˈ䇗⫼ ungetch ᇚ໮䇏ⱘϔϾᄫヺᬒ getword ߑ᭄Փ⫼њ㄀ 4 ゴЁⱘߑ᭄ getch ੠ ungetchDŽᔧ䇏ܹⱘᄫヺϡሲѢᄫ↡᭄ } return word[0]; *w = '\0'; } break; ungetch(*w); if (!isalnum(*w = getch())) { for ( ; ••lim > 0; w++) } return c; *w = '\0'; if (!isalpha(c)) { *w++ = c; if (c != EOF) ; while (isspace(c = getch())) char *w = word; void ungetch(int); int c, getch(void); { int getword(char *word, int lim) /* getword: get next word or character from input */ 䆡ⱘ㄀ϔϾᄫヺǃ᭛ӊ㒧ᴳヺ EOF ៪ᄫヺᴀ䑿˄བᵰ䆹ᄫヺϡᰃᄫ↡ᄫヺⱘ䆱˅DŽ ऩ䆡ৃҹᰃҹᄫ↡ᓔ༈ⱘᄫ↡੠᭄ᄫІˈгৃҹᰃϔϾ䴲ぎⱑヺᄫヺDŽߑ᭄䖨ಲؐৃ㛑ᰃऩ Ꮖ䍙ߎ䖭Ͼ⼎՟⿟ᑣⱘ㽕∖ˈϡ䖛ˈߑ᭄ᴀ䑿ᑊϡ໡ᴖDŽgetword Ң䕧ܹЁ䇏পϟϔϾऩ䆡ˈ ϟ䴶ᴹ䅼䆎ߑ᭄ getwordDŽ៥Ӏ䖭䞠㒭ߎϔϾ᳈䗮⫼ⱘ getword ߑ᭄DŽ䆹ߑ᭄ⱘࡳ㛑 ᑊϡ䅵ㅫ#define 䇁হЁⱘ㸼䖒ᓣˈ಴ℸˈ೼#define ЁՓ⫼ sizeof ᰃড়⊩ⱘDŽ ᴵӊ㓪䆥䇁হ#if Ёϡ㛑Փ⫼ sizeofˈ಴Ў乘໘⧚఼ϡᇍ㉏ൟৡ䖯㸠ߚᵤDŽԚ乘໘⧚఼ Փ⫼㄀Ѡ⾡ᮍ⊩ˈेՓ㉏ൟᬍবњˈгϡ䳔㽕ᬍࡼ⿟ᑣDŽ #define NKEYS (sizeof keytab / sizeof(keytab[0])) ㋴ⱘ䭓ᑺˈབϟ᠔⼎˖ܗ঺ϔ⾡ᮍ⊩ᰃ⫼᭄㒘ⱘ䭓ᑺ䰸ҹϔϾᣛᅮ #define NKEYS (sizeof keytab / sizeof(struct key)) Փ⫼њ䖭⾡ᮍ⊩䆒㕂 NKEYS ⱘؐ˖ ㋴ⱘ䭓ᑺDŽϟ䴶ⱘ#define 䇁হܗ೼䆹՟ᄤЁˈ݇䬂ᄫⱘϾ᭄ㄝѢ᭄㒘ⱘ䭓ᑺ䰸ҹऩϾ བ㒧ᵘ㉏ൟ៪ᣛ䩜㉏ൟDŽ ᇍ䈵ৃҹᰃব䞣ǃ᭄㒘៪㒧ᵘ˗㉏ൟৃҹᰃ෎ᴀ㉏ൟˈབ intǃdoubleˈгৃҹᰃ⌒⫳㉏ൟˈ 䖨ಲؐᰃ᮴ヺোᭈൟؐˈ݊㉏ൟЎ size_tˈ䆹㉏ൟ೼༈᭛ӊЁᅮНDŽ˅݊Ёˈ ぎ䯈ᄫ㡖᭄DŽ˄ϹḐഄ䇈ˈsizeof ⱘټᇚ䖨ಲϔϾᭈൟؐˈᅗㄝѢᣛᅮᇍ䈵៪㉏ൟऴ⫼ⱘᄬ sizeof(फ໸੠) w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! } return NULL; } return mid; else low = mid + 1; else if (cond > 0) high = mid; if ((cond = strcmp(word, mid•>word)) < 0) mid = low + (high•low) / 2; while (low < high) { struct key *mid; struct key *high = &tab[n]; struct key *low = &tab[0]; int cond; { struct key *binsearch(char *word, struck key *tab, int n) /* binsearch: find word in tab[0]...tab[n•1] */ } return 0; printf("%4d %s\n", p•>count, p•>word); if (p•>count > 0) for (p = keytab; p < keytab + NKEYS; p++) p•>count++; if ((p=binsearch(word, keytab, NKEYS)) != NULL) if (isalpha(word[0])) while (getword(word, MAXWORD) != EOF) struct key *p; char word[MAXWORD]; { main() /* count C keywords; pointer version */ struct key *binsearch(char *, struct key *, int); int getword(char *, int); #define MAXWORD 100 #include #include #include བϟ˖ keytab ⱘ໪䚼ໄᯢϡ䳔㽕ׂᬍˈԚ main ੠ binsearch ߑ᭄ᖙ乏ׂᬍDŽׂᬍৢⱘ⿟ᑣ ᣛ䩜ˈ㗠ϡՓ⫼᭄㒘ϟᷛDŽ Ўњ䖯ϔℹ䇈ᯢᣛ৥㒧ᵘⱘᣛ䩜੠㒧ᵘ᭄㒘ˈ៥Ӏ䞡ᮄ㓪ݭ݇䬂ᄫ㒳䅵⿟ᑣˈ䖭⃵䞛⫼ 6.4. ᣛ৥㒧ᵘⱘᣛ䩜 ࠊᣛҸDŽ䇋㓪ݭϔϾ᳈ᅠ୘ⱘ getword ߑ᭄DŽ 㒗д 6•1 Ϟ䗄 getword ߑ᭄ϡ㛑ℷ⹂໘⧚ϟߦ㒓ǃᄫヺІᐌ䞣ǃ⊼䞞ঞ乘໘⧚఼᥻ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! binsearch(char *word, struct key *tab, int n) struct key * 䗄䇁হ˖ ᕜ䲒ⳟߎߑ᭄ৡˈгϡ໾ᆍᯧՓ⫼᭛ᴀ㓪䕥఼ᡒࠄߑ᭄ৡDŽ៥Ӏৃҹ䞛⫼঺ϔ⾡ḐᓣкݭϞ struct key *binsearch(char *word, struct key *tab, int n) ᳔ৢˈ䇈ᯢϔ⚍⿟ᑣⱘḐᓣ䯂乬˖ᔧߑ᭄ⱘ䖨ಲؐ㉏ൟ↨䕗໡ᴖᯊ˄བ㒧ᵘᣛ䩜˅ˈ՟བ 䭓ᑺDŽ ぎ䯈ˈ㗠ϡᰃ 5 Ͼᄫ㡖DŽՓ⫼ sizeof 䖤ㅫヺৃҹ䖨ಲℷ⹂ⱘᇍ䈵ټৃ㛑䳔㽕 8 Ͼᄫ㡖ⱘᄬ }; int i; char c; struct { int ㉏ൟऴ⫼ 4 Ͼᄫ㡖ˈ߭ϟ߫㒧ᵘ˖ 䆒 char ㉏ൟऴ⫼ϔϾᄫ㡖ˈ؛ˈ∖ˈ᠔ҹˈ㒧ᵘЁৃ㛑Ӯߎ⦄᳾ੑৡⱘĀぎえĀ˄hole˅DŽ՟བ Ԛᰃˈगϛϡ㽕䅸Ў㒧ᵘⱘ䭓ᑺㄝѢ৘៤ਬ䭓ᑺⱘ੠DŽ಴Ўϡৠⱘᇍ䈵᳝ϡৠⱘᇍ唤㽕 䆩ᴵӊ֓ৃҹֱ䆕ᕾ⦃ℷ⹂㒜ℶˈ ㋴ˈ䖭ḋˈϞ䗄⌟ܗᯊˈᇚ೼ p ⱘ෎⸔ϞࡴϞϔϾℷ⹂ⱘؐˈҹ⹂ֱᕫࠄ㒧ᵘ᭄㒘ⱘϟϔϾ བᵰ p ᰃᣛ৥㒧ᵘⱘᣛ䩜ˈ߭ᇍ p ⱘㅫᴃ䖤ㅫ䳔㽕㗗㰥㒧ᵘⱘ䭓ᑺˈ᠔ҹˈ㸼䖒ᓣ p++ᠻ㸠 for (p = keytab; p < keytab + NKEYS; p++) Џ⿟ᑣ main Ё᳝ϟ߫䇁হ˖ &tab[n]˅ⱘᣛ䩜ㅫᴃ䖤ㅫৃҹℷ⹂ᠻ㸠DŽ ㋴˄ेܗ㗠ᇍৢ㗙ⱘ䯈᥹ᓩ⫼гᰃ䴲⊩ⱘDŽԚᰃˈC 䇁㿔ⱘᅮНֱ䆕᭄㒘᳿ሒПৢⱘ㄀ϔϾ ㋴DŽ䯂乬೼Ѣˈ&tab[•1]੠&tab[n]䛑䍙ߎњ᭄㒘 tab ⱘ㣗ೈDŽࠡ㗙ᰃ㒱ᇍ䴲⊩ⱘˈܗⱘ ᇍㅫ⊩ⱘ᳔䞡㽕ׂᬍ೼Ѣˈ㽕⹂ֱϡӮ⫳៤䴲⊩ⱘᣛ䩜ˈ៪㗙ᰃ䆩೒䆓䯂᭄㒘㣗ೈП໪ ㋴ⱘᣛ䩜DŽܗᇚ mid 䆒㕂Ўᣛ৥ԡѢ high ੠ low П䯈ⱘЁ䯈 mid = low + (high•low) / 2 ㋴ⱘϾ᭄ˈ಴ℸˈৃҹ⫼ϟ߫㸼䖒ᓣ˖ܗⱘؐህᰃ᭄㒘 䖤ㅫैᰃড়⊩ⱘˈhigh•low⫣ޣ䖭ᰃ಴ЎˈϸϾᣛ䩜П䯈ⱘࡴ⊩䖤ㅫᰃ䴲⊩ⱘDŽԚᰃˈᣛ䩜ⱘ mid = (low+high) / 2 /* WRONG */ ㋴ⱘԡ㕂˖ܗ䖭ḋˈ៥Ӏህ᮴⊩ㅔऩഄ䗮䖛ϟ߫㸼䖒ᓣ䅵ㅫЁ䯈 ⱘᣛ䩜DŽ ㋴ܗ㋴ৢ䴶ⱘϔϾܗ㋴ⱘᣛ䩜੠ᣛ৥㸼ሒܗ೼䖭䞠ˈlow ੠ high ⱘ߱ؐߚ߿ᰃᣛ৥㸼༈ 䕗໻ⱘׂᬍDŽخ ㋴೼䖭䞠ᰃ䗮䖛ᣛ䩜䆓䯂ⱘDŽ䖭ህ䳔㽕ᇍ binsearchܗ݊⃵ˈkeytab ⱘ ಲ NULLDŽ ㋴ⱘᣛ䩜ˈ৺߭䖨ܗ㋴ˈᅗᇚ䖨ಲϔϾᣛ৥䆹ܗབᵰ binsearch ᡒࠄϢ䕧ܹऩ䆡ऍ䜡ⱘ᭄㒘 ᣛ৥ struct key ㉏ൟⱘᣛ䩜ˈ㗠䴲ᭈൟˈ䖭೼ߑ᭄ॳൟঞ binsearch ߑ᭄Ё䛑㽕ໄᯢDŽ binsearch ߑ᭄೼ໄᯢЁᖙ乏㸼ᯢ˖ᅗ䖨ಲⱘؐ㉏ൟᰃϔϾˈܜ䖭䞠䳔㽕⊼ᛣ޴⚍DŽ佪 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! int count; /* number of occurrences */ char *word; /* points to the text */ struct tnode { /* the tree node: */ ៥Ӏݡᴹⳟ㡖⚍ⱘᦣ䗄䯂乬DŽ᳔ᮍ֓ⱘ㸼⼎ᮍ⊩ᰃ㸼⼎Ўࣙᣀ 4 Ͼ៤ਬⱘ㒧ᵘ˖ ᄤᷥˈ᠔ҹ䆹䖛⿟ᰃ䗦ᔦⱘDŽⳌᑨഄˈ೼ᦦܹ੠ᠧॄ᪡԰ЁՓ⫼䗦ᔦ䖛⿟гᰃᕜ㞾✊ⱘџᚙDŽ ᄬᬒᮄࡴܹऩ䆡ⱘℷ⹂ԡ㕂DŽ಴ЎҢӏᛣ㡖⚍ߎথⱘᶹᡒ䛑㽕ᣝ✻ৠḋⱘᮍᓣᶹᡒᅗⱘϔϾ েᄤᷥЁᶹᡒDŽབ೼᧰ᇏᮍ৥Ϟ᮴ᄤᷥˈ߭䇈ᯢᮄऩ䆡ϡ೼ᷥЁˈᑊϨˈᔧࠡⱘぎԡ㕂ህᰃ ऍ䜡ˈ߭ᕫࠄ㚃ᅮⱘㄨḜDŽ㢹ᮄऩ䆡ᇣѢ䆹㡖⚍Ёⱘऩ䆡ˈ߭೼ᎺᄤᷥЁ㒻㓁ᶹᡒˈ৺߭೼ 㽕ᶹᡒϔϾᮄऩ䆡ᰃ৺Ꮖ㒣೼ᷥЁˈৃҹҢḍ㡖⚍ᓔྟˈ↨䕗ᮄऩ䆡Ϣ䆹㡖⚍Ёⱘऩ䆡DŽ㢹 ೒ 6•3 time for all good men to come to the aid of their partyāЁ৘ऩ䆡ৢ⫳៤ⱘᷥDŽ ऩ䆡ˈেᄤᷥাࣙ৿ᣝᄫ݌ᑣ໻Ѣ䆹㡖⚍Ёऩ䆡ⱘ䙷ѯऩ䆡DŽ೒ 6•3 ᰃᣝᑣᦦܹহᄤ“now is the ᇍ㡖⚍ⱘ᠔᳝᪡԰㽕ֱ䆕ˈӏԩ㡖⚍ⱘᎺᄤᷥাࣙ৿ᣝᄫ݌ᑣᇣѢ䆹㡖⚍Ёऩ䆡ⱘ䙷ѯ ӏԩ㡖⚍᳔໮ᢹ᳝ϸϾᄤᷥˈгৃ㛑া᳝ϔϾᄤᷥ៪ϔϾ䛑≵᳝DŽ · ϔϾᣛ৥েᄤᷥⱘᣛ䩜 · ϔϾᣛ৥Ꮊᄤᷥⱘᣛ䩜 · ϔϾ㒳䅵ߎ⦄⃵᭄ⱘ䅵᭄ؐ · ϔϾᣛ৥䆹ऩ䆡ݙᆍⱘᣛ䩜 ↣Ͼϡৠⱘऩ䆡೼ᷥЁ䛑ᰃϔϾ㡖⚍ˈ↣Ͼ㡖⚍ࣙ৿˖ ✊Ӯᇐ㟈⿟ᑣᠻ㸠ⱘᯊ䯈䖛䭓DŽ៥ӀৃҹՓ⫼ϔ⾡⿄ЎѠঝᷥⱘ᭄᥂㒧ᵘᴹপ㗠ҷПDŽ ֱ䆕᠔᳝ऩ䆡ᰃᣝ乎ᑣᥦ߫ⱘDŽ㱑✊䖭ৃҹϡ⫼䗮䖛೼㒓ᗻ᭄㒘Ё⿏ࡼऩ䆡ᴹᅲ⦄ˈԚᅗҡ ᮍ⊩ᰃˈ೼䇏প䕧ܹЁӏᛣऩ䆡ⱘৠᯊˈህᇚᅗᬒ㕂ࠄℷ⹂ⱘԡ㕂ˈҢ㗠ྟ㒜އϔ⾡㾷 䖭ѯ᭄᥂ˈᠡ㛑໳᳝ᬜഄ໘⧚ϔ㋏߫ӏᛣⱘऩ䆡ਸ਼˛ ⹂ഄ䇈ˈ⿟ᑣⱘᠻ㸠ᯊ䯈ᰃϢ䕧ܹऩ䆡᭄ⳂⱘѠ⃵ᮍ៤↨՟ⱘDŽ˅៥Ӏ䆹བԩ㒘㒛ޚ䯈DŽ˄᳈ ⿟ᑣⱘᠻ㸠ᇚ㢅䌍໾䭓ⱘᯊˈخऩ䆡䛑ᠻ㸠ϔ⃵㒓ᗻᶹᡒˈⳟᅗ೼ࠡ䴶ᰃ৺Ꮖ㒣ߎ⦄ˈ䖭ḋ ⶹ䘧ߎ⦄ⱘऩ䆡߫㸼ˈ᠔ҹ᮴⊩ᮍ֓ഄᥦᑣˈᑊՓ⫼ᡬञᶹᡒ˗гϡ㛑ߚ߿ᇍ䕧ܹЁⱘ↣Ͼ ϡܜᅮ៥Ӏ䳔㽕໘⧚ϔϾ᳈ϔ㠀࣪ⱘ䯂乬˖㒳䅵䕧ܹЁ᠔᳝ऩ䆡ⱘߎ⦄⃵᭄DŽ಴Ў乘؛ 6.5. 㞾ᓩ⫼㒧ᵘ ݋ԧ䞛⫼ા⾡ݭ⊩ሲѢϾҎⱘдᛃ䯂乬ˈৃҹ䗝ᢽ㞾Ꮕ୰⃶ⱘᮍᓣᑊྟ㒜ֱᣕ㞾Ꮕⱘ亢ḐDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! char *strdup(char *); struct tnode *talloc(void); addtree 䖨ಲϔϾᣛ৥ᮄ㡖⚍ⱘᣛ䩜ˈ䆹ᣛ䩜ֱᄬ೼⠊㡖⚍ЁDŽ ؐࡴ 1˅ˈ៪䘛ࠄϔϾぎᣛ䩜˄㸼ᯢᖙ乏߯ᓎϔϾ㡖⚍ᑊࡴܹࠄᷥЁ˅DŽ㢹⫳៤њᮄ㡖⚍ˈ߭ ϟ䅵᭄މ䇗⫼ addtree 㗠䕀৥Ꮊᄤᷥ៪েᄤᷥDŽ䆹ऩ䆡᳔㒜ᇚϢᷥЁⱘᶤ㡖⚍ऍ䜡˄䖭⾡ᚙ ⱘऩ䆡䖯㸠↨䕗ˈ䱣ৢˈ䗮䖛䗦ᔦټⱘ᳔乊ሖ˄ेᷥⱘḍ˅DŽ೼↣ϔℹЁˈᮄऩ䆡Ϣ㡖⚍Ёᄬ ߑ᭄ addtree ᰃ䗦ᔦⱘDŽЏߑ᭄ main ҹখ᭄ⱘᮍᓣӴ䗦㒭䆹ߑ᭄ⱘϔϾऩ䆡ᇚ԰Ўᷥ } return 0; treeprint(root); root = addtree(root, word); if (isalpha(word[0])) while (getword(word, MAXWORD) != EOF) root = NULL; char word[MAXWORD]; struct tnode *root; { main() /* word frequency count */ int getword(char *, int); void treeprint(struct tnode *); struct tnode *addtree(struct tnode *, char *); #define MAXWORD 100 #include #include #include ЁDŽ ↨བ getword ㄝDŽЏߑ᭄䗮䖛 getword 䇏ܹऩ䆡ˈᑊ䗮䖛 addtree ߑ᭄ᇚᅗӀᦦܹࠄᷥ བϟ᠔⼎ˈᭈϾ⿟ᑣⱘҷⷕ䴲ᐌⷁᇣDŽᔧ✊ˈᅗ䳔㽕៥Ӏࠡ䴶㓪ݭⱘϔѯ⿟ᑣⱘᬃᣕˈ }; struct t *q; /* q points to a t */ ... struct s { }; struct s *p; /* p points to an s */ ... struct t { ᇨгӮՓ⫼㞾ᓩ⫼㒧ᵘⱘϔ⾡বԧ˖ϸϾ㒧ᵘⳌѦᓩ⫼DŽ݋ԧⱘՓ⫼ᮍ⊩བϟ˖ي៥Ӏ ᅗᇚ left ໄᯢЎᣛ৥ tnode ⱘᣛ䩜ˈ㗠ϡᰃ tnode ᅲ՟ᴀ䑿DŽ struct tnode *left; ᅲ՟ⱘ㒧ᵘᰃ䴲⊩ⱘˈԚᰃˈϟ߫ໄᯢᰃড়⊩ⱘ˖ ᰃϡ⹂ᅮⱘˈԚᅗⱘ⹂ᰃℷ⹂ⱘDŽϔϾࣙ৿݊㞾䑿ڣ䖭⾡ᇍ㡖⚍ⱘ䗦ᔦⱘໄᯢᮍᓣⳟϞএད }; struct tnode *right; /* right child */ struct tnode *left; /* left child */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ߑ᭄ⱘϔ⾡ᮍ⊩DŽ ᑧߑ᭄ mallocˈᅗ㛑໳⒵䎇ᇍ唤㽕∖DŽ㄀ 8 ゴᇚҟ㒡ᅲ⦄ mallocޚᇍ唤ˈ᠔ҹˈ៥ӀՓ⫼ᷛ ぎ䯈DŽ㄀ 5 ゴҟ㒡ⱘ alloc ߑ᭄ϡֱ䆕ӏԩ⡍ᅮ㉏ൟⱘټ䩜ህৃҹњˈ݊ҷӋᰃ⡎⡆ϔѯᄬ ᇍ唤㽕∖ϔ㠀↨䕗ᆍᯧ⒵䎇ˈা䳔㽕⹂ֱߚ䜡⿟ᑣྟ㒜䖨ಲ⒵䎇᠔᳝ᇍ唤䰤ࠊ㽕∖ⱘᣛ 㛑䖨ಲϡৠ㉏ൟⱘᣛ䩜ⱘ䯂乬˛ ᭄ഄഔϞ˅ˈ㄀ѠˈՓ⫼ҔМḋⱘໄᯢ㛑໘⧚ߚ䜡⿟ᑣᖙ乏ي∖˄՟བˈᭈൟ䗮ᐌᖙ乏ߚ䜡೼ ⱘᣛ䩜ˈ߭Ӯߎ⦄ϸϾ䯂乬DŽ㄀ϔˈᅗབԩ೼໻໮᭄ᅲ䰙ᴎ఼Ϟ⒵䎇৘⾡㉏ൟᇍ䈵ⱘᇍ唤㽕 Ͼߚ䜡⿟ᑣᴹ໘⧚໮⾡㉏ൟⱘ䇋∖ˈ↨བᣛ৥ char ㉏ൟⱘᣛ䩜੠ᣛ৥ struct tnode ㉏ൟ ᅮ⫼ϔ؛ˈߚ䜡⿟ᑣDŽԚᰃټぎ䯈ˈԚᰒ✊ˈ⿟ᑣЁাӮ᳝ϔϾᄬټ㽕Ўϡৠⱘᇍ䈵ߚ䜡ᄬ ߚ䜡⿟ᑣ䳔ټߚ䜡⿟ᑣⱘ䯂乬DŽሑㅵᄬټ೼㒧ᴳ䆹՟ᄤПࠡˈ៥Ӏㅔऩ䅼䆎ϔϟ᳝݇ᄬ ⱘᕅડˈ೼ℸ៥Ӏϡ䅼䆎DŽމᡒⱘᓔ䫔ᇚ䴲ᐌ໻DŽᶤѯᑓНѠঝᷥϡফ䖭⾡᳔ണᚙ ϟˈ㢹ऩ䆡Ꮖ㒣ᥦདᑣˈ߭⿟ᑣ῵ᢳ㒓ᗻᶹމϟˈ⿟ᑣⱘ䖤㸠ᯊ䯈ᇚ໻໻๲ࡴDŽ᳔ണⱘᚙމ 䖭䞠᳝ϔ⚍ؐᕫ⊼ᛣ˖བᵰऩ䆡ϡᰃᣝ✻䱣ᴎⱘ乎ᑣࠄ䖒ⱘˈᷥᇚবᕫϡᑇ㸵ˈ䖭⾡ᚙ } } treeprint(p•>right); printf("%4d %s\n", p•>count, p•>word); treeprint(p•>left); if (p != NULL) { { void treeprint(struct tnode *p) /* treeprint: in•order print of tree p */ 䆱ˈϡོ೼Ϟ䴶ⱘᷥЁ῵ᢳ treeprint ⱘᠻ㸠䖛⿟DŽ ✊ৢᰃ䆹ऩ䆡ᴀ䑿ˈ᳔ৢᰃেᄤᷥ˄໻Ѣ䆹ऩ䆡ⱘ᠔᳝ऩ䆡˅DŽབᵰԴᇍ䗦ᔦ᪡԰᳝ѯ⭥ᚥⱘ ᠧॄᎺᄤᷥ˄ᇣѢ䆹ऩ䆡ⱘ᠔᳝ऩ䆡˅ˈܜtreeprint ߑ᭄ᣝ乎ᑣᠧॄᷥDŽ೼↣Ͼ㡖⚍ˈᅗ 㸠DŽ䆹⿟ᑣᗑ⬹њᇍ strdup ੠ talloc 䖨ಲؐⱘߎ䫭Ẕᶹ˄䖭ᰒ✊ᰃϡᅠ୘ⱘ˅DŽ 䅵᭄ؐᇚ㹿߱ྟ࣪ˈϸϾᄤᷥ㹿㕂Ўぎ˄NULL˅DŽ๲ࡴᮄ㡖⚍ᯊˈ䖭䚼ߚҷⷕা೼ᷥ৊䚼ߚᠻ ᷥ㡖⚍ⱘぎ䯆ぎ䯈DŽߑ᭄ strdup ᇚᮄऩ䆡໡ࠊࠄᶤϾ䱤㮣ԡ㕂˄⿡ৢᇚ䅼䆎䖭ѯᄤ⿟ᑣ˅DŽ ぎ䯈⬅ᄤ⿟ᑣ talloc 㦋ᕫDŽtalloc ߑ᭄䖨ಲϔϾᣛ䩜ˈᣛ৥㛑ᆍ㒇ϔϾټᮄ㡖⚍ⱘᄬ } return p; p•>right = addtree(p•>right, w); else /* greater than into right subtree */ p•>left = addtree(p•>left, w); else if (cond < 0) /* less than into left subtree */ p•>count++; /* repeated word */ } else if ((cond = strcmp(w, p•>word)) == 0) p•>left = p•>right = NULL; p•>count = 1; p•>word = strdup(w); p = talloc(); /* make a new node */ if (p == NULL) { /* a new word has arrived */ int cond; { struct treenode *addtree(struct tnode *p, char *w) addtree: add a node with w, at or below p */ */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᶤѯ䇁হЁᯊˈབ˖ П㉏ⱘ⿟ᑣ㸠ᯊˈህ䳔㽕ᡞৡᄫ IN ੠᳓ᤶ᭛ᴀ 1 ᄬܹࠄᶤϾ㸼ЁDŽℸৢˈᔧৡᄫ IN ߎ⦄೼ #define IN 1 䇁হDŽᔧ䘛ࠄ㉏ԐѢ 䖭↉ҷⷕᕜ݌ൟˈৃҹ೼ᅣ໘⧚఼៪㓪䆥఼ⱘヺো㸼ㅵ⧚՟⿟ЁᡒࠄDŽ՟བˈ㗗㰥#define Ўњᇍ㒧ᵘⱘ᳈໮ᮍ䴶䖯㸠⏅ܹⱘ䅼䆎ˈ៥Ӏᴹ㓪ݭϔϾ㸼ᶹᡒ⿟ᑣࣙⱘḌᖗ䚼ߚҷⷕDŽ 6.6. 㸼ᶹᡒ ↣Ͼऩ䆡ⱘࠡ䴶ᷛϞᅗⱘߎ⦄⃵᭄DŽ 㒗д 6•4 㓪ݭϔϾ⿟ᑣˈḍ᥂ऩ䆡ⱘߎ⦄乥⥛ᣝ䰡ᑣᠧॄ䕧ܹⱘ৘Ͼϡৠऩ䆡ˈᑊ೼ Ͼ߫㸼ˈ䆄ᔩߎ⦄䖛䆹ऩ䆡ⱘ㸠োDŽᇍ theǃand ㄝ䴲ᅲНऩ䆡ϡќ㗗㰥DŽ 㒗д 6•3 㓪ݭϔϾѸঝᓩ⫼⿟ᑣˈᠧॄ᭛ḷЁ᠔᳝ऩ䆡ⱘ߫㸼ˈᑊϨ↣Ͼऩ䆡䖬᳝ϔ 䇋ᇚ 6 ԰ЎϔϾৃ೼ੑҸ㸠Ё䆒ᅮⱘখ᭄DŽ 㽕∖↣ϔ㒘ݙ৘ব䞣ৡⱘࠡ 6 ϾᄫヺⳌৠˈ݊ԭᄫヺϡৠDŽᄫヺІ੠⊼䞞Ёⱘऩ䆡ϡќ㗗㰥DŽ 㒗д 6•2 㓪ݭϔϾ⿟ᑣˈ⫼ҹ䇏ܹϔϾ C 䇁㿔⿟ᑣˈᑊᣝᄫ↡㸼乎ᑣߚ㒘ᠧॄব䞣ৡˈ 㾕㄀ 7 ゴ੠㄀ 8 ゴDŽ ぎ䯈ৃҹ䗮䖛䇗⫼ free ߑ᭄䞞ᬒҹ䞡⫼DŽ䆺㒚ֵᙃ䇋খټ䇗⫼ malloc ߑ᭄ᕫࠄⱘᄬ ߑ᭄ⱘ䇗⫼㗙䋳䋷ߎ䫭໘⧚DŽ ೼≵᳝ৃ⫼ぎ䯈ᯊˈmalloc ߑ᭄䖨ಲ NULLˈৠᯊˈstrdup ߑ᭄гᇚ䖨ಲ NULLˈstrdup } return p; strcpy(p, s); if (p != NULL) p = (char *) malloc(strlen(s)+1); /* +1 for '\0' */ char *p; { char *strdup(char *s) /* make a duplicate of s */ malloc ߑ᭄ᅲ⦄ⱘ˖ strdup ߑ᭄াᰃᡞ䗮䖛݊খ᭄ӴܹⱘᄫヺІ໡ࠊࠄᶤϾᅝܼⱘԡ㕂DŽᅗᰃ䗮䖛䇗⫼ } return (struct tnode *) malloc(sizeof(struct tnode)); { struct tnode *talloc(void) /* talloc: make a tnode */ #include ӊЁDŽ಴ℸˈৃҹᡞ talloc ߑ᭄ݭ៤ϟ߫ᔶᓣ˖ ༈᭛ޚൟⱘᣛ䩜ˈ✊ৢݡᰒᓣഄᇚ䆹ᣛ䩜ᔎࠊ䕀ᤶЎ᠔䳔㉏ൟDŽmalloc ঞⳌ݇ߑ᭄ໄᯢ೼ᷛ ༈⮐ⱘ䯂乬DŽ೼ C 䇁㿔Ёˈϔ⾡ড়䗖ⱘᮍ⊩ᰃᇚ malloc ⱘ䖨ಲؐໄᯢЎϔϾᣛ৥ void ㉏ malloc 䖭ḋⱘߑ᭄ⱘ㉏ൟໄᯢᘏᰃᕜҸҎ ڣˈᇍѢӏԩᠻ㸠ϹḐ㉏ൟẔᶹⱘ䇁㿔ᴹ䇈 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᬷ߫䖛⿟⫳៤њ೼᭄㒘 hashtab Ёᠻ㸠ᶹᡒⱘ䍋ྟϟᷛDŽབᵰ䆹ᄫヺІৃҹ㹿ᶹᡒࠄˈ ⬅Ѣ೼ᬷ߫䅵ㅫᯊ䞛⫼ⱘᰃ᮴ヺোㅫᴃ䖤ㅫˈ಴ℸֱ䆕њᬷ߫ؐ䴲䋳DŽ } return hashval % HASHSIZE; hashval = *s + 31 * hashval; for (hashval = 0; *s != '\0'; s++) unsigned hashval; { unsigned hash(char *s) /* hash: form hash value for string s */ ῵᪡԰ˈ݊㒧ᵰेᰃ䆹ߑ᭄ⱘ䖨ಲؐDŽ䖭ᑊϡᰃ᳔དⱘᬷ߫ߑ᭄ˈԚ↨䕗ㅔⷁ᳝ᬜDŽ ৠᄫヺІЁᔧࠡᄫヺⱘؐⳌࡴ(*s + 31 * hashval)ˈ✊ৢᇚ䆹㒧ᵰؐৠ᭄㒘䭓ᑺᠻ㸠প ㅫˈ↣⃵ᕾ⦃ЁˈᅗᇚϞϔ⃵ᕾ⦃Ё䅵ㅫᕫࠄⱘ㒧ᵰؐ㒣䖛বᤶ˄ेЬҹ 31˅ৢᕫࠄⱘᮄؐ ᬷ߫ߑ᭄ hash ೼ lookup ੠ install ߑ᭄Ё䛑㹿⫼ࠄˈᅗ䗮䖛ϔϾ for ᕾ⦃䖯㸠䅵 static struct nlist *hashtab[HASHSIZE]; /* pointer table */ #define HASHSIZE 101 Ⳍᑨⱘᣛ䩜᭄㒘ᅮНབϟ˖ }; char *defn; /* replacement text */ char *name; /* defined name */ struct nlist *next; /* next entry in chain */ struct nlist { /* table entry: */ ҹঞϔϾᣛ৥䆹䫒㸼ৢ㒻ഫⱘᣛ䩜DŽབᵰᣛ৥䫒㸼ৢ㒻ഫⱘᣛ䩜Ў NULLˈ߭㸼ᯢ䫒㸼㒧ᴳDŽ 䫒㸼Ёⱘ↣Ͼഫ䛑ᰃϔϾ㒧ᵘˈᅗࣙ৿ϔϾᣛ৥ৡᄫⱘᣛ䩜ǃϔϾᣛ৥᳓ᤶ᭛ᴀⱘᣛ䩜 ೒ 6•4 ㋴ⱘؐЎ NULL˄খ㾕೒ 6•4˅DŽܗ䗄݋᳝䆹ᬷ߫ؐⱘৡᄫDŽབᵰ≵᳝ৡᄫᬷ߫ࠄ䆹ؐˈ᭄߭㒘 ㋴ᣛ৥ᶤϾ䫒㸼ⱘ㸼༈ˈ䫒㸼Ёⱘ৘Ͼഫ⫼Ѣᦣܗᇚ԰ЎϔϾᣛ䩜᭄㒘ⱘϟᷛDŽ᭄㒘ⱘ↣Ͼ 䆹ㅫ⊩䞛⫼ⱘᰃᬷ߫ᶹᡒᮍ⊩——ᇚ䕧ܹⱘৡᄫ䕀ᤶЎϔϾᇣⱘ䴲䋳ᭈ᭄ˈ䆹ᭈ᭄䱣ৢ ಲᣛ৥䆹໘ⱘᣛ䩜˗㢹≵ᡒࠄˈ߭䖨ಲ NULLDŽ 䆄ᔩࠄᶤϾ㸼Ёˈ݊Ё s ੠ t ҙҙᰃᄫヺІDŽlookup(s)ߑ᭄೼㸼Ёᶹᡒ sˈ㢹ᡒࠄˈ߭䖨 ҹϟϸϾߑ᭄⫼ᴹ໘⧚ৡᄫ੠᳓ᤶ᭛ᴀDŽinstall(s, t)ߑ᭄ᇚৡᄫ s ੠᳓ᤶ᭛ᴀ t ህᖙ乏⫼ 1 ᴹ᳓ᤶ INDŽ statet = IN; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! C 䇁㿔ᦤկњϔϾ⿄Ў typedef ⱘࡳ㛑ˈᅗ⫼ᴹᓎゟᮄⱘ᭄᥂㉏ൟৡˈ՟བˈໄᯢ 6.7. ㉏ൟᅮН˄typedef˅ DŽԴӮথ⦄ getch ੠ ungetch ߑ᭄䴲ᐌ᳝⫼DŽ˅މ఼ⱘㅔऩ⠜ᴀ˄े᮴খ᭄ⱘᚙ 㒗д 6•6 ҹᴀ㡖ҟ㒡ⱘߑ᭄Ў෎⸔ˈ㓪ݭϔϾ䗖ড় C 䇁㿔⿟ᑣՓ⫼ⱘ#define ໘⧚ ঞ݊ᅮНDŽ 㒗д 6•5 㓪ݭߑ᭄ undefˈᅗᇚҢ⬅ lookup ੠ install 㓈ᡸⱘ㸼Ёߴ䰸ϔϾব䞣 } return np; return NULL; if ((np•>defn = strdup(defn)) == NULL) free((void *) np•>defn); /*free previous defn */ } else /* already there */ hashtab[hashval] = np; np•>next = hashtab[hashval]; hashval = hash(name); return NULL; if (np == NULL || (np•>name = strdup(name)) == NULL) np = (struct nlist *) malloc(sizeof(*np)); if ((np = lookup(name)) == NULL) { /* not found */ unsigned hashval; struct nlist *np; { struct nlist *install(char *name, char *defn) /* install: put (name, defn) in hashtab */ char *strdup(char *); struct nlist *lookup(char *); NULLDŽ ⱘᅮНপ㗠ҷП˗৺߭ˈ߯ᓎϔϾᮄ㸼乍DŽབ᮴䎇໳ぎ䯈߯ᓎᮄ㸼乍ˈ߭ install ߑ᭄䖨ಲ ࡽ lookup ߑ᭄߸ᮁᕙࡴܹⱘৡᄫᰃ৺Ꮖ㒣ᄬ೼DŽབᵰᏆᄬ೼ˈ߭⫼ᮄ׳install ߑ᭄ ... for (ptr = head; ptr != NULL; ptr = ptr•>next) ᮍ⊩ˈབϟ᠔⼎˖ޚlookup ߑ᭄Ёⱘ for ᕾ⦃ᰃ䘡ग़ϔϾ䫒㸼ⱘᷛ } return NULL; /* not found */ return np; /* found */ if (strcmp(s, np•>name) == 0) for (np = hashtab[hash(s)]; np != NULL; np = np•>next) struct nlist *np; { struct nlist *lookup(char *s) /* lookup: look for s in hashtab */ lookup ߑ᭄থ⦄㸼乍Ꮖᄬ೼ˈ߭䖨ಲᣛ৥䆹㸼乍ⱘᣛ䩜ˈ৺߭䖨ಲ NULLDŽ ߭ᅗϔᅮԡѢ䆹䍋ྟϟᷛᣛ৥ⱘ䫒㸼ⱘᶤϾഫЁDŽ݋ԧᶹᡒ䖛⿟⬅ lookup ߑ᭄ᅲ⦄DŽབᵰ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! খ᭄࣪ˈҹᦤ催⿟ᑣⱘৃ⿏ỡᗻDŽབᵰ typedef ໄᯢⱘ᭄᥂㉏ൟৠᴎ఼᳝݇ˈ䙷Мˈᔧ⿟ᑣ ᅗৃҹՓ⿟ᑣˈܜ䰸њ㸼䖒ᮍᓣ᳈ㅔ⋕П໪ˈՓ⫼ typedef 䖬᳝঺໪ϸϾ䞡㽕ॳ಴DŽ佪 PFI strcmp, numcmp; ಲؐ㉏ൟЎ intāˈᅗৃ⫼ѢᶤѯϞϟ᭛Ёˈ՟བˈৃҹ⫼೼㄀ 5 ゴⱘᥦᑣ⿟ᑣЁˈབϟ᠔⼎˖ 䆹䇁হᅮНњ㉏ൟ PFI ᰃĀϔϾᣛ৥ߑ᭄ⱘᣛ䩜ˈ䆹ߑ᭄݋᳝ϸϾ char *㉏ൟⱘখ᭄ˈ䖨 typedef int (*PFI)(char *, char *); 乘໘⧚఼ⱘ㛑࡯DŽ՟བ˖ ㉏ԐѢ#define 䇁হˈԚ⬅Ѣ typedef ᰃ⬅㓪䆥఼㾷䞞ⱘˈ಴ℸᅗⱘ᭛ᴀ᳓ᤶࡳ㛑㽕䍙䖛 䖭⾡ᮍᓣໄᯢⱘব䞣Ϣ䗮䖛᱂䗮ໄᯢᮍᓣໄᯢⱘব䞣݋᳝ᅠܼⳌৠⱘሲᗻDŽᅲ䰙Ϟˈtypedef ᶤϾᏆᄬ೼ⱘ㉏ൟ๲ࡴњϔϾᮄⱘৡ⿄㗠ᏆDŽtypedef ໄᯢг≵᳝๲ࡴӏԩᮄⱘ䇁Н˖䗮䖛 䖭䞠ᖙ乏ᔎ䇗ⱘᰃˈҢӏԩᛣНϞ䆆ˈtypedef ໄᯢᑊ≵᳝߯ᓎϔϾᮄ㉏ൟˈᅗাᰃЎ } return (Treeptr) malloc(sizeof(Treenode)); { Treeptr talloc(void) 㒧ᵘⱘᣛ䩜˅DŽ䖭ḋˈߑ᭄ talloc ৃⳌᑨഄׂᬍЎ˖ Ϟ䗄㉏ൟᅮН߯ᓎњϸϾᮄ㉏ൟ݇䬂ᄫ˖Treenode˄ϔϾ㒧ᵘ˅੠ Treeptr˄ϔϾᣛ৥䆹 } Treenode; struct tnode *right; /* right child */ struct tnode *left; /* left child */ int count; /* number of occurrences */ char *word; /* points to the text */ typedef struct tnode { /* the tree node: */ typedef struct tnode *Treeptr; 䖭䞠ВϔϾ᳈໡ᴖⱘ՟ᄤ˖⫼ typedef ᅮНᴀゴࠡ䴶ҟ㒡ⱘᷥ㡖⚍DŽབϟ᠔⼎˖ typedef ᅮНⱘ㉏ൟৡⱘ佪ᄫ↡ˈҹ⼎ऎ߿DŽ ㉏ externǃstatic ㄝDŽ៥Ӏ೼䖭䞠ҹ໻ݭᄫ↡԰ЎټৢDŽtypedef ೼䇁⊩Ϟ㉏ԐѢᄬ ⊼ᛣˈtypedef Ёໄᯢⱘ㉏ൟ೼ব䞣ৡⱘԡ㕂ߎ⦄ˈ㗠ϡᰃ㋻᥹೼݇䬂ᄫ typedef П p = (String) malloc(100); int strcmp(String, String); String p, lineptr[MAXLINES], alloc(int); Stringˈ՟བ˖ ᇚ String ᅮНЎϢ char *៪ᄫヺᣛ䩜ৠНˈℸৢˈ֓ৃҹ೼㉏ൟໄᯢ੠㉏ൟ䕀ᤶЁՓ⫼ typedef char* String˗ ㉏Ԑഄˈໄᯢ Length *lengths[]; Length len, maxlen; ᅗ੠㉏ൟ int ᅠܼⳌৠˈ՟བ˖ ᇚ Length ᅮНЎϢ int ݋᳝ৠㄝᛣНⱘৡᄫDŽ㉏ൟ Length ৃ⫼Ѣ㉏ൟໄᯢǃ㉏ൟ䕀ᤶㄝˈ typedef int Length; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᅮ᳝ϟ߫ⱘ㒧ᵘ᭄㒘ᅮН˖؛ˈ㸼⼎⊩Ϣጠ༫㒧ᵘⳌৠDŽ՟བ 㘨ড়ৃҹՓ⫼೼㒧ᵘ੠᭄㒘ЁˈডПѺৃDŽ䆓䯂㒧ᵘЁⱘ㘨ড়˄៪ডП˅ⱘᶤϔ៤ਬⱘ printf("bad type %d in utype\n", utype); else printf("%s\n", u.sval); if (utype == STRING) printf("%f\n", u.fval); if (utype == FLOAT) printf("%d\n", u.ival); if (utype == INT) 䴶䖭ḋՓ⫼㘨ড়˖ ϟڣᅗϢ䆓䯂㒧ᵘⱘᮍᓣⳌৠDŽབᵰ⫼ব䞣 utype 䎳䏾ֱᄬ೼ u Ёⱘᔧ᭄ࠡ᥂㉏ൟˈ߭ৃҹ ᄗწ•>юၔۦ॓ ៪ ੠.юၔۦ॓ ˖ৃҹ䗮䖛ϟ߫䇁⊩䆓䯂㘨ড়Ёⱘ៤ਬ Ѣ݋ԧⱘᅲ⦄DŽއབᵰֱᄬⱘ㉏ൟϢ䇏পⱘ㉏ൟϡϔ㟈ˈ݊㒧ᵰপ 㟈ⱘ˖䇏পⱘ㉏ൟᖙ乏ᰃ᳔䖥ϔ⃵ᄬܹⱘ㉏ൟDŽ⿟ᑣਬ䋳䋷䎳䏾ᔧֱࠡᄬ೼㘨ড়Ёⱘ㉏ൟDŽ ѯ㉏ൟЁⱘӏԩϔ⾡㉏ൟⱘᇍ䈵䛑ৃ䌟ؐ㒭 uˈϨৃՓ⫼೼䱣ৢⱘ㸼䖒ᓣЁˈԚᖙ乏ֱ䆕ᰃϔ ব䞣 u ᖙ乏䎇໳໻ˈҹֱᄬ䖭 3 ⾡㉏ൟЁ᳔໻ⱘϔ⾡ˈ݋ԧ䭓ᑺৠ݋ԧⱘᅲ⦄᳝݇DŽ䖭 } u; char *sval; float fval; int ival; union u_tag { ⊩෎Ѣ㒧ᵘˈབϟ᠔⼎˖ 䖭ህᰃ㘨ড়ⱘⳂⱘ——ϔϾব䞣ৃҹড়⊩ഄֱᄬ໮⾡᭄᥂㉏ൟЁӏԩϔ⾡㉏ൟⱘᇍ䈵DŽ݊䇁 ぎ䯈ˈϨֱᄬ೼ৠϔϾഄᮍⱘ䆱ˈ㸼ㅵ⧚ᇚ᳔ᮍ֓DŽټ䆹ᐌ䞣ⱘϡৠ㉏ൟऴ᥂Ⳍৠ໻ᇣⱘᄬ ᰃ intǃf1oat ៪ᄫヺᣛ䩜DŽ⡍ᅮ㉏ൟⱘᐌ䞣ؐᖙ乏ֱᄬ೼ড়䗖㉏ൟⱘব䞣Ёˈ✊㗠ˈབᵰ 䆒ϔϾᐌ䞣ৃ㛑؛៥ӀᴹⳟϔϾ՟ᄤ˄ৃҹ೼㓪䆥఼ⱘヺো㸼ㅵ⧚⿟ᑣЁᡒࠄ䆹՟ᄤ˅DŽ ೼⿟ᑣЁጠܹӏԩৠᴎ఼᳝݇ⱘֵᙃDŽᅗ㉏ԐѢ Pascal 䇁㿔Ёⱘবԧ䆄ᔩDŽ ऎЁㅵ⧚ϡৠ㉏ൟⱘ᭄᥂ˈ㗠ϡ䳔㽕ټ䭓ᑺ੠ᇍ唤㽕∖DŽ㘨ড়ᦤկњϔ⾡ᮍᓣˈҹ೼ऩഫᄬ 㘨ড়ᰃৃҹ˄೼ϡৠᯊࠏ˅ֱᄬϡৠ㉏ൟ੠䭓ᑺⱘᇍ䈵ⱘব䞣ˈ㓪䆥఼䋳䋷䎳䏾ᇍ䈵ⱘ 6.8. 㘨ড় Ўᣛ৥໡ᴖ㒧ᵘⱘᣛ䩜᳈ᆍᯧ䅽Ҏ⧚㾷DŽ typedef ⱘ㄀ѠϾ԰⫼ᰃЎ⿟ᑣᦤկ᳈དⱘ䇈ᯢᗻ——Treeptr ㉏ൟᰒ✊↨ϔϾໄᯢ size_t ੠ ptrdiff_t ㄝDŽ ᑧЁ᳝ϔѯ՟ᄤˈ՟བޚᆓЏᴎ䗝ᢽϔ㒘ড়䗖ⱘ shortǃint ੠ long ㉏ൟ໻ᇣेৃDŽᷛ ৘⾡ϡৠ໻ᇣⱘᭈൟؐᴹ䇈ˈ䛑Փ⫼䗮䖛 typedef ᅮНⱘ㉏ൟৡˈ✊ৢˈߚ߿Ў৘Ͼϡৠⱘ ᰃˈᇍѢމỡࠄ݊ᅗᴎ఼Ϟᯊˈা䳔ᬍব typedef ㉏ൟᅮНህৃҹњDŽϔϾ㒣ᐌ⫼ࠄⱘᚙ⿏ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ϟ߫䇁হ೼⿟ᑣЁ㒣ᐌߎ⦄˖ ঞ㸹ⷕ䖤ㅫ䖯㸠ㅔऩⱘԡ᪡԰DŽ 䖭ѯ᭄ᄫᖙ乏ᰃ 2 ⱘᐖDŽ䖭ḋˈ䆓䯂䖭ѯԡህব៤њ⫼㄀ 2 ゴЁᦣ䗄ⱘ⿏ԡ䖤ㅫǃሣ㬑䖤ㅫ enum { KEYWORD = 01, EXTERNAL = 02, STATIC = 04 }; ៪ #define STATIC 04 #define EXTRENAL 02 #define KEYWORD 01 䗮ᐌ䞛⫼ⱘᮍ⊩ᰃˈᅮНϔϾϢⳌ݇ԡⱘԡ㕂ᇍᑨⱘĀሣ㬑ⷕā䲚ড়ˈབ˖ ᳔ㅔ⋕ⱘᮍ⊩ህᰃՓ⫼ϔϾ char ៪ int ᇍ䈵Ёⱘԡᷛᖫ䲚ড়DŽ ՟བˈᅗᰃ৺Ў݇䬂ᄫˈᅗᰃ৺ᰃ໪䚼ⱘϨ˄៪˅ᰃ䴭ᗕⱘˈㄝㄝDŽᇍ䖭ѯֵᙃ䖯㸠㓪ⷕⱘ 㗗㰥㓪䆥఼Ёヺো㸼᪡԰ⱘ᳝݇㒚㡖DŽ⿟ᑣЁⱘ↣Ͼᷛ䆚ヺ䛑᳝ϢПⳌ݇ⱘ⡍ᅮֵᙃˈ 䆒໛᥹ষ˅г㒣ᐌ䳔㽕Ңᄫⱘ䚼ߚؐЁ䇏প᭄᥂DŽ ᮍ⊩ᰃˈՓ⫼㉏ԐѢ㓪䆥఼ヺো㸼ⱘऩϾѠ䖯ࠊԡᷛᖫ䲚ড়DŽ໪䚼ᔎࡴⱘ᭄᥂Ḑᓣ˄བ⹀ӊ ϟˈ᳝ৃ㛑䳔㽕ᇚ໮Ͼᇍ䈵ֱᄬ೼ϔϾᴎ఼ᄫЁDŽϔ⾡ᐌ⫼ⱘމぎ䯈ᕜᅱ䌉ⱘᚙټ೼ᄬ 6.9. ԡᄫ↉ 唤DŽ 䖍⬠Ϟᇍټߚ䜡⿟ᑣᇚ䇈ᯢབԩՓ⫼㘨ড়ᴹᔎࠊϔϾব䞣೼⡍ᅮ㉏ൟⱘᄬټ㄀ 8 ゴⱘᄬ ྟ࣪DŽ 㘨ড়া㛑⫼݊㄀ϔϾ៤ਬ㉏ൟⱘؐ䖯㸠߱ྟ࣪ˈ಴ℸˈϞ䗄㘨ড় u া㛑⫼ᭈ᭄ؐ䖯㸠߱ ЁϔϾ៤ਬDŽ 䖯㸠䌟ؐǃ໡ࠊǃপഄഔঞ䆓䯂݊ܗ䆌ⱘ᪡԰Ⳍৠ˖԰ЎϔϾᭈԧऩܕ䆌ⱘ᪡԰Ϣᇍ㒧ᵘܕ ໻ࠄ䎇໳ᆍ㒇᳔Āᆑāⱘ៤ਬˈᑊϨˈ݊ᇍ唤ᮍᓣ㽕䗖ড়Ѣ㘨ড়Ё᠔᳝㉏ൟⱘ៤ਬDŽᇍ㘨ড় 䞣䛑Ў 0ˈℸ㒧ᵘぎ䯈㽕⿏أᅲ䰙Ϟˈ㘨ড়ህᰃϔϾ㒧ᵘˈᅗⱘ᠔᳝៤ਬⳌᇍѢ෎ഄഔⱘ symtab[i].u.sval[0] *symtab[i].u.sval гৃҹ䗮䖛ϟ߫䇁হПϔᓩ⫼ᄫヺІ sval ⱘ㄀ϔϾᄫヺ˖ symtab[i].u.ival ৃҹ䗮䖛ϟ߫䇁হᓩ⫼݊៤ਬ ival˖ } symtab[NSYM]; } u; char *sval; float fval; int ival; union { int utype; int flags; char *name; struct { w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! Ϩ≵᳝ഄഔˈ಴ℸᇍᅗӀϡ㛑Փ⫼&䖤ㅫヺDŽ њᮍ֓⿏ỡˈ䳔㽕ᰒᓣໄᯢ䆹 int ㉏ൟᰃ signed 䖬ᰃ unsigned ㉏ൟDŽᄫ↉ϡᰃ᭄㒘ˈᑊ ⱘ䯂乬DŽձ䌪Ѣ䖭ѯ಴㋴ⱘ⿟ᑣᰃϡৃ⿏ỡⱘDŽᄫ↉гৃҹҙҙໄᯢЎ intˈЎܜ㰥ાッӬ ϟˈᖙ乏Ҩ㒚㗗މሑㅵᄫ↉ᇍ㓈ᡸݙ䚼ᅮНⱘ᭄᥂㒧ᵘᕜ᳝⫼ˈԚ೼䗝ᢽ໪䚼ᅮН᭄᥂ⱘᚙ ᶤѯᴎ఼Ϟᄫ↉ⱘߚ䜡ᰃҢᄫⱘᎺッ㟇েッ䖯㸠ⱘˈ㗠ᶤѯᴎ఼Ϟ߭ⳌডDŽ䖭ᛣੇⴔˈ ೼ϟϔϾᄫ䖍⬠Ϟᇍ唤DŽ ԰⫼DŽ⡍⅞ᆑᑺ 0 ৃҹ⫼ᴹᔎࠊܙ฿ᄫ↉ৃҹϡੑৡˈ᮴ৡᄫ↉˄া᳝ϔϾݦো੠ᆑᑺ˅䍋 ᄫ↉ⱘ᠔᳝ሲᗻ޴Т䛑ৠ݋ԧⱘᅲ⦄᳝݇DŽᄫ↉ᰃ৺㛑㽚Ⲫᄫ䖍⬠⬅݋ԧⱘᅲ⦄ᅮНDŽ ⫼Ѣᇍ is_extern ੠ is_static ԡ䖯㸠⌟䆩DŽ ... if (flags.is_extern == 0 && flags.is_static == 0) ᇚ is_extern ੠ is_static ԡ㕂Ў 0DŽϟ߫䇁হ˖ flags.is_extern = flags.is_static = 0; 䆹䇁হᇚ is_extern ੠ is_static ԡ㕂Ў 1DŽϟ߫䇁হ˖ flags.is_extern = flags.is_static = 1; 㸼䖒ᓣЁDŽ಴ℸˈϞ䴶ⱘ՟ᄤৃ⫼᳈㞾✊ⱘᮍᓣ㸼䖒Ў˖ flags.is_extern ㄝㄝDŽᄫ↉ⱘ԰⫼Ϣᇣᭈ᭄ⳌԐDŽৠ݊ᅗᭈ᭄ϔḋˈᄫ↉ৃߎ⦄೼ㅫᴃ ऩϾᄫ↉ⱘᓩ⫼ᮍᓣϢ݊ᅗ㒧ᵘ៤ਬⳌৠˈ՟བ˖flags.is_keyword ǃ 䖯ࠊԡ᭄㸼⼎˅DŽᄫ↉㹿ໄᯢЎ unsigned int ㉏ൟˈҹֱ䆕ᅗӀᰃ᮴ヺো䞣DŽ 䖭䞠ᅮНњϔϾব䞣 flagsˈᅗࣙ৿ 3 Ͼϔԡⱘᄫ↉DŽݦোৢⱘ᭄ᄫ㸼⼎ᄫ↉ⱘᆑᑺ˄⫼Ѡ } flags; unsigned int is_static : 1; unsigned int is_extern : 1; unsigned int is_keyword : 1; struct { བˈϞ䗄ヺো㸼ⱘ໮Ͼ#define 䇁হৃ⫼ϟ߫ 3 Ͼᄫ↉ⱘᅮНᴹҷ᳓˖ ᅗৠ݋ԧⱘᅲ⦄᳝݇DŽ՟ˈܗऩټ↉ˈᰃĀᄫāЁⳌ䚏ԡⱘ䲚ড়DŽĀᄫā˄word˅ᰃऩϾⱘᄬ 䆓䯂ϔϾᄫЁⱘԡᄫ↉ⱘ㛑࡯ˈ㗠ϡ䳔㽕䗮䖛ᣝԡ䘏䕥䖤ㅫヺDŽสᆓխ˄bit•field˅ˈ៪ㅔ⿄ᄫ ሑㅵ䖭ѯᮍ⊩ᕜᆍᯧᥠᦵˈԚᰃˈC 䇁㿔ҡ✊ᦤկњ঺ϔ⾡ৃ᳓ҷⱘᮍ⊩ˈेⳈ᥹ᅮН੠ ⱘؐЎⳳDŽ if ((flags & (EXTERNAL | STATIC)) == 0) ... ߭ᇚᅗӀ㕂Ў 0DŽᑊϨˈᔧ䖭ϸԡ䛑Ў 0 ᯊˈϟ߫㸼䖒ᓣ˖ flags &= ~(EXTERNAL | STATIC); 䆹䇁হᇚ flags Ёⱘ EXTERNAL ੠ STATIC ԡ㕂Ў 1ˈ㗠ϟ߫䇁হ˖ flags |= EXTERNAL | STATIC; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ߑ᭄ 䕧ܹϞDŽޚ⿟ᑣ prog ⱘᷛ 䕧ߎ䗮䖛ㅵ䘧䞡ᅮ৥ࠄޚᇚ䖤㸠ϸϾ⿟ᑣ otherprog ੠ progˈᑊᇚ⿟ᑣ otherprog ⱘᷛ otherprog | prog ㋏㒳Ёˈϟ߫ੑҸ㸠˖ བᵰ䕧ܹ䗮䖛ㅵ䘧ᴎࠊᴹ㞾Ѣ঺ϔϾ⿟ᑣˈ䙷М䖭⾡䕧ܹߛᤶгᰃϡৃ㾕ⱘDŽ↨བˈ೼ᶤѯ 䑿ᑊϡ೼ᛣ䕧ܹᮍᓣⱘᬍবˈᑊϨˈᄫヺІ"ЁᅮНˈ݊ؐϔ㠀Ў•1ˈԚ⿟ᑣЁᑨ䆹Փ⫼ EOF ᴹ⌟䆩᭛ӊᰃ৺ getchar ߑ᭄೼↣⃵㹿䇗⫼ᯊ䖨ಲϟϔϾ䕧ܹᄫヺDŽ㢹䘛ࠄ᭛ӊ㒧ሒˈ߭䖨ಲ EOFDŽヺোᐌ int getchar(void) 䕧ܹЁ˄ϔ㠀Ў䬂Ⲭ˅ϔ⃵䇏পϔϾᄫヺ˖ޚ᳔ㅔऩⱘ䕧ܹᴎࠊᰃՓ⫼ getchar ߑ᭄Ңᷛ ೼䕧ߎッ䖯㸠ড৥䕀ᤶDŽ ᑧৃҹ೼䕧ܹッᇚಲ䔺ヺ੠ᤶ㸠ヺ䛑䕀ᤶЎᤶ㸠ヺˈ㗠ޚᕫ䆹㋏㒳䗖ᑨ䖭⾡῵ᓣDŽ՟བˈᷛ ᑧᇚ䗮䖛ϔѯ᥾ᮑՓޚ៤ˈ↣ϔ㸠ⱘ㒧ሒᰃϔϾᤶ㸠ヺDŽབᵰ㋏㒳≵᳝䙉ᕾ䖭⾡῵ᓣˈ߭ᷛ ᑧᅲ⦄њㅔऩⱘ᭛ᴀ䕧ܹˋ䕧ߎ῵ᓣDŽ᭛ᴀ⌕⬅ϔ㋏߫㸠㒘ޚ៥Ӏ೼㄀ 1 ゴЁ䆆䖛ˈᷛ 䕧ܹˋ䕧ߎޚ7.1. ᷛ ᑧ䖯㸠њ䆺㒚ⱘᦣ䗄DŽޚᑧ㓪ݭ C 䇁㿔⿟ᑣDŽ䰘ᔩ B ᇍᷛޚ⫼ᷛ ᑧ䛑㔫߫Ѣℸˈ಴Ў៥Ӏ᳈݇ᖗབԩՓޚDŽ៥ӀϡᠧㅫᡞᭈϾᷛ 䖭ѯᑧߑ᭄ⱘሲᗻߚ߿೼क໮Ͼ༈᭛ӊЁໄᯢˈࠡ䴶Ꮖ㒣䘛ࠄ䖛ϔ䚼ߚˈབǃ ҢϔϾ㋏㒳⿏ỡࠄ঺ϔϾ㋏㒳ЁDŽ ᑧᦤկⱘࡳ㛑ˈ߭ৃҹϡ㒣ׂᬍഄޚ᭄ⱘݐᆍᔶᓣDŽབᵰ⿟ᑣⱘ㋏㒳ѸѦ䚼ߚҙҙՓ⫼њᷛ ㊒⹂ഄᅮНњ䖭ѯᑧߑ᭄ˈ᠔ҹˈ೼ӏԩৃҹՓ⫼ C 䇁㿔ⱘ㋏㒳Ё䛑᳝䖭ѯߑޚANSI ᷛ ݊ᅗϔѯ C 䇁㿔⿟ᑣⱘࡳ㛑DŽᴀゴ䅼䆎ⱘ䞡⚍ᇚᬒ೼䕧ܹˋ䕧ߎϞDŽ ㅵ⧚ߑ᭄Ϣ᭄ᄺߑ᭄ˈҹঞټᑧˈҟ㒡ϔѯ䕧ܹˋ䕧ߎߑ᭄ǃᄫヺІ໘⧚ߑ᭄ǃᄬޚ䆆䗄ᷛ 㽕໡ᴖᕜ໮ˈᴀゴᇚމ䇗ᅗӀDŽԚᰃˈ⿟ᑣϢ⦃๗П䯈ⱘѸѦ↨៥Ӏ೼ࠡ䴶䚼ߚЁᦣ䗄ⱘᚙ 䕧ܹˋ䕧ߎࡳ㛑ᑊϡᰃ C 䇁㿔ᴀ䑿ⱘ㒘៤䚼ߚˈ᠔ҹࠄⳂࠡЎℶˈ៥Ӏᑊ≵᳝䖛໮ഄᔎ ㄀7ゴ䕧ܹϢ䕧ߎ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䕧ߎߑ᭄ printf ᇚݙ䚼᭄ؐ䕀ᤶЎᄫヺⱘᔶᓣDŽࠡ䴶ⱘ᳝݇ゴ㡖ЁᏆ㒣Փ⫼䖛䆹ߑ᭄DŽ 7.2. Ḑᓣ࣪䕧ߎ——printf ߑ᭄ ݭᄫ↡䕀ᤶЎᇣݭᄫ↡៪ᇚᇣݭᄫ↡䕀ᤶЎ໻ݭᄫ↡ⱘࡳ㛑DŽ 㒗д 7•1 㓪ݭϔϾ⿟ᑣˈḍ᥂ᅗ㞾䑿㹿䇗⫼ᯊᄬᬒ೼ argv[0]Ёⱘৡᄫˈᅲ⦄ᇚ໻ ᰃབԩᅲ⦄ⱘˈՓ⫼䖭ѯߑ᭄ⱘ⿟ᑣ䛑ϡᖙњ㾷ᄫヺ䲚ⱘⶹ䆚DŽ ⫼ⱘᓔ䫔DŽ៥Ӏᇚ೼ 8.5 㡖ҟ㒡ᅗӀⱘᅲ⦄ᮍ⊩DŽ᮴䆎Ёⱘߑ᭄೼㒭ᅮⱘᴎ఼Ϟ њᇍ↣Ͼᄫヺ䛑䖯㸠ߑ᭄䇗ܡҹঞЁⱘ tolowerĀߑ᭄āϔ㠀䛑ᰃᅣˈ䖭ḋህ䙓 ᄫヺॳḋ䖨ಲDŽ៥Ӏ೼ࠡ䴶ᦤࠄ䖛ˈ༈᭛ӊЁⱘ getchar ੠ putcharĀߑ᭄” ߑ᭄ tolower ೼༈᭛ӊЁᅮНˈᅗᡞ໻ݭᄫ↡䕀ᤶЎᇣݭᔶᓣˈᑊᡞ݊ᅗ } return 0; putchar(tolower(c)); while ((c = getchar()) != EOF) int c { main() /* lower: convert input to lower case*/ #include #include Փ⫼䖭ѯߑ᭄ህ䎇໳њDŽ՟བˈ㗗㰥ϟ߫⿟ᑣ lowerˈᅗ⫼Ѣᇚ䕧ܹ䕀ᤶЎᇣݭᄫ↡ⱘᔶᓣ˖ 䇈Ꮖ㒣䎇໳њDŽ⡍߿ᰃˈབᵰ䗮䖛䞡ᅮ৥ᇚϔϾ⿟ᑣⱘ䕧ߎ䖲᥹ࠄ঺ϔϾ⿟ᑣⱘ䕧ܹˈҙҙ ᑣˈা䳔㽕Փ⫼ߑ᭄ getcharǃputchar ੠ printf ᅲ⦄䕧ܹˋ䕧ߎेৃˈᑊϨᇍ⿟ᑣᴹ 䆌໮⿟ᑣাҢϔϾ䕧ܹ⌕Ё䇏প᭄᥂ˈᑊϨা৥ϔϾ䕧ߎ⌕Ё䕧ߎ᭄᥂DŽᇍѢ䖭ḋⱘ⿟ ⱘ᭛ӊ˄՟བˈ೼ UNIX ㋏㒳Ёˈ᭛ӊϔ㠀ᬒ೼Ⳃᔩ/usr/include Ё ˅DŽ ᔧ᭛ӊৡ⫼ϔᇍᇪᣀো<੠>ᣀ䍋ᴹᯊˈ乘໘⧚఼ᇚ೼⬅݋ԧᅲ⦄ᅮНⱘ᳝݇ԡ㕂Ёᶹᡒᣛᅮ #include Փ⫼䕧ܹˋ䕧ߎᑧߑ᭄ⱘ↣Ͼ⑤⿟ᑣ᭛ӊᖙ乏೼ᓩ⫼䖭ѯߑ᭄Пࠡࣙ৿ϟ߫䇁হ 乎ᑣձ⃵ѻ⫳DŽৢܜ੠ printfˈ䕧ߎᇚᣝ✻ߑ᭄䇗⫼ⱘ 䕧ߎ䆒໛Ϟ䕧ߎ᭄᥂DŽ៥Ӏ೼⿟ᑣЁৃҹѸঝ䇗⫼ߑ᭄ putcharޚߑ᭄ printf г৥ᷛ 䕧ܹЁDŽޚ䕧ߎ䗮䖛ㅵ䘧䞡ᅮ৥ࠄ⿟ᑣ anotherprog ⱘᷛޚᇚᡞ⿟ᑣ prog ⱘ䕧ߎҢᷛ prog | anotherprog 䕧ߎ䆒໛䞡ᅮ৥ࠄ᭛ӊЁDŽབᵰ㋏㒳ᬃᣕㅵ䘧ˈ䙷МੑҸ㸠ޚᇚᡞ⿟ᑣ prog ⱘ䕧ߎҢᷛ prog > ೛ѻำߑ੠ བᵰ⿟ᑣ prog 䇗⫼њߑ᭄ putcharˈ䙷МੑҸ㸠 ϟˈгৃҹՓ⫼“>೛ѻำߑ੠āⱘḐᓣᇚ䕧ߎ䞡ᅮ৥ࠄᶤϾ᭛ӊЁDŽ՟བˈމৠḋˈ䗮ᐌᚙ ⼎DŽབᵰ≵᳝থ⫳䫭䇃ˈ߭ߑ᭄ putchar ᇚ䖨ৠ䕧ߎⱘᄫヺ˗བᵰথ⫳њ䫭䇃ˈ߭䖨ಲ EOFDŽ 䕧ߎЎሣᐩᰒޚϟˈᷛމ⫼Ѣ䕧ߎ᭄᥂DŽputchar(c)ᇚᄫヺ c 䗕㟇γᅹ೛ѻϞˈ೼咬䅸ᚙ int putchar(int) w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ៥Ӏ೼↣Ͼᄫ↉ⱘᎺ䖍੠ে䖍ࡴϞݦোˈ䖭ḋৃҹ⏙᱄ഄ㸼⼎ߎᄫ↉ⱘᆑᑺDŽ ᯢњ೼ᠧॄᄫヺІ"hello, world"˄12 Ͼᄫヺ˅ᯊḍ᥂ϡৠⱘ䕀ᤶ䇈ᯢѻ⫳ⱘϡৠ㒧ᵰDŽ ࠡ䴶ⱘゴ㡖ЁᏆ㒣ҟ㒡䖛໻䚼ߚⱘḐᓣ䕀ᤶˈԚ≵᳝ҟ㒡ϢᄫヺІⳌ݇ⱘ㊒ᑺDŽϟ㸼䇈 printf("%.*s", max, s); 䇁হ˖ ˄ᖙ乏Ў int ㉏ൟ˅ᴹ䅵ㅫDŽ՟བˈЎњҢᄫヺІ s Ёᠧॄ᳔໮ max ϾᄫヺˈৃҹՓ⫼ϟ߫ ೼䕀ᤶ䇈ᯢЁˈᆑᑺ៪㊒ᑺৃҹ⫼᯳ো*㸼⼎ˈ䖭ᯊˈᆑᑺ៪㊒ᑺⱘؐ䗮䖛䕀ᤶϟϔখ᭄ % ϡ䕀ᤶখ᭄˗ᠧॄϔϾⱒߚো% Ѣ݋ԧᅲ⦄˅އp void *㉏ൟ˗ᣛ䩜˄প ᇣ᭄⚍ϡᠧॄ g, G double ㉏ൟ˗བᵰᣛ᭄ᇣѢ•4 ៪໻ѢㄝѢ㊒ᑺˈ߭⫼%e ៪%E Ḑᓣ䕧ߎˈ৺߭⫼%f Ḑᓣ䕧ߎDŽሒ䚼ⱘ 0 ੠ e, E double ㉏ൟ˗[•]m.dddddd e ±xx ៪[•]m.dddddd E ±xxˈ݊Ё d ⱘϾ᭄⬅㊒ᑺᣛᅮ˄咬䅸ؐЎ 6˅ f double ㉏ൟ˗क䖯ࠊᇣ᭄[•]m.ddddddˈ݊Ё d ⱘϾ᭄⬅㊒ᑺᣛᅮ˄咬䅸ؐЎ 6˅ s char *㉏ൟ˗乎ᑣᠧॄᄫヺІЁⱘᄫヺˈⳈࠄ䘛ࠄ'\0'៪Ꮖᠧॄњ⬅㊒ᑺᣛᅮⱘᄫヺ᭄Ўℶ c int ㉏ൟ˗ऩϾᄫヺ u int ㉏ൟ˗᮴ヺোक䖯ࠊ᭄ x, X int ㉏ൟ˗᮴ヺোक݁䖯ࠊ᭄˄≵᳝ࠡᇐ 0x ៪ 0X˅ˈ 10̚15 ߚ߿⫼ abcdef ៪ ABCDEF 㸼⼎ o int ㉏ൟ˗᮴ヺোܿ䖯ࠊ᭄˄≵᳝ࠡᇐ 0˅ d, i int ㉏ൟ˗क䖯ࠊ᭄ ᄫヺ খ᭄㉏ൟ˖䕧ߎᔶᓣ 㸼 7•1 printf ߑ᭄෎ᴀⱘ䕀ᤶ䇈ᯢ ⱘDŽ 㸼 7•1 ߫ߎњ᠔᳝ⱘ䕀ᤶᄫヺˈབᵰ%ৢ䴶ⱘᄫヺϡᰃϔϾ䕀ᤶ䇈ᯢˈ߭䆹㸠Ўᰃ᳾ᅮН ㉏ൟᠧॄDŽ · ᄫ↡ h ៪ lˈᄫ↡ h 㸼ϡᇚᭈ᭄԰Ў short ㉏ൟᠧॄˈᄫ↡ l 㸼⼎ᇚᭈ᭄԰Ў long ᭈൟ᳔ᇥ䕧ߎⱘ᭄ᄫ᭄ⳂDŽ · ᭄ˈ⫼Ѣᣛᅮ㊒ᑺˈेᣛᅮᄫヺІЁ㽕ᠧॄⱘ᳔໻ᄫヺ᭄ǃ⍂⚍᭄ᇣ᭄⚍ৢⱘԡ᭄ǃ · ᇣ᭄⚍ˈ⫼Ѣᇚᄫ↉ᆑᑺ੠㊒ᑺߚᓔDŽ ҹֱ䆕᳔ᇣᄫ↉ᆑDŽܙ฿ ᵰ᳝ᖙ㽕ˈᄫ↉Ꮊ䖍˄བᵰՓ⫼Ꮊᇍ唤ⱘᮍᓣˈ߭Ўে䖍˅໮ԭⱘᄫヺԡ㕂⫼ぎḐ · ᭄ˈ⫼Ѣᣛᅮ᳔ᇣᄫ↉ᆑᑺDŽ䕀ᤶৢⱘখ᭄ᇚᠧॄϡᇣѢ᳔ᇣᄫ↉ᆑᑺⱘᄫ↉DŽབ · 䋳োˈ⫼Ѣᣛᅮ㹿䕀ᤶⱘখ᭄ᣝ✻Ꮊᇍ唤ⱘᔶᓣ䕧ߎDŽ ᄫヺ%੠䕀ᤶᄫヺЁ䯈ৃ㛑ձ⃵ࣙ৿ϟ߫㒘៤䚼ߚ˖ ⱘ䕀ᤶ੠ᠧॄˈ↣Ͼ䕀ᤶ䇈ᯢ䛑⬅ϔϾⱒߚোᄫヺ˄े%˅ᓔྟˈᑊҹϔϾ䕀ᤶᄫヺ㒧ᴳDŽ೼ ࡼഄ໡ࠊࠄ䕧ߎ⌕Ёˈ㗠䕀ᤶ䇈ᯢᑊϡⳈ᥹䕧ߎࠄ䕧ߎ⌕Ёˈ㗠ᰃ⫼Ѣ᥻ࠊ printf Ёখ᭄ ḐᓣᄫヺІࣙ৿ϸ⾡㉏ൟⱘᇍ䈵˖᱂䗮ᄫヺ੠䕀ᤶ䇈ᯢDŽ೼䕧ߎᯊˈ᱂䗮ᄫヺᇚॳḋϡ ໛ϞᠧॄߎᴹDŽᅗⱘ䖨ಲؐЎᠧॄⱘᄫヺ᭄DŽ 䕧ߎ䆒ޚߑ᭄ printf ೼䕧ߎḐᓣ format ⱘ᥻ࠊϟˈᇚ݊খ᭄䖯㸠䕀ᤶϢḐᓣ࣪ˈᑊ೼ᷛ int printf(char *format, arg1, arg2, ...); ϟ䴶া䆆䗄䆹ߑ᳔᭄݌ൟⱘ⫼⊩ˈ䰘ᔩ B Ё㒭ߎњ䆹ߑ᭄ᅠᭈⱘᦣ䗄DŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! va_endˈҹᅠ៤ϔѯᖙ㽕ⱘ⏙⧚Ꮉ԰DŽ ᅮ䖨ಲⱘᇍ䈵㉏ൟǃᣛ䩜⿏ࡼⱘℹ䭓DŽ᳔ৢˈᖙ乏೼ߑ᭄䖨ಲПࠡ䇗⫼އϔϾ㉏ൟৡᴹ ↣⃵䇗⫼ va_argˈ䆹ߑ᭄䛑ᇚ䖨ಲϔϾখ᭄ˈᑊᇚ ap ᣛ৥ϟϔϾখ᭄DŽva_arg Փ⫼ va_start ᇚ᳔ৢϔϾ᳝ৡখ᭄԰Ў䍋⚍DŽ ᭄ⱘᣛ䩜DŽ೼Փ⫼ ap Пࠡˈ䆹ᅣᖙ乏㹿䇗⫼ϔ⃵DŽখ᭄㸼ᖙ乏㟇ᇥࣙᣀϔϾ᳝ৡখ᭄ˈ ៥Ӏᇚ䆹ব䞣⿄Ў apˈᛣᗱᰃĀখ᭄ᣛ䩜āDŽᅣ va_start ᇚ ap ߱ྟ࣪Ўᣛ৥㄀ϔϾ᮴ৡখ va_list ㉏ൟ⫼ѢໄᯢϔϾব䞣ˈ䆹ব䞣ᇚձ⃵ᓩ⫼৘খ᭄DŽ೼ߑ᭄ minprintf Ёˈ ৠⱘᴎ఼㗠ϡৠˈԚᦤկⱘ᥹ষᰃϔ㟈ⱘDŽ ෎ѢϞ䴶䖭ѯ䅼䆎ˈ៥Ӏᅲ⦄ⱘㅔ࣪ printf ߑ᭄བϟ᠔⼎˖ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᴹⳟ㄀ϔϾ՟ᄤDŽ៥Ӏ䗮䖛ߑ᭄ scanf ᠻ㸠䕧ܹ䕀ᤶᴹᬍݭ㄀ 4 ゴЁⱘㅔऩ䅵ㅫ఼⿟ᑣˈ ᭄ᰃϔϾᣛ৥ double ㉏ൟ㗠䴲 float ㉏ൟⱘᣛ䩜DŽ ㉏ൟⱘᣛ䩜DŽ㉏Ԑഄˈ䕀ᤶ䇈ᯢ eǃf ੠ g ⱘࠡ䴶гৃҹࡴϞࠡ㓔 lˈᅗ㸼ᯢখ᭄㸼ⱘⳌᑨখ ᰃϔϾᣛ৥ short ㉏ൟ㗠䴲 int ㉏ൟⱘᣛ䩜ˈࠡ㓔 l 㸼ᯢখ᭄㸼ⱘⳌᑨখ᭄ᰃϔϾᣛ৥ long 䕀ᤶ䇈ᯢ dǃiǃoǃu ঞ x ⱘࠡ䴶ৃҹࡴϞᄫヺ h ៪ lDŽࠡ㓔 h 㸼ᯢখ᭄㸼ⱘⳌᑨখ᭄ % ᄫヺ%˗ϡ䖯㸠ӏԩ䌟ؐ᪡԰ e, f, g ⍂⚍᭄ˈᅗৃҹࣙᣀℷ䋳ো˄ৃ䗝˅ǃᇣ᭄⚍˄ৃ䗝˅ঞᣛ᭄䚼ߚ˄ৃ䗝˅˗float *㉏ൟ ヺІⱘ᳿ሒᇚ㹿⏏ࡴϔϾ㒧ᴳヺ'\0' s ᄫヺІ˄ϡࡴᓩো˅˗char * ㉏ൟˈᣛ৥ϔϾ䎇ҹᄬᬒ䆹ᄫヺІ˄䖬ࣙᣀሒ䚼ⱘᄫヺ'\0'˅ⱘᄫヺ᭄㒘DŽᄫ ぎⱑヺDŽབᵰ䳔㽕䇏ܹϟϔϾ䴲ぎⱑヺˈৃҹՓ⫼%1s c ᄫヺ˗char * ㉏ൟˈᇚ᥹ϟᴹⱘ໮Ͼ䕧ܹᄫヺ˄咬䅸Ў 1 Ͼᄫヺ˅ᄬᬒࠄᣛᅮԡ㕂DŽ䆹䕀ᤶ㾘㣗䗮ᐌϡ䏇䖛 x क݁䖯ࠊᭈ᭄˄ৃҹ 0x ៪ 0X ᓔ༈ˈгৃҹϡҹ 0x ៪ 0X ᓔ ༈ ˅˗ int *㉏ൟ u ᮴ヺোक䖯ࠊᭈ᭄˗unsigned int *㉏ൟ o ܿ䖯ࠊᭈ᭄˄ৃҹҹ 0 ᓔ༈ˈгৃҹϡҹ 0 ᓔ ༈ ˅˗ int *㉏ൟ i ᭈ᭄˗int *㉏ൟˈৃҹᰃܿ䖯ࠊ˄ҹ 0 ᓔ༈˅៪क݁䖯ࠊ˄ҹ 0x ៪ 0X ᓔ༈˅ d क䖯ࠊᭈ᭄˗int *㉏ൟ ᄫヺ 䕧᭄ܹ᥂˗খ᭄㉏ൟ 㸼 7•2 scanf ߑ᭄ⱘ෎ᴀ䕀ᤶ䇈ᯢ Н᠔㽕∖ⱘDŽ㸼 7•2 Ё߫ߎњ䖭ѯ䕀ᤶᄫヺDŽ 䕀ᤶᄫヺᣛᅮᇍ䕧ܹᄫ↉ⱘ㾷䞞DŽᇍᑨⱘখ᭄ᖙ乏ᰃᣛ䩜ˈ䖭гᰃ C 䇁㿔䗮䖛ؐ䇗⫼䇁 ῾৥ࠊ㸼ヺǃᤶ㸠ヺǃಲ䔺ヺǃ㒉৥ࠊ㸼ヺҹঞᤶ义ヺ˅DŽ 䖭㸼ᯢ scanf ߑ᭄ᇚ䍞䖛㸠䖍⬠䇏প䕧ܹˈ಴Ўᤶ㸠ヺгᰃぎⱑヺDŽ˄ぎⱑヺࣙᣀぎḐヺǃ ᅮНЎϔϾϡࣙᣀぎⱑヺⱘᄫヺІˈ݊䖍⬠ᅮНЎࠄϟϔϾぎⱑヺ៪䖒ࠄᣛᅮⱘᄫ↉ᆑᑺDŽ 䞣ЁDŽԚᰃˈབᵰ䕀ᤶ䇈ᯢЁ᳝䌟ؐ⽕ℶᄫヺ*ˈ߭䏇䖛䆹䕧ܹᄫ↉ˈϡ䖯㸠䌟ؐDŽ䕧ܹᄫ↉ 䕀ᤶ䇈ᯢ᥻ࠊϟϔϾ䕧ܹᄫ↉ⱘ䕀ᤶDŽϔ㠀ᴹ䇈ˈ䕀ᤶ㒧ᵰᄬᬒ೼Ⳍᑨⱘখ᭄ᣛ৥ⱘব 㒘៤DŽ ᄫ↉ᆑᑺ˅ǃϔϾৃ䗝ⱘ hǃl ៪ L ᄫヺ˄ᣛᅮⳂᷛᇍ䈵ⱘᆑᑺ˅ҹঞϔϾ䕀ᤶᄫヺ · 䕀ᤶ䇈ᯢˈձ⃵⬅ϔϾ%ǃϔϾৃ䗝ⱘ䌟ؐ⽕ℶᄫヺ*ǃϔϾৃ䗝ⱘ᭄ؐ˄ᣛᅮ᳔໻ · ᱂䗮ᄫヺ˄ϡࣙᣀ%˅ˈ⫼Ѣऍ䜡䕧ܹ⌕ЁϟϔϾ䴲ぎⱑヺᄫヺDŽ · ぎḐ៪ࠊ㸼ヺˈ೼໘⧚䖛⿟Ёᇚ㹿ᗑ⬹DŽ ḐᓣІ䗮ᐌ䛑ࣙ৿䕀ᤶ䇈ᯢˈ⫼Ѣ᥻ࠊ䕧ܹⱘ䕀ᤶDŽḐᓣІৃ㛑ࣙ৿ϟ߫䚼ߚ˖ arg2ǃĂ䖭ѯখ᭄ЁDŽ䖭ѯখ᭄ᖙ乏ᰃᣛ䩜DŽ ᅗᣝ✻Ḑᓣখ᭄ format Ё㾘ᅮⱘḐᓣᠿᦣᄫヺІ stringˈᑊᡞ㒧ᵰߚ߿ֱᄬࠄ arg1ǃ int sscanf(char *string, char *format, arg1, arg2, ...) ߫˖ 䕧ܹ˅Ё䇏পᄫヺᑣޚ঺໪䖬᳝ϔϾ䕧ܹߑ᭄ sscanfˈᅗ⫼ѢҢϔϾᄫヺІ˄㗠ϡᰃᷛ ϟϔ⃵䇗⫼ scanf ߑ᭄ᇚҢϞϔ⃵䕀ᤶⱘ᳔ৢϔϾᄫヺⱘϟϔϾᄫヺᓔྟ㒻㓁᧰㋶DŽ ᛣˈ䖨ಲ EOF Ϣ 0 ᰃϡৠⱘˈ0 㸼⼎ϟϔϾ䕧ܹᄫヺϢḐᓣІЁⱘ㄀ϔϾḐᓣ䇈ᯢϡऍ䜡DŽ 䖨ಲؐৃҹ⫼ᴹ⹂ᅮᏆऍ䜡ⱘ䕧ܹ乍ⱘϾ᭄DŽབᵰࠄ䖒᭛ӊⱘ㒧ሒˈ䆹ߑ᭄ᇚ䖨ಲ EOFDŽ⊼ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! scanf("%d", &n); ℷ⹂ⱘᔶᓣᑨ䆹Ў˖ scanf("%d", n); ៤ϟ߫ᔶᓣ˖ ⊼ᛣˈscanf ੠ sscanf ߑ᭄ⱘ᠔᳝খ᭄䛑ᖙ乏ᰃᣛ䩜DŽ᳔ᐌ㾕ⱘ䫭䇃ᰃᇚ䕧ܹ䇁হݭ 䇗⫼ᇚҢ scanf ≵᳝䇏পⱘ㄀ϔϾᄫヺ໘ᓔྟ䇏প᭄᥂DŽ scanf ߑ᭄ৃҹ੠݊ᅗ䕧ܹߑ᭄⏋ড়Փ⫼DŽ᮴䆎䇗⫼ાϾ䕧ܹߑ᭄ˈϟϔϾ䕧ܹߑ᭄ⱘ } printf("invalid: %s\n", line); /* invalid form */ else printf("valid: %s\n", line); /* mm/dd/yy form */ else if (sscanf(line, "%d/%d/%d", &month, &day, &year) == 3) printf("valid: %s\n", line); /* 25 Dec 1988 form */ if (sscanf(line, "%d %s %d", &day, monthname, &year) == 3) while (getline(line, sizeof(line)) > 0) { ᮹ᳳⱘḐᓣৃ㛑ᰃϞ䗄ӏϔ⾡ᔶᓣDŽ៥Ӏৃҹ䖭ḋ㓪ݭ⿟ᑣ˖ ᅮ៥Ӏ䳔㽕䇏পϔѯࣙ৿᮹ᳳ᭄᥂ⱘ䕧ܹ㸠ˈ؛ˈsscanf ᇚড়䗖ⱘḐᓣߚ⾏ߎᴹ䇏ܹDŽ՟བ Ḑǃࠊ㸼ヺǃᤶ㸠ヺㄝㄝ˅DŽབᵰ㽕䇏পḐᓣϡ೎ᅮⱘ䕧ܹˈ᳔ད↣⃵䇏ܹϔ㸠ˈ✊ৢݡ⫼ scanf ߑ᭄ᗑ⬹ḐᓣІЁⱘぎḐ੠ࠊ㸼ヺDŽℸ໪ˈ೼䇏প䕧ܹؐᯊˈᅗᇚ䏇䖛ぎⱑヺ˄ぎ scanf("%d/%d/%d", &month, &day, &year); int day, month, year; ៥ӀৃҹՓ⫼ϟ߫ scanf 䇁হ䇏ܹᔶབ mm/dd/yy ⱘ᮹ᳳ᭄᥂˖ ᄫヺᄫ䴶ؐгৃҹߎ⦄೼ scanf ⱘḐᓣІЁˈᅗӀᖙ乏Ϣ䕧ܹЁⳌৠⱘᄫヺऍ䜡DŽ಴ℸˈ ಴Ў᭄㒘ৡᴀ䑿ህᰃᣛ䩜ˈ᠔ҹˈmonthname ⱘࠡ䴶≵᳝পഄഔ䖤ㅫヺ&DŽ scanf("%d %s %d", &day, monthname, &year); char monthname[20]; int day, year; Ⳍᑨⱘ scanf 䇁হৃҹ䖭ḋ㓪ݭ˖ 25 Dec 1988 䆒៥Ӏ㽕䇏পࣙ৿ϟ߫᮹ᳳḐᓣⱘ䕧ܹ㸠˖؛ { ; return 0 printf("\t%.2f\n", sum += v); while (scanf("%lf", &v) == 1) sum = 0; double sum, v; { main() /* rudimentary calculator */ #include བϟ᠔⼎˖ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ৃ㛑ᇐ㟈䫭䇃ˈ↨བ䆩೒䇏পϔϾ᮴䇏পᴗ䰤ⱘ᭛ӊDŽབᵰথ⫳䫭䇃ˈfopen ᇚ䖨ಲ NULLDŽ ᭛ӊˈ߭䆹᭛ӊॳᴹⱘݙᆍᇚֱ⬭ϡবDŽ䇏ϔϾϡᄬ೼ⱘ᭛ӊӮᇐ㟈䫭䇃ˈ݊ᅗϔѯ᪡԰г ᓣᠧᓔϔϾᏆᄬ೼ⱘ᭛ӊᯊˈ䆹᭛ӊॳᴹⱘݙᆍᇚ㹿㽚ⲪDŽԚᰃˈབᵰҹ䗑ࡴᮍᓣᠧᓔϔϾ བᵰᠧᓔϔϾϡᄬ೼ⱘ᭛ӊ⫼Ѣݭ៪䗑ࡴˈ䆹᭛ӊᇚ㹿߯ᓎ˄བᵰৃ㛑ⱘ䆱˅DŽᔧҹݭᮍ ㋏㒳䖬ऎߚ᭛ᴀ᭛ӊ੠Ѡ䖯ࠊ᭛ӊˈᇍৢ㗙ⱘ䆓䯂䳔㽕೼῵ᓣᄫヺІЁ๲ࡴᄫヺ“bāDŽ 䆌ⱘ῵ᓣࣙᣀ˖䇏˄“rā˅ǃ ݭ ˄“ w ā˅ ঞ 䗑 ࡴ ˄“ a ā˅DŽᶤѯܕІˈ⫼Ѣᣛᅮ᭛ӊⱘՓ⫼ᮍᓣDŽ fopen ⱘ㄀ϔϾখ᭄ᰃϔϾᄫヺІˈᅗࣙ৿᭛ӊৡDŽ㄀ѠϾখ᭄ᰃ䆓䯂῵ᓣˈгᰃϔϾᄫヺ fp = fopen(name, mode); ೼⿟ᑣЁˈৃҹ䖭ḋ䇗⫼ fopen ߑ᭄˖ ˄UNIX ㋏㒳Ё fopen ⱘᅲ⦄㒚㡖ᇚ೼ 8.5 㡖Ё䅼䆎˅DŽ int ϔḋᰃϔϾ㉏ൟৡˈ㗠ϡᰃ㒧ᵘᷛ䆄DŽᅗᰃ䗮䖛 typedef ᅮНⱘ ڣ ᣛ䩜DŽ⊼ᛣˈFILE ೼ᴀ՟Ёˈfp ᰃϔϾᣛ৥㒧ᵘ FILE ⱘᣛ䩜ˈᑊϨˈfopen ߑ᭄䖨ಲϔϾᣛ৥㒧ᵘ FILE ⱘ FILE *fopen(char *name, char *mode); FILE *fp; ᑣЁা䳔ᣝ✻ϟ߫ᮍᓣໄᯢϔϾ᭛ӊᣛ䩜ेৃ˖ ᠋ϡᖙ݇ᖗ䖭ѯ㒚㡖ˈ಴ЎЁᏆ㒣ᅮНњϔϾࣙ৿䖭ѯֵᙃⱘ㒧ᵘ FILEDŽ೼⿟ ऎЁᔧࠡᄫヺⱘԡ㕂ǃ᭛ӊⱘ䇏៪ݭ⢊ᗕǃᰃ৺ߎ䫭៪ᰃ৺Ꮖ㒣ࠄ䖒᭛ӊ㒧ሒㄝㄝDŽ⫼ކ㓧 ऎⱘԡ㕂ǃކ䆹ᣛ䩜⿄Ўำߑᄗწˈᅗᣛ৥ϔϾࣙ৿᭛ӊֵᙃⱘ㒧ᵘˈ䖭ѯֵᙃࣙᣀ˖㓧 㒚㡖˅ˈᑊ䖨ಲϔϾ䱣ৢৃҹ⫼Ѣ᭛ӊ䇏ݭ᪡԰ⱘᣛ䩜DŽ ㉏ԐѢ x.c ៪ y.c 䖭ḋⱘ໪䚼ৡϢ᪡԰㋏㒳䖯㸠ᶤѯᖙ㽕ⱘ䖲᥹੠䗮ֵ˄៥Ӏϡᖙ݇ᖗ䖭ѯ ᮍ⊩݊ᅲᕜㅔऩDŽ೼䇏ݭϔϾ᭛ӊПࠡˈᖙ乏䗮䖛ᑧߑ᭄ fopen ᠧᓔ䆹᭛ӊDŽfopen ⫼ ໪䚼ৡৠ䇏প᭄᥂ⱘ䇁হ݇㘨䍋ᴹDŽ 䯂乬೼Ѣˈབԩ䆒䅵ੑৡ᭛ӊⱘ䇏প䖛⿟ਸ਼˛ᤶহ䆱䇈ˈབԩᇚ⫼᠋䳔㽕Փ⫼ⱘ᭛ӊⱘ 䕧ߎϞᠧॄ᭛ӊ x.c ੠ y.c ⱘݙᆍDŽޚᇚ೼ᷛ cat x.c y.c ՟བˈϟ߫ੑҸ㸠˖ Ϟᠧॄ᭛ӊˈᇍѢ䙷ѯ᮴⊩䗮䖛ৡᄫ䆓䯂᭛ӊⱘ⿟ᑣᴹ䇈DŽᅗ䖬ৃҹ⫼԰䗮⫼ⱘ䕧ܹᬊ䲚఼DŽ 䕧ߎϞDŽcat ৃ⫼ᴹ೼ሣᐩޚcat ৃҹ⫼ᴹ䇈ᯢ䆹䯂乬ˈᅗᡞϔᡍੑৡ᭛ӊІ㘨ৢ䕧ߎࠄᷛ ᥹ϟᴹˈ៥Ӏ㓪ݭϔϾ䆓䯂᭛ӊⱘ⿟ᑣˈϨᅗ᠔䆓䯂ⱘ᭛ӊ䖬≵᳝䖲᥹ࠄ䆹⿟ᑣDŽ⿟ᑣ 䕧ߎᰃ᪡԰㋏㒳㞾ࡼᦤկ㒭⿟ᑣ䆓䯂ⱘDŽޚ䕧ܹ੠ᷛ ޚ䕧ߎ䕧ߎ᭄᥂DŽᷛޚ䕧ܹ䇏প᭄᥂ˈᑊ৥ᷛޚࠄⳂࠡЎℶˈ៥Ӏ䅼䆎ⱘ՟ᄤ䛑ᰃҢᷛ 7.5. ᭛ӊ䆓䯂 ⦄䕧ܹҹঞ᭄ⱘ䕀ᤶDŽ 㒗д 7•5 ᬍݭ㄀ 4 ゴЁⱘৢ㓔䅵ㅫ఼⿟ᑣˈ⫼ scanf ߑ᭄੠˄៪˅sscanf ߑ᭄ᅲ 㒗д 7•4 ㉏ԐѢϞϔ㡖Ёⱘߑ᭄ minprintfˈ㓪ݭ scanf ߑ᭄ⱘϔϾㅔ࣪⠜ᴀDŽ 㓪䆥఼೼㓪䆥ᯊϔ㠀Ẕ⌟ϡࠄ䖭㉏䫭䇃DŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! } else { return 1; printf("cat: can't open %s\n, *argv); if ((fp = fopen(*++argv, "r")) == NULL) { while(••argc > 0) else filecopy(stdin, stdout); if (argc == 1) /* no args; copy standard input */ void filecopy(FILE *, FILE *) FILE *fp; { main(int argc, char *argv[]) /* cat: concatenate files, version 1 */ #include 䕧ܹDŽޚᑣ䗤Ͼ໘⧚DŽབᵰ≵᳝খ᭄ˈ߭໘⧚ᷛ ⿟ᑣⱘ䆒䅵ᗱ䏃੠݊ᅗ䆌໮⿟ᑣ㉏ԐDŽབᵰ᳝ੑҸ㸠খ᭄ˈখ᭄ᇚ㹿㾷䞞Ў᭛ӊৡˈᑊᣝ乎 ᥠᦵ䖭ѯ乘໛ⶹ䆚Пৢˈ៥Ӏ⦄೼ህৃҹ㓪ݭߎᇚ໮Ͼ᭛ӊ䖲᥹䍋ᴹⱘ cat ⿟ᑣњDŽ䆹 int fprintf(FILE *fp, char *format, ...) int fscanf(FILE *fp, char *format, ...) ᭄ᰃḐᓣІDŽབϟ᠔⼎˖ printf ߑ᭄ⱘऎ߿ҙҙ೼ѢᅗӀⱘ㄀ϔϾখ᭄ᰃϔϾᣛ৥᠔㽕䇏ݭⱘ᭛ӊⱘᣛ䩜ˈ㄀ѠϾখ ᇍѢ᭛ӊⱘḐᓣ࣪䕧ܹ៪䕧ߎˈৃҹՓ⫼ߑ᭄ fscanf ੠ fprintfDŽᅗӀϢ scanf ੠ #define putchar(c) putc((c), stdout) #define getchar() getc(stdin) getchar ੠ putchar ߑ᭄ৃҹ䗮䖛 getcǃputcǃstdin ঞ stdout ᅮНབϟ˖ 㹿䞡ᅮ৥ࠄ᭛ӊ៪ㅵ䘧DŽ stdout ੠ stderr ᣛ৥ᰒ⼎఼DŽ៥ӀҢ 7.1 㡖ⱘ䅼䆎Ёৃҹⶹ䘧ˈstdin ੠ stdout ৃҹ stdout ੠ stderrˈᅗӀ೼ЁໄᯢDŽ೼໻໮᭄⦃๗Ёˈstdin ᣛ৥䬂Ⲭˈ㗠 䫭䇃ˈⳌᑨⱘ᭛ӊᣛ䩜ߚ߿Ў stdinǃޚ䕧ߎ੠ᷛޚ䕧ܹǃᷛޚ㒭䆹⿟ᑣDŽ䖭 3 Ͼ᭛ӊߚ߿ᰃᷛ ਃࡼϔϾ C 䇁㿔⿟ᑣᯊˈ᪡԰㋏㒳⦃๗䋳䋷ᠧᓔ 3 Ͼ᭛ӊˈᑊᇚ䖭 3 Ͼ᭛ӊⱘᣛ䩜ᦤկ ㉏ԐѢ getchar ੠ putcharˈgetc ੠ putc ᰃᅣ㗠ϡᰃߑ᭄DŽ 䆹ߑ᭄ᇚᄫヺ c ݭܹࠄ fp ᣛ৥ⱘ᭛ӊЁˈᑊ䖨ಲݭܹⱘᄫヺDŽབᵰথ⫳䫭䇃ˈ߭䖨ಲ EOFDŽ int putc(int c, FILE *fp) putc ᰃϔϾ䕧ߎߑ᭄ˈབϟ᠔⼎˖ ಲ EOFˈ getc ߑ᭄䖨ಲ fp ᣛ৥ⱘ䕧ܹ⌕ЁⱘϟϔϾᄫヺDŽབᵰࠄ䖒᭛ӊሒ៪ߎ⦄䫭䇃ˈ䆹ߑ᭄ᇚ䖨 int getc(FILE *fp) ᅮᇍાϾ᭛ӊᠻ㸠᪡԰˖ getc ੠ putc ߑ᳔᭄ЎㅔऩDŽgetc Ң᭛ӊЁ䖨ಲϟϔϾᄫヺˈᅗ䳔㽕ⶹ䘧᭛ӊᣛ䩜ˈҹ⹂ ᭛ӊ㹿ᠧᓔৢˈህ䳔㽕㗗㰥䞛⫼ા⾡ᮍ⊩ᇍ᭛ӊ䖯㸠䇏ݭDŽ᳝໮⾡ᮍ⊩ৃկ㗗㰥ˈ݊Ёˈ ҹ᳈䖯ϔℹഄᅮԡ䫭䇃ⱘ㉏ൟˈ݋ԧᮍ⊩䇋খ㾕䰘ᔩ B.1 㡖Ё݇Ѣ䫭䇃໘⧚ߑ᭄ⱘ䅼䆎DŽ˅ৃ˄ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! filecopy(stdin, stdout); if (argc == 1 ) /* no args; copy standard input */ char *prog = argv[0]; /* program name for errors */ void filecopy(FILE *, FILE *); FILE *fp; { main(int argc, char *argv[]) /* cat: concatenate files, version 2 */ #include 䫭䇃᭛ӊϞDŽޚϟ䴶៥Ӏᬍݭ cat ⿟ᑣˈᇚ݊ߎ䫭ֵᙃݭࠄᷛ 䕧ߎ䖯㸠њ䞡ᅮ৥ˈݭࠄ stderr Ёⱘ䕧ߎ䗮ᐌгӮᰒ⼎೼ሣᐩϞDŽޚे stderrDŽेՓᇍᷛ ঺ϔϾ䕧ߎ⌕ҹϢ stdin ੠ stdout Ⳍৠⱘᮍᓣߚ⌒㒭⿟ᑣˈˈމЎњ᳈དഄ໘⧚䖭⾡ᚙ ফњDŽ ⾡໘⧚ᮍ⊩ᇮৃҹ᥹ফˈԚབᵰ䕧ߎࠄϔϾ᭛ӊ៪䗮䖛ㅵ䘧䕧ߎࠄ঺ϔϾ⿟ᑣᯊˈህ᮴⊩᥹ ӊ᮴⊩䆓䯂ˈⳌᑨⱘ䆞ᮁֵᙃ㽕೼䆹䖲᥹ⱘ䕧ߎⱘ᳿ሒᠡ㛑ᠧॄߎᴹDŽᔧ䕧ߎࠄሣᐩᯊˈ䖭 cat ⿟ᑣⱘ䫭䇃໘⧚ࡳ㛑ᑊϡᅠ୘DŽ䯂乬೼Ѣˈབᵰ಴Ўᶤ⾡ॳ಴㗠䗴៤݊ЁⱘϔϾ᭛ 7.6. 䫭䇃໘⧚——stderr ੠ exit freopen 䞡ᮄᣛᅮᅗӀDŽ) fclose ߑ᭄DŽ˄བᵰϡ䳔㽕Փ⫼ stdin Ϣ stdoutˈৃҹᡞᅗӀ݇䯁ᥝDŽгৃҹ䗮䖛ᑧߑ᭄ ߑ᭄ℷ೼ᬊ䲚ⱘ䕧ߎݭࠄ᭛ӊЁDŽᔧ⿟ᑣℷᐌ㒜ℶᯊˈ⿟ᑣӮ㞾ࡼЎ↣Ͼᠧᓔⱘ᭛ӊ䇗⫼ ऎЁ⬅ putcކⱘ䙷ḋDŽᇍ䕧ߎ᭛ӊᠻ㸠 fclose 䖬᳝঺໪ϔϾॳ಴˖ᅗᇚᡞ㓧خ⿟ᑣЁ᠔ ៥Ӏ೼ catڣӊ᭄ˈ᠔ҹˈᔧ᭛ӊᣛ䩜ϡݡ䳔㽕ᯊህᑨ䆹䞞ᬒˈ䖭ᰃϔϾདⱘ㓪⿟дᛃˈህ 䞞ᬒ᭛ӊᣛ䩜ҹկ݊ᅗ᭛ӊՓ⫼DŽ಴Ў໻໮᭄᪡԰㋏㒳䛑䰤ࠊњϔϾ⿟ᑣৃҹৠᯊᠧᓔⱘ᭛ ᠻ㸠੠ fopen Ⳍডⱘ᪡԰ˈᅗᮁᓔ⬅ fopen ߑ᭄ᓎゟⱘ᭛ӊᣛ䩜੠໪䚼ৡП䯈ⱘ䖲᥹ˈᑊ int fclose(FILE *fp) ߑ᭄ ᇍᅗӀ䌟ؐDŽ ᭛ӊᣛ䩜 stdin Ϣ stdout 䛑ᰃ FILE *㉏ൟⱘᇍ䈵DŽԚᅗӀᰃᐌ䞣ˈ㗠䴲ব䞣DŽ಴ℸϡ㛑 } putc(c, ofp); while ((c = getc(ifp)) != EOF) int c; { void filecopy(FILE *ifp, FILE *ofp) /* filecopy: copy file ifp to file ofp */ } return 0; } fclose(fp); filecopy(fp, stdout); w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! fgets ߑ᭄Ң fp ᣛ৥ⱘ᭛ӊЁ䇏পϟϔϾ䕧ܹ㸠˄ࣙᣀᤶ㸠ヺ˅ˈᑊᇚᅗᄬᬒ೼ᄫヺ᭄㒘 char *fgets(char *line, int maxline, FILE *fp) ᑧᦤկњϔϾ䕧ܹߑ᭄ fgetsˈᅗ੠ࠡ䴶޴ゴЁ⫼ࠄⱘߑ᭄ getline ㉏ԐDŽޚᷛ 7.7. 㸠䕧ܹ੠㸠䕧ߎ ᇍѢӏԩ䞡㽕ⱘ⿟ᑣᴹ䇈ˈ䛑ᑨ䆹䅽⿟ᑣ䖨ಲ᳝ᛣНϨ᳝⫼ⱘؐDŽ ೼Ϟ䴶ⱘᇣ⿟ᑣЁˈ៥ӀⱘⳂⱘᰃЎњ䇈ᯢ䯂乬ˈ಴ℸᑊϡ໾݇ᖗ⿟ᑣⱘ䗔ߎ⢊ᗕˈԚ int feof(FILE *fp) 0 ؐDŽ ߑ᭄ feof(FILE *)Ϣ ferror ㉏ԐDŽབᵰᣛᅮⱘ᭛ӊࠄ䖒᭛ӊ㒧ሒˈᅗᇚ䖨ಲϔϾ䴲 ᶹ䖭⾡㉏ൟⱘ䫭䇃DŽ ሑㅵ䕧ߎ䫭䇃ᕜᇥߎ⦄ˈԚ䖬ᰃᄬ೼ⱘ˄՟བˈᔧ⺕Ⲭ⒵ᯊ˅ˈ಴ℸˈ៤❳ⱘѻક⿟ᑣᑨ䆹Ẕ int ferror(FILE *fp) བᵰ⌕ fp Ёߎ⦄䫭䇃ˈ߭ߑ᭄ ferror 䖨ಲϔϾ䴲 0 ؐDŽ ᡒ䖭ѯ䇗⫼DŽ ᳝ϔϾӬ⚍ˈᅗৃҹҢ݊ᅗߑ᭄Ё䇗⫼ˈᑊϨৃҹ⫼㉏ԐѢ㄀ 5 ゴЁᦣ䗄ⱘ῵ᓣᶹᡒ⿟ᑣᶹ ೼Џ⿟ᑣ main Ёˈ䇁হ return expr ㄝӋѢ exit(expr)DŽԚᰃˈՓ⫼ߑ᭄ exit ᳝䕧ߎݭࠄⳌᑨⱘ᭛ӊЁDŽ ऎЁⱘ᠔ކDŽexit Ў↣ϾᏆᠧᓔⱘ䕧ߎ᭛ӊ䇗⫼ fclose ߑ᭄ˈҹᇚ㓧މ㸼⼎ߎ⦄њᓖᐌᚙ ⿟ᑣᴹ⌟䆩䆹⿟ᑣⱘᠻ㸠ᰃ৺៤ࡳDŽᣝ✻ᛃ՟ˈ䖨ಲؐ 0 㸼⼎ϔߛℷᐌˈ㗠䴲 0 䖨ಲؐ䗮ᐌ ԩ䇗⫼䆹⿟ᑣⱘ䖯⿟䛑ৃҹ㦋প exit ⱘখ᭄ؐˈ಴ℸˈৃ䗮䖛঺ϔϾᇚ䆹⿟ᑣ԰Ўᄤ䖯⿟ⱘ ᑧߑ᭄ exitˈᔧ䆹ߑ᭄㹿䇗⫼ᯊˈᅗᇚ㒜ℶ䇗⫼⿟ᑣⱘᠻ㸠DŽӏޚ݊⃵ˈ⿟ᑣՓ⫼њᷛ ⑤DŽ ᙃЁࣙ৿ argv[0]Ёⱘ⿟ᑣৡˈ಴ℸˈᔧ䆹⿟ᑣ੠݊ᅗ⿟ᑣϔ䍋䖤㸠ᯊˈৃҹ䆚߿䫭䇃ⱘᴹ stderr Ϟˈ಴ℸ䆞ᮁֵᙃᇚӮᰒ⼎೼ሣᐩϞˈ㗠ϡᰃҙҙ䕧ߎࠄㅵ䘧៪䕧ߎ᭛ӊЁDŽ䆞ᮁֵ ᇚ fprintf ߑ᭄ѻ⫳ⱘ䆞ᮁֵᙃ䕧ߎࠄˈܜ䆹⿟ᑣ䗮䖛ϸ⾡ᮍᓣথߎߎ䫭ֵᙃDŽ佪 } exit(0); } exit(2); fprintf(stderr, "%s: error writing stdout\n", prog); if (ferror(stdout)) { } fclose(fp); filecopy(fp, stdout); } else { exit(1); prog, *argv); fprintf(stderr, "%s: can't open %s\n", if ((fp = fopen(*++argv, "r")) == NULL) { while (••argc > 0) else w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 㒗д 7•6 㓪ݭϔϾ⿟ᑣˈ↨䕗ϸϾ᭛ӊᑊᠧॄᅗӀ㄀ϔϾϡⳌৠⱘ㸠DŽ } return strlen(line); else return 0; if (fgets(line, max, stdin) == NULL) { int getline(char *line, int max) /* getline: read a line, return length */ Փ⫼ fgets ߑ᭄ᕜᆍᯧᅲ⦄ getline ߑ᭄˖ 䖨ಲϔϾ䴲䋳ؐDŽމ݊ᅗᚙ 㾘ᅮˈferror ೼থ⫳䫭䇃ᯊ䖨ಲ䴲 0 ؐˈ㗠 fputs ೼থ⫳䫭䇃ᯊ䖨ಲ EOFˈޚANSI ᷛ } return ferror(iop) ? EOF : 0; putc(c, iop); while (c = *s++) int c; { int fputs(char *s, FILE *iop) /* fputs: put string s on file iop */ } return (c == EOF && cs == s) ? NULL : s; *cs = '\0'; break; if ((*cs++ = c) == '\n') while (••n > 0 && (c = getc(iop)) != EOF) cs = s; register char *cs; register int c; { char *fgets(char *s, int n, FILE *iop) /* fgets: get at most n chars from iop */ ᳝ҔМ⡍߿ⱘഄᮍDŽҷⷕབϟ᠔⼎˖ ᑧЁ fgets ੠ fputs ߑ᭄ⱘҷⷕˈҢЁৃҹⳟߎˈ䖭ϸϾߑ᭄ᑊ≵ޚϟ䴶ⱘҷⷕᰃᷛ 㗠 puts ߑ᭄೼ݭܹᄫヺІᯊᇚ೼㒧ሒ⏏ࡴϔϾᤶ㸠ヺDŽ 䖯㸠᪡԰DŽ᳝ϔ⚍៥Ӏ䳔㽕⊼ᛣˈgets ߑ᭄೼䇏পᄫヺІᯊᇚߴ䰸㒧ሒⱘᤶ㸠ヺ˄'\n'˅ˈ ᑧߑ᭄gets੠putsⱘࡳ㛑Ϣfgets੠fputsߑ᭄㉏ԐˈԚᅗӀᰃᇍstdin੠stdout བᵰথ⫳䫭䇃ˈ䆹ߑ᭄ᇚ䖨ಲ EOFˈ৺߭䖨ಲϔϾ䴲䋳ؐDŽ int fputs(char *line, FILE *fp) 䕧ߎߑ᭄ fputs ᇚϔϾᄫヺІ˄ϡ䳔㽕ࣙ৿ᤶ㸠ヺ˅ݭܹࠄϔϾ᭛ӊЁ˖ getline ߑ᭄䖨ಲ㸠ⱘ䭓ᑺˈ䖭Ͼؐ᳈᳝⫼ˈᔧᅗЎ 0 ᯊᛣੇⴔᏆ㒣ࠄ䖒њ᭛ӊⱘ㒧ሒ˅DŽ ϟˈfgets 䖨ಲ lineˈԚབᵰ䘛ࠄњ᭛ӊ㒧ሒ៪থ⫳њ䫭䇃ˈ߭䖨ಲ NULL˄៥Ӏ㓪ݭⱘމ line Ёˈᅗ᳔໮ৃ䇏প maxline•1 ϾᄫヺDŽ䇏পⱘ㸠ᇚҹ'\0'㒧ሒֱᄬࠄ᭄㒘ЁDŽ䗮ᐌᚙ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! tolower(c) 䖨ಲ c ⱘᇣݭᔶᓣ toupper(c) 䖨ಲ c ⱘ໻ݭᔶᓣ ߭䖨ಲϔϾ䴲 0 ؐ isspace(c) 㢹 c ᰃぎḐǃ῾৥ࠊ㸼ヺǃᤶ㸠ヺǃಲ䔺ヺˈᤶ义ヺ៪㒉৥ࠊ㸼ヺˈ isalnum(c) 㢹 isalpha(c)៪ isdigit(c)ˈ߭䖨ಲϔϾ䴲 0 ؐˈ৺߭䖨ಲ 0 isdigit(c) 㢹 c ᰃ᭄ᄫˈ߭䖨ಲϔϾ䴲 0 ؐˈ৺߭䖨ಲ 0 islower(c) 㢹 c ᰃᇣݭᄫ↡ˈ߭䖨ಲϔϾ䴲 0 ؐˈ৺߭䖨ಲ 0 isupper(c) 㢹 c ᰃ໻ݭᄫ↡ˈ߭䖨ಲϔϾ䴲 0 ؐˈ৺߭䖨ಲ 0 isalpha(c) 㢹 c ᰃᄫ↡ˈ߭䖨ಲϔϾ䴲 0 ؐˈ৺߭䖨ಲ 0 ϔϾৃ㸼⼎Ў unsigned char ㉏ൟ៪ EOF ⱘ int ᇍ䈵DŽ䆹ߑ᭄ⱘ䖨ಲؐ㉏ൟЎ intDŽ ༈᭛ӊЁᅮНњϔѯ⫼Ѣᄫヺ⌟䆩੠䕀ᤶⱘߑ᭄DŽ೼ϟ䴶৘Ͼߑ᭄Ёˈc ᰃ 7.8.2. ᄫヺ㉏߿⌟䆩੠䕀ᤶߑ᭄ ԡ㕂ⱘᣛ䩜ˈ৺߭䖨ಲ NULL strrchr(s, c) ೼ s ᣛ৥ⱘᄫヺІЁᶹᡒ cˈ㢹ᡒࠄˈ߭䖨ಲᣛ৥ᅗ᳔ৢϔ⃵ߎ⦄ⱘ 㕂ⱘᣛ䩜ˈ৺߭䖨ಲ NULL strchr(s, c) ೼ s ᣛ৥ⱘᄫヺІЁᶹᡒ cˈ㢹ᡒࠄˈ߭䖨ಲᣛ৥ᅗ㄀ϔ⃵ߎ⦄ⱘԡ strlen(s) 䖨ಲ s ᣛ৥ⱘᄫヺІⱘ䭓ᑺ strncpy(s, t, n) ᇚ t ᣛ৥ⱘᄫヺІЁࠡ n Ͼᄫヺ໡ࠊࠄ s ᣛ৥ⱘԡ㕂 strcpy(s, t) ᇚ t ᣛ৥ⱘᄫヺІ໡ࠊࠄ s ᣛ৥ⱘԡ㕂 strncmp(s, t, n) ৠ strcmp ⳌৠˈԚা೼ࠡ n ϾᄫヺЁ↨䕗 ߚ߿䖨ಲ䋳ᭈ᭄ǃ0 ៪ℷᭈ᭄ˈމᣛ৥ⱘᄫヺІⱘϡৠᚙ strcmp(s, t) ḍ᥂ s ᣛ৥ⱘᄫヺІᇣѢ˄st˅t strncat(s, t, n) ᇚ t ᣛ৥ⱘᄫヺІЁࠡ n Ͼᄫヺ䖲᥹ࠄ s ᣛ৥ⱘᄫヺІⱘ᳿ሒ strcat(s, t) ᇚ t ᣛ৥ⱘᄫヺІ䖲᥹ࠄ s ᣛ৥ⱘᄫヺІⱘ᳿ሒ ЁᅮНDŽ೼ϟ䴶ⱘ৘Ͼߑ᭄Ёˈs Ϣ t Ў char *㉏ൟˈc Ϣ n Ў int ㉏ൟDŽ ࠡ䴶Ꮖ㒣ᦤࠄ䖛ᄫヺІߑ᭄ strlenǃstrcpyǃstrcat ੠ strcmpˈᅗӀ䛑೼༈᭛ӊ 7.8.1. ᄫヺІ᪡԰ߑ᭄ ᳈䆺㒚ⱘֵᙃҹঞ݊ᅗ䆌໮≵᳝ҟ㒡ⱘߑ᭄䇋খ㾕䰘ᔩ BDŽ ϔϾㅔ㽕ⱘὖ䗄DŽخᑧᦤկњᕜ໮ࡳ㛑৘ᓖⱘߑ᭄DŽᴀ㡖ᇚᇍ݊Ё⡍߿᳝⫼ⱘߑ᭄ޚᷛ 7.8. ݊ᅗߑ᭄ ᠧॄ↣Ͼ᭛ӊⳌᑨⱘᷛ乬੠义᭄DŽ 㒗д 7•8 㓪ݭϔϾ⿟ᑣˈҹᠧॄϔϾ᭛ӊ䲚ড়ˈ↣Ͼ᭛ӊҢᮄⱘϔ义ᓔྟᠧॄˈᑊϨ ᑨ䆹ᇚⳌᑨⱘ᭛ӊৡᠧॄߎᴹ˛ 䕧ܹЁ䇏প䕧ܹDŽᔧথ⦄ϔϾऍ䜡㸠ᯊˈᰃ৺ޚӊৡখ᭄ᯊ˅ˈབᵰ≵᳝᭛ӊৡখ᭄ˈ߭Ңᷛ 㒗д 7•7 ׂᬍ㄀ 5 ゴⱘ῵ᓣᶹᡒ⿟ᑣˈՓᅗҢϔϾੑৡ᭛ӊⱘ䲚ড়Ё䇏প䕧ܹ˄᳝᭛ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᅗ䗮䖛ϔϾᕾ⦃䞞ᬒ߫㸼Ёⱘ乍Ⳃ˖ ぎ䯈ৠḋᰃ䫭䇃ⱘDŽϟ䴶᠔⼎ⱘҷⷕᰃϔϾᕜ݌ൟⱘ䫭䇃ҷⷕ↉ˈټՓ⫼Ꮖ㒣䞞ᬒⱘᄬ ぎ䯈ˈᇚᰃϔϾᕜϹ䞡ⱘ䫭䇃DŽټ៪ calloc ߑ᭄ᕫࠄⱘᣛ䩜᠔ᣛ৥ⱘᄬ ぎ䯈ⱘ䞞ᬒ乎ᑣ≵᳝ҔМ䰤ࠊˈԚᰃˈབᵰ䞞ᬒϔϾϡᰃ䗮䖛䇗⫼ mallocټ᭄ᕫࠄⱘᣛ䩜DŽᄬ ぎ䯈ˈ݊Ёˈp ᰃℸࠡ䗮䖛䇗⫼ malloc ៪ calloc ߑټfree(p)ߑ᭄䞞ᬒ p ᣛ৥ⱘᄬ ip = (int *) calloc(n, sizeof(int)); int *ip; ՟ᄤ䖯㸠њ㉏ൟ䕀ᤶ˖ ḍ᥂䇋∖ⱘᇍ䈵㉏ൟˈmalloc ៪ calloc ߑ᭄䖨ಲⱘᣛ䩜⒵䎇ℷ⹂ⱘᇍ唤㽕∖DŽϟ䴶ⱘ ぎ䯈㹿߱ྟ࣪Ў 0DŽټ៤ⱘ᭄㒘ˈ৺߭䖨ಲ NULLDŽ䆹ᄬ ᔧߚ䜡៤ࡳᯊˈᅗ䖨ಲϔϾᣛ䩜ˈ䆹ᣛ䩜ᣛ৥ⱘぎ䯆ぎ䯈䎇ҹᆍ㒇⬅ n Ͼᣛᅮ䭓ᑺⱘᇍ䈵㒘 void *calloc(size_t n, size_t size) NULLDŽߑ᭄ calloc ⱘໄᯢЎ ぎ䯈ˈ৺߭䖨ಲټᔧߚ䜡៤ࡳᯊˈᅗ䖨ಲϔϾᣛ䩜ˈ䆒ᣛ䩜ᣛ৥ n ᄫ㡖䭓ᑺⱘ᳾߱ྟ࣪ⱘᄬ void *malloc(size_t n) ഫDŽߑ᭄ malloc ⱘໄᯢབϟ˖ټߑ᭄ malloc ੠ calloc ⫼Ѣࡼᗕഄߚ䜡ᄬ ㅵ⧚ߑ᭄ټ7.8.5. ᄬ 䖨ಲؐDŽ ᗕؐˈ݊ؐᴹ㞾Ѣᠻ㸠ⱘੑҸˈᑊৠ݋ԧ㋏㒳᳝݇DŽ೼ UNIX ㋏㒳Ёˈ䖨ಲⱘ⢊ᗕᰃ exit ⱘ 䕧ߎϞᠧॄᔧ໽ⱘ᮹ᳳ੠ᯊ䯈DŽsystem ߑ᭄䖨ಲϔϾᭈൟⱘ⢊ޚᇚᠻ㸠⿟ᑣ dateˈᅗ೼ᷛ system("date"); হ ݙᆍ೼ᕜ໻⿟ᑺϞϢ᠔⫼ⱘ᪡԰㋏㒳᳝݇DŽϟ䴶ᴹⳟϔϾ UNIX ᪡԰㋏㒳⦃๗ⱘᇣ՟ᄤDŽ䇁 ߑ᭄ system(char* s)ᠻ㸠ࣙ৿೼ᄫヺ⬇ s ЁⱘੑҸˈ✊ৢ㒻㓁ᠻ㸠ᔧࠡ⿟ᑣDŽs ⱘ 7.8.4. ੑҸᠻ㸠ߑ᭄ getcharDŽ ᥹ᬊϔϾݭಲᄫヺDŽungetc ߑ᭄ৃҹ੠ӏԩϔϾ䕧ܹߑ᭄ϔ䍋Փ⫼ˈ↨བ scanfǃgetc ៪ 䆹ߑ᭄ᇚᄫヺ c ݭಲࠄ᭛ӊ fp ЁDŽབᵰᠻ㸠៤ࡳˈ߭䖨ಲ cˈ৺߭䖨ಲ EOFDŽ↣Ͼ᭛ӊা㛑 int ungetc(int c, FILE *fp) ফ䰤ࠊDŽ ᑧᦤկњϔϾ⿄Ў ungetc ⱘߑ᭄ˈᅗϢ㄀ 4 ゴЁ㓪ݭⱘߑ᭄ ungetch Ⳍ↨ࡳ㛑᳈ޚᷛ ungetc ߑ᭄ .7.8.3 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ⱘⳂⱘDŽ㗗㰥㡖ⳕぎ䯈៪ᯊ䯈ⱘᅲ⦄ᮍᓣDŽ 㒗д 7•9 ㉏ԐѢ isupper 䖭ḋⱘߑ᭄ৃҹ䗮䖛ᶤ⾡ᮍᓣᅲ⦄ҹ䖒ࠄ㡖ⳕぎ䯈៪ᯊ䯈 rand ੠ srand ߑ᭄ⱘৃ⿏ỡⱘᅲ⦄DŽ ⱘޚߑ᭄ srand(unsigned)䆒㕂 rand ߑ᭄ⱘ⾡ᄤ᭄DŽ៥Ӏ೼ 2.7 㡖Ё㒭ߎњ䙉ᕾᷛ ݋᳝᳈དⱘ㒳䅵ᄺ⡍ᗻDŽ˅ ˄བᵰ᠔⫼ⱘߑ᭄ᑧЁᏆ㒣ᦤկњϔϾ⫳៤⍂⚍䱣ᴎ᭄ⱘߑ᭄ˈ䙷Мᅗৃ㛑↨Ϟ䴶䖭Ͼߑ᭄ #define frand() ((double) rand() / (RAND_MAX+1.0)) ᮍ⊩˖ ᭛ӊЁᅮНⱘヺোᐌ䞣DŽϟ䴶ᰃϔ⾡⫳៤໻ѢㄝѢ 0 ԚᇣѢ 1 ⱘ䱣ᴎ⍂⚍᭄ⱘ ߑ᭄ rand()⫳៤ҟѢ 0 ੠ RAND_MAX П䯈ⱘӾ䱣ᴎᭈ᭄ᑣ߫DŽ݊Ё RAND_MAX ᰃ೼༈ 7.8.7. 䱣ᴎ᭄থ⫳఼ߑ᭄ fabs(x) x ⱘ㒱ᇍؐ sqrt(x) x ⱘᑇᮍḍ˄[•0˅ pow(x, y) 䅵ㅫ xy ⱘؐ log10(x) x ⱘᐌ⫼ᇍ᭄˄ҹ 10 Ўᑩ˅ˈ݊Ёˈx>0 log(x) x ⱘ㞾✊ᇍ᭄˄ҹ e Ўᑩ˅ˈ݊Ёˈx>0 exp(x) ᣛ᭄ߑ᭄ ex atan2(y, x) y/x ⱘডℷߛߑ᭄ˈ݊Ёˈx ੠ y ⫼ᓻᑺ㸼⼎ cos(x) x ⱘԭᓺߑ᭄ˈ݊Ё x ⫼ᓻᑺ㸼⼎ sin(x) x ⱘℷᓺߑ᭄ˈ݊Ё x ⫼ᓻᑺ㸼⼎ ᏺ᳝ϔϾ៪ϸϾ double ㉏ൟⱘখ᭄ˈᑊ䖨ಲϔϾ double ㉏ൟⱘؐDŽ ༈᭛ӊЁໄᯢњ 20 ໮Ͼ᭄ᄺߑ᭄DŽϟ䴶ҟ㒡ϔѯᐌ⫼ⱘ᭄ᄺߑ᭄ˈ↣Ͼߑ᭄ 7.8.6. ᭄ᄺߑ᭄ ഫৃҹҹӏᛣ乎ᑣ䞞ᬒDŽټ ߚ䜡⿟ᑣߚ䜡ⱘᄬټߚ䜡⿟ᑣⱘᅲ⦄DŽ䆹ᄬټ8.7 㡖㒭ߎњϔϾ㉏ԐѢ malloc ߑ᭄ⱘᄬ } free(p); q = p•>next; for (p = head; p != NULL; p = q) { ᇚϔߛᖙ㽕ⱘֵᙃֱᄬ䍋ᴹˈབϟ᠔⼎˖ܜℷ⹂ⱘ໘⧚ᮍ⊩ᰃˈ೼䞞ᬒ乍ⳂПࠡ free(p); for (p = head; p != NULL; p = p•>next) /* WRONG */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᑣՓ⫼᭛ӊ 0 ԰Ў䕧ܹˈ᭛ӊ 1 ੠ 2 ԰Ў䕧ߎˈᅗህϡӮⶹ䘧⿟ᑣⱘ䕧ܹҢા䞠ᴹˈᑊ䕧ߎ ϟˈ᭛ӊ䌟ؐⱘᬍব䛑ϡᰃ⬅⿟ᑣᅠ៤ⱘˈ㗠ᰃ⬅ shell ᅠ៤ⱘDŽা㽕⿟މⱘ⡍ᗻDŽ೼ӏԩᚙ ҡϢᰒ⼎఼Ⳍ݇㘨ˈ䖭ḋˈߎ䫭ֵᙃӮ䕧ߎࠄᰒ⼎఼ϞDŽϢㅵ䘧Ⳍ݇ⱘ䕧ܹˋ䕧ߎг᳝㉏Ԑ ϟˈshell ᡞ᭛ӊᦣ䗄ヺ 0 ੠ 1 ⱘ咬䅸䌟ؐᬍবЎᣛᅮⱘ᭛ӊDŽ䗮ᐌˈ᭛ӊᦣ䗄ヺ 2މ䖭⾡ᚙ prog < ೛దำߑ੠ > ೛ѻำߑ੠ ⿟ᑣⱘՓ⫼㗙ৃ䗮䖛<੠>䞡ᅮ৥⿟ᑣⱘ I/O˖ Ё䇏ˈᇍ 1 ੠ 2 䖯㸠ݭˈህৃҹ䖯㸠䕧ˋ䕧ߎ㗠ϡᖙ݇ᖗᠧᓔ᭛ӊⱘ䯂乬DŽ 䫭䇃DŽབᵰ⿟ᑣҢ᭛ӊ 0ޚ䕧ߎ੠ᷛޚ䕧ܹˈᷛޚⱘ᭛ӊᦣ䗄ヺߚ߿Ў 0ˈ1ˈ2ˈձ⃵㸼⼎ᷛ ⡍߿ⱘᅝᥦDŽᔧੑҸ㾷䞞⿟ᑣ˄े“shellā˅䖤㸠ϔϾ⿟ᑣⱘᯊ׭ˈᅗᇚᠧᓔ 3 Ͼ᭛ӊˈᇍᑨ њخ಴Ў໻໮᭄ⱘ䕧ܹˋ䕧ߎᰃ䗮䖛䬂Ⲭ੠ᰒ⼎఼ᴹᅲ⦄ⱘˈЎњᮍ֓䍋㾕ˈUNIX ᇍℸ ӊᦣ䗄ヺᓩ⫼᭛ӊˈ ᣛ䩜៪ MS•DOS Ёⱘ᭛ӊহᶘDŽ˅㋏㒳䋳䋷㓈ᡸᏆᠧᓔ᭛ӊⱘ᠔ֵ᳝ᙃˈ⫼᠋⿟ᑣা㛑䗮䖛᭛ ᑧЁⱘ᭛ӊޚ䗮䖛᭛ӊᦣ䗄ヺᷛ䆚᭛ӊˈ㗠ϡᰃ䗮䖛᭛ӊৡᷛ䆚᭛ӊDŽ˄᭛ӊᦣ䗄ヺ㉏ԐѢᷛ ৥⿟ᑣ䖨ಲϔϾᇣⱘ䴲䋳ᭈ᭄ˈ䆹ᭈ᭄⿄Ў᭛ӊᦣ䗄ヺDŽӏԩᯊ׭ᇍ᭛ӊⱘ䕧ܹˋ䕧ߎ䛑ᰃ ㋏㒳ẔᶹԴⱘᴗ࡯˄䆹᭛ӊᰃ৺ᄬ೼˛ᰃ৺᳝䆓䯂ᅗⱘᴗ䰤˛˅ˈབᵰϔߛℷᐌˈ᪡԰䞛㒳ᇚ Ꮖᄬ೼ⱘݙᆍDŽܜ߯ᓎ䆹᭛ӊˈгৃ㛑䳔㽕϶ᓗ䆹᭛ӊЁॳܜབᵰᰃݭϔϾ᭛ӊˈ߭ৃ㛑䳔㽕 ᇚ䖭Ͼᛣ೒䗮ⶹ㋏㒳ˈ䆹䖛⿟⿄Ўӕࢗ᭛ӊDŽܜϟˈ೼䇏៪ݭ᭛ӊПࠡˈᖙ乏މ䗮ᐌᚙ ᥹ষህৃҹ໘⧚໪ೈ䆒໛੠⿟ᑣП䯈ⱘ᠔᳝䗮ֵDŽ ᭛ӊˈ಴ℸˈ᠔᳝ⱘ䕧ܹˋ䕧ߎ䛑㽕䗮䖛䇏᭛ӊ៪ݭ᭛ӊᅠ៤DŽгህᰃ䇈ˈ䗮䖛ϔϾऩϔⱘ ೼ UNIX ᪡԰㋏㒳Ёˈ᠔᳝ⱘ໪ೈ䆒໛˄ࣙᣀ䬂Ⲭ੠ᰒ⼎఼˅䛑㹿ⳟ԰ᰃ᭛ӊ㋏㒳Ёⱘ 8.1. ᭛ӊᦣ䗄ヺ ᑧDŽޚ䕧ܹ੠䕧ߎⱘ㋏㒳䇗⫼ˈᑊҟ㒡བԩ䗮䖛ᅗӀᅲ⦄ᷛ ᑧߑ᭄ⱘᅲ⦄ᖙ乏䗮䖛ᆓЏ㋏㒳ᦤկⱘࡳ㛑ᴹᅲ⦄DŽ᥹ϟᴹⱘ޴㡖ᇚҟ㒡 UNIX ㋏㒳Ё⫼Ѣ ޚ㄀ 7 ゴҟ㒡ⱘ䕧ܹˋ䕧ߎ᥹ষᇍӏԩ᪡԰㋏㒳䛑ᰃϔḋⱘDŽ೼ӏԩ⡍ᅮⱘ㋏㒳Ёˈᷛ ݙᆍ㽕∖䇏㗙ᇍ UNIX ㋏㒳ⱘ໪䚼⡍ᗻ᳝ϔᅮⱘњ㾷DŽ ߚ䜡DŽ݊Ёˈࠡϸ䚼ߚⱘټᴀゴⱘݙᆍࣙᣀ 3 ϾЏ㽕䚼ߚˈ䕧ܹˋ䕧ߎǃ᭛ӊ㋏㒳੠ᄬ ᑧDŽޚЎ෎⸔ᓎゟ䍋ᴹⱘˈ᠔ҹˈᄺдᴀゴЁⱘ⿟ᑣ䖬ᇚ᳝ࡽѢ᳈དഄ⧚㾷ᷛ ߑ᭄ᑧᰃҹ UNIX ㋏㒳ޚЁⱘҷⷕ݋᳝ⳌԐᗻˈাᰃϔѯ㒚㡖Ϟ᳝ऎ߿㗠ᏆDŽ಴Ў ANSI C ᷛ ㋏㒳ϞՓ⫼ C 䇁㿔ˈᴀゴⱘ՟ᄤгᇚӮᐂࡽԴᇍ C 䇁㿔⿟ᑣ䆒䅵᳝᳈⏅ܹⱘњ㾷DŽϡৠ㋏㒳 ᑧЁ≵᳝ⱘᶤѯࡳ㛑DŽԚᰃˈेՓ䇏㗙ᰃ೼݊ᅗ᪡԰ޚ䇗⫼ҹ㦋ᕫ᳔催ⱘᬜ⥛ˈ៪㗙䆓䯂ᷛ ࡽѢ㋏㒳׳བᵰ䇏㗙Փ⫼ⱘᰃ UNIXˈᴀゴᇚӮᇍԴ᳝Ⳉ᥹ⱘᐂࡽˈ䖭ᰃ಴Ўˈ៥Ӏ㒣ᐌ䳔㽕 ᭄ˈᅗӀৃҹ㹿⫼᠋⿟ᑣ䇗⫼DŽᴀゴᇚҟ㒡བԩ೼ C 䇁㿔⿟ᑣЁՓ⫼ϔѯ䞡㽕ⱘ㋏㒳䇗⫼DŽ UNIX ᪡԰㋏㒳䗮䖛ϔ㋏߫ⱘຂහԸဈᦤկ᳡ࡵˈ䖭ѯ㋏㒳䇗⫼ᅲ䰙Ϟᰃ᪡԰㋏㒳ݙⱘߑ ㄀8ゴ UNIX ㋏㒳᥹ষ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! { int getchar(void) /* getchar: unbuffered single character input */ #include "syscalls.h" 䕧ܹDŽކ䇏ܹϔϾᄫヺᴹᅲ⦄᮴㓧 䕧ܹޚputchar ㄝⱘ催㑻ߑ᭄DŽ՟བˈҹϟᰃ getchar ߑ᭄ⱘϔϾ⠜ᴀˈᅗ䗮䖛↣⃵Ңᷛ Ўњ᳈དഄᥠᦵ᳝݇ὖᗉˈϟ䴶ᴹ䇈ᯢབԩ⫼ read ੠ write ᵘ䗴㉏ԐѢ getcharǃ Ͼ䕗ᇣⱘᄫ㡖᭄ˈwrite ݡᣝ䖭Ͼᄫ㡖᭄ݭˈℸৢݡ䇗⫼ read ᇚ䖨ಲ 0DŽ ᭄ˈ߭ᇍ read ⱘᶤ⃵䇗⫼Ӯ䖨ಲϔסᰃϔϾ䕗ড়䗖ⱘ᭄ؐDŽབᵰ᭛ӊ໻ᇣϡᰃ BUFSIZ ⱘ খ᭄ BUFSIZ гᏆ㒣೼ syscalls.h ༈᭛ӊЁᅮНDŽᇍѢ᠔Փ⫼ⱘ᪡԰㋏㒳ᴹ䇈ˈ䆹ؐ ⱘDŽޚ⿟ᑣ䛑ᇚࣙ৿䆹༈᭛ӊDŽϡ䖛ˈ䆹᭛ӊⱘৡᄫϡᰃᷛ ៥ӀᏆ㒣ᇚ㋏㒳䇗⫼ⱘߑ᭄ॳൟ䲚Ёᬒ೼ϔϾ༈᭛ӊ syscalls.h Ёˈ಴ℸˈᴀゴЁⱘ } return 0; write(1, buf, n); while ((n = read(0, buf, BUFSIZ)) > 0) int n; char buf[BUFSIZ]; { main() /* copy input to output */ #include "syscalls.h" ࠄӏԩ᭛ӊ៪䆒໛DŽ ໡ࠊ⿟ᑣ೼ࡳ㛑ϞⳌৠDŽ⿟ᑣৃҹᇚӏᛣ䕧ܹ໡ࠊࠄӏᛣ䕧ߎˈ಴Ў䕧ܹˋ䕧ߎৃҹ䞡ᅮ৥ 㒧ড়ҹϞⱘ䅼䆎ˈ៥Ӏৃҹ㓪ݭϔϾㅔऩⱘ⿟ᑣˈᇚ䕧ܹ໡ࠊࠄ䕧ߎˈ䖭Ϣ㄀ 1 ゴЁⱘ ᇥњDŽޣⱘؐDŽ⫼᳈໻ⱘؐ䇗⫼䆹ߑ᭄ৃҹ㦋ᕫ᳈催ⱘᬜ⥛ˈ಴Ў㋏㒳䇗⫼ⱘ⃵᭄ ៪ᰃ㉏ԐѢ 1024̚4096 䖭ḋⱘϢ໪ೈ䆒໛ⱘ⠽⧚ഫ໻ᇣⳌᑨˈ˅ކߎ៪ݭܹ 1 Ͼᄫヺ˄᮴㓧 ೼ϔ⃵䇗⫼Ёˈ䇏ߎ៪ݭܹⱘ᭄᥂ⱘᄫ㡖᭄ৃҹЎӏᛣ໻ᇣDŽ᳔ᐌ⫼ⱘؐЎ 1ˈे↣⃵䇏 䫭䇃DŽ ӊᯊˈ䖨ಲؐᰃᅲ䰙ݭܹⱘᄫ㡖᭄DŽབᵰ䖨ಲؐϢ䇋∖ݭܹⱘᄫ㡖᭄ϡⳌㄝˈ߭䇈ᯢথ⫳њ ᵰ䖨ಲؐЎ 0ˈ߭㸼⼎Ꮖࠄ䖒᭛ӊⱘ㒧ሒ˗བᵰ䖨ಲؐЎ•1ˈ߭㸼⼎থ⫳њᶤ⾡䫭䇃DŽ೼ݭ᭛ ↣Ͼ䇗⫼䖨ಲᅲ䰙Ӵ䕧ⱘᄫ㡖᭄DŽ೼䇏᭛ӊᯊˈߑ᭄ⱘ䖨ಲؐৃ㛑ӮᇣѢ䇋∖ⱘᄫ㡖᭄DŽབ int n_written = write(int fd, char *buf, int n); int n_read = read(int fd, char *buf, int n); ᑣЁᄬᬒ䇏៪ݭⱘ᭄᥂ⱘᄫヺ᭄㒘ˈ㄀ϝϾখ᭄ᰃ㽕Ӵ䕧ⱘᄫ㡖᭄DŽ ੠ write 䆓䯂䖭ϸϾ㋏㒳䇗⫼DŽ䖭ϸϾߑ᭄Ёˈ㄀ϔϾখ᭄ᰃ᭛ӊᦣ䗄ヺˈ㄀ѠϾখ᭄ᰃ⿟ 䕧ܹϢ䕧ߎᰃ䗮䖛read੠write㋏㒳䇗⫼ᅲ⦄ⱘDŽ೼C䇁㿔⿟ᑣЁˈৃҹ䗮䖛ߑ᭄read 8.2. Ԣ㑻 I/O——read ੠ write ࠄા䞠এDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ⠜ᴀЁ߭೼ЁᅮНDŽ ೼ System V UNIX ㋏㒳Ёˈ䖭ѯᐌ䞣೼༈᭛ӊЁᅮНˈ㗠೼ Berkeley˄BSD˅ O_RDWR ҹ䇏ݭᮍᓣᠧᓔ᭛ӊ O_WRONLY ҹাݭᮍᓣᠧᓔ᭛ӊ O_RDONLY ҹা䇏ᮍᓣᠧᓔ᭛ӊ ൟⱘؐˈᅗ䇈ᯢҹԩ⾡ᮍᓣᠧᓔ᭛ӊˈЏ㽕ⱘ޴Ͼؐབϟ᠔⼎˖ Ϣ fopen ϔḋˈখ᭄ name ᰃϔϾࣙ৿᭛ӊৡⱘᄫヺІDŽ㄀ѠϾখ᭄ flags ᰃϔϾ int ㉏ fd = open(name, flags, perms); int open(char *name, int flags, int perms); int fd; #include ϔϾ int ㉏ൟⱘ᭄ؐDŽ㗠ৢ㗙䖨ಲϔϾ᭛ӊᣛ䩜DŽབᵰথ⫳䫭䇃ˈopen ᇚ䖨ಲ•1DŽ open Ϣ㄀ 7 ゴ䅼䆎ⱘ fopen ⳌԐˈϡৠⱘᰃˈࠡ㗙䖨ಲϔϾ᭛ӊᦣ䗄ヺˈᅗҙҙাᰃ ഄᠧᓔDŽ㋏㒳䇗⫼ open ੠ creat ⫼Ѣᅲ⦄䆹ࡳ㛑DŽ 䫭䇃᭛ӊ໪ˈ݊ᅗ᭛ӊ䛑ᖙ乏೼䇏៪ݭПࠡᰒᓣޚ䕧ߎ੠ᷛޚ䕧ܹǃᷛޚ䰸њ咬䅸ⱘᷛ 8.3. openǃcreatǃclose ੠ unlink ᅲ⦄ⱘDŽ #undef 乘໘⧚ᣛҸপ⍜ৡᄫ getchar ⱘᅣᅮНˈ಴Ў೼༈᭛ӊЁˈgetchar ᰃҹᅣᮍᓣ ϟ㓪䆥䖭ѯ⠜ᴀⱘ getchar ߑ᭄ˈህ᳝ᖙ㽕⫼މབᵰ㽕೼ࣙ৿༈᭛ӊⱘᚙ } return (••n >= 0) ? (unsigned char) *bufp++ : EOF; } bufp = buf; n = read(0, buf, sizeof buf); if (n == 0) { /* buffer is empty */ static int n = 0; static char *bufp = buf; static char buf[BUFSIZ]; { int getchar(void) /* getchar: simple buffered version */ #include "syscalls.h" getchar ⱘ㄀ѠϾ⠜ᴀϔ⃵䇏ܹϔ㒘ᄫヺˈԚ↣⃵া䕧ߎϔϾᄫヺDŽ ೼䖨ಲ䇁হЁᇚ c 䕀ᤶЎ unsigned char ㉏ൟৃҹ⍜䰸ヺোᠽሩ䯂乬DŽ ݊Ёˈc ᖙ乏ᰃϔϾ char ㉏ൟⱘব䞣ˈ಴Ў read ߑ᭄䳔㽕ϔϾᄫヺᣛ䩜㉏ൟⱘখ᭄˄&c˅DŽ } return (read(0, &c, 1) == 1) ? (unsigned char) c : EOF; char c; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ⊼ᛣˈߑ᭄ error ㉏ԐѢߑ᭄ printfˈ೼䇗⫼ᯊৃᏺব䭓খ᭄㸼DŽϟ䴶䗮䖛 error ߑ 㦋ᕫϔϾᏆᄬ೼᭛ӊⱘ῵ᓣˈᑊᇚℸ῵ᓣ䌟ؐ㒭ᅗⱘࡃᴀDŽ 䆹⿟ᑣ߯ᓎⱘ䕧ߎ᭛ӊ݋᳝೎ᅮⱘᴗ䰤 0666DŽ߽⫼ 8.6 㡖Ёᇚ㽕䅼䆎ⱘ stat ㋏㒳䇗⫼ˈৃҹ } return 0; error("cp: write error on file %s", argv[2]); if (write(f2, buf, n) != n) while ((n = read(f1, buf, BUFSIZ)) > 0) argv[2], PERMS); error("cp: can't create %s, mode %03o", if ((f2 = creat(argv[2], PERMS)) == •1) error("cp: can't open %s", argv[1]); if ((f1 = open(argv[1], O_RDONLY, 0)) == •1) error("Usage: cp from to"); if (argc != 3) char buf[BUFSIZ]; int f1, f2, n; { main(int argc, char *argv[]) /* cp: copy f1 to f2 */ void error(char *, ...); #define PERMS 0666 /* RW for owner, group, others */ #include "syscalls.h" #include #include Ⳃᷛ᭛ӊⱘᴗ䰤ϡᰃ䗮䖛໡ࠊ㦋ᕫⱘˈ㗠ᰃ䞡ᮄᅮНⱘDŽ 䆌⫼Ⳃᔩ԰Ў㄀ѠϾখ᭄ˈᑊϨˈܕ᭛ӊDŽ៥Ӏ㓪ݭⱘ䖭Ͼ⠜ᴀҙҙা㛑໡ࠊϔϾ᭛ӊˈϡ ϟ䴶䗮䖛ϔϾㅔ࣪ⱘ UNIX ⿟ᑣ cp 䇈ᯢ creat ⱘ⫼⊩DŽ䆹⿟ᑣᇚϔϾ᭛ӊ໡ࠊࠄ঺ϔϾ ៤ਬা㛑䖯㸠䇏੠ᠻ㸠᪡԰DŽ ⱘᴗ䰤ˈ՟བˈ0755 䇈ᯢ᭛ӊⱘ᠔᳝㗙ৃҹᇍᅗ䖯㸠䇏ǃݭ੠ᠻ㸠᪡԰ˈ㗠᠔᳝㗙㒘੠݊Ҫ ݊Ҫ៤ਬᇍ᭛ӊⱘ䇏ǃݭ੠ᠻ㸠䆓䯂DŽ಴ℸˈ䗮䖛ϔϾ 3 ԡⱘܿ䖯ࠊ᭄ህৃᮍ֓ഄ䇈ᯢϡৠ ӊ㋏㒳Ёˈ↣Ͼ᭛ӊᇍᑨϔϾ 9 ↨⡍ⱘᴗ䰤ֵᙃˈᅗӀߚ߿᥻ࠊ᭛ӊⱘ᠔᳝㗙ǃ᠔᳝㗙㒘੠ བᵰ㽕߯ᓎⱘ᭛ӊϡᄬ೼ˈ߭ creat ⫼খ᭄ perms ᣛᅮⱘᴗ䰤߯ᓎ᭛ӊDŽ೼ UNIX ᭛ ⱘ᭛ӊϡӮᇐ㟈䫭䇃DŽ Ꮖ᳝ⱘݙᆍDŽՓ⫼ creat ߯ᓎϔϾᏆᄬ೼ܜcreat ᇚᡞ䆹᭛ӊⱘ䭓ᑺ៾ᮁЎ 0ˈҢ㗠϶ᓗॳ བᵰ creat ៤ࡳഄ߯ᓎњ᭛ӊˈᅗᇚ䖨ಲϔϾ᭛ӊᦣ䗄ヺˈ৺߭䖨ಲ•1DŽབᵰℸ᭛ӊᏆᄬ೼ˈ fd = creat(name, perms); int creat(char *name, int perms); ᭛ӊ៪㽚ⲪᏆ᳝ⱘᮻ᭛ӊˈབϟ᠔⼎˖ བᵰ⫼ open ᠧᓔϔϾϡᄬ೼ⱘ᭛ӊˈ߭ᇚᇐ㟈䫭䇃DŽৃҹՓ⫼ creat ㋏㒳䇗⫼߯ᓎᮄ ೼ᴀゴⱘ䅼䆎Ёˈopen ⱘখ᭄ perms ⱘؐྟ㒜Ў 0DŽ fd = open(name, O_RDONLY,0); ҹՓ⫼ϟ߫䇁হᠧᓔϔϾ᭛ӊҹᠻ㸠䇏᪡԰˖ৃ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! lseek(fd, 0L, 0); 㢹㽕䖨ಲ᭛ӊⱘᓔྟ໘˄ेড㒩˅ˈ߭ৃҹՓ⫼ϟ߫䇗⫼˖ lseek(fd, 0L, 2); “aā˅ˈ߭೼ݭ᪡԰Пࠡᖙ乏Փ⫼ϟ߫㋏㒳䇗⫼ᡒࠄ᭛ӊⱘ᳿ሒ˖ ӊⱘሒ䚼⏏ࡴݙᆍ˄೼ UNIX shell⿟ᑣЁՓ⫼䞡ᅮ৥ヺ>>៪೼㋏㒳䇗⫼ fopen ЁՓ⫼খ᭄ ⫼Ѣᣛᅮ offset Ң᭛ӊᓔྟǃҢᔧࠡԡ㕂៪Ң᭛ӊ㒧ᴳ໘ᓔྟㅫ䍋DŽ՟བˈЎњ৥ϔϾ᭛ ⱘԡ㕂㗠㿔ⱘDŽ䱣ৢ䖯㸠ⱘ䇏ݭ᪡԰ᇚҢℸԡ㕂ᓔྟˈorigin ⱘؐৃҹЎ 0ǃ1 ៪ 2ˈߚ߿ ᇚ᭛ӊᦣ䗄ヺЎ fd ⱘ᭛ӊⱘᔧࠡԡ㕂䆒㕂Ў offsetˈ݊Ёˈoffset ᰃⳌᇍѢ orgin ᣛᅮ long lseek(int fd, long offset, int origin); ᛣ⿏ࡼԡ㕂㗠ϡᅲ䰙䇏ݭӏԩ᭄᥂˖ ᪡԰ⱘԡ㕂ПৢDŽԚᰃˈ᳝ᯊ׭䳔㽕ҹӏᛣ乎ᑣ䆓䯂᭛ӊˈ㋏㒳䇗⫼ lseek ৃҹ೼᭛ӊЁӏ 䕧ܹˋ䕧ߎ䗮ᐌᰃ乎ᑣ䖯㸠ⱘ˖↣⃵䇗⫼ read ੠ write 䖯㸠䇏ݭⱘԡ㕂㋻䎳೼ࠡϔ⃵ 8.4. 䱣ᴎ䆓䯂——lseek 䞡ݭ㄀ 7 ゴⱘ cat ⿟ᑣˈᑊ䗮䖛ᅲ偠↨䕗ϸϾ⠜ᴀⱘⳌᇍᠻ㸠䗳ᑺDŽ ᑧЁࡳ㛑ㄝӋⱘߑ᭄ˈޚ㒗д 8•1 ⫼ readǃwriteǃopen ੠ close ㋏㒳䇗⫼ҷ᳓ᷛ removeDŽ ᑧߑ᭄ޚߑ᭄ unlink(char *name)ᇚ᭛ӊ name Ң᭛ӊ㋏㒳Ёߴ䰸ˈᅗᇍᑨѢᷛ Џ⿟ᑣЁ䖨ಲˈ᠔᳝ᠧᓔⱘ᭛ӊᇚ㹿݇䯁DŽ ऎDŽབᵰ⿟ᑣ䗮䖛 exit ߑ᭄䗔ߎ៪Ңކfclose ߑ᭄ⳌᇍᑨˈԚᅗϡ䳔㽕⏙⋫˄flush˅㓧 ᑧЁⱘޚᠧᓔ᭛ӊП䯈ⱘ䖲᥹ˈᑊ䞞ᬒℸ᭛ӊᦣ䗄ヺˈҹկ݊ᅗ᭛ӊՓ⫼DŽclose ߑ᭄Ϣᷛ ⧚䆌໮᭛ӊˈ䙷Мᅗᖙ乏䞡⫼᭛ӊᦣ䗄ヺDŽߑ᭄ close˄int fd ˅⫼ᴹᮁᓔ᭛ӊᦣ䗄ヺ੠Ꮖ ϔϾ⿟ᑣৠᯊᠧᓔⱘ᭛ӊ᭄ᰃ᳝䰤ࠊⱘ˄䗮ᐌЎ 20˅DŽⳌᑨഄˈབᵰϔϾ⿟ᑣ䳔㽕ৠᯊ໘ } exit(1); va_end(args); fprintf(stderr, "\n"); vprintf(stderr, fmt, args); fprintf(stderr, "error: "); va_start(args, fmt); va_list args; { void error(char *fmt, ...) /* error: print an error message and die */ #include #include ߑ᭄㉏ԐDŽ va_start ᅣ䖯㸠߱ྟ࣪DŽৠḋˈvfprintf ੠ vsprintf ߑ᭄ߚ߿Ϣ fprintf ੠ sprintf ᭄Ϣ printf ߑ᭄㉏Ԑˈ᠔ϡৠⱘᰃˈᅗ⫼ϔϾখ᭄পҷњব䭓খ᭄㸼ˈϨℸখ᭄䗮䖛䇗⫼ ᑧߑ᭄ vprintf ߑޚ᭄ⱘᅲ⦄䇈ᯢབԩՓ⫼ printf ߑ᭄ᆊᮣⱘ঺ϔϾ៤ਬ vprintfDŽᷛ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! extern FILE _iob[OPEN_MAX]; } FILE; int fd; /* file descriptor */ int flag; /* mode of file access */ char *base; /* location of buffer */ char *ptr; /* next character position */ int cnt; /* characters left */ typedef struct _iobuf { #define OPEN_MAX 20 /* max #files open at once */ #define BUFSIZ 1024 #define EOF (•1) #define NULL 0 䆹㑺ᅮDŽ ᑧߑ᭄䛑䙉ᕾޚさDŽ᠔᳝ⱘᷛކ⫼ⱘৡᄫҹϟߦ㒓ᓔྟˈ಴ℸϔ㠀ϡӮϢ⫼᠋⿟ᑣЁⱘৡᄫ ᑧЁ݊ᅗߑ᭄᠔ՓޚᑧЁⱘ݊ᅗߑ᭄ࣙ৿DŽ೼ϟ䴶䖭↉݌ൟⱘҷⷕ↉Ёˈাկᷛ ᭄ⱘ⿟ᑣ䛑ᖙ乏೼⑤᭛ӊЁࣙ৿䖭Ͼ༈᭛ӊ˄䗮䖛#include ᣛҸࣙ৿༈᭛ӊ˅DŽℸ᭛ӊг㹿 䕧ܹˋ䕧ߎᑧЁߑޚᦣ䗄᭛ӊⱘ᭄᥂㒧ᵘࣙ৿೼༈᭛ӊЁˈӏԩ䳔㽕Փ⫼ᷛ ⢊ᗕⱘᷛᖫㄝDŽ ऎЁϟϔϾᄫヺⱘᣛ䩜˗᭛ӊᦣ䗄ヺ˗ᦣ䗄䇏ˋݭ῵ᓣⱘᷛᖫ˗ᦣ䗄䫭䇃ކ఼˗ϔϾᣛ৥㓧 ऎЁ࠽ԭⱘᄫヺ᭄ⱘ䅵᭄ކऎⱘᣛ䩜ˈ䗮䖛ᅗৃҹϔ⃵䇏ܹ᭛ӊⱘϔ໻ഫݙᆍ˗ϔϾ䆄ᔩ㓧 ކ᭛ӊᣛ䩜ᰃϔϾᣛ৥ࣙ৿᭛ӊ৘⾡ֵᙃⱘ㒧ᵘⱘᣛ䩜ˈ䆹㒧ᵘࣙ৿ϟ߫ݙᆍ˖ϔϾᣛ৥㓧 ᑧЁⱘ᭛ӊϡᰃ䗮䖛᭛ӊᦣ䗄ヺᦣ䗄ⱘˈ㗠ᰃՓ⫼᭛ӊᣛ䩜ᦣ䗄ⱘDŽޚ៥Ӏಲᖚϔϟˈᷛ 䍋ᴹՓ⫼DŽ ᑧߑ᭄ fopen ੠ getc ⱘϔ⾡ᅲ⦄ᮍ⊩Ў՟ᴹ䇈ᯢབԩᇚ䖭ѯ㋏㒳䇗⫼㒧ড়ޚϟ䴶ҹᷛ 8.5. ᅲ՟——fopen ੠ getc ߑ᭄ⱘᅲ⦄ Ϩ೼থ⫳䫭䇃ᯊ䖨ಲϔϾ䴲 0 ؐDŽ ᑧߑ᭄ fseek Ϣ㋏㒳䇗⫼ lseek ㉏Ԑˈ᠔ϡৠⱘᰃˈࠡ㗙ⱘ㄀ϔϾখ᭄ᰃ FILE * ㉏ൟˈޚᷛ lseek ㋏㒳䇗⫼䖨ಲϔϾ long ㉏ൟⱘؐˈℸؐ㸼⼎᭛ӊⱘᮄԡ㕂ˈ㢹থ⫳䫭䇃ˈ߭䖨ಲ•1DŽ } return •1; else return read(fd, buf, n); if (lseek(fd, pos, 0) >= 0) /* get to pos */ { int get(int fd, long pos, char *buf, int n) /*get: read n bytes from position pos */ #include "syscalls.h" 䇃ˈ߭䖨ಲ•1DŽ བˈϟ䴶ⱘߑ᭄ᇚҢ᭛ӊⱘӏᛣԡ㕂䇏ܹӏᛣ᭄Ⳃⱘᄫ㡖ˈᅗ䖨ಲ䇏ܹⱘᄫ㡖᭄ˈ㢹থ⫳䫭 Փ⫼ lseek ㋏㒳䇗⫼ᯊˈৃҹᇚ᭛ӊ㾚ЎϔϾ໻᭄㒘ˈ݊ҷӋᰃ䆓䯂䗳ᑺӮ᜶ϔѯDŽ՟ ϔ㟈DŽ 䇋⊼ᛣˈখ᭄ 0L гৃݭЎ(long)0ˈ៪ҙҙݭЎ 0ˈԚᰃ㋏㒳䇗⫼ lseek ⱘໄᯢᖙ乏ֱᣕ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! if (fp >= _iob + OPEN_MAX) /* no free slots */ break; /* found free slot */ if ((fp•>flag & (_READ | _WRITE)) == 0) for (fp = _iob; fp < _iob + OPEN_MAX; fp++) return NULL; if (*mode != 'r' && *mode != 'w' && *mode != 'a') FILE *fp; int fd; { FILE *fopen(char *name, char *mode) #define PERMS 0666 /* RW for owner, group, others */ #include "syscalls.h" #include ᭛ӊᯊ⬅ߑ᭄_fillbuf ᅠ៤ⱘDŽ ऎⱘߚ䜡ᰃ೼㄀ϔ⃵䇏ކऎぎ䯈ˈ㓧ކ㕂ˈ䆒㕂ᷛᖫԡҹᣛ⼎Ⳍᑨⱘ⢊ᗕDŽᅗϡߚ䜡ӏԩ㓧 ϟ䴶៥Ӏᴹⴔ᠟㓪ݭߑ᭄ fopenDŽfopen ߑ᭄ⱘЏ㽕ࡳ㛑ᰃᠧᓔ᭛ӊˈᅮԡࠄড়䗖ⱘԡ ݊Ёࣙ৿њ䆓䯂䫭䇃䕧ߎǃ᭛ӊ㒧ᴳ⢊ᗕ੠᭛ӊᦣ䗄ヺⱘᅣDŽ ऎ⒵ᯊˈᅗᇚ䇗⫼ߑ᭄_flushbufDŽℸ໪ˈ៥Ӏ䖬೼ކⱘ᪡԰Ϣ getc ߑ᭄䴲ᐌ㉏Ԑˈᔧ㓧 ሑㅵ೼䖭䞠៥Ӏᑊϡᛇ䅼䆎ϔѯ㒚㡖ˈԚ⿟ᑣЁ䖬ᰃ㒭ߎњ putc ߑ᭄ⱘᅮНˈҹ㸼ᯢᅗ ㉏ൟDŽҹ⹂ֱ᠔᳝ⱘᄫヺЎℷؐDŽ ऎˈ䞡ᮄ߱ྟ࣪㒧ᵘⱘݙᆍˈᑊ䖨ಲϔϾᄫヺDŽ䖨ಲⱘᄫヺЎ unsignedކ㓧ܙ฿ _fillbuf Ͼ䭓ⱘ#define 䇁হৃ⫼ড᭰ᴴߚ៤޴㸠DŽ˅Ԛᰃˈབᵰ䅵᭄ؐবЎ䋳ؐˈgetc ህ䇗⫼ߑ᭄ ᇚᣛ䩜⿏ࠄϟϔϾԡ㕂ˈ✊ৢ䖨ಲᄫヺDŽ˄ࠡ䴶䆆䖛ˈϔˈ1 ޣᇚ䅵఼᭄ܜᅣ getc ϔ㠀 #define putcher(x) putc((x), stdout) #define getchar() getc(stdin) ? *(p)•>ptr++ = (x) : _flushbuf((x),p)) #define putc(x,p) (••(p)•>cnt >= 0 \ ? (unsigned char) *(p)•>ptr++ : _fillbuf(p)) #define getc(p) (••(p)•>cnt >= 0 \ #define fileno(p) ((p)•>fd) #define ferror(p) ((p)•>flag & _ERR) != 0) #define feof(p) ((p)•>flag & _EOF) != 0) int _flushbuf(int, FILE *); int _fillbuf(FILE *); }; _ERR = 020 /* error occurred on this file */ _EOF = 010, /* EOF has occurred on this file */ _UNBUF = 04, /* file is unbuffered */ _WRITE = 02, /* file open for writing */ _READ = 01, /* file open for reading */ enum _flags { #define stderr (&_iob[2]) #define stdout (&_iob[1]) define stdin (&_iob[0])# w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᳔ৢϔӊџᚙ֓ᰃབԩᠻ㸠䖭ѯߑ᭄DŽ៥Ӏᖙ乏ᅮН੠߱ྟ᭄࣪㒘_iob Ёⱘ stdinǃ } return (unsigned char) *fp•>ptr++; } return EOF; fp•>cnt = 0; fp•>flag |= _ERR; else fp•>flag |= _EOF; if (fp•>cnt == •1) if (••fp•>cnt < 0) { fp•>cnt = read(fp•>fd, fp•>ptr, bufsize); fp•>ptr = fp•>base; return EOF; /* can't get buffer */ if ((fp•>base = (char *) malloc(bufsize)) == NULL) if (fp•>base == NULL) /* no buffer yet */ bufsize = (fp•>flag & _UNBUF) ? 1 : BUFSIZ; return EOF; if ((fp•>flag&(_READ|_EOF_ERR)) != _READ) int bufsize; { int _fillbuf(FILE *fp) /* _fillbuf: allocate and fill input buffer */ #include "syscalls.h" ऎᏆ㒣ߚ䜡DŽކЁⱘ㄀ϔϾᄫヺDŽ䱣ৢ䖯㸠ⱘ_fillbuf 䇗⫼Ӯথ⦄㓧 ऎކऎˈ䆒㕂䅵᭄ؐ੠ᣛ䩜ˈᑊ䖨ಲ㓧ކℸ㓧ܙ฿ ऎৢˈ_fillbuf 䇗⫼ readކᓎゟ㓧 ᮍᓣ䖯㸠ⱘ䆱˅DŽކऎ˄བᵰ䇏᪡԰ᰃҹ㓧ކ䆩೒ߚ䜡ϔϾ㓧 _fillbufDŽབᵰ_fillbuf থ⦄᭛ӊϡᰃҹ䇏ᮍᓣᠧᓔⱘˈᅗᇚゟे䖨ಲ EOF˗৺߭ˈᅗᇚ ᇍѢᶤϔ⡍ᅮⱘ᭛ӊˈ㄀ϔ⃵䇗⫼ getc ߑ᭄ᯊ䅵᭄ؐЎ 0ˈ䖭ḋህᖙ乏䇗⫼ϔ⃵ߑ᭄ 䆌ৠᯊ䖯㸠䇏੠ݭⱘ+ᷛᖫDŽܕUNIX ㋏㒳Ё䖭⾡ᮍᓣᰃ≵᳝ᛣНⱘDŽৠᯊˈᅗгϡ㛑䆚߿ ᇥҷⷕDŽ⡍߿ᰃˈ䆹⠜ᴀⱘ fopen ϡ㛑䆚߿㸼⼎Ѡ䖯ࠊ䆓䯂ᮍᓣⱘ b ᷛᖫˈ䖭ᰃ಴Ўˈ೼ C ⱘ᠔᳝䆓䯂῵ᓣˈԚᰃˈࡴܹ䖭ѯ῵ᓣᑊϡ䳔㽕๲ࡴ໮ ޚ䆹⠜ᴀⱘ fopen ߑ᭄≵᳝⍝ঞᷛ } return fp; fp•>flag = (*mode == 'r') ? _READ : _WRITE; fp•>base = NULL; fp•>cnt = 0; fp•>fd = fd; return NULL; if (fd == •1) /* couldn't access name */ fd = open(name, O_RDONLY, 0); } else lseek(fd, 0L, 2); fd = creat(name, PERMS); if ((fd = open(name, O_WRONLY, 0)) == •1) else if (*mode == 'a') { fd = creat(name, PERMS); if (*mode == 'w') return NULL; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ⬭԰㒗дDŽމUNIX ㋏㒳ⱘⳂᔩ㒧ᵘⳌৠⱘ㋏㒳Ϟᅲ⦄䖭ѯߑ᭄DŽ݊ᅗᚙ 㓪োⱘ䆓䯂DŽ៥Ӏᇚ߽⫼ℸ᥹ষ㓪ݭ fsize ⿟ᑣˈ✊ৢ䇈ᯢབԩ೼Ϣ Version 7੠System V ߑ᭄ opendirǃreaddir ੠ closedirˈᅗӀᦤկϢ㋏㒳᮴݇ⱘᇍⳂᔩ乍Ёⱘৡᄫ੠ i 㒧⚍ ⾏ߎϡৃ⿏ỡⱘ䚼ߚˈ៥Ӏᡞӏࡵߚ៤ϸ䚼ߚDŽ໪ሖᅮНњϔϾ⿄Ў Dirent ⱘ㒧ᵘ੠ 3 Ͼ 䘫ធⱘᰃˈ೼ϡৠ⠜ᴀⱘ㋏㒳ЁˈⳂᔩⱘḐᓣ੠⹂ߛⱘݙᆍᰃϡϔḋⱘDŽ಴ℸˈЎњߚ ৡ੠ i 㒧⚍㓪োDŽ ᭛ӊⱘ i 㒧⚍ᰃᄬᬒ䰸᭛ӊৡҹ໪ⱘ᠔᳝᭛ӊֵᙃⱘഄᮍDŽⳂᔩ乍䗮ᐌҙࣙ৿ϸϾᴵⳂ˖᭛ӊ ᭛ӊৡ߫㸼੠ϔѯᣛ⼎᭛ӊԡ㕂ⱘֵᙃDŽĀԡ㕂āᰃϔϾᣛ৥݊ᅗ㸼˄े i 㒧⚍㸼˅ⱘ㋶ᓩDŽ ಲ乒 UNIX ᭛ӊ㋏㒳ⱘ㒧ᵘDŽ೼ UNIX ㋏㒳ЁˈⳂᔩህᰃ᭛ӊˈᅗࣙ৿њϔϾܜ៥Ӏ佪 ᔦ䇗⫼㞾䑿DŽབᵰੑҸ㸠Ё≵᳝ӏԩখ᭄ˈ߭ fsize ⿟ᑣ໘⧚ᔧࠡⳂᔩDŽ 㸠খ᭄㸼Ёᣛᅮⱘ᠔᳝᭛ӊⱘ䭓ᑺDŽབᵰ݊ЁϔϾ᭛ӊᰃⳂᔩˈ߭ fsize ⿟ᑣᇚᇍℸⳂᔩ䗦 ҹϟᇚ䗮䖛⿟ᑣ fsize 䇈ᯢ䖭ϔ⚍DŽfsize ⿟ᑣᰃ ls ੑҸⱘϔϾ⡍⅞ᔶᓣˈᅗᠧॄੑҸ ݋ԧⱘ㋏㒳᳝݇ˈ៥Ӏ䳔㽕ᦤկϔ⾡Ϣ㋏㒳᮴݇ⱘ䆓䯂᭛ӊֵᙃⱘ䗨ᕘDŽ ⫮㟇㦋প᭛ӊৡг䳔㽕Փ⫼㋏㒳䇗⫼ˈ՟བ೼ MS•DOS ㋏㒳ЁेབℸDŽ᮴䆎ᅲ⦄ᮍᓣᰃ৺ৠ ᰃˈབᵰ䳔㽕㦋প᭛ӊⱘ݊ᅗֵᙃˈ↨བ䭓ᑺㄝˈህ䳔㽕Փ⫼㋏㒳䇗⫼DŽ೼݊ᅗϔѯ㋏㒳Ёˈ ⬅Ѣ UNIX ЁⱘⳂᔩህᰃϔ⾡᭛ӊˈ಴ℸˈls া䳔㽕䇏ℸ᭛ӊህৃ㦋ᕫ᠔᳝ⱘ᭛ӊৡDŽԚ ᳝㉏Ԑⱘࡳ㛑DŽ ӊৡҹঞ݊ᅗϔѯৃ䗝ֵᙃˈབ᭛ӊ䭓ᑺǃ䆓䯂ᴗ䰤ㄝㄝDŽMS•DOS ᪡԰㋏㒳Ёⱘ dir ੑҸг ⱘ݋ԧݙᆍDŽⳂᔩ߫㸼⿟ᑣ֓ᰃ݊ЁⱘϔϾ՟ᄤˈ↨བ UNIX ੑҸ lsˈᅗᠧॄϔϾⳂᔩЁⱘ᭛ ៥Ӏᐌᐌ䖬䳔㽕ᇍ᭛ӊ㋏㒳ᠻ㸠঺ϔ⾡᪡԰ˈҹ㦋ᕫ᭛ӊⱘֵ᳝݇ᙃˈ㗠ϡᰃ䇏প᭛ӊ 8.6. ᅲ՟——Ⳃᔩ߫㸼 㛑໳णৠᎹ԰DŽކⱘ㓧 ؐᰃϔϾ int ㉏ൟⱘ⢊ᗕ㗠䴲ԡ㕂ؐDŽ㓪ݭߑ᭄ fseekˈᑊ⹂ֱ䆹ߑ᭄ϢᑧЁ݊ᅗߑ᭄Փ⫼ ㉏ԐѢߑ᭄ lseekˈ᠔ϡৠⱘᰃˈ䆹ߑ᭄Ёⱘ fp ᰃϔϾ᭛ӊᣛ䩜㗠ϡᰃ᭛ӊᦣ䗄ヺˈϨ䖨ಲ int fseek(FILE *fp, long offset, int origin) ᑧߑ᭄ޚ㒗д 8•4 ᷛ 㒗д 8•3 䆒䅵ᑊ㓪ݭߑ᭄_flushbufǃfflush ੠ fcloseDŽ ⷕⱘ䭓ᑺ੠ᠻ㸠䗳ᑺDŽ 㒗д 8•2 ⫼ᄫ↉ҷ᳓ᰒᓣⱘᣝԡ᪡԰ˈ䞡ݭ fopen ੠_fillbuf ߑ᭄DŽ↨䕗Ⳍᑨҷ ᮍᓣⱘݭ᪡԰DŽކᠻ㸠㓧 䆹㒧ᵘЁ flag 䚼ߚⱘ߱ؐ㸼ᯢˈᇚᇍ stdin ᠻ㸠䇏᪡԰ǃᇍ stdout ᠻ㸠ݭ᪡԰ǃᇍ stderr }; { 0, (char *) 0, (char *) 0, _WRITE, | _UNBUF, 2 } { 0, (char *) 0, (char *) 0, _WRITE, 1 }, { 0, (char *) 0, (char *) 0, _READ, 0 }, FILE _iob[OPEN_MAX] = { /* stdin, stdout, stderr */ stdout ੠ stderr ؐ˖ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! #define S_IFCHR 0020000 /* character special */ #define S_IFDIR 0040000 /* directory */ #define S_IFMT 0160000 /* type of file: */ 䳔㽕໘⧚᭛ӊ㉏ൟⱘ᳝݇䚼ߚ˖ st_mode 乍ࣙ৿њᦣ䗄᭛ӊⱘϔ㋏߫ᷛᖫˈ䖭ѯᷛᖫ೼ЁᅮНDŽ៥Ӏা ЁᅮНˈ⿟ᑣЁᖙ乏ࣙ৿ℸ᭛ӊDŽ 䆹㒧ᵘЁ໻䚼ߚⱘؐᏆ೼⊼䞞Ё䖯㸠њ㾷䞞DŽdev_t ੠ ino_t ㄝ㉏ൟ೼༈᭛ӊ }; time_t st_ctime; /* time originally created */ time_t st_mtime; /* time last modified */ time_t st_atime; /* time last accessed */ off_t st_size; /* file size in characters */ dev_t st_rdev; /* for special files */ short st_gid; /* owners group id */ short st_uid; /* owners user id */ short st_nlink; /* number of links to file */ short st_mode; /* mode bits */ ino_t st_ino; /* inode number */ dev_t st_dev; /* device of inode */ { struct stat /* inode information returned by stat */ ⱘ䖨ಲؐⱘ㒧ᵘDŽ䆹㒧ᵘⱘϔϾ݌ൟᔶᓣབϟ᠔⼎˖ 㒧ᵘ stbufDŽ༈᭛ӊЁࣙ৿њᦣ䗄 statܙ฿ᅗ⫼᭛ӊ name ⱘ i 㒧⚍ֵᙃ stat(name, &stbuf); int stat(char *, struct stat *); struct stat stbuf; char *name; བϟ᠔⼎˖ ㋏㒳䇗⫼ stat ҹ᭛ӊৡ԰Ўখ᭄ˈ䖨ಲ᭛ӊⱘ i 㒧⚍Ёⱘ᠔ֵ᳝ᙃ˗㢹ߎ䫭ˈ߭䖨ಲ•1DŽ void closedir(DIR *dfd); Dirent *readdir(DIR *dfd); DIR *opendir(char *dirname); } DIR; Dirent d; /* the directory entry */ int fd; /* file descriptor for the directory */ typedef struct { /* minimal DIR: no buffering, etc. */ } Dirent; char name[NAME_MAX+1]; /* name + '\0' terminator */ long ino; /* inode number */ typedef struct { /* portable directory entry */ /* system•dependent */ #define NAME_MAX 14 /* longest filename component; */ ᅗᇚ㹿 readdir ੠ closedir Փ⫼DŽ᠔᳝䖭ѯֵᙃᄬᬒ೼༈᭛ӊ dirent.h ЁDŽ ᅮDŽopendir 䖨ಲϔϾᣛ৥⿄Ў DIR ⱘ㒧ᵘⱘᣛ䩜ˈ䆹㒧ᵘϢ㒧ᵘ FILE ㉏Ԑˈއⱘؐ⬅㋏㒳 㒧ᵘ Dirent ࣙ৿ i 㒧⚍㓪ো੠᭛ӊৡDŽ᭛ӊৡⱘ᳔໻䭓ᑺ⬅ NAMZ_MAX 䆒ᅮˈNAME_MAX w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! Ў fsize ߑ᭄ᇍ↣ϾⳂᔩ䛑㽕䇗⫼ dirwalk ߑ᭄ˈ᠔ҹ䖭ϸϾߑ᭄ᰃⳌѦ䗦ᔦ䇗⫼ⱘDŽ ᠧᓔⳂᔩˈᕾ⦃䘡ग़݊Ёⱘ↣Ͼ᭛ӊˈᑊᇍ↣Ͼ᭛ӊ䇗⫼䆹ߑ᭄ˈ✊ৢ݇䯁Ⳃᔩ䖨ಲDŽ಴ܜ ߑ᭄ dirwalk ᰃϔϾ䗮⫼ⱘߑ᭄ˈᅗᇍⳂᔩЁⱘ↣Ͼ᭛ӊ䛑䇗⫼ߑ᭄ fcn ϔ⃵DŽᅗ佪 } printf("%8ld %s\n", stbuf.st_size, name); dirwalk(name, fsize); if ((stbuf.st_mode & S_IFMT) == S_IFDIR) } return; fprintf(stderr, "fsize: can't access %s\n", name); if (stat(name, &stbuf) == •1) { struct stat stbuf; { void fsize(char *name) /* fsize: print the name of file "name" */ void dirwalk(char *, void (*fcn)(char *)); int stat(char *, struct stat *); 㑻DŽܜⱘӬ 㑻ԢѢ==䖤ㅫヺܜS_IFDIR ᴹ߸ᅮ᭛ӊᰃϡᰃϔϾⳂᔩDŽᣀোᰃᖙ乏ⱘˈ಴Ў&䖤ㅫヺⱘӬ ߑ᭄໘⧚ᅗ᠔ࣙ৿ⱘ᠔᳝᭛ӊDŽ⊼ᛣབԩՓ⫼᭛ӊЁⱘᷛᖫৡ S_IFMT ੠ 䇗⫼ dirwalkܜߑ᭄ fsize ᠧॄ᭛ӊⱘ䭓ᑺDŽԚᰃˈབᵰℸ᭛ӊᰃϔϾⳂᔩˈ߭ fsize 佪 } return 0; fsize(*++argv); while (••argc > 0) else fsize("."); if (argc == 1) /* default: current directory */ { main(int argc, char **argv) /* print file name */ void fsize(char *) #include "dirent.h" #include /* structure returned by stat */ #include /* typedefs */ #include /* flags for read and write */ #include "syscalls.h" #include #include Џ⿟ᑣ main ໘⧚ੑҸ㸠খ᭄ˈᑊᇚ↣Ͼখ᭄Ӵ䗦㒭ߑ᭄ fsizeDŽ ⧚ⳂᔩЁⱘ᭛ӊDŽ⬅Ѣ䆹Ⳃᔩৃ㛑ࣙ৿ᄤⳂᔩˈ಴ℸ䆹䖛⿟ᰃ䗦ᔦⱘDŽ ᔩˈህᕜᆍᯧ㦋ᕫ䆹᭛ӊⱘ䭓ᑺˈᑊⳈ᥹䕧ߎDŽԚᰃˈབᵰ᭛ӊᰃϔϾⳂᔩˈ߭ᖙ乏䗤Ͼ໘ ϟ䴶៥Ӏᴹⴔ᠟㓪ݭ⿟ᑣ fsizeDŽབᵰ⬅ stat 䇗⫼㦋ᕫⱘ῵ᓣ䇈ᯢᶤ᭛ӊϡᰃϔϾⳂ /* ... */ #define S_IFREG 0010000 /* regular */ define S_IFBLK 0060000 /* block special */# w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! int fstat(int fd, struct stat *); ㉏ԐˈԚᅗҹ᭛ӊᦣ䗄ヺ԰Ўখ᭄˅ˈ✊ৢߚ䜡ϔϾⳂᔩ㒧ᵘˈᑊֱᄬֵᙃ˖ ᠧᓔⳂᔩˈ偠䆕ℸ᭛ӊᰃϔϾⳂᔩ˄䇗⫼㋏㒳䇗⫼ fstatˈᅗϢ statܜopendir ߑ᭄佪 ЁⱘⳂᔩֵᙃˈབϟ᠔⼎˖ ϞᦤկϔϾ opendirǃreaddir ੠ closedir ⱘ᳔ㅔऩ⠜ᴀDŽҹϟⱘߑ᭄䗖⫼Ѣ Version 7 ⱘџᚙህᰃ೼ᶤϾ݋ԧⱘ㋏㒳خࠄ⦄೼䖭ϔℹЎℶˈҷⷕϢⳂᔩⱘḐᓣ᮴݇DŽϟϔℹ㽕 ᖙ乏䏇䖛ᅗӀˈ৺߭ᇚӮᇐ㟈᮴䰤ᕾ⦃DŽ ⱘ᭛ӊˈ䆹ߑ᭄ᇚ䖨ಲ NULLDŽ↣ϾⳂᔩ䛑ࣙ৿㞾䑿“.ā੠⠊Ⳃᔩ“..āⱘ乍Ⳃˈ೼໘⧚ᯊ ↣⃵䇗⫼ readdir 䛑ᇚ䖨ಲϔϾᣛ䩜ˈᅗᣛ৥ϟϔϾ᭛ӊⱘֵᙃDŽབᵰⳂᔩЁᏆ≵᳝ᕙ໘⧚ } closedir(dfd); } } (*fcn)(name); sprintf(name, "%s/%s", dir, dp•>name); else { dir, dp•>name); fprintf(stderr, "dirwalk: name %s %s too long\n", if (strlen(dir)+strlen(dp•>name)+2 > sizeof(name)) continue; /* skip self and parent */ || strcmp(dp•>name, "..")) if (strcmp(dp•>name, ".") == 0 while ((dp = readdir(dfd)) != NULL) { } return; fprintf(stderr, "dirwalk: can't open %s\n", dir); if ((dfd = opendir(dir)) == NULL) { DIR *dfd; Dirent *dp; char name[MAX_PATH]; { void dirwalk(char *dir, void (*fcn)(char *)) /* dirwalk: apply fcn to all files in dir */ define MAX_PATH 1024# w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ϡ䳔㽕ࣙ৿ⳌᑨⱘໄᯢDŽ݊⃵ˈ᳝ৃ㛑ЎϢ㋏㒳Ⳍ݇ⱘᇍ䈵߯ᓎϔϾϢ㋏㒳᮴݇ⱘ᥹ষDŽᷛ ༈᭛ӊЁˈՓ⫼ᅗӀⱘ⿟ᑣা䳔㽕೼᭛ӊЁࣙ৿䖭ѯ༈᭛ӊेৃˈ㗠ޚᙃⱘ㸼⼎ҙߎ⦄೼ᷛ Ā㋏㒳⿟ᑣāˈᅗӀҙҙՓ⫼⬅᪡԰㋏㒳㓈ᡸⱘֵᙃDŽᇍѢ䖭ḋⱘ⿟ᑣˈᕜ䞡㽕ⱘϔ⚍ᰃˈֵ 䆌໮⿟ᑣᑊϡᰃˈܜሑㅵ fsize ⿟ᑣ䴲ᐌ⡍⅞ˈԚᰃᅗⱘ⹂䇈ᯢњϔѯ䞡㽕ⱘᗱᛇDŽ佪 } return NULL; } return &d; d.name[DIRSIZ] = '\0'; /* ensure termination */ strncpy(d.name, dirbuf.d_name, DIRSIZ); d.ino = dirbuf.d_ino; continue; if (dirbuf.d_ino == 0) /* slot not in use */ == sizeof(dirbuf)) { while (read(dp•>fd, (char *) &dirbuf, sizeof(dirbuf)) static Dirent d; /* return: portable structure */ struct direct dirbuf; /* local directory structure */ { Dirent *readdir(DIR *dp) /* readdir: read directory entries in sequence */ #include /* local directory structure */ readdir ߑ᭄ᇚ㽚Ⲫࠡϔ⃵䇗⫼㦋ᕫⱘֵᙃDŽ ੠Ⳃᔩৡᬒ೼ϔϾ static ㉏ൟⱘ㒧ᵘЁˈᑊ㒭⫼᠋䖨ಲϔϾᣛ৥ℸ㒧ᵘⱘᣛ䩜DŽ↣⃵䇗⫼ Փ⫼˄಴Ўߴ䰸њϔϾ᭛ӊ˅ˈ߭ᅗⱘ i 㒧⚍㓪োЎ 0ˈᑊ䏇䖛䆹ԡ㕂DŽ৺߭ˈᇚ i 㒧⚍㓪ো ᳔ৢˈߑ᭄ readdir Փ⫼ read ㋏㒳䇗⫼䇏প↣ϾⳂᔩ乍DŽབᵰᶤϾⳂᔩԡ㕂ᔧࠡ≵᳝ } } free(dp); close(dp•>fd); if (dp) { { void closedir(DIR *dp) /* closedir: close directory opened by opendir */ closedir ߑ᭄⫼Ѣ݇䯁Ⳃᔩ᭛ӊᑊ䞞ᬒݙᄬぎ䯈˖ } return dp; dp•>fd = fd; return NULL; || (dp = (DIR *) malloc(sizeof(DIR))) == NULL) || (stbuf.st_mode & S_IFMT) != S_IFDIR || fstat(fd, &stbuf) == •1 if ((fd = open(dirname, O_RDONLY, 0)) == •1 DIR *dp; struct stat stbuf; int fd; { DIR *opendir(char *dirname) opendir: open a directory for readdir calls */ */ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ぎ䯆ഫࣙ৿ϔϾᣛ৥䫒㸼ЁϟϔϾഫⱘᣛ䩜ǃϔϾഫ໻ᇣⱘ䆄ᔩ੠ϔϾᣛ৥ぎ䯆ぎ䯈ᴀ ៪ long ㉏ൟDŽ ೼ᶤѯᴎ఼Ёˈ᳔ফ䰤ⱘ㉏ൟᰃ double ㉏ൟ˗㗠೼঺໪ϔѯᴎ఼Ёˈ᳔ফ䰤ⱘ㉏ൟᰃ int ೼ᶤϾ⡍ᅮⱘഄഔЁˈ߭݊ᅗ᠔᳝ⱘ㉏ൟгৃҹᄬᬒ೼ℸഄഔЁDŽټབᵰ᳔ফ䰤ⱘ㉏ൟৃҹᄬ ֱᄬⱘᇍ䈵ⱘᇍ唤㽕∖DŽ㱑✊ᴎ఼㉏ൟ৘ᓖˈԚᰃˈ↣Ͼ⡍ᅮⱘᴎ఼䛑᳝ϔϾ᳔ফ䰤ⱘ㉏ൟ˖ ぎ䯈⒵䎇ᇚ㽕ټ៥Ӏ೼㄀ 5 ゴЁ᳒ᦤߎњ䖭ḋⱘ䯂乬ˈे⹂ֱ⬅ malloc ߑ᭄䖨ಲⱘᄬ ৺ぎ䯆DŽ ໮ⱘ⹢⠛DŽ಴Ўぎ䯆ഫ䫒㸼ᰃҹഄഔⱘ䗦๲乎ᑣ䫒᥹೼ϔ䍋ⱘˈ᠔ҹᕜᆍᯧ߸ᮁⳌ䚏ⱘഫᰃ ぎ䯈ϡӮ᳝໾ټᬒഫⳌ䚏ⱘӏϔ䖍ᰃϔϾぎ䯆ഫˈ߭ᇚ䖭ϸϾഫড়៤ϔϾ᳈໻ⱘഫˈ䖭ḋᄬ ㋶ぎ䯆ഫ䫒㸼ˈҹᡒࠄৃҹᦦܹ㹿䞞ᬒഫⱘড়䗖ԡ㕂DŽབᵰϢ㹿䞞᧰ܜ䞞ᬒ䖛⿟гᰃ佪 ϔϾ䎇໳໻ⱘഫˈ߭৥᪡԰㋏㒳⬇䇋ϔϾ໻ഫᑊࡴܹࠄぎ䯆ഫ䫒㸼ЁDŽ ߭ᇚᅗߚ៤ϸ䚼ߚ˖໻ᇣড়䗖ⱘഫ䖨ಲ㒭⫼᠋ˈ࠽ϟⱘ䚼ߚ⬭೼ぎ䯆ഫ䫒㸼ЁDŽབᵰᡒϡࠄ ഫDŽབᵰ䆹ഫᙄདϢ䇋∖ⱘ໻ᇣⳌヺড়ˈ߭ᇚᅗҢ䫒㸼Ё⿏䍄ᑊ䖨ಲ㒭⫼᠋DŽབᵰ䆹ഫ໾໻ˈ ЎĀ佪⃵䗖ᑨā˄first fit˅˗ϢПⳌᇍⱘㅫ⊩ᰃĀ᳔Շ䗖ᑨā˄best fit˅ˈᅗᇏᡒ⒵䎇ᴵӊⱘ᳔ᇣ ᔧ᳝⬇䇋䇋∖ᯊˈmalloc ᇚᠿᦣぎ䯆ഫ䫒㸼ˈⳈࠄᡒࠄϔϾ䎇໳໻ⱘഫЎℶDŽ䆹ㅫ⊩⿄ ೒ 8•1 㾕೒ 8•1˅DŽ ഄഔⱘछᑣ㒘㒛ˈ᳔ৢϔഫ˄᳔催ഄഔ˅ᣛ৥㄀ϔഫ˄খټぎ䯈ⱘᣛ䩜DŽ䖭ѯഫᣝ✻ᄬټᄬ ҹぎ䯆ഫ䫒㸼ⱘᮍᓣ㒘㒛ˈ↣Ͼഫࣙ৿ϔϾ䭓ᑺǃϔϾᣛ৥ϟϔഫⱘᣛ䩜ҹঞϔϾᣛ৥㞾䑿 ぎ䯈ټ䗮䖛݊ᅗᮍᓣ⬇䇋ぎ䯈˅ˈ᠔ҹˈmalloc ㅵ⧚ⱘぎ䯈ϡϔᅮᰃ䖲㓁ⱘDŽ䖭ḋˈぎ䯆ᄬ ৥᪡԰㋏㒳⬇䇋ぎ䯈DŽ಴Ў⿟ᑣЁⱘᶤѯഄᮍৃ㛑ϡ䗮䖛 malloc 䇗⫼⬇䇋ぎ䯈˄гህᰃ䇈ˈ ぎ䯈ˈ㗠ᰃ೼䳔㽕ᯊټmalloc ᑊϡᰃҢϔϾ೼㓪䆥ᯊህ⹂ᅮⱘ೎ᅮ໻ᇣⱘ᭄㒘Ёߚ䜡ᄬ 乬ˈৠᯊгሩ⼎њ㒧ᵘǃ㘨ড়੠ typedef ⱘᅲ䰙ᑨ⫼DŽ ぎ䯈DŽ䖭ѯ⿟ᑣ䇈ᯢњ䗮䖛ϔ⾡Ϣ㋏㒳᮴݇ⱘᮍᓣ㓪ݭϢ㋏㒳᳝݇ⱘҷⷕᯊᑨ㗗㰥ⱘ䯂ټᄬ 䰤ࠊˈৃҹҹӏᛣ⃵ᑣ䇗⫼ malloc ੠ freeDŽmalloc ೼ᖙ㽕ᯊ䇗⫼᪡԰㋏㒳ҹ㦋প᳈໮ⱘ ߚ䜡⿟ᑣDŽᴀ㡖ᇚ㽕㓪ݭⱘ⠜ᴀ≵᳝ټ៥Ӏ೼㄀ 5 ゴ㒭ߎњϔϾࡳ㛑᳝䰤ⱘ䴶৥ᷜⱘᄬ ߚ䜡⿟ᑣټ8.7. ᅲ՟——ᄬ 㒗д 8•5 ׂᬍ fsize ⿟ᑣˈᠧॄ i 㒧⚍乍Ёࣙ৿ⱘ݊ᅗֵᙃDŽ ᑧЁⱘߑ᭄ህᰃᕜདⱘ՟ᄤDŽޚ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! void *malloc(unsigned nbytes) /* malloc: general•purpose storage allocator */ static Header *freep = NULL; /* start of free list */ static Header base; /* empty list to get started */ DŽܗぎ䯈ˈे↨ᣛ৥༈䚼ⱘᣛ䩜໻ϔϾऩټ䯆ᄬ ϟˈ䖨ಲ㒭⫼᠋ⱘᣛ䩜䛑ᣛ৥ഫݙⱘぎމ߱ྟഫⱘ༈䚼া䳔㽕ׂᬍ size ᄫ↉ेৃDŽ೼ӏԩᚙ ᓔྟDŽ䆹ㄪ⬹ৃҹֱ䆕䫒㸼ᰃഛࣔⱘDŽབᵰᡒࠄⱘഫ໾໻ˈ߭ᇚ݊ሒ䚼䖨ಲ㒭⫼᠋ˈ䖭ḋˈ ϟˈᔧ䇋∖ぎ䯆ぎ䯈ᯊˈ䛑ᇚ᧰㋶ぎ䯆ഫ䫒㸼DŽ᧰㋶ҢϞϔ⃵ᡒࠄぎ䯆ഫⱘഄᮍ˄freep˅ މᇚ߯ᓎϔϾ䗔࣪ⱘぎ䯆ഫ䫒㸼ˈᅗাࣙ৿ϔϾ໻ᇣЎ 0 ⱘഫˈϨ䆹ഫᣛ৥ᅗ㞾ᏅDŽӏԩᚙ ব䞣 base 㸼⼎ぎ䯆ഫ䫒㸼ⱘ༈䚼DŽ㄀ϔ⃵䇗⫼ malloc ߑ᭄ᯊˈfreep Ў NULLˈ㋏㒳 ৃ㛑䗮䖛ᣛ䩜ㅫᴃ䖤ㅫ䅵ㅫ݊໻ᇣDŽ ݊Ёⱘ size ᄫ↉ᰃᖙ䳔ⱘˈ಴Ў⬅ malloc ߑ᭄᥻ࠊⱘഫϡϔᅮᰃ䖲㓁ⱘˈ䖭ḋህϡ ೒ 8•2 malloc 䖨ಲⱘഫ 䫒㸼DŽ೒ 8•2 㸼⼎⬅ malloc 䖨ಲⱘഫDŽ ぎ䯈П໪ݭ᭄ܹ᥂ˈ߭ৃ㛑Ӯ⸈ണഫټぎ䯈䖯㸠ӏԩ᪡԰ˈԚᰃˈབᵰ೼ߚ䜡ⱘᄬټᕫⱘᄬ 䚼ⱘ size ᄫ↉ЁDŽmalloc ߑ᭄䖨ಲⱘᣛᓩᇚᣛ৥ぎ䯆ぎ䯈ˈ㗠ϡᰃഫⱘ༈䚼DŽ⫼᠋ৃᇍ㦋 Ѣ༈䚼ᴀ䑿DŽᅲ䰙ߚ䜡ⱘഫⱘ໻ᇣᇚ㹿䆄ᔩ೼༈⫼ˈܗDŽᅲ䰙ߚ䜡ⱘഫᇚ໮ࣙ৿ϔϾऩס᭄ ೼ malloc ߑ᭄Ёˈ䇋∖ⱘ䭓ᑺ˄ҹᄫヺЎऩԡ˅ᇚ㹿㟡ܹˈҹֱ䆕ᅗᰃ༈䚼໻ᇣⱘᭈ 唤㽕∖DŽ ϟ⒵䎇ᇍމ೼䆹㘨ড়ЁˈAlign ᄫ↉∌䖰ϡӮ㹿Փ⫼ˈᅗҙҙ⫼Ѣᔎࠊ↣Ͼ༈䚼೼᳔ണⱘᚙ typedef union header Header; }; Align x; /* force alignment of blocks */ } s; unsigned size; /* size of this block */ union header *ptr; /* next block if on free list */ struct { union header { /* block header */ typedef long Align; /* for alignment to long boundary */ Ў᳔ফ䰤ⱘ㉏ൟ˖ ᅮ long ㉏ൟ؛ⱘ༈䚼㒧ᵘҹঞϔϾᇍ唤㽕∖᳔ফ䰤ⱘ㉏ൟⱘᅲ՟ˈ೼ϟ䴶䖭↉⿟ᑣЁˈ៥Ӏ Ϩ༈䚼Ꮖℷ⹂ഄᇍ唤DŽ䖭ᰃ䗮䖛ϔϾ㘨ড়ᅲ⦄ⱘˈ䆹㘨ড়ࣙ৿᠔䳔ˈס乏ᰃ༈䚼໻ᇣⱘᭈ᭄ 䑿ⱘᣛ䩜DŽԡѢഫᓔྟ໘ⱘ᥻ࠊֵᙃ⿄ЎĀ༈䚼ĀDŽЎњㅔ࣪ഫⱘᇍ唤ˈ᠔᳝ഫⱘ໻ᇣ䛑ᖙ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! up = (Header *) cp; return NULL; if (cp == (char *) •1) /* no space at all */ cp = sbrk(nu * sizeof(Header)); nu = NALLOC; if (nu < NALLOC) Header *up; char *cp, *sbrk(int); { static Header *morecore(unsigned nu) /* morecore: ask system for more memory */ #define NALLOC 1024 /* minimum #units to request */ ಴ℸˈা᳝೼ϔ㠀ᣛ䩜䯈ⱘ↨䕗᪡԰᳝ᛣНⱘᴎ఼Ϟˈ䆹⠜ᴀⱘ malloc ߑ᭄ᠡ㛑໳⿏ỡDŽ 䆌ᣛ৥ৠϔϾ᭄㒘ⱘᣛ䩜䯈ⱘ↨䕗DŽܕᑊ≵ֱ᳝䆕䖭ϔ⚍ˈᅗাޚ㸠᳝ᛣНⱘ↨䕗DŽANSI ᷛ ᅮˈ⬅ sbrk 䇗⫼䖨ಲⱘᣛ৥ϡৠഫⱘ໮Ͼᣛ䩜П䯈ৃҹ䖯؛ⱘϡৠⱘᕅડDŽԚᰃˈ䖭䞠ҡ✊ ㉏ൟˈҹ֓Ϣ䖨ಲؐ䖯㸠↨䕗DŽ㗠Ϩˈᔎࠊ㉏ൟ䕀ᤶՓᕫ䆹ߑ᭄ϡӮফϡৠᴎ఼Ёᣛ䩜㸼⼎ 䯆ぎ䯈ˈሑㅵ䖨ಲ NULL ৃ㛑᳈དϔѯˈԚ sbrk 䇗⫼䖨ಲ•1DŽᖙ乏ᇚ•1 ᔎࠊ䕀ᤶЎ char * ぎ䯈DŽབᵰ≵᳝ぎټUNIX ㋏㒳䇗⫼ sbrk(n)䖨ಲϔϾᣛ䩜ˈ䆹ᣛ䩜ᣛ৥ n Ͼᄫ㡖ⱘᄬ ぎ䯈ᦦܹࠄぎ䯆ऎඳЁDŽټ ᥂䳔㽕ߚ៤䕗ᇣⱘഫDŽ೼䆒㕂ᅠ size ᄫ↉Пৢˈmorecore ߑ᭄䇗⫼ free ߑ᭄ᡞ໮ԭⱘᄬ DŽ䖭Ͼ䕗໻ⱘഫᇚḍܗᠻ㸠䆹᪡԰ˈ෎Ѣ䖭Ͼ㗗㰥ˈmorecore ߑ᭄䇋∖㟇ᇥ NALLOC Ͼऩ ぎ䯈ᰃϔϾᓔ䫔ᕜ໻ⱘ᪡԰ˈ಴ℸˈ៥ӀϡᏠᳯ↣⃵䇗⫼ malloc ߑ᭄ᯊ䛑ټ৥㋏㒳䇋∖ᄬ ぎ䯈ˈ݊ᅲ⦄㒚㡖಴㋏㒳ⱘϡৠ㗠ϡৠDŽ಴Ўټߑ᭄ morecore ⫼Ѣ৥᪡԰㋏㒳䇋∖ᄬ } } return NULL; /* none left */ if ((p = morecore(nunits)) == NULL) if (p == freep) /* wrapped around free list */ } return (void *)(p+1); freep = prevp; } p•>s.size = nunits; p += p•>s.size; p•>s.size •= nunits; else { /* allocate tail end */ prevp•>s.ptr = p•>s.ptr; if (p•>s.size == nunits) /* exactly */ if (p•>s.size >= nunits) { /* big enough */ for (p = prevp•>s.ptr; ; prevp = p, p = p•>s.ptr) { } base.s.size = 0; base.s.ptr = freeptr = prevptr = &base; if ((prevp = freep) == NULL) { /* no free list yet */ nunits = (nbytes+sizeof(Header)•1)/sizeof(header) + 1; unsigned nunits; Header *moreroce(unsigned); Header *p, *prevp; } w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䫒㸼Ё⏏ࡴϔϾ䴭ᗕ៪໪䚼᭄㒘DŽ ⬅ malloc ੠ free 㓈ᡸⱘぎ䯆ഫ䫒㸼ЁDŽ䗮䖛Փ⫼ bfreeˈ⫼᠋ৃҹ೼ӏᛣᯊࠏ৥ぎ䯆ഫ 㒗д 8•8 㓪ݭߑ᭄ bfree(p, n)ˈ䞞ᬒϔϾࣙ৿ n Ͼᄫヺⱘӏᛣഫ pˈᑊᇚᅗᬒܹ ߭䅸Ў㹿䞞ᬒⱘഫࣙ৿ϔϾ᳝ᬜⱘ䭓ᑺᄫ↉DŽᬍ䖯䖭ѯߑ᭄ˈՓᅗӀ݋᳝䫭䇃Ẕᶹⱘࡳ㛑DŽ ぎ䯈ⱘ䇋∖ᯊˈᑊϡẔᶹ䇋∖䭓ᑺⱘড়⧚ᗻ˗㗠 freeټ㒗д 8•7 malloc ᥹ᬊᇍᄬ ߑ᭄DŽ ぎ䯈䛑㹿߱ྟ࣪Ў 0DŽ䗮䖛䇗⫼៪ׂᬍ malloc ߑ᭄ᴹᅲ⦄ callocټⱘᇍ䈵ˈϨ᠔᳝ߚ䜡ⱘᄬ ᑧߑ᭄ calloc(n, size)䖨ಲϔϾᣛ䩜ˈᅗᣛ৥ n Ͼ䭓ᑺЎ sizeޚ㒗д 8•6 ᷛ DŽމ䖭⾡䗮⫼ᮍ⊩г䗖⫼Ѣ݊ᅗᚙ ߚ䜡ˈԚᰃˈټ㟇ৃҹ໘⧚䆒䅵ϡ໳དⱘ㋏㒳᥹ষ䯂乬DŽ㱑✊䖭䞠᠔䆆ⱘݙᆍা⍝ঞࠄᄬ⫮ خᅮ sbrk 䖨ಲⱘᰃড়䗖ⱘᣛ䩜˅䯂乬DŽ㉏ൟⱘᔎࠊ䕀ᤶՓᕫᣛ䩜ⱘ䕀ᤶᰃᰒᓣ䖯㸠ⱘˈ䖭ḋ ؛˄њഄഔⱘᇍ唤އⳌ݇ⱘ䚼ߚˈᑊᇚ䖭䚼ߚ⿟ᑣ᥻ࠊࠄ᳔ᇥ䞣DŽtypedef ੠ union ⱘՓ⫼㾷 ߚ䜡Ңᴀ䋼ϞᰃϢᴎ఼Ⳍ݇ⱘˈԚᰃˈҹϞⱘҷⷕ䇈ᯢњབԩ᥻ࠊϢ݋ԧᴎ఼ټ㱑✊ᄬ } freep = p; p•>s.ptr = bp; } else p•>s.ptr = bp•>s.ptr; p•>s.size += bp•>s.size; if (p + p•>size == bp) { /* join to lower nbr */ bp•>s.ptr = p•>s.ptr; } else bp•>s.ptr = p•>s.ptr•>s.ptr; bp•>s.size += p•>s.ptr•>s.size; if (bp + bp•>size == p•>s.ptr) { /* join to upper nbr */ break; /* freed block at start or end of arena */ if (p >= p•>s.ptr && (bp > p || bp < p•>s.ptr)) for (p = freep; !(bp > p && bp < p•>s.ptr); p = p•>s.ptr) bp = (Header *)ap • 1; /* point to block header */ Header *bp, *p; { void free(void *ap) /* free: put block ap in free list */ ᕜㅔऩˈা䳔㽕䆒㕂ᣛ䩜ᣛ৥ℷ⹂ⱘԡ㕂ˈᑊ䆒㕂ℷ⹂ⱘഫ໻ᇣህৃҹњDŽ ϟˈབᵰ㹿䞞ᬒⱘഫϢ঺ϔぎ䯆ഫⳌ䚏ˈ߭ᇚ䖭ϸϾഫড়ᑊ䍋ᴹDŽড়ᑊϸϾഫⱘ᪡԰މ⾡ᚙ ᡒৃҹᦦܹぎ䯆ഫⱘഄᮍDŽ䆹ԡ㕂ৃ㛑೼ϸϾぎ䯆ഫП䯈ˈгৃ㛑೼䫒㸼ⱘ᳿ሒDŽ೼ӏԩϔ ៥Ӏ᳔ৢᴹⳟϔϟ free ߑ᭄DŽᅗҢ freep ᣛ৥ⱘഄഔᓔྟˈ䗤Ͼᠿᦣぎ䯆ഫ䫒㸼ˈᇏ } return freep; free((void *)(up+1)); up•>s.size = nu; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᄫ䴶ؐЁDŽ ⊼䞞ҹᄫヺ/*ᓔྟˈҹ*/㒧ᴳDŽ⊼䞞ϡ㛑໳ጠ༫ˈгϡ㛑໳ߎ⦄೼ᄫヺІᄫ䴶ؐ៪ᄫヺ A.2.2 ⊼䞞 Ёৃ㛑ᵘ៤䆄োⱘ᳔䭓ⱘᄫヺІDŽ བᵰࠄᶤϔᄫヺЎℶⱘ䕧ܹ⌕㹿ߚ䱨៤㢹ᑆ䆄োˈ䙷МˈϟϔϾ䆄োህᰃৢ㓁ᄫヺᑣ߫ 䱨䆄োˈ಴ℸᇚ㹿ᗑ⬹DŽⳌ䚏ⱘᷛ䆚ヺǃ݇䬂ᄫ੠ᐌ䞣П䯈䳔㽕⫼ぎⱑヺᴹߚ䱨DŽ ぎḐˈ῾৥ࠊ㸼ヺ੠㒉৥ࠊ㸼ヺǃᤶ㸠ヺˈᤶ义ヺ੠⊼䞞˄㒳⿄ぎⱑヺ˅೼⿟ᑣЁҙ⫼ᴹߚ C 䇁㿔Ё᳝݅ 6 ㉏䆄ো˖ᷛ䆚ヺǃ݇䬂ᄫǃᐌ䞣ǃᄫヺІᄫ䴶ؐǃ䖤ㅫヺ੠݊ᅗߚ䱨ヺDŽ A.2.1 䆄ো ⿟ᑣ㹿ᔦ㑺៤ϔϾ䆄োᑣ߫DŽ ヺ#ᓔ༈ⱘ㸠ЁⱘᣛҸˈᑊ䖯㸠ᅣᅮН੠ᅣᠽሩDŽ೼乘໘⧚˄ᇚ೼ A.12 㡖Ёҟ㒡˅ᅠ៤ৢˈ 䰊↉ᅠ៤ˈ䖭䚼ߚݙᆍᇚ೼ A.12 㡖Ёҟ㒡DŽ㗏䆥ⱘ㄀ϔ䰊↉ᅠ៤Ԣ㑻ⱘ䆡⊩䕀ᤶˈᠻ㸠ҹᄫ translation unit˅㒘៤DŽ⿟ᑣⱘ㗏䆥ߚ޴Ͼ˄ܗ೼᭛ӊЁⱘϔϾ៪໮Ͼ㗏䆥ऩټ⿟ᑣ⬅ᄬ A.2 䆡⊩㾘߭ 㓪䆥఼ᬃᣕⱘ䇁㿔П䯈ⱘᏂ߿DŽ C 䇁㿔Ϣᴀк㄀ 1 ⠜ᅮНⱘ C 䇁㿔៪݊ᅗ ޚᴀ᠟ݠЁˈ䇈ᯢ䚼ߚⱘ᭛ᄫᣛߎњ ANSI ᷛ 乘໘⧚఼ⱘᅮНг≵᳝ᔶᓣ࣪DŽ ㋴ⱘੑৡৃ㛑᳝ѯϡৠˈ䆡⊩䆄ো੠ܗᰃⳌৠⱘˈԚᰃˈ݊Ёᇥ䞣ޚᴀ᠟ݠ㒭ߎⱘ䇁⊩Ϣᷛ ෎ᴀ㉏ԐˈϢᴀкⱘ㄀ 1 ⠜г㉏ԐˈԚᰃᇍ㒚㡖ⱘ㒘㒛᳝ѯϡৠDŽޚ䆹᠟ݠⱘ㒘㒛Ϣᷛ ᏆDŽ ⱘϔϾ㾷䞞㗠ޚᴀ䑿ˈ㗠ҙҙাᰃᇍᷛޚݠ԰Ў C 䇁㿔ⱘᣛफҟ㒡㒭䇏㗙ˈԚᅗ↩コϡᰃᷛ ⹂ഄᇚ䆹᠟ޚ∖C ⿟ᑣ䆒䅵䇁㿔ˈX3.159•1989āDŽሑㅵ៥ӀᏆሑ᳔໻ࡾ࡯ˈ࡯——ޚᙃ㋏㒳ᷛ োЎĀ㕢೑೑ᆊֵޚᴀ᠟ݠᦣ䗄ⱘ C 䇁㿔ᰃ 1988 ᑈ 10 ᳜ 31 ᮹ᦤѸ㒭 ANSI ⱘ㤝Ḝˈᡍ A.1 ᓩ㿔 䰘ᔩAখ㗗᠟ݠ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᳝ৢ㓔Ϩᰃक䖯ࠊ㸼⼎ˈ߭݊㉏ൟᕜৃ㛑ᰃ intǃlong int ៪ unsigned long intDŽབ ᭈൟᐌ䞣ⱘ㉏ൟৠᅗⱘᔶᓣǃؐ੠ৢ㓔᳝݇˄᳝݇㉏ൟⱘ䅼䆎ˈখ㾕 A.4 㡖˅DŽབᵰᅗ≵ ߭㸼⼎ᅗᰃϔϾ䭓ᭈൟ᭄˗㢹ҹᄫ↡ UL Ўৢ㓔ˈ߭㸼⼎ᅗᰃϔϾ᮴ヺো䭓ᭈൟ᭄DŽ ᭈൟᐌ䞣㢹ҹᄫ↡ u ៪ U Ўৢ㓔ˈ߭㸼⼎ᅗᰃϔϾ᮴ヺো᭄˗㢹ҹᄫ↡ l ៪ L Ўৢ㓔ˈ a˄៪ A˅ࠄ f˄៪ F˅ⱘᄫ↡ˈᅗӀߚ߿㸼⼎᭄ؐ 10 ࠄ 15DŽ 䖯ࠊᐌ䞣ϡࣙᣀ᭄ᄫ 8 ੠ 9ˈҹ 0x ੠ 0X ᓔ༈ⱘ᭄ᄫᑣ߫㸼⼎क݁䖯ࠊ᭄ˈक݁䖯ࠊ᭄ࣙ৿Ң ᭈൟᐌ䞣⬅ϔІ᭄ᄫ㒘៤DŽབᵰᅗҹ᭄ᄫ 0 ᓔ༈ˈ߭Ўܿ䖯ࠊ᭄ˈ৺߭Ўक䖯ࠊ᭄DŽܿ 1. ჼ໸Ш२ ਛࡨШ२ ׼ԤШ२ ᆓ׸Ш२ ჼ໸Ш२ ᐌ䞣˖ ᐌ䞣᳝໮⾡㉏ൟDŽ↣⾡㉏ൟⱘᐌ䞣䛑᳝ϔϾ᭄᥂㉏ൟDŽ෎ᴀ᭄᥂㉏ൟᇚ೼ A.4.2 㡖䅼䆎DŽ A.2.5 ᐌ䞣 ྸ࠼Ϣಾॴd ēຣၽڶߏᆓӬҶร΄ರဈڑ࿫ဈĢentry ႛ࠼΄ͬঝนַڜ΄໭႙ަԅēຣྸ܊͑ ಾԛ 1 ߏᆓ constcsigned ۤ volatile ಾ ANSI γᅹᄯ໭႙ަԅĢenum ۤ voidڑ˖䇈ᯢ ᶤѯᅲ⦄䖬ᡞ fortran ੠Ϩ asm ֱ⬭Ў݇䬂ᄫDŽ do if static while default goto sizeof volatile continue for signed void const float short unsigned char extern return union case enum register typedef break else long switch auto double int struct ϟ߫ᷛ䆚ヺ㹿ֱ⬭԰Ў݇䬂ᄫˈϨϡ㛑⫼Ѣ݊ᅗ⫼䗨˖ A.2.4 ݇䬂ᅛ ϔѯˈᅲ⦄ৃ㛑া䅸Ў䖭ѯᷛ䆚ヺⱘࠡ 6 Ͼᄫヺᰃ᳝ᬜⱘˈ㗠Ϩ᳝ৃ㛑ᗑ⬹໻ᇣݭⱘϡৠDŽ ৡ੠݊ᅗ᠔᳝≵᳝໪䚼䖲᥹˄খ㾕 A.11.2 㡖˅ⱘৡᄫDŽᏺ᳝໪䚼䖲᥹ⱘᷛ䆚ヺⱘ䰤ࠊ᳈ϹḐ 31 Ͼᄫ↡ᰃ᳝ᬜⱘˈ೼ᶤѯᅲ⦄Ёˈ᳝ᬜⱘᄫヺ᭄ৃ㛑᳈໮DŽݙ䚼ᷛ䆚ヺࣙᣀ乘໘⧚఼ⱘᅣ ↡DŽ໻ݭᄫ↡੠ᇣݭᄫ↡ᰃϡৠⱘDŽᷛ䆚ヺৃҹЎӏᛣ䭓ᑺDŽᇍѢݙ䚼ᷛ䆚ヺᴹ䇈ˈ㟇ᇥࠡ ᷛ䆚ヺᰃ⬅ᄫ↡੠᭄ᄫᵘ៤ⱘᑣ߫DŽ㄀ϔϾᄫヺᖙ乏ᰃᄫ↡ˈϟߦ㒓“_āг㹿ⳟ៤ᰃᄫ A.2.3 ᷛ䆚ヺ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! l ៪ L ৢ㓔㸼ᯢᅗᰃ long double ㉏ൟ˗≵᳝ৢ㓔߭㸼ᯢᰃ double ㉏ൟDŽ ᭄䚼ߚ˄Ԛϡ㛑ϸ㗙䛑≵᳝˅DŽ⍂⚍ᐌ䞣ⱘ㉏ൟ⬅ৢ㓔⹂ᅮˈF ៪ f ৢ㓔㸼⼎ᅗᰃ float ㉏ൟ˗ ߫㒘៤DŽৃҹ≵᳝ᭈ᭄䚼ߚ៪ᇣ᭄䚼ߚ˄Ԛϡ㛑ϸ㗙䛑≵᳝˅ˈ䖬ৃҹ≵᳝ᇣ᭄⚍៪㗙 e ੠ᣛ ᭄੠ϔϾৃ䗝ⱘ㸼⼎㉏ൟⱘৢ㓔˄े fǃFǃl ៪ L Пϔ˅㒘៤DŽᭈ᭄੠ᇣ᭄䚼ߚഛ⬅᭄ᄫᑣ ⍂⚍ᐌ䞣⬅ᭈ᭄䚼ߚǃᇣ᭄⚍ǃᇣ᭄䚼ߚǃϔϾ e ៪ EǃϔϾৃ䗝ⱘᏺヺোᭈൟ㉏ൟⱘᣛ 3. ׼ԤШ२ ಾนॴζವཐᄽԅဴཝd ࢶྻဈ char फ໸ࠩ໻Ω৲ē႙ަ wchar_t ԅᅖྑંԅނ๦૘ൎဈԅᆓ׸ۤڳඹШெࣣຏēਥ 䇈ᯢ˖੶໔ᅧ࿌༝ॹಾ໭႙ަԅēඋιಾಥঢࠩᄥᆓ׸ԅζವdࣴႺᆓ׸ྙಾ໭႙ަԅd ߭㒧ᵰᰃ᳾ᅮНⱘDŽ 䞣ৃҹՓ⫼ܿ䖯ࠊ៪क݁䖯ࠊ䕀Нᄫヺᑣ߫˗Ԛᰃˈབᵰؐ䍙䖛 wchar_t ৃҹ㸼⼎ⱘ㣗ೈˈ ༈᭛ӊЁDŽϢ䗮ᐌⱘᄫヺᐌ䞣ϔḋˈᆑᄫヺᐌޚ䖭ᰃϔ⾡ᭈൟ㉏ൟˈᅮН೼ᷛ ⱘᐌ䞣㽕ҹϔϾࠡᇐヺ L ᓔ༈˄՟བ L'x'DŽ˅ˈ⿄Ўᆑᄫヺᐌ䞣DŽ䖭⾡ᐌ䞣ⱘ㉏ൟЎ wchar_tDŽ ೼ C 䇁㿔ⱘᶤѯᅲ⦄Ёˈ䖬᳝ϔϾᠽሩⱘᄫヺ䲚ˈᅗϡ㛑⫼ char ㉏ൟ㸼⼎DŽᠽሩ䲚Ё ߭݊㸠Ўᰃ᳾ᅮНⱘDŽ ᅗ㹿ᔎࠊ䕀ᤶЎ char ㉏ൟϔḋDŽབᵰ\ৢ䴶㋻䎳ⱘᄫヺϡ೼ҹϞᣛᅮⱘᄫヺЁˈڣሩˈህད ᰃᏺヺোⱘˈ߭ᇚᇍᄫヺؐ䖯㸠ヺোᠽخࠊ៪क݁䖯ࠊ䕀Нᄫヺˈབᵰᅲ⦄Ёᇚ㉏ൟ char ⳟ ؐDŽ᭄ᄫⱘϾ᭄≵᳝䰤ࠊˈԚབᵰᄫヺؐ䍙䖛᳔໻ⱘᄫヺؐˈ䆹㸠Ўᰃ᳾ᅮНⱘDŽᇍѢܿ䖯 ߫\xhh Ёˈড᭰ᴴৢ䴶㋻䎳 x ҹঞक݁䖯ࠊ᭄ᄫˈ䖭ѯक݁䖯ࠊ᭄⫼ᴹᣛᅮ᠔ᳳᳯⱘᄫヺⱘ ᅮ᠔ᳳᳯⱘᄫヺⱘؐDŽ\0˄݊ৢ≵᭄᳝ᄫ˅֓ᰃϔϾᐌ㾕ⱘ՟ᄤˈᅗ㸼⼎ᄫヺ NULDŽ䕀Нᑣ 䕀Нᑣ߫\ooo ⬅ড᭰ᴴৢ䎳 1 Ͼǃ2 Ͼ៪ 3 Ͼܿ䖯ࠊ᭄ᄫ㒘៤ˈ䖭ѯܿ䖯ࠊ᭄ᄫ⫼ᴹᣛ ડ䪗ヺ BEL \a ᤶ义ヺ FF \f क݁䖯ࠊ᭄ hh \xhh ಲ䔺ヺ CR \r ܿ䖯ࠊ᭄ ooo \ooo ಲ䗔ヺ BS \b ঠᓩো "\" 㒉৥ࠊ㸼ヺ VT \v ऩᓩো '\' ῾৥ࠊ㸼ヺ HT \t 䯂ো ?\? ᤶ㸠ヺ NL(LF) \n ড᭰ᴴ \\\ ᄫヺ˖ ᄫヺᐌ䞣ϡࣙᣀᄫヺ'੠ᤶ㸠ヺˈৃҹՓ⫼ҹϟ䕀Нᄫヺᑣ߫㸼⼎䖭ѯᄫヺҹঞ݊ᅗϔѯ 㸠ᯊᴎ఼ᄫヺ䲚Ёℸᄫヺᇍᑨⱘ᭄ؐˈ໮ᄫヺᐌ䞣ⱘؐ⬅݋ԧᅲ⦄ᅮНDŽ ᄫヺᐌ䞣ᰃ⫼ऩᓩোᓩ䍋ᴹⱘϔϾ៪໮Ͼᄫヺᵘ៤ⱘᑣ߫ˈབ'x'DŽऩᄫヺᐌ䞣ⱘؐᰃᠻ 2. ᆓ׸Ш२ ᅷಾ໭႙ަԅd܊ࠧ΄ࢡᆳಾ long फ໸dU 䇈ᯢ˖ANSI γᅹᄯēჼ໸Ш२ԅफ໸Αԛ 1 ͑ྑ؏ၶԄտdၽԛ 1 ͑ᄯēӖԅჼ໸Ш२ long intDŽ ៪ ungigned long int DŽབᵰᅗⱘৢ㓔Ў l ៪ Lˈ߭݊㉏ൟᕜৃ㛑ᰃ long int ៪ unsigned int ៪ unsigned long int DŽབᵰᅗⱘৢ㓔Ў u ៪ Uˈ߭݊㉏ൟᕜৃ㛑ᰃ unsigned int ᵰᅗ≵᳝ৢ㓔Ϩᰃܿ䖯ࠊ៪क݁䖯ࠊ㸼⼎ˈ߭݊㉏ൟᕜৃ㛑ᰃ intǃunsigned int ǃlong w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᅮ঺ϔ԰⫼ඳЁⱘৠϔϾৡᄫᰃ৺އϾ䖲᥹DŽ԰⫼ඳे⿟ᑣЁৃҹ䆓䯂ℸৡᄫⱘऎඳˈ䖲᥹ ᅮњᷛ䆚ᇍ䈵Ёؐⱘ৿НDŽৡᄫ䖬݋᳝ϔϾ԰⫼ඳ੠ϔއऎඳⱘ⫳ᄬᳳˈ㉏ൟټⳌ݇㘨ⱘᄬ ᅮњϢ䆹ᷛ䆚ᇍ䈵އ㉏ټ㉏੠㉏ൟDŽᄬټԡ㕂DŽᇍᅗⱘ㾷䞞ձ䌪ѢϸϾЏ㽕ሲᗻ˖ᄬټϾᄬ ៤ਬ៪㘨ড়៤ਬ˗ᵮВᐌ䞣˗㉏ൟᅮНৡ˗ᷛোҹঞᇍ䈵ㄝDŽᇍ䈵᳝ᯊг⿄Ўব䞣ˈᅗᰃϔ ᷛ䆚ヺг⿄Ўৡᄫˈৃҹᣛҷ໮⾡ᅲԧ˖ߑ᭄ǃ㒧ᵘᷛ䆄ǃ㘨ড়ᷛ䆄੠ᵮВᷛ䆄˗㒧ᵘ A.4 ᷛ䆚ヺⱘ৿Н ໿ມ಴ζӒѻࣿॴdۦࠒۤ މ䇈ᯢ˖ူ·ೠԛ 1 ͑ٓѻԅဴ֥ൎϢලԅಾēҮ҉ٓѻԅဴ֥ߜζӒ಴ၮസ׸ԅညຕ 㸼⼎ϔϾᣀ೼㢅ᣀোЁⱘ㸼䖒ᓣˈ䆹㸼䖒ᓣᰃৃ䗝ⱘDŽA.13 㡖ᇍ䇁⊩䖯㸠њᘏ㒧DŽ { 㸼䖒ᓣ opt } ҹᬒ೼ϔ㸠Ёˈᑊҹⷁ䇁“one ofāᷛ䆚DŽৃ䗝ⱘ㒜㒧ヺ៪䴲㒜㒧ヺᏺ᳝ϟᷛ“optāDŽ՟བ˖ ϟˈϔ㒘ᄫヺ䭓ᑺ䕗ⷁⱘ׭䗝乍ৃމ㸼ϡDŽ໮Ͼ׭䗝㉏߿䗮ᐌ߫೼ϡৠⱘ㸠ЁˈԚ೼ϔѯᚙ ೼ᴀ᠟ݠ⫼ࠄⱘ䇁⊩ヺোЁˈ䇁⊩㉏߿⫼Ὃԧঞ᭰ԧᄫ㸼⼎DŽ᭛ᄫ੠ᄫヺҹᠧᄫൟᄫԧ A.3 䇁⊩ヺো ԅd ၭ༘ອঃᆓ׸ґᆓੋᄔࠩ໻ॕࠄdࣚᆓ׸ᆓ׸ґᆓੋᄔྙಾ ANSI γᅹᄯ໭႙ަރґᆓੋᄔྻ ᆓ׸؟cࠬᄘ༉דՇ՛ಾ ANSI γᅹᄯ໭႙ަԅġᆓ׸ґᆓੋᄔϢΡࠩ໻௕ڟ䇈ᯢ˖ຏॹ 䖯㸠䖲᥹ⱘ㸠Ўᰃ᳾ᅮНⱘDŽ ᄫヺІᄫ䴶ؐⱘ㉏ൟЎ“wchar_t ㉏ൟⱘ᭄㒘āDŽᇚ᱂䗮ᄫヺІᄫ䴶ؐ੠ᆑᄫヺᄫヺІᄫ䴶ؐ Ϣᄫヺᐌ䞣ϔḋˈᠽሩᄫヺ䲚ЁⱘᄫヺІᄫ䴶ؐгҹࠡᇐヺ L 㸼⼎ˈབ L"…"DŽᆑᄫヺ ӀDŽ ᄫヺІᄫ䴶ؐϡࣙ৿ᤶ㸠ヺ੠ঠᓩোᄫヺˈԚৃҹ⫼Ϣᄫヺᐌ䞣Ⳍৠⱘ䕀Нᄫヺᑣ߫㸼⼎ᅗ ೼ᄫヺІⱘৢ䴶๲ࡴϔϾぎᄫ㡖\0ˈ䖭ḋˈᠿᦣᄫヺІⱘ⿟ᑣ֓ৃҹᡒࠄᄫヺІⱘ㒧ᴳԡ㕂DŽ ៥ӀৃҹᡞⳌ䚏ⱘᄫヺІᄫ䴶ؐ䖲᥹ЎϔϾऩϔⱘᄫヺІDŽᠻ㸠ӏԩ䖲᥹᪡԰ৢˈ䛑ᇚ ヺІᄫ䴶ؐˈ߭㸠Ўᰃ᳾ᅮНⱘDŽ Ѣ݋ԧⱘᅲ⦄DŽབᵰ⿟ᑣ䆩೒ׂᬍᄫއ䖯㸠߱ྟ࣪DŽᇍⳌৠⱘᄫヺІᄫ䴶ؐᰃ৺䖯㸠ऎߚপ ㉏Ў static˄খ㾕 A.4 㡖˅ˈᅗՓ⫼㒭ᅮⱘᄫヺټ“…”DŽᄫヺІⱘ㉏ൟЎĀᄫヺ᭄㒘āˈᄬ ᄫヺІᄫ䴶ؐ˄string literal˅г⿄ЎᄫヺІᐌ䞣ˈᰃ⫼ঠᓩোᓩ䍋ᴹⱘϔϾᄫヺᑣ߫ˈབ A.2.6 ᄫヺІᄫ䴶ؐ ໄᯢЎᵮВヺⱘᷛ䆚ヺᰃ int ㉏ൟⱘᐌ䞣˄খ㾕 A.8.4 㡖 ˅DŽ 4. ਛࡨШ२ ᅷಾ໭႙ަԅd܊䇈ᯢ˖׼ԤШ२ԅ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! Ёⱘӏԩ㉏ൟ䛑ৃ㛑ᰃৠНⱘˈԚ㊒ᑺҢࠡࠄৢᰃ䗦๲ⱘDŽ ऩ㊒ᑺ⍂⚍᭄˄float˅ǃঠ㊒ᑺ⍂⚍᭄˄double˅੠໮㊒ᑺ⍂⚍᭄˄long double˅ 㸼⼎ᰃⳌৠⱘDŽ ೼Ⳍᑨⱘ᮴ヺোᇍ䈵Ёⱘؐⱘᄤ䲚ˈᑊϨˈ䖭ϸϾ䲚ড়ⱘ䞡঴䚼ߚⱘټ䋳ؐⱘ䲚ড়ᰃৃҹᄬ ೼ᏺヺোᇍ䈵Ёⱘ䴲ټⱘѠ䖯ࠊԡ᭄ˈ䖭ḋˈᇍ᮴ヺো᭄ⱘㅫᴃ䖤ㅫ∌䖰ϡӮ⑶ߎDŽৃҹᄬ ҹ݇䬂ᄫ unsigned ໄᯢⱘ᮴ヺোᭈ᭄䙉ᅜㅫᴃ῵ 2n ⱘ㾘߭ˈ݊Ёˈn ᰃ㸼⼎Ⳍᑨᭈ᭄ 䰸䴲⡍߿䇈ᯢˈint ㉏ൟ䛑㸼⼎ᏺヺো᭄DŽ Փᕫϔ㠀ᭈൟ˄int˅Ϣⷁᭈൟ˄short int˅៪䭓ᭈൟ˄long int˅݋᳝ৠḋⱘ໻ᇣDŽ ぎ䯈˗Ԛᰃ݋ԧⱘᅲ⦄ৃҹټ৘⾡⡍⅞ⱘ⫼䗨DŽ䕗䭓ⱘᭈ᭄㟇ᇥ㽕ऴ᳝Ϣ䕗ⷁᭈ᭄ϔḋⱘᄬ ᅮⱘ㞾✊䭓ᑺⳌৠDŽ݊ᅗ㉏ൟⱘᭈൟৃҹ⒵䎇އ䗮 int ᇍ䈵ⱘ䭓ᑺϢ⬅ᆓЏᴎ఼ⱘԧ㋏㒧ᵘ 䰸 char ㉏ൟ໪ˈ䖬᳝ 3 ⾡ϡৠ໻ᇣⱘᭈൟ㉏ൟ˖short intǃint ੠ long intDŽ᱂ ႙ަԅd 䇈ᯢ˖·ೠԅԛ 1 ͑ᄯਠပ unsigned char फ໸ēӬუᄵဈ֥۳Шߎdsigned char ಾ໭ 䋳ⱘDŽҹ signed char ᰒᓣໄᯢⱘᏺヺোᄫヺϢ᱂䗮ᄫヺгऴ⫼ৠḋ໻ᇣⱘぎ䯈DŽ ҹ unsigned char ໄᯢⱘ᮴ヺোᄫヺϢ᱂䗮ᄫヺऴ⫼ৠḋ໻ᇣⱘぎ䯈ˈԚ݊ؐᘏᰃ䴲 োˈৠ݋ԧⱘᅲ⦄᳝݇DŽ ೼ char ㉏ൟⱘব䞣ЁˈԚ݊পؐ㣗ೈˈ⡍߿ᰃ݊ؐᰃ৺ᏺヺټؐDŽ݊ᅗ㉏ൟⱘᇍ䈵гৃҹᄬ ೼ϔϾ char ㉏ൟⱘᇍ䈵Ёˈ߭䆹ᇍ䈵ⱘؐㄝѢᄫヺⱘᭈൟ㓪ⷕؐˈᑊϨᰃ䴲䋳ټᶤϾᄫヺᄬ ᠻ㸠ᄫヺ䲚ЁⱘӏԩᄫヺDŽབᵰᄫヺ䲚ЁⱘټໄᯢЎᄫヺ˄char˅ⱘᇍ䈵㽕໻ࠄ䎇ҹᄬ ㉏ൟⱘ᳔໻ؐ੠᳔ᇣؐDŽ䰘ᔩ B 㒭ߎⱘ᭄ؐ㸼⼎᳔ᇣⱘৃ᥹ফ䰤ᑺDŽ ༈᭛ӊЁᅮНњᴀഄᅲ⦄Ё↣⾡ޚ෎ᴀ㉏ൟࣙᣀ໮⾡DŽ䰘ᔩ B Ёᦣ䗄ⱘᷛ A.4.2 ෎ᴀ㉏ൟ ᯢᯊˈᇍ䈵ᇍᭈϾ⿟ᑣᴹ䇈ᰃܼሔৃ䆓䯂ⱘˈᑊϨ݋᳝໪䚼䖲᥹DŽ ㉏៪䗮䖛݇䬂ᄫ extern 䖯㸠ໄټᇍ䈵ˈ䖭⾡㉏ൟⱘᇍ䈵ᇚ݋᳝ݙ䚼䖲᥹DŽᔧⳕ⬹ᰒᓣⱘᄬ ⱘሔ䚼ܗৠϔ㑻ⱘᇍ䈵ᘏᰃ䴭ᗕⱘDŽৃҹ䗮䖛 static ݇䬂ᄫᇚᇍ䈵ໄᯢЎᶤϾ⡍ᅮ㗏䆥ऩ ⷕⱘ⿟ᑣഫ˅ݙˈ䴭ᗕᇍ䈵⫼݇䬂ᄫ static ໄᯢDŽ೼᠔᳝⿟ᑣഫ໪䚼ໄᯢϨϢߑ᭄ᅮН೼ ೼䗔ߎ੠ݡ䖯ܹߑ᭄៪⿟ᑣഫᯊ݊ؐᇚֱᣕϡবDŽ೼ϔϾ⿟ᑣഫ˄ࣙᣀᦤկߑ᭄ҷˈމ⾡ᚙ 䴭ᗕᇍ䈵ৃҹᰃᶤϾ⿟ᑣഫⱘሔ䚼ᇍ䈵ˈгৃҹᰃ᠔᳝⿟ᑣഫⱘ໪䚼ᇍ䈵DŽ᮴䆎ᰃાϔ ೼ᴎ఼ⱘᖿ䗳ᆘᄬ఼Ё˄བᵰৃ㛑ⱘ䆱˅DŽټ㉏ᇍ䈵ˈᑊϨᇚ㹿ᄬټⱘᇍ䈵гᰃ㞾ࡼᄬ ㉏ᇍ䈵DŽໄᯢЎ registerټབᵰՓ⫼њ auto 䰤ᅮヺˈ߭⿟ᑣഫЁⱘໄᯢ⫳៤ⱘ䛑ᰃ㞾ࡼᄬ ㉏䇈ᯢヺˈ៪㗙ټA.9.3 㡖˅ᴹ䇈ᰃሔ䚼ⱘˈ೼䗔ߎ⿟ᑣഫᯊ䆹ᇍ䈵ᇚ⍜༅DŽབᵰ≵᳝Փ⫼ᄬ ㉏ᇍ䈵ᇍѢϔϾ⿟ᑣഫ˄খ㾕ټ㉏DŽ㞾ࡼᄬټᅮњᇍ䈵ⱘᄬއѯ݇䬂ᄫ੠ໄᯢⱘϞϟ᭛݅ৠ ㉏˄static˅DŽໄᯢᇍ䈵ᯊՓ⫼ⱘϔټ㉏˄automatic˅੠䴭ᗕᄬټ㉏ߚЎϸ㉏˖㞾ࡼᄬټᄬ ㉏ټA.4.1 ᄬ ᣛ৥ৠϔϾᇍ䈵៪ߑ᭄DŽ԰⫼ඳ੠䖲᥹ᇚ೼ A.11 㡖Ё䅼䆎DŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᇚ䇈ᯢ䖭⾡䕀ᤶѻ⫳ⱘ㒧ᵰDŽA.6.5 㡖ᇚ䅼䆎໻໮᭄᱂䗮䖤ㅫヺ᠔㽕∖ⱘ䕀ᤶˈ៥Ӏ೼䆆㾷↣ ḍ᥂᪡԰᭄ⱘϡৠˈᶤѯ䖤ㅫヺӮᓩ䍋᪡԰᭄ⱘؐҢᶤ⾡㉏ൟ䕀ᤶЎ঺ϔ⾡㉏ൟDŽᴀ㡖 A.6 䕀ᤶ 䳔㽕䇈ᯢℸ䖤ㅫヺᰃ৺䳔㽕ϔϾᎺؐ᪡԰᭄ҹঞᅗᰃ৺ѻ⫳ϔϾᎺؐDŽ ᴹ⑤Ѣ䌟ؐ㸼䖒ᓣ E1=E2ˈ݊ЁˈᎺ᪡԰᭄ E1 ᖙ乏ᰃϔϾᎺؐ㸼䖒ᓣDŽᇍ↣Ͼ䖤ㅫヺⱘ䅼䆎 ᰃϔϾᣛ䩜㉏ൟⱘ㸼䖒ᓣˈ*E ߭ᰃϔϾᎺؐ㸼䖒ᓣˈᅗᓩ⫼⬅ E ᣛ৥ⱘᇍ䈵DŽৡᄫĀᎺؐ” ㉏ⱘᷛ䆚ヺ֓ᰃᎺؐ㸼䖒ᓣⱘϔϾᯢᰒⱘ՟ᄤDŽᶤѯ䖤ㅫヺৃҹѻ⫳ᎺؐDŽ՟བˈབᵰ Eټᄬ ऎඳˈᎺؐ˄lvalue˅ᰃᓩ⫼ᶤϾᇍ䈵ⱘ㸼䖒ᓣDŽ݋᳝ড়䗖㉏ൟϢټᇍ䈵ᰃϔϾੑৡⱘᄬ A.5 ᇍ䈵੠Ꮊؐ পؐⱘ㣗ೈˈгϡᕅડ݊ㅫᴃሲᗻDŽ䰤ᅮヺᇚ೼ A.8.2 㡖Ё䅼䆎DŽ ҹׂᬍ˗ໄᯢЎ volatile ⱘᇍ䈵㸼ᯢᅗ݋᳝ϢӬ࣪Ⳍ݇ⱘ⡍⅞ሲᗻDŽ䰤ᅮヺ᮶ϡᕅડᇍ䈵 ᇍ䈵ⱘ㉏ൟৃҹ䗮䖛䰘ࡴⱘ䰤ᅮヺ䖯㸠䰤ᅮDŽໄᯢЎ const ⱘᇍ䈵㸼ᯢℸᇍ䈵ⱘؐϡৃ A.4.4 ㉏ൟ䰤ᅮヺ ϟˈ䖭ѯᵘ䗴ᇍ䈵ⱘᮍ⊩ৃҹ䗦ᔦՓ⫼DŽމϔ㠀ᚙ · ৃҹࣙ৿໮Ͼϡৠ㉏ൟᇍ䈵ЁӏᛣϔϾᇍ䈵ⱘ㘨ড় · ࣙ৿ϔ㋏߫ϡৠ㉏ൟᇍ䈵ⱘ㒧ᵘ · ᣛ৥㒭ᅮ㉏ൟᇍ䈵ⱘᣛ䩜 · 䖨ಲ㒭ᅮ㉏ൟᇍ䈵ⱘߑ᭄ · 㒭ᅮ㉏ൟᇍ䈵ⱘ᭄㒘 ൟৃҹ᳝᮴䰤໮Ͼ˖ 䰸෎ᴀ㉏ൟ໪ˈ៥Ӏ䖬ৃҹ䗮䖛ҹϟ޴⾡ᮍ⊩ᵘ䗴⌒⫳㉏ൟˈҢὖᗉᴹ䆆ˈ䖭ѯ⌒⫳㉏ A.4.3 ⌒⫳㉏ൟ void ㉏ൟ䇈ᯢϔϾؐⱘぎ䲚ড়ˈᅗᐌ㹿⫼ᴹ䇈ᯢϡ䖨ಲӏԩؐⱘߑ᭄ⱘ㉏ൟDŽ ㉏ൟ floatǃdouble ੠ long double 㒳⿄Ў⍂⚍㉏ൟ˄floating type˅DŽ ㉏ൟǃ৘⾡໻ᇣⱘ int ㉏ൟ˄᮴䆎ᰃ৺ᏺヺো˅ҹঞᵮВ㉏ൟ䛑㒳⿄Ўᭈൟ㉏ൟ˄integral type˅DŽ ಴ЎҹϞ䖭ѯ㉏ൟⱘᇍ䈵䛑ৃҹ㹿㾷䞞Ў᭄ᄫˈ᠔ҹˈৃҹᇚᅗӀ㒳⿄Ўㅫᴃ㉏ൟDŽchar ЁⱘϔϾˈ៪㗙䌟ؐϡᰃϔϾৠ㉏ൟⱘ㸼䖒ᓣˈ߭㓪䆥఼䗮ᐌӮѻ⫳䄺ਞֵᙃDŽ 㾕 A.8.4 㡖˅DŽᵮВ㉏ൟ㉏ԐѢᭈൟDŽԚᰃˈབᵰᶤϾ⡍ᅮᵮВ㉏ൟⱘᇍ䈵ⱘ䌟ؐϡᰃ݊ᐌ䞣 ᵮВᰃϔϾ݋᳝ᭈൟؐⱘ⡍⅞ⱘ㉏ൟDŽϢ↣ϾᵮВⳌ݇㘨ⱘᰃϔϾੑৡᐌ䞣ⱘ䲚ড়˄খ ຣၽಾϢອලԅd 䇈ᯢ˖long double ಾ໭႙ަԅफ໸dၽԛ 1 ͑ᄯēlong float ူ double फ໸ԉޮēӬ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! double ㉏ൟDŽ བᵰӏԩϔϾ᪡԰᭄Ў long double ㉏ൟˈ߭ᇚ঺ϔϾ᪡԰᭄䕀ᤶЎ longˈܜ佪 䕀ᤶDŽ ᳝᪡԰᭄䕀ᤶЎৠϔ݀݅㉏ൟˈᑊҹℸ԰Ў㒧ᵰⱘ㉏ൟDŽ䖭⾡ᮍᓣⱘ䕀ᤶ⿄Ў᱂䗮ㅫᴃ㉏ൟ 䆌໮䖤ㅫヺ䛑Ӯҹ㉏Ԑⱘᮍᓣ೼䖤ㅫ䖛⿟Ёᓩ䍋䕀ᤶˈᑊѻ⫳㒧ᵰ㉏ൟDŽ݊ᬜᵰᰃᇚ᠔ A.6.5 ㅫᴃ㉏ൟ䕀ᤶ ᵰৃ㛑ᰃϟϔϾ䕗催៪䕗Ԣⱘৃ㸼⼎ؐDŽབᵰ㒧ᵰ೼ৃ㸼⼎㣗ೈП໪ˈ߭݊㸠Ўᰃ᳾ᅮНⱘDŽ Ͼ䕗催㊒ᑺⱘ⍂⚍㉏ൟؐ䕀ᤶЎ䕗Ԣ㊒ᑺⱘ⍂⚍㉏ൟᯊˈབᵰᅗⱘؐ೼ৃ㸼⼎㣗ೈݙˈ߭㒧 ᇚϔϾ㊒ᑺ䕗Ԣⱘ⍂⚍ؐ䕀ᤶЎⳌৠ៪᳈催㊒ᑺⱘ⍂⚍㉏ൟᯊˈᅗⱘֱؐᣕϡবDŽᇚϔ A.6.4 ⍂⚍㉏ൟ НⱘDŽ ߭㒧ᵰৃ㛑ᰃϟϔϾ䕗催៪䕗Ԣⱘৃ㸼⼎ؐDŽབᵰ䆹ؐ䍙ߎৃ㸼⼎ⱘ㣗ೈˈ߭݊㸠Ўᰃ᳾ᅮ ᔧᡞᭈൟؐ䕀ᤶЎ⍂⚍㉏ൟᯊˈབᵰ䆹ؐ೼䆹⍂⚍㉏ൟৃ㸼⼎ⱘ㣗ೈݙԚϡ㛑㊒⹂㸼⼎ˈ ݊㸠Ўᰃ᳾ᅮНⱘDŽ⡍߿ᰃˈᇚ䋳ⱘ⍂⚍᭄䕀ᤶЎ᮴ヺোᭈൟⱘ㒧ᵰᰃ≵᳝ᅮНⱘDŽ ᔧᡞ⍂⚍㉏ൟⱘؐ䕀ᤶЎᭈൟᯊˈᇣ᭄䚼ߚᇚ㹿϶ᓗDŽབᵰ㒧ᵰؐϡ㛑⫼ᭈൟ㸼⼎ˈ߭ A.6.3 ᭈ᭄੠⍂⚍᭄ ৺߭ᅗⱘؐৠ݋ԧⱘᅲ⦄᳝݇DŽ ᇚӏԩᭈ᭄䕀ᤶЎᏺヺো㉏ൟᯊˈབᵰᅗৃҹ೼ᮄ㉏ൟЁ㸼⼎ߎᴹˈֱ߭݊ؐᣕϡবˈ DŽܙ฿ ؐ䖯㸠ヺোᠽሩ੠ᇍ᮴ヺোؐ䖯㸠 0 ⱘԡ῵ᓣ䕗じˈ䖭ህⳌᔧѢᎺ៾প˗བᵰ䆹᮴ヺো㉏ൟⱘԡ῵ᓣ䕗ᆑˈ䖭ህⳌᔧѢᇍᏺヺো ؐࡴ 1 Ў῵ˈᡒߎϢℸᭈ᭄ৠԭⱘ᳔ᇣⱘ䴲䋳ؐDŽ೼ᇍѠⱘ㸹ⷕ㸼⼎Ёˈབᵰ䆹᮴ヺো㉏ൟ ᇚӏԩᭈ᭄䕀ᤶЎᶤ⾡ᣛᅮⱘ᮴ヺো㉏ൟ᭄ⱘᮍ⊩ᰃ˖ҹ䆹᮴ヺো㉏ൟ㛑໳㸼⼎ⱘ᳔໻ A.6.2 ᭈൟ䕀ᤶ ಖ˄integral promotion˅DŽ ߭݊ؐᇚ㹿䕀ᤶЎ int ㉏ൟ˗৺߭ᇚ㹿䕀ᤶЎ unsigned int ㉏ൟDŽ䖭ϔ䖛⿟⿄Ўჼ໸ඔ ៪ᭈൟԡᄫ↉ˈ䖬ৃҹՓ⫼ᵮВ㉏ൟⱘᇍ䈵DŽབᵰॳྟ㉏ൟⱘ᠔᳝ؐ䛑ৃ⫼ int ㉏ൟ㸼⼎ˈ ೼ϔϾ㸼䖒ᓣЁˈ޵ᰃৃҹՓ⫼ᭈൟⱘഄᮍ䛑ৃҹՓ⫼ᏺヺো៪᮴ヺোⱘᄫヺǃⷁᭈൟ A.6.1 ᭈൟᦤछ DŽܙϔѯ㸹خϾ䖤ㅫヺᯊᇚ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᇍ唤䰤ࠊⱘᇍ䈵ⱘᣛ䩜ˈᑊৃҹֱ䆕ॳᇕϡࡼഄݡ䕀ᤶಲᴹDŽټϔϾᣛ৥݋᳝᳈ᇣ៪Ⳍৠᄬ ᇍ唤㽕∖ˈ߭㒧ᵰᣛ䩜ৃ㛑Ӯᇐ㟈ഄഔᓖᐌDŽᣛ৥ᶤᇍ䈵ⱘᣛ䩜ৃҹ䕀ᤶЎټ⒵䎇ϔᅮⱘᄬ ᣛ৥ᶤϔ㉏ൟⱘᣛ䩜ৃҹ䕀ᤶЎᣛ৥঺ϔ㉏ൟⱘᣛ䩜ˈԚᰃˈབᵰ䆹ᣛ䩜ᣛ৥ⱘᇍ䈵ϡ ձ䌪Ѣ݋ԧⱘᅲ⦄DŽމᤶЎৠϔϾᣛ䩜ˈ݊ᅗᚙ ᭈൟᇍ䈵ৃҹᰒᓣഄ䕀ᤶЎᣛ䩜DŽ䖭⾡᯴ᇘᘏᰃᇚϔϾ䎇໳ᆑⱘҢᣛ䩜䕀ᤶᴹⱘᭈ᭄䕀 ᭄гձ䌪Ѣ݋ԧⱘᅲ⦄DŽ ᣛ䩜ৃҹ䕀ᤶЎᭈൟˈԚℸᭈൟᖙ乏䎇໳໻˗᠔㽕∖ⱘ໻ᇣձ䌪Ѣ݋ԧⱘᅲ⦄DŽ᯴ᇘߑ Ͼᰒᓣⱘ㉏ൟ䕀ᤶ䖤ㅫヺ៪ᔎࠊ㉏ൟ䕀ᤶᴹᣛᅮ˄খ㾕 A.7.5 㡖੠ A.8.8 㡖 ˅DŽ 䆌䖯㸠ᣛ䩜Ⳍ݇ⱘ݊ᅗᶤѯ䕀ᤶˈԚ݊㒧ᵰձ䌪Ѣ݋ԧⱘᅲ⦄DŽ䖭ѯ䕀ᤶᖙ乏⬅ϔܕ䖬 ϔぎᣛ䩜ˈԚϡㄝѢӏԩᣛ৥ߑ᭄៪ᇍ䈵ⱘᣛ䩜DŽ 䕗᪡԰䕀ᤶЎӏᛣ㉏ൟⱘᣛ䩜DŽ݊㒧ᵰᇚѻ⫳ϔϾぎᣛ䩜ˈℸぎᣛ䩜ㄝѢᣛ৥ৠϔ㉏ൟⱘ঺ ؐЎ 0 ⱘᭈൟᐌ䞣㸼䖒ᓣ៪ᔎࠊ䕀ᤶЎ void * ㉏ൟⱘ㸼䖒ᓣৃ䗮䖛ᔎࠊ䕀ᤶǃ䌟ؐ៪↨ 䖤ㅫヺⱘᮍᓣ䖯㸠˄খ㾕 A.7.7 㡖 ˅DŽ⫣ޣ✻䕀ᤶᮍᓣᣝ 䖤ㅫˈ݊㒧ᵰᇚ㹿䕀ᤶЎᭈൟ˗⫣ޣϸϾᣛ৥ৠϔ᭄㒘Ёৠϔ㉏ൟⱘᇍ䈵ⱘᣛ䩜ৃҹ䖯㸠 ヺⱘᮍᓣ䖯㸠˄খ㾕 A.7.7 㡖 ˅DŽ ϟˈᭈൟ㸼䖒ᓣⱘ䕀ᤶᣝ✻ࡴ⊩䖤ㅫމএϔϾᭈൟ㸼䖒ᓣDŽ೼䖭⾡ᚙޣᣛ䩜ৃҹࡴϞ៪ A.6.6 ᣛ䩜੠ᭈ᭄ ಾํ֥၇ॸԅd ఏ௶ڴࣣຏԅৰְēӲྡྷّํ׸ۜζӒ಴ူྡྷّ࡮ပලྂЩէԅӛ׸ۜζӒ಴ອΑࠀನēࠒ ರဈெۦ႕౭ฑ؏ၶྡྷ໔ēӬ߈౲ॴํ׸ۜ೴ူӛ׸ۜ೴ݡڟۜफ໸ᆙಾ҉ဟᄆଅԙสd໭ फ໸Ģ֗ၽԛ 1 ͑ᄯēํ׸ڴЩԅӛ׸ۜफ໸ྡྷ୯ၮസನēϢߜํ׸ۜफ໸ԅ೫໿ҎԞٓࠒ Շēൎပԅ׼Ԥၮസ՛ಾഀ࠺էdԛ֝ēӲࠀիԅํ׸ۜफ໸ူࠀڟഀ࠺էĢ֗ၽԛ 1 ͑ᄯ dԛྡྷēճ float फ໸Ѐᆴ೴ԅസ೬ၮസࢶྻᄚဈӦ࠺է֗Ϣಾܤ䇈ᯢ˖უऺပ०ّέ ৺߭ˈᇚϸϾ᪡԰᭄䛑䕀ᤶЎ int ㉏ൟDŽ int ㉏ൟDŽ ৺߭ˈབᵰӏԩϔϾ᪡԰᭄Ў unsigned int ㉏ൟˈ߭ᇚ঺ϔϾ᪡԰᭄䕀ᤶЎ unsigned ৺߭ˈབᵰϔϾ᪡԰᭄Ў long int ㉏ൟˈ߭ᇚ঺ϔϾ᪡԰᭄䕀ᤶЎ long int ㉏ൟDŽ unsigned long int ㉏ൟDŽ ᇚ unsigned int ㉏ൟⱘ᪡԰᭄䕀ᤶЎ long int ˗བᵰϡৃҹˈ߭ᇚϸϾ᪡԰᭄䛑䕀ᤶЎ 㒧ᵰձ䌪Ѣ long int ㉏ൟᰃ৺ৃҹ㸼⼎᠔᳝ⱘ unsigned int ㉏ൟⱘؐDŽབᵰৃҹˈ߭ ৺߭ˈབᵰϔϾ᪡԰᭄Ў long int ㉏ൟϨ঺ϔϾ᪡԰᭄Ў unsigned int ㉏ൟˈ߭ int ㉏ൟˈ߭ᇚ঺ϔϾ᪡԰᭄䕀ᤶЎ unsigned long int ㉏ൟDŽ ৺߭ˈৠᯊᇍϸϾ᪡԰᭄䖯㸠ᭈൟᦤछ˗✊ৢˈབᵰӏԩϔϾ᪡԰᭄Ў unsigned long ৺߭ˈབᵰӏԩϔϾ᪡԰᭄Ў float ㉏ൟˈ߭ᇚ঺ϔϾ᪡԰᭄䕀ᤶЎ float ㉏ൟDŽ ৺߭ˈབᵰӏԩϔϾ᪡԰᭄Ў double ㉏ൟˈ߭ᇚ঺ϔϾ᪡԰᭄䕀ᤶЎ double ㉏ൟDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ໪ˈ㸼䖒ᓣⱘ∖ؐ⃵ᑣމ㑻੠㒧ড়ᗻ᳝ᯢ⹂ⱘ㾘ᅮˈԚᰃˈ䰸ᇥ᭄՟໪ᚙܜ䖤ㅫヺⱘӬ ড়ᗻDŽ 㑻੠㒧ܜ䅼䆎䆹㡖⍝ঞࠄⱘ䖤ㅫヺⱘᎺǃে㒧ড়ᗻDŽA.13 㡖Ё㒭ߎⱘ䇁⊩㓐ড়њ䖤ㅫヺⱘӬ 㑻ⳌৠDŽ↣Ͼᇣ㡖Ё䖬ᇚܜヺ+˄খ㾕 A.7.7 㡖˅ⱘ᪡԰᭄DŽ೼↣ϔᇣ㡖Ёˈ৘Ͼ䖤ㅫヺⱘӬ 㑻ҟ㒡DŽВϾ՟ᄤˈᣝ✻䖭⾡݇㋏ˈA.7.1 㟇 A.7.6 㡖ЁᅮНⱘ㸼䖒ᓣৃҹ⫼԰ࡴ⊩䖤ㅫܜӬ 㑻ˈ៥Ӏᇚձ⃵ᣝ✻Ң催ࠄԢⱘܜᴀ㡖Ё৘Џ㽕ᇣ㡖ⱘ乎ᑣህҷ㸼њ㸼䖒ᓣ䖤ㅫヺⱘӬ A.7 㸼䖒ᓣ ֗ճୣ൑फ໸ᄗწԅݡဈ႕ྑௐࠩ໻ມ಴ஜᄥफ໸ᅧܰd ຂζӒ಴ᄯݡဈēڑANSI γᅹඋιၭ༘ void *फ໸ԅᄗწူୣ൑ճເᄗწၽ؎ᄔζӒ಴ۤ 䇈ᯢ˖ճ void *ᄗწԅࠓೄಾ໭႙ަԅdྻ஍ēchar *ᄗწ͒སᆂඹဈᄗწԅ߸ౄd 䌟ؐ㒭 void *㉏ൟⱘᣛ䩜ˈᑊৃϢ void *㉏ൟⱘᣛ䩜䖯㸠↨䕗DŽ ϔ㠀䳔㽕ᰒᓣⱘᔎࠊ䕀ᤶˈ䖭䞠᠔ϡৠⱘᰃˈᣛ䩜ৃҹ㹿䌟ؐЎ void * ㉏ൟⱘᣛ䩜ˈгৃҹ ߱ྟᣛ䩜㉏ൟˈ߭ৃҹᘶ໡߱ྟᣛ䩜DŽ៥Ӏ೼ A.6.6 㡖Ё䅼䆎䖛ˈᠻ㸠ᣛ䩜ࠄᣛ䩜ⱘ䕀ᤶᯊˈ ᣛ৥ӏԩᇍ䈵ⱘᣛ䩜䛑ৃҹ䕀ᤶЎ void * ㉏ൟˈϨϡӮ϶༅ֵᙃDŽབᵰᇚ㒧ᵰݡ䕀ᤶЎ A.6.8 ᣛ৥ void ⱘᣛ䩜 ᆂd ರဈַڜ΄ē൑ྡྷᄐ܊䇈ᯢ˖void ਠပၽ·ೠԅԛ 1 ͑ᄯѻຣēӬಾၽ·ೠԛ 1 ͑ѻ͑ ࠊ㉏ൟ䕀ᤶᇚ϶ᥝߑ᭄䇗⫼ⱘ䖨ಲؐDŽ ৃҹ䗮䖛ᔎࠊ㉏ൟ䕀ᤶᇚ㸼䖒ᓣ䕀ᤶЎ void ㉏ൟDŽ՟བˈ೼㸼䖒ᓣ䇁হЁˈϔϾぎⱘᔎ 㡖 ˅DŽ ⱘഄᮍˈ՟བ԰ЎϔϾ㸼䖒ᓣ䇁হ˄খ㾕 A.9.2 㡖˅៪԰Ў䗫ো䖤ㅫヺⱘᎺ᪡԰᭄˄খ㾕 A.7.18 䴲ぎ㉏ൟDŽ಴Ўぎ˄void˅㸼䖒ᓣ㸼⼎ϔϾϡᄬ೼ⱘؐˈ䖭ḋⱘ㸼䖒ᓣাৃҹ⫼೼ϡ䳔㽕ؐ void ᇍ䈵ⱘ˄ϡᄬ೼ⱘ˅ؐϡ㛑໳ҹӏԩᮍᓣՓ⫼ˈгϡ㛑㹿ᰒᓣ៪䱤ᓣഄ䕀ᤶЎӏϔ A.6.7 void Ϣॳᴹⱘᣛ䩜ϔ㟈DŽ ߑ᭄ⱘ㒧ᵰձ䌪Ѣ݋ԧⱘᅲ⦄DŽԚᰃˈབᵰ䕀ᤶৢⱘᣛ䩜㹿䞡ᮄ䕀ᤶЎॳᴹⱘ㉏ൟˈ߭㒧ᵰ ᳔ৢˈᣛ৥ϔϾߑ᭄ⱘᣛ䩜ৃҹ䕀ᤶЎᣛ৥঺ϔϾߑ᭄ⱘᣛ䩜DŽ䇗⫼䕀ᤶৢᣛ䩜᠔ᣛⱘ ⱘ䰤ᅮヺⱘ䰤ࠊDŽ ৠⱘᰃ๲ࡴњ䰤ᅮヺᏺᴹⱘ䰤ࠊDŽབᵰߴ䰸њ䰤ᅮヺˈ߭ᇍᑩሖᇍ䈵ⱘ䖤ㅫҡফᅲ䰙ໄᯢЁ 䰸໪DŽབᵰ๲ࡴњ䰤ᅮヺˈ߭ᮄᣛ䩜Ϣॳᣛ䩜ㄝӋˈϡމヺ˄খ㾕 A.4.4 㡖੠ A.8.2 㡖˅ⱘᚙ ϔϾᣛ䩜ৃҹ䕀ᤶЎৠ㉏ൟⱘ঺ϔϾᣛ䩜ˈԚ๲ࡴ៪ߴ䰸њᣛ䩜᠔ᣛⱘᇍ䈵㉏ൟⱘ䰤ᅮ 㡖ⱘ䅼䆎Ёⳟࠄˈᣛ䩜гৃҹ䕀ᤶЎ void *㉏ൟˈᑊৃॳᇕϡࡼഄ䕀ᤶಲᴹDŽ Āᇍ唤āⱘὖᗉձ䌪Ѣ݋ԧⱘᅲ⦄ˈԚ char ㉏ൟⱘᇍ䈵݋᳔᳝ᇣⱘᇍ唤䰤ࠊDŽ៥Ӏᇚ೼ A.6.8 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᰃ৺ᰃᎺؐϡফᣀোⱘᕅડDŽ ⫼ᣀোᣀ䍋ᴹⱘ㸼䖒ᓣᰃ߱ㄝ㸼䖒ᓣˈᅗⱘ㉏ൟ੠ؐϢ᮴ᣀোⱘ㸼䖒ᓣⳌৠDŽℸ㸼䖒ᓣ ᶤѯ߱ྟ࣪⿟ᑣЁϡ䖯㸠䖭ḋⱘ䕀ᤶˈ䆺㒚ֵᙃˈখ㾕 A.8.7 㡖DŽ ৥ char ㉏ൟ˄៪ wchar_t ㉏ൟ˅ⱘᣛ䩜ā㉏ൟˈ݊㒧ᵰᰃᣛ৥ᄫヺІЁ㄀ϔϾᄫヺⱘᣛ䩜DŽ ヺІˈ߭Ў“wchar_t ㉏ൟⱘ᭄㒘ā㉏ൟ˅ˈԚ䙉ᕾ A.7.1 㡖Ёⱘ㾘߭DŽᅗ䗮ᐌ㹿ׂᬍЎĀᣛ ᄫヺІᄫ䴶ؐᰃ߱ㄝ㸼䖒ᓣDŽᅗⱘ߱ྟ㉏ൟЎ“char ㉏ൟⱘ᭄㒘ā㉏ൟ˄ᇍѢᆑᄫヺᄫ ᐌ䞣ᰃ߱ㄝ㸼䖒ᓣˈ݊㉏ൟৠ݊ᔶᓣ᳝݇DŽ᳈䆺㒚ⱘֵᙃˈখ㾕 A.2.5 㡖Ёⱘ䅼䆎DŽ ᣛ䩜ˈ䙷МᅗህᰃϔϾᎺؐDŽ ໄᯢᣛᅮDŽབᵰᷛ䆚ヺᓩ⫼ϔϾᇍ䈵˄খ㾕 A.5 㡖˅ˈᑊϨ݊㉏ൟᰃㅫᴃ㉏ൟǃ㒧ᵘǃ㘨ড়៪ བᵰᣝ✻ϟ䴶ⱘᮍᓣᇍᷛ䆚ヺ䖯㸠䗖ᔧⱘໄᯢˈ䆹ᷛ䆚ヺህᰃ߱ㄝ㸼䖒ᓣDŽ݊㉏ൟ⬅݊ ďζӒ಴Đ ᆓ׸ґ Ш२ γಭ׸ ѺԉζӒ಴ġ ߱ㄝ㸼䖒ᓣࣙᣀᷛ䆚ヺǃᐌ䞣ǃᄫヺІ៪ᏺᣀোⱘ㸼䖒ᓣDŽ A.7.2 ߱ㄝ㸼䖒ᓣ ⱘߑ᭄ⱘᣛ䩜ā㉏ൟDŽ ⱘ᪡԰᭄ˈ৺߭ˈ㉏ൟЎĀ䖨ಲ T ㉏ൟؐⱘߑ᭄āⱘ㸼䖒ᓣᇚ㹿䕀ᤶЎĀᣛ৥䖨ಲ T ㉏ൟؐ 䖤ㅫヺ&៪ sizeofˈ߭ϡӮ䖯㸠䕀ᤶDŽ㉏Ԑഄˈ䰸䴲㸼䖒ᓣ㹿⫼԰&䖤ㅫヺܗℸ㸼䖒ᓣᰃϔ ᣛ৥᭄㒘Ё㄀ϔϾᇍ䈵ⱘᣛ䩜ˈᑊϨℸ㸼䖒ᓣⱘ㉏ൟᇚ㹿䕀ᤶЎĀᣛ৥ T ㉏ൟⱘᣛ䩜āDŽབᵰ ᇍѢᶤ㉏ൟ Tˈབᵰᶤ㸼䖒ᓣ៪ᄤ㸼䖒ᓣⱘ㉏ൟЎ“T ㉏ൟⱘ᭄㒘āˈ߭ℸ㸼䖒ᓣⱘؐᰃ A.7.1 ᣛ䩜⫳៤ ᑧߑ᭄䖯㸠䇗ᭈDŽޚ䴲ᷛ DŽᇍ䰸᭄Ў 0 ੠᠔᳝⍂⚍ᓖᐌⱘ໘⧚ˈϡৠⱘᅲ⦄䞛⫼ϡৠⱘᮍᓣˈ᳝ᯊ׭ৃҹ⫼خ䛑䖭М 䇁㿔ⱘᅲ⦄೼䖯㸠ᏺヺোᭈൟ㸼䖒ᓣⱘ∖ؐҹঞ䌟ؐᯊᗑ⬹⑶ߎᓖᐌˈԚᑊϡᰃ᠔᳝ⱘᅲ⦄ C 䇁㿔≵᳝ᅮН㸼䖒ᓣ∖ؐ䖛⿟Ёⱘ⑶ߎǃ䰸⊩Ẕᶹ੠݊ᅗᓖᐌⱘ໘⧚DŽ໻໮᭄⦄᳝ C ࢶટ֟ಓ࿎ѻԅெࣣdރസྻޙ׼Ԥ೴ၽࠄ࠭ୣ࠺էຫᄥನԅ έࠧ࿵ູ؟৐dუّۦസನࢶટݖϢਁᆠࠒޙ৐ನēࢶྻճζӒ಴ᄷ໭૦༝ēӬಾēၽۦࠒ ġӲζӒ಴ᄯԅၮസ׸ၽ೴༰౨ਁᆠ߬ܰ৐ۤއ႕ēڟ႕׌҂ॴၐຕԅྡྷّڟ䇈ᯢ˖؞ ᓣᰃݐᆍⱘDŽ ᤶ∖ؐ⃵ᑣDŽԚᰃˈ↣Ͼ䖤ㅫヺᇚ݊᪡԰᭄⫳៤ⱘؐ㒧ড়䍋ᴹⱘᮍᓣϢ㸼䖒ᓣⱘ䇁⊩ߚᵤᮍ ݊᪡԰᭄ᣝᶤϔ⡍ᅮ乎ᑣ∖ؐˈ৺߭ˈ݋ԧⱘᅲ⦄ৃҹ㞾⬅䗝ᢽӏϔ∖ؐ⃵ᑣˈ⫮㟇ৃҹѸ ᳝ᅮНˈ⫮㟇ᶤѯ᳝ࡃ԰⫼ⱘᄤ㸼䖒ᓣг≵᳝ᅮНDŽгህᰃ䇈ˈ䰸䴲䖤ㅫヺⱘᅮНֱ䆕њ≴ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ߑ᭄ໄᯢⱘ䅼䆎ˈখ㾕 A.8.6 㡖੠ A.10.1 㡖DŽ ᰃߑ᭄㉏ൟⱘϔ䚼ߚˈ䖭⾡ໄᯢ⿄Ўߑ᭄ॳൟDŽ೼ᮻⱘᮍᓣЁˈϡᣛᅮᔶᓣখ᭄㉏ൟDŽ᳝݇ ৃҹ䗮䖛ϸ⾡ᮍᓣໄᯢߑ᭄DŽ೼ᮄⱘໄᯢᮍᓣЁˈᔶᓣখ᭄ⱘ㉏ൟᰃᰒᓣໄᯢⱘˈᑊϨ ⱘؐDŽԚᰃˈৃҹᇚᣛ䩜԰Ўᅲ䰙খ᭄Ӵ䗦ˈ䖭ḋˈߑ᭄֓ৃҹׂᬍᣛ䩜ᣛ৥ⱘᇍ䈵ⱘؐDŽ ᭄ৃ㛑Ӯׂᬍᔶᓣখ᭄ᇍ䈵ⱘؐ˄ेᅲ䰙খ᭄㸼䖒ᓣⱘࡃᴀ˅ˈԚ䖭ϾׂᬍϡӮᕅડᅲ䰙খ᭄ ೼䇗⫼ߑ᭄Пࠡˈߑ᭄ⱘ↣Ͼᅲ䰙খ᭄ᇚ㹿໡ࠊˈ᠔᳝ⱘᅲ䰙খ᭄ϹḐഄᣝؐӴ䗦DŽߑ ⼎ߑ᭄ᅮН៪ߑ᭄ໄᯢЁⱘ䕧ܹᇍ䈵˄៪ᷛ䆚ヺ˅DŽ 䗮ᐌ⫼ᴃ䇁Āᅲ䰙খ᭄ā㸼⼎Ӵ䗦㒭ߑ᭄䇗⫼ⱘ㸼䖒ᓣˈ㗠ᴃ䇁Āᔶᓣখ᭄ā߭⫼ᴹ㸼 ೴Ըဈdࡘԅဴ֥ఏ௶ပ໒dۆ೴ԅᄗწࠩ໻ۆᄗ຿ڶဈۤඹ ೴Ըۆ೴ನΡ༖ပྡྷّມ಴ԅ*ၮസ׸dANSI γᅹၭ༘ຣပԅྡྷ໔Ω࿒୶ဈලྂԅဴ֥ࠩ໻ ۆ೴ԅᄗწԸဈۆᄗ຿ڶ೴rफ໸ēωயēඹۆ䇈ᯢ˖ၽԛ 1 ͑ᄯē؞फ໸΄ຫᄥนq T ㉏ൟⱘߑ᭄ⱘᣛ䩜āˈ݊Ё T Ўᶤ⾡㉏ൟˈϨߑ᭄䇗⫼ⱘؐⱘ㉏ൟЎ TDŽ 䆹ৢ㓔㸼䖒ᓣ˄೼ৃ㛑ⱘ䱤ᓣໄᯢ੠ᣛ䩜⫳៤Пৢˈখ㾕 A.7.1 㡖˅ⱘ㉏ൟᖙ乏ЎĀᣛ৥䖨ಲ extern int ᷛ䆚ヺ() 䱤ᓣഄໄᯢˈㄝৠѢ೼ᠻ㸠ℸߑ᭄䇗⫼ⱘ᳔ݙሖ⿟ᑣഫЁࣙ৿ϟ߫ໄᯢ˖ ህᰃߑ᭄ⱘখ᭄DŽབᵰৢ㓔㸼䖒ᓣࣙ৿ϔϾᔧࠡ԰⫼ඳЁϡᄬ೼ⱘᷛ䆚ヺˈ߭ℸᷛ䆚ヺᇚ㹿 ⱘ䌟ؐ㸼䖒ᓣ߫㸼㒘៤ˈ݊Ёⱘ䌟ؐ㸼䖒ᓣ߫㸼ৃ㛑Ўぎˈᑊ⬅䗫ো䖯㸠ߚ䱨ˈ䖭ѯ㸼䖒ᓣ ߑ᭄䇗⫼⬅ϔϾৢ㓔㸼䖒ᓣ˄⿄Ўߑ᭄ᷛᖫヺˈfunction designator˅ৢ䎳⬅೚ᣀোᣀ䍋ᴹ 2 ߑ᭄䇗⫼ *((E1) + (E2))DŽ᭄᳝݇㒘ᓩ⫼ⱘ᳈໮䅼䆎ˈখ㾕 A.8.6 㡖DŽ 䖒ᓣⱘ㉏ൟᖙ乏ЎᭈൟDŽ㒧ᵰᕫࠄϟᷛ㸼䖒ᓣⱘ㉏ൟЎ TDŽ㸼䖒ᓣ E1[E2]೼ᅮНϞㄝৠѢ ᮍᣀোࠡⱘৢ㓔㸼䖒ᓣⱘ㉏ൟᖙ乏ЎĀᣛ৥ T ㉏ൟⱘᣛ䩜āˈ݊Ё T Ўᶤ⾡㉏ൟ˗ᮍᣀোЁ㸼 ᏺϟᷛⱘ᭄㒘ᓩ⫼ৢ㓔㸼䖒ᓣ⬅ϔϾৢ㓔㸼䖒ᓣৢ䎳ϔϾᣀ೼ᮍᣀোЁⱘ㸼䖒ᓣ㒘៤DŽ 1 ᭄㒘ᓩ⫼ ϵ೴ζӒ಴ζ,؎ᄔζӒ಴ ؎ᄔζӒ಴ ϵ೴ζӒ಴ζġ կζӒ಴••܊ ++ᅷζӒ಴܊ ᅷζӒ಴•>γಭ׸܊ ᅷζӒ಴.γಭ׸܊ (ϵ೴ζӒ಴ζ opt ᅷζӒ಴܊ [ᅷζӒ಴>ζӒ಴܊ ѺԉζӒ಴ ᅷζӒ಴ġ܊ ৢ㓔㸼䖒ᓣЁⱘ䖤ㅫヺ䙉ᕾҢᎺࠄেⱘ㒧ড়㾘߭DŽ A.7.3 ৢ㓔㸼䖒ᓣ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䖤ㅫヺⱘ㸼䖒ᓣ䙉ᕾҢেࠄᎺⱘ㒧ড়ᗻDŽܗᏺϔ 䖤ㅫヺܗA.7.4 ϔ 㡖˅Ёⱘ䅼䆎DŽ݊㒧ᵰϡᰃᎺؐDŽ ԰᭄ⱘ䰤ࠊ੠䖤ㅫ㒚㡖ⱘ䆺㒚ֵᙃˈখ㾕ࡴ⊩㉏䖤ㅫヺ˄A.7.7 㡖˅੠䌟ؐ㉏䖤ㅫヺ˄A.7.17 DŽ᪡԰᭄ᖙ乏ᰃϔϾᎺؐDŽ᳝݇᪡˅••˄1 ޣᠻ㸠ᅠ䆹㸼䖒ᓣৢˈ᪡԰᭄ⱘؐᇚࡴ 1˄++˅៪ ৢ㓔㸼䖒ᓣৢ䎳ϔϾ++៪••䖤ㅫヺҡᰃϔϾৢ㓔㸼䖒ᓣDŽ㸼䖒ᓣⱘؐህᰃ᪡԰᭄ⱘؐDŽ 䖤ㅫヺޣ4 ৢ㓔㞾๲䖤ㅫヺϢৢ㓔㞾 ႕dڟ႕ωਠပஜᄥᄓ໻dᆫ໭ԅΩ࿒୶ۤ ANSI γᅹஜᄥᄓ໻ॴუྡྷڟēӬಾē؞ۦݧ॓ٲࠒ ᅷζӒ಴ᄗՇԅ܊ՇॴუᄵζӒ಴ᄯюၔԅ੠ᆓΡ༖೫ဟڟ䇈ᯢ˖ၽ·ೠԅԛ 1 ͑ᄯē ಴ℸˈ㸼䖒ᓣ E1•>MOS Ϣ(*E1).MOS ㄝӋDŽ㒧ᵘ੠㘨ড়ᇚ೼ A.8.3 㡖Ё䅼䆎DŽ ᰃ᭄㒘㉏ൟˈ߭㒧ᵰᰃϔϾᎺؐDŽ ৥ᣛ䩜㸼䖒ᓣᣛ৥ⱘ㒧ᵘ៪㘨ড়Ёੑৡⱘ៤ਬˈ㒧ᵰ㉏ൟᰃᇍᑨ៤ਬⱘ㉏ൟDŽབᵰ䆹㉏ൟϡ 㸼䖒ᓣᖙ乏ᰃϔϾᣛ৥㒧ᵘ៪㘨ড়ⱘᣛ䩜ˈᷛ䆚ヺᖙ乏ᰃ㒧ᵘ៪㘨ড়ⱘ៤ਬⱘৡᄫDŽ㒧ᵰᣛ ৢ㓔㸼䖒ᓣৢ䎳ϔϾㆁ༈˄⬅•੠>㒘៤˅੠ϔϾᷛ䆚ヺҡᰃৢ㓔㸼䖒ᓣDŽ㄀ϔϾ᪡԰᭄ ᰃ᭄㒘㉏ൟˈ߭ᭈϾ㸼䖒ᓣᰃϔϾᎺؐDŽ ਬˈ݊㉏ൟᰃᇍᑨ៤ਬⱘ㉏ൟDŽབᵰ㄀ϔϾ㸼䖒ᓣᰃϔϾᎺؐˈᑊϨ㄀ѠϾ㸼䖒ᓣⱘ㉏ൟϡ 乏ᰃ㒧ᵘ៪㘨ড়ˈᷛ䆚ヺᖙ乏ᰃ㒧ᵘ៪㘨ড়ⱘ៤ਬⱘৡᄫDŽ㒧ᵰؐᰃ㒧ᵘ៪㘨ড়Ёੑৡⱘ៤ ৢ㓔㸼䖒ᓣৢ䎳ϔϾ೚⚍੠ϔϾᷛ䆚ヺҡᰃৢ㓔㸼䖒ᓣDŽ㄀ϔϾ᪡԰᭄㸼䖒ᓣⱘ㉏ൟᖙ 3 㒧ᵘᓩ⫼ 䆌䖯㸠䗦ᔦ䇗⫼DŽܕᅲ䰙খ᭄੠ߑ᭄ᷛᖫヺᰃᅠܼ∖ؐⱘˈࣙᣀ᠔᳝ⱘࡃ԰⫼DŽᇍӏԩߑ᭄䛑 ᅲ䰙খ᭄ⱘ∖ؐ⃵ᑣ≵᳝ᣛᅮDŽϡৠ㓪䆥఼ⱘᅲ⦄ᮍᓣ৘ϡⳌৠDŽԚᰃˈ೼䖯ܹߑ᭄ࠡˈ ರဈdۦ೴ಒੜݡۆ಴ ರဈd࿫࠰ࢶટΥੇ໭ࡘۦ೴ԅݡۆ႕ׄШ؏ၶē࿙นΡ༖ྑࢩৎ໭ࡘ಴ڟ䇈ᯢ˖უ໔ ᔶᓣখ᭄㉏ൟ㒣䖛খ᭄ᦤछৢ˅DŽ ߑ᭄ॳൟЁ↣Ͼᔶᓣখ᭄ⱘ㉏ൟᖙ乏Ϣߑ᭄ᅮНЁⳌᑨⱘᔶᓣখ᭄㉏ൟϔ㟈˄ߑ᭄ᅮНЁⱘ 䰙খ᭄㽕䖯㸠咬䅸ⱘখ᭄ᦤछˈᦤछᮍ⊩ৠࠡ䴶᠔䗄DŽབᵰߑ᭄ᰃҹᮻᓣᮍᓣᅮНⱘˈ䙷Мˈ ⱘ᭄Ⳃᖙ乏ㄝѢ៪䍙䖛ᔶᓣখ᭄ⱘ᭄Ⳃ˗ᇍѢሒ䚼≵᳝ᰒᓣᣛᅮ㉏ൟⱘᔶᓣখ᭄ˈⳌᑨⱘᅲ ϟˈᅲ䰙খ᭄މ᭄ⳂⳌৠˈ䰸䴲ߑ᭄ໄᯢⱘᔶᓣখ᭄㸼ҹⳕ⬹ো˄, ...˅㒧ሒDŽ೼䖭⾡ᚙ ൟЁⱘⳌᑨᔶᓣখ᭄㉏ൟˈ䖭Ͼ䖛⿟㉏ԐѢ䌟ؐDŽᅲ䰙খ᭄᭄Ⳃᖙ乏Ϣᰒᓣໄᯢⱘᔶᓣখ᭄ ೼ߑ᭄䇗⫼ⱘ԰⫼ඳЁˈབᵰߑ᭄ᰃҹᮄᓣᮍᓣໄᯢⱘˈ߭ᅲ䰙খ᭄ᇚ㹿䕀ᤶЎߑ᭄ॳ ᦤछৢⱘᅲ䰙খ᭄㉏ൟᖙ乏Ϣ≵᳝ᦤछⱘᔶᓣখ᭄㞾䑿ⱘ㉏ൟֱᣕϔ㟈DŽ ↨䕗㒣ᦤछৢߑ᭄䇗⫼Ёⱘᅲ䰙খ᭄㉏ൟ੠ᦤछৢⱘᔶᓣখ᭄㉏ൟ˗བᵰᰃᮄᓣⱘᅮНˈ߭ ㉏ൟϔ㟈ᗻձ䌪Ѣߑ᭄ᰃҹᮄᓣᮍᓣᅮНⱘ䖬ᰃҹᮻᓣᮍᓣᅮНⱘDŽབᵰᰃᮻᓣⱘᅮНˈ߭ 㗙ᶤϾᅲ䰙খ᭄ⱘ㉏ൟᦤछৢϢⳌᑨⱘᔶᓣখ᭄㉏ൟϡϔ㟈ˈ߭ߑ᭄䇗⫼ⱘ㒧ᵰᰃ᳾ᅮНⱘDŽ ᭄䕀ᤶЎ double ㉏ൟDŽབᵰ䇗⫼ᯊᅲ䰙খ᭄ⱘ᭄ⳂϢߑ᭄ᅮНЁᔶᓣখ᭄ⱘ᭄Ⳃϡㄝˈ៪ 䖯㸠咬䅸খ᭄ᦤछ˖ᇍ↣Ͼᭈൟখ᭄䖯㸠ᭈൟᦤछ˄খ㾕 A.6.l 㡖˅˗ᇚ↣Ͼ float ㉏ൟⱘখ ೼ߑ᭄䇗⫼ⱘ԰⫼ඳЁˈབᵰߑ᭄ᰃҹᮻᓣᮍᓣໄᯢⱘˈ߭ᣝҹϟᮍᓣᇍ↣Ͼᅲ䰙খ᭄ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᵰⱘ㉏ൟЎᦤछৢⱘ᪡԰᭄ⱘ㉏ൟDŽ ⱘ᪡԰᭄䕀ᤶЎⳌᑨⱘ᮴ヺো㉏ൟˈՓ⫼䖤ㅫヺ~䅵ㅫডⷕˈݡᇚ㒧ᵰ䕀ᤶЎᏺヺো㉏ൟDŽ㒧 এ᪡԰᭄ⱘؐ㗠ᕫࠄⱘ㒧ᵰؐDŽབᵰ᪡԰᭄Ўᏺヺো㉏ൟˈ߭㒧ᵰⱘ䅵ㅫᮍᓣЎ˖ᇚᦤछৢ ޣ԰᭄䖯㸠ᭈൟᦤछDŽབᵰ᪡԰᭄Ў᮴ヺো㉏ൟˈ߭㒧ᵰЎᦤछৢⱘ㉏ൟ㛑໳㸼⼎ⱘ᳔໻ؐ 䖤ㅫヺ~ⱘ᪡԰᭄ᖙ乏ᰃᭈൟˈ㒧ᵰЎ᪡԰᭄ⱘѠ䖯ࠊডⷕDŽ೼䖤ㅫ䖛⿟Ё䳔㽕ᇍ᪡ܗϔ 6 Ѡ䖯ࠊডⷕ䖤ㅫヺ ᦤछৢⱘ᪡԰᭄ⱘؐˈ✊ৢࡴ 1˗Ԛ 0 ⱘ䋳ؐҡЎ 0DŽ㒧ᵰ㉏ൟЎᦤछৢⱘ᪡԰᭄ⱘ㉏ൟDŽ এޣ䖯㸠ᭈൟᦤछDŽᏺヺো᭄ⱘ䋳ؐⱘ䅵ㅫᮍ⊩Ў˖ᇚᦤछৢᕫࠄⱘ㉏ൟ㛑໳㸼⼎ⱘ᳔໻ؐ 䖤ㅫヺ•ⱘ᪡԰᭄ᖙ乏ᰃㅫᴃ㉏ൟˈ㒧ᵰЎ᪡԰᭄ⱘ䋳ؐDŽབᵰ᪡԰᭄ᰃᭈൟˈ߭ᇚܗϔ 䖤ㅫヺޣܗ5 ϔ 䇈ᯢ˖ྡྷ၍ၮസ׸+ಾ ANSI γᅹ໭႙ަԅē႙ަ؞ၮസ׸ಾนॴူྡྷ၍ၮസ׸•ճ࿫d 䖯㸠ᭈൟᦤछˈ㒧ᵰ㉏ൟᰃ㒣䖛ᦤछৢⱘ᪡԰᭄ⱘ㉏ൟDŽ 䖤ㅫヺ+ⱘ᪡԰᭄ᖙ乏ᰃㅫᴃ㉏ൟˈ݊㒧ᵰᰃ᪡԰᭄ⱘؐDŽབᵰ᪡԰᭄ᰃᭈൟˈ߭ᇚܗϔ ࡴ䖤ㅫヺܗ4 ϔ Āᣛ৥ T ㉏ൟⱘᣛ䩜āˈ߭㒧ᵰ㉏ൟЎ TDŽ ᣛ䩜Ϩᣛ৥ⱘᇍ䈵ᰃㅫᴃǃ㒧ᵘǃ㘨ড়៪ᣛ䩜㉏ൟˈ߭ᅗᰃϔϾᎺؐDŽབᵰ㸼䖒ᓣⱘ㉏ൟЎ 䖤ㅫヺ*㸼⼎䯈᥹ᇏഔˈᅗ䖨ಲ݊᪡԰᭄ᣛ৥ⱘᇍ䈵៪ߑ᭄DŽབᵰᅗⱘ᪡԰᭄ᰃϔϾܗϔ 3 䯈᥹ᇏഔ䖤ㅫヺ ߑ᭄DŽབᵰ᪡԰᭄ⱘ㉏ൟЎ Tˈ߭㒧ᵰⱘ㉏ൟЎᣛ৥ T ㉏ൟⱘᣛ䩜DŽ ᯢЎ register ㉏ൟⱘᇍ䈵˅ˈ៪㗙Ўߑ᭄㉏ൟDŽ㒧ᵰؐᰃϔϾᣛ䩜ˈᣛ৥Ꮊؐᣛ৥ⱘᇍ䈵៪ 䖤ㅫヺ&⫼Ѣপ᪡԰᭄ⱘഄഔDŽ䆹᪡԰᭄ᖙ乏ᰃϔϾᎺؐ˄ϡᣛ৥ԡᄫ↉ǃϡᣛ৥ໄܗϔ 2 ഄഔ䖤ㅫヺ ヺ˄খ㾕 A.7.17 㡖˅DŽ݊㒧ᵰϡᰃᎺؐDŽ ᳝݇᪡԰᭄ⱘ䰤ࠊ੠䖤ㅫ㒚㡖ⱘ䆺㒚ֵᙃˈখ㾕ࡴ⊩㉏䖤ㅫヺ˄খ㾕 A.7.7 㡖˅੠䌟ؐ㉏䖤ㅫ ҹৢⱘؐDŽ᪡԰᭄ᖙ乏ᰃϔϾᎺؐDŽ 1 ޣ㸼䖒ᓣⱘؐᰃ㒣䖛ࡴ 1ǃˈ˅••˄1 ޣࡴ 1˄++˅៪ 㸼䖒ᓣDŽ᪡԰᭄ᇚ㹿ܗ㸼䖒ᓣⱘࠡ䴶⏏ࡴ䖤ㅫヺ++៪••ৢᕫࠄⱘ㸼䖒ᓣᰃϔϾϔܗ೼ϔ 䖤ㅫヺޣ1 ࠡ㓔㞾๲䖤ㅫヺϢࠡ㓔㞾 & * + • ~ ! ྡྷ၍ၮസ׸ġone of sizeof(फ໸੠) sizeoff ྡྷ၍ζӒ಴ ྡྷ၍ၮസ׸ ஜᄥफ໸ᅧܰζӒ಴ ••ྡྷ၍ζӒ಴ ++ྡྷ၍ζӒ಴ ᅷζӒ಴܊ ၍ζӒ಴ġྡྷ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᭄ⱘ㒱ᇍؐᇣѢ䰸᭄ⱘ㒱ᇍؐDŽ ㄝѢ a ∌䖰៤ゟDŽབᵰϸϾ᪡԰᭄ഛЎ䴲䋳ˈ߭ԭ᭄Ў䴲䋳ؐϨᇣѢ䰸᭄ˈ৺߭ˈҙֱ䆕ԭ ϸϾ᪡԰᭄Ⳍ䰸ৢ᠔ᕫⱘԭ᭄DŽབᵰ㄀ѠϾ᪡԰᭄Ў 0ˈ߭㒧ᵰ≵᳝ᅮНDŽᑊϨˈ(a/b)*b+a%b 䖤ㅫヺ/⫼Ѣ䅵ㅫ㄀ϔϾ᪡԰᭄ৠ㄀ѠϾ᪡԰᭄Ⳍ䰸᠔ᕫⱘଚˈ㗠䖤ㅫヺ%⫼Ѣ䅵ㅫܗѠ 䖤ㅫヺ*㸼⼎Ь⊩DŽܗѠ ᅮDŽއ㸠᱂䗮ⱘㅫᴃ㉏ൟ䕀ᤶˈ㒧ᵰ㉏ൟ⬅ᠻ㸠ⱘ䕀ᤶ 䖤ㅫヺ*੠/ⱘ᪡԰᭄ᖙ乏Ўㅫᴃ㉏ൟˈ䖤ㅫヺ&ⱘ᪡԰᭄ᖙ乏ЎᭈൟDŽ䖭ѯ᪡԰᭄䳔㽕䖯 ѐ֥फζӒ಴%ஜᄥफ໸ᅧܰζӒ಴ ѐ֥फζӒ಴/ஜᄥफ໸ᅧܰζӒ಴ ѐ֥फζӒ಴*ஜᄥफ໸ᅧܰζӒ಴ ஜᄥफ໸ᅧܰζӒ಴ ѐ֥फζӒ಴ġ Ь⊩㉏䖤ㅫヺ&ǃ/੠%䙉ᕾҢᎺࠄেⱘ㒧ড়ᗻDŽ A.7.6 Ь⊩㉏䖤ㅫヺ ᔎࠊ㉏ൟ䕀ᤶⱘ㸼䖒ᓣϡᰃᎺؐDŽ 䖭⾡㒧ᵘ⿄Ўᔎࠊ㉏ൟ䕀ᤶDŽ㉏ൟৡᇚ೼ A.8.8 㡖ᦣ䗄DŽ䕀ᤶⱘ㒧ᵰᏆ೼ A.6 㡖䅼䆎䖛DŽࣙ৿ फ໸੠ ஜᄥफ໸ᅧܰζӒ಴ ྡྷ၍ζӒ಴ ஜᄥफ໸ᅧܰζӒ಴ġ 㸼䖒ᓣᇚᇐ㟈㸼䖒ᓣⱘؐ㹿䕀ᤶЎᣛᅮⱘ㉏ൟDŽܗҹᣀোᣀ䍋ᴹⱘ㉏ൟৡᓔ༈ⱘϔ A.7.5 ᔎࠊ㉏ൟ䕀ᤶ 㾕䰘ᔩ B˅Ёˈ䖭ϔ㉏ൟ㹿ᅮНЎ size_t ㉏ൟDŽ ༈᭛ӊ˄খޚ↉DŽ㒧ᵰᰃϔϾ᮴ヺোᭈൟᐌ䞣ˈ݋ԧⱘ㉏ൟ⬅ᅲ⦄ᅮНDŽ೼ᷛ DŽℸ䖤ㅫヺϡ㛑⫼Ѣߑ᭄㉏ൟ੠ϡᅠᭈ㉏ൟⱘ᪡԰᭄ˈгϡ㛑⫼Ѣԡᄫס ㋴䭓ᑺⱘ nܗ᭄㒘 ㋴ⱘ᭄㒘ⱘ䭓ᑺᰃϔϾܗぎ䯈˖᳝ n Ͼܙ฿ⱘᄫ㡖᭄ˈࣙᣀᇍ䈵Ёࣙ৿ⱘ᭄㒘᠔䳔㽕ⱘӏԩ ᵰؐЎ 1˗ᇚᅗᑨ⫼Ѣ᭄㒘ᯊˈ݊ؐЎ᭄㒘Ёᄫ㡖ⱘᘏ᭄DŽᑨ⫼Ѣ㒧ᵘ៪㘨ড়ᯊˈ㒧ᵰЎᇍ䈵 ؐⱘ㸼䖒ᓣˈгৃҹЎϔϾ⫼ᣀোᣀ䍋ᴹⱘ㉏ൟৡDŽᇚ sizeof ᑨ⫼Ѣ char ㉏ൟᯊˈ݊㒧 Ϣ݊᪡԰᭄ৠ㉏ൟⱘᇍ䈵᠔䳔ⱘᄫ㡖᭄DŽ᪡԰᭄ৃҹЎϔϾ᳾∖ټsizeof 䖤ㅫヺ䅵ㅫᄬ 8 sizeof 䖤ㅫヺ Ў 0DŽ㒧ᵰ㉏ൟЎ intDŽ 䖤ㅫヺ!ⱘ᪡԰᭄ᖙ乏ᰃㅫᴃ㉏ൟ៪㗙ᣛ䩜DŽབᵰ᪡԰᭄ㄝѢ 0ˈ߭㒧ᵰЎ 1ˈ৺߭㒧ᵰ 䘏䕥䴲䖤ㅫヺ 7 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ݇㋏䖤ㅫヺ䙉ᕾҢᎺࠄেⱘ㒧ড়ᗻˈԚ䖭Ͼ㾘߭≵᳝ҔМ԰⫼DŽa>E2 ⱘؐЎ E1 ৥ে⿏ E2 ԡᕫࠄⱘ㒧ᵰDŽབᵰ E1 Ў᮴ヺো E1<>ަ֥फζӒ಴ ྭสζӒ಴<<ަ֥फζӒ಴ ަ֥फζӒ಴ ྭสζӒ಴ġ Ѣ៪ㄝѢᎺ᪡԰᭄㉏ൟⱘԡ᭄ˈ߭㒧ᵰ≵᳝ᅮНDŽ 䙉ᕾᭈൟᦤछॳ߭DŽ㒧ᵰⱘ㉏ൟᰃᦤछৢⱘᎺ᪡԰᭄ⱘ㉏ൟDŽབᵰে᪡԰᭄Ў䋳ؐˈ៪㗙໻ ⿏ԡ䖤ㅫヺ<<੠>>䙉ᕾҢᎺࠄেⱘ㒧ড়ᗻDŽ↣Ͼ䖤ㅫヺⱘ৘᪡԰᭄䛑ᖙ乏ЎᭈൟˈᑊϨ A.7.8 ⿏ԡ䖤ㅫヺ ㋴ˈ߭(P+1)•P ⱘؐЎ 1DŽܗԚᰃˈབᵰ P ᣛ৥᭄㒘ⱘ᳔ৢϔϾ ЁᅮНЎ ptrdiff_tDŽা᳝ᔧᣛ䩜ᣛ৥ⱘᇍ䈵ሲѢৠϔ᭄㒘ᯊˈᏂؐᠡ᳝ᛣНDŽ ༈᭛ӊޚ䞣Ў 1DŽ㒧ᵰⱘ㉏ൟৠ݋ԧⱘᅲ⦄᳝݇ˈԚ೼ᷛ⿏أ䞣DŽⳌ䚏ᇍ䈵䯈ⱘ⿏أП䯈ⱘ ߭㒧ᵰᰃϔϾᏺヺোᭈൟ᭄ˈ㸼⼎ᅗӀᣛ৥ⱘᇍ䈵ˈޣབᵰᣛ৥ৠϔ㉏ൟⱘϸϾᣛ䩜Ⳍ ㅫⱘ䕀ᤶ㾘߭੠ᴵӊϢࡴ⊩ⱘⳌৠDŽ 䖤⫣ޣˈএϔϾӏԩᭈൟⱘؐޣ䖤ㅫヺ•⫼Ѣ䅵ㅫϸϾ᪡԰᭄ⱘᏂؐDŽৃҹҢᶤϾᣛ䩜Ϟ ೴ᆦ၍ഭdู׀ԙܬ༶ژྻ̟ზඹШԅ๽ 䇈ᯢ˖ၭ༘ᄗწᄗ຿೴ᆦ੬ม၍ഭԅຏྡྷّ၍ഭಾ ANSI ᄯ໭႙ަԅඋჸē൑ರԄแਬࢶ ㋴ৢⱘ㄀ϔϾԡ㕂ˈ߭㒧ᵰ≵᳝ᅮНDŽ ܗЁϟϔϾᇍ䈵ⱘᣛ䩜DŽབᵰⳌࡴ᠔ᕫⱘ੠ᇍᑨⱘᣛ䩜ϡ೼᭄㒘ⱘ㣗ೈݙˈϨϡᰃ᭄㒘᳿ሒ 䞣DŽ಴ℸˈབᵰ P ᰃϔϾᣛ৥᭄㒘ЁᶤϾᇍ䈵ⱘᣛ䩜ˈ߭㸼䖒ᓣ P+1 ᰃᣛ৥᭄㒘⿏أϔᅮⱘ Ϣ߱ྟᣛ䩜݋᳝Ⳍৠⱘ㉏ൟˈᑊᣛ৥ৠϔ᭄㒘Ёⱘ঺ϔϾᇍ䈵ˈℸᇍ䈵Ϣ߱ྟᇍ䈵П䯈݋᳝ 䞣DŽⳌࡴᕫࠄⱘ੠ᰃϔϾᣛ䩜ˈᅗ⿏أⳌࡴˈৢ㗙ᇚ䗮䖛Ьҹ᠔ᣛᇍ䈵ⱘ䭓ᑺ㹿䕀ᤶЎഄഔ 䖤ㅫヺ+⫼Ѣ䅵ㅫϸϾ᪡԰᭄ⱘ੠DŽᣛ৥᭄㒘ЁᶤϾᇍ䈵ⱘᣛ䩜ৃҹ੠ϔϾӏԩᭈൟⱘؐ ަ֥फζӒ಴•ѐ֥फζӒ಴ ަ֥फζӒ಴+ѐ֥फζӒ಴ ѐ֥फζӒ಴ ަ֥फζӒ಴ġ 䖯㸠᱂䗮ⱘㅫᴃ㉏ൟ䕀ᤶDŽ↣Ͼ䖤ㅫヺৃ㛑Ў໮⾡㉏ൟDŽ ࡴ⊩㉏䖤ㅫヺ+੠•䙉ᕾҢᎺࠄেⱘ㒧ড়ᗻDŽབᵰ᪡԰᭄Ё᳝ㅫᴃ㉏ൟⱘ᪡԰᭄ˈ߭䳔㽕 A.7.7 ࡴ⊩㉏䖤ㅫヺ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䖤ㅫヺҙ䗖⫼Ѣᭈൟ᪡԰᭄DŽ ᠻ㸠ᣝԡϢ䖤ㅫᯊ㽕䖯㸠᱂䗮ⱘㅫᴃ㉏ൟ䕀ᤶDŽ㒧ᵰЎ᪡԰᭄㒣ᣝԡϢ䖤ㅫৢᕫࠄⱘؐDŽ䆹 ̟สူζӒ಴&ອԉफζӒ಴ ອԉफζӒ಴ ̟สူζӒ಴ġ A.7.11 ᣝԡϢ䖤ㅫヺ ৃҹϢؐЎ 0 ⱘᐌ䞣ᭈൟ㸼䖒ᓣ៪ᣛ৥ void ⱘᣛ䩜䖯㸠↨䕗DŽখ㾕 A.6.6 㡖DŽ 䆌ᠻ㸠ϟ߫↨䕗˖ᣛ䩜ܕⳌㄝ㉏䖤ㅫヺϢ݇㋏䖤ㅫヺ݋᳝Ⳍৠⱘ㾘߭ˈԚ䖭㉏䖤ㅫヺ䖬 Ϣ c˄໻Ѣ˅ǃ<=˄ᇣѢㄝѢ˅੠>=˄໻ѢㄝѢ˅؛ᔧ݇㋏㸼䖒ᓣⱘ㒧ᵰЎ ຂζӒ಴>=ྭสζӒ಴ڑ ຂζӒ಴<=ྭสζӒ಴ڑ ຂζӒ಴>ྭสζӒ಴ڑ ຂζӒ಴<ྭสζӒ಴ڑ ྭสζӒ಴ ຂζӒ಴ġڑ 㹿㾷䞞Ў(a>= &= ^= != ؎ᄔၮസ׸ġone of ྡྷ၍ζӒ಴ ؎ᄔၮസ׸ ؎ᄔζӒ಴ ඨߑζӒ಴ ؎ᄔζӒ಴ġ 䌟ؐ䖤ㅫヺ᳝໮ϾˈᅗӀ䛑ᰃҢᎺࠄে㒧ড়DŽ A.7.17 䌟ؐ㸼䖒ᓣ ⬹ˈԚ㒧ᵰ㉏ൟӮ㒻ᡓᴵӊⱘ৘ߚᬃⱘ䰤ᅮヺDŽ ೼↨䕗ᣛ䩜ⱘ㉏ൟᯊˈᣛ䩜᠔ᣛᇍ䈵ⱘ㉏ൟⱘӏԩ㉏ൟ䰤ᅮヺ˄খ㾕 A.8.2 㡖˅䛑ᇚ㹿ᗑ ൟⱘᣛ䩜ᇚ㹿䕀ᤶЎᣛ৥ void ⱘᣛ䩜ˈ䖭гᰃ㒧ᵰⱘ㉏ൟDŽ བᵰϔϾ᪡԰᭄Ўᣛ৥ void ⱘᣛ䩜ˈ㗠঺ϔϾ᪡԰᭄Ўᣛ৥݊ᅗ㉏ൟⱘᣛ䩜ˈ߭ᣛ৥݊ᅗ㉏ ݊ЁϔϾ᪡԰᭄ᰃᣛ䩜ˈ㗠঺ϔϾᰃᐌ䞣 0ˈ߭ 0 ᇚ㹿䕀ᤶЎᣛ䩜㉏ൟˈϨ㒧ᵰЎᣛ䩜㉏ൟDŽ 㘨ড়ˈ៪㗙ᰃᣛ৥ৠϔ㉏ൟⱘᇍ䈵ⱘᣛ䩜ˈ߭㒧ᵰⱘ㉏ൟϢ䖭ϸϾ᪡԰᭄ⱘ㉏ൟⳌৠDŽབᵰ Ӏⱘ㉏ൟⳌৠˈ䆹㉏ൟгᰃ㒧ᵰⱘ㉏ൟDŽབᵰᅗӀ䛑ᰃ void ㉏ൟˈ៪㗙ᰃৠϔ㉏ൟⱘ㒧ᵘ៪ Ӯ㹿䅵ㅫDŽབᵰ㄀ѠϾ੠㄀ϝϾ᪡԰᭄Ўㅫᴃ㉏ൟˈ߭㽕䖯㸠᱂䗮ⱘㅫᴃ㉏ൟ䕀ᤶˈҹՓᅗ ѠϾ㸼䖒ᓣⱘؐˈ৺߭㒧ᵰЎ㄀ϝϾ㸼䖒ᓣⱘؐDŽ㄀ѠϾ੠㄀ϝϾ᪡԰᭄Ёҙ᳝ϔϾ᪡԰᭄ 䅵ㅫ㄀ϔϾ㸼䖒ᓣ˄ࣙᣀ᠔᳝ৃ㛑ⱘࡃ԰⫼˅ˈབᵰ䆹㸼䖒ᓣⱘؐϡㄝѢ 0ˈ߭㒧ᵰЎ㄀ܜ佪 ݧζӒ಴?ζӒ಴:ඨߑζӒ಴ހ০ ݧζӒ಴ހ০ ඨߑζӒ಴ġ A.7.16 ᴵӊ䖤ㅫヺ ㉏ൟDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ໄᯢ ぎ䯈ⱘໄᯢ⿄ЎᅮН˄definition˅DŽໄᯢⱘᔶᓣབϟ˖ټ乘⬭ᄬ ぎ䯈DŽټໄ ᯢ˄ declaration˅⫼Ѣ䇈ᯢ↣Ͼᷛ䆚ヺⱘ৿Нˈ㗠ᑊϡ䳔㽕Ў↣Ͼᷛ䆚ヺ乘⬭ᄬ A.8 ໄᯢ 䞣੠ᔎࠊ㉏ൟ䕀ᤶDŽ䆺㒚ֵᙃখ㾕 A.12.5 㡖DŽ 䆌 sizeof 㸼䖒ᓣǃᵮВᐌܕ䆌ߎ⦄೼#if ৢ䴶ⱘᭈൟᐌ䞣㸼䖒ᓣⱘ㣗ೈ䕗ᇣˈᅗϡܕ এϔϾᐌ䞣DŽޣࠡ䴶ໄᯢⱘ໪䚼៪䴭ᗕᇍ䈵ⱘഄഔࡴϞ៪ 䖤ㅫヺ&ᇚ㹿䱤ᓣഄᑨ⫼DŽ߱ؐ䅵ㅫⱘ㒧ᵰؐᖙ乏Ўϟ߫Ѡ㗙Пϔ˖ϔϾᐌ䞣˗ܗϔˈމ᭄ⱘᚙ ԰⫼Ѣ໪䚼ǃ䴭ᗕᇍ䈵ҹঞҹᐌ䞣㸼䖒ᓣЎϟᷛⱘ໪䚼៪䴭ᗕ᭄㒘DŽᇍѢ᮴ϟᷛⱘ᭄㒘៪ߑ 䖤ㅫヺ&ৃҹܗ䆌᳈໻ⱘ㣗ೈ˖᪡԰᭄ৃҹᰃӏᛣ㉏ൟⱘᐌ䞣ˈϔܕ߱ؐЁⱘᐌ䞣㸼䖒ᓣ ԩ㉏ൟⱘ᪡԰᭄DŽ˅ ℸ㾘߭ᇍ᭄㒘ǃ䯈᥹䆓䯂ǃপഄഔ䖤ㅫヺ੠㒧ᵘ៤ਬ᪡԰ϡ䗖⫼DŽ˄Ԛᰃˈsizeof ৃҹᏺӏ ヺ੠⍂⚍ᐌ䞣㒘៤˗ᔎࠊ㉏ൟ䕀ᤶᖙ乏ᣛᅮЎᭈൟˈӏԩ⍂⚍ᐌ䞣䛑ᇚ㹿ᔎࠊ䕀ᤶЎᭈൟDŽ ߑ᭄䇗⫼៪䗫ো䖤ㅫヺDŽབᵰ㽕∖ᐌ䞣㸼䖒ᓣЎᭈൟˈ߭ᅗⱘ᪡԰᭄ᖙ乏⬅ᭈൟǃᵮВǃᄫ 䖤ㅫヺǃޣ䰸њ԰Ў sizeof ⱘ᪡԰᭄П໪ˈᐌ䞣㸼䖒ᓣЁৃҹϡࣙ৿䌟ؐǃ㞾๲៪㞾 ᄫ↉ⱘ䭓ᑺǃᵮВᐌ䞣ⱘؐǃ߱ؐҹঞᶤѯ乘໘⧚఼㸼䖒ᓣDŽ ᶤѯϞϟ᭛㽕∖㸼䖒ᓣⱘؐЎᐌ䞣ˈ՟བˈswitch 䇁হЁ case ৢ䴶ⱘ᭄ؐǃ᭄㒘䖍⬠੠ԡ ඨߑζӒ಴ Ш२ζӒ಴ġ Ң䇁⊩Ϟⳟˈᐌ䞣㸼䖒ᓣᰃ䰤ᅮѢ䖤ㅫヺⱘᶤϔϾᄤ䲚ⱘ㸼䖒ᓣ˖ A.7.19 ᐌ䞣㸼䖒ᓣ ࣙ৿ 3 Ͼখ᭄ˈ݊Ё㄀ѠϾখ᭄ⱘؐЎ 5DŽ f(a, (t=3, t+2), c) ϟ߫ߑ᭄䇗⫼˖ 䖭ḋˈ䗫ো䖤ㅫヺҙߎ⦄೼೚ᣀোЁDŽ՟བˈˈܗ㡖˅Ёˈ䳔㽕Փ⫼䌟ؐ㸼䖒ᓣ԰Ў䇁⊩ऩ 䅵ㅫDŽ೼䗫ো᳝⡍߿৿НⱘϞϟ᭛Ёˈབ೼ߑ᭄খ᭄㸼˄খ㾕 A.7.3 㡖˅੠߱ؐ߫㸼˄A.8.7 ൟ੠ؐህᰃ㒧ᵰⱘ㉏ൟ੠ؐDŽ೼ᓔྟ䅵ㅫে᪡԰᭄ҹࠡˈᇚᅠ៤Ꮊ᪡԰᭄⍝ঞࠄⱘࡃ԰⫼ⱘ ⬅䗫োߚ䱨ⱘϸϾ㸼䖒ᓣⱘ∖ؐ⃵ᑣЎҢᎺࠄেˈᑊϨᎺ㸼䖒ᓣⱘؐ㹿϶ᓗDŽে᪡԰᭄ⱘ㉏ ζӒ಴, ؎ᄔζӒ಴ ؎ᄔζӒ಴ ζӒ಴ġ A.7.18 䗫ো䖤ㅫヺ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䆌˅DŽܕϡ 䖤ㅫヺ&˄ᰒᓣᑨ⫼៪䱤ᓣᑨ⫼䛑ܗབᵰϔϾᇍ䈵㹿ໄᯢЎ registerˈ߭ᇚϡ㛑ᇍᅗᑨ⫼ϔ ⱘᇍ䈵㹿ⳳℷᄬᬒ೼ᆘᄬ఼ЁˈᑊϨা᳝⡍ᅮ㉏ൟᠡৃҹDŽ䆹䰤ࠊৠ݋ԧⱘᅲ⦄᳝݇DŽԚᰃˈ Ѣᏺ᳝ auto 䇈ᯢヺⱘໄᯢˈ᠔ϡৠⱘᰃˈࠡ㗙ᱫ⼎њໄᯢⱘᇍ䈵ᇚ㹿乥㐕ഄ䆓䯂DŽা᳝ᕜᇥ ぎ䯈DŽᏺ᳝ register 䇈ᯢヺⱘໄᯢㄝӋټ᭄ЁDŽ䖭⾡ໄᯢг݋᳝ᅮНⱘ԰⫼ˈᑊᇚ乘⬭ᄬ ㉏ᇍ䈵ˈ䖭ѯᇍ䈵ҙৃ⫼೼ߑټ䇈ᯢヺ auto ੠ register ᇚໄᯢⱘᇍ䈵䇈ᯢЎ㞾ࡼᄬ ㉏ⱘᛣНˈ៥ӀᏆ೼ A.4 㡖Ё䅼䆎䖛DŽټ᳝݇ᄬ typedef extern static register auto ㉏䇈ᯢヺ˖ټᄬ ㉏䇈ᯢヺབϟ᠔⼎˖ټᄬ ㉏䇈ᯢヺټA.8.1 ᄬ 䆌ぎໄᯢDŽܕ៤ਬDŽϡ 乏㟇ᇥࣙ৿ϔϾໄᯢヺˈ៪㗙݊㉏ൟ䇈ᯢヺᖙ乏ໄᯢϔϾ㒧ᵘᷛ䆄ǃϔϾ㘨ড়ᷛ䆄៪ᵮВⱘ ໄᯢヺᇚ೼⿡ৢ䚼ߚ䅼䆎˄খ㾕 A.8.5 㡖˅DŽໄᯢヺࣙ৿њ㹿ໄᯢⱘৡᄫDŽϔϾໄᯢЁᖙ ໄᯢヺ = ߱ؐ ໄᯢヺ ߱ྟ࣪ໄᯢヺ˖ ߱ྥ࣪ໄᯢヺ㹕ˈ߱ྟ࣪ໄᯢヺ ߱ྟ࣪ໄᯢヺ ߱ྥ࣪ໄᯢヺ㸼˖ ㉏ൟ䰤ᅮヺ ໄᯢ䇈ᯢヺ opt ㉏ൟ䇈ᯢヺ ໄᯢ䇈ᯢヺ opt ㉏䇈ᯢヺ ໄᯢ䇈ᯢヺ optټᄬ ໄᯢ䇈ᯢヺ˖ 䇈ᯢヺ㒘៤DŽ ㉏ټ߱ྟ࣪ໄᯢヺ㸼Ёⱘໄᯢヺࣙ৿㹿ໄᯢⱘᷛ䆚ヺ˗ໄᯢ䇈ᯢヺ⬅ϔ㋏߫ⱘ㉏ൟ੠ᄬ ໄᯢ䇈ᯢヺ ߱ྟ࣪ໄᯢヺ㸼 opt; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䆌ᏺ signed ໄᯢˈԚ䖭ᰃܕᯢヺᇍѢᔎࠊ char ᇍ䈵ᏺヺোԡᰃ䴲ᐌ᳝⫼ⱘ˗݊ᅗᭈൟг ϟ咬䅸Ў int ㉏ൟDŽsigned 䇈މϔ䍋Փ⫼DŽsigned ੠ unsigned ৃҹऩ⣀Փ⫼ˈ䖭⾡ᚙ unsigned 䖭ϸϾ㉏ൟ䇈ᯢヺЁ᳔໮᳝ϔϾৃৠᯊϢ intǃint ⱘ short ៪ long ᔶᓣǃchar ϟⳕ⬹݇䬂ᄫ int ⱘ৿НгᰃϔḋⱘDŽlong ৃϢ double ϔ䍋Փ⫼DŽsigned ੠މ⾡ᚙ ݊Ёˈlong ੠ short 䖭ϸϾ㉏ൟ䇈ᯢヺЁ᳔໮᳝ϔϾৃৠᯊϢ int ϔ䍋Փ⫼ˈᑊϨˈ೼䖭 ㉏ൟᅮНৡ ᵮВ䇈ᯢヺ 㒧ᵘ៪㘨ড়䇈ᯢヺ unsigned signed double float long int short char void ㉏ൟ䇈ᯢヺ˖ ㉏ൟ䇈ᯢヺⱘᅮНབϟ˖ A.8.2 ㉏ൟ䇈ᯢヺ 㒚ֵᙃখ㾕 A.10 㡖੠ A.11 㡖DŽ extern ㉏ൟ˗೼ߑ᭄໪䚼ໄᯢⱘᇍ䈵Ϣߑ᭄ᇚ㹿䅸Ўᰃ static ㉏ൟˈϨ݋᳝໪䚼䖲᥹DŽ䆺 㾘߭䖯㸠˖೼ߑ᭄ݙ䚼ໄᯢⱘᇍ䈵㹿䅸Ўᰃ auto ㉏ൟ˗೼ߑ᭄ݙ䚼ໄᯢⱘߑ᭄㹿䅸Ўᰃ ㉏䇈ᯢヺˈ߭ᇚᣝ✻ϟ߫ټ㉏䇈ᯢヺDŽབᵰ≵᳝ᣛᅮᄬټϔϾໄᯢЁ᳔໮া㛑᳝ϔϾᄬ ⊩ᦣ䗄Ϟⱘᮍ֓DŽ៥Ӏᇚ೼ A.8.9 㡖Ё䅼䆎ᅗDŽ ㉏䇈ᯢヺˈᰃЎњ䇁ټぎ䯈DŽП᠔ҹᇚᅗ⿄Ўᄬټtypedef 䇈ᯢヺᑊϡӮЎᇍ䈵乘⬭ᄬ ヺ೼ߑ᭄໪䚼ⱘ԰⫼খ㾕 A.11.2 㡖DŽ ぎ䯈ᅮН೼݊ᅗഄᮍDŽ᳝݇䆹䇈ᯢټߑ᭄ݙ䚼ⱘ extern ໄᯢ㸼ᯢˈ㹿ໄᯢⱘᇍ䈵ⱘᄬ ໪䚼ⱘ԰⫼খ㾕 A.11.2 㡖DŽ ぎ䯈ⱘߚ䜡ˈ݋᳝ᅮНⱘ԰⫼DŽ᳝݇䆹䇈ᯢヺ೼ߑ᭄ټ䚼DŽ೼ߑ᭄ݙ䚼ˈ䆹䇈ᯢヺᇚᓩ䍋ᄬ ㉏DŽ䖭⾡ᇍ䈵ৃҹ⫼೼ߑ᭄ݙ䚼៪ߑ᭄໪ټ䇈ᯢヺ static ᇚໄᯢⱘᇍ䈵䇈ᯢЎ䴭ᗕᄬ 䖭ᰃϔϾᮄ๲ࡴⱘ㾘߭DŽ 䇈ᯢ˖ᇍໄᯢЎ register Ԛᅲ䰙ᣝ✻ auto ㉏ൟ໘⧚ⱘᇍ䈵ⱘഄഔ䖯㸠䅵ㅫᰃ䴲⊩ⱘDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䇈ᯢヺ䰤ᅮヺ㸼˖ 䇈ᯢヺ䰤ᅮヺ㸼 㒧ᵘໄᯢヺ㸼 㒧ᵘໄᯢ˖ 㒧ᵘໄᯢ㸼 㒧ᵘໄᯢ 㒧ᵘໄᯢ 㒧ᵘໄᯢ㸼 㒧ᵘໄᯢ㸼ᰃᇍ㒧ᵘ៪㘨ড়ⱘ៤ਬ䖯㸠ໄᯢⱘໄᯢᑣ߫˖ union struct 㒧ᵘ៪㘨ড়˖ 㒧ᵘ៪㘨ড় ᷛ䆚ヺ 㒧ᵘ៪㘨ড়ᷛ䆚ヺ opt {㒧ᵘໄᯢ㸼} 㒧ᵘ៪㘨ড়䇈ᯢヺ˖ Ͼϡৠ㉏ൟ៤ਬЁⱘӏᛣϔϾ៤ਬDŽ㒧ᵘ੠㘨ড়䇈ᯢヺ݋᳝ⳌৠⱘᔶᓣDŽ 㒧ᵘᰃ⬅ϡৠ㉏ൟⱘੑৡ៤ਬᑣ߫㒘៤ⱘᇍ䈵DŽ㘨ড়гᰃᇍ䈵ˈ೼ϡৠᯊࠏˈᅗࣙ৿໮ A.8.3 㒧ᵘ੠㘨ড়ໄᯢ ໪ˈ㓪䆥఼ৃ㛑Ӯᗑ⬹䖭ѯ䰤ᅮヺDŽމׂᬍ const ᇍ䈵ⱘᚙ ৥ volatile ⱘᣛ䩜ˈⳂⱘᰃ䰆ℶ㓪䆥఼䗮䖛ᣛ䩜ߴ䰸ᯢᰒ໮ԭⱘᓩ⫼DŽ䰸њ䆞ᮁᰒᓣᇱ䆩 䕧ܹˋ䕧ߎⱘᴎ఼ˈᣛ৥䆒໛ᆘᄬ఼ⱘᣛ䩜ৃҹໄᯢЎᣛڣབˈᇍѢ݋᳝ݙᄬ᯴צⱘӬ࣪DŽ ఼Ёⱘᇍ䈵ˈᑊৃ㛑ᦤ催Ӭ࣪ⱘৃ㛑ᗻDŽvolatile ⫼ѢᔎࠊᶤϾᅲ⦄ሣ㬑ৃ㛑ټ೼া䇏ᄬ ᮄ๲ࡴⱘ⡍ᗻDŽconst ⫼Ѣໄᯢৃҹᄬᬒޚ䇈ᯢ˖const ੠ volatile ሲᗻᰃ ANSI ᷛ ҹৢϡ㛑䖯㸠䌟ؐDŽvolatile ᇍ䈵≵᳝Ϣᅲ⦄᮴݇ⱘ䇁НDŽ ㉏ൟ䰤ᅮヺৃϢӏԩ㉏ൟ䇈ᯢヺϔ䍋Փ⫼DŽৃҹᇍ const ᇍ䈵䖯㸠߱ྟ࣪ˈԚ೼߱ྟ࣪ volatile const ㉏ൟ䰤ᅮヺ˖ ㉏ൟгৃҹ⫼䰤ᅮヺ䰤ᅮˈҹᣛᅮ㹿ໄᯢᇍ䈵ⱘ⡍⅞ሲᗻDŽ ㉏ൟ䇈ᯢヺˈ߭咬䅸Ў int ㉏ൟDŽ П໪ˈ೼ϔϾໄᯢЁ᳔໮া㛑Փ⫼ϔϾ㉏ൟ䇈ᯢヺDŽབᵰໄᯢЁ≵᳝މ䰸њϞ䴶䖭ѯᚙ ໮ԭⱘDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! さDŽϔϾ៤ਬৡᄫϡ㛑೼ৠϔ㒧ކさˈгϡӮϢ᱂䗮ব䞣ކ៤ਬ੠ᷛ䆄ⱘৡᄫϡӮⳌѦ 㹿ᅗ᠔೼ⱘໄᯢⳈ᥹ᓩ⫼DŽ ݋᳝㒧ᵘໄᯢ㸼㗠᮴ᷛ䆄ⱘ㒧ᵘ䇈ᯢヺ៪㘨ড়䇈ᯢヺ⫼Ѣ߯ᓎϔϾᚳϔⱘ㉏ൟˈᅗা㛑 ᔦ䇗⫼ⱘ㒧ᵘˈԚ䖭ѯ㒧ᵘⱘᷛ䆄ৃ㛑Ꮖ೼໪ሖ԰⫼ඳЁໄᯢDŽ 䇈ᯢ˖䖭ᰃ ANSI ЁϔϾᮄⱘ↨䕗䲒⧚㾷ⱘ㾘߭DŽᅗᮼ೼໘⧚ݙሖ԰⫼ඳЁໄᯢⱘⳌѦ䗦 ᔧࠡ԰⫼ඳݙϔϾᮄⱘϡᅠᭈ㉏ൟⱘ㒧ᵘᷛ䆄៪㘨ড়ⱘᷛ䆄DŽ ሖ԰⫼ඳЁᏆໄᯢ䖛ⱘ㒧ᵘᷛ䆄៪㘨ড়ⱘᷛ䆄˄খ㾕 A.11.1 㡖˅ˈ䆹ໄᯢҡᇚՓ䆹ᷛ䆚ヺ៤Ў 䖭⾡ᔶᓣⱘໄᯢໄᯢњϔϾ㒧ᵘ៪㘨ড়ˈԚᅗ≵᳝ໄᯢ㸼੠ໄᯢヺDŽेՓ䆹ᷛ䆚ヺᰃ໪ 㒧ᵘ៪㘨ড় ᷛ䆚ヺˈ ϟ߫ᔶᓣⱘໄᯢ䗖⫼ϔϾ䴲ᐌ⡍⅞ⱘ㾘߭˖ ᭈ㉏ൟⱘᣛ䩜ˈ᠔ҹˈ㒧ᵘ៪㘨ড়ৃࣙ৿ᣛ৥㞾䑿ᅲ՟ⱘᣛ䩜DŽ 䰸њৃҹੑৡ㒧ᵘ៪㘨ড়㉏ൟ໪ˈᷛ䆄䖬ৃҹ⫼ᴹᅮН㞾ᓩ⫼㒧ᵘDŽ⬅Ѣৃҹໄᯢᣛ৥ϡᅠ 㒧ᵘЁϡ㛑ࣙ৿ϡᅠᭈ㉏ൟⱘ៤ਬDŽ಴ℸˈϡ㛑ໄᯢࣙ৿㞾䑿ᅲ՟ⱘ㒧ᵘ៪㘨ড়DŽԚᰃˈ Ⳉࠄ㢅ᣀো“}ā㒜ℶ䆹䇈ᯢヺᯊˈໄᯢⱘ㉏ൟᠡ៤Ўᅠᭈ㉏ൟDŽ ೼ࣙ৿㒧ᵘໄᯢ㸼ⱘ䇈ᯢヺЁˈ೼䆹㒧ᵘໄᯢ㸼ݙໄᯢⱘ㒧ᵘ៪㘨ড়㉏ൟгᰃϡᅠᭈⱘˈϔ ৢˈབᵰ݋᳝䆹ᷛ䆄ⱘ䇈ᯢヺݡ⃵ߎ⦄ᑊࣙ৿ϔϾໄᯢ㸼ˈ߭䆹㉏ൟ៤Ўᅠᭈ㉏ൟDŽेՓᰃ 䆌DŽ೼ᓩ⫼Пܕ߭ϡމᅮНЁ˅ˈᅗৃ⫼Ѣ䇈ᯢϔϾᣛ䩜៪߯ᓎϔϾ typedef ㉏ൟˈ݊ԭᚙ ϡᅠᭈ㒧ᵘ៪㘨ড়㉏ൟⱘᇍ䈵ৃ೼ϡ䳔㽕ᇍ䈵໻ᇣⱘϞϟ᭛Ёᓩ⫼ˈ↨བˈ೼ໄᯢЁ˄ϡᰃ བᵰ䇈ᯢヺЁা᳝ᷛ䆄㗠᮴㒧ᵘໄᯢ㸼ˈᑊϨᷛ䆄≵᳝ໄᯢˈ߭䅸Ў㧕Ўϡᅠᭈ㉏ൟDŽ݋᳝ 㒧ᵘ៪㘨ড় ᷛ䆚ヺ 㸼˅ᴹᓩ⫼ৠϔ㉏ൟˈབϟ᠔⼎˖ ೼ৠϔ԰⫼ඳ៪ݙሖ԰⫼ඳЁⱘৢ㓁ໄᯢЁˈৃҹ೼䇈ᯢヺЁՓ⫼ᷛ䆄˄㗠ϡՓ⫼㒧ᵘໄᯢ 㒧ᵘ៪㘨ড়ᷛ䆚ヺ {㒧ᵘໄᯢ㸼} ϟ߫ᔶᓣⱘ㉏ൟ䇈ᯢヺᇚ݊Ёⱘᷛ䆚ヺໄᯢЎ㒧ᵘໄᯢ㸼ᣛᅮⱘ㒧ᵘ៪㘨ড়ⱘᷛ䆄˖ ໄᯢヺ opt: ᐌ䞣㸼䖒ᓣ ໄᯢヺ 㒧ᵘໄᯢヺ˖ 䖭⾡៤ਬ⿄Ўԡᄫ↉ˈ៪ҙ⿄Ўᄫ↉ˈ݊䭓ᑺ⬅䎳೼ໄᯢヺݦোПৢⱘᐌ䞣㸼䖒ᓣᣛᅮDŽ 䗮ᐌˈ㒧ᵘໄᯢヺህᰃ㒧ᵘ៪㘨ড়៤ਬⱘໄᯢヺDŽ㒧ᵘ៤ਬгৃ㛑⬅ᣛᅮ᭄Ⳃⱘ↨⡍ԡ㒘៤ˈ 㒧ᵘໄᯢヺ㸼, 㒧ᵘໄᯢヺ 㒧ᵘໄᯢヺ 㒧ᵘໄᯢヺ㸼˖ ㉏ൟ䰤ᅮヺ 䇈ᯢヺ䰤ᅮヺ㸼 opt ㉏ൟ䇈ᯢヺ 䇈ᯢヺ䰤ᅮヺ㸼 opt w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᓩ⫼ sp ᣛ৥ⱘ㒧ᵘⱘ count ᄫ↉ˈ㗠 sp•>count Ϟˈ㸼䖒ᓣ ᇚᡞ s ໄᯢЎ㒭ᅮ㉏ൟⱘ㒧ᵘˈᡞ sp ໄᯢЎᣛ৥㒭ᅮ㉏ൟⱘ㒧ᵘⱘᣛ䩜DŽ೼䖭ѯໄᯢⱘ෎⸔ struct tnode s, *sp; 䖭ḋⱘໄᯢৢˈϟ߫䇈ᯢ˖ 䆹㒧ᵘࣙ৿ϔϾ݋᳝ 20 Ͼᄫヺⱘ᭄㒘ǃϔϾᭈ᭄ҹঞϸϾᣛ৥㉏Ԑ㒧ᵘⱘᣛ䩜DŽ೼㒭ߎ } struct tnode *right; struct tnode *left; int count; char tword[20]; struct tnode { བϟ᠔⼎ᰃ㒧ᵘໄᯢⱘϔϾㅔऩ՟ᄤ˖ ៤ਬⱘᣛ䩜㉏ൟˈ߭㒧ᵰᇚᣛ৥䆹៤ਬDŽ ЁⱘϔϾ៤ਬDŽབᵰᣛ৥ᶤϔ㘨ড়ⱘᣛ䩜㹿ᔎࠊ䕀ᤶЎᣛ৥ϔϾ݊ټӏϔᯊࠏᅗ᳔໮া㛑ᄬ 䞣䛑Ў 0ˈᑊϨ݊໻ᇣ䎇ҹᆍ㒇ӏԩ៤ਬDŽ⿏أ㘨ড়ৃҹ㹿ⳟ԰Ў㒧ᵘˈ݊᠔᳝៤ਬ䍋ྟ 㒧ᵘ㄀ϔϾ៤ਬⱘᣛ䩜㉏ൟˈ߭㒧ᵰᇚᣛ৥䆹㒧ᵘⱘ㄀ϔϾ៤ਬDŽ ⬠Ϟᇍ唤ˈ಴ℸˈ೼㒧ᵘЁৃ㛑ᄬ೼᮴ৡぎえDŽ㢹ᣛ৥ᶤϔ㒧ᵘⱘᣛ䩜㹿ᔎࠊ䕀ᤶЎᣛ৥䆹 㒧ᵘ៤ਬⱘഄഔؐᣝᅗӀໄᯢⱘ乎ᑣ䗦๲DŽ䴲ᄫ↉㉏ൟⱘ㒧ᵘ៤ਬḍ᥂݊㉏ൟ೼ഄഔ䖍 ϟˈᖙ乏њ㾷ᴀഄᅲ⦄ⱘϔѯ㾘߭DŽމᏗሔˈԚ䆹ᮍ⊩ϡৃ⿏ỡˈ೼䖭⾡ᚙټϞᦣ䗄ᄬ ぎ䯈˄ҷӋᰃ๲ࡴњᣛҸぎ䯈੠䆓䯂ᄫ↉ⱘᯊ䯈˅ˈৠᯊˈᅗ䖬ৃҹ⫼ᴹ೼ԡሖ⃵ټᴹ㡖ⳕᄬ ᄫ↉ˈᓎ䆂䯙䇏ϔϟ䆹䇁㿔㾘߭DŽ԰Ўϔ⾡ৃ⿏ỡⱘᮍ⊩ˈᏺᄫ↉ⱘ㒧ᵘৃ⫼ټ݇ⱘᮍᓣᄬ ㄀ 1 ⠜᳈ձ䌪Ѣ݋ԧⱘᅲ⦄DŽབᵰ㽕ᣝ✻Ϣᅲ⦄Ⳍ↨ޚ䇈ᯢ˖೼ᄫ↉໘⧚ᮍ䴶ˈANSI ᷛ DŽټⱘ䖍⬠ᓔྟᄬܗऩ Ң㗠Փᕫϟϔᄫ↉Ңϟϔߚ䜡ˈܙ฿⾡DŽ៥Ӏৃҹ⫼ᆑᑺЎ 0 ⱘ᮴ৡᄫ↉ᴹᔎࠊ䖯㸠䖭ܙ฿ Ёⱘ࠽ԭ䚼ߚгৃ㛑㹿ܗऩټЁˈ៪㗙ᰃˈᄬܗऩټЁˈ߭ᅗৃ㛑Ӯ㹿ߚࡆᄬᬒࠄ໮Ͼᄬܗ ऩټԧⱘᅲ⦄᳝݇˅DŽབᵰᶤᄫ↉Пৢⱘ঺ϔᄫ↉᮴⊩ܼ䚼ᄬܹᏆ㹿ࠡ䴶ⱘᄫ↉䚼ߚऴ⫼ⱘᄬ Ё˄ৠ݋ܗऩټ᳝݇DŽ㒧ᵘⱘⳌ䚏ᄫ↉៤ਬҹᶤ⾡࡯ᓣ˄ৠ݋ԧⱘᅲ⦄᳝݇˅ᄬᬒ೼ᶤѯᄬ ᅮ䭓ᑺ˄⫼Ѡ䖯ࠊԡ㸼⼎˅ⱘᭈൟᇍ䈵ˈint ㉏ൟⱘᄫ↉ᰃ৺ⳟ԰Ў᳝ヺো᭄ৠ݋ԧⱘᅲ⦄ ໄᯢヺˈ಴ℸৃҹϡੑৡ˅ⱘ㉏ൟЎ intǃunsigned int ៪ signed intˈᑊ㹿㾷䞞Ўᣛ 䰸ᄫ↉㉏ൟⱘ៤ਬ໪ˈ㒧ᵘ៤ਬ៪㘨ড়៤ਬৃҹЎӏᛣᇍ䈵㉏ൟDŽᄫ↉៤ਬ˄ᅗϡ䳔㽕 ࠊᅮࠡˈ䖭⾡݇㘨೼㓪䆥఼Ё᱂䘡ᄬ೼DŽ ޚ䇈ᯢ˖೼ᴀкⱘ㄀ 1 ⠜Ёˈ㒧ᵘ៪㘨ড়ⱘ៤ਬৡϢ݊⠊䕜᮴݇㘨DŽԚᰃˈ೼ ANSI ᷛ ᵘ៪㘨ড়Ёߎ⦄ϸ⃵ˈԚⳌৠⱘ៤ਬৡᄫৃ⫼೼ϡৠⱘ㒧ᵘ៪㘨ড়ЁDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䡈њ㒧ᵘ䇈ᯢヺ੠㘨ড়䇈ᯢヺⱘᔶᓣDŽ׳ヺDŽᵮВ䇈ᯢヺⱘᔶᓣ ᵮВ㉏ൟᰃϔ⾡⡍⅞ⱘ㉏ൟˈᅗⱘؐࣙ৿೼ϔϾੑৡⱘᐌ䞣䲚ড়ЁDŽ䖭ѯᐌ䞣⿄ЎᵮВ A.8.4 ᵮВ ... sin(u.nf.floatnode) ... if (u.n.type == FLOAT) ... u.nf.floatnode = 3.14; u.nf.type = FLOAT; ... } u; } nf; float floatnode; int type; struct { } ni; int intnode; int type; struct { } n; int type; struct { union { ՟བˈϟ䴶䖭↉⿟ᑣᰃড়⊩ⱘ˖ 䆌ᓩ⫼䖭ѯ㒧ᵘЁӏϔ㒧ᵘⱘ݀݅߱ྟ䚼ߚDŽܕᑊϨ䆹㘨ড়ᔧࠡࣙ৿䖭ѯ㒧ᵘЁⱘᶤϔϾˈ߭ ҹㅔ࣪㘨ড়ⱘՓ⫼˖བᵰϔϾ㘨ড়ࣙ৿݅ѿϔϾ݀݅߱ྟᑣ߫ⱘ໮Ͼ㒧ᵘˈৃމϾ⡍⅞ⱘᚙ ϟˈ៥Ӏ᮴⊩Ẕᶹ㘨ড়ⱘᶤϔ៤ਬˈ䰸䴲Ꮖ⫼䆹៤ਬ㒭㘨ড়䌟ؐDŽԚᰃˈ᳝ϔމ䗮ᐌᚙ ᓩ⫼ s েᄤᷥЁ tword ៤ਬⱘ㄀ϔϾᄫヺDŽ s.right•>tword[0] ߭ᓩ⫼㒧ᵘⱘᎺᄤᷥᣛ䩜ˈ㸼䖒ᓣ s.left w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! Ⳉ᥹ໄᯢヺ(ᷛ䆚㸼 opt) Ⳉ᥹ໄᯢヺ(ᔶᓣখ᭄㉏ൟ㸼) Ⳉ᥹ໄᯢヺ [ᐌ䞣㸼䖒ᓣ opt] (ໄᯢヺ) ᷛ䆚ヺ Ⳉ᥹ໄᯢヺ ᣛ䩜 opt Ⳉ᥹ໄᯢヺ ໄᯢヺ˖ ໄᯢヺⱘ䇁⊩བϟ᠔⼎ A.8.5 ໄᯢヺ ᑈњDŽ 䇈ᯢ˖ⳌᇍѢᴀк㄀ 1 ⠜ˈᵮВ㉏ൟᰃϔϾᮄὖᗉˈԚᅗ԰Ў C 䇁㿔ⱘϔ䚼ߚᏆ᳝ད໮ Ё঺ϔϾ݋᳝ᵮВヺ㸼ⱘ䇈ᯢヺDŽ ϟⱘ㾘߭Ϣ㒧ᵘ៪㘨ড়ЁⳌᑨⱘ㾘߭ⳌৠDŽ᮴ᵮВヺ㸼ⱘᵮВ䇈ᯢヺⱘᷛ䆄ᖙ乏ᣛ৥԰⫼ඳ މᵮВ㉏ൟDŽ䰸њϡᄬ೼ϡᅠᭈᵮВ㉏ൟП໪ˈᵮВ䇈ᯢヺ೼᳝᮴ᷛ䆄ǃ᳝᮴ᵮВヺ㸼ⱘᚙ ᵮВ䇈ᯢヺЁᷛ䆚ヺⱘ԰⫼Ϣ㒧ᵘ䇈ᯢヺЁ㒧ᵘᷛ䆄ⱘ԰⫼㉏ԐˈᅗੑৡњϔϾ⡍ᅮⱘ ⳌৠDŽ ৠϔ԰⫼ඳЁⱘ৘ᵮВヺⱘৡᄫᖙ乏ѦϡⳌৠˈгϡ㛑Ϣ᱂䗮ব䞣ৡⳌৠˈԚ݊ؐৃҹ ྟձ⃵䗦๲DŽ བᵰ݊Ёࣙᣀᏺ᳝=ⱘᵮВヺˈ߭䆹ᵮВヺⱘؐ⬅䆹㸼䖒ᓣᣛᅮˈ݊ৢⱘᷛ䆚ヺⱘؐҢ䆹ؐᓔ བᵰ݊Ёϡࣙᣀᏺ᳝=ⱘᵮВヺˈ߭Ⳍᑨᐌ䞣ؐҢ 0 ᓔྟˈϨᵮВᐌ䞣ؐҢᎺ㟇েձ⃵䗦๲ 1DŽ ᵮВヺ㸼Ёⱘᷛ䆚ヺໄᯢЎ int ㉏ൟⱘᐌ䞣ˈᅗӀৃҹ⫼೼ᐌ䞣ৃҹߎ⦄ⱘӏԩഄᮍDŽ ᷛ䆚ヺ = ᐌ䞣㸼䖒ᓣ ᷛ䆚ヺ ᵮВヺ˖ ᵮВヺ㸼, ᵮВヺ ᵮВヺ ᵮВヺ㸼˖ enum ᷛ䆚ヺ enum ᷛ䆚ヺ opt {ᵮВヺ㸼} ᵮВ䇈ᯢヺ˖ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! int i, *pi, *const cpi = &i; ៥Ӏᴹⳟ঺໪ϔϾ՟ᄤDŽϟ߫ໄᯢ˖ ৥ int ㉏ൟⱘᣛ䩜᭄㒘ā㉏ൟDŽ ㉏ൟ䰤ᅮヺ㸼ЎぎˈϨ㉏ൟׂ佄ヺЎ“……ⱘ᭄㒘āDŽ಴ℸˈ䆹ໄᯢᅲ䰙Ϟᇚᡞ ap ໄᯢЎĀᣛ ݊Ёˈap[]ⱘ԰⫼ㄝӋѢ D1ˈໄ ᯢ“int ap[] āᇚᡞ ap ⱘ㉏ൟໄᯢЎ“int ㉏ൟⱘ᭄㒘āˈ int *ap[]; ՟བDŽ㗗㰥ϟ߫ໄᯢ˖ 䈵DŽ ൟ䰤ᅮヺ㸼ᣛ৥ T ⱘᣛ䩜āDŽ᯳ো*ৢⱘ䰤ᅮヺ԰⫼Ѣᣛ䩜ᴀ䑿ˈ㗠ϡᰃ԰⫼Ѣᣛ䩜ᣛ৥ⱘᇍ Ϩໄᯢ T D1 Ёⱘᷛ䆚ヺⱘ㉏ൟЎĀ㉏ൟׂ佄ヺ Tāˈ߭ D Ёᷛ䆚ヺⱘ㉏ൟЎĀ㉏ൟׂ佄ヺ ㉏ * ㉏ൟ䰤ᅮヺ㸼 D1 ೼ໄᯢ T D Ёˈབᵰ D ݋᳝ϟ߫ᔶᓣ˖ 1ˈᣛ䩜ໄᯢヺ ߭ D1 Ёᷛ䆚ヺⱘ㉏ൟϢ D ⱘ㉏ൟⳌৠDŽ೚ᣀোϡᬍব㉏ൟˈԚৃᬍব໡ᴖໄᯢヺП䯈ⱘ㒧ড়DŽ (D1) ೼ໄᯢ T D Ёˈབᵰ D ⱘᔶᓣЎ˖ ೼ໄᯢ T D Ёˈབᵰ D ᰃϔϾϡࡴӏԩ䰤ᅮⱘᷛ䆚ヺˈ߭䆹ᷛ䆚ヺⱘ㉏ൟЎ TDŽ 䖭⾡ᔶᓣᴹ㸼䗄DŽ “T D āⱘᔶᓣˈ݊Ё T ҷ㸼㉏ൟˈD ҷ㸼ໄᯢヺDŽ೼ϡৠᔶᓣⱘໄᯢЁˈᷛ䆚ヺⱘ㉏ൟৃ⫼ བᵰা㗗㰥ໄᯢ䇈ᯢヺ˄খ㾕 A.8.2 㡖˅ⱘ㉏ൟ䚼ߚঞ⡍ᅮⱘໄᯢヺˈ߭ໄᯢৃҹ㸼⼎Ў 䆹ໄᯢヺᇚ㹿԰ЎϔϾᮁ㿔ˈ݊㒧ᵰᇚѻ⫳ϔϾᣛᅮ㉏ൟⱘᇍ䈵DŽ ᅮDŽᔧໄᯢヺⱘᷛ䆚ヺߎ⦄೼Ϣ䆹ໄᯢヺᔶᓣⳌৠⱘ㸼䖒ᓣЁᯊˈއԚ݊㉏ൟ⬅ໄᯢヺⱘᔶᓣ ㉏䇈ᯢヺৃⳈ᥹԰⫼Ѣ䆹ᷛ䆚ヺˈټ䆚ヺˈ䆹ᷛ䆚ヺᰃⳈ᥹ໄᯢヺѻ⫳ᓣⱘ㄀ϔϾ׭䗝ᓣDŽᄬ ㉏䇈ᯢヺᑣ߫ПৢDŽ↣ϾໄᯢヺໄᯢϔϾⴶϔⱘЏᷛټໄᯢヺ㸼ߎ⦄೼㉏ൟ䇈ᯢヺ੠ᄬ A.8.6 ໄᯢヺⱘ৿Н ໄᯢヺⱘ㒧ᵘϢ䯈᥹ᣛ䩜ǃߑ᭄ঞ᭄㒘㸼䖒ᓣⱘ㒧ᵘ㉏Ԑˈ㒧ড়ᗻгⳌৠDŽ ㉏ൟ䰤ᅮヺ㸼 ㉏ൟ䰤ᅮヺ ㉏ൟ䰤ᅮヺ ㉏ൟ䰤ᅮヺ㸼 * ㉏ൟ䰤ᅮヺ㸼 opt ᣛ䩜 * ㉏ൟ䰤ᅮヺ㸼 opt ᣛ䩜˖ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! D1(ᔶᓣখ᭄㉏ൟ㸼) ೼ᮄᓣⱘߑ᭄ໄᯢ T D Ёˈབᵰ D ݋᳝ϟ߫ᔶᓣ˖ 3 ߑ᭄ໄᯢヺ ԰⫼DŽ ऎ໻ᇣˈԚ㄀ϔ㓈ϟᷛ೼ϟᷛ䅵ㅫᯊ᮴݊ᅗټᅮ᭄㒘᠔䳔ⱘᄬއᖿ˅ˈϨໄᯢЁⱘ㄀ϔ㓈ϟᷛ ᳔ৢϔ㓈ϟᷛবࡼ᳔˄ټࡴ⊩䖤ㅫ䳔㽕Ьҹᭈൟ㉏ൟⱘ䭓ᑺDŽᅗ䙉ᕾϟ߫㾘߭˖᭄㒘ᣝ㸠ᄬ ᣝ✻ A.7.1 㡖Ёⱘ㾘߭䕀ᤶЎ“ᣛ৥ᭈൟ᭄㒘ⱘᣛ䩜”㉏ൟˈ㗠ḍ᥂ A.7.7 㡖Ёⱘ㾘߭ˈ䖭䞠ⱘ ೼ᴀ՟Ёˈx3d[i][j][k]ㄝӋѢ*(x3d[i][j]+k)DŽ㄀ϔϾᄤ㸼䖒ᓣ x3d[i][j]ᇚ A.7.1 㡖Ϣ A.7.7 㡖˅ˈ㢹 E1 ᰃ᭄㒘Ϩ E2 ᰃᭈ᭄ˈ߭ E1[E2]ҷ㸼 E1 ⱘ㄀ E2 Ͼ៤ਬDŽ ϡᇍ⿄ˈԚϟᷛ䖤ㅫᰃৃѸᤶⱘ䖤ㅫDŽḍ᥂䗖⫼Ѣ䖤ㅫヺ+੠᭄㒘ⱘ䕀ᤶ㾘߭˄খ㾕 A.6.6 㡖ǃ ḍ᥂᭄㒘ϟᷛ䖤ㅫⱘᅮНˈE1[E2]ㄝӋѢ*(E1+E2)DŽ಴ℸˈሑㅵ㸼䖒ᓣⱘᔶᓣⳟϞএ ⱘ᭄㒘DŽ ㋴ܗ㋴জᰃϔϾ݋᳝ 7 Ͼᭈൟܗ㋴ⱘ᭄㒘ˈ㗠݊Ёⱘ↣Ͼܗ㋴ⱘ᭄㒘˗x3d[i]߭ᰃ᳝ 5 Ͼܗ ⹂ഄ䇈ˈx3d[i][j]ᰃϔϾ᳝ 7 ϾᭈൟޚЁDŽࠡϝ㗙ᰃ᭄㒘㉏ൟˈ᳔ৢϔϾᰃ int ㉏ൟDŽ᳈ ៤ⱘ᭄㒘DŽx3dǃx3d[i]ǃx3d[i][j]Ϣ x3d[i][j][k]䛑ৃҹড়⊩ഄߎ⦄೼ϔϾ㸼䖒ᓣ ᭄㒘ˈ↣Ͼ乍䛑ᰃ⬅ 5 Ͼ᭄㒘㒘៤ⱘϔϾ᭄㒘ˈ5 Ͼ᭄㒘Ёⱘ↣Ͼ᭄㒘জ䛑ᰃ⬅ 7 Ͼᭈൟ᭄㒘 ߭ໄᯢњϔϾ䴭ᗕⱘϝ㓈ᭈൟ᭄㒘ˈ݊໻ᇣЎ 3×5×7DŽ݋ԧᴹ䇈ˈx3d ᰃϔϾ⬅ 3 Ͼ乍㒘៤ⱘ static int x3d[3][5][7]; ໄᯢњϔϾ⍂⚍᭄᭄㒘੠ϔϾᣛ৥⍂⚍᭄ⱘᣛ䩜᭄㒘ˈ㗠 float fa[17], *afp[17]; 㡖˅ᴹՓ݊ᅠᭈDŽ՟བ˖ ᴹ䇈ˈ݊㉏ൟৃҹ䗮䖛ᇍ䆹ᇍ䈵䖯㸠঺ϔϾᅠᭈໄᯢ˄খ㾕 A.10.2 㡖˅៪߱ྟ࣪˄খ㾕 A.8.7 ៪㒧ᵘDŽгህᰃ䇈ˈᇍѢ໮㓈᭄㒘ᴹ䇈ˈা᳝㄀ϔ㓈ৃҹ㔎ⳕDŽᇍѢϡᅠᭈ᭄㒘㉏ൟⱘᇍ䈵 ᵘ䗴㗠៤˄⫳៤໮㓈᭄㒘˅DŽᵘ䗴᭄㒘ⱘ㉏ൟᖙ乏ᰃᅠᭈ㉏ൟˈ㒱ᇍϡ㛑ᰃϡᅠᭈ㉏ൟⱘ᭄㒘 ᭄㒘ৃҹ⬅ㅫᴃ㉏ൟǃᣛ䩜㉏ൟǃ㒧ᵘ㉏ൟ៪㘨ড়㉏ൟᵘ䗴㗠៤ˈгৃҹ⬅঺ϔϾ᭄㒘 㒘Ϟ⬠ⱘᐌ䞣㸼䖒ᓣˈ߭䆹᭄㒘㉏ൟᰃϡᅠᭈ㉏ൟDŽ ㉏ൟⱘ᭄㒘āDŽབᵰᄬ೼ᐌ䞣㸼䖒ᓣˈ߭䆹ᐌ䞣㸼䖒ᓣᖙ乏ЎᭈൟϨؐ໻Ѣ 0DŽ㢹㔎ᇥᣛᅮ᭄ Ϩໄᯢ T D1 Ёᷛ䆚ヺⱘ㉏ൟᰃĀ㉏ൟׂ佄ヺ Tāˈ߭ D ⱘᷛ䆚ヺ㉏ൟЎĀ㉏ൟׂ佄ヺ T D1[ᐌ䞣㸼䖒ᓣ opt] ೼ໄᯢ T D Ёˈབᵰ D ݋᳝ϟ߫ᔶᓣ˖ 2 ᭄㒘ໄᯢヺ ϔϾഄᮍˈԚᅗ᠔ᣛП໘ⱘؐϡ㛑䗮䖛 pci 䌟ؐᴹᬍবˈ བᴀ՟Ё᠔⼎˅DŽpci ⱘ㉏ൟᰃĀᣛ৥ const int ⱘᣛ䩜āˈpci ᴀ䑿ৃҹ㹿ׂᬍҹᣛ৥঺ ᣛ৥ৠϔԡ㕂ˈԚᅗ᠔ᣛП໘ⱘؐৃҹᬍবDŽᭈൟ ci ᰃᐌ䞣ˈгϡ㛑ׂᬍ˄ৃҹ䖯㸠߱ྟ࣪ˈ ໄᯢњϔϾᭈൟ i ੠ϔϾᣛ৥ᭈൟⱘᣛ䩜 piDŽϡ㛑ׂᬍᐌ䞣ᣛ䩜 cpi ⱘؐˈ䆹ᣛ䩜ᘏᰃ const int ci = 3, *pci; w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ໄᯢњϔϾ䖨ಲᭈൟؐⱘߑ᭄ fǃϔϾ䖨ಲᣛ৥ᭈൟⱘᣛ䩜ⱘߑ᭄ fpi ҹঞϔϾᣛ৥䖨ಲᭈ int f(), *fpi(), (*pfi)(); ՟བˈϟ߫ໄᯢ˖ A.10.1 㡖˅DŽໄᯢϡᦤկ᳝݇ᔶᓣখ᭄㉏ൟⱘֵᙃDŽ ೼ᮻᓣⱘໄᯢヺЁˈ䰸䴲೼ߑ᭄ᅮНⱘࠡ䴶Փ⫼њໄᯢヺˈ৺߭ˈᷛ䆚ヺ㸼ᖙ乏ぎ㔎˄খ㾕 ᷛ䆚ヺ㸼, ᷛ䆚ヺ ᷛ䆚ヺ ᷛ䆚ヺ㸼˖ ؐϨ᳾ᣛᅮখ᭄ⱘþ㉏ൟׂ佄ヺÿ㉏ൟⱘߑ᭄āDŽᔶᓣখ᭄˄བᵰ᳝ⱘ䆱˅ⱘᔶᓣབϟ˖ ᑊϨໄᯢ D1 Ёⱘᷛ䆚ヺⱘ㉏ൟᰃĀ㉏ൟׂ佄ヺ Tāˈ߭ D ⱘᷛ䆚ヺ㉏ൟЎĀ䖨ಲ T ㉏ൟ D1(ᷛ䆚ヺ㸼 opt) ೼ᮻᓣⱘߑ᭄ໄᯢ T D Ёˈབᵰ D ݋᳝ϟ߫ᔶᓣ˖ ⱘᢑ䈵ໄᯢヺᇚ೼ A.8.8 㡖Ё䅼䆎DŽ ࣙ৿ᷛ䆚ヺˈϨߑ᭄ᅮНⱘᓔ༈≵᳝ߑ᭄ໄᯢヺˈ߭䆹ᷛ䆚ヺ䍙ߎњ԰⫼ඳDŽϡ⍝ঞᷛ䆚ヺ ㉏䇈ᯢヺᇚ㹿ᗑ⬹DŽ㉏Ԑഄˈབᵰᔶᓣখ᭄ໄᯢЁⱘໄᯢヺټ༈ࣙᣀߑ᭄ໄᯢヺˈ৺߭䆹ᄬ ㉏䇈ᯢヺᰃ registerˈᑊϨˈ䰸䴲ߑ᭄ᅮНⱘᓔټ䆌ⱘᄬܕᣛ䩜DŽᔶᓣখ᭄ⱘໄᯢЁᚳϔ བᵰᔶᓣখ᭄㉏ൟᰃ᭄㒘៪ߑ᭄ˈᣝ✻খ᭄䕀ᤶ㾘߭˄খ㾕 A.10.1 㡖˅ˈᅗӀᇚ㹿䕀ᤶЎ 㽕໮DŽ䆺㒚ֵᙃখ㾕 A.7.3 㡖DŽ ᭄㸼ҹⳕ⬹ো“, ...ā㒧ሒˈ߭䆹ߑ᭄ৃ᥹ফⱘᅲ䰙খ᭄Ͼ᭄↨ᰒᓣ䇈ᯢⱘᔶᓣখ᭄Ͼ᭄ ໄᯢⱘ᮴ᔶᓣখ᭄ߑ᭄ⱘໄᯢヺг᳝ϔϾᔶᓣখ᭄㸼ˈ䆹㸼ҙࣙ৿݇䬂ᄫ voidDŽབᵰᔶᓣখ ೼䖭⾡ᮄᓣⱘໄᯢЁˈᔶᓣখ᭄㸼ᣛᅮњᔶᓣখ᭄ⱘ㉏ൟDŽ䖭䞠᳝ϔϾ⡍՟ˈᣝ✻ᮄᓣᮍᓣ ໄᯢ䇈ᯢヺ ᢑ䈵ໄᯢヺ opt ໄᯢ䇈ᯢヺ ໄᯢヺ ᔶᓣখ᭄ໄᯢ˖ ᔶᓣখ᭄㸼, ᔶᓣখ᭄ໄᯢ ᔶᓣখ᭄ໄᯢ ᔶᓣখ᭄㸼˖ ᔶᓣখ᭄㸼, ... ᔶᓣখ᭄㸼 ᔶᓣখ᭄㉏ൟ㸼˖ ᔶᓣখ᭄ⱘ䇁⊩ᅮНЎ˖ Ϩ݋᳝þᔶᓣখ᭄㉏ൟ㸼ÿЁⱘখ᭄ⱘþ㉏ൟׂ佄ヺÿ㉏ൟⱘߑ᭄āDŽ ᑊϨˈໄᯢ T D1 Ёᷛ䆚ヺⱘ㉏ൟЎĀ㉏ൟׂ佄ヺ Tāˈ߭ D ⱘᷛ䆚ヺ㉏ൟᰃĀ䖨ಲ T ㉏ൟؐ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᳾ᰒᓣ߱ྟ࣪ⱘ㞾ࡼᇍ䈵ⱘ߱ྟؐ≵᳝ᅮНDŽ ᳾ᰒᓣ߱ྟ࣪ⱘ䴭ᗕᇍ䈵ᇚ㹿䱤ᓣ߱ྟ࣪ˈ݊ᬜᵰㄝৠѢᅗ˄៪ᅗⱘ៤ਬ˅㹿䌟ҹᐌ䞣 0DŽ 䗮䖛ᐌ䞣㒧ᵘ䖯㸠߱ྟ࣪ˈ䰸䴲߱ؐৃҹ䗮䖛ㅔऩ㸼䖒ᓣ㸼⼎ߎᴹDŽ 䆌ⱘˈԚা㛑ܕᰃޚ䇈ᯢ˖㄀ 1 ⠜ϡᬃᣕ㞾ࡼ㒧ᵘǃ㘨ড়៪᭄㒘ⱘ߱ྟ࣪DŽ㗠 ANSI ᷛ ߭ᅗϡᖙᰃᐌ䞣㸼䖒ᓣˈԚᖙ乏ヺড়ᇍ䈵䌟ؐⱘ㉏ൟ㽕∖DŽ ؐЁⱘ㸼䖒ᓣгৠḋᖙ乏ᰃᐌ䞣㸼䖒ᓣDŽԚᰃˈབᵰ㞾ࡼᇍ䈵ⱘ߱ؐᰃϔϾऩϾⱘ㸼䖒ᓣˈ བᵰ߱ؐᰃ⫼㢅ᣀোᣀ䍋ᴹⱘ߱ؐ㸼ˈ߭ᇍ auto ៪ register ㉏ൟⱘᇍ䈵៪᭄㒘ᴹ䇈ˈ߱ ᇍ䴭ᗕᇍ䈵៪᭄㒘㗠㿔ˈ߱ؐЁⱘ᠔᳝㸼䖒ᓣᖙ乏ᰃ A.7.19 㡖Ёᦣ䗄ⱘᐌ䞣㸼䖒ᓣDŽ ߱ؐ㸼, ߱ؐ ߱ؐ ߱ؐ㸼˖ {߱ؐ㸼,} {߱ؐ㸼} 䌟ؐ㸼䖒ᓣ ߱ؐ˖ ৃҹՓḐᓣㅔ⋕㕢㾖DŽ ৃҹᰃϔϾ㸼䖒ᓣˈгৃҹᰃጠ༫೼㢅ᣀোЁⱘ߱ؐᑣ߫DŽ߱ؐᑣ߫ৃҹҹ䗫ো㒧ᴳˈ䖭ḋ ໄᯢᇍ䈵ᯊˈᇍ䈵ⱘ߱ྟ࣪ໄᯢヺৃЎ݊ᣛᅮϔϾ߱ྟؐDŽ߱ؐ㋻䎳೼䖤ㅫヺ=Пৢˈᅗ A.8.7 ߱ྟ࣪ 䖭ѯ㸼⼎⊩䍋⑤Ѣ C++DŽ ᮍϞ⽕ℶⱘˈԚৃ䴲ℷᓣഄՓ⫼DŽ ༈᭛ӊЁⱘϔѯᅣˈ݅ৠᇚ䖭Ͼᴎࠊℷᓣ࣪њDŽ䆹ᴎࠊ೼㄀ 1 ⠜Ёᰃᅬޚড়ᷛ ЁᮄᓩܹⱘˈᑊϨˈ㒧ޚгᰃ ANSI ᷛ⫣خ䞛⫼ⳕ⬹ো“, ... ā㸼⼎ߑ᭄ব䭓খ᭄㸼ⱘ 䆄DŽ ᣕݐᆍˈህϡᕫϡ೼䇁⊩Ϟ䖯㸠ϔѯ໘⧚ˈे䞛⫼ void ԰Ўᮄᓣⱘ᮴ᔶᓣখ᭄ߑ᭄ⱘᰒᓣᷛ ᭄ᔎࠊ䕀ᤶˈԚᓩܹⱘৠᯊгᏺᴹњᕜ໮⏋х੠咏⚺ˈ㗠Ϩ䖬ᖙ乏ݐᅶ䖭ϸ⾡ᔶᓣDŽЎњֱ 㿔ব࣪DŽᅗӀӬѢ㄀ 1 ⠜ЁⱘĀᮻᓣāໄᯢヺˈ಴ЎᅗӀᦤկњߑ᭄䇗⫼ᯊⱘ䫭䇃Ẕᶹ੠খ Ёᓩܹⱘ᳔䞡㽕ⱘϔϾ䇁ޚ䇈ᯢ˖ࠄⳂࠡЎℶˈᏺᔶᓣখ᭄ॳൟⱘߑ᭄ໄᯢヺᰃ ANSI ᷛ ㄀ѠϾߑ᭄ rand ϡᏺখ᭄ˈϨ䖨ಲ㉏ൟЎ intDŽ ㄀ϔϾᅲ䰙খ᭄᯳ϔϾᣛ৥ᐌ䞣ᄫヺⱘᣛ䩜DŽ݊Ёⱘᔶᓣখ᭄ৡᄫৃҹ䍋ࠄ⊼䞞䇈ᯢⱘ԰⫼DŽ strcpy ᰃϔϾ䖨ಲ int ㉏ൟⱘߑ᭄ˈᅗ᳝ϸϾᅲ䰙খ᭄ˈ㄀ϔϾᅲ䰙খ᭄ᰃϔϾᄫヺᣛ䩜ˈ int strcpy(char *dest, const char *source), rand(void); ೼ϟ߫ᮄᓣⱘໄᯢЁ˖ ൟⱘߑ᭄ⱘᣛ䩜 pfiDŽᅗӀ䛑≵᳝䇈ᯢᔶᓣখ᭄㉏ൟˈ಴ℸ䛑ሲѢᮻᓣⱘໄᯢDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ㋴ᇚ㹿߱ྟ࣪Ў 0ˈᅠܼⳌৠⱘᬜᵰ䖬ৃҹ䗮䖛ϟ߫ໄᯢ㦋ᕫ˖ܗy[3]Ёⱘ y[0][1]੠ y[0][2]DŽ㉏Ԑഄˈ঺ϸ㸠ᇚ߱ྟ࣪ y[1]੠ y[2]DŽ಴Ў߱ؐⱘ᭄Ⳃϡ໳ˈ᠔ҹ ᰃϔϾᅠܼ⫼㢅ᣀোߚ䱨ⱘ߱ྟ࣪˖1ǃ3੠5䖭3Ͼ᭄߱ྟ᭄࣪㒘y[0]ⱘ㄀ϔ㸠ˈेy[0][0]ǃ }; { 3, 5, 7 }, { 2, 4, 6 }, { 1, 3, 5 }, float y[4][3] = { ؐDŽϟ䴶ⱘ՟ᄤ˖ ᇚ x ໄᯢᑊ߱ྟ࣪ЎϔϾ݋᳝ 3 Ͼ៤ਬⱘϔ㓈᭄㒘ˈ䖭ᰃ಴Ўˈ᭄㒘᳾ᣛᅮ໻ᇣϨ᳝ 3 Ͼ߱ int x[] = { 1, 3, 5 }; ՟བ˖ 㘮䲚ⱘϟϔϾ៤ਬDŽ ㋴԰ЎѢ㘮䲚ⱘ៤ਬˈ݊ᅗ࠽ԭⱘ៤ਬᇚ⫼ᴹ߱ྟ࣪䆹ᄤ㘮䲚᠔೼ⱘܗ㸼Ёপߎ䎇໳᭄Ⳃⱘ 䆌䍙䖛៤ਬⱘ᭄ⳂDŽԚᰃˈབᵰᄤ㘮䲚ⱘ߱ؐϡҹᎺ㢅ᣀোᓔ༈ˈ߭াҢ߱ؐܕؐⱘ᭄Ⳃϡ ⱘ߱ྟ࣪ヺҹᎺ㢅ᣀোᓔ༈ˈ߭ৢ㓁䚼ߚЁ⫼䗫ো䱨ᓔⱘ߱ؐ㸼ᇚ߱ྟ࣪ᄤ㘮䲚ⱘ៤ਬDŽ߱ ⱘ߱ྟ࣪Ёৃҹⳕ⬹ᣀো˖བᵰ㘮䲚ⱘ៤ਬг䎇ϔϾ㘮䲚ˈϨ䆹៤ਬމྟ࣪㾘߭DŽ೼ϟ߫ᚙ 㘮䲚ᰃϔϾ㒧ᵘ៪᭄㒘DŽབᵰϔϾ㘮䲚ࣙ৿㘮䲚㉏ൟⱘ៤ਬˈ߭߱ྟ࣪ᯊᇚ䗦ᔦՓ⫼߱ ANSI 㾘߭䖬㒭ߎњ䴲ᰒᓣ߱ྟ࣪ⱘ䴭ᗕ㘨ড়ⱘ㊒⹂䇁НDŽ 䆌ҹϔ⾡ㅔऩᮍᓣᇍ㘨ড়䖯㸠ᰒᓣ߱ྟ࣪໪ˈܕϟᕜ䲒ᇍᅗ䖯㸠ϔ㠀࣪DŽ䰸њ㟇ᇥމ⊩ⱘᚙ 䆌ᇍ㘨ড়䖯㸠੠ྟ࣪DŽĀ㄀ϔϾ៤ਬā㾘߭ᑊϡᕜᅠ㕢ˈԚ೼≵᳝ᮄ䇁ܕ䇈ᯢ˖㄀ 1 ⠜ϡ ⱘ߱ؐDŽ 㘨ড়ⱘ߱ؐৃҹᰃ㉏ൟⳌৠⱘऩϾ㸼䖒ᓣˈгৃҹᰃᣀ೼㢅ᣀোЁⱘ㘨ড়ⱘ㄀ϔϾ៤ਬ ᭄㒘໻ᇣ೎ᅮˈ߭ᄫヺІЁⱘᄫヺ᭄˄ϡ䅵ሒ䚼ⱘぎᄫヺ˅ϡ㛑䍙䖛᭄㒘ⱘ໻ᇣDŽ ᅮDŽ㢹އ˅㒘DŽ㢹᭄㒘໻ᇣ᳾ⶹˈ᭄߭㒘໻ᇣᇚ⬅ᄫヺІЁᄫヺⱘ᭄Ⳃ˄ࣙᣀሒ䚼ⱘぎᄫヺ ᭄㒘ЁⱘⳌᑨ៤ਬDŽ㉏Ԑഄˈᆑᄫヺᄫ䴶ؐ˄খ㾕 A.2.6 㡖˅ৃҹ߱ྟ࣪ wchar_t ㉏ൟⱘ᭄ 䖭䞠᳝ϔϾ⡍՟˖ᄫヺ᭄㒘ৃ⫼ᄫヺІᄫ䴶ؐ߱ྟ࣪DŽᄫヺІЁⱘ৘Ͼᄫヺձ⃵߱ྟ࣪ ៤ਬᇚ㹿߱ྟ࣪Ў 0DŽ ⱘ᭄Ⳃϡ㛑䍙䖛᭄㒘៤ਬⱘ᭄ⳂDŽབᵰ߱ؐⱘ᭄Ⳃ↨᭄㒘៤ਬⱘ᭄Ⳃᇥˈ߭ሒ䚼ԭϟⱘ᭄㒘 ᅮ᭄㒘ⱘ໻ᇣˈҢ㗠Փ᭄㒘㉏ൟ៤Ўᅠᭈ㉏ൟDŽ㢹᭄㒘໻ᇣ೎ᅮˈ߭߱ؐއ߭߱ؐⱘ᭄Ⳃᇚ ᭄㒘ⱘ߱ؐᰃϔϾᣀ೼㢅ᣀোЁⱘǃ⬅᭄㒘៤ਬⱘ߱ؐᵘ៤ⱘ㸼DŽབᵰ᭄㒘໻ᇣ᳾ⶹˈ ᇥˈ߭ৢ䴶ԭϟⱘ㒧ᵘ៤ਬᇚ㹿߱ྟ࣪Ў 0DŽ߱ؐⱘ᭄Ⳃϡ㛑↨៤ਬ᭄໮DŽ ؐ㸼DŽ᮴ৡⱘԡᄫ↉៤ਬᇚ㹿ᗑ⬹ˈ಴ℸϡ㹿߱ྟ࣪DŽབᵰ㸼Ё߱ؐⱘ᭄Ⳃ↨㒧ᵘⱘ៤ਬ᭄ 㒧ᵘⱘ߱ؐৃҹᰃ㉏ൟⳌৠⱘ㸼䖒ᓣˈгৃҹᰃᣀ೼㢅ᣀোЁⱘᣝ݊៤ਬ⃵ᑣᥦ߫ⱘ߱ ؐ㒭ᇍ䈵DŽ ᣛ䩜៪ㅫᴃ㉏ൟᇍ䈵ⱘ߱ؐᰃϔϾऩϾⱘ㸼䖒ᓣˈгৃ㛑ᣀ೼㢅ᣀোЁDŽ䆹㸼䖒ᓣᇚ䌟 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! int * int 䆒ᷛ䆚ヺⱘ㉏ൟⳌৠDŽ՟བ˖؛ⱘ㉏ൟᇚϢ བᵰ䆹㒧ᵘᰃໄᯢЁⱘϔϾໄᯢヺˈህ᳝ৃ㛑ᚳϔ⹂ᅮᷛ䆚ヺ೼ᢑ䈵ໄᯢヺЁⱘԡ㕂DŽੑৡ Ⳉ᥹ᢑ䈵ໄᯢヺ opt (ᔶᓣখ᭄㉏ൟ㸼 opt) Ⳉ᥹ᢑ䈵ໄᯢヺ opt [ᐌ䞣㸼䖒ᓣ opt] (ᢑ䈵ໄᯢヺ) Ⳉ᥹ᢑ䈵ໄᯢヺ ᣛ䩜 opt Ⳉ᥹ᢑ䈵ໄᯢヺ ᣛ䩜 ᢑ䈵ໄᯢヺ˖ 䇈ᯢヺ䰤ᅮヺ㸼 ᢑ䈵ໄᯢヺ opt ㉏ൟৡ˖ 䖭Ͼ䯂乬ˈҢ䇁⊩Ϟ䆆ˈгህᰃᇍᶤ⾡㉏ൟⱘᇍ䈵䖯㸠ໄᯢˈাᰃⳕ⬹њᇍ䈵ⱘৡᄫ㗠ᏆDŽ އ᭄㉏ൟǃ԰Ў sizeof ⱘᅲ䰙খ᭄ㄝ˅ˈ៥Ӏ䳔㽕ᦤկ᭄᥂㉏ൟⱘৡᄫDŽՓ⫼㉏ൟৡৃҹ㾷 ೼ᶤѯϞϟ᭛Ё˄՟བˈ䳔㽕ᰒᓣ䖯㸠ᔎࠊ㉏ൟ䕀ᤶǃ䳔㽕೼ߑ᭄ໄᯢヺЁໄᯢᔶᓣখ A.8.8 ㉏ൟৡ ሒ䚼ⱘぎᄫヺDŽ ㋴DŽ䆹᭄㒘ⱘ໻ᇣࣙᣀܗໄᯢњϔϾᄫヺ᭄㒘ˈᑊ⫼ϔϾᄫヺІᄫ䴶ؐ߱ྟ࣪䆹ᄫヺ᭄㒘ⱘ char msg[] = "Syntax error on line %s\n"; ᳔ৢ ㋴ᇚ咬䅸߱ྟ࣪Ў 0DŽܗᇚ߱ྟ࣪ y ⱘ㄀ϔ߫˄ᇚ y ⳟ៤ЎϔϾѠ㓈᭄㒘˅ˈ݊ԭⱘ }; { 1 }, { 2 }, { 3 }, { 4 } float y[4][3] = { ϟ߫ໄᯢ˖ ㋴䖯㸠߱ྟ࣪ˈy[2]ձℸ㉏᥼DŽ঺໪ˈܗ㋴DŽৠ⧚ˈy[1]ᇚՓ⫼ৢ㓁ⱘ 3 Ͼܗ⫼㸼Ёⱘ 3 Ͼ y ⱘ߱ؐҹᎺ㢅ᣀোᓔྟˈԚ y[0]ⱘ߱ؐ߭≵᳝ҹᎺ㢅ᣀোᓔྟˈ಴ℸ y[0]ⱘ߱ྟ࣪ᇚՓ }; 1, 3, 5, 2, 4, 6, 3, 5, 7 float y[4][3] = { w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! extern int Blockno; ᑊ≵᳝䞡ᮄໄᯢ BlocknoˈԚϟ߫ໄᯢ˖ extern Blockno; ϟ߫ໄᯢ˖ ㉏ൟᅮНৡৃ೼ݙሖ԰⫼ඳЁ䞡ᮄໄᯢˈԚᖙ乏㒭ߎϔϾ䴲ぎⱘ㉏ൟ䇈ᯢヺ䲚ড়DŽ՟བˈ ҹ䗮䖛঺ϔ⾡ᮍᓣ䖯㸠㉏ൟໄᯢDŽ೼ᴀջЁˈb Ϣ݊ᅗӏԩ long ㉏ൟᇍ䈵ⱘ㉏ൟⳌৠDŽ typedef ㉏ൟᅮНᑊ≵᳝ᓩܹᮄⱘ㉏ൟˈᅗাᰃᅮНњ᭄᥂㉏ൟⱘৠН䆡ˈ䖭ḋˈህৃ ᅮⱘ㒧ᵘ㉏ൟˈzp ⱘ㉏ൟЎᣛ৥䆹㒧ᵘⱘᣛ䩜DŽ 䛑ᰃড়⊩ⱘໄᯢDŽb ⱘ㉏ൟЎ longˈbp ⱘ㉏ൟЎĀᣛ৥ long ㉏ൟⱘᣛ䩜āDŽz ⱘ㉏ൟЎᣛ Complex z, *zp; extern Blockptr bp; Blockno b; Пৢˈϟ䗄ᔶᓣ˖ typedef struct { double r, theta; } Complex; typedef long Blockno, *Blockptr; ՟བˈ೼ᅮН ℸৢˈ㉏ൟᅮНৡ೼䇁⊩ϞህㄝӋѢⳌ݇㉏ൟⱘ㉏ൟ䇈ᯢヺ݇䬂ᄫDŽ typedef ໄᯢᣝ✻᱂䗮ⱘໄᯢᮍᓣᇚϾ㉏ൟᣛ⌒㒭݊ໄᯢヺЁⱘ↣Ͼৡᄫ˄খ㾕 A.8.6 㡖 ˅DŽ ᷛ䆚ヺ ㉏ൟᅮНৡ˖ ᷛ䆚ヺ⿄Ў㉏ൟᅮНৡDŽ ㉏䇈ᯢヺЎ typedef ⱘໄᯢϡ⫼Ѣໄᯢᇍ䈵ˈ㗠ᰃᅮНЎ㉏ൟੑৡⱘᷛ䆚ヺDŽ䖭ѯټᄬ A.8.9 typedef ಲϔϾᭈൟؐāDŽ ㋴Ўᣛ৥ߑ᭄ⱘᣛ䩜ˈ䆹ߑ᭄≵᳝খ᭄Ϩ䖨ܗ䩜ⱘߑ᭄”ǃ“ϔϾ᭄㒘ˈ݊䭓ᑺ᳾ᣛᅮˈ᭄㒘ⱘ ㋴Ͼ᭄ⱘᭈൟ᭄㒘ⱘᣛ䩜”ǃ“᳾ᣛᅮখ᭄ǃ䖨ಲᣛ৥ᭈൟⱘᣛܗⱘᣛ䩜ⱘ᭄㒘”ǃ“ᣛ৥᳾ᣛᅮ ݊Ёⱘ 6 Ͼໄᯢߚ߿ੑৡњϟ߫㉏ൟ˖“ᭈൟ”ǃ“ᣛ৥ᭈൟⱘᣛ䩜”ǃ“ࣙ৿ 3 Ͼᣛ৥ᭈൟ int (*[])(void) int *() int (*)[] int *[3] w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! case ᷛো੠ default ᷛো⫼೼ switch 䇁হЁ˄খ㾕 A.9.4 㡖 ˅DŽ case ᷛোЁⱘᐌ䞣 ᑊϨϡ㛑㹿䞡ᮄໄᯢDŽ䆺㒚ֵᙃখ㾕 A.11.1 㡖DŽ ᷛDŽᷛ䆚ヺⱘ԰⫼ඳᰃᔧࠡߑ᭄DŽ಴Ўᷛো᳝㞾Ꮕⱘৡᄫぎ䯈ˈ಴ℸϡӮϢ݊ᅗᷛ䆚ヺ⏋⎚ˈ ⬅ᷛ䆚ヺᵘ៤ⱘᷛোໄᯢњ䆹ᷛ䆚ヺDŽᷛ䆚ヺᷛোⱘᚳϔ⫼䗨ህᰃ԰Ў goto 䇁হⱘ䏇䕀Ⳃ default: 䇁হ case ᐌ䞣㸼䖒ᓣ: 䇁হ ᷛ䆚ヺ: 䇁হ ᏺᷛো䇁হ˖ 䇁হৃᏺ᳝ᷛোࠡ㓔DŽ A.9.1 ᏺᷛো䇁হ 䏇䕀䇁হ ᕾ⦃䇁হ 䗝ᢽ䇁হ ໡ড়䇁হ 㸼䖒ᓣ䇁হ ᏺᷛো䇁হ 䇁হ˖ ߚЎ޴⾡㉏ൟDŽ བᵰϡ⡍߿ᣛᯢˈ䇁হ䛑ᰃ乎ᑣᠻ㸠ⱘDŽ䇁হᠻ㸠䛑᳝ϔᅮⱘ㒧ᵰˈԚ≵᳝ؐDŽ䇁হৃ A.9 䇁হ 䭓ᑺ੠ߑ᭄ᔶᓣখ᭄㉏ൟᰃ݊Ёᕜ䞡㽕ⱘ಴㋴DŽ 䈵ໄᯢヺ˄খ㾕 A.8.8 㡖˅ⳌৠˈϨᅗӀⱘ㉏ൟ䇈ᯢヺ㸼ㄝӋˈ߭䖭ϸϾ㉏ൟᰃⳌৠⱘDŽ᭄㒘 ೼ሩᓔ݊Ёⱘӏԩ typedef ㉏ൟᑊߴ䰸᠔᳝ߑ᭄ᔶᓣখ᭄ᷛ䆚ヺৢˈབᵰϸϾ㉏ൟⱘᢑ ᮴ᷛ䆄ⱘᵮВᣛᅮⱘ㉏ൟгᰃϡㄝӋⱘDŽ 䆄ⱘ㒧ᵘǃϡৠᷛ䆄ⱘ㘨ড়੠ϡৠᷛ䆄ⱘᵮВᰃϡㄝӋⱘˈ᮴ᷛ䆄ⱘ㘨ড়ǃ᮴ᷛ䆄ⱘ㒧ᵘ៪ ㋏ˈ՟བˈऩ⣀ⱘ long 㭈৿њ long int˅ˈ߭䖭ϸϾ㉏ൟ䇈ᯢヺ㸼ᰃㄝӋⱘDŽ݋᳝ϡৠᷛ བᵰϸϾ㉏ൟ䇈ᯢヺ㸼ࣙ৿Ⳍৠⱘ㉏ൟ䇈ᯢヺ䲚ড়˄䳔㽕㗗㰥㉏ൟ䇈ᯢヺП䯈ⱘ㭈⎉݇ A.8.10 ㉏ൟㄝӋ ߭䞡ᮄໄᯢњ BlocknoDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䗝ᢽ䇁হ˖ 䗝ᢽ䇁হࣙᣀϟ߫޴⾡᥻ࠊ⌕ᔶᓣ˖ A.9.4 䗝ᢽ䇁হ ྟ࣪ϔ⃵DŽ བᵰᠻ㸠䏇䕀䇁হ䖯ܹ⿟ᑣഫˈ߭ϡ䖯㸠߱ྟ࣪DŽStatic ㉏ൟⱘᇍ䈵ҙ೼⿟ᑣᓔྟᠻ㸠ࠡ߱ 㞾ࡼᇍ䈵ⱘ߱ྟ࣪೼↣⃵䖯ܹ⿟ᑣഫⱘ乊ッᯊᠻ㸠ˈᠻ㸠ⱘ乎ᑣᣝ✻ໄᯢⱘ乎ᑣ䖯㸠DŽ ߭г䗖⫼Ѣৠϔৡᄫぎ䯈ⱘᷛ䆚ヺ˄খ㾕 A.11 㡖˅ˈϡৠৡᄫぎ䯈ⱘᷛ䆚ヺ㹿䅸ЎᰃϡৠⱘDŽ A.11.1 㡖˅ˈ೼⿟ᑣഫПৢݡᘶ໡݊԰⫼DŽ೼ৠϔ⿟ᑣഫЁˈϔϾᷛ䆚ヺা㛑ໄᯢϔ⃵DŽℸ㾘 བᵰໄᯢ㸼Ёⱘᷛ䆚ヺԡѢ⿟ᑣഫ໪ⱘ԰⫼ඳЁˈ߭໪䚼ໄᯢ೼⿟ᑣഫݙᇚ㹿ᣖ䍋˄খ㾕 䇁হ㸼 䇁হ 䇁হ 䇁㞾㸼 ໄᯢ㸼 ໄᯢ ໄᯢ ໄᯢ㸼˖ {ໄᯢ㸼 opt 䇁হ㸼 opt} ໡ড়䇁হ˖ ᭄ᅮНЁⱘߑ᭄ԧህᰃϔϾ໡ড়䇁হDŽ ᔧ䳔㽕ᡞ㢹ᑆᴵ䇁হ԰Ўϔᴵ䇁হՓ⫼ᯊˈৃҹՓ⫼໡ড়䇁হ˄г⿄ЎĀ⿟ᑣഫā˅DŽߑ A.9.3 ໡ড়䇁হ 䆒㕂ᷛোDŽ ࠡ㒧ᴳDŽ≵᳝㸼䖒ᓣⱘ䇁হ⿄Ўぎ䇁হDŽぎ䇁হᐌᐌ⫼ᴹЎᕾ⦃䇁হᦤկϔϾぎⱘᕾ⦃ԧ៪ ໻໮᭄㸼䖒ᓣ䇁হЎ䌟ؐ䇁হ៪ߑ᭄䇗⫼䇁হDŽ㸼䖒ᓣᓩ䍋ⱘ᠔᳝ࡃ԰⫼೼ϟϔᴵ䇁হᠻ㸠 㸼䖒ᓣ opt; 㸼䖒ᓣ䇁হ˖ ໻䚼ߚ䇁হЎ㸼䖒ᓣ䇁হˈ݊ᔶᓣབϟ᠔⼎˖ A.9.2 㸼䖒ᓣ䇁হ ᷛোᴀ䑿ϡӮᬍব⿟ᑣⱘ᥻ࠊ⌕DŽ 㸼䖒ᓣᖙ乏ЎᭈൟDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ㄝӋѢ for (㸼䖒ᓣ 1; 㸼䖒ᓣ 2; 㸼䖒ᓣ 3) 䇁হ continue 䇁হˈ߭䇁হ ྟ࣪ˈ݊㉏ൟ≵᳝䰤ࠊDŽ᠔᳝㸼䖒ᓣⱘࡃ԰⫼೼䅵ㅫ݊ؐৢゟे㒧ᴳDŽབᵰᄤ䇁হЁ≵᳝ ⱘؐㄝѢ 0ˈ߭ for 䇁হ㒜ℶᠻ㸠DŽ㄀ϝϾ㸼䖒ᓣ೼↣⃵ᕾ⦃ৢ䅵ㅫˈҹ䞡ᮄᇍᕾ⦃䖯㸠߱ ࠊDŽ㄀ѠϾ㸼䖒ᓣᖙ乏Ўㅫᴃ㉏ൟ៪ᣛ䩜㉏ൟˈ೼↣⃵ᓔྟᕾ⦃ࠡ䅵ㅫ݊ؐDŽབᵰ䆹㸼䖒ᓣ ೼ for 䇁হЁˈ㄀ϔϾ㸼䖒ᓣা䅵ㅫϔ⃵ˈ⫼Ѣᇍᕾ⦃߱ྟ࣪DŽ䆹㸼䖒ᓣⱘ㉏ൟ≵᳝䰤 㗠 do 䇁হ೼↣⃵ᕾ⦃ৢ⌟䆩㸼䖒ᓣDŽ 䖒ᓣᖙ乏Ўㅫᴃ㉏ൟ៪ᣛ䩜㉏ൟDŽwhile 䇁হ೼䇁হᠻ㸠ࠡ⌟䆩㸼䖒ᓣˈᑊ䅵ㅫ݊ࡃ԰⫼ˈ ೼ while 䇁হ੠ do 䇁হЁˈা㽕㸼䖒ᓣⱘؐϡЎ 0ˈ݊Ёⱘᄤ䇁হᇚϔⳈ䞡໡ᠻ㸠DŽ㸼 for (㸼䖒ᓣ opt; 㸼䖒ᓣ opt; 㸼䖒ᓣ opt) 䇁হ do 䇁হ while (㸼䖒ᓣ); while (㸼䖒ᓣ) 䇁হ ᕾ⦃䇁হ ᕾ⦃䇁হ⫼Ѣᣛᅮ⿟ᑣ↉ⱘᕾ⦃ᠻ㸠DŽ A.9.5 ᕾ⦃䇁হ 䇈ᯢ˖೼ᴀк㄀ 1 ⠜Ёˈswitch 䇁হⱘ᥻ࠊ㸼䖒ᓣϢ case ᐌ䞣䛑ᖙ乏Ў int ㉏ൟDŽ ᵰ≵᳝ case ᐌ䞣ऍ䜡ˈϨ≵᳝ default ᷛোˈ߭ switch 䇁হⱘ᠔᳝ᄤ䇁হ䛑ϡᠻ㸠DŽ case ᐌ䞣Ϣ㸼䖒ᓣऍ䜡ˈᑊϨ᳝ default ᷛোˈ߭ᇚ᥻ࠊ䕀৥ default ᷛোⱘ䇁হDŽབ བᵰᶤϾ case ᐌ䞣Ϣ㸼䖒ᓣⱘؐⳌৠˈ߭ᇚ᥻ࠊ䕀৥Ϣ䆹 case ᷛোऍ䜡ⱘ䇁হDŽབᵰ≵᳝ 䅵ㅫ㸼䖒ᓣⱘؐঞ݊ࡃ԰⫼ˈᑊᇚ݊ؐϢ↣Ͼ case ᐌ䞣↨䕗ˈܜswitch 䇁হᠻ㸠ᯊˈ佪 ༫ˈcase ៪ default ᷛোϢࣙ৿ᅗⱘ᳔䖥ⱘ switch Ⳍ݇㘨DŽ ৢϡ㛑᳝ⳌৠⱘؐDŽϔϾ switch 䇁হ᳔໮ৃҹ᳝ϔϾ default ᷛোDŽswltch 䇁হৃҹጠ case ᐌ䞣ᇚ㹿䕀ᤶЎᭈൟᦤछৢⱘ㉏ൟDŽৠϔ switch 䇁হЁⱘӏԩϸϾ case ᐌ䞣೼䕀ᤶ ৃᏺϔϾ៪໮Ͼ case ᷛো˄খ㾕 A.9.1 㡖˅DŽ᥻ࠊ㸼䖒ᓣ䳔㽕䖯㸠ᭈൟᦤछ˄খ㾕 A.6.1 㡖 ˅ˈ োᣀ䍋ᴹⱘ㸼䖒ᓣᖙ乏Ўᭈൟˈℸ䇁হ᥻ࠊⱘᄤ䇁হϔ㠀ᰃ໡ড়䇁হDŽᄤ䇁হЁⱘӏԩ䇁হ switch 䇁হḍ᥂㸼䖒ᓣⱘϡৠপؐᇚ᥻ࠊ䕀৥ⳌᑨⱘߚᬃDŽ݇䬂ᄫ switch Пৢ⫼೚ᣀ else ⱘ℻Нᗻ䯂乬DŽ އҹ㾷 㸠㄀ѠϾᄤ䇁হˈ䗮䖛ᇚ else Ϣৠϔጠ༫ሖЁ⺄ࠄⱘ᳔䖥ⱘ᳾ऍ䜡 else ⱘ if Ⳍ䖲᥹ˈৃ ⱘࡃ԰⫼˅ˈབᵰϡㄝѢ 0ˈ߭ᠻ㸠㄀ϔϾᄤ䇁হDŽ೼㄀Ѡ⾡ᔶᓣЁˈབᵰ㸼䖒ᓣЎ 0ˈ߭ᠻ 㹿∖ؐ˄ࣙᣀ᠔᳝ܜ೼ϸ⾡ᔶᓣⱘ if 䇁হЁˈ㸼䖒ᓣ˄ᖙ乏Ўㅫᴃ㉏ൟ៪ᣛ䩜㉏ൟ˅佪 switch (㸼䖒ᓣ) 䇁হ if (㸼䖒ᓣ) 䇁হ else 䇁হ if (㸼䗝ᓣ) 䇁হ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䛑ᰃ≵᳝ᅮНⱘDŽ ϟˈ䖨ಲؐމ᥻ࠊࠄ䖒ߑ᭄ⱘ㒧ሒㄝӋѢϔϾϡᏺ㸼䖒ᓣⱘ return 䇁হDŽ೼䖭ϸ⾡ᚙ ߑ᭄ⱘ䖨ಲؐ㉏ൟDŽ 䗮䖛䌟ؐ᪡԰䕀ᤶ㉏ൟ䙷ḋˈ䆹㸼䖒ᓣᇚ㹿䕀ᤶЎᅗ᠔೼ⱘڣᓣⱘؐᇚ䖨ಲ㒭ߑ᭄䇗⫼㗙DŽ return 䇁হ⫼Ѣᇚ᥻ࠊҢߑ᭄䖨ಲ㒭䇗⫼㗙DŽᔧ return 䇁হৢ䎳ϔϾ㸼䖒ᓣᯊˈ㸼䖒 হⱘᠻ㸠ˈᑊᇚ᥻ࠊ䕀৥㹿㒜ℶ䇁হⱘϟϔᴵ䇁হDŽ break 䇁হা㛑⫼೼ᕾ⦃䇁হ៪ switch 䇁হЁˈᅗᇚ㒜ℶࣙ৿䆹䇁হⱘ᳔ݙሖᕾ⦃䇁 བᵰ continuet 䇁হϡࣙ৿೼᳈ᇣⱘᕾ⦃䇁হЁˈ߭݊԰⫼Ϣ goto contin 䇁হㄝӋDŽ } } while (...); } contin: ; contin: ; contin: ; ... ... ... while (...) { do { for (...) { ⹂ഄ䇈ˈ೼ϟ߫ӏϔ䇁হЁ˖ޚ᳈ continue 䇁হা㛑ߎ⦄೼ᕾ⦃䇁হݙˈᅗᇚ᥻ࠊ䕀৥ࣙ৿ℸ䇁হⱘ᳔ݙሖᕾ⦃䚼ߚDŽ ᅮⱘ䇁হDŽ ೼ goto 䇁হЁˈᷛ䆚ヺᖙ乏ᰃԡѢᔧࠡߑ᭄Ёⱘᷛো(খ㾕 A.9.1 㡖)DŽ᥻ࠊᇚ䕀⿏ࠄᷛোᣛ return 㸼䖒ᓣ opt; break; continue; goto ᷛ䆚ヺ; 䏇䕀䇁হ˖ 䏇䕀䇁হ⫼Ѣ᮴ᴵӊഄ䕀⿏᥻ࠊDŽ A.9.6 䏇䕀䇁হ for 䇁হЁⱘ 3 Ͼ㸼䖒ᓣЁ䛑ৃҹⳕ⬹DŽ㄀ѠϾ㸼䖒ᓣⳕ⬹ᯊㄝӋѢ⌟䆩ϔϾ䴲 0 ᐌ䞣DŽ } 㸼䖒ᓣ 3; 䇁হ while (㸼䖒ᓣ 2) { 㸼䖒ᓣ 1˗ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ㄀Ѡ⾡ᔶᓣᰃϔ⾡ᮻᓣⱘߑ᭄ᅮН˖ᷛ䆚ヺ㸼߫ߎˈᔶᓣখ᭄ⱘৡᄫˈ䖭ѯᔶᓣখ᭄ⱘ ೼䰘ᔩ B Ёҟ㒡DŽᏺ᳝ৃবᔶᓣখ᭄ⱘߑ᭄ᖙ乏㟇ᇥ᳝ϔϾੑৡⱘᔶᓣখ᭄DŽ ༈᭛ӊ c) ? m : c; m = (a > b) ? a : b; int m; { int max(int a, int b, int c) ϟ䴶ᰃϔϾᮄᓣߑ᭄ᅮНⱘᅠᭈ՟ᄤ˖ ϔϾᣛ৥ᔶᓣখ᭄ⱘᣛ䩜ᯊˈᅗӀП䯈ⱘऎ߿ህᰒ㗠ᯧ㾕њDŽ ࣪DŽ㄀ 1 ⠜ᣛᅮˈfloat ㉏ൟⱘᔶᓣখ᭄ໄᯢᇚ㹿䇗ᭈЎ double ㉏ൟDŽᔧ೼ߑ᭄ݙ䚼⫳៤ ᮄᓩܹⱘϔϾ⡍ᕕDŽ᳝݇ᦤछⱘϔѯ㒚㡖г᳝㒚ᖂⱘবޚ䇈ᯢ˖ᮄᓣߑ᭄ᅮНᰃ ANSI ᷛ ⫼ߑ᭄ᯊˈᖙ㽕ᯊ㽕ᇍᅲ䰙খ᭄䖯㸠㉏ൟ䕀ᤶˈ✊ৢ䌟ؐ㒭ᔶᓣখ᭄ˈ䆺㒚ֵᙃখ㾕 A.7.3 㡖DŽ ಲ type ㉏ൟؐⱘߑ᭄āˈ߭䆹ໄᯢᇚӮ㹿䇗ᭈЎĀᣛ৥䖨ಲ type ㉏ൟؐⱘߑ᭄ⱘᣛ䩜āDŽ䇗 ߭䆹ໄᯢᇚӮ㹿㞾ࡼ䇗ᭈЎĀᣛ৥ type ㉏ൟⱘᣛ䩜āDŽ㉏Ԑഄˈབᵰᶤϔᔶᓣখ᭄ໄᯢЎĀ䖨 䆹໡ড়䇁হⱘݙሖ⿟ᑣഫЁ䞡ᮄໄᯢ˅DŽབᵰᶤϔᔶᓣখ᭄ໄᯢⱘ㉏ൟЎ“type ㉏ൟⱘ᭄㒘āˈ ᅗᷛ䆚ヺϔḋ೼݊ڣ໘䖯㸠ໄᯢˈᑊϨ೼䆹໡ড়䇁হЁϡ㛑䞡໡ໄᯢⳌৠⱘᷛ䆚ヺ˄Ԛৃҹ ೼䖭ϸ⾡ᮍᓣⱘߑ᭄ᅮНЁˈৃҹ䖭ḋ⧚㾷ᔶᓣখ᭄˖೼ᵘ៤ߑ᭄ԧⱘ໡ড়䇁হⱘᓔྟ ㉏䇈ᯢヺ registerDŽټ䆌䖯㸠߱ྟ࣪ˈᑊϨҙৃՓ⫼ᄬܕ䆚ヺ㸼Ёᣛᅮⱘᔶᓣখ᭄ˈϡ ㉏ൟ⬅ໄᯢ㸼ᣛᅮDŽᇍѢ᳾ໄᯢⱘᔶᓣখ᭄ˈ݊㉏ൟ咬䅸Ў int ㉏ൟDŽໄᯢ㸼ᖙ乏াໄᯢᷛ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䯈˗ᡞϡৠ⾡㉏ⱘᷛ䆄ᬒ೼ৠϔৡᄫぎ䯈Ёᰃᮄ๲ࡴⱘ䰤ࠊDŽϢ㄀ 1 ⠜П䯈᳔໻ⱘϡৠ೼Ѣ˖ 䯈˗㒧ᵘᷛ䆄੠㘨ড়ᷛ䆄ߚ߿᳝৘㞾ⱘৡᄫぎ䯈ˈ೼ᶤѯᅲ⦄ЁᴪВᷛ䆄г᳝㞾Ꮕⱘৡᄫぎ 䇈ᯢ˖䖭ѯ㾘߭Ϣᴀ᠟ݠ㄀ 1 ⠜Ё᠔䗄ⱘݙᆍ᳝޴⚍ϡৠDŽҹࠡᷛো≵᳝㞾Ꮕⱘৡᄫぎ ㉏ൟᅮНৡ੠ᵮВᐌ䞣˗ᷛো˗㒧ᵘᷛ䆄ǃ㘨ড়ᷛ䆄੠ᵮВᷛ䆄˗৘㒧ᵘ៪㘨ড়㞾䑿ⱘ៤ਬDŽ ೼ৠϔ԰⫼ඳݙˈⳌৠⱘᷛ䆚ヺгৃ⫼ѢϡৠⱘⳂⱘDŽৡᄫぎ䯈ⱘ㉏ൟࣙᣀ˖ᇍ䈵ǃߑ᭄ǃ ᷛ䆚ヺৃҹ೼㢹ᑆϾৡᄫぎ䯈ЁՓ⫼㗠ѦϡᕅડDŽབᵰԡѢϡৠⱘৡᄫぎ䯈ЁˈेՓᰃ A.11.1 䆡⊩԰⫼ඳ Ёᷛ䆚ヺП䯈ⱘ䖲᥹DŽܗऩ⣀㓪䆥ⱘ㗏䆥ऩ ᅮ৘Ͼއヺ⡍ᗻⱘ⿟ᑣ᭛ᴀऎඳ˗㄀Ѡ⾡ᰃϢ݋᳝໪䚼䖲᥹ⱘᇍ䈵੠ߑ᭄Ⳍ݇ⱘ԰⫼ඳˈᅗ ಴ℸˈ៥Ӏ䳔㽕㗗㰥ϸ⾡㉏ൟⱘ԰⫼ඳ˖㄀ϔ⾡ᰃᷛ䆚ヺⱘ䆡⊩԰⫼ඳˈᅗᰃԧ⦄ᷛ䆚 ҹ䗮䖛䇗⫼੠᪡԰໪䚼᭄᥂ᴹᅲ⦄DŽ 㓪䆥䖛ⱘ՟⿟ৃҹҢᑧЁ䖯㸠ࡴ䕑ˈ⿟ᑣЁߑ᭄䯈ⱘ䗮ֵৃܜ乘ˈܗЁৃҹࣙ৿໮Ͼ㗏䆥ऩ ϡᖙৠᯊ䖯㸠㓪䆥DŽ⑤᭛ӊ᭛ᴀৃֱᄬ೼㢹ᑆϾ᭛ӊЁˈ↣Ͼ᭛ӊܗϔϾ⿟ᑣⱘ᠔᳝ऩ A.11 ԰⫼ඳϢ䖲᥹ ᠔᳝ЈᯊᅮНᇚ㹿䕀বЎ߱ؐЎ 0 ⱘᅮНDŽ བᵰᅮН೼⿟ᑣЁⱘᶤϾഄᮍߎ⦄ˈ߭ЈᯊᅮНҙ㹿䅸ЎᰃໄᯢˈԚབᵰ≵᳝ᅮНߎ⦄ˈ߭ ⱘϔ㠀ᠽሩDŽޚЁߚ߿㗗㰥ˈUNIX ㋏㒳䗮ᐌህ䞛⫼䖭⾡ᮍ⊩ˈᑊϨ㹿䅸Ўᰃ䆹ᷛܗ೼৘㗏䆥ऩ ⱘ໪䚼䖲᥹ᇍ䈵ⱘ᠔᳝ЈᯊᅮНᇚ䲚Ё䖯㸠㗗㰥ˈ㗠ϡᰃܗᔶᓣЁˈϔϾ⿟ᑣЁ᠔᳝㗏䆥ऩ Ԛ೼ᬜᵰϞᰃㄝӋⱘDŽᶤѯᅲ⦄䗮䖛ᇚЈᯊᅮНⱘὖᗉϔ㠀࣪㗠ᬒᆑњ䖭Ͼ䰤ࠊDŽ೼঺ϔ⾡ 䇈ᯢ˖㱑✊ऩϔᅮН㾘߭˄0ne•definition rule ˅೼㸼䖒ϞϢᴀк㄀ 1 ⠜᳝᠔ϡৠˈ 䆹㾘߭䗖⫼ѢᭈϾ⿟ᑣDŽ ᰃᚳϔⱘDŽᇍѢ݋᳝໪䚼䖲᥹ⱘᇍ䈵ˈܗ䖭ᰃ಴Ўˈݙ䚼䖲᥹ⱘᇍ䈵ᇍ↣Ͼ㗏䆥ऩˈܗ㗏䆥ऩ ↣Ͼᇍ䈵䛑ᖙ乏᳝Ϩҙ᳝ϔϾᅮНDŽᇍѢ݋᳝ݙ䚼䖲᥹ⱘᇍ䈵ˈ䆹㾘߭ߚ߿䗖⫼Ѣ↣Ͼ ᅮНˈ߭䆹ЈᯊᅮНᇚ䕀বЎϔϾ߱ؐЎ 0 ⱘᅮНDŽ Ёϡᄬ೼䆹ᇍ䈵ⱘܗЁDŽ߭᠔᳝ЈᯊᅮН䛑ᇚҙҾ㹿䅸Ўᰃ໮ԭⱘໄᯢ˗བᵰ䆹㗏䆥ऩܗऩ ᏺ᳝߱ؐˈᑊϨϡࣙ৿ extern 䇈ᯢヺˈ߭ᅗᰃϔϾЈᯊᅮНDŽབᵰᇍ䈵ⱘᅮНߎ⦄೼㗏䆥 བᵰϔϾᇍ䈵ⱘ໪䚼ໄᯢᏺ᳝߱ؐˈ߭䆹ໄᯢህᰃϔϾᅮНDŽབᵰϔϾ໪䚼ᇍ䈵ໄᯢϡ ৺߭݋᳝໪䚼䖲᥹DŽ᳝݇䖲᥹ⱘ䆺㒚ֵᙃˈখ㾕 A.11.2 㡖Ёⱘ䅼䆎DŽ བᵰϔϾᇍ䈵៪ߑ᭄ⱘ㄀ϔϾ໪䚼ໄᯢࣙ৿ static 䇈ᯢヺˈ߭䆹ᷛ䆚ヺ݋᳝ݙ䚼䖲᥹ˈ Ӏⱘ㉏ൟгᰃϔ㟈ⱘDŽ ᭄ˈ㗠঺ϔϾ㉏ൟᣛᅮњᏺᔶᓣখ᭄ໄᯢⱘᮄᓣߑ᭄ˈѠ㗙П䯈݊ᅗᮍ䴶䛑Ⳍৠˈ߭䅸Ўᅗ ൟˈ݊ᅗሲᗻ䛑Ⳍৠˈ߭䅸Ў䖭ϸϾ㉏ൟᰃϔ㟈ⱘDŽ᳔ৢˈབᵰϔϾ㉏ൟᣛᅮњϔϾᮻᓣߑ 㟈ⱘDŽℸ໪ˈབᵰϔϾ㉏ൟЎϡᅠᭈ᭄㒘㉏ൟ˄খ㾕 A.8.6 㡖˅ˈ㗠঺ϔϾ㉏ൟЎᅠᭈ᭄㒘㉏ В㉏ൟ˄খ㾕 A.8.3 㡖˅ˈ㗠঺ϔϾᰃᇍᑨⱘᏺৠϔᷛ䆄ⱘᅠᭈ㉏ൟˈ߭䅸Ў䖭ϸϾ㉏ൟᰃϔ 㟈ⱘDŽᑊϨˈབᵰϸϾໄᯢП䯈ⱘऎ߿ҙҙ೼Ѣ˖݊ЁϔϾⱘ㉏ൟЎϡᅠᭈ㒧ᵘǃ㘨ড়៪ᵮ བᵰϔϾᇍ䈵៪ߑ᭄ⱘϸϾໄᯢ䙉ᕾ A.8.10 㡖Ё᠔䗄ⱘ㾘߭ˈ߭䅸ЎᅗӀⱘ㉏ൟᰃϔ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᑊ䖯㸠ᅣᠽሩ˄খ㾕 A.12.3 㡖̚A.12.10 㡖 ˅DŽ 3 ˅ᇚ⿟ᑣߚ៤⫼ぎⱑヺߚ䱨ⱘ䆄োDŽ⊼䞞ᇚ㹿᳓ᤶЎϔϾぎⱑヺDŽ᥹ⴔᠻ㸠乘໘⧚ᣛҸˈ 㡖 ˅DŽ 2˅ᇚᣛҸ㸠ЁԡѢᤶ㸠ヺࠡⱘড᭰ᴴヺ\ߴ䰸ᥝˈҹᡞ৘ᣛҸ㸠䖲᥹䍋ᴹ˄খ㾕 A.12.2 㽕೼⑤᭛ӊⱘ৘㸠П䯈ᦦܹᤶ㸠ヺDŽ ᇚ A.12.1 㡖᠔䗄ⱘϝᄫヺᑣ߫᳓ᤶЎㄝӋᄫヺDŽབᵰ᪡԰㋏㒳⦃๗䳔㽕ˈ䖬ˈܜ1˅佪 DŽ˅ޣ乘໘⧚䖛⿟೼䘏䕥ϞৃҹߦߚЎ޴Ͼ䖲㓁ⱘ䰊↉˄೼ᶤѯ⡍⅞ⱘᅲ⦄Ёৃҹ㓽 㸠Ёˈ䰸ぎḐǃ῾৥ࠊ㸼ヺ໪ⱘ݊ᅗぎⱑヺⱘ԰⫼ᰃ≵᳝ᅮНⱘDŽ ⱘᄫヺᑣ߫ˈℸ໪ˈ᠔᳝᳾䖯㸠݊ᅗᅮНⱘᄫヺ䛑ᇚ㹿䅸Ўᰃ䆄োDŽԚᰃˈ೼乘໘⧚఼ᣛҸ 㿔ˈ䆄োৃҹᰃӏԩ䇁㿔䆄োˈгৃҹᰃ㉏ԐѢ#include ᣛҸ˄খ㾕 A.12.4 㡖˅Ё㸼⼎᭛ӊৡ ↣ϔ㸠䛑ᇚऩ⣀䖯㸠ߚᵤ˄᳝݇བԩᇚ㸠䖲㒧䍋ᴹⱘ䆺㒚ֵᙃখ㾕 A.12.4 㡖˅DŽᇍ乘໘⧚఼㗠 ⱘ᳿ሒ˄Ϣ԰⫼ඳ᮴݇˅DŽ㸠䖍⬠ᰃ᳝ᅲ䰙ᛣНⱘ˗ܗ೼ӏԩഄᮍˈ݊԰⫼ৃᓊ㓁ࠄ᠔೼㗏䆥ऩ ぎḐ˅ህᰃ乘໘⧚఼໘⧚ⱘᇍ䈵DŽ䖭ѯੑҸ㸠ⱘ䇁⊩⣀ゟѢ䇁㿔ⱘ݊ᅗ䚼ߚˈᅗӀৃҹߎ⦄ 乘໘⧚఼ᠻ㸠ᅣ᳓ᤶǃᴵӊ㓪䆥ҹঞࣙ৿ᣛᅮⱘ᭛ӊˈҹ#ᓔ༈ⱘੑҸ㸠˄“#āࠡৃҹ᳝ A.12 乘໘⧚ ߭䆹䖲᥹ᰃ໪䚼ⱘDŽ Ϣ䆹໪䚼ໄᯢ݋᳝Ⳍৠⱘ䖲᥹ˈᑊᓩ⫼ৠϔᇍ䈵៪ߑ᭄DŽԚᰃˈབᵰ≵᳝ৃ㾕ⱘ໪䚼ໄᯢˈ extern 䇈ᯢヺˈᑊϨˈ೼ࣙ৿䆹⿟ᑣഫⱘ԰⫼ඳЁ᳝ϔϾ䆹ᷛ䆚ヺⱘ໪䚼ໄᯢˈ߭䆹ᷛ䆚ヺ ࣙ৿ extern 䇈ᯢヺˈ߭䆹ᷛ䆚ヺ≵᳝䖲᥹ˈᑊϨ೼ߑ᭄ЁᰃᚳϔⱘDŽབᵰ䖭⾡ໄᯢЁࣙ৿ 䆚ヺ݋᳝ݙ䚼䖲᥹ˈ৺߭ˈ䆹ᷛ䆚ヺᇚ݋᳝໪䚼䖲᥹DŽབᵰ⿟ᑣഫЁᇍϔϾᷛ䆚ヺⱘໄᯢϡ བ A.10.2 㡖᠔䗄ˈབᵰՓ⫼њ static 䇈ᯢヺˈ߭ᷛ䆚ヺⱘ㄀ϔϾ໪䚼ໄᯢᇚՓᕫ䆹ᷛ ᠔᳝ໄᯢгᓩ⫼ৠϔᅲԧˈᑊϨ䆹ᇍ䈵៪ߑ᭄ᰃ㹿ᭈϾ⿟ᑣЁ݅ѿⱘDŽ ᴹ䇈ᰃᚳϔⱘDŽ݋᳝໪䚼䖲᥹ⱘৠϔᇍ䈵៪ߑ᭄ᷛ䆚ヺⱘܗϨˈ䆹ᇍ䈵៪ߑ᭄ᇍ䖭Ͼ㗏䆥ऩ Ёˈ݋᳝ݙ䚼䖲᥹ⱘৠϔᇍ䈵៪ߑ᭄ᷛ䆚ヺⱘ᠔᳝ໄᯢ䛑ᓩ⫼ৠϔᅲԧˈᑊܗ೼㗏䆥ऩ A.11.2 䖲᥹ 䚼Ёℸᷛ䆚ヺⱘӏԩໄᯢ䛑ᇚ㹿ᣖ䍋ˈⳈࠄ⿟ᑣഫ㒧ᴳݡᘶ໡݊԰⫼DŽ བᵰᶤϔᷛ䆚ヺᰒᓣഄ೼⿟ᑣഫ˄ࣙᣀᵘ៤ߑ᭄ⱘ⿟ᑣഫ˅༈䚼Ёໄᯢˈ߭䆹⿟ᑣഫ໪ 㗠㿔˅៪ࠄ⿟ᑣഫ㒧ᴳЎℶ˄ᇍߑ᭄ݙ䚼ໄᯢ㗠㿔˅DŽ 㒧ᴳЎℶ˄ᇍ໪䚼ໄᯢܗᷛ䆄៪ᵮВᐌ䞣ⱘ԰⫼ඳҢ݊ߎ⦄೼㉏ൟ䇈ᯢヺЁᓔྟˈࠄ㗏䆥ऩ ԰⫼ඳᰃ݊᠔೼ⱘᭈϾ⿟ᑣഫDŽᷛোⱘ԰⫼ඳᰃ݊᠔೼ⱘߑ᭄DŽ㒧ᵘᷛ䆄ǃ㘨ড়ᷛ䆄ǃᵮВ ߑ᭄˗ߑ᭄ໄᯢЁᔶᓣখ᭄ⱘ԰⫼ඳࠄໄᯢヺⱘ᳿ሒ໘㒧ᴳDŽ⿟ᑣഫ༈䚼Ёໄᯢⱘᷛ䆚ヺⱘ 㒧ᴳЎℶDŽߑ᭄ᅮНЁᔶᓣখ᭄ⱘ԰⫼ඳҢᅮНߑ᭄ⱘ⿟ᑣഫᓔྟ໘ᓔྟˈᑊ䌃こᭈϾܗऩ ೼໪䚼ໄᯢЁˈᇍ䈵៪ߑ᭄ᷛ䆚ヺⱘ䆡⊩԰⫼ඳҢ݊ໄᯢ㒧ᴳⱘԡ㕂ᓔྟˈࠄ᠔೼㗏䆥 䖭ϔ㾘߭೼᳔䖥޴ᑈՓ⫼ᕫᕜ໮DŽ Ͼ㒧ᵘ੠㘨ড়䛑Ў݊៤ਬᓎゟϡৠⱘৡᄫぎ䯈ˈ಴ℸৠϔৡᄫৃߎ⦄೼໮Ͼϡৠⱘ㒧ᵘЁDŽ↣ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! #undef ᷛ䆚ヺ ㉏ԐѢϟ߫ᔶᓣⱘ᥻ࠊᣛҸ˖ ߭ᖙ乏ֱ䆕݊ᔶᓣখ᭄Ͼ᭄ǃᣐݭঞ䆄োᑣ߫䛑ᖙ乏Ϣࠡ䴶ⱘᅮНⳌৠDŽ ぎḐDŽৠ㄀ϔ⾡ᔶᓣϔḋˈ䆄োᑣ߫ࠡৢⱘぎⱑヺ䛑ᇚ㹿϶ᓗᥝDŽབᵰ㽕ᇍᅣ䖯㸠䞡ᅮНˈ ᰃϔϾᏺ᳝ᔶᓣখ᭄˄⬅ᷛ䆚ヺ㸼ᣛᅮ˅ⱘᅣᅮНˈ݊Ё㄀ϔϾᷛ䆚ヺϢ೚ᣀো˄П䯈≵᳝ #define ᷛ䆚ヺ(ᷛ䆚ヺ㸼 opt) 䆄োᑣ߫ ㉏ԐѢϟ߫ᔶᓣⱘᣛҸ㸠˖ ᷛ䆄ᑣ߫Ϣ㄀ϔ⃵Ⳍৠ˄᠔᳝ⱘぎⱑߚ䱨ヺ㹿䅸ЎᰃⳌৠⱘ˅DŽ ⱑヺ䛑ᇚ㹿϶ᓗᥝDŽ㄀Ѡ⃵⫼#define ᣛҸᅮНৠϔᷛ䆚ヺᰃ䫭䇃ⱘˈ䰸䴲㄀Ѡ⃵ᅮНЁⱘ ᇚՓᕫ乘໘⧚఼ᡞ䆹ᷛ䆚ヺৢ㓁ߎ⦄ⱘ৘Ͼᅲ՟⫼㒭ᅮⱘ䆄োᑣ߫᳓ᤶDŽ䆄োᑣ߫ࠡৢⱘぎ #define ᷛ䆚ヺ 䆄োᑣ߫ ㉏ԐѢϟ߫ᔶᓣⱘ᥻ࠊᣛҸ˖ A.12.3 ᅣᅮН੠ᠽሩ ড়ᑊ៤ϔ㸠DŽ䖭⾡໘⧚㽕೼ߚ䱨䆄োПࠡ䖯㸠DŽ 䗮䖛ᇚҹড᭰ᴴ\㒧ᴳⱘᣛҸ㸠᳿ሒⱘড᭰ᴴ੠݊ৢⱘᤶ㸠ヺߴ䰸ᥝDŽৃҹᇚ㢹ᑆᣛҸ㸠 A.12.2 㸠䖲᥹ ᮄᓩܹⱘ⡍ᕕDŽޚ䇈ᯢ˖ϝᄫヺᑣ߫ᰃ ANSI ᷛ 䰸ℸП໪ϡ䖯㸠݊ᅗ᳓ᤶDŽ ??' ^ ??! | ??• ~ ??/ \ ??) ] ??> } ??= # ??( [ ??< { ᄫヺ᳓ᤶˈ䖭⾡᳓ᤶ೼䖯㸠᠔᳝ᅗҪ໘⧚Пࠡ䖯㸠DŽ ⱘᄫヺ䲚㸼⼎ߎᴹˈϟ߫᠔⼎ⱘ᠔᳝ϝᄫヺᑣ߫䛑㽕⫼ⳌᑨⱘऩϾޣЎњᇚ⿟ᑣ䗮䖛䖭⾡㓽 C 䇁㚆⑤⿟ᑣⱘᄫヺ䲚ᰃ 7 ԡ ASCII ⷕⱘᄤ䲚ˈԚᅗᰃ ISO 646•1983 ϡবҷⷕ䲚ⱘ䍙䲚DŽ A.12.1 ϝᄫヺᑣ߫ ໘⧚ᕫࠄⱘ㒧ᵰˈ✊ৢϢ݊ᅗ⿟ᑣ੠ᑧ䖲᥹䍋ᴹDŽ 5˅ᬊ䲚ᖙ㽕ⱘ⿟ᑣ੠᭄᥂ˈᑊᇚ໪䚼ߑ᭄੠ᇍ䈵ⱘᓩ⫼Ϣ݊ᅮНⳌ䖲᥹ˈ㗏䆥㒣䖛ҹϞ ㄝӋᄫヺˈ✊ৢᡞⳌ䚏ⱘᄫヺІᄫ䴶ؐ䖲᥹䍋ᴹDŽ ᇚᄫヺᐌ䞣੠ᄫヺІᄫ䴶ؐЁⱘ䕀Нᄫヺᑣ߫˄খ㾕 A.2.5 㡖Ϣ A.2.6 㡖˅᳓ᤶЎ˅4 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᅣ䇗⫼ tempfile(/usr/tmp)ᇚ⫳៤ #define tempfile(dir) #dir "%s" ᅮ᳝ϟ߫ᅮН˖؛ ⃵ˈϔ⃵䖯㸠⌟䆩ˈ঺ϔ⃵߭⫳៤ؐDŽ 䖨ಲؐৃҹᰃӏᛣㅫᴃ㉏ൟˈ⫮㟇ৃҹᰃᣛ䩜DŽৠᯊˈখ᭄ৃ㛑᳝ࡃ԰⫼ˈ㗠Ϩ䳔㽕䅵ㅫϸ ᅮНњϔϾᅣˈᅗ䖨ಲϸϾখ᭄ПᏂⱘ㒱ᇍؐDŽϢᠻ㸠ৠḋࡳ㛑ⱘߑ᭄᠔ϡৠⱘᰃˈখ᭄Ϣ #define ABSDIFF(a, b) ((a)>(b) ? (a)•(b) : (b)•(a)) ᅮН int table[TABSIZE]; #define TABSIZE 100 ՟བˈ䖭⾡ࡳ㛑ৃ⫼ᴹᅮНĀ㸼⼎ᐌ䞣āˈབϟ՟᠔⼎˖ ↨䕗⣀⡍˄খ㾕ϟ䴶ⱘ՟ᄤ˅DŽ ࡴܹњ#੠##䖤ㅫヺˈ䖭ህՓᕫᓩ⫼੠䖲᥹៤Ўৃ㛑DŽᶤѯᮄ㾘߭˄⡍߿ᰃϢ䖲᥹᳝݇ⱘ㾘߭˅ ㄀ 1 ⠜ᦣ䗄ᕫ᳈䆺㒚DŽ᳔䞡㽕ⱘব࣪ᰃ↨ޚ䇈ᯢ˖᳝݇ᅣᠽሩ໘⧚ⱘ㒚㡖ֵᙃˈANSI ᷛ ेՓᠻ㸠ᅣᠽሩৢᕫࠄⱘ᳔㒜㒧ᵰҹ#ᠧ༈ˈгϡ䅸Ўᅗᰃ乘໘⧚ᣛҸDŽ ᰃֱᣕϡবDŽ ᶤϾᷛ䆚ヺ೼ᶤϾᠽሩЁ㹿᳓ᤶৢˈݡ⃵ᠿᦣᑊݡ⃵䘛ࠄℸᷛ䆚ヺᯊϡݡᇍ݊ᠻ㸠᳓ᤶˈ㗠 ᇍ䖭ϸ⾡㉏ൟⱘᅣˈ䛑㽕䞡໡ᠿᦣ᳓ᤶ䆄োᑣ߫ҹᶹᡒ᳈໮ⱘᏆᅮНᷛ䆚ヺDŽԚᰃDŽᔧ ᤶ䆄োᑣ߫ⱘᓔ༈៪㒧ሒDŽ ᮴ᬜˈ៪㗙㒧ᵰձ䌪Ѣ##䖤ㅫヺⱘ໘⧚乎ᑣˈ߭㒧ᵰ≵᳝ᅮНDŽৠᯊˈ##гৃҹϡߎ⦄೼᳓ ݊ࠡৢⱘぎⱑヺ䛑ߴ䰸ᥝˈҹ֓ᇚⳌ䚏䆄ো䖲᥹䍋ᴹᔶ៤ϔϾᮄ䆄োDŽབᵰ䖭ḋѻ⫳ⱘ䆄ো ݊⃵ˈ᮴䆎ા⾡ᅣⱘᅮН䆄োᑣ߫Ёࣙ৿ϔϾ##䖤ㅫヺˈ೼ᔶᓣখ᭄᳓ᤶৢ䛑㽕ᡞ##ঞ 䞣ϸ䖍៪ݙ䚼ⱘ↣Ͼঠᓩো˄"˅៪ড᭰ᴴ˄\˅ࠡ䴶䛑㽕ᦦܹϔϾড᭰ᴴ˄\˅DŽ ੠ᔶᓣখ᭄ᷛ䆚ヺᇚ㹿⫼ᓩোᓩ䍋ᴹⱘᅲ䰙খ᭄᳓ᤶDŽᅲ䰙খ᭄ЁⱘᄫヺІᄫ䴶ؐǃᄫヺᐌ ᥹ᰃϔϾ#ヺো˄ᅗӀП䯈≵᳝ぎⱑヺ˅ˈⳌᑨᔶᓣখ᭄ⱘϸ䖍ᇚ㹿ࡴϞঠᓩো˄"˅ˈ䱣ৢˈ# བᵰ᳓ᤶ䆄োᑣ߫ЁⱘᶤϾᔶᓣখ᭄ࠡ䴶ⳈˈܜϸϾ⡍⅞ⱘ䖤ㅫヺӮᕅડ᳓ᤶ䖛⿟DŽ佪 㽕ᇍᅣ䇗⫼ⱘᅲ䰙খ᭄䆄ো䖯㸠Ẕᶹˈᑊ೼ᖙ㽕ᯊ䖯㸠ᠽሩDŽ ᤶᑣ߫Ёⱘᔶᓣখ᭄ⱘࠡ䴶᳝ϔϾ#ヺোˈ៪㗙݊ࠡ䴶៪ৢ䴶᳝ϔϾ##ヺোˈ৺߭ˈ೼ᦦܹࠡ োᑣ߫ᇚ᳓ᤶ᳾⫼ᓩোᓩ䍋ᴹⱘⳌᑨᔶᓣখ᭄ⱘᷛ䆚ヺ˄ԡѢᅣⱘ᳓ᤶ䆄োᑣ߫Ё˅DŽ䰸䴲᳓ Ⳃऍ䜡DŽᅲ䰙খ᭄㹿ߚ⾏ৢˈࠡᇐ੠ሒ䚼ⱘぎⱑヺᇚ㹿ߴ䰸DŽ䱣ৢˈ⬅৘ᅲ䰙খ᭄ѻ⫳ⱘ䆄 ⧚䖛⿟Ёˈᅲ䰙খ᭄ϡ䖯㸠ᅣᠽሩDŽᅣ䇗⫼ᯊˈᅲ䰙খ᭄ⱘ᭄Ⳃᖙ乏ϢᅮНЁᔶᓣখ᭄ⱘ᭄ ᰃ⫼䗫োߚ䱨ⱘ䆄োᑣ߫ˈ⫼ᓩো៪ጠ༫ⱘᣀোᣀ䍋ᴹⱘ䗫োϡ㛑⫼Ѣߚ䱨ᅲ䰙খ᭄DŽ೼໘ ৢ⫼ϔᇍ೚ᣀোᣀ䍋ᴹⱘǃ⬅䗫োߚ䱨ⱘ䆄োᑣ߫ህᵘ៤њϔϾᅣ䇗⫼DŽᅣ䇗⫼ⱘᅲ䰙খ᭄ ᣝ✻㄀Ѡ⾡ᔶᓣᅮНᅣᯊˈᅣᷛ䆚ヺ˄ৢ䴶ৃҹ䎳ϔϾぎⱑヺˈぎⱑヺᰃৃ䗝ⱘ˅ঞ݊ ⱘᷛ䆚ヺ˅ᑊϡӮᇐ㟈䫭䇃DŽ Ѣপ⍜ᷛ䆚ヺⱘ乘໘⧚఼ᅮНDŽᇚ#undef ᑨ⫼Ѣ᳾ⶹᷛ䆚ヺ˄े᳾⫼#define ᣛҸᅮН⫼ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᇍϔϾ⿟ᑣⱘᶤѯ䚼ߚৃҹ䖯㸠ᴵӊ㓪䆥ˈᴵӊ㓪䆥ⱘ䇁⊩ᔶᓣབϟ˖ A.12.5 ᴵӊ㓪䆥 #include ᭛ӊৃҹጠ༫DŽ 乏㹿㾷䞞Ў<...>៪"..."ϸ⾡ᔶᓣПϔˈ✊ৢݡᣝ✻Ϟ䗄ᮍᓣ䖯㸠Ⳍᑨⱘ໘⧚DŽ 䛑ϡৠˈᅗᇚᣝ✻ᠽሩ᱂䗮᭛ᴀⱘᮍᓣᠽሩ䆄োᑣ߫䖯㸠㾷䞞DŽ䆄োᑣ߫ᖙމৠϞ䗄ϸ⾡ᚙ #include 䆄োᑣ߫ ᳔ৢˈϟ߫ᔶᓣⱘᣛҸ㸠˖ ᰃ≵᳝ᅮНⱘˈԚৃҹՓ⫼ᄫヺ>DŽ ᅮⱘ᭛ӊˈ߭ᣝ✻㄀ϔ⾡ᅮНⱘᮍᓣ໘⧚DŽབᵰ᭛ӊৡЁࣙ৿ᄫヺ'ǃ\ǃ៪/*ˈ݊㒧ᵰҡ✊ Ң⑤᭛ӊⱘԡ㕂ᓔྟ᧰㋶ᣛᅮ᭛ӊ˄᧰㋶䖛⿟Ϣ݋ԧⱘᅲ⦄Ⳍ݇˅ˈབᵰ≵᳝ᡒࠄᣛܜ佪 #include "᭛ӊৡ" ㉏Ԑഄˈϟ߫ᔶᓣⱘ᥻ࠊᣛҸ˖ ᡒⱘԡ㕂Ϣ݋ԧⱘᅲ⦄Ⳍ݇DŽ ヺ"ǃ'ǃ\ǃ៪/*ˈ߭݊㸠Ў≵᳝ᅮНDŽ乘໘⧚఼ᇚ೼ᶤѯ⡍ᅮⱘԡ㕂ᶹᡒᣛᅮⱘ᭛ӊˈᶹ ᇚᡞ䆹㸠᳓ᤶЎ᭛ӊৡᣛᅮⱘ᭛ӊⱘݙᆍDŽ᭛ӊৡϡ㛑ࣙ৿>៪ᤶ㸠ヺDŽབᵰ᭛ӊৡЁࣙ৿ᄫ #include <᭛ӊৡ> ϟ߫ᔶᓣⱘ᥻ࠊᣛҸ˖ A.12.4 ᭛ӊࣙ৿ ㉏ԐഄˈABSDIFF(ABSDIFF(a,b),c)ᇚ⫳៤᠔ᳳᳯⱘ㒣ᅠܼᠽሩৢⱘ㒧ᵰDŽ ϡࣙ৿##䖤ㅫヺDŽ ៥Ӏህৃҹᕫࠄℷ⹂ⱘ㒧ᵰDŽxcat(xcat(1, 2), 3)ᇚ⫳៤ 123ˈ಴Ў xcat 㞾䑿ⱘᠽሩ #define xcat(x, y) cat(x,y) ো䖲᥹㗠៤DŽབᵰݡᓩܹ㄀ѠሖⱘᅣᅮНˈབϟ᠔⼎˖ ᑊϨˈ)3 (ϡᰃϔϾড়⊩ⱘ䆄োˈᅗ⬅㄀ϔϾখ᭄ⱘ᳔ৢϔϾ䆄োϢ㄀ѠϾখ᭄ⱘ㄀ϔϾ䆄 cat ( 1 , 2 )3 Н˖##䰏ℶњ໪ሖ䇗⫼ⱘখ᭄ⱘᠽሩDŽ಴ℸˈᅗᇚ⫳៤ϟ߫䆄োІ˖ 䙷Мˈᅣ䇗⫼ cat(var, 123)ᇚ⫳៤ var123DŽԚᰃˈᅣ䇗⫼ cat(cat(1,2),3)≵᳝ᅮ #define cat(x, y) x ## y 䱣ৢˈ䆹㒧ᵰᇚ㹿䖲᥹ЎϔϾऩϾⱘᄫヺІDŽ㒭ᅮϟ߫ᅮН˖ usr/tmp" "%s"/" w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᑊϨ݊Ёϡࣙ৿ sizeofǃᔎࠊ㉏ൟ䕀ᤶ䖤ㅫヺ៪ᵮВᐌ䞣DŽ 䖯㸠Ϟ䗄໘⧚Пৢⱘᐌ䞣㸼䖒ᓣ˄খ㾕 A.7.19 㡖˅⒵䎇ϟ߫䰤ࠊᴵӊ˖ᅗᖙ乏ᰃᭈൟˈ ᮴ヺো䭓ᭈൟⱘ᪡԰᭄П䯈䖯㸠ⱘ䖤ㅫDŽ Ͼᭈൟᐌ䞣䛑㹿乘໘⧚఼䅸Ў݊ৢ䴶䎳᳝ৢ㓔 Lˈҹ֓ᡞ᠔᳝ⱘㅫᴃ䖤ㅫ䛑ᔧ԰ᰃ೼䭓ᭈൟ៪ ߭ˈ⫼ 0 ᳓ᤶDŽ乘໘⧚఼䖯㸠ᅣᠽሩПৢҡ✊ᄬ೼ⱘӏԩᷛ䆚ヺ䛑ᇚ⫼ 0 ᴹ᳓ᤶDŽ᳔ৢˈ↣ 䛑ᇚ೼ᠻ㸠ᅣᠿᦣПࠡ䖯㸠᳓ᤶˈབᵰ䆹ᷛ䆚ヺ೼乘໘⧚఼ЁᏆ㒣ᅮНˈ߭⫼ 1 ᳓ᤶᅗˈ৺ Defined(ᷛ䆚ヺ) ៪ defined ᷛ䆚ヺ #if ੠#elif Ёⱘᐌ䞣㸼䖒ᓣᇚᠻ㸠䗮ᐌⱘᅣ᳓ᤶDŽᑊϨˈӏԩϟ߫ᔶᓣⱘ㸼䖒ᓣ˖ ⱘߚᬃ˅᥻ࠊⱘ᭛ᴀ䛑ᇚ㹿ᗑ⬹DŽ؛ӊؐЎ ҸПৢⱘ᭛ᴀDŽ䰸њᇍᴵӊ㓪䆥ᣛҸⱘጠ༫䖯㸠ẔᶹП໪ˈᴵӊ㓪䆥ᣛҸⱘ᮴ᬜߚᬃ˄ेᴵ ᐌ䞣㸼䖒ᓣⱘؐ䛑Ў 0ˈᑊϨ䆹ᴵӊ㓪䆥ᣛҸ䫒Ёࣙ৿ϔᴵ#else ᣛҸDŽ߭ᇚ䗝ᢽ#else ᣛ ⱘ㓪䆥䰊↉Փ⫼ᯊˈৢ㓁ⱘ#elif ੠#else ᴵӊ㓪䆥ᣛҸঞⳌᑨⱘ᭛ᴀᇚ㹿ᬒᓗDŽབᵰ᠔᳝ থ⦄ᶤϾ#if ៪#elif ᴵӊ㓪䆥ᣛҸЁⱘᐌ䞣㸼䖒ᓣⱘؐϡЎ 0ˈᑊ䗝ᢽ݊ৢⱘ᭛ᴀկҹৢ ϡሲѢᴵӊ㓪䆥ᣛҸ㒧ᵘⱘ⿟ᑣҷⷕˈᅗৃҹࣙ৿乘໘⧚ᣛҸˈгৃҹЎぎDŽϔᮺ乘໘⧚఼ ੠#elif ᣛҸПৢⱘ᭛ᴀᇚᣝ✻݊ᅗ᱂䗮⿟ᑣҷⷕϔḋ䖯㸠㓪䆥DŽ೼䖭䞠ˈĀ᭛ᴀāᰃᣛӏԩ 䞣㸼䖒ᓣЎ䴲 0 ؐЎℶˈ䖭ᯊᇚᬒᓗؐЎ 0 ⱘᣛҸ㸠ৢ䴶ⱘ᭛ᴀDŽᐌ䞣㸼䖒ᓣϡЎ 0 ⱘ#if 乘໘⧚఼ձ⃵ᇍ#if ҹঞৢ㓁ⱘ#elif 㸠Ёⱘᐌ䞣㸼䖒ᓣ䖯㸠䅵ㅫˈⳈࠄথ⦄ᶤϾᣛҸⱘᐌ ݊Ёˈ↣Ͼᴵӊ㓪䆥ᣛҸ˄if 㸠ǃelif 㸠ǃelse 㸠ҹঞ#endif˅೼⿟ᑣЁഛऩ⣀ऴϔ㸠DŽ #else else 㸠˖ else 㸠 ᭛ᴀ else 䚼ߚ˖ #elif ᐌ䞣㸼䖒ᓣ elif 㸠˖ elif 㸠 ᭛ᴀ elif 䚼ߚ opt elif 䚼ߚ˖ #ifndef ᷛ䆚ヺ #ifdef ᷛ䆚ヺ #if ᐌ䞣㸼䖒ᓣ if 㸠˖ if 㸠᭛ᴀ elif 䚼ߚ opt else 䚼ߚ opt #endif 乘໘⧚఼ᴵӊ˖ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! # ϟ߫ᔶᓣⱘ乘໘⧚఼㸠ϡᠻ㸠ӏԩ᪡԰˖ A.12.9 ぎᣛҸ ᇚՓ乘໘⧚఼ᠻ㸠ϔϾϢ݋ԧᅲ⦄Ⳍ݇ⱘ᪡԰DŽ᮴⊩䆚߿ⱘ pragma˄㓪䆥ᣛ⼎˅ᇚ㹿ᗑ⬹ᥝDŽ #pragma 䆄োᑣ߫ opt ϟ߫ᔶᓣⱘ᥻ࠊᣛҸ˖ A.12.8 pragma ᇚՓ乘໘⧚఼ᠧॄࣙ৿䆹䆄োᑣ߫ⱘ䆞ᮁֵᙃDŽ #error 䆄োᑣ߫ opt ϟ߫ᔶᓣⱘ乘໘⧚఼᥻ࠊᣛҸ˖ A.12.7 䫭䇃ֵᙃ⫳៤ 䖯㸠ᠽሩˈ✊ৢݡ䖯㸠㾷䞞DŽܜϡᬍবᔧࠡ㓪䆥ⱘ⑤᭛ӊⱘৡᄫDŽ㸠Ёⱘᅣᇚ ߎⱘˈᑊϨˈᔧࠡⱘ䕧ܹ᭛ӊᰃ⬅䆹ᷛ䆚ヺੑৡⱘDŽབᵰ㔎ᇥᏺঠᓩোⱘ᭛ӊৡ䚼ߚˈ߭ᇚ ᇚՓ㓪䆥఼䅸Ў˄ߎѢ䫭䇃䆞ᮁⱘⳂⱘ˅˖ϟϔ㸠⑤ҷⷕⱘ㸠োᰃҹक䖯ࠊᭈൟᐌ䞣ⱘᔶᓣ㒭 #line ᐌ䞣 #line ᐌ䞣 "᭛ӊৡ" Ўњ֓Ѣ݊ᅗ乘໘⧚఼⫳៤ C 䇁㿔⿟ᑣˈϟ߫ᔶᓣⱘᣛҸ㸠˖ A.12.6 㸠᥻ࠊ њDŽdefined 乘໘⧚఼䖤ㅫヺгᰃ ANSI Ёᮄᓩܹⱘ⡍ᕕDŽ 䇈ᯢ˖#elif ᰃ ANSI Ёᮄᓩܹⱘᴵӊ㓪䆥ᣛҸˈԚℸࠡᅗᏆ㒣೼ᶤѯ乘໘⧚఼Ёᅲ⦄ #if !defined ᷛ䆚ヺ #if defined ᷛ䆚ヺ ߚ߿ㄝӋѢ˖ #ifndef ᷛ䆚ヺ #ifdef ᷛ䆚㩟 ϟ߫᥻ࠊᣛҸ˖ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ໄᯢ ໄᯢ㸼˖ ໄᯢ䇈ᯢヺ ߱ྟ࣪ໄᯢヺ㸼 opt; ໄᯢ˖ ໄᯢ䇈ᯢヺ opt ໄᯢヺໄᯢ㸼 opt ໡ড়䇁হ ߑ᭄ᅮН˖ ໄᯢ ߑ᭄ᅮН ໪䚼ໄᯢ˖ ໪䚼ໄᯢ ܗ㗏䆥ऩ ໪䚼ໄᯢ ܗ㗏䆥ऩ さDŽކ఼᥹ফˈԚ⬅Ѣ if•else ⱘ℻Нᗻ䯂乬ˈ䖬ᄬ೼ϔ໘ ᷛ䆚ヺāˈ䖭ḋህՓᕫ݊Ёⱘ㉏ൟᅮНৡ៤ЎϾ㒜㒧ヺDŽ䆹䇁⊩ৃ㹿 YACC 䇁⊩ߚᵤ⿟ᑣ⫳៤ Ͼᏺ᳝ opt ヺোˈϔϾ≵᳝ opt ヺোDŽ䖭䞠䖬᳝ϔϾব࣪ˈेߴ䰸њѻ⫳ᓣĀ㉏ൟᅮНৡ: “one of”㒧ᵘˈᑊ˄ḍ᥂䇁⊩ߚᵤ⿟ᑣ⫳៤఼ⱘ㾘߭˅໡ࠊ↣Ͼᏺ᳝ opt ヺোⱘѻ⫳ᓣ˖ϔ ᑣ⫳៤఼ৃҹ᥹ফⱘ䕧ܹDŽ䰸њ๲ࡴ䇁⊩䆄ো䇈ᯢѻ⫳ᓣЁⱘ׭䗝乍໪ˈ䖬䳔㽕ᠽሩ݊Ёⱘ ᐌ䞣DŽҹᠧᄫᄫԧᔶᓣ㸼⼎ⱘऩ䆡੠ヺোᰃ㒜㒧ヺDŽᴀ䇁⊩ৃҹⳈ᥹䕀ᤶЎ㞾ࡼ䇁⊩ߚᵤ⿟ ᴀ䇁⊩≵᳝ᅮНϟ߫㒜㒧ヺ˖ᭈൟᐌ䞣ǃᄫヺᐌ䞣ǃ⍂⚍ᐌ䞣ǃᷛ䆚ヺǃᄫヺІ੠ᵮВ ᑣ᳝ϔѯ䇗ᭈDŽ 䖭ϔ䚼ߚⱘݙᆍᇚㅔ㽕ὖ䗄ᴀ䰘ᔩࠡ䴶䚼ߚЁ䆆䗄ⱘ䇁⊩DŽᅗӀⱘݙᆍᅠܼⳌৠˈԚ乎 A.13 䇁⊩ ࠡᏆ㒣೼ᶤѯ㓪䆥఼Ёᅲ⦄DŽܜᰃᮄᓩܹⱘˈ݊Ёⱘϔѯᅣ Ёᮄᓩܹⱘ⡍ᕕDŽ䖭ѯ乘ᅮঝⱘ乘໘⧚఼ᅣгޚ䇈ᯢ˖#error Ϣ#pragma ᰃ ANSI ᷛ ⱘᅲ⦄Ё䆹ᷛ䆚ヺᠡ㹿ᅮНЎ 1DŽޚ__STDC__ ᭈൟᐌ䞣 1DŽা᳝೼䙉ᕾᷛ __TIME__ ࣙ৿㓪䆥ᯊ䯈ⱘᄫヺІᄫ䴶ؐˈ݊ᔶᓣЎ“hh:mm:ssāDŽ __DATE__ ࣙ৿㓪䆥᮹ᳳⱘᄫヺІᄫ䴶ؐˈ݊ᔶᓣЎ“Mmm dd yyyyāDŽ __FILE__ ࣙ৿ℷ೼㹿㓪䆥ⱘ⑤᭛ӊৡᄫⱘᄫヺІᄫ䴶ؐDŽ __LINE__ ࣙ৿ᔧࠡ⑤᭛ӊ㸠᭄ⱘक䖯ࠊᐌ䞣DŽ defined ϔḋˈϡ㛑প⍜ᅮН៪䞡ᮄ䖯㸠ᅮНDŽ ᶤѯᷛ䆚ヺᰃ乘ᅮНⱘˈᠽሩৢᇚ⫳៤⡍ᅮⱘֵᙃDŽᅗӀৠ乘໘⧚఼㸼䖒ᓣ䖤ㅫヺ A.12.10乘ᅮНৡᄫ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ㉏ൟ䇈ᯢヺ 䇈ᯢヺ䰤ᅮヺ㸼 opt 䇈ᯢヺ䰤ᅮヺ㸼˖ 䇈ᯢヺ䰤ᅮヺ㸼 㒧ᵘໄᯢヺ㸼; 㒧ᵘໄᯢ˖ ໄᯢヺ=߱ྟ࣪ヺ ໄᯢヺ ߱ྟ࣪ໄᯢヺ˖ ߱ྟ࣪ໄᯢヺ㸼, ߱ྟ࣪ໄᯢヺ ߱ྟ࣪ໄᯢヺ ߱ྟ࣪ໄᯢヺ㸼 㒧ᵘໄᯢ㸼 㒧ᵘໄᯢ 㒧ᵘໄᯢ 㒧ᵘໄᯢ㸼˖ struct union 㒧ᵘ៪㘨ড়˖one of 㒧ᵘ៪㘨ড় ᷛ䆚ヺ 㒧ᵘ៪㘨ড় ᷛ䆚ヺ opt {㒧ᵘໄᯢ㸼} 㒧ᵘ៪㘨ড়䇈ᯢヺ˖ const volatile ㉏ൟ䰤ᅮヺ˖one of unsigned 㒧ᵘ៪㘨ড়䇈ᯢヺ ᵮВ䇈ᯢヺ ㉏ൟᅮНৡ void char short int long float double signed ㉏ൟ䇈ᯢヺ˖one of auto register static extern tyedef ㉏䇈ᯢヺ˖one ofټᄬ ㉏ൟ䰤ᅮヺ ໄᯢ䇈ᯢヺ opt ㉏ൟ䇈ᯢヺ ໄᯢ䇈ᯢヺ opt ㉏䇈ᯢヺ ໄᯢ䇈ᯢヺ optټᄬ ໄᯢ䇈ᯢヺ˖ ໄᯢ㸼 ໄᯢ ㉏ൟ䰤ᅮヺ 䇈ᯢヺ䰤ᅮヺ㸼 opt 㒧ᵘໄᯢヺ㸼˖ 㒧ᵘໄᯢヺ 㒧ᵘໄᯢヺ㸼, 㒧ᵘໄᯢヺ 㒧ᵘໄᯢヺ˖ ໄᯢヺ ໄᯢヺ opt: ᐌ䞣㸼䖒ᓣ ᬝВ䇈ᯢヺ enum ᷛ䆚ヺ opt {ᵮВヺ㸼} enum ᷛ䆚ヺ ᵮВヺ㸼˖ ᵮВヺ ᵮВヺ㸼, ᵮВヺ ᵮВヺ ᷛ䆚ヺ ᷛ䆚ヺ=ᐌ䞣㸼䖒ᓣ ໄᯢヺ ᣛ䩜 opt Ⳉ᥹ໄᯢヺ Ⳉ᥹ໄᯢヺ˖ ᷛ䆚ヺ (ໄᯢヺ) Ⳉ᥹ໄᯢヺ[ᐌ䞣㸼䖒ᓣ] Ⳉ᥹ໄᯢヺ(ᔶᓣখ᭄㉏ൟ㸼) Ⳉ᥹ໄᯢヺ(ᷛ䆚ヺ㸼 opt) ᣛ䩜˖ * ㉏ൟ䰤ᅮヺ㸼 opt * ㉏ൟ䰤ᅮヺ㸼 opt ᣛ䩜 ㉏ൟ䰤ᅮヺ㸼˖ ㉏ൟ䰤ᅮヺ ㉏ൟ䰤ᅮヺ㸼 ㉏ൟ䰤ᅮヺ Click to buy NOW! PDF•XC H ANGE w w w . d o c u • t r a c k.com w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᷛ䆚ヺ ㉏ൟᅮНৡ˖ Ⳉ᥹ᢑ䈵ໄᯢヺ opt (ᔶᓣখ᭄㉏ൟ㸼 opt) Ⳉ᥹ᢑ䈵ໄᯢヺ opt [ᐌ䞣㸼䖒ᓣ] (ᢑ䈵ໄᯢヺ) Ⳉ᥹ᢑ䈵ໄᯢヺ˖ ᣛ䩜 opt Ⳉ᥹ᢑ䈵ໄᯢヺ ᣛ䩜 ᢑ䈵ໄᯢヺ˖ 䇈ᯢヺ䰤ᅮヺ㸼 ᢑ䈵ໄᯢヺ opt ㉏ൟৡ˖ ߱ؐ㸼, ߱ؐ ߱ؐ ߱ؐ㸼˖ {߱ؐ㸼, } {߱ؐ㸼} 䌟ؐ㸼䖒ᓣ ߱ؐ˖ ᷛ䆚ヺ㸼, ᷛ䆚ヺ ᷛ䆚ヺ ᷛ䆚ヺ㸼˖ ໄᯢ䇈ᯢヺ ᢑ䈵ໄᯢヺ opt ໄᯢ䇈ᯢヺ ໄᯢヺ ᔶᓣখ᭄ໄᯢ˖ ᔶᓣখ᭄㸼, ᔶᓣখ᭄ໄᯢ ᔶᓣখ᭄㸼ໄᯢ ᔶᓣখ᭄㸼˖ ᔶᓣখ᭄㸼, ... ᔶᓣখ᭄㸼 ᔶᓣখ᭄㉏ൟ㸼˖ 䇁হ˖ ᏺᷛো䇁হ 㸼䖒ᓣ䇁হ ໡ড়䇁হ 䗝ᢽ䇁হ ᕾ⦃䇁হ 䏇䕀䇁হ ᏺᷛো䇁হ˖ ᷛ䆚ヺ: 䇁হ case ᐌ䞣㸼䖒ᓣ䇁হ dafault: 䇁হ 㸼䖒ᓣ䇁হ˗ 㸼䖒ᓣ opt; ໡ড়䇁হ˖ {ໄᯢ㸼 opt 䇁হ㸼 opt} 䇁হ㸼˖ 䇁হ 䇁হ㸼 䇁হ 䗝ᢽ䇁হ˖ if (㸼䖒ᓣ) 䇁হ if (㸼䖒ᓣ) 䇁হ else 䇁হ switch (㸼䖒ᓣ) 䇁হ ᕾ⦃䇁হ while (㸼䖒ᓣ) 䇁হ do 䇁হ while (㸼䖒ᓣ); for (㸼䖒ᓣ opt; 㸼䖒ᓣ opt; 㸼䖒ᓣ opt) 䇁হ 䏇䕀䇁হ˖ goto ᷛ䆚ヺ; continue; break; Click to buy NOW! PDF•XC H ANGE w w w . d o c u • t r a c k.com w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! Ⳍㄝ㉏㸼䖒ᓣ˖ ᣝԡϢ㸼䖒ᓣ&Ⳍㄝ㉏㸼䖒ᓣ Ⳍㄝ㉏㸼䖒ᓣ ᣝԡϢ㸼䖒ᓣ˖ ᥹ԡᓖ៪㸼䖒ᓣ^ᣝԡϢ㸼䖒ᓣ ᣝԡϢ㸼䖒ᓣ ᥹ԡᓖ៪㸼䖒ᓣ˖ ᣝԡ៪㸼䖒ᓣ|ᣝԡᓖ៪㸼䖒ᓣ ᣝԡᓖ៪㸼䖒ᓣ ᣝԡ៪㸼䖒ᓣ˖ 䘏䕥Ϣ㸼䖒ᓣ&&ᣝԡ៪㸼䖒ᓣ ᥹ԡ៪㸼䖒ᓣ 䘏䕥Ϣ㸼䖒ᓣ 䘏䕥៪㸼䖒ᓣ||䘏䕥Ϣ㸼䖒ᓣ 䘏䕥Ϣ㸼䖒ᓣ 䘏䕥៪㸼䖒ᓣ˖ ᴵӊ㸼䖒ᓣ ᐌ䞣㸼䖒ᓣ˖ ៪㸼䖒ᓣ?㸼䖒ᓣ:ᴵӊ㸼䖒ᓣ 䘏䕥៪㸼䖒ᓣ ᴵӊ㸼䖒ᓣ˖ = *= /= %= += •= <<= >>= &= ^= |= 䌟ؐ䖤ㅫヺ˖one of 㸼䖒ᓣ 䌟ؐ䖤ㅫヺ 䌟ؐ㸼䖒ᓣܗϔ ᴵӊ㸼䖒ᓣ 䌟ؐ㸼䖒ᓣ˖ 㸼䖒ᓣ, 䌟ؐ㸼䗝ᓣ 䌟ؐ㸼䖒ᓣ 㸼䖒ᓣ˖ return 㸼䖒ᓣ opt; ݇㋏㸼䖒ᓣ Ⳍㄝ㉏㸼䖒ᓣ==݇㋏㸼䖒ᓣ Ⳍㄝ㉏㸼䖒ᓣ!=݇㋏㸼䖒ᓣ ݇㋏㸼䖒ᓣ˖ ⿏ԡ㸼䖒ᓣ ݇㋏㸼䖒ᓣ<⿏ԡ㸼䖒ᓣ ݇㋏㸼䖒ᓣ>⿏ԡ㸼䖒ᓣ ݇㋏㸼䖒ᓣ<=⿏ԡ㸼䖒ᓣ ݇㋏㸼䖒ᓣ>=⿏ԡ㸼䖒ᓣ ⿏ԡ㸼䖒ᓣ ࡴ⊩㉏㸼䖒ᓣ ⿏ԡ㸼䖒ᓣ<<ࡴ⊩㉏㸼䖒ᓣ ⿏ԡ㸼䖒ᓣ>>ࡴ⊩㉏㸼䖒ᓣ ࡴ⊩㉏㸼䖒ᓣ˖ Ь⊩㉏㸼䖒ᓣ ࡴ⊩㉏㸼䖒ᓣ+Ь⊩㉏㸼䖒ᓣ ࡴ⊩㉏㸼䖒ᓣ•Ь⊩㉏㸼䖒ᓣ Ь⊩㉏㸼䖒ᓣ˖ ᔎࠊ㉏ൟ䕀ᤶ㸼䖒ᓣ ㉏㸼䖒ᓣ*ᔎࠊ㉏ൟ䕀ᤶ㸼䖒ᓣ Ь⊩㉏㸼䖒⚍/ᔎࠊ㉏ൟ䕀ᤶ㸼䖒ᓣ Ь⊩㉏㸼䖒ᓣ%ᔎࠊ㉏ൟ䕀ᤶ㸼䖒ᓣ ᔎࠊ㉏ൟ䕀ᤶ㸼䖒ᓣ˖ ϔܗ㸼䖒ᓣ (㉏ൟৡ)ᔎࠊ㉏ൟ䕀ᤶ㸼䖒ᓣ ϔܗ㸼䖒ᓣ˖ ৢ㓔ϔܗᓣ ++ϔܗ㸼䖒ᓣ ••ϔܗ㸼䖒ᓣ ϔܗ䖤ㅫヺᔎࠊ㉏ൟ䕀ᤶ㸼䖒ᓣ Click to buy NOW! PDF•XC H ANGE w w w . d o c u • t r a c k.com w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! =define ᷛ䆚ヺ(ᷛ䆚ヺ㸼 opt) 䆄োᑣ߫ =define ᷛ䆚ヺ 䆄োᑣ߫ ᥻ࠊᣛҸ˖ োĀ᭛ᴀā˄े䗮ᐌⱘ⿟ᑣ᭛ᴀ˅ǃ䴲ᴵӊ乘໘⧚఼᥻ࠊᣛҸ៪ᅠᭈⱘ乘໘⧚఼ᴵӊ㒧ᵘDŽ ϟ߫乘໘⧚఼䇁⊩ᘏ㒧њ᥻ࠊᣛҸⱘ㒧ᵘˈԚϡ䗖ড়ѢᴎẄ࣪ⱘ䇁⊩ߚᵤDŽ݊Ёࣙ৿ヺ ᵮВᐌ䞣 ⍂⚍ᐌ䞣 ᄫヺᐌ䞣 ᭈൟᐌ䞣 ᐌ䞣˖ খ᭄㸼䖒ᓣ㸼, 䌟ؐ㸼䖒ᓣ 䌟ؐ㸼䖒ᓣ খ᭄㸼䖒ᓣ㸼˖ (㸼䖒ᓣ) ᄫヺІ ᐌ䞣 ᷛ䆚ヺ ߱ㄝ㸼䖒ᓣ˖ ৢ㓔㸼䖒ᓣ•• ৢ㓔㸼䖒ᓣ++ ৢ㓔㸼䖒ᓣ•>ᷛ䆚ヺ ৢ㓔㸼䖒ᓣ.ᷛ䆚ヺ ৢ㓔㸼䖒ᓣ(খ᭄㸼䖒ᓣ㸼 opt) ৢ㓔㸼䖒ᓣ[㸼䖒ᓣ] ߱ㄝ㸼䖒ᓣ ৢ㓔㸼䖒ᓣ˖ & * + • ~ ! 䖤ㅫヺ˖one ofܗϔ sizeof(㉏ൟৡ) 㸼䖒ᓣܗsizeof ϔ #undef ᷛ䆚ヺ #include <᭛ӊৡ> #include "᭛ӊৡ" #include 䆄োᑣ߫ #line ᐌ䞣 "᭛ӊৡ" #line ᐌ䞣 #error 䆄োᑣ߫DŽ #pragma 䆄োᑣ߫DŽ # 乘໘⧚఼ᴵӊᣛҸ 乘໘⧚఼ᴵӊᣛҸ˖ if 㸠 ᭛ᴀ elif 䚼ߚ opt else 䚼ߚ opt #endif if 㸠˖ #if ᐌ䞣㸼䖒ᓣ #ifdef ᷛ䆚ヺ #ifndef ᷛ䆚ヺ elif 䚼ߚ˖ elif 㸠 ᭛ᴀ elif 䚼ߚ opt elif 㸠˖ #elif ᐌ䞣㸼䖒ᓣ else 䚼ߚ˖ else 㸠 ᭛ᴀ else 㸠˖ #else Click to buy NOW! PDF•XC H ANGE w w w . d o c u • t r a c k.com w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ⿟ᑣᓔྟᠻ㸠ᯊˈstdinǃstdout ੠ stderr 䖭 3 Ͼ⌕Ꮖ㒣໘Ѣᠧᓔ⢊ᗕDŽ ϟˈ៥Ӏ೼ϟ᭛ЁᇚϡݡऎߚĀ᭛ӊᣛ䩜ā੠Ā⌕āDŽމ䍋℻Нⱘᚙ ᭛ӊᇚ䖨ಲϔϾᣛ৥ FILE ㉏ൟᇍ䈵ⱘᣛ䩜ˈ䆹ᣛ䩜䆄ᔩњ᥻ࠊ䆹⌕ⱘ᠔᳝ᖙ㽕ֵᙃˈ೼ϡᓩ ᠧᓔϔϾ⌕ˈᇚᡞ䆹⌕ϢϔϾ᭛ӊ៪䆒໛䖲᥹䍋ᴹˈ݇䯁⌕ᇚᮁᓔ䖭⾡䖲᥹ˈᠧᓔϔϾ ⱘݙᆍᅠܼⳌৠDŽ ᑊ݋᳝ϟ߫ᗻ䋼˖བᵰ೼ৠϔ㋏㒳ЁݭܹѠ䖯ࠊ⌕ˈ✊ৢݡ䇏প䆹Ѡ䖯ࠊ⌕ˈ߭䇏ߎ੠ݭܹ ᔶᓣ䕀ᤶЎ᭛ᴀ⌕DŽѠ䖯ࠊ⌕ᰃ⬅᳾㒣໘⧚ⱘᄫ㡖ᵘ៤ⱘᑣ߫ˈ䖭ѯᄫ㡖䆄ᔩⴔݙ䚼᭄᥂ˈ ৃ㛑䳔㽕ᇚ᭛ᴀ⌕䕀ᤶЎ݊ᅗ㸼⼎ᔶᓣ˄՟བᡞ'\n'᯴ᇘ៤ಲ䔺ヺ੠ᤶ㸠ヺ˅ˈ៪Ң݊ᅗ㸼⼎ ᭛ᴀ⌕ᰃ⬅᭛ᴀ㸠㒘៤ⱘᑣ߫ˈ↣ϔ㸠ࣙ৿ 0 Ͼ៪໮Ͼᄫヺˈᑊҹ'\n' 㒧ሒDŽ೼ᶤѯ⦃๗Ёˈ ᑧҡ✊ᦤկњ䖭ϸ⾡㉏ൟⱘ⌕DŽޚ೼㨫ৡⱘ UNIX ㋏㒳Ё˅ˈ᭛ᴀ⌕੠Ѡ䖯ࠊ⌕ᰃⳌৠⱘˈԚᷛ ⌕˄stream˅ᰃϢ⺕Ⲭ៪݊ᅗ໪ೈ䆒໛݇㘨ⱘ᭄᥂ⱘ⑤៪ⳂⱘഄDŽሑㅵ೼ᶤѯ㋏㒳Ё˄བ ߚПϔDŽ ᑧⱘϝޚ༈᭛ӊЁᅮНⱘ䕧ܹ੠䕧ߎߑ᭄ǃ㉏ൟҹঞᅣⱘ᭄Ⳃ޴ТऴᭈϾᷛ B.1 䕧ܹϢ䕧ߎ˖ ᑧՓ⫼DŽޚݭᄫ↡ᓔ༈ⱘᷛ䆚ヺҹঞҹϸϾϟߦ㒓ᓔ༈ⱘᷛ䆚ヺг䛑ֱ⬭㒭ᷛ ᑧՓ⫼ˈৠᯊˈ݊ᅗ᠔᳝ҹϔϾϟߦ㒓੠ϔϾ໻ޚҹϟߦ㒓ᓔ༈ⱘ໪䚼ᷛ䆚ヺֱ⬭㒭ᷛ ᭛ӊDŽ ᅮНП໪ˈᑊϨˈᖙ乏೼Փ⫼༈᭛ӊЁⱘӏԩໄᯢПࠡࣙ৿༈᭛ӊDŽ༈᭛ӊϡϔᅮᰃϔϾ⑤ ༈᭛ӊⱘࣙ৿乎ᑣᰃӏᛣⱘˈᑊৃࣙ৿ӏᛣ໮⃵DŽ༈᭛ӊᖙ乏㹿ࣙ৿೼ӏԩ໪䚼ໄᯢ៪ #include <༈᭛ӊ> ৃҹ䗮䖛ϟ߫ᮍᓣ䆓䯂༈᭛ӊ˖ ༈᭛ӊЁᅮН˖ޚᑧЁⱘߑ᭄ˈ㉏ൟҹঞᅣߚ߿೼ϟ䴶ⱘᷛޚᷛ ݇ⱘሲᗻDŽ ໛䅼䆎ϢऎඳⳌ݇ⱘϔѯሲᗻˈгህ᯳Ϣᴀഄ䇁㿔ǃ೑㈡៪᭛࣪Ⳍޚヺⱘݙᆍˈৠᯊˈгϡ њϔѯՓ⫼↨䕗ফ䰤ⱘߑ᭄ҹঞϔѯৃҹ䗮䖛݊ᅗߑ᭄ㅔऩড়៤ⱘߑ᭄ˈгⳕ⬹њ໮ᄫ㡖ᄫ C ⱘᅲ⦄Ӯᦤկ䆹ߑ᭄ᑧЁⱘߑ᭄ໄᯢǃ㉏ൟҹঞᅣᅮНDŽ೼䖭䚼ߚݙᆍЁˈ៥Ӏⳕ⬹ ޚᷛ ᑧϡᰃ C 䇁㿔ᴀ䑿ⱘᵘ៤䚼ߚˈԚᰃᬃᣕޚᅮНⱘߑ᭄ᑧDŽᷛޚᴀ䰘ᔩᘏ㒧њ ANSI ᷛ ᑧޚ䰘ᔩBᷛ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! char *tmpnam(char s[L_tmpnam]) 㞾ࡼߴ䰸DŽབᵰ߯ᓎ᪡԰៤ࡳˈ䆹ߑ᭄䖨ಲϔϾ⌕˗བᵰ߯ᓎ᭛ӊ༅䋹ˈ߭䖨ಲ NULLDŽ tmpfile ߑ᭄ҹ῵ᓣ"wb+"߯ᓎϔϾЈᯊ᭛ӊˈ䆹᭛ӊ೼㹿݇䯁៪⿟ᑣℷᐌ㒧ᴳᯊᇚ㹿 FILE *tmpfile(void) rename ߑ᭄ׂᬍ᭛ӊⱘৡᄫDŽབᵰ᪡԰༅䋹ˈ߭䖨ಲϔϾ䴲 0 ؐDŽ int rename(const char *oldname, const char *newname) བᵰߴ䰸᪡԰༅䋹ˈ߭䖨ಲϔϾ䴲 0 ؐDŽ remove ߑ᭄ߴ䰸 filename ᣛᅮⱘ᭛ӊˈ䖭ḋˈৢ㓁䆩೒ᠧᓔ䆹᭛ӊⱘ᪡԰ᇚ༅䋹DŽ int remove(const char *filename) ऎˈ᳔ৢ݇䯁⌕DŽ㢹ߎ䫭߭䖨ಲ EOFˈ৺߭䖨ಲ 0DŽކᑊ䞞ᬒ㞾ࡼߚ䜡ⱘܼ䚼㓧 ऎЁⱘ᠔᳝᳾䇏䕧᭄ܹ᥂ˈކfclose ߑ᭄ᇚ᠔᳝᳾ݭܹⱘ᭄᥂ݭܹ stream Ёˈ϶ᓗ㓧 int fclose(FILE *stream) fflush(NULL)ᇚ⏙⋫᠔᳝ⱘ䕧ߎ⌕DŽ 䕧ܹ⌕ᴹ䇈ˈ݊㒧ᵰᰃ᳾ᅮНⱘDŽབᵰ೼ݭⱘ䖛⿟Ёথ⫳䫭䇃ˈ߭䖨ಲ EOFˈ৺߭䖨ಲ 0DŽ ऎԚᇮ᳾ݭܹ᭛ӊⱘ᠔᭄᳝᥂ݭࠄ᭛ӊЁDŽᇍކᇍ䕧ߎ⌕ᴹ䇈ˈfflush ߑ᭄ᇚᏆݭࠄ㓧 int fflush(FILE *stream) stdinǃstdout ੠ stderr Ⳍ݇㘨ⱘ᭛ӊDŽ stream ᣛᅮⱘ⌕DŽᅗ䖨ಲ stream˗㢹ߎ䫭߭䖨ಲ NULLDŽFreopen ߑ᭄ϔ㠀⫼ѢᬍবϢ freopen ߑ᭄ҹ mode ᣛᅮⱘ῵ᓣᠧᓔ filename ᣛᅮⱘ᭛ӊˈᑊᇚ䆹᭛ӊ݇㘨ࠄ FILE *freopen(const char *filename, const char *mode, FILE *stream) ⃵᳔໮ৃᠧᓔ FOPEN_MAX Ͼ᭛ӊDŽ ߭㸼⼎ᇍѠ䖯ࠊ᭛ӊ䖯㸠᪡԰DŽ᭛ӊৡ filename 䰤ᅮ᳔໮Ў FILENAME_MAX ϾᄫヺDŽϔ ⫼ fflush ߑ᭄៪᭛ӊᅮԡߑ᭄DŽབᵰ೼Ϟ䗄䆓䯂῵ᓣПৢݡࡴϞ bˈབ“ rbā៪“ w+bāㄝˈ 䆌ᇍৠϔ᭛ӊ䖯㸠䇏੠ݭDŽ೼䇏੠ݭⱘѸঝ䖛⿟Ёˈᖙ乏䇗ܕ˅ৢ 3 ⾡ᮍᓣ˄᳈ᮄᮍᓣ "a+" 䗑ࡴ˗ᠧᓔ៪߯ᓎ᭛ᴀ᭛ӊ⫼Ѣ᳈ᮄˈݭ᭛ӊᯊ䗑ࡴࠄ᭛ӊ᳿ሒ "w+" ߯ᓎ᭛ᴀ᭛ӊ⫼Ѣ᳈ᮄˈᑊߴ䰸Ꮖᄬ೼ⱘݙᆍ˄བᵰ᳝ⱘ䆱˅ "r+" ᠧᓔ᭛ᴀ᭛ӊ⫼Ѣ᳈ᮄ˄े䇏੠ݭ˅ "a" 䗑ࡴ˗ᠧᓔ៪߯ᓎ᭛ᴀ᭛ӊˈᑊ৥᭛ӊ᳿ሒ䗑ࡴݙᆍ "w" ߯ᓎ᭛ᴀ᭛ӊ⫼Ѣݭˈᑊߴ䰸Ꮖᄬ೼ⱘݙᆍ˄བᵰ᳝ⱘ䆱˅ "r" ᠧᓔ᭛ᴀ᭛ӊ⫼Ѣ䇏 䆓䯂῵ᓣ mode ৃҹЎϟ߫ড়⊩ؐПϔ˖ 䋹ˈ߭䖨ಲ NULLDŽ fopen ߑ᭄ᠧᓔ filename ᣛᅮⱘ᭛ӊˈᑊ䖨ಲϔϾϢПⳌ݇㘨ⱘ⌕DŽབᵰᠧᓔ᪡԰༅ FILE *fopen(const char *filename, const char *mode) ᮴ヺোᭈൟDŽ ϟ߫ߑ᭄⫼Ѣ໘⧚Ϣ᭛ӊ᳝݇ⱘ᪡԰DŽ݊Ёˈ㉏ൟ size_t ᰃ⬅䖤ㅫヺ sizeof ⫳៤ⱘ B.1.1 ᭛ӊ᪡԰ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! · 䭓ᑺׂ佄ヺ hǃl ៪ LDŽh 㸼⼎ᇚⳌᑨⱘখ᭄ᣝ short ៪ unsigned short ㉏ൟ䕧 ԡ 0 ҹ䖒ࠄ㽕∖ⱘᆑᑺ˅DŽܙ฿ᇍѢᭈൟ᭄ˈᅗᣛᅮᠧॄⱘ᭄ᄫԡ᭄˄ᖙ㽕ᯊৃࡴ ᅗᣛᅮᠧॄⱘᇣ᭄⚍ৢⱘ᭄ᄫԡ᭄˗ᇍѢ g ៪ G 䕀ᤶˈᅗᣛᅮᠧॄⱘ᳝ᬜ᭄ᄫԡ᭄˗ · 㸼⼎㊒ᑺⱘ᭄DŽᇍѢᄫヺІˈᅗᣛᅮᠧॄⱘᄫヺⱘ᳔໻Ͼ᭄˗ᇍѢ eǃE ៪ f 䕀ᤶˈ · ⚍োˈ⫼Ѣߚ䱨ᄫ↉ᆑᑺ੠㊒ᑺDŽ ᄫヺЎ 0DŽܙ฿ᷛᖫˈ߭ܙ฿ ᄫヺ䗮ᐌЎぎḐˈԚᰃˈབᵰ䆒㕂њ 0ܙ฿ϔѯᄫヺDŽܙ ฿˅བᵰখ᭄ⱘᄫヺ᭄ᇣѢℸ᭄ؐˈ߭೼খ᭄ᆑ䖍˄བᵰ㽕∖Ꮊᇍ唤ⱘ䆱߭Ўে䖍 · ϔϾ᭄ؐˈ⫼Ѣᣛᅮ᳔ᇣᄫ↉ᆑᑺDŽ䕀ᤶৢⱘখ᭄䕧ߎᆑᑺ㟇ᇥ㽕䖒ࠄ䖭Ͼ᭄ؐDŽ ᘏࣙᣀϔϾᇣ᭄⚍˗ᇍѢ g ៪ G 䕀ᤶˈᣛᅮ䕧ߎؐሒ䚼᮴ᛣНⱘ 0 ᇚ㹿ֱ⬭ ߭ᣛᅮ೼䕧ߎⱘ䴲 0 ؐࠡࡴ 0x ៪ 0X˗ᇍѢ eǃEǃfǃg ៪ G 䕀ᤶˈᣛᅮ䕧ߎ ᣛᅮ঺ϔ⾡䕧ߎᔶᓣDŽབᵰЎ o 䕀ᤶˈ߭㄀ϔϾ᭄ᄫЎ䳊˗བᵰЎ x ៪ X 䕀ᤶˈ # ܙ฿0 ᇍѢ᭄ؐ䕀ᤶˈᔧ䕧ߎ䭓ᑺᇣѢᄫ↉ᆑᑺᯊˈ⏏ࡴࠡᇐ 0 䖯㸠 ぎḐ བᵰ㄀ϔϾᄫヺϡᰃℷ䋳োˈ߭೼݊ࠡ䴶ࡴϞϔϾぎḐ + ᣛᅮ೼䕧ߎⱘ᭄ࠡ䴶ࡴϞℷ䋳ো • ᣛᅮ㹿䕀ᤶⱘখ᭄೼݊ᄫ↉ݙᎺᇍ唤 · ᷛᖫ˄ৃҹҹӏᛣ乎ᑣߎ⦄˅ˈ⫼Ѣׂᬍ䕀ᤶ䇈ᯢ ᄫヺП䯈ৃҹձ⃵ࣙᣀϟ߫ݙᆍ˖ ᅮϟϔৢ㓁খ᭄ⱘ䕀ᤶ੠ᠧॄ˅DŽ↣Ͼ䕀ᤶ䇈ᯢഛҹᄫヺ%ᓔ༈ˈҹ䕀ᤶᄫヺ㒧ᴳDŽ೼%Ϣ䕀ᤶ އḐᓣІ⬅ϸ⾡㉏ൟⱘᇍ䈵㒘៤˖᱂䗮ᄫヺ˄ᇚ㹿໡ࠊࠄ䕧ߎ⌕Ё˅Ϣ䕀ᤶ䇈ᯢ˄ߚ߿ ᰃᅲ䰙ݭܹⱘᄫヺ᭄DŽ㢹ߎ䫭߭䖨ಲϔϾ䋳ؐDŽ fprintf ߑ᭄ᣝ✻ format 䇈ᯢⱘḐᓣᇍ䕧ߎ䖯㸠䕀ᤶˈᑊݭࠄ stream ⌕ЁDŽ䖨ಲؐ int fprintf(FILE *stream, const char *format, ...) printf ߑ᭄ᦤկḐᓣ࣪䕧ߎ䕀ᤶDŽ B.1.2 Ḑᓣ࣪䕧ߎ (void)setvbuf(stream, buf, _IOFBF, BUFSIZ)DŽ ৺߭ setbuf ߑ᭄ㄝӋѢ˗ކབᵰ buf ⱘؐЎ NULLˈ߭݇䯁⌕ stream ⱘ㓧 void setbuf(FILE *stream, char *buf) ऎⱘ䭓ᑺDŽབᵰ setvbuf ߑ᭄ߎ䫭ˈ߭䖨ಲϾϔ䴲 0 ؐDŽކ ᅮ㓧އ ऎDŽsizeކऎˈ৺߭ᇚߚ䜡ϔϾ㓧ކ߭ setvbuf ߑ᭄ᇚ buf ᣛ৥ⱘऎඳ԰Ў⌕ⱘ㓧 DŽབᵰ buf ⱘؐϡᰃ NULLˈކᔧ mode ⱘؐЎ_IONBF ᯊˈ㸼⼎ϡ䆒㕂㓧ˈކ᭛ӊ䖯㸠㸠㓧 DŽᔧ mode ⱘؐЎ_IOLBF ᯊˈᇚᇍ᭛ᴀކߑ᭄DŽᔧ mode ⱘؐЎ_IOFBF ᯊˈᇚ䖯㸠ᅠܼ㓧 DŽ೼ᠻ㸠䇏ǃݭҹঞ݊ᅗӏԩ᪡԰Пࠡᖙ乏䇗⫼ℸކsetvbuf ߑ᭄᥻ࠊ⌕ stream ⱘ㓧 int setvbuf(FILE *stream, char *buf, int mode, size_t size) Ѣ߯ᓎϔϾৡᄫˈ㗠ϡᰃ߯ᓎϔϾ᭛ӊDŽ ⿟ᑣᠻ㸠ⱘ䖛⿟Ёˈ᳔໮া㛑⹂ֱ⫳៤ TMP_MAX ϾϡৠⱘৡᄫDŽ⊼ᛣˈtmpnam ߑ᭄াᰃ⫼ Ё㟇ᇥ㽕᳝ L_tmpnam Ͼᄫヺⱘぎ䯈DŽTmpnam ߑ᭄೼↣⃵㹿䇗⫼ᯊഛ⫳៤ϡৠⱘৡᄫDŽ೼ ᭄㒘ⱘᣛ䩜DŽtmpnam(S)ߑ᭄ᡞ߯ᓎⱘᄫヺІֱᄬࠄ᭄㒘 s Ёˈᑊᇚᅗ԰Ўߑ᭄ؐ䖨ಲDŽs tmpnam(NULL)ߑ᭄߯ᓎϔϾϢ⦄᳝᭛ӊৡϡৠⱘᄫヺІˈᑊ䖨ಲϔϾᣛ৥ϔݙ䚼䴭ᗕ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! 䆺㒚ֵᙃখ㾕 B.7 㡖Ёᇍ༈᭛ӊⱘ䅼䆎DŽ Ӏ⫼ arg ҷ᳓њৃবখ᭄㸼DŽarg ⬅ᅣ va_start ߱ྟ࣪ˈгৃ㛑⬅ va_arg 䇗⫼߱ྟ࣪DŽ vprintfǃvfprintfǃvsprintf 䖭 3 Ͼߑ᭄ߚ߿Ϣᇍᑨⱘ printf ߑ᭄ㄝӋˈԚᅗ int vsprintf(char *s, const char *format, va_list arg) int vfprintf(FILE *stream, const char *format, va_list arg) int vprintf(const char *format, va_list arg) 㒧ᴳDŽs ᖙ乏䎇໳໻ˈҹ䎇໳ᆍ㒇ϟ䕧ߎ㒧ᵰDŽ䆹ߑ᭄䖨ಲᅲ䰙䕧ߎⱘᄫヺ᭄ˈϡࣙᣀ'\0'DŽ sprintf ߑ᭄Ϣ printf ߑ᭄෎ᴀⳌৠˈԚ݊䕧ߎᇚ㹿ݭܹࠄᄫヺІ s Ёˈᑊҹ'\0' int sprintf(char *s, const char *format, ...) printf(...)ߑ᭄ㄝӋѢ fprintf(stdout, …)DŽ int printf(const char *format, ...) % ϡ䖯㸠খ᭄䕀ᤶ˗ᠧॄϔϾヺো% n int *˗ࠄⳂࠡЎℶˈℸ printf 䇗⫼䕧ߎⱘᄫヺⱘ᭄Ⳃᇚ㹿ݭܹࠄⳌᑨখ᭄ЁDŽϡ䖯㸠খ᭄䕀ᤶ p void *˗ᠧॄᣛ䩜ؐ˄݋ԧ㸼⼎ᮍᓣϢᅲ⦄᳝݇˅ ϡᠧॄ g, G double˗ᔧᣛ᭄ᇣѢ•4 ៪໻ѢㄝѢ㊒ᑺᯊˈ䞛⫼%e ៪%E ⱘḐᓣˈ৺߭䞛⫼%f ⱘḐᓣDŽሒ䚼ⱘ 0 ੠ᇣ᭄⚍ 䕧ߎᇣ᭄⚍ e, E double˗ᔶᓣЎ[•]m.dddddd e ±xx ៪[•]m.dddddd E ±xxDŽd ⱘ᭄Ⳃ⬅㊒ᑺ⹂ᅮˈ咬䅸㊒ᑺЎ 6DŽ㊒ᑺЎ 0 ᯊϡ ߎᇣ᭄⚍ f double˗ᔶᓣЎ[•]mmm.ddd ⱘक䖯ࠊ㸼⼎ˈ݊Ёˈd ⱘ᭄Ⳃ⬅㊒ᑺ⹂⹂ᅮˈ咬䅸㊒ᑺЎ 6DŽ㊒ᑺЎ 0 ᯊϡ䕧 s char *˗ᠧॄᄫヺІЁⱘᄫヺˈⳈࠄ䘛ࠄ'\0'៪Ꮖᠧॄњ⬅㊒ᑺᣛᅮⱘᄫヺ᭄ c int˗䕀ᤶЎ unsigned char ㉏ൟৢЎϔϾᄫヺ u int˗᮴ヺোक䖯ࠊ㸼⼎ ⫼ ABCDEF x, X unsigned int ˗᮴ヺোक݁䖯ࠊ㸼⼎˄≵᳝ࠡᇐ 0x ៪ 0X˅ˈབᵰᰃ 0xˈ߭Փ⫼ abcdefˈབᵰᰃ 0Xˈ߭Փ o unsigned int˗᮴ヺোܿ䖯ࠊ㸼⼎ d, i int˗᳝ヺোक䖯ࠊ㸼⼎ 䕀ᤶᄫヺ খ᭄㉏ൟ˗䕀ᤶ㒧ᵰ 㸼 B•1 printf ߑ᭄ⱘ䕀ᤶᄫヺ ᅮНDŽ 㸼 B•1 Ё߫ߎњ䖭ѯ䕀ᤶᄫヺঞ݊ᛣНDŽབᵰ%ৢ䴶ⱘᄫヺϡᰃ䕀ᤶᄫヺˈ߭݊㸠Ў≵᳝ ᭄䅵ㅫᕫࠄ˄ϟϔϾখ᭄ᖙ乏Ў int ㉏ൟ˅DŽ ϟˈ䆹ؐᇚ䗮䖛䕀ᤶϟϔϾখމᆑᑺ੠㊒ᑺЁⱘӏԩϔϾ៪ϸ㗙䛑ৃҹ⫼*ᣛᅮˈ䖭⾡ᚙ ᭄ᣝ long double ㉏ൟ䕧ߎDŽ ߎDŽl 㸼⼎ᇚⳌᑨⱘখ᭄ᣝ long ៪ unsigned long ㉏ൟ䕧ߎ˗L 㸼⼎ᇚⳌᑨⱘখ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! e, f, g ⍂⚍᭄ˈfloat *DŽFloat ㉏ൟ⍂⚍᭄ⱘ䕧ܹḐᓣЎ˖ϔϾৃ䗝ⱘℷ䋳োǃϔϾৃ㛑ࣙ৿ᇣ᭄⚍ⱘ᭄ᄫІˈ ֱᄬ䆹ᄫヺІҹঞ೼ሒ䚼⏏ࡴⱘ'\0'ᄫヺ s ⬅䴲ぎⱑヺ㒘៤ⱘᄫヺІ˄ϡࣙ৿ᓩো˅˗char *ˈᅗᣛ৥ϔϾᄫヺ᭄㒘ˈ䆹ᄫヺ᭄㒘ᖙ乏᳝䎇໳ぎ䯈ˈҹ ϟˈ䇏প䕧ܹᯊᇚϡ䏇䖛ぎⱑヺDŽབᵰ䳔㽕䇏ܹϟϔϾ䴲ぎⱑヺˈৃҹՓ⫼%1sމ೼䖭⾡ᚙ c ᄫヺ˗char * ˈᣝ✻ᄫ↉ᆑᑺⱘ໻ᇣᡞ䇏পⱘᄫヺֱᄬࠄࠊᅮⱘ᭄㒘Ёˈϡ๲ࡴ'\0'ᄫ↉ᆑᑺⱘ咬䅸ؐЎ 1DŽ x क݁䖯ࠊᭈൟ᭄˄ৃҹᏺ៪ϡᏺࠡᇐ 0x ៪ 0X˅˗ int * u ᮴ヺোक䖯ࠊᭈൟ᭄˗unsigned int * o ܿ䖯ࠊᭈൟ᭄˄ৃҹᏺ៪ϡᏺࠡᇐ 0˅˗ int * i ᭈൟ᭄˗int *DŽ䆹ᭈൟ᭄ৃҹᰃܿ䖯ࠊ˄ҹ 0 ᓔ༈˅៪क݁䖯ࠊ˄ҹ 0x ៪ 0X ᓔ༈˅ d क䖯ࠊᭈ᭄˗int * 䕀ᤶᄫヺ 䕧᭄ܹ᥂˗খ᭄㉏ൟ 㸼 B•2 scanf ߑ᭄ⱘ䕀ᤶᅛヺ ࡴϞᄫ↡ LDŽ ҹࡴϞᄫ↡ lDŽབᵰখ᭄ᰃᣛ৥ long double ㉏ൟⱘᣛ䩜ˈ߭೼䕀ᤶᄫヺ eǃf ੠ g ࠡৃҹ ↡ lDŽབᵰখ᭄ᰃᣛ৥ double ㉏ൟ㗠䴲 float ㉏ൟⱘᣛ䩜ˈ߭೼䕀ᤶᄫヺ eǃf ੠ g ࠡৃ ПࠡৃҹࡴϞࠡ㓔 hDŽབᵰখ᭄ᰃᣛ৥ long ㉏ൟⱘᣛ䩜ˈ߭೼䖭޴Ͼ䕀ᤶᄫヺࠡৃҹࡴϞᄫ བᵰখ᭄ᰃᣛ৥ short ㉏ൟ㗠䴲 int ㉏ൟⱘᣛ䩜ˈ߭೼䕀ᤶᄫヺ dǃiǃnǃoǃu ੠ x B•2 ᠔⼎DŽ 䕀ᤶᄫヺ䇈ᯢњᇍ䕧ܹᄫ↉ⱘ㾷䞞ᮍᓣDŽᇍᑨⱘখ᭄ᖙ乏ᰃᣛ䩜DŽড়⊩ⱘ䕀ᤶᄫヺབ㸼 ᤶ㸠ヺǃಲ䔺ヺ੠ᤶ义ヺ˅DŽ 䍞㸠ⱘ䖍⬠䇏প䕧ܹˈ಴Ўᤶ㸠ヺгᰃぎⱑヺ˄ぎⱑヺࣙᣀぎḐǃ῾৥ࠊ㸼ヺǃ㒉৥ࠊ㸼ヺǃ ໻ᄫ↉ᆑᑺ˄བᵰ᳝ⱘ䆱˅ᯊˈᇍᔧࠡ䕧ܹᄫ↉ⱘ䇏প㒧ᴳDŽ䖭ᛣੇⴔˈscanf ߑ᭄ৃҹ䎼 ϡ䖯㸠䌟ؐDŽ䕧ܹᄫ↉ᯊϔϾ⬅䴲ぎⱑヺᄫヺ㒘៤ⱘᄫヺІˈᔧ䘛ࠄϟϔϾぎⱑヺ៪䖒ࠄ᳔ 䞣ЁDŽԚᰃˈབᵰ䕀ᤶ䇈ᯢЁࣙ৿䌟ؐሣ㬑ᄫヺ*ˈ՟བ%*sˈ߭ᇚ䏇䖛ᇍᑨⱘ䕧ܹᄫ↉ˈᑊ ᅮњϟϔϾ䕧ܹᄫ↉ⱘ䕀ᤶᮍᓣDŽ䗮ᐌ㒧ᵰᇚ㹿ֱᄬ೼⬅ᇍᑨখ᭄ᣛ৥ⱘবއ䕀ᤶ䇈ᯢ 䗝˅ǃϔϾᣛᅮⳂᷛᄫ↉ᆑᑺⱘᄫヺ˄hǃl ៪ L˅˄ৃ䗝˅ҹঞϔϾ䕀ᤶᄫヺ㒘៤DŽ · 䕀ᤶ䇈ᯢˈ⬅ϔϾ%ǃϔϾ䌟ؐሣ㬑ᄫヺ*˄ৃ䗝˅ǃϔϾᣛᅮ᳔໻ᄫ↉ᆑᑺⱘ᭄˄ৃ · ᱂䗮ᄫヺ˄%䰸໪˅ˈᅗᇚϢ䕧ܹ⌕ЁϟϔϾ䴲ぎⱑᄫヺ䖯㸠ऍ䜡 · ぎḐ៪ࠊ㸼ヺ ৿ϟ߫乍Ⳃ˖ ḐᓣІ format 䗮ᐌࣙᣀ䕀ᤶ䇈ᯢˈᅗ⫼Ѣᣛᇐᇍ䕧ܹ䖯㸠㾷䞞DŽḐᓣᄫヺІЁৃҹࣙ 䕧ܹ乍ⱘ᭄ⳂDŽ ᵰࠄ䖒᭛ӊⱘ᳿ሒ៪೼䕀ᤶ䕧ܹࠡߎ䫭ˈ䆹ߑ᭄䖨ಲ EOF˗৺߭ˈ䖨ಲᅲ䰙㹿䕀ᤶᑊ䌟ؐⱘ 㓁৘Ͼখ᭄ˈ݊Ёⱘ↣Ͼখ᭄䛑ᖙ乏ᰃϔϾᣛ䩜DŽᔧḐᓣІ format ⫼ᅠᯊˈߑ᭄䖨ಲDŽབ fscanf ߑ᭄ḍ᥂ḐᓣІ format Ң⌕ stream Ё䇏প䕧ܹˈᑊᡞ䕀ᤶৢⱘؐ䌟ؐ㒭ৢ int fscanf(FILE *stream, const char *format, ...) scanf ߑ᭄໘⧚Ḑᓣ࣪䕧ܹ䕀ᤶDŽ B.1.3 Ḑᓣ࣪䕧ܹ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! getchar ߑ᭄ㄝӋѢ getc(stdin)DŽ int getchar(void) stream ⱘؐDŽ getc ߑ᭄ㄝӋѢ fgetcˈ᠔ϡৠⱘᰃˈᔧ getc ߑ᭄ᅮНЎᅣᯊˈᅗৃ㛑໮⃵䅵ㅫ int getc(FILE *stream) 㢹ߎ䫭߭䖨ಲ EOFDŽ fputs ߑ᭄ᡞᄫヺІ s˄ϡࣙ৿ᄫヺ'\n'˅䕧ߎࠄ⌕ Btream Ё˗ᅗ䖨ಲϔϾ䴲䋳ؐˈ int fputs(const char *s, FILE *stream) ܹⱘᄫヺˈ㢹ߎ䫭߭䖨ಲ EOFDŽ fputc ߑ᭄ᡞᄫヺ c˄䕀ᤶЎ unsigned char ㉏ൟ˅䕧ߎࠄ⌕ stream ЁDŽᅗ䖨ಲݭ int fputc(int c, FILE *stream) ៪থ⫳䫭䇃ˈ߭䖨ಲ NULLDŽ 㒘 s Ёˈ䇏প䖛⿟㒜ℶDŽ᭄㒘 s ҹ'\0'㒧ሒDŽfgets ߑ᭄䖨ಲ᭄㒘 sDŽབᵰࠄ䖒᭛ӊⱘ᳿ሒ fgets ߑ᳔᭄໮ᇚϟ n•1 Ͼᄫヺ䇏ܹࠄ᭄㒘 s ЁDŽᔧ䘛ࠄᤶ㸠ヺᯊˈᡞᤶ㸠ヺ䇏ܹࠄ᭄ char *fgets(char *s, int n, FILE *stream) ㉏ൟ˅DŽབᵰࠄ䖒᭛ӊ᳿ሒ៪থ⫳䫭䇃ˈ߭䖨ಲ EOFDŽ fqetc ߑ᭄䖨ಲ stream ⌕ⱘϟϔϾᄫヺˈ䖨ಲ㉏ൟЎ unsigned char˄㹿䕀ᤶЎ int int fgetc(FILE *stream) B.1.4 ᄫヺ䕧ܹˋ䕧ߎߑ᭄ І sDŽ sscanf(s, ...)ߑ᭄Ϣ scanf(...)ㄝӋˈ᠔ϡৠⱘᰃˈࠡ㗙ⱘ䕧ܹᄫヺᴹ⑤Ѣᄫヺ int sscanf(const char *s, const char *format, ...) scanf(...)ߑ᭄Ϣ fscanf(stdin, ...)ⳌৠDŽ int scanf(const char *format, ...) % 㸼⼎“%āˈϡ䖯㸠䌟ؐ 䲚ড়Ёϡࣙ৿ᄫヺ“]” [^...] ϢᮍᣀোЁⱘᄫヺ䲚ড়ϡऍ䜡ⱘ䕧ܹᄫヺЁ᳔䭓ⱘ䴲ぎᄫヺІ˗char *DŽ᳿ሒᇚ⏏ࡴ'\0'DŽ[^]...]㸼⼎ Ёࣙ৿ᄫヺ“]” [...] ϢᮍᣀোЁⱘᄫヺ䲚ড়ऍ䜡ⱘ䕧ܹᄫヺЁ᳔䭓ⱘ䴲ぎᄫヺІ˗char * DŽ᳿ሒᇚ⏏ࡴ'\0'DŽ[]...]㸼⼎䲚ড় 䅵᭄ n ᇚࠄⳂࠡЎℶ䆹ߑ᭄䇗⫼䇏পⱘᄫヺ᭄ݭܹᇍᑨⱘখ᭄Ё˗int *DŽ䚼䇏প䕧ܹᄫヺDŽϡ๲ࡴᏆ䕀ᤶⱘ乍Ⳃ p printf("%p")ߑ᭄䇗⫼ᠧॄⱘᣛ䩜ؐ˗void * ϔϾৃ䗝ⱘᣛ᭄ᄫ↉˄ᄫ↡ e ៪ E ৢ䎳ϔϾৃ㛑ᏺℷ䋳োⱘᭈൟ᭄˅ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! long ftell(FILE *stream) ߑ᭄೼ߎ䫭ᯊ䖨ಲϔϾ䴲 0 ؐDŽ ᖙ乏䆒㕂Ў 0ˈ៪㗙ᰃ⬅ߑ᭄ ftell 䖨ಲⱘؐ˄ℸᯊ origin ⱘؐᖙ乏ᰃ SEEK_SET˅DŽfseek ˄᭛ӊᓔྟ໘˅ǃSEEK_CUR˄ᔧࠡԡ㕂˅៪ SEEK_END˄᭛ӊ㒧ᴳ໘˅DŽᇍѢ᭛ᴀ⌕ˈoffset ᭛ӊˈℸԡ㕂㹿䆒㕂ЎҢoriginᓔྟⱘ㄀offsetϾᄫヺ໘DŽOriginⱘؐৃҹЎSEEK_SET fseek ߑ᭄䆒㕂⌕ stream ⱘ᭛ӊԡ㕂ˈৢ㓁ⱘ䇏ݭ᪡԰ᇚҢᮄԡ㕂ᓔྟDŽᇍѢѠ䖯ࠊ int fseek(FILE *stream, long offset, int origin) B.1.6 ᭛ӊᅮԡߑ᭄ ЁDŽᅗ䖨ಲ䕧ߎⱘᇍ䈵᭄ⳂDŽབᵰথ⫳䫭䇃ˈ䖨ಲؐӮᇣѢ nobj ⱘؐDŽ fwriteߑ᭄Ңptrᣛ৥ⱘ᭄㒘Ё䇏পnobjϾ䭓ᑺЎsizeⱘᇍ䈵ˈᑊ䕧ߎࠄ⌕stream size_t fwrite(const void *ptr, size_t size, size_t nobj, FILE *stream) 㦋ᕫ㒧ᵰᠻ㸠⢊ᗕDŽ ⱘ᭄㒘ЁDŽᅗ䖨ಲ䇏পⱘᇍ䈵᭄Ⳃˈℸ䖨ಲؐৃ㛑ᇣѢ nobjDŽᖙ乏䗮䖛ߑ᭄ feof ੠ ferror fread ߑ᭄Ң⌕ stream Ё䇏প᳔໮ nobj Ͼ䭓ᑺЎ size ⱘᇍ䈵ˈᑊֱᄬࠄ ptr ᣛ৥ size_t fread(void *ptr, size_t size, size_t nobj, FILE *stream) B.1.5 Ⳉ᥹䕧ܹˋ䕧ߎߑ᭄ ߑ᭄䖨ಲ㹿ݭಲⱘᄫヺˈབᵰথ⫳䫭䇃ˈ߭䖨ಲ EOFDŽ 䖯㸠䇏᪡԰ᯊˈᇚ䖨ಲ䆹ᄫヺDŽᇍ↣Ͼ⌕া㛑ݭಲϔϾᄫヺˈϨℸᄫヺϡ㛑ᰃ EOFDŽungetc ungetc ߑ᭄ᡞ c˄䕀ᤶЎ unsigned char ㉏ൟ˅ݭಲࠄ⌕ stream Ёˈϟ⃵ᇍ䆹⌕ int ungetc(int c, FILE *stream) ߭䖨ಲϔϾ䴲䋳ؐDŽ puts ߑ᭄ᡞᄫヺІ s ੠ϔϾᤶ㸠ヺ䕧ߎࠄ stdout ЁDŽབᵰথ⫳䫭䇃ˈ߭䖨ಲ EOF˗৺ int puts(const char *s) putchar(c)ߑ᭄ㄝӋѢ putc(c, stdout)DŽ int putchar(int c) stream ⱘؐDŽ putc ߑ᭄ㄝӋѢ fputcˈ᠔ϡৠⱘᰃˈᔧ putc ߑ᭄ᅮНЎᅣᯊˈᅗৃ㛑໮⃵䅵ㅫ int putc(int c, FILE *stream) ಲ᭄㒘 sˈབᵰࠄ䖒᭛ӊⱘ᳿ሒ៪থ⫳䫭䇃ˈ߭䖨ಲ NULLDŽ gets ߑ᭄ᡞϟϔϾ䕧ܹ㸠䇏ܹࠄ᭄㒘 s Ёˈᑊᡞ᳿ሒⱘᤶ㸠ヺ᳓ᤶЎᄫヺ'\0'DŽᅗ䖨 char *gets(char *s) w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! isalnum(c) ߑ᭄ isalpha(c)៪ isdigit(c)Ўⳳ DŽ䖭ѯߑ᭄ࣙᣀ˖˅؛⼎খ᭄ c ⒵䎇ᣛᅮⱘᴵӊˈ߭ߑ᭄䖨ಲ䴲 0 ؐ˄㸼⼎ⳳ˅ˈ৺߭䖨ಲ 0˄㸼 ⱘؐᖙ乏ᰃ EOF ៪ৃ⫼ unsigned char ㉏ൟ㸼⼎ⱘᄫヺˈߑ᭄ⱘ䖨ಲؐЎ int ㉏ൟDŽབᵰ ༈᭛ӊЁໄᯢњϔѯ⌟䆩ᄫヺⱘߑ᭄DŽ↣Ͼߑ᭄ⱘখ᭄ഛЎ int ㉏ൟˈখ᭄ B.2 ᄫヺ㉏߿⌟䆩˖ ᳝݇ߑ᭄ strerror ⱘֵᙃˈখ㾕 B.3 㡖Ёⱘҟ㒡DŽ fprintf(stderr, "%s: %s\n", s, "error message"); ԧݙᆍϢ݋ԧⱘᅲ⦄᳝݇DŽ䆹ߑ᭄ⱘࡳ㛑㉏ԐѢᠻ㸠ϟ߫䇁হ˖ perror(s)ߑ᭄ᠧॄᄫヺІ s ҹঞϢ errno ЁᭈൟؐⳌᑨⱘ䫭䇃ֵᙃˈ䫭䇃ֵᙃⱘ݋ void perror(const char *s) བᵰ䆒㕂њϢ stream ⌕Ⳍ݇ⱘ䫭䇃ᣛ⼎ヺˈferror ߑ᭄ᇚ䖨ಲϔϾ䴲 0 ؐDŽ int ferror(FILE *stream) བᵰ䆒㕂њϢ stream ⌕Ⳍ݇ⱘ᭛ӊ㒧ᴳᣛ⼎ヺˈfeof ߑ᭄ᇚ䖨ಲϔϾ䴲 0 ؐˈ int feof(FILE *stream) clearerr ߑ᭄⏙䰸Ϣ⌕ stream Ⳍ݇ⱘ᭛ӊ㒧ᴳヺ੠䫭䇃ᣛ⼎ヺDŽ void clearerr(FILE *stream) Ͼ䫭䇃㓪োˈ᥂ℸৃҹ䖯ϔℹњ㾷᳔䖥ϔ⃵ߎ䫭ⱘֵᙃDŽ ⼎ヺৃ㹿ᰒᓣഄ䆒㕂੠⌟䆩DŽ঺໪ˈᭈൟ㸼䖒ᓣ errno˄೼Ёໄᯢ˅ৃҹࣙ৿ϔ ᑧЁⱘ䆌໮ߑ᭄䛑Ӯ䆒㕂⢊ᗕᣛ⼎ヺDŽ䖭ѯ⢊ᗕᣛޚᔧথ⫳䫭䇃៪ࠄ䖒᭛ӊ᳿ሒᯊˈᷛ B.1.7 䫭䇃໘⧚ߑ᭄ ߭䖨ಲϔϾ䴲 0 ؐDŽ fsetpos ߑ᭄ᇚ⌕ stream ⱘᔧࠡԡ㕂䆒㕂Ў fgetpos 䆄ᔩ೼*ptr Ёⱘԡ㕂DŽ㢹ߎ䫭 int fsetpos(FILE *stream, const fpos_t *ptr) ⫼DŽ㢹ߎ䫭߭䖨ಲϔϾ䴲 0 ؐDŽ fgetpos ߑ᭄ᡞ stream ⌕ⱘᔧࠡԡ㕂䆄ᔩ೼*ptr Ёˈկ䱣ৢⱘ fsetpos ߑ᭄䇗⫼Փ int fgetpos(FILE *stream, fpos_t *ptr) 㒧ᵰDŽ rewind(fp)ߑ᭄ㄝӋѢ䇁হ fseek(fp, 0L, SEEK_SET)˗clearerr(fp)ⱘᠻ㸠 void rewind(FILE *stream) ftell ߑ᭄䖨ಲ stream ⌕ⱘᔧࠡ᭛ӊԡ㕂ˈߎ䫭ᯊ䆹ߑ᭄䖨ಲ•lLDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᣛ䩜˗བᵰ cs Ёϡࣙ৿ cˈ߭䆹ߑ᭄䖨ಲ NULL char *strchr(cs,c) 䖨ಲᣛ৥ᄫヺ c ೼ᄫヺІ cs Ё㄀ϔ⃵ߎ⦄ⱘԡ㕂ⱘ 0˗ᔧ cs>ct ᯊˈ䖨ಲϔϾℷ᭄ ᔧ csct ᯊˈ䖨ಲϔϾℷ int strcmp(cs,ct) ↨䕗ᄫヺІ cs ੠ ct˗ᔧ csЁᅮНњϸ㒘ᄫヺІߑ᭄DŽ㄀ϔ㒘ߑ᭄ⱘৡᄫҹ str ᓔ༈˗㄀Ѡ㒘 B.3 ᄫヺІߑ᭄˖ ߭ toupper(c)䖨ಲⳌᑨⱘ໻ݭᄫ↡ˈ৺߭䖨ಲ cDŽ བᵰ c ᰃ໻ݭᄫ↡ˈ߭ tolower(c)䖨ಲⳌᑨⱘᇣݭᄫ↡ˈ৺߭䖨ಲ cDŽབᵰ c ᰃᇣݭᄫ↡ˈ int toupper(int c) ᇚ c 䕀ᤶЎ໻ݭᄫ↡ int tolower(int c) ᇚ c 䕀ᤶЎᇣݭᄫ↡ ঺໪ˈϟ䴶ϸϾߑ᭄ৃ⫼Ѣᄫ↡ⱘ໻ᇣݭ䕀ᤶ˖ ᄫヺᰃҢ 0˄NUL˅ࠄ 0xlF˄US˅П䯈ⱘᄫヺҹঞᄫヺ 0x7F˄DEL˅DŽ ೼ 7 ԡ ASCII ᄫヺ䲚ЁˈৃᠧॄᄫヺᰃҢ 0x20˄' '˅ࠄ 0x7E˄'~'˅П䯈ⱘᄫヺ˗᥻ࠊ isxdigit(c) cᰃक݁䖯ࠊ᭄ᄫ isupper(c) cᰃ໻ݭᄫ↡ isspace(c) cᰃぎḐǃᤶ义ヺǃᤶ㸠ヺǃಲ䔺ヺǃ῾৥ࠊ㸼ヺ៪㒉৥ࠊ㸼ヺ ispunct(c) cᰃ䰸ぎḐǃᄫ↡੠᭄ᄫ໪ⱘৃᠧॄᄫヺ isprint(c) cᰃࣙᣀぎḐⱘৃᠧॄᄫヺ islower(c) cᰃᇣݭᄫ↡ isgraph(c) cᰃ䰸ぎḐ໪ⱘৃᠧॄᄫヺ isdigit(c) cЎक䖯ࠊ᭄ᄫ iscntrl(c) cЎ᥻ࠊᄫヺ isalpha(c) ߑ᭄ isupper(c)៪ islower(c)Ўⳳ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᇚ㹿䆒㕂Ў ERANGEDŽᔧ㒧ᵰϟ⑶ᯊˈߑ᭄䖨ಲ 0ˈ㗠 errno ᰃ৺䆒㕂Ў ERANGE 㽕㾚݋ԧ Ӯথ⫳ؐඳ䫭䇃DŽᔧ㒧ᵰϞ⑶ᯊˈߑ᭄䖨ಲ HUGE_VALˈᑊᏺ᳝ℷ⹂ⱘℷ䋳োˈerrpo ⱘؐ 㕂Ў EDOMˈߑ᭄ⱘ䖨ಲؐϢ݋ԧⱘᅲ⦄Ⳍ݇DŽབᵰߑ᭄ⱘ㒧ᵰϡ㛑⫼ double ㉏ൟ㸼⼎ˈ߭ ⱘ԰⫼ඳП໪ᯊˈህӮߎ⦄ᅮНඳ䫭䇃DŽ೼থ⫳ᅮНඳ䫭䇃ᯊˈܼሔব䞣 errno ⱘؐᇚ㹿䆒 ᭄ⱘᅮНඳ䫭䇃੠ؐඳ䫭䇃˗HUGE_VAL ᰃϔϾ double ㉏ൟⱘℷ᭄DŽᔧখ᭄ԡѢߑ᭄ᅮН ᅣ EDOM ੠ ERANGE˄೼༈᭛ӊЁໄᯢ˅ᰃϸϾ䴲 0 ᭈൟᐌ䞣ˈ⫼Ѣᣛ⼎ߑ ༈᭛ӊЁໄᯢњϔѯ᭄ᄺߑ᭄੠ᅣDŽ B.4 ᭄ᄺߑ᭄˖ void *memset(s,c,n) ᇚ s Ёⱘࠡ n Ͼᄫヺ᳓ᤶЎ cˈᑊ䖨ಲ s cs ⱘࠡ n ϾᄫヺЁᡒϡࠄऍ䜡ˈ߭䖨ಲ NULL void *memchr(cs,c,n) 䖨ಲϔϾᣛ䩜ˈᅗᣛ৥ c ೼ cs Ё㄀ϔ⃵ߎ⦄ⱘԡ㕂DŽབᵰ ⱘ䖨ಲؐⳌৠ int memcmp(cs,ct,n) ᇚ cs ⱘࠡ n ϾᄫヺϢ ct 䖯㸠↨䕗ˈ݊䖨ಲؐϢ strcmp ᯊˈ䆹ߑ᭄ҡ㛑ℷ⹂ᠻ㸠 void *memmove(s,ct,n) 䆹ߑ᭄ⱘࡳ㛑Ϣ memcpy ⳌԐˈ᠔ϡৠⱘᰃˈᔧᇍ䈵䞡঴ void *memcpy(s,ct,n) ᇚᄫヺІ ct Ёⱘ n Ͼᄫヺᣋ䋱ࠄ s Ёˈᑊ䖨ಲ s n ⱘ㉏ൟЎ size_tˈc ⱘ㉏ൟЎ int˄ᇚ㹿䕀ᤶЎ unsigned char ㉏ൟ˅DŽ ষDŽ೼ϟ㸼߫ߎⱘߑ᭄Ёˈs ੠ t ⱘ㉏ൟഛЎ void * ˈcs ੠ ct ⱘ㉏ൟഛЎ const void * ˈ ҹ mem ᓔ༈ⱘߑ᭄ᣝ✻ᄫヺ᭄㒘ⱘᮍᓣ᪡԰ᇍ䈵ˈ݊Џ㽕ⳂⱘᰃᦤկϔϾ催ᬜⱘߑ᭄᥹ 䆄োᯊˈ䖨ಲ NULLDŽ↣⃵䇗⫼ᯊᄫヺІ ct ৃҹϡৠDŽ ˄⬅ s ⱘؐᰃ৺Ў NULL ᣛ⼎˅ˈഛ䖨ಲϟϔϾϡࣙ৿ ct Ёᄫヺⱘ䆄োDŽᔧ s Ё≵᳝䖭ḋⱘ ᇚ s ЁⱘϟϔϾᄫヺ᳓ᤶЎ'\0'ˈᑊ䖨ಲᣛ৥䆄োⱘᣛ䩜DŽ䱣ৢˈ↣⃵䇗⫼ strtok ߑ᭄ᯊ ⱘᄫヺЎߚ⬠ヺDŽ㄀ϔ⃵䇗⫼ᯊˈs Ў䴲ぎDŽᅗ᧰㋶ sˈᡒࠄϡࣙ৿ ct Ёᄫヺⱘ㄀ϔϾ䆄োˈ ᇍ strtok(s, ct) 䖯㸠ϔ㋏߫䇗⫼ˈৃҹᡞᄫヺІ s ߚ៤䆌໮䆄োˈ䖭ѯ䆄োҹ ct Ё 䆺㒚ֵᙃখ㾕ϟ䴶ⱘ䅼䆎 char *strtok(s,ct) strtok ߑ᭄೼ s Ё᧰㋶⬅ ct Ёⱘᄫヺ⬠ᅮⱘ䆄োDŽ ᄫヺІ˄䫭䇃ֵᙃⱘ݋ԧݙᆍϢ݋ԧᅲ⦄Ⳍ݇˅ char *strerror(n) 䗔ಲϔϾᣛ䩜ˈᅗᣛ৥Ϣ䫭䇃㓪ো n ᇍᑨⱘ䫭䇃ֵᙃ size_t strlen(cs) 䖨ಲᄫヺІ cs ⱘ䭓ᑺ ಲ NULL І cs Ёⱘԡ㕂˗བᵰ cs Ёϡࣙ৿ᄫヺІ ctˈ߭䖨 char *strstr(cs,ct) 䖨ಲϔϾᣛ䩜ˈᅗᣛ৥ᄫヺІ ct ㄀ϔ⃵ߎ⦄೼ᄫヺ Ⳍৠⱘᄫヺˈ߭䖨ಲ NULL ⃵ߎ⦄೼ᄫヺІ cs Ёⱘԡ㕂˗བᵰ cs Ё≵᳝Ϣ ct char *strpbrk(cs,ct) 䖨ಲϔϾᣛ䩜ˈᅗᣛ৥ᄫヺІ ct Ёⱘӏᛣᄫヺ㄀ϔ size_t strcspn(cs,ct) 䖨ಲᄫヺІ cs Ёϡࣙ৿ ct Ёⱘᄫヺⱘࠡ㓔ⱘ䭓ᑺ size_t strspn(cs,ct) 䖨ಲᄫヺІ cs Ёࣙ৿ ct Ёⱘᄫヺⱘࠡ㓔ⱘ䭓ᑺ ⱘᣛ䩜˗བᵰ cs Ёϡࣙ৿ cˈ߭䆹ߑ᭄䖨ಲ NULL char *strrchr(cs,c) 䖨ಲᣛ৥ᄫヺ c ೼ᄫヺІ cs Ё᳔ৢϔ⃵ߎ⦄ⱘԡ㕂 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! (char**)NULL, 10)DŽ atoi ߑ᭄ᇚᄫヺІ s 䕀ᤶЎ int ㉏ൟDŽ䆹ߑ᭄ㄝӋѢ(int)strtol(s, int atoi(const char *s) (char**)NULL˅DŽ atof ߑ᭄ᇚᄫヺІ s 䕀ᤶЎ double ㉏ൟDŽ䆹ߑ᭄ㄝӋѢ strtod ˄ s, double atof(const char *s) ༈᭛ӊЁໄᯢњϔѯᠻ㸠᭄ؐ䕀ᤶǃݙᄬߚ䜡ҹঞ݊ᅗ㉏ԐᎹ԰ⱘߑ᭄DŽ B.5 ᅲ⫼ߑ᭄˖ ⱘᅲ⦄Ⳍ݇ fmod(x,y) ∖ JJy ⱘ⍂⚍ԭ᭄ˈヺোϢ j ⳌৠDŽབᵰ y Ў 0ˈ߭㒧ᵰϢ݋ԧ ᭄䖨ಲᇣ᭄䚼ߚˈᭈ᭄䚼ߚֱᄬ೼‘ip Ё modf(x, double *ip) ᡞ J ߚ៤ᭈ᭄੠ᇣ᭄ϸ䚼ߚˈϸ䚼ߚⱘℷ䋳োഛϢ x ⳌৠDŽ䆹ߑ ߭䖭ϸ䚼ߚഛЎ 0 ᵰᇚ䖨ಲⳳߚ᭄䚼ߚˈᑊᇚᐖ᭄ֱᄬ೼*exp ЁDŽབᵰ x Ў 0ˈ frexp(x, int *ip) ᡞ x ߚ៤ϔϾ೼[1/2, 1]ऎ䯈ݙⱘⳳߚ᭄੠ϔϾ 2 ⱘᐖ᭄DŽ㒧 ldexp(x,n) 䅵ㅫ x.2n ⱘؐ fabs(x) x ⱘ㒱ᇍؐ|x| floor(x) ϡ໻Ѣ x ⱘ᳔໻ᭈൟ᭄ˈ݊Ё x ⱘ㉏ൟЎ double ceil(x) ϡᇣѢ x ⱘ᳔ᇣᭈൟ᭄ˈ݊Ё x ⱘ㉏ൟЎ double sqrt(x) x ⱘᑇᮍḍˈ݊Ё xı0 ඳ䫭䇃 pow(x,y) xyDŽབᵰ x=0 Ϩ yİ0ˈ៪㗙 x<0 Ϩ y ϡᰃᭈൟ᭄ˈᇚѻ⫳ᅮН log10(x) ҹ 10 Ўᑩⱘᇍ᭄ log10(x)ˈ݊Ё x>0 log(x) 㞾✊ᇍ᭄ ln(x)ˈ݊Ё x>0 exp(x) ᐖߑ᭄ ex tanh(x) x ⱘঠ᳆ℷߛؐ cosh(x) x ⱘঠ᳆ԭᓺؐ sinh(x) x ⱘঠ᳆ℷᓺؐ atan2(y,x) tan•1(y/x)ˈؐඳЎ[•±, ±] atan(x) tan•1(x)ˈؐඳЎ[±/2, ±/2] acos(x) cos•1(x)ˈؐඳЎ[0, ±]ˈ݊Ё [ę[•1, 1] asin(x) sin•1(x)ˈؐඳЎ[±/2, ±/2]ˈ݊Ё [ę[•1, 1] tan(x) x ⱘℷߛؐ cos(x) x ⱘԭᓺؐ sin(x) x ⱘℷᓺؐ doubleDŽϝ㾦ߑ᭄ⱘ㾦ᑺ⫼ᓻᑺ㸼⼎DŽ ೼ϟ㸼Ёˈx ੠ y ⱘ㉏ൟЎ doubleˈn ⱘ㉏ൟЎ intˈ᠔᳝ߑ᭄ⱘ䖨ಲؐⱘ㉏ൟഛЎ ⱘᅲ⦄㗠ᅮDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ࠡՓ⫼ࡼᗕߚ䜡ߑ᭄ malloeǃrealloc ៪ calloc ߚ䜡ⱘぎ䯈DŽܜ乏ᣛ৥ free ߑ᭄䞞ᬒ p ᣛ৥ⱘݙᄬぎ䯈DŽᔧ p ⱘؐЎ NULL ᯊˈ䆹ߑ᭄ϡᠻ㸠ӏԩ᪡԰DŽP ᖙ void free(void *p) ݙᆍֱᣕϡবDŽܗϟˈॳᣛ䩜 p ᣛ৥ⱘऩމ߭䖨ಲ NULLDŽ೼䖭⾡ᚙ ϡ㹿߱ྟ࣪˖realloc ߑ᭄䖨ಲᣛ৥ᮄߚ䜡ぎ䯈ⱘᣛ䩜˗㢹᮴⊩⒵䎇㽕∖ˈܗ߭ᮄߚ䜡ݙᄬऩ ໻ˈ߭ॳݙᄬⱘݙᆍֱᣕϡবˈ๲ࡴⱘぎ䯈ϡ䖯㸠߱ྟ࣪DŽབᵰᮄߚ䜡ⱘݙᄬ↨ॳݙᄬᇣˈ realloc ߑ᭄ᇚ p ᣛ৥ⱘᇍ䈵ⱘ䭓ᑺׂᬍЎ size Ͼᄫ㡖DŽབᵰᮄߚ䜡ⱘݙᄬ↨ॳݙᄬ void *realloc(void *p, size_t size) 㽕∖ˈ߭䖨ಲ NULLDŽ䆹ߑ᭄ϡᇍߚ䜡ⱘݙᄬऎඳ䖯㸠߱ྟ࣪DŽ malloc ߑ᭄Ў䭓ᑺЎ size ⱘᇍ䈵ߚ䜡ݙᄬˈᑊ䖨ಲᣛ৥ߚ䜡ऎඳⱘᣛ䩜˗㢹᮴⊩⒵䎇 void *malloc(size_t size) ඳⱘᣛ䩜˗㢹᮴⊩⒵䎇㽕∖ˈ߭䖨ಲ NULLDŽ䆹ぎ䯈ⱘ߱ྟ䭓ᑺЎ 0 ᄫ㡖DŽ calloc ߑ᭄Ў⬅ nobj Ͼ䭓ᑺЎ size ⱘᇍ䈵㒘៤ⱘ᭄㒘ߚ䜡ݙᄬˈᑊ䖨ಲᣛ৥ߚ䜡ऎ void *calloc(size_t nobj, size_t size) srand ߑ᭄ᇚ seed ԰Ў⫳៤ᮄⱘӾ䱣ᴎ᭄ᑣ߫ⱘ⾡ᄤ᭄DŽ⾡ᄤ᭄ seed ⱘ߱ؐЎ 1DŽ void srand(unsigned int seed) rand ߑ᭄ѻ⫳ϔϾ 0̚RAND_MAX П䯈ⱘӾ䱣ᴎᭈ᭄DŽRAND_MAX ⱘপؐ㟇ᇥЎ 32767DŽ int rand(void) Ў ULONG_MAXDŽ strtoul ߑ᭄ⱘࡳ㛑Ϣ strtol ߑ᭄ⳌৠˈԚ݊㒧ᵰЎ unsigned long ㉏ൟˈ䫭䇃ؐ unsigned long strtoul(const char *s, char **endp, int base) ৠᯊᇚ errno ⱘؐ䆒㕂Ў ERANGEDŽ ҹࡴϞࠡᇐ 0x ៪ 0XDŽབᵰ㒧ᵰϞ⑶ˈ߭ߑ᭄ḍ᥂㒧ᵰⱘヺো䖨ಲ LONG_MAX ៪ LONG_MINˈ ϟDŽᄫ↡ഛ㸼⼎ 10̚base•1 П䯈ⱘ᭄ᄫDŽབᵰ base ؐᰃ 16ˈ߭ৃމ䖯ࠊDŽ᮴䆎೼ા⾡ᚙ ߭෎ᑩЎܿ䖯ࠊǃक䖯ࠊ៪क݁䖯ࠊDŽҹ 0 Ўࠡ㓔ⱘᰃܿ䖯ࠊˈҹ 0x ៪ 0X Ўࠡ㓔ⱘᰃक݁ ᅮ䕧ܹᰃҹ䆹᭄Ў෎ᑩⱘ˗བᵰ base ⱘপؐЎ 0ˈ؛ЁDŽབᵰ base ⱘপؐ೼ 2̚36 П䯈ˈ߭ 䴲 endp Ў NULLˈ৺߭䆹ߑ᭄ᇚᡞᣛ৥ s Ё᳿䕀ᤶ䚼ߚ˄s ⱘৢ䕡䚼ߚ˅ⱘᣛ䩜ֱᄬ೼*endp strtol ߑ᭄ᇚᄫヺІ s ⱘࠡ㓔䕀ᤶЎ long ㉏ൟˈᑊ೼䕀ᤶᯊ䏇䖛 s ⱘࠡᇐぎⱑヺDŽ䰸 long strtol(const char *s, char **endp, int base) ϟˈerrno 䛑ᇚ㹿䆒㕂Ў ERANGEDŽމ⾡ᚙ ЁDŽབᵰ㒧ᵰϞ⑶ˈ߭ߑ᭄䖨ಲᏺ᳝䗖ᔧヺো HUGE_VAL˗བᵰ㒧ᵰϟ⑶ˈ߭䖨ಲ 0DŽ೼䖭ϸ 䰸њ endp Ў NULLˈ৺߭䆹ߑ᭄ᇚᡞᣛ৥ sЁ᳾䕀ᤶ䚼ߚ˄sⱘৢ㓔䚼ߚ˅ⱘᣛ䩜ֱᄬ೼*endp strtod ߑ᭄ᇚᄫヺІ s ⱘࠡ㓔䕀ᤶЎ double ㉏ൟˈᑊ೼䕀ᤶᯊ䏇䖛 s ⱘࠡᇐぎⱑヺDŽ double strtod(const char *s, char **endp) 10)DŽ atol ߑ᭄ᇚᄫヺІ s 䕀ᤶЎ long ㉏ൟDŽ䆹ߑ᭄ㄝӋѢ strtol(s, (char**)NULL, long atol(const char *s) w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! idiv ߑ᭄䅵ㅫ num/denom ⱘଚ੠ԭ᭄ˈᑊᡞ㒧ᵰߚ߿ֱᄬ೼㒧ᵘ㉏ൟ idiv_t ⱘϸϾ ldiv_t ldiv(long num, long denom) ㉏ൟⱘ៤ਬ quot ੠ rem ЁDŽ div ߑ᭄䅵ㅫ num/denom ⱘଚ੠ԭ᭄ˈᑊᡞ㒧ᵰߚ߿ֱᄬ೼㒧ᵘ㉏ൟ div_t ⱘϸϾ int div_t div(int num, int denom) labs ߑ᭄䖨ಲ long ㉏ൟখ᭄ n ⱘ㒱ᇍؐDŽ long labs(long n) abs ߑ᭄䖨ಲ int ㉏ൟখ᭄ n ⱘ㒱ᇍؐDŽ int abs(int n) 䭓ᑺЎ sizeDŽ↨䕗ߑ᭄ cmp Ϣ bsearch ߑ᭄Ёⱘᦣ䗄ⳌৠDŽ qsort ߑ᭄ᇍ base[0]...base[n•1]᭄㒘Ёⱘᇍ䈵䖯㸠छᑣᥦᑣˈ᭄㒘Ё↣Ͼᇍ䈵ⱘ int (*cmp)(const void *, const void *)) void qsort(void *base, size_t n, size_t size, བᵰϡᄬ೼ऍ䜡乍ˈ߭䖨ಲ NULLDŽ ؐDŽ᭄㒘 base Ёⱘ乍ᖙ乏ᣝछᑣᥦ߫DŽbsearch ߑ᭄䖨ಲϔϾᣛ䩜ˈᅗᣛ৥ϔϾऍ䜡乍ˈ খ᭄ㄝѢ㄀ѠϾখ᭄ˈᅗᖙ乏䖨ಲ䳊˗བᵰ㄀ϔϾখ᭄໻Ѣ㄀ѠϾখ᭄ˈᅗᖙ乏䖨ಲϔϾℷ བᵰ㄀ϔϾখ᭄˄ᶹᡒ݇䬂ᄫ˅ᇣѢ㄀ѠϾখ᭄˄㸼乍˅ˈᅗᖙ乏䖨ಲϔϾ䋳ؐ˗བᵰ㄀ϔϾ bsearch ߑ᭄೼ base[0]...base[n•1]П䯈ᶹᡒϢ*key ऍ䜡ⱘ乍DŽ೼ߑ᭄ cmp Ёˈ int (*cmp)(const void *keyval, const void *datum)) size_t n, size_t size, void *bsearch(const void *key, const void *base, 㒚㡖Ϣ݋ԧⱘᅲ⦄᳝݇DŽ getenv ߑ᭄䖨ಲϢ name ᳝݇ⱘ⦃๗ᄫヺІDŽབᵰ䆹ᄫヺІϡᄬ೼ˈ߭䖨ಲ NULLDŽ݊ char *getenv(const char *name) ߭䆹ߑ᭄䖨ಲ䴲 0 ؐDŽབᵰ s ⱘؐϡᰃ NULLˈ߭䖨ಲؐϢ݋ԧⱘᅲ⦄᳝݇DŽ system ߑ᭄ᇚᄫヺІ s Ӵ䗦㒭ᠻ㸠⦃๗DŽབᵰ s ⱘؐЎ NULLˈᑊϨ᳝ੑҸ໘⧚⿟ᑣˈ int system(const char *s) 䴲 0 ؐDŽ atexit ߑ᭄ⱏ䆄ߑ᭄ fcnˈ䆹ߑ᭄ᇚ೼⿟ᑣℷᐌ㒜ℶᯊ㹿䇗⫼DŽབᵰⱏ䆄༅䋹ˈ߭䖨ಲ int atexit(void (*fcn)(void)) ੠ EXIT_FAILURE ԰Ў䖨ಲؐDŽ ⱘؐབԩ䖨ಲ㒭⦃๗㽕㾚݋ԧⱘᅲ⦄㗠ᅮˈԚ 0 ؐ㸼⼎㒜ℶ៤ࡳDŽгৃՓ⫼ؐ EXIT_SUCCESS ऎᇚ㹿⏙⋫ˈ᠔᳝Ꮖᠧᓔⱘ⌕ᇚ㹿݇䯁ˈ᥻ࠊгᇚ䖨ಲ㒭⦃๗DŽstatusކ᠔᳝Ꮖᠧᓔⱘ᭛ӊ㓧 ϟˈމexit ߑ᭄Փ⿟ᑣℷᐌ㒜ℶDŽatexit ߑ᭄ⱘ䇗⫼乎ᑣϢⱏ䆄ⱘ乎ᑣⳌডˈ䖭⾡ᚙ void exit(int status) abort ߑ᭄Փ⿟ᑣ䴲ℷᐌ㒜ℶDŽ݊ࡳ㛑Ϣ raise(SIGABRT)㉏ԐDŽ void abort(void) w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! setjmp ᅣᇚ⢊ᗕֵᙃֱᄬࠄ env Ёˈկ longjmp Փ⫼DŽབᵰⳈ᥹䇗⫼ setjmpˈ߭䖨 int setjmp(jmp_buf env) 䆌ゟेҢϔϾ⏅ሖጠ༫ⱘߑ᭄䇗⫼Ё䖨ಲDŽܕ߿ᰃˈᅗ ༈᭛ӊЁⱘໄᯢᦤկњϔ⾡ϡৠѢ䗮ᐌⱘߑ᭄䇗⫼੠䖨ಲ乎ᑣⱘᮍᓣˈ⡍ B.8 䴲ሔ䚼䏇䕀˖ void va_end(va_list ap); ೼᠔᳝ⱘখ᭄໘⧚ᅠ↩ПৢˈϨ೼䗔ߎߑ᭄ f Пࠡˈᖙ乏䇗⫼ᅣ va_end ϔ⃵ˈབϟ᠔⼎˖ type va_arg(va_list ap, type); ᅗৠᯊ䖬ׂᬍ apˈҹՓᕫϟϔ⃵ᠻ㸠 va_arg ᯊ䖨ಲϟϔϾখ᭄˖ ℸৢˈ↣⃵ᠻ㸠ᅣ va_arg 䛑ᇚѻ⫳ϔϾϢϟϔϾ᳾ੑৡⱘখ᭄݋᳝Ⳍৠ㉏ൟ੠᭄ؐⱘؐˈ va_start(va_list ap, lastarg); ೼䆓䯂ӏԩ᳾ੑৡⱘখ᭄ࠡˈᖙ乏⫼ va—start ᅣ߱ྟ࣪ ap ϔ⃵˖ va_list ap; ೼ߑ᭄ f ݙໄᯢϔϾ㉏ൟЎ va_list ⱘব䞣 apˈᅗᇚձ⃵ᣛ৥↣Ͼᅲ䰙খ᭄˖ ᅮߑ᭄ f ᏺ᳝ৃব᭄Ⳃⱘᅲ䰙খ᭄ˈlastarg ᰃᅗⱘ᳔ৢϔϾੑৡⱘᔶᓣখ᭄DŽ䙷Мˈ؛ ༈᭛ӊᦤկњ䘡ग़᳾ⶹ᭄Ⳃ੠㉏ൟⱘߑ᭄খ᭄㸼ⱘࡳ㛑DŽ B.7 ৃবখ᭄㸼˖ བᵰᅮНњᅣ NDEBUGˈৠᯊজࣙ৿њ༈᭛ӊˈ߭ assert ᅣᇚ㹿ᗑ⬹DŽ __FILE__ঞ__LINE__DŽ ᠧॄ⍜ᙃৢˈ䆹ᅣᇚ䇗⫼ abort 㒜ℶ⿟ᑣⱘᠻ㸠DŽ݊Ёⱘ⑤᭛ӊৡ੠㸠োᴹ㞾Ѣ乘໘⧚఼ᅣ Assertion failed: 㸼䖲ᓣ, file ⑤᭛ӊৡ, line 㸠ো ᯊˈ㸼䖒ᓣⱘؐЎ 0ˈ߭ assert ᅣᇚ೼ stderr Ёᠧॄϔᴵ⍜ᙃˈ↨བ˖ assert(expression) བᵰᠻ㸠䇁হ void assert(int expression) assert ᅣ⫼ѢЎ⿟ᑣ๲ࡴ䆞ᮁࡳ㛑DŽ݊ᔶᓣབϟ˖ B.6 䆞ᮁ˖ long ㉏ൟⱘ៤ਬ quot ੠ rem ЁDŽ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ᔧഄᯊ䯈ˈ಴Ўᯊऎㄝॳৠˈᔧഄᯊ䯈Ϣ᮹ग़ᯊ䯈ৃ㛑ϡⳌৠDŽclock_t ੠ time_t ᰃϸϾ ༈᭛ӊЁໄᯢњϔѯ໘⧚᮹ᳳϢᯊ䯈ⱘ㉏ൟ੠ߑ᭄DŽ݊Ёⱘϔѯߑ᭄⫼Ѣ໘⧚ B.10 ᮹ᳳϢᯊ䯈ߑ᭄˖ raise ৥⿟ᑣথ䗕ֵো sigDŽབᵰথ䗕ϡ៤ࡳˈ߭䖨ಲϔϾ䴲 0 ؐDŽ int raise(int sig) ֵোⱘ߱ྟ⢊ᗕ⬅݋ԧⱘᅲ⦄ᅮНDŽ ᠻ㸠DŽ handler)(sig)䇗⫼ⱘϔḋDŽֵো໘⧚⿟ᑣ䖨ಲৢˈ⿟ᑣᇚҢֵোথ⫳ⱘԡ㕂䞡ᮄᓔྟ*) ⬅ڣᔧ䱣ৢ⺄ࠄֵো sig ᯊˈ䆹ֵোᇚᘶ໡Ў咬䅸㸠Ўˈ䱣ৢ䇗⫼ֵো໘⧚⿟ᑣˈህད ᇍѢ⡍ᅮⱘֵোˈsignal ᇚ䖨ಲ handler ⱘࠡϔϾؐ˗བᵰߎ⦄䫭䇃ˈ߭䖨ಲؐ SIG_ERRDŽ SIGTERM থ䗕㒭⿟ᑣⱘ㒜ℶ䇋∖ ܗ఼䆓䯂ˈབ䆓䯂ϡᄬ೼ⱘݙᄬऩټSIGSEGV 䴲⊩ᄬ SIGINT ⫼ѢѸѦᓣⳂⱘֵোˈབЁᮁ བ䴲⊩ᣛҸˈڣSIGILL 䴲⊩ߑ᭄᯴ SIGFPE ㅫᴃ䖤ㅫߎ䫭ˈབ䰸᭄Ў 0 ៪⑶ߎ SIGABRT ᓖᐌ㒜ℶˈ՟བ⬅ abort ᓩ䍋ⱘ㒜ℶ ৥ⱘߑ᭄(ҹֵো԰Ўখ᭄)DŽ᳝ᬜⱘֵোࣙᣀ˖ ᅮНⱘ咬䅸㸠Ў˗བᵰ handler ⱘؐᰃ SIG_IGNˈ߭ᗑ⬹䆹ֵো˗৺߭ˈ䇗⫼ handler ᣛ ᅮњབԩ໘⧚ৢ㓁ⱘֵোDŽབᵰ handler ⱘؐᰃ SIG_DFLˈ߭䞛⫼⬅ᅲ⦄އ signal void (*signal(int sig, void (*handler)(int)))(int) ⑤Ѣ໪䚼ⱘЁᮁֵো៪⿟ᑣᠻ㸠䫭䇃ᓩ䍋ⱘЁᮁֵোDŽ ༈᭛ӊᦤկњϔѯ໘⧚⿟ᑣ䖤㸠ᳳ䯈ᓩথⱘ৘⾡ᓖᐌᴵӊⱘࡳ㛑ˈ↨བᴹ B.9 ֵো˖ ߭ᅗӀᇚব៤᳾ᅮН⢊ᗕDŽ ৠ˖೼䇗⫼ setjmp ᅣৢˈབᵰ䇗⫼ setjmp ᅣⱘߑ᭄Ёⱘ䴲 volatile 㞾ࡼব䞣ᬍবњˈ ໪ˈৃ䆓䯂ᇍ䈵ⱘؐৠ䇗⫼ longjmp ᯊⱘؐⳌމ⫼ⱘߑ᭄ⱘᠻ㸠ᖙ乏䖬≵᳝㒜ℶDŽ䰸ϟ߫ᚙ ᘶ໡ᠻ㸠ˈ݊⢊ᗕㄝৠѢ setjmp ᅣ䇗⫼߮߮ᠻ㸠ᅠᑊ䖨ಲ䴲 0 ؐ valDŽࣙ৿ setjmp ᅣ䇗 longjmp 䗮䖛᳔䖥ϔ⃵䇗⫼ setjmp ᯊֱᄬࠄ env Ёⱘֵᙃᘶ໡⢊ᗕˈৠᯊˈ⿟ᑣ䞡ᮄ void longjmp(jmp_buf env, int val) /* get here by calling longjmp */ else /* get here on direct call */ if (setjmp(env) == 0) ՟བ˖ ᭛Ёˈབ⫼Ѣ if 䇁হǃswitch 䇁হǃᕾ⦃䇁হⱘᴵӊ⌟䆩Ёҹঞϔѯㅔऩⱘ݇㋏㸼䖒ᓣЁDŽ ಲؐЎ 0˗བᵰᰃ೼ longjmp Ё䇗⫼ setjmpˈ߭䖨ಲؐЎ䴲 0DŽSetjmp া㛑⫼ѢᶤѯϞϟ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! localtime ߑ᭄ᇚ㒧ᵘ*tp Ёⱘ᮹ग़ᯊ䯈䕀ᤶЎᔧഄᯊ䯈DŽ struct tm *localtime(const time_t *tp) ߑ᭄䖨ಲ NULLDŽߑ᭄ৡᄫ gmtime ᳝ϔᅮⱘग़৆ᛣНDŽ gmtime ߑ᭄ᇚ*tp Ёⱘ᮹ग़ᯊ䯈䕀ᤶЎण䇗Ϫ⬠ᯊ˄UTC˅DŽབᵰ᮴⊩㦋প UTCˈ߭䆹 struct tm *gmtime(const time_t *tp) asctime(localtime(tp)) ctime ߑ᭄ᇚ㒧ᵘ*tp Ёⱘ᮹ग़ᯊ䯈䕀ᤶЎᔧഄᯊ䯈DŽᅗㄝӋѢϟ߫ߑ᭄䇗⫼˖ char *ctime(const time_t *tp) Sun Jan 3 15:14:13 1988\n\0 asctime ߑ᭄ᇚ㒧ᵘ*tp Ёⱘᯊ䯈䕀ᤶЎϟ߫᠔⼎ⱘᄫヺІᔶᓣ˖ char *asctime(const struct tm *tp) ϟ䴶 4 Ͼߑ᭄䖨ಲᣛ৥ৃ㹿݊ᅗ䇗⫼㽚Ⲫⱘ䴭ᗕᇍ䈵ⱘᣛ䩜DŽ 㛑㸼⼎ˈ߭䖨ಲ•1DŽ ৘៤ਬⱘؐԡѢϞ䴶᠔⼎㣗ೈПݙDŽmktime ߑ᭄䖨ಲ䕀ᤶৢᕫࠄⱘ᮹ग़ᯊ䯈˗བᵰ䆹ᯊ䯈ϡ mktime ߑ᭄ᇚ㒧ᵘ*tp Ёⱘᔧഄᯊ䯈䕀ᤶЎϢ time 㸼⼎ᮍᓣⳌৠⱘ᮹ग़ᯊ䯈ˈ㒧ᵘЁ time_t mktime(struct tm *tp) difftime ߑ᭄䖨ಲ time2•time1 ⱘؐ˄ҹ⾦Ўऩԡ˅DŽ double difftime(time_t time2, time_t time1) ߭ৠᯊᇚ䖨ಲؐ䌟㒭*tpDŽ time ߑ᭄䖨ಲᔧࠡ᮹ग़ᯊ䯈DŽབᵰ᮴⊩㦋প᮹ग़ᯊ䯈ˈ߭䖨ಲؐЎ•1DŽབᵰ tp ϡᰃ NULLˈ time_t time(time_t *tp) ؐЎ•1DŽclock()/CLOCKS_PER_SEC ᰃҹ⾦Ўऩԡ㸼⼎ⱘᯊ䯈DŽ clock ߑ᭄䖨ಲ⿟ᑣᓔྟᠻ㸠ৢऴ⫼ⱘ໘⧚఼ᯊ䯈DŽབᵰ᮴⊩㦋প໘⧚఼ᯊ䯈ˈ߭䖨ಲ clock_t clock(void) Փ⫼Ҹᯊˈtm_isdst ⱘؐЎℷˈ৺߭Ў 0DŽབᵰ䆹ֵᙃ᮴ᬜˈ߭݊ؐЎ䋳DŽ int tm_isdst; ໣Ҹᯊᷛ䆄 int tm_yday; Ң 1 ᳜ 1 ᮹䍋㒣䖛ⱘ໽᭄(0, 365) int tm_wday; Ң᯳ᳳ໽䍋㒣䖛ⱘ໽᭄(0, 6) int tm_year; Ң 1900 ᑈ䍋㒣䖛ⱘᑈ᭄ int tm_mon; Ң 1 ᳜䍋㒣䖛ⱘ᭄᳜(0, 11) int tm_mday; ᔧ᳜ⱘ໽᭄(1, 31) int tm_hour; Ңज໰ᓔྟ㒣䖛ⱘᇣᯊ᭄(0, 23) int tm_min; Ңᔧࠡᇣᯊᓔྟ㒣䖛ⱘߚ䩳᭄(0, 59) int tm_sec; Ңᔧࠡߚ䩳ᓔྟ㒣䖛ⱘ⾦᭄(0, 61) ⫼䗨ঞপؐ㣗ೈབϟ᠔⼎˖ 㸼⼎ᯊ䯈ⱘㅫᴃ㉏ൟˈstruct tm ⫼Ѣֱᄬ᮹ग़ᯊ䯈ⱘ৘Ͼᵘ៤䚼ߚDŽ㒧ᵘ tm Ё৘៤ਬⱘ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! LONG_MIN •2147483647 long ㉏ൟⱘ᳔ᇣؐ LONG_MAX 2147483647 long ㉏ൟⱘ᳔໻ؐ INT_MIN •32767 int ㉏ൟⱘ᳔ᇣؐ INT_MAX 32767 int ㉏ൟⱘ᳔໻ؐ CHAR_MIN 0 ៪ SCHAR_MIN char ㉏ൟⱘ᳔ᇣؐ CHAR_MAX UCHAR_MAX ៪ SCHAR_MAX char ㉏ൟⱘ᳔໻ؐ CHAR_BIT 8 char ㉏ൟⱘԡ᭄ ೼ᅲ䰙㋏㒳ЁৃҹՓ⫼᳈໻ⱘؐDŽ ༈᭛ӊᅮНњϔѯ㸼⼎ᭈൟ໻ᇣⱘᐌ䞣DŽҹϟ᠔߫ⱘؐᰃৃ᥹ফⱘ᳔ᇣؐˈ B.11 Ϣ݋ԧᅲ⦄Ⳍ݇ⱘ䰤ࠊ˖ %% %ᴀ䑿 %Z ᯊऎৡ˄བᵰ᳝ⱘ䆱˅ %Y ᏺϪ㑾᭄Ⳃⱘᑈӑ %y ϡᏺϪ㑾᭄Ⳃⱘᑈӑ˄00•99˅ %X ᔧഄᯊ䯈㸼⼎ %x ᔧഄ᮹ᳳ㸼⼎ %W ϔᑈЁⱘ᯳ᳳᑣো˄00•53ˈᇚ᯳ᳳϔⳟ԰ᰃ↣਼ⱘ㄀ϔ໽˅ %w ϔ਼Ёⱘ৘໽˄0•6ˈ᯳ᳳ᮹Ў 0˅ %U ϔᑈЁⱘ᯳ᳳᑣো˄00•53ˈᇚ᯳ᳳ᮹ⳟ԰ᰃ↣਼ⱘ㄀ϔ໽˅ %S ⾦˄00•61˅ %p Ϣ AM Ϣ PM Ⳍᑨⱘᔧഄᯊ䯈ㄝӋ㸼⼎ᮍ⊩ %M ߚ䩳˄00•59˅ %m ᳜ӑ˄01•12˅ %j ϔᑈЁⱘ৘໽˄001—366˅ %I ᇣᯊ˄12 ᇣᯊ㸼⼎˅˄01•12˅ %H ᇣᯊ˄24 ᇣᯊ㸼⼎˅˄00•23˅ %d ϔϾ᳜Ёⱘᶤϔ໽˄01•31˅ %c ᔧഄᯊ䯈੠᮹ᳳ㸼⼎ %B ᳜ӑܼৡ %b 㓽ݭⱘ᳜ӑৡ %A ϔ᯳ᳳЁ৘໽ⱘܼৡ %a ϔ᯳ᳳЁ৘໽ⱘ㓽ݭৡ fmt ⱘ䕀ᤶ䇈ᯢঞ݊৿Нབϟ᠔⼎˖ ᭄໮Ѣ smaxˈ䆹ߑ᭄ᇚ䖨ಲؐ 0DŽ ᄫヺݭࠄ s ЁDŽstrftime ߑ᭄䖨ಲᅲ䰙ݭࠄ s Ёⱘᄫヺ᭄˄ϡࣙᣀᄫヺ'\0'˅˗བᵰᄫヺ ᇚ໡ࠊࠄ s ЁDŽ↣Ͼ%c ᇚᣝ✻ϟ䴶ᦣ䗄ⱘḐᓣ᳓ᤶЎϢᴀഄ⦃๗Ⳍ䗖ᑨⱘؐDŽ᳔໮ smax Ͼ ࠄ s Ёˈ݊Ё fmt ㉏ԐѢ printf ߑ᭄ЁⱘḐᓣ䇈ᯢDŽ᱂䗮ᄫヺ˄ࣙᣀ㒜㒧ヺ'\0'˅ټᑊᄬ strftime ߑ᭄ḍ᥂ fmt ЁⱘḐᓣᡞ㒧ᵘ*tp Ёⱘ᮹ᳳϢᯊ䯈ֵᙃ䕀ᤶЎᣛᅮⱘḐᓣˈ *tp) size_t strftime(char *s, size_t smax, const char *fmt, const struct tm w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! DBL_MIN_EXP ᳔ᇣⱘ᭄ nˈn ⒵䎇˖10n ᰃϔϾ㾘Ḑ᭄࣪ DBL_MIN 1E•37 ᳔ᇣⱘ㾘Ḑ࣪ঠ㊒ᑺ⍂⚍᭄ DBL_MAX_EXP ᳔໻ⱘ᭄ nˈn ⒵䎇 FLT_RADIXn•1 ҡᰃৃ㸼⼎ⱘ DBL_MAX 1E+37 ᳔໻ⱘঠ㊒ᑺ⍂⚍᭄ DBL_MANT_DIG ሒ᭄Ёⱘ᭄˄ҹ FLT_RADIX Ў෎᭄˅ DBL_EPSILON 1E•9 ᳔ᇣⱘ᭄ xˈx ⒵䎇˖1.0 + x  1.0 DBL_DIG 10 㸼⼎㊒ᑺⱘक䖯ࠊ᭄ᄫ FLT_MIN_EXP ᳔ᇣⱘ᭄ nˈn ⒵䎇˖10n ᰃϔϾ㾘Ḑ᭄࣪ FLT_MIN 1E•37 ᳔ᇣⱘ㾘Ḑ࣪⍂⚍᭄ FLT_MAX_EXP ᳔໻ⱘ᭄ nˈn ⒵䎇 FLT_RADIXn•1 ҡᰃৃ㸼⼎ⱘ FLT_MAX 1E+37 ᳔໻ⱘ⍂⚍᭄ FLT_MANT_DIG ሒ᭄Ёⱘ᭄˄ҹ FLT_RADIX Ў෎᭄˅ FLT_EPSILON 1E•5 ᳔ᇣⱘ᭄ xˈx ⒵䎇˖1.0 + x  1.0 FLT_DIG 6 㸼⼎㊒ᑺⱘक䖯ࠊ᭄ᄫ FLT_ROUNDS ࡴ⊩ⱘ⍂⚍㟡ܹ῵ᓣ FLT_RADIX 2 ᣛ᭄㸼⼎ⱘ෎᭄ˈ՟བ 2ǃ16 ߎⱘ↣Ͼؐҷ㸼Ⳍᑨ䞣ⱘ᳔ᇣপؐDŽ৘Ͼᅲ⦄ৃҹᅮН䗖ᔧⱘؐDŽ ϟ㸼߫ߎⱘৡᄫᰃⱘϔϾᄤ䲚ˈᅗӀᰃϢ⍂⚍ㅫᴃ䖤ㅫⳌ݇ⱘϔѯᐌ䞣DŽ㒭 USHRT_MAX 65535 unsigned short ㉏ൟⱘ᳔໻ؐ ULONG_MAX 4294967295 unsigned long ㉏ൟⱘ᳔໻ؐ UINT_MAX 65535 unsigend int ㉏ൟⱘ᳔໻ؐ UCHAR_MAX 255 unsigned char ㉏ൟⱘ᳔໻ؐ SHRT_MIN •32767 short ㉏ൟⱘ᳔ᇣؐ SHRT_MAX +32767 short ㉏ൟⱘ᳔໻ؐ SCHAR_MIN •127 signed char ㉏ൟⱘ᳔ᇣؐ SCHAR_MAX +127 signed char ㉏ൟⱘ᳔໻ؐ w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! ϟˈᣛ䩜ϢᭈൟП䯈ҹঞϡৠ㉏ൟⱘᣛ䩜П䯈䖤ㅫⱘ㾘߭DŽމࠊ㉏ൟ䕀ᤶⱘᚙ 䗮⫼ᣛ䩜㉏ൟ˗೼ℸПࠡ char * ᡂⓨⴔ䖭ϔ㾦㡆DŽৠᯊˈᯢ⹂ഄ㾘ᅮњ೼ϡ䖯㸠ᔎ ᓩܹњ void * ㉏ൟˈᑊ԰Ўϔ⾡ޚ· ᕜ໮㓪䆥఼೼޴ᑈࠡህᅲ⦄њ void ㉏ൟDŽᮄᷛ ⫼ᴹᰒᓣ㸼⼎ᄫヺ੠݊ᅗᭈൟᇍ䈵ⱘヺোDŽ ᓩܹњ݇䬂ᄫ signedˈޚ· ᳝↉ᯊ䯈ˈC 䇁㿔ЁৃҹՓ⫼ unsigned char ㉏ൟDŽᮄᷛ ⱘ⫼⊩ˈԚৃҹ⫼ long double ໄᯢ᳈催㊒ᑺⱘ⍂⚍᭄DŽ ᏺヺো㉏ൟ៪᮴ヺো㉏ൟDŽᬒᓗњᇚ long float ԰Ў double ⱘৠН䆡䖭⾡⣀⡍ · Ϣ݊ᅗ㉏ൟϔḋˈᇍᄫヺ㉏ൟгৃҹՓ⫼݇䬂ᄫ signed ៪ unsigned ᰒᓣໄᯢЎ · ᦤկњᆑᄫヺᄫヺІᄫ䴶ؐ੠ᄫヺᐌ䞣ⱘ㸼⼎ᮍ⊩ˈখ㾕 A.2.6 㡖DŽ · Ⳍ䚏ⱘᄫヺІᇚ㹿䖲᥹೼ϔ䍋DŽ ⫼Ѣ⍂⚍᭄DŽᅗৠᯊг㒚࣪њ᮴ৢ㓔ᐌ䞣㉏ൟⱘⳌ݇㾘߭˄খ㾕 A.2.5 㡖 ˅DŽ ᓩܹњ᳈໻ⱘৢ㓔䲚ড়ˈՓᕫᐌ䞣ⱘ㉏ൟ᳈ࡴᯢ⹂˖U ៪ L ⫼ѢᭈൟˈF ៪ Lޚ· ᮄᷛ · ᠔᳝Ҏ䛑୰⃶ⱘϔϾᇣব࣪˖8 ੠ 9 ϡ⫼԰ܿ䖯ࠊ᭄ᄫDŽ ϡᰃ䕀Нᑣ߫ˈ߭݊㒧ᵰᰃ᳾ᅮНⱘDŽখ㾕 A.2.5 㡖DŽ · ᅮНњᄫヺᐌ䞣੠ᄫヺІᄫ䴶ؐЁՓ⫼ⱘᮄ䕀Нᄫヺᑣ߫DŽབᵰ\ঞ݊ৢᄫヺᵘ៤ⱘ ᇚϡݡՓ⫼DŽ · ᓩܹњϔѯᮄ݇䬂ᄫ˄voidǃconstǃvolatileǃsigned ੠ enum˅DŽ݇䬂ᄫ entry ৃ㛑Ӯᬍবࣙ৿“??āⱘᄫヺІⱘ৿НDŽ \ǃ^ǃ[ǃ]ǃ{ǃ}ǃ|ǃ~ㄝ䕀Нᄫヺˈখ㾕 A.12.1 㡖DŽ⊼ᛣˈϝᄫヺᑣ߫ⱘᓩܹ · 䗮䖛ঠ䯂ো”??āᓩܹⱘϝᄫヺᑣ߫ৃҹ㸼⼎ᶤѯᄫヺ䲚Ё㔎ᇥⱘᄫヺDŽᅮНњ#ǃ 䆌᳈䭓ⱘᷛ䆚ヺ˅DŽܕᬜ䭓ᑺҡ✊Ў 6 Ͼᄫヺ˄ᕜ໮ᅲ⦄Ё · ᠔᳝ݙ䚼ᷛ䆚ヺⱘ᳔ᇣ᳝ᬜ䭓ᑺ๲ࡴЎ 31 Ͼᄫヺ˗݋᳝໪䚼䖲᥹ⱘᷛ䆚ヺⱘ᳔ᇣ᳝ ҙҙ䰤Ѣ೼ᄫヺІ੠ᅣᅮНЁDŽ䆺㒚ֵᙃখ㾕 A.12 㡖DŽ 䆌೼ӏԩഄᮍՓ⫼ড᭰ᴴᄫ“\ā䖯㸠㸠ⱘ䖲᥹ˈ㗠ϡܕЁⱘᔶᓣখ᭄ϡݡ㹿᳓ᤶDŽ 䆌Փ⫼Ⳍৠ䆄োᑣ߫䞡ᮄໄᯢᅣ˗ᄫヺІܕ᥻ࠊᣛҸ˄བ#elif ੠#pragma˅˗ᯢ⹂ Ў෎⸔˗๲ࡴњ䖲᥹䆄োⱘ䖤ㅫヺ˄##˅੠⫳៤ᄫヺІⱘ䖤ㅫヺ˄#˅˗๲ࡴњᮄⱘ ᯢ⹂ҹ䆄ো˖ܙC Ё݇Ѣ乘໘⧚ⱘᅮН᳈ࡴ㒚㟈ˈᑊ䖯㸠њᠽ ޚ· Ϣ㄀ 1 ⠜Ⳍ↨ˈᷛ ᮴ৃ↨П໘ˈ಴Ў㄀ 1 ⠜ᑊ≵᳝䆩೒㾘ᅮϔϾ⦃๗៪ᑧDŽ ⱘ䞡㽕㒘៤䚼ߚˈԚᅗӀϢ㄀ 1 ⠜޴Тޚ䇁㿔ᴀ䑿ˈϡ⍝ঞ⦃๗੠ᑧDŽሑㅵ⦃๗੠ᑧгᰃᷛ П䯈ⱘᏂ߿DŽ៥Ӏ೼䖭䞠ҙ䅼䆎ޚᴀ䰘ᔩᘏ㒧њᴀк㄀ 1 ⠜ᅮНⱘ C 䇁㿔Ϣ ANSI ᮄᷛ њDŽ ᳳ䞛⫼ܜথᏗПࠡˈANSI ⱘ᡹ਞህᏆ㒣㹿ϔѯ㓪䆥఼ᦤկଚ䚼ߚഄޚℷDŽ⫮㟇೼ℷᓣⱘ C ᷛ ࣪ᯊ䞛㒇њ݊Ё㒱໻䚼ߚⱘׂᬍˈᑊ䖯㸠њ݊ᅗϔѯ䞡㽕ׂޚ࣪णӮ೼ᇍ C 䇁㿔䖯㸠ᷛޚᷛ AT&T ᦤկⱘ㓪䆥఼ⱘ᭛ḷϔৠথᏗⱘˈᑊ㹿ℸৢⱘ݊ᅗ C 㓪䆥఼կᑨଚ䞛㒇DŽࠡϡЙˈANSI ѯׂᬍׂℷњॳ⠜ᴀЁⱘ℻Нᗻᦣ䗄˗ᶤѯׂᬍᰃᇍᏆ᳝⠜ᴀⱘব᳈DŽ䆌໮ᮄ๲ࡳ㛑䛑ᰃ䱣 䛑ᰃ㒣䖛㊒ᖗ䆒䅵ⱘˈᑊֱᣕњϢ⦄᳝⠜ᴀⱘݐᆍᗻ˗݊Ёⱘϔܙৠᯊ↣⃵ᠽˈܙⱘϔ⃵ᠽ 㞾ᴀк㄀ 1 ⠜ߎ⠜ҹᴹˈC 䇁㿔ⱘᅮНᏆ㒣থ⫳њϔѯব࣪DŽ޴Т↣⃵ব࣪䛑ᰃᇍॳ䇁㿔 䰘ᔩCব᳈ᇣ㒧 w . d o c u • t r a c k.com w w PDF•XC H ANGE Click to buy NOW! · switch 䇁হⱘ᥻ࠊ㸼䖒ᓣ੠ case ᷛোৃҹᰃӏᛣᭈൟDŽ \0˅DŽ · ᰒᓣᣛᅮ䭓ᑺⱘᄫヺ᭄㒘ৃҹ⫼Ϣℸ䭓ᑺⳌৠⱘᄫヺІᄫ䴶ؐ߱ྟ࣪˄ϡࣙᣀᄫヺ · 㞾ࡼ㒧ᵘǃ㘨ড়੠᭄㒘ৃҹ䖯㸠߱ྟ࣪ˈԚ᳝ϔѯ䰤ࠊDŽ · 㘨ড়ৃҹ䖯㸠߱ྟ࣪ˈ߱ؐᓩ⫼݊㄀ϔϾ៤ਬDŽ њ˅DŽ⫣خ݊᠔ሲⱘ㒧ᵘ៪㘨ড়Ⳍ݇㘨˄䖭Ꮖ㒣ᰃ䆌໮ᅲ⦄ⱘ݅ৠ ৠᯊгЎᷛোᓩܹњϔϾऩ⣀ⱘৡᄫぎ䯂ˈখ㾕 A.1.1 㡖DŽ㒧ᵘ៪㘨ড়ⱘ៤ਬৡᇚϢ ᇚ᠔᳝ⱘᷛোᬒ೼Ͼऩ⣀ⱘৡᄫぎ䯈Ёˈޚ· ᷛ䆚ヺⱘৡᄫぎ䯈᳝ϔѯব࣪DŽANSI ᷛ さDŽކϢᔶᓣখ᭄ · ᔶᓣখ᭄ⱘ԰⫼ඳᠽሩࠄߑ᭄ⱘ໡ড়䇁হЁˈ಴ℸˈߑ᭄Ё᳔乊ሖⱘব䞣ໄᯢϡ㛑 ᯢ⹂㾘ᅮˈ䖭⾡ໄᯢⱘ԰⫼ඳҙЎ䆹⿟ᑣഫDŽޚᅗ䚼ߚৃ㾕DŽANSI ᷛ · ೼ᶤѯᅲ⦄Ёˈབᵰݙሖ⿟ᑣഫЁࣙ৿ϔϾ extern ໄᯢˈ߭䆹ໄᯢᇍ䆹᭛ӊⱘ݊ · ⽕ℶ≵᳝ӏԩ䇈ᯢヺ៪䰤ᅮヺ˄াᰃϔϾぎⱘໄᯢヺ˅ⱘ໪䚼᭄᥂䇈ᯢDŽ 䆄ໄᯢ೼໪ሖ԰⫼ඳЁгᰃ䖭ḋDŽ ঺ϔᮍ䴶ˈҙҙাᏺ㒧ᵘᷛ䆄៪㘨ড়ᷛ䆄ⱘໄᯢᰃᇍ䆹ᷛ䆄ⱘ䞡ᮄໄᯢˈेՓ䆹ᷛ ℶぎໄᯢˈे≵᳝ໄᯢヺˈϨ≵᳝㟇ᇥໄᯢϔϾ㒧ᵘˈ㘨ড়៪ᵮВⱘໄᯢDŽ⽕ޚ· ᷛ ߑ᭄ҡ✊ৃҹՓ⫼ˈԚ᳝ϔᅮ䰤ࠊDŽ 䅸ৃⱘ໘⧚ৃবᔶᓣখ᭄㸼ⱘᮍ⊩DŽখ㾕 A.7.3 㡖ǃA.8.6 㡖੠ B.7 㡖DŽᮻᓣໄᯢⱘ 㸼ⱘߑ᭄ⱘᮍ⊩ˈᑊᦤկњϔ⾡㹿ܗЁ䖬㾘ᅮњᰒᓣໄᯢᏺৃববޚൟDŽৠᯊˈᷛ ⱘ㉏ܗ䡈Ѣ C++˅ᓩܹњߑ᭄ॳൟໄᯢⱘ㸼⼎⊩ˈߑ᭄ॳൟЁৃҹໄᯢব׳˄ޚ· ᷛ ᴃ੠݇㋏䖤ㅫDŽখ㾕 A.7.7 㡖DŽ 䆌ᇍ݊䖯㸠ㅫܕ㋴ⱘϟϔϾԡ㕂ⱘᣛ䩜ˈᑊܗ䆌߯ᓎϔϾᣛ৥᭄㒘᳔ৢϔϾܕޚ· ᷛ · ⿏ԡ㸼䖒ᓣⱘ㉏ൟᰃ݊Ꮊ᪡԰᭄ⱘ㉏ൟˈে᪡԰᭄ϡ㛑ᦤछ㒧ᵰ㉏ൟDŽখ㾕 A.7.8 㡖DŽ 䆌Փ⫼ഄഔ䖤ㅫヺDŽܕᬒ೼ᆘᄬ఼Ёгϡ · ഄഔ䖤ㅫヺ&ϡৃᑨ⫼ѢໄᯢЎ register ⱘᇍ䈵ˈेՓ݋ԧⱘᅲ⦄᳾ᇚ䖭⾡ᇍ䈵ᄬ ൟ˄ptrdiff_t˅г᳝㉏Ԑⱘব࣪DŽখ㾕 A.7.4 㡖Ϣ A.7.7 㡖DŽ ༈᭛ӊЁᅮНDŽ݇ѢϸϾᣛ䩜ⱘᏂⱘ㒧ᵰ㉏ޚᇚ݊㉏ൟ size_t ೼ᷛ ᯢ⹂њ䆹䖤ㅫヺⱘ㒧ᵰ㉏ൟϢ݋ԧⱘᅲ⦄᳝݇ˈԚ㽕∖ޚ԰Ў unsigned ㉏ൟDŽᷛ · ೼㄀ 1 ⠜Ёˈsizeof 䖤ㅫヺⱘ㒧ᵰ㉏ൟЎ intˈԚ䱣ৢᕜ໮㓪䆥఼ⱘᅲ⦄ᇚℸ㒧ᵰ 䆌ᇍ᭄㒘ᑨ⫼ഄഔ䖤ㅫヺˈ݊㒧ᵰЎᣛ৥᭄㒘ⱘᣛ䩜DŽܕ · · 㒧ᵘৃҹ㹿䌟ؐǃӴ䗦㒭ߑ᭄ҹঞ㹿ߑ᭄䖨ಲDŽ · ᣛ৥ߑ᭄ⱘᣛ䩜ৃҹ԰Ўߑ᭄ⱘᷛᖫヺˈ㗠ϡ䳔㽕ᰒᓣⱘ*䖤ㅫヺDŽখ㾕 A.7.3 㡖DŽ 䖤ㅫヺ+DŽܗ䖤ㅫヺ•ⱘᇍ⿄ˈᓩܹњϔܗ· ЎњֱᣕϢϔ 䅵ㅫϞгᰃৃ㒧ড়ⱘDŽخ· ೼㓪䆥఼Ёˈϡݡᇚ᭄ᄺϞৃ㒧ড়ⱘ䖤ㅫヺᔧ ೼㄀ 1 ⠜ЁˈᅗӀᰃϸϾ䆄োˈЁ䯈ৃҹ⫼ぎⱑヺߚᓔDŽ · ᮻⱘ䌟ؐ㉏䖤ㅫヺ˄བ=+˅ᏆϡݡՓ⫼DŽৠᯊˈ䌟ؐ㉏䖤ㅫヺ⦄೼ᰃऩϾ䆄ো˗㗠 ᘏᰃ䕀ᤶЎ double ㉏ൟāᏆ᳈ᬍЎĀᦤछࠄ᳔ᇣⱘ䎇໳໻ⱘ㉏ൟāDŽখ㾕 A.6.5 㡖DŽ · ׂᬍњĀ᱂䗮ㅫᴃ㉏ൟ䕀ᤶāˈ⡍߿ഄˈĀᭈൟᘏᰃ䕀ᤶЎ unsigned ㉏ൟˈ⍂⚍᭄ · ᄫヺІϡݡᰃৃҹׂᬍⱘˈ಴ℸৃҹᬒ೼া䇏ݙᄬऎЁDŽ 䞛⫼њ C++Ёⱘ㉏ൟ䰤ᅮヺⱘὖᗉˈབ const˄খ㾕 A.8.2 㡖 ˅DŽޚ· ᷛ · ᮄ๲ࡴⱘᵮВ㉏ൟᰃ㄀ 1 ⠜Ё᠔≵᳝ⱘDŽ ˅Ё㒭ߎњ৘⾡⡍ᅮᅲ⦄ⱘ⡍ᗻDŽ ᯢ⹂ᣛᅮњㅫᴃ㉏ൟপؐ㣗ೈⱘ᳔ᇣؐˈᑊ೼ϸϾ༈᭛ӊ˄੠ޚᮄᷛ ·
还剩231页未读

继续阅读

下载pdf到电脑,查找使用更方便

pdf的实际排版效果,会与网站的显示效果略有不同!!

需要 10 金币 [ 分享pdf获得金币 ] 2 人已下载

下载pdf

pdf贡献者

muzili

贡献于2014-05-10

下载需要 10 金币 [金币充值 ]
亲,您也可以通过 分享原创pdf 来获得金币奖励!
下载pdf