The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+16 votes
2.5k views

Consider the C program shown below:

#include<stdio.h>
#define print(x) printf("%d", x)

int x;
void Q(int z)
{
        z+=x;
        print(z);
}

void P(int *y)
{
        int x = *y + 2;
        Q(x);
        *y = x - 1;
        print(x);
}
main(void) {
        x = 5;
        P(&x);
        print(x);
}

The output of this program is:

  1. $12 \ 7 \ 6$
  2. $22 \ 12 \ 11$
  3. $14 \ 6 \ 6$
  4. $7 \ 6 \ 6$
asked in Programming by Veteran (59.5k points)
edited by | 2.5k views

4 Answers

+26 votes
Best answer
main: x = 5; //Global x becomes 5
P: int x = *y + 2; //local x in P becomes 5+2 = 7
Q: z+=x; //local z in Q becomes 7 + 5 = 12
Q: print(z); //prints 12
P:  *y  = x - 1; 
//content of address of local variable y 
(same as global variable x) becomes 7 - 1 = 6
P: print(x); //prints local variable x in P = 7
main: print(x); //prints the global variable x = 6
answered by Veteran (355k points)
edited by
+3
@Arjun sir how do we know dyanamic scoping here is not used here in that case answer will differ and it will be C
+7
C program has only static scoping.
+2
Here y is pointing to value at main() function or global variable too. So only change in y is reflected in main().
0
Here actual ans should be 12,6,6

Why not local x change with value of *y change?

Any reference?
+2
@srestha

Local X inside P only has scope till there, and you are printing that which has value $7$.

And, the Y inside P() is related to global X not local X .
0
oh thanks @Kapil
0
@Arjun sir Why the contents of local variable y becomes same as global variable x in last print .
+4 votes
The answer for this is A).. Can get the answer by a simple dry run..
answered by Junior (541 points)
0
@Arjun sir it means initializing the global varibles inside the main function or initializing it globally out of all functions both are same?
0
How can we initialize a global variable inside main? :O
0
As in the above program int x is initialized inside the main which is acting as the gloabl value of x?
+8
Nopes. Initialization means assigning initial value when memory is created. That happens before main starts execution. In C, if a value is not given, all global/static variables are initialized to 0. So, in the given question x is initialized to 0 and in main the value gets changed to 5.
0
thank you @Arjun sir you made me clear now. :)
+3 votes
void P(int *y)
{
        int x = *y + 2; // x=5+2=7
        Q(x); // Q(7)
        void Q(int z) // z=7 
           { 
               z+=x; // z=z+x=7+5=12 as here x is global and its value is 5 define in main() section print(z); // 12 is printed 
           } 
        *y = x - 1; // 7-1=6
        print(x);  // here value of x is 7 as it is local so 7 is printed   
}                     
 print(x); // here value of y is printed i.e 6 as it represents the final value of global x

so output is 12 7 6

                    
 
answered by Loyal (7.2k points)
0 votes

 Output is 12 7 6

answered by (451 points)
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

38,112 questions
45,619 answers
132,315 comments
49,297 users