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

Farman said:   9 years ago
I think the void variable is a general type if we assign any value to the void data type we must be type cast according to the data which we want to be stored.

Umesh kumar said:   1 decade ago
void v=0;

In this statement, v is assigned the value 0 which is a integer value and the void type is used to declare it. Both doesnot match, so, its a error.

Rachna said:   1 decade ago
void should not be written here. Only this is the syntax error, otherwise it is correct. void just used for return statement and not in front of declaration.

Saif said:   1 decade ago
Before declaring any variable we have to write its type which is void in this problem and void means nothing that means we didn't declare the variable 'v'.

Mithilesh Upadhyay said:   1 decade ago
The void is primitive data type in C. The "void" can only used as return type for function definition. It cann't used for variable declaration.

Suryakirru said:   1 decade ago
Actually void main means that function does not returns any value to the function but in the question he asked that return 0; That's the error I found.

Hetram Lakhera said:   9 years ago
Hello, all.

We can not assign the value of the pointer.
but void *p;p=0;
int *i;i=0; is valid but p=1, p=9, p=67 and i=12, i=4 is not valid why?

Sunny said:   1 decade ago
In gcc compiler it generate an error like.

Error: variable or field ‘v' declared void.

Declaration of empty data type to a variable is an error.

Ritesh bhadani said:   1 decade ago
@Sumathi.

int main()

Is a function which is necessary in all C programs, in represents that the main function must return a value of integer type.

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.


Post your comments here:

Your comments will be displayed after verification.