Test Your Knowledge (Ch 7)

The following questions are based on what we have learned so far in Chapter 7 – Storage Classes in C. To test your understanding, try answering the following questions. The explanations are also provided to each question so you can always check the correctness of your answers. All the best 👍


Question 1

We should use register storage class for variables

which are frequently used in a program
which are least used in a program
whose values we want to persist even after a function finishes its execution
None of the above
explanation

The register storage class should be used for variables that are frequently used in a program because register memory is the fastest type of memory to access. Since the number of registers is limited, we should only store the values of frequently used variables in registers to speed up program execution.

Question 2

We should use static storage class for variables

which are used by almost all the functions in a program
which are frequently used in a program
whose values we want to persist even after a function finishes its execution
None of the above
explanation

The static storage should be used for variables whose values we want to persist even after a function (in which it is defined) finishes its execution. This is because static storage is allocated once at the start of the program and remains allocated until the program terminates. This ensures that the variable’s value is preserved even after the function in which it is defined has finished executing.

Question 3

We should use extern storage class for variables

which are frequently used in a program
that needs to be declared and not defined
whose values we want to persist even after a function finishes its execution
None of the above
explanation

The extern storage class should be used for variables that need to be declared but not defined in the current file.

Question 4

Determine the output of the following program:

#include <stdio.h>

int x;

void fun()
{
    x = 10;
    printf(“%d ", x);
    x = x + 1;
}

int main()
{
    printf("%d ", x);
    fun();
    printf("%d ", x);
    fun();
    printf("main's x is %d\n", x);
    return 0;
}
Garbage value as x is not initialized
0 10 11 11 12
0 10 11 10 11
Compilation error
explanation

The code prints the following output:

0 10 11 10 11

Here is a step-by-step explanation of the code:

1. The first line of code calls the printf() function to print the value of the variable x. Since x is not defined in the main() function, the compiler looks for its definition in the global scope. The variable x is defined as a global variable, so the compiler accesses its value. The default value of a global variable is zero, so the value of x is zero. The printf() function prints the value of x, which is 0.
2. The second line of code calls the fun() function. This means that the control temporarily transfers from the main() function to the fun() function.
3. Inside the fun() function, the variable x is assigned the value 10. This value is assigned to the global variable x because there is no definition of x inside the fun() function.
4. The next line of code calls the printf() function to print the value of x, which is now 10.
5. The next line of code increments the value of x by 1. This means that the value of x is now 11.
6. Since there are no more statements to execute inside the fun() function, the control transfers back to the caller, which is the main() function.
7. The next statement in the main() function is another call to the printf() function. This time, the function prints the value of x, which is now 11.
8. The next statement in the main() function is another call to the fun() function. This means that the control transfers to the fun() function again.
9. Inside the fun() function, the variable x is assigned the value 10 again. This means that the value of x is now 10.
10. The next line of code calls the printf() function to print the value of x, which is now 10.
11. The next line of code increments the value of x by 1. This means that the value of x is now 11.
12. Since there are no more statements to execute inside the fun() function, the control transfers back to the caller, which is the main() function.
13. The last statement in the main() function is another call to the printf() function. This time, the function prints the value of x, which is now 11.

Therefore, the final output of the code is 0 10 11 10 11.

Question 5

Determine the output of the following program:

#include <stdio.h>

void fun()
{
    auto int x = 10;
    static int y = 20;
    printf("%d %d ", x, y);
    x = x + 10;
    y = y + 10;
}

int main()
{
    fun();
    fun();
    return 0;
}
10 20 10 20
10 20 20 20
10 20 20 30
10 20 10 30
explanation

Let’s understand the code line by line:

1. In the main() function, the fun() function is called for the first time. Therefore, the control will transfer to the fun() function.
2. Inside the fun() function, two variables are defined: x with the initial value of 10, and y with the initial value of 20 (since y is a static variable, it retains its value even after the function execution).
3. The printf() function is called to display the values of x and y on the screen, which are currently 10 and 20 respectively. This means that at this point, the output is 10 and 20.
4. The values of x and y are then incremented by 10, so x becomes 20 and y becomes 30.
5. The function execution of fun() is completed, and the control returns to the caller, which is the main() function. At this point, the variable x is destroyed because it is an automatic variable and only exists within the function’s execution. However, y remains in the memory with its value of 30 since it is a static variable.
6. The next line in the main() function calls fun() again. This transfers the control back to the fun() function.
7. This time, only x is defined and initialized with a value of 10, while y retains its value of 30 from the previous execution.
8. The printf() function is called again to print the values of x and y, which are now 10 and 30 respectively.
9. Therefore, the overall output of the program is 10, 20, 10, 30.
10. Finally, since there are no more statements to execute in the main() function, the program terminates.

So, the final output of the program is: 10, 20, 10, 30.

Your score is out of



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.