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:
165 comments Page 17 of 17.
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'.
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.
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)
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.
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)
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)
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;
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers