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 5 of 17.

Panchanan said:   1 decade ago
When we return a data into void type type cast most be need & assign the value into another variable. Void means nothing we cant assign any value into the void type.

Shakil Chand said:   1 decade ago
Void means empty (or nthing) so we use void main () function intialy in any program whcih means we don't return any value because it is void, but if we use int main() than we have to use 'return 0' .

So void can not be used.

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.

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.

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.

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

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

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

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.

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.


Post your comments here:

Your comments will be displayed after verification.