Upgrade to Pro — share decks privately, control downloads, hide ads and more …

C Lecture 2013.09.19

masawada
September 19, 2013

C Lecture 2013.09.19

masawada

September 19, 2013
Tweet

More Decks by masawada

Other Decks in Technology

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  7. ߨशͷର৅

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  10. ͍ͬͯ͏ਓ޲͚

    View full-size slide

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

    View full-size slide

  12. ຊ೔ͷ໨త

    View full-size slide

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

    View full-size slide

  14. ຊ೔ͷ͓͠ͳ͕͖
    • ϓϩάϥϛϯάͱ͸
    • Cͷجૅ
    • ม਺
    • ੍ޚߏจ
    • ഑ྻ/ߏ଄ମ
    • ϙΠϯλ
    • ؔ਺
    • ԋश

    View full-size slide

  15. ϓϩάϥϛϯάͱ͸

    View full-size slide

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

    View full-size slide

  17. ͻͱͱ͓Γͷཧղ

    View full-size slide

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

    View full-size slide

  19. + ݴޠಛ༗ͷػೳ

    View full-size slide

  20. •ϙΠϯλ
    •ߏ଄ମ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  25. ܭࢉػͷ࢓૊ΈΛ
    ཧղ͢Δ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  28. Cݴޠͷجૅ

    View full-size slide

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

    View full-size slide

  30. $ emacs hello.c
    ---
    #include
    int main() {
    printf(“Hello, world!\n”);
    return 0;
    }

    View full-size slide

  31. $ gcc hello.c -o hello
    $ ./hello
    Hello, world!

    View full-size slide

  32. #include
    int main() {
    printf(“Hello, world!\n”);
    return 0;
    }

    View full-size slide

  33. ࣮͸ͳͯ͘΋ྑ͍

    View full-size slide

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

    View full-size slide

  35. ඪ४ೖग़ྗ

    View full-size slide

  36. •printf
    •scanf

    View full-size slide

  37. #include
    int main() {
    printf(“Hello, world!\n”);
    return 0;
    }

    View full-size slide

  38. #include
    int main() {
    printf(“Hello, world!\n”);
    return 0;
    }

    View full-size slide

  39. #include
    int main() {
    printf(“Hello, world!\n”);
    return 0;
    }

    View full-size slide

  40. $ gcc hello.c -o hello
    ɹ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  45. $ ./hello
    Hello, World

    View full-size slide

  46. Ҏ্ɺCͷجૅ

    View full-size slide

  47. ͻͱͱ͓Γͷཧղ

    View full-size slide

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

    View full-size slide

  49. + ݴޠಛ༗ͷػೳ

    View full-size slide

  50. •ϙΠϯλ
    •ߏ଄ମ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  53. x = 10
    y = 20

    View full-size slide

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

    View full-size slide

  55. 0100101001010100...

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  58. ੍ޚߏจͷ࿩

    View full-size slide

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

    View full-size slide

  60. ॱ൪Λม͍͑ͨʂ

    View full-size slide

  61. ൺֱԋࢉࢠ

    View full-size slide

  62. <ͱ͔>ͱ͔==ͱ͔

    View full-size slide

  63. a b a < b
    0 0 0
    0 1 1
    1 0 0
    1 1 0

    View full-size slide

  64. 0ͳΒِ
    0Ҏ֎ͳΒਅ

    View full-size slide

  65. •if - else - ৚݅Ͱ෼ذ
    •while - ಉ͡ॲཧΛ·ͱΊΔ
    •for - whileͷ্Ґ൛

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  72. 10 9 8 7 6 5 4 3 2 1

    View full-size slide

  73. int i = 10; // i: 10
    int tmp = i - 1; // i: 10
    i = tmp; // i: 9

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  76. whileͷ্Ґ൛

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  110. ·ͱΊ
    • ॲཧͷॱ൪Λม͑Δͷʹ੍ޚจΛ࢖͏
    • ৚݅Ͱ෼ذ͢Δ
    • ifͱ͔whileͱ͔͍Ζ͍Ζ͋Δ
    • for͸whileͷ্Ґ൛Έ͍ͨͳײ͡

    View full-size slide

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

    View full-size slide

  112. ม਺·ͱΊͨ΍ͭ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  115. int a0 = 3;
    ...
    int a6 = 22;
    int a7 = 24;
    int a8 = 27;

    View full-size slide

  116. ໊લ෇͚Δͷ͕
    ͱͯ΋ΊΜͲ͍͘͞

    View full-size slide

  117. int a[100];
    a[0] = 3;
    ...
    a[6] = 22;
    a[7] = 24;
    a[8] = 27;

    View full-size slide

  118. ศརʂʂʂ

    View full-size slide

  119. int a[100];
    ܕ ഑ྻ໊[ ֬อ͢Δ਺ ];
    a[0] = 3;
    ഑ྻ໊[ ఴࣈ ] = ஋;

    View full-size slide

  120. จࣈྻ char[]

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  126. ߏ଄ମͷ࿩

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  129. ໊લ ೥ྸ ਎௕ ମॏ
    Ұ࿠ 19 165 59
    ೋ࿠ 20 172 64
    ࡾ࿠ 21 183 72
    ࢛࿠ 16 159 53

    View full-size slide

  130. struct brother {
    int age;
    double height;
    double weight;
    }; // ηϛίϩϯΘ͢Εͣʹ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  134. ͍͍ͪͪstructॻ͘ͷ
    ΊΜͲ͏

    View full-size slide

  135. ܕͷΑ͏ʹѻ͍͍ͨ

    View full-size slide

  136. struct brother {
    char name[20];
    int age;
    double height;
    double weight;
    };

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  139. f(x) = x * x

    View full-size slide

  140. ͍͍ͩͨ͋ͬͯΔ

    View full-size slide

  141. int sq (int num) {
    return num * num;
    }
    int main(){
    int a = 3;
    printf(“%d\n”, sq(a));
    return 0;
    }

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  149. int sq (int num) {
    return num * num;
    }
    int main(){
    int a = 3;
    printf(“%d\n”, sq(a));
    return 0;
    }

    View full-size slide

  150. f(x) = x * x
    b = 3
    f(b) = 9

    View full-size slide

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

    View full-size slide

  152. ϙΠϯλ͸”ࢀর”

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  157. // ϙΠϯλͷએݴ
    // “৔ॴ”ΛೖΕΔม਺
    int *b_button;
    b_button = &button;
    button = 1;
    printf(“%d\n”, *b_button); // 1

    View full-size slide

  158. ϙΠϯλ͸”ࢀর”

    View full-size slide

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

    View full-size slide

  160. 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);

    View full-size slide

  161. ·ͱΊ
    • ಉ͡ॲཧΛผͷ৔ॴͰߦ͍ͬͯΔͱ͖
    ؔ਺ʹ·ͱΊΔͱݟ௨͕͠Α͘ͳΔ
    • ฦΓ஋͕ෳ਺ͩͬͨΓɺ഑ྻͷ৔߹͸
    ϙΠϯλΛ࢖͏

    View full-size slide

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

    View full-size slide