What is void
in C?
What does void
mean in C? I think it’s confusing to think of it having a singular meaning. It is part of a number of unrelated syntactic forms, and should be understood separately in each:
- As a return “type”. In the function declaration
void foo(int)
, thevoid
signifies that “foo
does not return a value”. - As a parameter type list. In the function declaration
char bar(void)
, thevoid
signifies that “bar
has zero parameters”. - As the target “type” of a pointer. In the declaration
void* baz
, thevoid
signifies that “baz
points to a value of unknown type”.
It is confusing to call void
a “type”. There are positions where a normal type (int
) can be used, but void
cannot. For example, int foo;
is a valid declaration, but void foo;
is not. There are also positions where void
can be used, but a normal type cannot. For example, char foo(int) { return 'c'; }
is not a valid function definition.
The standard calls void
an “incomplete type”, but I find this unenlightening in the case of char bar(void)
.
Tagged . All content copyright James Fisher 2016. This post is not associated with my employer.