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 15 of 17.
Manish said:
9 years ago
Void means empty so nothing should get returned, that it must not have any value.
Kunal Chatterjee said:
9 years ago
The error is shown as in any variable declaration it is actually an indication to the compiler about the type and size of the variable.
For eg : In the statement int a; the compiler determines that when a will be defined we need to allot 2 bytes to it.
But for avoiding data type it cannot assign determine the type & the size of memory to be allocated. Hence, an error is reported.
For eg : In the statement int a; the compiler determines that when a will be defined we need to allot 2 bytes to it.
But for avoiding data type it cannot assign determine the type & the size of memory to be allocated. Hence, an error is reported.
Jagjit said:
9 years ago
Well you can't declare any variable as void, but you can declare pointers as void thats why we are getting error.
Gopi vykuntapu said:
9 years ago
When we are declaring a variable we should provide what type it is that int, char, float, double.
From that compiler arrange memory for that variable, bur void is used in function passing values and return value. If fun returns int type data than we provide an int. If fun returns nothing then we provide void. But in the case of a pointer that pointer may be any type it takes the 4 bytes (in GCC) so that whether it is int, char, float, any it takes 4 bytes. So we can use void pointer variable that can typecast to any data according to our requirement.
From that compiler arrange memory for that variable, bur void is used in function passing values and return value. If fun returns int type data than we provide an int. If fun returns nothing then we provide void. But in the case of a pointer that pointer may be any type it takes the 4 bytes (in GCC) so that whether it is int, char, float, any it takes 4 bytes. So we can use void pointer variable that can typecast to any data according to our requirement.
Farman said:
9 years ago
I think the void variable is a general type if we assign any value to the void data type we must be type cast according to the data which we want to be stored.
Reema said:
8 years ago
Void is not a keyword.
Jeratin said:
8 years ago
The void is datatype.
Ketan said:
8 years ago
The void is data type but its initialize with NULL value so no one can change or assign a value to the null variable.
Bharath said:
8 years ago
Void itself is a null value. You cannot declare a null value.
Sayali said:
8 years ago
void is a return type, not a data type. Hence we can not use it to declare a variable.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers