Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we declare a variable of type void?

People also ask

Can we declare a variable of void type?

The void type has no values therefore we cannot declare it as variable as we did in case of integer and float. The void data type is usually used with function to specify its type. Like in our first C program we declared "main()" as void type because it does not return any value.

Can we use void with variable?

Speaking strictly about Java, void is a keyword that's not usable with variables.

Why void is not return type?

Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value when the function is executed. The void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement.

Can a variable be void in C?

C # in Telugu The void pointer in C is a pointer which is not associated with any data types. It points to some data location in the storage means points to the address of variables. It is also called general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.


You cannot declare a variable of type void because variables must have object type or be references, extern void f; doesn't declare a reference, and void is not an object type:

Section 3 [basic] says that

A variable is introduced by the declaration of a reference other than a non-static data member or of an object.

Section 3.9 [basic.types] says that

An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not a void type.


"void type is an incomplete type"

You can't create variables of any incomplete type

"...that cannot be completed"

While your example of extern incomplete struct can be completed at some later point, the compiler knows that any declaration of type void can never be completed.


[edit] The answer below makes valid observations, but they're contradicting. As these might be valuable, I'll not delete them, but see Ben Voight's answer and the comments there for a more straightforward approach.

Your observations about extern declarations are specifically allowed by 7.1.1/8:

The name of a declared but undefined class can be used in an extern declaration. Such a declaration can only be used in ways that do not require a complete class type.

void is not a "declared but undefined class", and there's no other exception in 7.1.1 which applies.

Additionally, 3.9/5 is fairly explicit that it is in fact allowed:

A class that has been declared but not defined, an enumeration type in certain contexts (7.2), or an array of unknown size or of incomplete element type, is an incompletely-defined object type. [45] Incompletely defined object types and the void types are incomplete types (3.9.1). Objects shall not be defined to have an incomplete type.

Emphasis mine. This part of the standard is quite specific about the differences between definitions and declarations, so by omission it specifies that declarations are allowed.


If the variable has an empty set of values, it can't be used for anything.

You can't assign to it, because there are no possible values to assign.

You can't access it, because you never assigned to it, so it has an indeterminate value.

Since there are no possible values, there's no size of the variable.

void is just used as a placeholder in variable places. It's used as a return type to indicate that the function doesn't return a value. It's used in C in the argument list to indicate that the function takes no arguments (to resolve an ambiguity from the pre-prototype version of the language). And it's used with pointer declarations to create generic pointers that can be translated to any other pointer type. There's no such analogous use for it in variable declarations.


Because C and C++ assume that any objects may be compared for identity by comparing their addresses, they must ensure that all objects have fixed non-zero size. Were it not for that requirement, there are in fact many cases where it would be somewhat useful to declare zero-sized objects [e.g. in code which uses templates which contain fields that will sometimes be useful and sometimes not, or as a means of forcing a structure to be padded to a certain alignment requiring that it contain an element requiring such alignment]. As it is, however, zero-size types would be inconsistent with fact that the rule specifying that every object has a unique address includes no exception which would allow for the existence of zero-sized objects that could share an address.

Even if zero-size objects were permissible, however, a "pointer to unknown object" should not be the same as a "pointer to a zero-size object". Given that the type void* is used for the former, that would imply that something else should be used for the latter, which would in turn imply that something other than void should be the type of thing to which a zero-sized object points.


void is an incomplete type - you can only declare pointers to them and use them in function signatures. Obviously, extern Foo f; is permitted because struct Foo can be defined in another compilation unit (and if it's not the error will be detected by the linker), but void can't ever be "defined" (and the compiler knows this, of course) so void's quite special in this case.