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 2 of 2.
Mohammed Hessen said:
8 years ago
Delete the variable i in this line;
fun(i=fun(fun(i)));
TO BE
fun(fun(fun(i)));
and it will return 3.
fun(i=fun(fun(i)));
TO BE
fun(fun(fun(i)));
and it will return 3.
OMKAR GURAV said:
10 years ago
I agree with @Chetan and @Sahil. Why the L value required error won't be there?
Deependra said:
9 years ago
The value of local variable i, can be change in function parameter? How?
Niharika said:
9 years ago
It is post increment right so the answer will be 3.
Rajesh said:
7 years ago
How to say we are not saving value of i?
Sahil Ahmed said:
1 decade ago
Won't it give an Lvalue required error?
Ranjeet said:
8 years ago
The right answer is 6.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers