C primer Plus Chap 02

C primer Plus Chap 02

C primer Plus 第二章代码与练习记录。

第二章 C 语言概述

  • xt3.c

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #include <stdio.h>
    int main(void)
    {
    int day,age;
    printf("please enter your age:\n");
    scanf("%d",&age);
    day=365*age;
    printf("your age is %d,about %d days.\n",age,day);
    return 0;
    }

    输出结果:

    1
    2
    3
    please enter your age:
    23
    your age is 23,about 8395 days.
  • xt4.c

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    #include <stdio.h>
    void print(void);
    int main(void)
    {
    print();
    print();
    print();
    printf("which nobody can deny!\n");
    return 0;
    }

    void print(void)
    {
    printf("for he's a jolly good fellow!\n");
    }

    输出结果:

    1
    2
    3
    4
    for he's a jolly good fellow!
    for he's a jolly good fellow!
    for he's a jolly good fellow!
    which nobody can deny!
  • xt4a.c

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #include <stdio.h>
    void print1(void);
    void print2(void);
    int main(void)
    {
    print1();
    print1();
    print1();
    print2();
    return 0;
    }

    void print1(void)
    {
    printf("for he's a jolly good fellow!\n");
    }

    void print2(void)
    {
    printf("which nobody can deny!\n");
    }

    输出结果:

    1
    2
    3
    4
    for he's a jolly good fellow!
    for he's a jolly good fellow!
    for he's a jolly good fellow!
    which nobody can deny!
  • xt5.c

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #include <stdio.h>
    int main(void)
    {
    int toes,squared,cubed;
    toes=10;
    squared=toes*toes;
    cubed=squared*toes;
    printf("toes=%d,squared=%d,cubed=%d\n",toes,squared,cubed);
    return 0;
    }

    输出结果:

    1
    toes=10,squared=100,cubed=1000
  • xt6.c

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #include <stdio.h>
    void smile(void);
    int main(void)
    {
    smile();smile();smile();
    printf("\n");
    smile();smile();
    printf("\n");
    smile();
    printf("\n");
    }

    void smile(void)
    {
    printf("smile!");
    }

    输出结果:

    1
    2
    3
    smile!smile!smile!
    smile!smile!
    smile!
  • xt7.c

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    /*C函数原型及其调用*/
    #include <stdio.h>
    void two(void);
    void one_three(void);
    int main(void)
    {
    printf("starting now:\n");
    one_three();
    printf("done!\n");
    return 0;
    }

    void one_three(void)
    {
    printf("one\n");
    two();
    printf("three\n");
    }
    void two(void)
    {
    printf("two\n");
    }

    输出结果:

    1
    2
    3
    4
    5
    starting now:
    one
    two
    three
    done!

C primer Plus Chap 02
https://ywmy.xyz/2022/05/07/C-primer-Plus-Chap-02/
作者
ian
发布于
2022年5月7日
许可协议