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;
}
Discussion:
168 comments Page 1 of 17.
Sarang S Nair said:
1 week ago
1. void v = 0; is invalid.
* void means “no value” or “no type”.
* You cannot declare a variable of type void.
2. printf("%d", v); is also invalid.
*Since v is not a valid variable, it cannot be printed.
* void means “no value” or “no type”.
* You cannot declare a variable of type void.
2. printf("%d", v); is also invalid.
*Since v is not a valid variable, it cannot be printed.
Bhalodiya said:
1 month ago
The error in this program occurs because void is used as a variable type in void v = 0;, which is not allowed in C.
The void type represents the absence of a value and does not have any storage size, so a variable of type void cannot be created.
As a result, the compiler reports that the size of v is unknown or zero, and using it in printf is also invalid.
The void type represents the absence of a value and does not have any storage size, so a variable of type void cannot be created.
As a result, the compiler reports that the size of v is unknown or zero, and using it in printf is also invalid.
Sonia Senapati said:
4 months ago
According to me, in C void doesn't have any size. So it's unknown.
Aastha Pokhriyal said:
2 years 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;
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;
(5)
Nithish said:
2 years 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:
3 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.
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:
3 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.
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:
4 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:
4 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.
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers