Exercises : Answer the following Question

Submitted by Anonymous (not verified) on Sun, 03/10/2013 - 01:12

To help solidify your understanding of this lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix E, "Answers to Quiz Questions and Exercises."
Quiz

    Given the following code portion, which variables are global variables, and which ones are local variables with block scope?

    int x = 0;
    float y = 0.0;
    int myFunction()
    {
       int i, j;
       float y;
       . . .
       {
          int x, y;
          . . .
       }
       . . .
    }

    When two variables with the same name are defined, how does the compiler know which one to use?
    Identify the storage class of each declaration in the following code portion:

    int i = 0;
    static int x;
    extern float y;
    int myFunction()
    {
       int i, j;
       extern float z;
       register long s;
       static int index;
       const char str[] = "Warning message.";
       . . .
    }

    Given the following declaration:

    const char ch_str[] = "The const specifier";

    is the ch_str[9] = `-'; statement legal?

Exercises

    Given the following,
        An int variable with block scope and temporary storage
        A constant character variable with block scope
        A float local variable with permanent storage
        A register int variable
        A char pointer initialized with a null character

    write declarations for all of them.
    Rewrite the program in Listing 14.2. This time, pass the int variable x and the float variable y as arguments to the function_1() function. What do you get on your screen after running the program?
    Compile and run the following program. What do you get on the screen, and why?

    #include <stdio.h>
    int main()
    {
       int i;

       for (i=0; i<5; i++){
          int x = 0;
          static int y = 0;
          printf("x=%d, y=%d\n", x++, y++);
       }
       return 0;
    }

   

Related Items

The #define and #undef Directives

The #define and #undef Directives

The #define directive is the most common preprocessor directive, which tells the preprocessor to replace every occurrence of a particular character string (that is, a macro name) with a specified value (that is, a macro body).

The C Preprocessor Versus the Compiler

The C Preprocessor Versus the Compiler

One important thing you need to remember is that the C preprocessor is not part of the C compiler.

What Is the C Preprocessor?

If there is a constant appearing in several places in your program, it's a good idea to associate a symbolic name to the constant, and then use the symbolic name to replace the constant throughout the program. There are two advantages to doing so. First, your program will be more readable.

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Question and Answer

    Q Why is random access to a disk file necessary?