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.

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)

Nithish said:   1 year ago
A void is not exactly a data type, but when you define any function as a void it means that the function returns nothing/no value.
(2)

ABINESH DEVARAJ said:   2 years ago
In C, the void keyword is used to indicate that a function does not return any value.

It is not a valid data type for variables. In this program, you are trying to declare a variable V of type void, which is not allowed.
(12)

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)

Vivek Tutika said:   3 years ago
Since void is the data type of variable 'v', initialisation of we as 0 (v=0) leads to redeclaration of 'v'.

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.

C G said:   4 years ago
Here they use the format specifier as %d but it is not mentioned that V is an integer. Anyone, explain this in detail.
(1)

Tdk said:   4 years ago
The void is used here as a data type for a memory holder but it doesnt works that way, but it can hold as a void *ptr.

Dipak said:   5 years ago
Void is keyword in c so that we can't use it for variable name.
(2)

Dhruv singh said:   5 years ago
Actually, we can mention the return type of a variable at the time of declaration as well as intilization but in this case it should have return some integer type value. So we can not determine it as void.


Post your comments here:

Your comments will be displayed after verification.