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

Rohan said:   9 years ago
extern int fun(float).

In this statement "extern" also there. So that's means the memory of this function allocated in some other program. So this is one of the error.
(1)

Sourav sachdeva said:   10 years ago
The first error seems obvious with mismatch of float and input in declaration and definition but can anyone cover the 2nd one in mismatch of parameters (is it referring to float and int), I think both are referring to same thing.

Anjaneyareddy said:   10 years ago
@Pooja.

Function declaration means like Declaring a variable.

Like when ever you are using Function, that must be matched to the Function Definition.

Syntax for Function Declaration is:

Type functionName (type [argname] [,type,.]);

Example:

Declare a function prototype for the add function, taking two integer.

//Arguments and returning their sum.

int add (int lhs, int rhs);

Pooja said:   10 years ago
Error: Previous declaration of 'fun' was here.

Can anybody explain me the meaning of the above error?

Abinaya said:   10 years ago
I too don't understand the 2nd error. Please explain me.

Trisha said:   1 decade ago
What is that 2nd error? I didn't get that point. Can you please explain?

Sri said:   1 decade ago
Can any one explain this program? I can't understand.

Sam said:   1 decade ago
What is redeclaration of fun?

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;
}

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


Post your comments here:

Your comments will be displayed after verification.