• edited by
15,855 views
60 60 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$

3 Answers

Best answer
71 71 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 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:
Position:
Show:

Related questions

51 51 votes
3 answers 3 answers
16.0k
16.0k views
Ishrat Jahan asked Nov 2, 2014
16,019 views
Consider the following C program:#include <stdio.h typedef struct { char *a; char *b; } t; void f1 (t s); void f2 (t *p); main() { static t s = {"A", "B"}; printf ("%s %s...
35 35 votes
3 answers 3 answers
11.9k
11.9k views
Ishrat Jahan asked Nov 2, 2014
11,866 views
Choose the correct option to fill the $?1$ and $?2$ so that the program prints an input string in reverse order. Assume that the input string is terminated by a new line ...
57 57 votes
6 answers 6 answers
19.4k
19.4k views
Ishrat Jahan asked Nov 2, 2014
19,373 views
Consider the following C program which is supposed to compute the transpose of a given $4 \times 4$ matrix $M$. Note that, there is an $X$ in the program which indicates ...
50 50 votes
5 answers 5 answers
17.7k
17.7k views
Ishrat Jahan asked Nov 1, 2014
17,698 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 - ...