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 1 of 17.
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)
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)
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)
Dipak said:
5 years ago
Void is keyword in c so that we can't use it for variable name.
(2)
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)
Shivkumar Hegonde said:
7 years ago
Void is not a valid data type to store the data. It should be void *.
(1)
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)
Prachi Deshmukh said:
2 decades ago
Is the question correct?
BOth the pointers point to v ?
cptr = &v;
vptr = &v;
Or is it
cptr = &c?
BOth the pointers point to v ?
cptr = &v;
vptr = &v;
Or is it
cptr = &c?
Mymuna said:
2 decades ago
Correct.
Sandeep said:
2 decades ago
I run it on Dev C IDE then the error variable or field v declared void!
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers