C Lecture 2013.09.19

E35ded9f4d9da564ecf85ad527f11639?s=47 masawada
September 19, 2013

C Lecture 2013.09.19

E35ded9f4d9da564ecf85ad527f11639?s=128

masawada

September 19, 2013
Tweet

Transcript

  1. ϓϩάϥϛϯάߨश 2013.09.19 by @masawada, @alstamber

  2. Έͳ͞Μ ͓͸Α͏͍͟͝·͢

  3. ߨࢣ

  4. masawada

  5. alstamber

  6. ΑΖ͓͘͠ئ͍͠·͢

  7. None
  8. ॾ࿈བྷ

  9. • ձ৔͕12:00·Ͱ͔͠࢖͑·ͤΜ • MMAͷΠϕϯτͰ͸ͳ͍ͨΊڭࣨͱΕͣ • ۭ͍ͯͨΒ13:00-15:00͕ͱΕ·͢ • னٳܜΛ12:00-13:00Ͱ͸͞Έ·͢

  10. • ๻͸৯ਆʹ͍͖·͢(ଟ෼) • ॏཁͳ෦෼͸12:00·ͰʹऴΘΒͤ·͢ • 13:00͔Β͸໰୊Λղ͖·͢ • ͍ΘΏΔԋश • ్தୀࣨՄͰ͢

  11. • શһ͍ͳ͘ͳͬͨΒ΍Ί·͢ • ॾ஫ҙ௕͍Ͱ͢Ͷ • ͜Μͳײ͡ͰਐΊ·͢ • Θ͔Βͳ͍͜ͱ͋ͬͨΒਵ࣭࣌໰Λ ͱ͹͍ͯͩ͘͠͞

  12. ߨशͷର৅

  13. ޙظ͔Β ϓϩάϥϛϯά௨࿦ ͕࢝·Δ

  14. ϓϩάϥϛϯά Α͘Θ͔ΒΜ

  15. ͍ͬͯ͏ਓ޲͚

  16. ܭࢉػ௨࿦ ͱͬͯͳ͍ͻͱ޲͚

  17. ຊ೔ͷ໨త

  18. ޙظͷतۀʹ ଱͑͏Δ஌ࣝΛ͚ͭΔ

  19. None
  20. ຊ೔ͷ͓͠ͳ͕͖ • ϓϩάϥϛϯάͱ͸ • Cͷجૅ • ม਺ • ੍ޚߏจ •

    ഑ྻ/ߏ଄ମ • ϙΠϯλ • ؔ਺ • ԋश
  21. ϓϩάϥϛϯάͱ͸

  22. Scratch

  23. ͜Ε΋ ϓϩάϥϛϯά

  24. ͻͱͱ͓Γͷཧղ

  25. •ม਺/഑ྻ •੍ޚߏจ •ؔ਺

  26. + ݴޠಛ༗ͷػೳ

  27. •ϙΠϯλ •ߏ଄ମ

  28. ͦ΋ͦ΋ͳΜͰ CΛษڧ͢Δͷʁ

  29. Rubyͱ͔Pythonͱ͔ φ΢͍ݴޠ΍Γ͍ͨ

  30. ౦େͩͬͯΞϧΰͰ Ruby࢖ͬͯΔ͡ΌΜ

  31. •×ίϯϐϡʔλΛಈ͔͢ •◦ίϯϐϡʔλΛཧղ͢Δ

  32. None
  33. ܭࢉػͷ࢓૊ΈΛ ཧղ͢Δ

  34. ਖ਼௚ͦ͜·Ͱ ਂ͘΍Βͳͯ͘Α͍

  35. • ϓϩάϥϛϯάݴޠҰൠͷߟ͑ํ • Cݴޠಛ༗ͷॻ͖ํ • ͱ͔Λ͞Βͬͱ΍Γ·͢

  36. Cݴޠͷجૅ

  37. •ίʔυΛॻ͘ •ίϯύΠϧ͢Δ •࣮ߦ͢Δ Cݴޠͷجૅ

  38. $ emacs hello.c --- #include <stdio.h> int main() { printf(“Hello,

    world!\n”); return 0; }
  39. $ gcc hello.c -o hello $ ./hello Hello, world!

  40. #include <stdio.h> int main() { printf(“Hello, world!\n”); return 0; }

  41. ࣮͸ͳͯ͘΋ྑ͍

  42. stdio.h = st(andar)d I(n/)O(ut).h(eader)

  43. ඪ४ೖग़ྗ

  44. •printf •scanf

  45. #include <stdio.h> int main() { printf(“Hello, world!\n”); return 0; }

  46. #include <stdio.h> int main() { printf(“Hello, world!\n”); return 0; }

  47. #include <stdio.h> int main() { printf(“Hello, world!\n”); return 0; }

  48. $ gcc hello.c -o hello ɹ

  49. $ [gcc] hello.c -o hello ίϯύΠϧ༻ίϚϯυ

  50. $ gcc [hello.c] -o hello ίϯύΠϧ͢Διʔείʔυ

  51. $ gcc hello.c [-o] hello outfile Φϓγϣϯ

  52. $ gcc hello.c -o [hello] ϑΝΠϧ໊

  53. $ ./hello Hello, World

  54. Ҏ্ɺCͷجૅ

  55. ม਺ͷ࿩

  56. ͻͱͱ͓Γͷཧղ

  57. •ม਺/഑ྻ •੍ޚߏจ •ؔ਺

  58. + ݴޠಛ༗ͷػೳ

  59. •ϙΠϯλ •ߏ଄ମ

  60. • ৘ใͷอଘํ๏(σʔλߏ଄) • ੍ޚ/ؔ਺

  61. • ৘ใͷอଘ(σʔλߏ଄) • ม਺ɺ഑ྻɺ(ϙΠϯλ)ɺߏ଄ମ • ੍ޚ/ؔ਺ • ੍ޚߏจɺؔ਺

  62. ม਺

  63. x = 10 y = 20

  64. int x = 10; int y = 20;

  65. ܭࢉػ

  66. ON 1 OFF 0

  67. 0100101001010100...

  68. None
  69. •int ੔਺ •float, double ුಈখ਺఺(খ਺) •char จࣈ

  70. • ม਺: 1ͭͷσʔλΛอଘ͢Δ΋ͷ • ≒σʔλʹ໊લΛ෇͚͓ͯ࣋ͬͯ͘ • ܕ͸σʔλͷछྨΛද͢ ·ͱΊ

  71. ੍ޚߏจͷ࿩

  72. ϓϩάϥϜ͸্͔Β ॱ൪ʹॲཧ

  73. ॱ൪Λม͍͑ͨʂ

  74. ൺֱԋࢉࢠ

  75. <ͱ͔>ͱ͔==ͱ͔

  76. ཭ࢄ਺ֶ

  77. a b a < b 0 0 0 0 1

    1 1 0 0 1 1 0
  78. 0ͳΒِ 0Ҏ֎ͳΒਅ

  79. ੍ޚߏจ

  80. •if - else - ৚݅Ͱ෼ذ •while - ಉ͡ॲཧΛ·ͱΊΔ •for -

    whileͷ্Ґ൛
  81. if - else

  82. ΋͠ʙͳΒ Ͱͳ͚Ε͹

  83. aͱbͰେ͖͍ํΛ ग़ྗ͍ͨ͠

  84. int a = 10; int b = 20; if (

    a < b ) { printf(“%d\n”, b); } else { printf(“%d\n”, a); }
  85. int a = 10; int b = 20; if (

    a > b ) { printf(“%d\n”, b); } else { printf(“%d\n”, a); }
  86. while

  87. ʙͷؒ

  88. 10͔Β1·Ͱ Χ΢ϯτμ΢ϯ͍ͨ͠

  89. int i = 10; printf(“%d\n”, i); i = i -

    1; printf(“%d\n”, i); i = i - 1; ...
  90. 10 9 8 7 6 5 4 3 2 1

  91. i = i - 1;

  92. int i = 10; // i: 10 int tmp =

    i - 1; // i: 10 i = tmp; // i: 9
  93. int i = 10; printf(“%d\n”, i); i = i -

    1; printf(“%d\n”, i); i = i - 1; ...
  94. int i = 10; while ( 0 < i )

    { printf(“%d\n”, i); i = i - 1; }
  95. for

  96. whileͷ্Ґ൛

  97. int i = 10; while ( 0 < i )

    { printf(“%d\n”, i); i = i - 1; }
  98. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); }
  99. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); }
  100. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); }
  101. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10
  102. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10
  103. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10
  104. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9
  105. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9
  106. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9
  107. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8
  108. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8
  109. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8
  110. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7
  111. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7
  112. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7
  113. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6
  114. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6
  115. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6
  116. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5
  117. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5
  118. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5
  119. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5 4
  120. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5 4
  121. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5 4
  122. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5 4 3
  123. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5 4 3
  124. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5 4 3
  125. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5 4 3 2
  126. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5 4 3 2
  127. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5 4 3 2
  128. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5 4 3 2 1
  129. int i; for (i = 10; 0 < i; i

    = i - 1) { printf(“%d\n”, i); } 10 9 8 7 6 5 4 3 2 1
  130. ·ͱΊ • ॲཧͷॱ൪Λม͑Δͷʹ੍ޚจΛ࢖͏ • ৚݅Ͱ෼ذ͢Δ • ifͱ͔whileͱ͔͍Ζ͍Ζ͋Δ • for͸whileͷ্Ґ൛Έ͍ͨͳײ͡

  131. ·ͱΊ • ଞʹ΋do-whileͱ͔switchͱ͔͋Δ • άάͬͯ(͋Μ·Γ࢖Θͳ͍ؾ͕͢Δ)

  132. None
  133. ഑ྻͷ࿩

  134. ม਺·ͱΊͨ΍ͭ

  135. ม਺·ͱΊͯ ਺ࣈॱʹͳΒ΂ͨ΍ͭ

  136. a͞ΜͷମॏΛ ೥ྸॱʹه࿥͍ͨ͠

  137. int a0 = 3; ... int a6 = 22; int

    a7 = 24; int a8 = 27;
  138. ໊લ෇͚Δͷ͕ ͱͯ΋ΊΜͲ͍͘͞

  139. int a[100]; a[0] = 3; ... a[6] = 22; a[7]

    = 24; a[8] = 27;
  140. ศརʂʂʂ

  141. int a[100]; ܕ ഑ྻ໊[ ֬อ͢Δ਺ ]; a[0] = 3; ഑ྻ໊[

    ఴࣈ ] = ஋;
  142. จࣈ char

  143. จࣈྻ char[]

  144. char name[8] = “haru067”;

  145. ཉ͍͠จࣈ਺+1 Ͱ֬อ

  146. char name[8] = “haru067”;

  147. char name[8] = “haru067\0”;

  148. • ഑ྻΛ࢖͏ͱม਺Λ·ͱΊΒΕΔ • จࣈྻ͸จࣈܕ(char)ͷ഑ྻ ·ͱΊ

  149. ߏ଄ମͷ࿩

  150. ߏ଄ମ͸ දͷΑ͏ͳ΋ͷ

  151. ͪΐͬͱ͚ͩ ഑ྻʹࣅͯΔ

  152. ໊લ ೥ྸ ਎௕ ମॏ Ұ࿠ 19 165 59 ೋ࿠ 20

    172 64 ࡾ࿠ 21 183 72 ࢛࿠ 16 159 53
  153. struct brother { int age; double height; double weight; };

    // ηϛίϩϯΘ͢Εͣʹ
  154. struct brother ichiro; ichiro.age = 19; ichiro.height = 165; ichiro.weight

    = 59; printf(“%d\n”, ichiro.age); // 19
  155. struct brother ichiro; ichiro.age = 19; ichiro.height = 165; ichiro.weight

    = 59; printf(“%d\n”, ichiro.age); // 19
  156. brother ichiro; ichiro.age = 19; ichiro.height = 165; ichiro.weight =

    59; printf(“%d\n”, ichiro.age); // 19
  157. ͍͍ͪͪstructॻ͘ͷ ΊΜͲ͏

  158. ܕͷΑ͏ʹѻ͍͍ͨ

  159. struct brother { char name[20]; int age; double height; double

    weight; };
  160. typedef struct { char name[20]; int age; double height; double

    weight; } brother;
  161. ·ͱΊ • ߏ଄ମ͸ؔ࿈͢ΔσʔλΛ·ͱΊΔ • දΈ͍ͨͳײ͡ • ޙظͷߨٛͰׂͱॏཁͬΆ͍

  162. ؔ਺ͷ࿩

  163. None
  164. ؔ਺

  165. f(x) = x * x

  166. ͍͍ͩͨ͋ͬͯΔ

  167. int sq (int num) { return num * num; }

    int main(){ int a = 3; printf(“%d\n”, sq(a)); return 0; }
  168. Math: f(x) = x * x C: int sq (int

    num) { return num * num; }
  169. Math: f(x) = x * x C: int sq (int

    num) { return num * num; }
  170. Math: f(x) = x * x C: int sq (int

    num) { return num * num; }
  171. Math: f(x) = x * x C: int sq (int

    num) { return num * num; }
  172. Math: f(x) = x * x C: int sq (int

    num) { return num * num; }
  173. int sq (int num) { return num * num; }

    ܕ ؔ਺໊ ( ܕ Ҿ਺ ) { ॲཧ ؔ਺ͷܕʹ߹ͬͨฦΓ஋ }
  174. int sq (int num) { return num * num; }

    ܕ ؔ਺໊ ( ܕ Ҿ਺, ܕ Ҿ਺ ) { ॲཧ ؔ਺ͷܕʹ߹ͬͨฦΓ஋ }
  175. int sq (int num) { return num * num; }

    int main(){ int a = 3; printf(“%d\n”, sq(a)); return 0; }
  176. f(x) = x * x b = 3 f(b) =

    9
  177. Ҿ਺पΓͰ ೰Ή͜ͱ͸ͳ͍

  178. ฦΓ஋

  179. ϙΠϯλ

  180. None
  181. ϙΠϯλ͸”ࢀর”

  182. • ม਺ͷ”৔ॴ(ΞυϨε)”ΛೖΕΔม਺ • ม਺: ࣗൢػͷ্ͷϘλϯ • ϙΠϯλม਺: ࣗൢػͷԼͷϘλϯ ্ͷϘλϯͷ”৔ॴ”Λ֮͑Δ ͭ·Γɺ”த਎”Λ֮͑ΔͷͰ͸ͳ͍

    ϙΠϯλ
  183. // ϙΠϯλͷએݴ // “৔ॴ”ΛೖΕΔม਺ int *b_button;

  184. // ϙΠϯλͷએݴ // “৔ॴ”ΛೖΕΔม਺ int *b_button; b_button = &button; //

    &͸ม਺ͷ”৔ॴ”Λࣔ͢
  185. // ϙΠϯλͷએݴ // “৔ॴ”ΛೖΕΔม਺ int *b_button; b_button = &button; button

    = 1;
  186. // ϙΠϯλͷએݴ // “৔ॴ”ΛೖΕΔม਺ int *b_button; b_button = &button; button

    = 1; printf(“%d\n”, *b_button); // 1
  187. ϙΠϯλ͸”ࢀর”

  188. ϙΠϯλ͸ࢦఆ͞Εͨ ৔ॴͷม਺ͷ஋Λݟʹߦ͘

  189. void sq (int *num1, int *num2) { num1 = &a;

    num2 = &b; *num1 = *num1 * *num1; *num2 = *num2 * *num2; } int main(){ int a = 3, b = 6; sq(&a, &b); printf(“%d, %d\”, a, b);
  190. ·ͱΊ • ಉ͡ॲཧΛผͷ৔ॴͰߦ͍ͬͯΔͱ͖ ؔ਺ʹ·ͱΊΔͱݟ௨͕͠Α͘ͳΔ • ฦΓ஋͕ෳ਺ͩͬͨΓɺ഑ྻͷ৔߹͸ ϙΠϯλΛ࢖͏

  191. ·ͱΊ • ϙΠϯλ͸ࢀর