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

What is the output of the following program?

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$
asked in Programming by Boss (19.1k points)
edited by | 1.7k views

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. 

4 Answers

+31 votes
Best answer
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$.

answered by Veteran (384k points)
edited by
@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 ?
Subtraction - we have only add rt?
yes but if suppose it wuld have been subraction then ?
yes, then it shouldn't be same. This works because addition is commutative.
Yes :) Now its fine .
Sir nice copy paste from madeeasy gate book solution....plz explain it with diagram
Idiot. Mind your words. It is madeasy people who copied. Not me.
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?

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.

@meghna thank you
Well said sir
+1 vote
Ans: A
answered by Loyal (7.8k points)
+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

So the Answer is A

answered by Junior (669 points)
0 votes

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

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");
int F(){printf("\ninside f");
   printf("\nx= %d" , x);
here final output is 2 for F() + G() and 3 for  G() +  F().



answered by Active (3k points)

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
47,913 questions
52,294 answers
67,742 users