跳转至

复习题

第二章 C语言概述 复习题

1.C语言的基本模块是什么?

基本模块和用户自定义模块的都称为函数。

2.什么是语法错误和语义错误? - 语法错误:违反组成语句或程序的语法规则。例:printf "hello world";

  • 语义错误:含义错误。例:n3 = n2*n

  • Indiana Sloth编写了下面的程序,并征求你的意见,请帮助他评定。

    1
    2
    3
    4
    5
    6
    7
    8
    #include stdio.h
    
    int main(void)
    (
        int s
        s:=56;
        printf(There are s weeks in a year.);
        return 0;
    
    程序错误是无法正常运行的。正确的修改如下:
    1
    2
    3
    4
    5
    6
    7
    8
    #include <stdio.h>
    int main(void)
    {
        int s;
        s = 56; // 变量赋值方式不正确
        printf("There are %d weeks int a year.",s);
        return 0;
    }
    

  • 假设下面的4个例子都是完整程序中的一部分,他们都输出什么结果?

    1
    2
    3
    4
    5
    6
    7
    a. printf("Baa Baa Black Sheep."); /*输出结果为:Baa Baa Black Sheep.Have you any wool?*/
       printf("Have you any wool?\n");
    b. printf("Begone!\nO creature of lard!\n"); /*输出结果为:Begone!此处换行 O creature of lard!*/
    c. printf("What?\nNo/nfish?\n"); /*输出结果为:What? 此处换行 No/nfish?*/
    d. int num;
       num = 2;
       printf("%d + %d = %d",num,num,num+num); /*输出结果为:2 + 2 = 4*/
    

  • main、int、function、char、=中,哪些是C语言的关键字?

关键字为:main、int char。

  1. 如何以下面的格式输出变量words和lines的值(例,3020和350代表两个变量的值)? There were 3020 words and 350 lines

    #include <stdio.h>
    
    int main(void)
    {
        int words,lines;
        words = 3020;
        lines = 350;
    
        printf("There were %d words and %d lines\n",words,lines);
    
        return 0;
    }
    

  2. 考虑下面的程序

    #include <stdio.h>
    int main(void)
    {
        int a,b;
        a = 5;
        b = 2; /*第 7 行*/
        b = a; /*第 8 行*/
        a = b; /*第 9 行*/
        printf("%d %d \n",b,a);
        return 0;
    }
    
    请问,在执行完第7、第8、第9行后,程序的状态分别是什么?

  3. 执行完第7行,a = 5,b = 2;
  4. 执行完第8行,a = 5,b = 5
  5. 执行完第9行,a = 5,b = 5

  6. 考虑下面的程序

    #include <stdio.h>
    
    int main(void)
    {
        int x,y;
        x = 10;
        y = 5; /*第 7 行*/
        y = x + y; /*第 8 行*/
        x = x * y; /*第 9 行*/
        printf("%d %d \n",x,y);
        return 0;
    }
    
    请问,在执行完第7、第8、第9行后,程序的状态分别是什么?

  7. 执行第7行后,x = 10,y = 5
  8. 执行第8行后,x = 10,y = 15
  9. 执行第9行后,x = 150,y = 15