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.

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

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.

Panu said:   9 years ago
How two functions are mismatch?

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

Parth said:   8 years ago
Had the data type of the argument in function definition of function 'fun' been correct, I guess the program still should not run because the function 'fun' has been declared inside the main function. So, technically it's scope is inside only the main function. So why, would the function be called? Can someone clear my doubt?

Chandana said:   7 years ago
Output should be linker error isn't it? Because function declared as extern is taken care of linker not by the compiler.

Ravi said:   3 years ago
@Lakshmi.

Yes, you are correct it can be ++a;.

Chinnu said:   2 months ago
@All.

Though the extern statement was called in the program, as Rohan said, the memory of the function is assigned in some other program, but still we are not using any scope of it in the current program, which would not create an error.

The error is due to in function definition argument passed was an int, but it should be a float and the function was defined inside the main function; it should be defined out of it.

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.

Priyanka Gupta said:   1 decade ago
In this where type mismatch in redeclaration of function fun.


Post your comments here:

Your comments will be displayed after verification.