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;
}
Error: Declaration syntax error 'v' (or) Size of v is unknown or zero.
Program terminates abnormally.
No error.
None of these.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
165 comments Page 3 of 17.

Asritha said:   8 years ago
The void data type is used to hold any type of variable, provided that it should be typecasted.

Here the void variable is holding int value and it's not declared/typecasted hence the compiler doesn't know what type it is and hence it raises an error.

Jayaprakash said:   1 decade ago
Void means null and we usually used to declare the return type in functions. Variable declaration usually allocates memory to the variable.

Void keyword cannot assign any memory to the variable and hence it cannot be used for variable declaration.

Vishakha said:   1 decade ago
Only pointers can be declared as void not any other variable because pointers are of fixed size so even on being void its allocated space in memory but a normal variable declared as cannot be allocated space in memory and thus gives error.

Hemanth kumar.v said:   7 years ago
Actually, In this program, variable we is defined with void, at the time of compiling compiler will not able to allocated memory for that variable, because datatype of variable we is not given, so, error is thrown by compiler.

Thank you.

Uttam barik said:   1 decade ago
We declare for variables for user defined or predefined data type. But void is a return type which returns nothing. It not possible to assign a variable to a return type. Becuse we declare variables for datatypes only not for returntype.

Shashank patchalla said:   7 years ago
@All.

As per my knowledge; It is

#include<stdio.h>
int main()
{
void v = 0;

printf("%d", v);//* the address is not allocated so it's a very easy but somewhat difficult to crack ok *//

return 0;
}

Shakil Chand said:   1 decade ago
Void means empty (or nthing) so we use void main () function intialy in any program whcih means we don't return any value because it is void, but if we use int main() than we have to use 'return 0' .

So void can not be used.

Kareem Maghawry said:   9 years ago
We never use void for declaration variables because void means nothing, so if we used void in the variable that means no variable existing and this not related to c language. But we use void in function argument declaration.

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.
(12)

Teju said:   1 decade ago
void is a primary data type and since it does not return anything it cannot be used for declaration of a variable

or

if void is to used then it should be declared as pointer but not variable.


Post your comments here:

Your comments will be displayed after verification.