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 goto Statement

The goto Statement

The continue Statement

The continue Statement

The break Statement

The break Statement

You can add a break statement at the end of the statement list following every case label, if you want to exit the switch construct after the statements within a selected case are executed.

The switch Statement

The switch Statement

Nested if Statements

Nested if Statements

As you saw in the previous sections, one if statement enables a program to make one decision. In many cases, a program has to make a series of decisions. To enable it to do so, you can use nested if statements.