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 10 of 17.
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.
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.
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.
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. !
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.
Suprava mallick said:
1 decade ago
void can't be a data type, It is a return type which return null so it can't be declare with a variable name.
Ex: void a;// It show u an error.
Pointer variable declared as void. So it gives an error
Ex: void a;// It show u an error.
Pointer variable declared as void. So it gives an error
Gourab Paul said:
1 decade ago
The void type is said to comprise an empty set of values, and the C language does not provide any way to declare an object or represent a value with type void.
Thats why for the above problem the compiler shows the following:
Error: Declaration syntax error 'v' (or) Size of v is unknown or zero.
#include<stdio.h>
int main()
{
void v = 0;
/*C language does not provide any way to
declare an object or represent a value with type void*/
printf("%d", v);
return 0;
}
Thats why for the above problem the compiler shows the following:
Error: Declaration syntax error 'v' (or) Size of v is unknown or zero.
#include<stdio.h>
int main()
{
void v = 0;
/*C language does not provide any way to
declare an object or represent a value with type void*/
printf("%d", v);
return 0;
}
Babulu said:
1 decade ago
void is a data type it can holds any type of data(int,float,char..)so, if u want to print void type of data need to type cast in to that stream .
printf("%d", v);
In this statement %d(int type)
So need to typecast in to integer like
printf("%d",(int)v);
printf("%d", v);
In this statement %d(int type)
So need to typecast in to integer like
printf("%d",(int)v);
Prathyu said:
1 decade ago
We couldn't use &v in printf. We use that symbol for taking input from user in scanf statement not in printf statement.
K SURESHBABU said:
1 decade ago
void means that it does not return nothing.Here void declare to the V is not taken.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers