C Programming - Functions - Discussion
Discussion Forum : Functions - Point Out Errors (Q.No. 3)
3.
Point out the error in the program
#include<stdio.h>
int main()
{
int a=10;
void f();
a = f();
printf("%d\n", a);
return 0;
}
void f()
{
printf("Hi");
}
Answer: Option
Explanation:
The function void f() is not visible to the compiler while going through main() function. So we have to declare this prototype void f(); before to main() function. This kind of error will not occur in modern compilers.
Discussion:
14 comments Page 1 of 2.
Gauravj282 said:
1 decade ago
The variable 'a' is int type and after a=f() call f() which return hi to print but a is int type.
This reason is correct or not ? explain.
This reason is correct or not ? explain.
Vijay said:
1 decade ago
Wrong because the function f () is not returning anything?
Sumit said:
1 decade ago
f() is a void type function which does not return value so assignment is not allowed.
Rajesh.T.K. said:
1 decade ago
Can anyone tell me about the function prototype?
Whether it should be declared only outside the main()?
Whether it should be declared only outside the main()?
Suman said:
1 decade ago
@Rajesh.
It should be before your calling function.
It should be before your calling function.
Amrita chaurasia said:
1 decade ago
A function declared before main() or after main() is not going to affect the definition of function. Hence how the compiler didn't recognize void f() ; when compiler start tracing the main() , at void f() ;.
It will come to void f() function definition.
According to me the error can be, f() function returns string and assigned to a. Whereas a is integer type. Here I think error would occur. Still I'm doubtful. So, would request the clarification on it.
It will come to void f() function definition.
According to me the error can be, f() function returns string and assigned to a. Whereas a is integer type. Here I think error would occur. Still I'm doubtful. So, would request the clarification on it.
Mrunali said:
1 decade ago
I also think the error is with there being no return variable but still a=f(); is given.
Arjun Verma said:
1 decade ago
I'm totally agree with @Amrita that compiler will go to the function call i.e void f(); in main, so how can u say that we have to define it before main?
SATHISH KUMAR said:
8 years ago
Even if it is declared above main, I am still getting the following error:
main.c: In function \'main\':
main.c:7:7: error: void value not ignored as it ought to be
a = f();
^
Can anyone help me to clear this?
main.c: In function \'main\':
main.c:7:7: error: void value not ignored as it ought to be
a = f();
^
Can anyone help me to clear this?
Mayank said:
8 years ago
If declaration of the function is done before the main function then output is :Hi 2
Even our function doesn't return anything but still we get the output without any error.
Can some give the explanation how can a=f();?
Doesn't lead to error if a function is declared before main.Rest of the code is same.
Even our function doesn't return anything but still we get the output without any error.
Can some give the explanation how can a=f();?
Doesn't lead to error if a function is declared before main.Rest of the code is same.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers