1.5k views

What is the output of the following program?

#include<stdio.h>
int funcf (int x);
int funcg (int y);
main ()
{
int x = 5, y = 10, count;
for (count = 1; count <= 2; ++count) {
y += funcf(x) + funcg(x);
printf ("%d", y);
}
}
funcf (int x) {
int y;
y = funcg(x);
return (y);
}
funcg (int x) {
static int y = 10;
y += 1;
return (y + x);
}
1. $43 \ 80$
2. $42 \ 74$
3. $33 \ 37$
4. $32 \ 32$
edited | 1.5k views
+1

Some random information.

If someone is thinking they are trying to test associativity of operator '+' then notice following line in @Arjun ji's answer.

even if the order of call is reversed, result will be same.

funcf(x) + funcg(x)

$funcf$ or $funcg$ can be executed first as whether the first operand or the second operand to '+' operator is first executed is not defined in C language and it depends on the compiler implementation. But here the order does not matter as both the operands are not affecting each other and '+' is commutative. Lets assume $funcf$ is executed first. It calls $funcg$ - so even if the order of call is reversed, result will be same.

In first call of $funcg$, $y$ becomes $11$ and it returns $5+11 = 16$.

In second call of $funcg$, $y$ becomes $12$ and it returns $5+12 = 17$.

So, in main $y$ is incremented by $16+17 = 33$ to become $10+33 = 43$. (Choice A)

In the second iteration $y$ will be incremented by $18+19 = 37$ to give $43+37 = 80$.

edited by
0
@arjun sir : You wrote here any one function can excute first . but what we have subraction then due to some dependecy result can differ ryt ? So what is order , which one is excuted first ?
0
Subtraction - we have only add rt?
0
yes but if suppose it wuld have been subraction then ?
+5
yes, then it shouldn't be same. This works because addition is commutative.
+1
Yes :) Now its fine .
–1
Sir nice copy paste from madeeasy gate book solution....plz explain it with diagram
+13
Idiot. Mind your words. It is madeasy people who copied. Not me.
0
sir, that means because of addition is commutative that's why  every time y increase while it perform y=y+1???.....that means if there have multiplication then it will perform same manner. and i have a question that if static int is not there in funcg()[only int y=10] will the increment would be same?
0

Nilabja Sarkar 12 if there would have been no static in funcg() then every time funcg() is called, y would be reinitialized to 10 and then incremented as per y=y+1.

And by addition is commutative, it means that in the statement  y+=funcf(x)+funcg(x) with some dependency, either of funcf() and funcg() can be executed first and answer would be same.but if in place of addition we had subtraction then result will be dependent upon which among both is executed first since - is not commutative and so then result  will be undefined. Multiplication is commutative too.

0
@meghna thank you
0
Well said sir
+1 vote
Ans: A
+1 vote

The count=1 and it goes till two,so following statement will be executed twice.

 y += funcf(x) + funcg(x);


1st call- funcg(x);   // y = 11        y+x=  16.

2nd call funcg(x); // y= 12         y+x=  17.

First iteration-> main()->y = 16+17 +10 = 43

Second iteration-> main() y= 18+19 +43 =80

In this question it really does not matter whether

 y += funcf(x) + funcg(x);

funcf(x)  execute first or funcg(x). So better not to take chance and go way in which compiler execute this statement .

i.e. by operator precedence of + operator which is left to right http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm

so in this way first y evaluated then funcf(x) and then  funcg(x).

Question in which precedence can matter is like one below

#include <stdio.h>
int x =1;
int G(){printf("\ninside G");
x=2;
}
int F(){printf("\ninside f");
x=3;
}
main()
{
;
printf("\nx= %d" , x);
}
here final output is 2 for F() + G() and 3 for  G() +  F().

1
2