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;
}
5.000000
3.000000
Garbage value
4.000000
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
21 comments Page 2 of 3.

Agam said:   1 decade ago
How can a float value is passed to fun which have argument as int data type? This should give an error of type mismatch.

Preethi said:   1 decade ago
Can anyone explain me this program?

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.

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.

Shreya said:   1 decade ago
Good explanatrion dude.


Post your comments here:

Your comments will be displayed after verification.