C Programming - Functions - Discussion
Discussion Forum : Functions - Find Output of Program (Q.No. 19)
19.
What will be the output of the program?
#include<stdio.h>
int fun(int i)
{
i++;
return i;
}
int main()
{
int fun(int);
int i=3;
fun(i=fun(fun(i)));
printf("%d\n", i);
return 0;
}
Answer: Option
Explanation:
Step 1: int fun(int); This is prototype of function fun(). It tells the compiler that the function fun() accept one integer parameter and returns an integer value.
Step 2: int i=3; The variable i is declared as an integer type and initialized to value 3.
Step 3: fun(i=fun(fun(i)));. The function fun(i) increements the value of i by 1(one) and return it.
Lets go step by step,
=> fun(i) becomes fun(3) is called and it returns 4.
=> i = fun(fun(i)) becomes i = fun(4) is called and it returns 5 and stored in variable i.(i=5)
=> fun(i=fun(fun(i))); becomes fun(5); is called and it return 6 and nowhere the return value is stored.
Step 4: printf("%d\n", i); It prints the value of variable i.(5)
Hence the output is '5'.
Discussion:
17 comments Page 1 of 2.
Swathi said:
9 years ago
#include<stdio.h>
int fun(int i)
{
i++;
return i;
}
int main()
{
int fun(int);
int i=3;
printf("%d\n", fun(i=fun(fun(i))));
printf("%d\n", i);
return 0;
}
In this, you will get the output near the first printf is 6,
and near second printf it is 5,
first 5 is assigned to i in main function so i will become 5.
After that, we are calling fun(5)
Here we are not collecting return value so in fun(5) i will become 6 but i is local to that function only so that variable life is end with that function life.
int fun(int i)
{
i++;
return i;
}
int main()
{
int fun(int);
int i=3;
printf("%d\n", fun(i=fun(fun(i))));
printf("%d\n", i);
return 0;
}
In this, you will get the output near the first printf is 6,
and near second printf it is 5,
first 5 is assigned to i in main function so i will become 5.
After that, we are calling fun(5)
Here we are not collecting return value so in fun(5) i will become 6 but i is local to that function only so that variable life is end with that function life.
(1)
Jetha lal said:
3 years ago
@Shashi.
First, the control will come on main and in the main check will call and directly it will return 34 and 34 will be get stored in c like c=34.
Now c will print so 34 will print and then the compiler go to the next line and find return 0 to the main (), return 0 to the main () is a message for a compiler that its work has been done.
So it will stop.
Hope you will get it.
First, the control will come on main and in the main check will call and directly it will return 34 and 34 will be get stored in c like c=34.
Now c will print so 34 will print and then the compiler go to the next line and find return 0 to the main (), return 0 to the main () is a message for a compiler that its work has been done.
So it will stop.
Hope you will get it.
Shashi said:
8 years ago
#include<stdio.h>
int check (int, int);
int main()
{
int check(int i,int j)
{
return 34;
}
float n=1;
int c;
c = check(10, 20);
printf("c=%d\n", c);
return 0;
}
int check(int i, int j)
{
int *p, *q;
p=&i;
q=&j;
return i>=45 ? *p:*q;
}
Please, explain why it gives output as 34?
int check (int, int);
int main()
{
int check(int i,int j)
{
return 34;
}
float n=1;
int c;
c = check(10, 20);
printf("c=%d\n", c);
return 0;
}
int check(int i, int j)
{
int *p, *q;
p=&i;
q=&j;
return i>=45 ? *p:*q;
}
Please, explain why it gives output as 34?
Abhinav said:
1 decade ago
Output will be 5 only because of fun (i=5), here 5 is stored in I so output is 5. If the question would have been like:
i = fun (i=fun(fun(i))); then the output will be 6.
i = fun (i=fun(fun(i))); then the output will be 6.
Srinivas said:
1 decade ago
fun(i = fun(fun(i)));
Becomes fun(5); is called and it return 6 and nowhere the return value is stored.
Why it is not returning the value 6 can one please tell me?
Becomes fun(5); is called and it return 6 and nowhere the return value is stored.
Why it is not returning the value 6 can one please tell me?
Fajal said:
1 decade ago
But when there is fun (i=5) the function again gets called and it returns 6. So according to my point of view the answer is 6. Can anyone explain how it is 5.
Ketan said:
1 decade ago
fun(i=fun(fun(i)))
i is 3.
So,
fun(i=fun(fun(3)))
i.e. fun(i=fun(4))
i.e. fun(i=5)
So here i is assigned 5.
So 5 will get printed.
i is 3.
So,
fun(i=fun(fun(3)))
i.e. fun(i=fun(4))
i.e. fun(i=5)
So here i is assigned 5.
So 5 will get printed.
(1)
Ishita said:
1 decade ago
As there is no such variable in which value 6 returned can be stored. So answer is 5. As we have to print value of i.
Chetan Kulkarni said:
10 years ago
Its error, when it calls fun (i=5). It needs a variable on left side. It should give L value required error.
Rav said:
1 decade ago
I don't agree with the reason of @Ketan. Its wrong in my opinion. Why is it not returning? Can you explain?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers