跳转至

复习题

第三章 数据和C 复习题

  1. 指出下面各种数据使用的合适数据类型(有些可使用多种数据类型):

    a. East Simpleton的人口

    int short int unsigned short

    b. DVD 影碟的价格

    float double

    c. 本章出现次数最多的字母

    char

    d. 本章出现次数最多的字母次数

    int unsigned int

  2. 在什么情况下使用long类型的变量代替int类型的变量? > 超出了int能够表示的范围,如果要处理更大的值,需保持系统一致(如32位或64位),从而可以提高程序的可移植性。

  3. 使用哪些可移植性的数据类型可以获得32位有符号整数?选择的理由是什么? 常见的:int_32t(系统本身32位)、int_fast32_t(运算最快的32位)

  4. 指出下列常量的类型和含义(如果有的话):

    a. '\b' char类型

    b. 1066 int 类型

    c. 99.44 float类型

    d. 0XAA int 类型(十六进制格式)

    e. 2.0e30 long double类型

  5. Dottie Cawm编写了一个程序,请找出程序中的错误。

    1
    2
    3
    4
    5
    6
    7
    8
    #include <stdio.h>
    main
    (
        float g;h;
        float tax,rate;
        g = e21;
        tax = rate * g;
    )
    
    正确的代码如下:
    #include <stdio.h>
    int main(void)
    {
        float g,h;
        float tax,rate;
        /*上面可合并*/
        float g,h,tax,rate;
        g = 1e21; //e前面至少要有一个数字
        tax = rate * g;
    }
    

  6. 假设一个程序开始处有如下的声明:
    1
    2
    3
    4
    int imate = 2;
    long shot = 53456;
    char grade = 'A';
    float log = 2.71828;
    
    在下面printf()语句中添上合适的类型说明符:
    printf("The odds against the %___ were %___ to 1.\n", imate, shot);
    printf("A score of %___ is not an %___ grade.\n", log, grade);
    
    答案code如下:
    printf("The odds against the %d were %d to 1.\n", imate, shot);
    printf("A score of %f is not an %c grade.\n", log, grade);
    
  7. 假设ch为char类型变量。使用转义序列、十进制值、八进制字符常量以及十六进制字符常量等方法将其赋值为回车符(假设使用ASCII编码)。
    1
    2
    3
    4
    char ch = '\r';
    char ch = 77;
    char ch = '\0115';
    char ch = '\x4D';
    
  8. 改正下面程序(在C中/表示除法)。
    1
    2
    3
    4
    5
    6
    7
    8
    void main(int) / this progarm is perfect /
    {
       cows, legs integer;
       printf("How many cow legs did you count?\n);
       scanf("%c", legs);
       cows = legs / 4;
       printf("That implies there are %f cows.\n", cows)
    }
    
    修正后的Code如下:
    #include <stdio.h>
    int main(void) /*this progarm is perfect*/
    {
        int cows,legs;
        printf("How many cow legs did you count ? \n");
        scanf("%d",&legs);
        cows = legs/4;
        printf("That implies there are %d cows .\n",cows);
        return 0;
    }
    
  9. 指出下列转义字符的含义:
    1
    2
    3
    4
    a.\n /*换行符*/
    b.\\ /*反斜杠*/
    c.\" /*双引号*/
    d.\t /*指标符号Tab*/