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 8 of 17.
Sunny said:
1 decade ago
In gcc compiler it generate an error like.
Error: variable or field ‘v' declared void.
Declaration of empty data type to a variable is an error.
Error: variable or field ‘v' declared void.
Declaration of empty data type to a variable is an error.
Georgekutty george said:
1 decade ago
'void' is primary data type. So we can create void data.
But in the case of variable. It is used to store value. Using void in variable is meaningless. So we got the error. !
But in the case of variable. It is used to store value. Using void in variable is meaningless. So we got the error. !
Arti said:
1 decade ago
'void' is a keyword and a keyword can't be initialized, if we try to do so it will show error.
Nitish Patil said:
1 decade ago
Since "void" is a primary data type, I believe we can create a variable of its type, but in printf statement there is a effort made in displaying the variable as an integer so just typecasting it to int would have made the code work.
Correct me, if I am wrong.
Correct me, if I am wrong.
SkYlAr said:
1 decade ago
Whenever you are declaring a void variable, it means that you are saying to compiler that memory requirements to store that variable is unknown, so when you will for printing it then compiler will never know up-to how much bytes of memory location it have to fetch the data and will give error.
Pushpa Rz said:
1 decade ago
void can be used for functions and pointers. Similarly variable can be declared void as well.
like void v;
A variable that is itself declared void (such as v) is useless, it cannot be assigned a value, cannot be cast to another type as pointer, infact, cannot be used in any way i.e useless.
like void v;
A variable that is itself declared void (such as v) is useless, it cannot be assigned a value, cannot be cast to another type as pointer, infact, cannot be used in any way i.e useless.
Rahini said:
1 decade ago
Variable is not assigned in void, only pointer variable is declared.
R.Harish said:
1 decade ago
void is not an data type.
void does not return any value.
void does not return any value.
Srinivas said:
1 decade ago
void is an predefined data type, void means nothing.
So we can't define variables for void.
So we can't define variables for void.
Shiva Keerthi said:
1 decade ago
Void is not a "data type". Its a result type or a "side effect" of a function. Result type again need not be always binary, like true or false, it can be interpreted as having some result or having no result. About void pointers. They cannot be deference since void pointers indicate the absence of type and also pointer arithmetic is not possible since "data" can not be determined. !
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers