What is void in C programming? It means “no type”, “no value” or “no parameters”, depending on the context. We use it to indicate that:
Advertise on this site. I promise you will like the rates :)
This is probably the most used context of the void keyword. Here we use it as a return type of a function. Such function does not return a value. However, it still can use the return statement to return control to the caller at any given time.
See how this function does not need to return a value? In this case we wanted to print all the information, so we did not use the “return;” statement to stop the function execution.
In C, if you don’t specify the parameters of a function, it can accept any number of parameters of any type. If you come from another programming language, this could be confusing at first. The way to define a function, that does not accept parameters in C is to use the keyword void as the only element in the parameters list.
Now “sumFirst10Numbers” explicitly says that it does not accept parameters. If we try to call it and pass one or more arguments, the compiler will give a warning or an error. It depends on the compiler. We could have created the function without the void and it will do its job the same way.
Then why do we prefer the first definition? Because it is safer. In both cases we don’t use any parameters. But if, by mistake, we call it with arguments we want to be warned that they will not be used.
It is a pointer, whose type is not known. It could point to an int, char, double, structure or any type.
In C, we don’t have a mechanism to test its type(for contrast to languages like C# and Java). That’s why, if you need to use a void pointer, you also want to keep track of the pointed type. This is usually done with a helper variable.
For example, helper = 1 means int, helper = 2 means double and so on.
Note that in order to use the value in a void pointer you need to dereference it. You must know the type of its value in order to dereference it. Void pointers in C are a powerful technique, but use it carefully.
The source code for all examples is available in this zip archive.
Now that you know what is void and its different usages, you can read about: