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.
Dawood Ibrahim Bhat said:
7 years ago
Dear friends, true explanation for this option in my view is that:
integer (a) is assigned equal to a void function, since void returns nothing, therefore in the program above we are equating an integer to nothing, it is wrong, we can never equate an integer equal to void. Hence this kind of assignment is not allowed.
And declaring a function before defining it is important ....but in C we can declare functions in main function, use int instead of void for f().and also add a return statement to the block of f() function.
Thanks.
integer (a) is assigned equal to a void function, since void returns nothing, therefore in the program above we are equating an integer to nothing, it is wrong, we can never equate an integer equal to void. Hence this kind of assignment is not allowed.
And declaring a function before defining it is important ....but in C we can declare functions in main function, use int instead of void for f().and also add a return statement to the block of f() function.
Thanks.
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.
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.
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?
Nitesh said:
5 years ago
@Mayank.
In modern Compilers, you never get this error. And in your case, You get 2 means In printf statement you have 2 characters 1=H and 2=i so it returns 2 .
In modern Compilers, you never get this error. And in your case, You get 2 means In printf statement you have 2 characters 1=H and 2=i so it returns 2 .
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?
Nishu said:
5 years ago
I think, the error here is that the value of type void can't be assigned in type int
a=f();
Here return type of f() is void and a is of int type.
a=f();
Here return type of f() is void and a is of int type.
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.
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()?
Mrunali said:
1 decade ago
I also think the error is with there being no return variable but still a=f(); is given.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers