edited by
9,431 views
35 votes
35 votes

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 by

2 Answers

Best answer
53 votes
53 votes
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
2 votes
2 votes

 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

So the Answer is A

Answer:

Related questions

29 votes
29 votes
3 answers
4
Ishrat Jahan asked Nov 1, 2014
9,629 views
Let $x$ be an integer which can take a value of $0$ or $1$. The statementif (x == 0) x = 1; else x = 0;is equivalent to which one of the following ?$x = 1 + x;$$x = 1 - ...