void Type

This page was translated by a robot.

The type voidcannot be used for variables like other types. It is used in very specific situations to tell the compiler how code behaves. Depending on the situation, the specification voidthen means invalid , to be ignored , unknown or empty . In principle, the type can be translated voidwith the term nothing .

Details

The most common use of the type voidis when specifying the return type of a function.

void setSize(int x, int y);

A function voiddeclared with does not return a value. This is also known as a procedure in other programming languages, but this term is not common in C and C++. If you still try to return a value using the returnstatement with such functions , a C compiler issues a warning: return with a value, in function returning void . In C++, returning a value for a function with voida return type is not allowed and will result in an error: return-statement with a value, in function returning void .

The reason C only issues a warning and no error is that a function always reserves space for a return value, regardless of whether a value is returned or not. The C compiler thus writes a return value at this point at the programmer's request, even if the function was voiddeclared with . In contrast, however, addressing the return value of a function without a return value is not permitted in both C and C++ and results in the error message Void value not ignored as it ought to be . The supposed return value is considered invalid.

The parameter list of a function can also be voidmarked with:

int getArea(void);

In this case, specifying means voidthat the parameter list is explicitly empty, so no arguments are expected when the function is called. In C++, the function's parameter list can also simply be left blank, which means exactly the same thing. Due to the widespread use of C++, this specification voidis rarely found in the parameter list. In C, however, an empty parameter list means that the number of parameters is not known in the function declaration and can therefore be different in a later definition. However, specifying voidthe empty parameter list is also explicitly specified in C.

Another use of the type voidcan be found in the void-Pointer , the pointer to an indefinite type.

Explicit Discarding of Results

In C, every expression evaluates to a value. If only operands are evaluated in an expression but never written, then this expression has no effect (in other sources effect is often referred to as a side effect ). If an entire statement has no effect, a compiler today issues a warning like Statement has no effect .

However, there are cases where a programmer may want to write such a statement explicitly. Since the final result of a statement is automatically discarded by the compiler anyway, a programmer can simply post void-cast the expression. Because of this cast, the compiler will explicitly discard the result and will not issue a warning.

Most often, a programmer needs this explicit discard for excess parameters. Some function signatures (for various reasons) partially define parameters that are never used within the programmed part of the function. Depending on the compiler and settings, warnings are issued for such parameters.

To suppress such warnings, a programmer can use the following two methods:

discard result
overwrite with the same value
(void)unusedparameter;
unusedparameter = unusedparameter;

With the first method, the unused parameter is simply written down as an expression and the statement is ;terminated with a semicolon. voidThe warning is suppressed by casting to .

The second method assigns the parameter the same value that the parameter already has. This creates a statement that explicitly has an effect. Thus, no warning is issued either. However, this method is less common because it constcannot be used for parameters and it explicitly defines an effect, which contradicts the semantic goal, namely not addressing the parameter.

Both variants are fully optimized by today's compilers and can also be implemented using a preprocessor macro. Since casting voidis safer and more common after a little, here is a typical macro definition:

#define UNUSED(x) (void)(x)

Another example of a statement with no effect is the so-called empty statement . It consists of either a single semicolon ;or empty braces {}.

Have a break.
Stretch and Relax.
;
{}

In contrast to other statements without an effect, a compiler will either give no warning at all for such statements or, for example, something like empty statement or empty body for control structures . However, the behavior is compiler specific.