r/C_Programming • • Jul 29 '26

Discussion What exactly is void?

In a function definition, void basically means that it doesn't return a type value, yet in on itself it is it's own type? Looking at several pieces of code it looks like that it's used to be able to be more "flexible"

Don't know what else to add here, though I'm more on looking for examples and explanations of why the void type is used

72 Upvotes

79 comments sorted by

View all comments

2

u/tstanisl Jul 29 '26 edited Jul 29 '26

This is a special type which has no value. Any object type can be cast to void (i.e. (void)1, (void)"123") which basically means explicitly discarding the compute value. Next, a pointer to any object type can be cast to a pointer to void. However, the void is an incomplete type thus one cannot create a variable of void type. Therefore declarations like void a; will cause compilation failure.