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

Hemanth said:   2 years ago
@All.

Here is my explanation.

The above code will not compile and will give an error.

The reason for the error is that you cannot declare a void variable in C. The void keyword is used to indicate that a function returns no value. In this case, you are trying to declare a void variable, which is not valid.

If you want to declare a variable with an initial value of zero, you can use the following code:

#include <stdio.h>
int main()
{
int v = 0;

printf("%d", v);

return 0;
}

This will declare an integer variable v and initialize it to zero. The printf statement will print the value of v to the console.
(10)

Gopi vykuntapu said:   9 years ago
When we are declaring a variable we should provide what type it is that int, char, float, double.

From that compiler arrange memory for that variable, bur void is used in function passing values and return value. If fun returns int type data than we provide an int. If fun returns nothing then we provide void. But in the case of a pointer that pointer may be any type it takes the 4 bytes (in GCC) so that whether it is int, char, float, any it takes 4 bytes. So we can use void pointer variable that can typecast to any data according to our requirement.

Gourab Paul said:   1 decade ago
The void type is said to comprise an empty set of values, and the C language does not provide any way to declare an object or represent a value with type void.
Thats why for the above problem the compiler shows the following:
Error: Declaration syntax error 'v' (or) Size of v is unknown or zero.

#include<stdio.h>
int main()
{
void v = 0;
/*C language does not provide any way to
declare an object or represent a value with type void*/

printf("%d", v);

return 0;
}

Aastha Pokhriyal said:   9 months ago
@All.

Listen to my explanation.
It's simple. We can only use void in function declaration/definition or with a pointer.
For eg:
void display(){} //indicates that the function will not be returning a value
int factorial(void){} //indicates that the function has no parameters
void *ptr; //indicates that the pointer ptr points to nothing

But using void in a normal variable declaration is not valid.
Eg: void sum = 0;
(3)

Atul ANAND said:   1 decade ago
It is very simple, but first lets get one thing straight that YES void is a PRIMARY datatype.

We can use void pointer because size of pointer does not depend on datatype, when you have to access your data using void pointer then you have to typecast.

When we use void variable the size of variable is unknown.

So this part is tricky as the compiler doesn't know how much memory it needs to store this variable.

Shiva Keerthi said:   1 decade ago
Void is not a "data type". Its a result type or a "side effect" of a function. Result type again need not be always binary, like true or false, it can be interpreted as having some result or having no result. About void pointers. They cannot be deference since void pointers indicate the absence of type and also pointer arithmetic is not possible since "data" can not be determined. !

Kunal Chatterjee said:   9 years ago
The error is shown as in any variable declaration it is actually an indication to the compiler about the type and size of the variable.

For eg : In the statement int a; the compiler determines that when a will be defined we need to allot 2 bytes to it.

But for avoiding data type it cannot assign determine the type & the size of memory to be allocated. Hence, an error is reported.

Mohsin said:   2 decades ago
v and *vptr is declared as void pointers and we cannot use v and *vptr until and unless we convert that variables to the desired data type.

Here we are assigning value 0 to v and v doent hold any memory location to assign the value first we have to convert void v to integer type then only we can assing the value to it.

Ex (int) v = 0 ;
or v = (int) 0 ;

Anything I am not sure.

Vinit VJ said:   1 decade ago
void is like a 'general type' which contains nothing nor can be given/return but we can cast the variable of void type to other data type! thus we can make the same variable in exists which contains a 'memory'.

In short a data type except 'void', returns a memory space to variable in program to make it alive.

Chaitu said:   1 decade ago
Unlike other primitive data types in c, void data type does not create any variable but returns an empty set of values. Thus, we can say that it stores null.

I guess there is an error in the above program i.e., the 'A'.

Since it should not be given an integer value since it is having a null. ,


Post your comments here:

Your comments will be displayed after verification.