C Reference ยท Types and Literals
C void: the empty type
Reference for the C void keyword: functions that return nothing, empty parameter lists, void pointers for generic code, casting to void and C23 changes.
void is a type with no values. C uses it in three places. As a return type, it says a function returns nothing. In a parameter list, it says a function takes no arguments. And in a pointer type, void *, it means "address of an object of unknown type", the basis of generic code such as malloc, qsort and memcpy.
You cannot declare a variable of type void, and you cannot take sizeof(void) in standard C (GCC allows it as an extension and returns 1). Everything void does is about omitting something.
void log_line(const char *text); /* returns nothing */ int next_id(void); /* takes no arguments */ void *buffer = malloc(64); /* pointer to unknown type */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
void function(void); /* no parameters */ void function(); /* C23: also no parameters; before C23: unspecified parameters */ void *pointer; /* generic object pointer */ (void)expression; /* discard a value */
| Use | Meaning |
|---|---|
void f(...) |
f returns no value. Using its "result" is an error. |
int f(void) |
f takes no parameters. A call with arguments is a compile error. |
void *p |
A pointer that can hold the address of any object. Cannot be dereferenced or used with arithmetic in standard C. |
(void)expr |
Evaluates expr and throws the value away; silences "unused result" or "unused parameter" warnings. |
Conversions: in C, a void * converts to and from any object pointer type implicitly, so int *p = malloc(n); is valid. In C++ the conversion from void * needs a cast. A void * cannot hold a function pointer portably.
Errors / undefined behavior: dereferencing a void * or doing p + 1 on one is a constraint violation (GCC accepts arithmetic as an extension, treating the size as 1). Converting a void * to a type with a stricter alignment than the object has is undefined. Reaching the end of a non-void function and using its result is undefined.
Usage notes
f()vsf(void): before C23, an empty parentheses list in a declaration meant "unspecified arguments", soint f();accepted any call. From C23,int f();means the same asint f(void);. Write(void)in code that must also compile under C17 and earlier.- Returning a value from a
voidfunction is an error;return;alone is fine. - Generic containers store
void *elements and the user supplies the type; the price is that the compiler can no longer check types. - A
void *hides the size, so functions likememcpyandqsorttake an explicit byte count or element size. - Cast before you dereference:
*(int *)vp. See void pointers. - Do not
freeormemcpythrough a wrong pointer type; the compiler will not stop you becausevoid *accepts anything. (void)xis the standard idiom for unused variables; in C23 you can also use[[maybe_unused]].sizeof(void)is not valid standard C.sizeof(void *)is the size of a pointer.
Related
- Pointers, Function declarations,
return,malloc,qsort, Attributes - Lessons: Void pointers, Functions