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;
}
Answer: Option
Explanation:
2 Errors
1. Type mismatch in redeclaration of fun
2. Type mismatch in parameter aa
1. Type mismatch in redeclaration of fun
2. Type mismatch in parameter aa
Discussion:
35 comments Page 1 of 4.
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;
}
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;
}
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.
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.
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);
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);
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.
So compiler is unable to find the defination for int fun(float)
function & thus the error comes.
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?
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.
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.
Sundar said:
1 decade ago
In Turbo C it shows the following error:
Error PROGRAM.C 11: Type mismatch in redeclaration of 'fun'
In GCC (Linux) it shows the following errors:
Line 11: error: conflicting types for 'fun'
Line 4: error: previous declaration of 'fun' was here
Error PROGRAM.C 11: Type mismatch in redeclaration of 'fun'
In GCC (Linux) it shows the following errors:
Line 11: error: conflicting types for 'fun'
Line 4: error: previous declaration of 'fun' was here
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.
Udhaya said:
8 years ago
#include<stdio.h>
int main()
{
extern int fun(float);
float a;
a = fun(3.14);
printf("%d\n", a);
return 0;
}
int fun(float a)
{
return (int)++a;
}
This give output '0'.
int main()
{
extern int fun(float);
float a;
a = fun(3.14);
printf("%d\n", a);
return 0;
}
int fun(float a)
{
return (int)++a;
}
This give output '0'.
(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?
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers