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

Using the Precision Specifier

Using the Precision Specifier

Aligning Output

Aligning Output
As you might have noticed in the previous section, all output is right-justified. In other words, by default, all output is placed on the right edge of the field, as long as the field width is longer than the width of the output.

 

Adding the Minimum Field Width

Adding the Minimum Field Width

Converting to Hex Numbers

Converting to Hex Numbers

Revisiting the printf() Function

Revisiting the printf() Function

The printf() function is the first C library function you used in this book to print out messages on the screen. printf() is a very important function in C, so it's worth it to spend more time on it.