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");
}
Error: Not allowed assignment
Error: Doesn't print anything
No error
None of above
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.

Anomie said:   3 years ago
Can anyone help me to clear this in detail?

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.

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 .

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.

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.

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?

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?

Mrunali said:   1 decade ago
I also think the error is with there being no return variable but still a=f(); is given.

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.

Suman said:   1 decade ago
@Rajesh.

It should be before your calling function.


Post your comments here:

Your comments will be displayed after verification.