Look at the C code given below and answer the questions that follow: int a = 1;
ID: 3668400 • Letter: L
Question
Look at the C code given below and answer the questions that follow:
int a = 1;
int b = 2;
int c = 3;
void funB()
{
c = 6;
printf(“In funB, a = %d, b = %d, c = %d”, a, b, c);
}
void funA()
{
int b = 4;
printf(“In funA, a = %d, b = %d, c = %d”, a, b, c);
funB();
}
void main()
{
int a = 2;
printf(“In main, a = %d, b = %d, c = %d”, a, b, c);
funA();
printf(“In main, a = %d, b = %d, c = %d”, a, b, c);
}
a) Which lines declare global variables? Which lines declare local variables?
b) If C follows static allocation, what is the output of the complete program?
c) If C followed dynamic allocation, what would be the output of the complete
program?
Explanation / Answer
int a = 1; //Global Variables.
int b = 2; //Global Variables.
int c = 3; //Global Variables.
void funB()
{
c = 6;
printf(“In funB, a = %d, b = %d, c = %d”, a, b, c);
}
void funA()
{
int b = 4;
printf(“In funA, a = %d, b = %d, c = %d”, a, b, c);
funB();
}
void main()
{
int a = 2;
printf(“In main, a = %d, b = %d, c = %d”, a, b, c);
funA();
printf(“In main, a = %d, b = %d, c = %d”, a, b, c);
}
a. The first 3 lines of the code are global variables.
b. The output of the code is:
a = 2, b = 2, c = 3 //This is in main().
a = 1, b = 4, c = 3 //This is in funA().
a = 1, b = 2, c = 6 //This is in funB().
a = 2, b = 2, c = 6 //This is in main() again.
c.
This also produces the same value as it did in b.
Therefore, The output of the code is:
a = 2, b = 2, c = 3 //This is in main().
a = 1, b = 4, c = 3 //This is in funA().
a = 1, b = 2, c = 6 //This is in funB().
a = 2, b = 2, c = 6 //This is in main() again.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.