C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Point Out Errors (Q.No. 2)
2.
Point out the error in the following program.
#include<stdio.h>
int main()
{
    void v = 0;

    printf("%d", v);

    return 0;
}
Error: Declaration syntax error 'v' (or) Size of v is unknown or zero.
Program terminates abnormally.
No error.
None of these.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
165 comments Page 13 of 17.

Dharanesh said:   1 decade ago
As we know void is keyword not a datatype. It does return nothing.

Atul said:   1 decade ago
Error: variable or field we is void.

Uttam barik said:   1 decade ago
We declare for variables for user defined or predefined data type. But void is a return type which returns nothing. It not possible to assign a variable to a return type. Becuse we declare variables for datatypes only not for returntype.

Abhay said:   1 decade ago
#include<stdio.h>
int main()
{
void *v = 0;

printf("%d", v);

return 0;
}

output: zero

Nandan said:   1 decade ago
A variable must have some space (size of variable) to store some value in it. Void has no size and thus a variable declared as void can't store any value in it.

SuryaMaruti said:   1 decade ago
I think void is not variable define datatype.

Karthi said:   1 decade ago
The output of this program will return some error in

Line 4: error: variable or field 'v' declared void...

Pramod karale said:   1 decade ago
I think we cant declare variable with void datatype. We can only use void with function or declaring pointer i.e (void*)

So it gives the error.

Teju said:   1 decade ago
void is a primary data type and since it does not return anything it cannot be used for declaration of a variable

or

if void is to used then it should be declared as pointer but not variable.

Ankit said:   1 decade ago
Obviously this is correct question. A variable does not have a return type and void is a return type, so this is syntex error.


Post your comments here:

Your comments will be displayed after verification.