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

SkYlAr said:   1 decade ago
Whenever you are declaring a void variable, it means that you are saying to compiler that memory requirements to store that variable is unknown, so when you will for printing it then compiler will never know up-to how much bytes of memory location it have to fetch the data and will give error.

Pushpa Rz said:   1 decade ago
void can be used for functions and pointers. Similarly variable can be declared void as well.

like void v;

A variable that is itself declared void (such as v) is useless, it cannot be assigned a value, cannot be cast to another type as pointer, infact, cannot be used in any way i.e useless.

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.

Saki said:   1 decade ago
Here declaration of a variable must be done by using predefined and user defined datatypes as void is also a predefined data type also we cannot use it here which it does not allocates any memory during declaration of variable.

ex: int a=0;

int i=0; these statements can be correct.

Venugopal said:   1 decade ago
The void type has no values therefore we cannot declare it as variable as we did in case of integer and float.

The void data type is usually used with function to specify its type. Like in our first C program we declared "main()" as void type because it does not return any value.

Hari said:   3 years ago
Variable or field 'v' declared void.

Since we declared it as void before using it, we need to typecast the void variable into desired data type so that we can use v and also we can only declare void pointers which we can convert into desired data type which we need in our project.

Babulu said:   1 decade ago
void is a data type it can holds any type of data(int,float,char..)so, if u want to print void type of data need to type cast in to that stream .
printf("%d", v);

In this statement %d(int type)
So need to typecast in to integer like
printf("%d",(int)v);

Charu said:   1 decade ago
We can declare it as void v; or we can initialize it as int v=0;
If we do both means no meaning.

Moreover, In printf, we used %d which indicates that the variable is of type 'int' and keeps memory for it. But there is no int in declaration. That's the error actually.

Nitish Patil said:   1 decade ago
Since "void" is a primary data type, I believe we can create a variable of its type, but in printf statement there is a effort made in displaying the variable as an integer so just typecasting it to int would have made the code work.

Correct me, if I am wrong.

DevendraSingh said:   1 decade ago
1. void is fundamental data type, which always return null(i.e void).
2. value can not be assigned to void because we cannot assign value to null, as it is already null, so it does not accept
void v = 0;
3. void is used to show return type of function only.


Post your comments here:

Your comments will be displayed after verification.