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 4 of 4.

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.

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?

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.

Naveen said:   1 decade ago
There is no need of explicit typecasting because the return type is int.

Ravi said:   1 decade ago
#include<stdio.h>
int main()
{
extern int fun(float);
/*here we inform to the compiler that we are sending a float */argument
int a;
a = fun(3.14);
/*here we are successfully sent the float value as argument
*/
printf("%d\n", a);
return 0;
}
int fun(int aa)
/*but here it is defined as, it will receive integer argument
we are violating the declaration so it is shows that errors */
{
return (int)++aa;
}


Post your comments here:

Your comments will be displayed after verification.