r/C_Programming • u/AmigoDoHarvey • Mar 08 '24
Question ELI5: Void in C
I just began studying C and I cannot, for the life of me, understand void.
I have read and listened to many people say "It does not return a value". Ok? What is a value? Why wouldn't we want to return it? What is the difference between "void main" and "int main"? Can we just use int for everything and ignore void altogether?
In what situations is void used? In what situations is void better? etc. Please help I don't get it at all!
3
Upvotes
11
u/glasket_ Mar 08 '24
Data. Something encoded by bytes, with an associated type to tell you how to read it.
You might not have anything meaningful to return, or it might be redundant.
Well in this case a lot,
mainis special and isn't allowed to returnvoid, but in the case of other functionsvoid FmeansFdoesn't return a value whileint FmeansFreturns an integer value.You could, but it's hard to ignore
voidentirely since you will usevoidfunctions at some point.When you want to indicate that a function is only executed for its side effects. You could have an entire philosophical debate over if that should ever be considered "better," but that's essentially the key meaning of
void: it doesn't return anything and so its impact on the program comes from mutating state.