C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 8)
8.
What is the output of the program
#include<stdio.h>
int main()
{
    extern int fun(float);
    int a;
    a = fun(3.14);
    printf("%d\n", a);
    return 0;
}
int fun(int aa)
{
	return (int)++aa;
}
3
3.14
4
Compile Error
Answer: Option
Explanation:
2 Errors
1. Type mismatch in redeclaration of fun
2. Type mismatch in parameter aa
Discussion:
35 comments Page 3 of 4.

Nishanth said:   1 decade ago
@Neha.

After increment we get 4.14.

But when it return it will be type casted to int so 4 will get stored in a.

Neha said:   1 decade ago
I didn't understand that how 3.14 becomes 3?

And then we are adding 1 to it.

Becz according to me we should add 3.14+1=4.14.

But the correct answer is 4.

Please clear my doubt how 3.14 becomes 3 first?

Rekha said:   1 decade ago
extern int fun(float); this is the declaration.

int fun(int aa)
This is the function defined.

But both the prototype is not matching.
fun(float) fun(int) so error.

If fun(int) is replaced with fun(float)then 3.14 is assigned to aa and returns 3 after that ++a.

So 3+1=4.

Amlan Karmakar said:   1 decade ago
@Suman.

(int)++a means that float value of aa is type casted to int. It is called explicit conversion.

Suman said:   1 decade ago
Can anyone explain this statement: return (int)++aa in this program ?

Priyanka said:   1 decade ago
When extern int fun(float) is declared in main(), the compiler search for its defination in the program. But it found a function int fun(int aa) which differ from what is declared in main(),& it treated it as another function.
So compiler is unable to find the defination for int fun(float)
function & thus the error comes.

Hemanthkumar said:   1 decade ago
This word "mismatch" I am really fed up of this can any one explain?

Palak agrawal said:   1 decade ago
How is possible? and what is floatt?

Suresh said:   1 decade ago
This concept I cannot understand how is possible?

Sushma said:   1 decade ago
What is actually "1. type mismatch in redeclaration of function" in this question?


Post your comments here:

Your comments will be displayed after verification.