What is void in C

    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:

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!

A void function does not return a value

    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. 

    void printCompanyInfo()
    {
        printf("====================\n");
        printf("Company **************\n");
        printf("Company Id ******************\n");

        printf("Contact information: \n");
        printf("address *********************\n");

        printf("Phone ****************** \n");
        printf("Fax ****************** \n");

        printf("Email ****************** \n");
        printf("====================\n");

    }

    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.

function(void) – does not accept parameters

    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.               

    int sumFirst10Numbers(void)
   
{
       
int sum = 0;
       
int i;
        for(i = 1; i <= 10; ++i)
           
sum += i;
       
return sum;
   
}

    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.

    int sumFirst10Numbers()
   
{
       
int sum = 0;
       
int i;
        for(i = 1; i <= 10; ++i)
           
sum += i;
       
return sum;
   
}

    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.

What is void pointer?

    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 instance helper = 1 means int, helper = 2 means double and so on.

    void voidPointer(void)
   
{
       
void *pointer;
       
int number = 5;
       
char symbol = 'a';
       
int helper = 1;
       
pointer = &number;

        //Uncomment the next to lines to test test see that the pointer could be used with a different type
       
//helper = 2;
       
//pointer = &symbol;

        if(helper == 1)    // use an int pointer
           
printf("The number is %d.\n", *(int *)pointer);
       
else if(helper == 2)      // use a char pointer
           
printf("The symbol is %c.\n", *(char *)pointer);
   
}

    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:

Previous keyword: unsigned

Next keyword: volatile

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!


› void

Did this help? Support me with your vote ;-)

Did this help?