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

Madhu said:   1 decade ago
2 errors will occur they were
*value of type void is not allowed
*size of "V" is not know

Venky said:   1 decade ago
void *p;

Here it allocates the chunk of bytes memory and compiler doesn't know how to use this memory.

You can use this memory by using malloc() OR calloc() functions with proper typecasting.

p=(int*)(malloc(sizeof(int));

Now it occupies the memeory 2bytes for every int values.

Sai sandeep(Chinna) said:   1 decade ago
Hai,

Void is a primary datatype, but we can not use it for declaring a variable.

A pointer variable declared as a void but not a variable.

Hense, compiler sends the error message as declaration of variable is wrong.

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.


Post your comments here:

Your comments will be displayed after verification.