1. 程式人生 > >Chapter2 Introducing C-Review Questions

Chapter2 Introducing C-Review Questions

1.What are the basic module of a C program called?

They are called functions.

2.What is a syntax error? Give an example of one in English and one in C.

A syntax error is a violation of the rules governing how sentences or programs are put together. Here's an example in English:"Me speak English good." Here's an example in C.

printf"Where are the parentheses?";

3.What is a semantic error? Give an example of one in English and one in C.

A semantic error is one of meaning. Here's an example in English : " This sentence is excellent Czech."  Here's a C example:

thrice_n = 3 + n;

4.Indiana Sloth has prepared the following program and brought it to you for approval. Please help him out. 

Line 1: Begin with a #; spell the file stdio.h; place the filename within angle brackets.

Line 2: Use() , not {}; end the comment with */, not /*.

Line 3: Use {, not (.

Line 4: Complete the statement with a semicolon.

Line 5: Indiana got this one (the blank line) right!

Line 6: Use =, not := for assignment.(Apparently, Indiana knows a little Pascal.) User 52, not 56, weeks per year.

Line 7: Should be

printf("There are %d weeks in a year.\n", s);

Line 8: There isn't a line 9, but there should be, and it should consist of the closing brace, }.

5.  Assuming that each of the following examples is part of a complete program, what will each one print? 

     a. printf("Baa Baa Black Sheep.");

         printf("Have you any wool?\n");  

     b. printf("Begone!\nO creature of lard!\n");  

     c. printf("What?\nNo/nfish?\n");  

     d. int num;      

        num = 2;    

        printf("%d + %d = %d", num, num, num + num);     

a: Baa Baa Black Sheep.Have you any wool? (Note that there's no space after the period. You could have had a space by using " have instead of "have.)

b: Begone!

    O creature of lard! (Note that the cursor is left at the end of the second line.)

c: What?

    No/nfish? (Note that the slash[/] does not have the same effect as the backslash [\]; it simply prints as a slash.)

6.    Which of the following are C keywords?  main ,  int ,  function ,  char ,  =    

int and char. 

7.    How would you print the values of the variables  words  and  lines  so they appear in the following form: 
     

       There were 3020 words and 350 lines.   
 
       Here,  3020  and  350  represent the values of the two variables. 

printf("There were %d words and %d lines.\n", words, lines);

8.    Consider the following program: 

#include <stdio.h>

int main(void) {
    int a, b;

    a = 5;
    b = 2;    /* line 7 */
    b = a;    /* line 8 */
    a = b;    /* line 9 */
    printf("%d %d\n", b, a);
    return 0;
}

 What is the program state after line 7? Line 8? Line 9? 

After line 7, a is 5 and b is 2. After line 8, both a and b are 5. After line 9, both a and b are still 5. (Note that a cant be 2 because by the time you say a = b; b has already been changed to 5.)

 9.    Consider the following program: 

#include <stdio.h>

int main(void) {
    int x, y;

    x = 10;
    y = 5;          /* line 7 */
    y = x + y;      /* line 8 */
    x = x * y;      /* line 9 */
    printf("%d %d\n", x, y);
    return 0;
}

What is the program state after line 7? Line 8? Line 9? 

After line 7, x is 10 and y is 5. After line 8, x is 10 and y is 15. After line 9, x is 150 and y is 15.