2.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 | 2.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$.

by Veteran
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 ?
+8
yes, then it shouldn't be same. This works because addition is commutative.
+1
Yes :) Now its fine .
–3
Sir nice copy paste from madeeasy gate book solution....plz explain it with diagram
+22
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
0

Can anyone explain why were the int funcf(int x) and int funcg(int y) are declared initially, couldn't we just write them when we are writting function below the main like this instead of declaring :

#include<stdio.h>
main ()
{
int x = 5, y = 10, count;
for (count = 1; count <= 2; ++count) {
y += funcf(x) + funcg(x);
printf ("%d", y);
}
}
int funcf (int x) {
int y;
y = funcg(x);
return (y);
}
int funcg (int x) {
static int y = 10;
y += 1;
return (y + x);
}
0
Let's assume those 2 lines are not declared initially.when the compiler first encounter the

$funcf()$ and $funcg()$ calls in the main functoins ,it  doesn't have any information about no of parameters it have,types of the parameters and return type (since compiler compiles form top to bottom).Hence,it may not able to map function call to the corresponding function definition resulting an error.

By declaring functions initially (called functions prototypes) compiler knows the information about them.
+1 vote
Ans: A
by Loyal
+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

by Junior
+1 vote

In first iteration:
In first case of funcf, which in turn calls 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.
In second iteration:
y will be incremented by 18+19=37 to give 43+37=80.

by Active
0