The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+22 votes

What is printed by the following C program?

int f(int x, int *py, int **ppz)			
    int y, z;					                		
    **ppz += 1; z = **ppz;		 // corrected z = *ppz; to z = **ppz;                
    *py += 2; y = *py;					                	
    x += 3;					                    	
    return x+y+z;

void main()
    int c, *b, **a;
    c = 4; b = &c; a = &b;
    printf("%d", f(c, b, a));
  1. $18$
  2. $19$
  3. $21$
  4. $22$
asked in Programming by Veteran (59.8k points)
edited by | 3.7k views

Mistake is there.

that also has mistake rt?

5 Answers

+29 votes
Best answer

Return $x+y+z =$ return $7 + 7 + 5 =$ return $19$

So, option B $= 19$ is correct.

answered by Boss (30.9k points)
edited by
good explanation
on execution of "x+=3;" the value returned should be 10+7+5=22.
x being an integer.
X is  just a local variable of f, it is not same as C,( call by value) while calling the function f , c's value was passed to it, that's it.  Any changes to C's ( using its pointer variable)vàlue will not effect X 's value..
The main point to be careful above code is X=X+3, not X= *X+3
Can anyone explain the difference between f(c,b,a) & f(c,&b,&a).

Becuase in question it is pass by value i.e

 f(c,b,a). How the actual argument is changing with out passing the address of the actual argument.
x is passed by value

so x's value should be 4  and answer should be 16 right?
I also want to know.. did you get a reason?
+16 votes

Answer is B.

z = *ppz is a typo and it must be z = **ppz; 

**ppz+=1; \text{ modifies the value of c to 5. }\therefore \text{ z=5.}\\ **py+=2; \text{ modifies the value of c to 7. }\therefore \text{ y=7.}\\ \text{ But x will be called as x=4, }\therefore \text{ x=7.}\\ \text{Answer: 7+7+5=19.}

answered by Active (4.2k points)
it will be *py ...
+1 vote
Answer is B.
answered by Boss (19.9k points)
+1 vote

Answer is B

answered by Junior (763 points)
Its really appriciatable
0 votes
z = *ppz Shouldn't be z = **ppz ??
answered by Veteran (59.7k points)

Yes. But you can't complain so in GATE. GATE takers are expected to apply logic to correct the questions since GATE 2013. Never say "mistake" in question paper- they are made by experts.

So Sir. we are supposed to correct the question paper and apply the logic ?
They happen rarely and this one is most stupid mistake. Everyone can point out it.
yes. And this year they have answer scrutiny. So, if you find any question wrong do not bother to spend time on it- but question wrong is extremely rare so do not count too much on this.
if there is any mistake in the question marks are awarded to everyone right

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
49,408 questions
53,589 answers
70,870 users