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 2 of 4.
Suman said:
1 decade ago
Can anyone explain this statement: return (int)++aa in this program ?
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.
(int)++a means that float value of aa is type casted to int. It is called explicit conversion.
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.
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?
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.
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;
}
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;
}
Sam said:
1 decade ago
What is redeclaration of fun?
Sri said:
1 decade ago
Can any one explain this program? I can't understand.
Trisha said:
1 decade ago
What is that 2nd error? I didn't get that point. Can you please explain?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers