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 1 of 3.
Preethi said:
1 decade ago
Can anyone explain me this program?
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
Shreya said:
1 decade ago
Good explanatrion dude.
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.
Vaibhav said:
1 decade ago
This is't possible... the prototype is int... we can't pass a float!!!!... plz help me out here!!!!
Dharmvir Kumar said:
1 decade ago
Pls again explain me this program?
Ranga teja said:
1 decade ago
An excellent explanation by ankit. Thank you.
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.
Udaysiri said:
1 decade ago
fun(k=fun(fun(k))) if two times increment answer is 5
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers