C Programming - Functions - Discussion
Discussion Forum : Functions - Find Output of Program (Q.No. 20)
20.
What will be the output of the program?
#include<stdio.h>
int fun(int);
int main()
{
float k=3;
fun(k=fun(fun(k)));
printf("%f\n", k);
return 0;
}
int fun(int i)
{
i++;
return i;
}
Discussion:
21 comments Page 2 of 3.
Onkar said:
1 decade ago
The answer should be garbage value. When 3 is passed to fun (int i) by value, a variable I gets created at location say 100. At this 100th location the value of I gets incremented to 4. When the return statement is called its returning a value at the 100th location which 'i' doesn't occupy anymore as it was a local variable.
Praveen said:
1 decade ago
Float can be passed though it is declared as an int, due to the implicit conversion float is converted to int and the compiler creates an warning (may loss some data) for that not the error. It acts as an int in the called function and as float in main function.
Udaysiri said:
1 decade ago
fun(k=fun(fun(k))) if two times increment answer is 5
Arpit said:
1 decade ago
Vaibhav is absolutely correct. How can float be passed to an integer value?
Arguments should have same data type.
Arguments should have same data type.
Ranga teja said:
1 decade ago
An excellent explanation by ankit. Thank you.
Dharmvir Kumar said:
1 decade ago
Pls again explain me this program?
Vaibhav said:
1 decade ago
This is't possible... the prototype is int... we can't pass a float!!!!... plz help me out here!!!!
Rajat jain said:
1 decade ago
Here we want to find answer fun(k=fun(fun(k)))
1.firstly evaluate fun(k),which gives the value 4.
2.Now we have fun(k=fun(4)).
3.The value of fun(4)=5.
4.Now we have fun(k=5)
5.fun(5) return the value which is equal to 6.
6.But here we print the value of k which equals to 5.
1.firstly evaluate fun(k),which gives the value 4.
2.Now we have fun(k=fun(4)).
3.The value of fun(4)=5.
4.Now we have fun(k=5)
5.fun(5) return the value which is equal to 6.
6.But here we print the value of k which equals to 5.
Shreya said:
1 decade ago
Good explanatrion dude.
Ankit said:
1 decade ago
fun(k=fun(fun(k)));
fun(k=fun(fun(3))); here fun(3)returns 4
fun(k=fun(4)); //here fun(4) returns 5
fun(k=5); //here 5 is assigned to k
& hence since it is float value it prints 5.000000
if statement was like this k=(fun(fun(fun(k))); k=6.000000
fun(k=fun(fun(3))); here fun(3)returns 4
fun(k=fun(4)); //here fun(4) returns 5
fun(k=5); //here 5 is assigned to k
& hence since it is float value it prints 5.000000
if statement was like this k=(fun(fun(fun(k))); k=6.000000
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers