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 while Loop

The while Loop

The while statement is also used for looping. Unlike the situation with the for statement, there is only one expression field in the while statement.

The general form of the while statement is

while (expression) {

Question and Answer

Question and Answer

    Q How does a for loop work?

Exercises : Answer the following Question

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

 

Working with the Cast Operator

Playing with the Cast Operator

In C, you can convert one data type to a different one by prefixing the cast operator to the operand.

The general form of the cast operator is

(data-type)x

Question and Answer

Question and Answer

    Q What is the difference between the pre-increment operator and the post-increment operator?