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 1 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.

Kavi said:   1 decade ago
main()
{
int k=35,z;
k=fun(k=fun(k=fun(k)));
printf("k=%d",k);
}
fun(int k)
{
k++;
return(k);
}

What will be the output of the program? When it evaluates first time the fun will be k=fun (k=fun (k=36)); it will assign the value k=36 but after what will be the output?

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.

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.

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

Kukku said:   5 years ago
It is returning an integer value, but it prints integer value using floats format specifier (%f) , so it prints that value as float.

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.

Arpit said:   1 decade ago
Vaibhav is absolutely correct. How can float be passed to an integer value?

Arguments should have same data type.

Aashis said:   7 years ago
How is it possible?

If a float value and accepting it in an integer data type. It should show error.

Vaibhav said:   1 decade ago
This is't possible... the prototype is int... we can't pass a float!!!!... plz help me out here!!!!


Post your comments here:

Your comments will be displayed after verification.