The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+22 votes
2k 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$
in Programming by Boss (16.3k points)
edited by | 2k 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. 

3 Answers

+34 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$.

by Veteran (420k points)
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 .
–2
Sir nice copy paste from madeeasy gate book solution....plz explain it with diagram
+18
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);
}
+1 vote
Ans: A
by Loyal (7.1k 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

by Junior (687 points)
Answer:

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
50,309 questions
55,743 answers
192,222 comments
90,490 users