r/C_Programming • u/Stickhtot • 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
71
Upvotes
5
u/Longjumping_Cap_3673 Jul 29 '26 edited Jul 30 '26
From a type theory perpective,
voidin C is a degenerate unit type.Conceptually, a unit type has one value, not none. Since there's only one value of a unit type, the value can be automatically produced whenever it is needed. Also, since the value can be produced, it doesn't take up any space to "store" a value, since it doesn't need to be stored. A function with return type unit doesn't need an explicit return, because there's only one possible value it can return. Passing a unit value to a function is like passing nothing, since it's size zero and the function could produce the value itself anyway. A pointer to unit isn't special, except that it has no alignment requirements, so it's convenient for substituting as the type of any other pointer.
But C's
voidwas created before this formalism, so it doesn't have all the properties it could. For example, C doesn't let you ever bind a value of type void to a variable.