C Programming - Functions - Discussion

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");
}

[A]. Error: Not allowed assignment
[B]. Error: Doesn't print anything
[C]. No error
[D]. None of above

Answer: Option A

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.


Gauravj282 said: (Oct 24, 2011)  
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.

Vijay said: (Dec 2, 2011)  
Wrong because the function f () is not returning anything?

Sumit said: (Jun 15, 2012)  
f() is a void type function which does not return value so assignment is not allowed.

Rajesh.T.K. said: (Nov 18, 2012)  
Can anyone tell me about the function prototype?

Whether it should be declared only outside the main()?

Suman said: (Nov 23, 2012)  
@Rajesh.

It should be before your calling function.

Amrita Chaurasia said: (Jul 16, 2013)  
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.

Mrunali said: (Aug 18, 2014)  
I also think the error is with there being no return variable but still a=f(); is given.

Arjun Verma said: (Aug 31, 2014)  
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: (Jun 6, 2017)  
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?

Mayank said: (Jan 7, 2018)  
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.

Dawood Ibrahim Bhat said: (Mar 11, 2018)  
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.

Nitesh said: (Dec 6, 2020)  
@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 .

Nishu said: (Feb 23, 2021)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.