C Programming

What Does void Mean in C, C++, and C#

In simple words, void means “nothing” or “empty”. It is used before the function name to indicate that the function is a void function. A void function is a function that does not return any value. It is used when we want to perform an action, but we do not care about the result of that action. For example, if we want to print a message on the screen, we can use a void function.

In programming, void is a keyword used in C, C++, and C# which refers to the absence of a value. It is a data type that does not hold any value or memory allocation. Instead, it is used as a placeholder or a marker for functions that do not return a value or when it is designed to simply terminate the program.

Follow this article’s guidelines to know about void use in C, C++, and C#.

What Does void Mean in C, C++, and C#

The void meaning in C, C++ and C# is the same but its use may vary depending on which programming language you use.

Void in C

In C, you use the void to declare a function that does not return any value (no return argument). For instance, a function that displays a message or performs a task without producing a result is usually declared void. An example of such a function in C is shown below:

#include<stdio.h>

void sum(int a, int b){
  printf("This is a function that has no return type \n");
  printf("Printing the sum of a and b: %d", a + b);
}
int main() {
    sum(5, 1);
    return 0;
}

In the above code, we are using a void function sum() to print a message and sum of two variables, and sum() is then called in the main() function.

Output

In some cases, a pointer may not have any memory location to point to, and in such cases, we use the void pointer. A void pointer is a special type of pointer that holds the address of any data type or object, regardless of its type or size. However, the void pointer cannot be dereferenced directly as it does not point to any specific data type or object.

#include<stdio.h>
int main() {
    int a = 4;
    char b = 'A';
    void * ptr = &a;
    int *a2 = (int*) ptr;
    printf("The integer is: %d \n", *a2);
    ptr = &b;
    char *b2 = (char*) ptr;
    printf("The character is: %c \n", *b2);
    return 0;
}

In the C code above, we are using a void pointer in the main() function and the pointer is pointed towards the variable a which is an integer, and prints its value. The pointer is then pointed to the variable b which is a character, and the value is printed then.

Output

Void Keyword in C++

In C++, the use of void in C is not limited to functions and pointers only. It can also be used as a type for function parameters, which indicates that the specific argument passed to the function does not have any specific data type or value. For instance, the main() function in C++ does not need any arguments and can be declared void.  An example is shown below:

#include <iostream>
using namespace std;

void message (void)
     {
         cout << "I'm a function!";
     }
int main (void)
    {
        message ();
    }

In the C++ code above, we are using void as a function parameter for the function message(). The output will then be printed when the function message() is called from the void main() function.

Output

Void Keyword in C#

In C# void is used as a return type for methods that do not return anything. This includes functions that return no value, as well as constructors, destructors, and event handlers. When a method has a void return type, it cannot be assigned to a variable or used in an expression. An example of a void method in C# is shown below:

using System;
class GFG {

    public void Text()
    {
        Console.WriteLine("Linuxhint");
    }
    public void sum(int a, int b)
    {
        Console.WriteLine("multi = " + (a * b));
    }
};
class Prog {
    static void Main(string[] args)
    {
        GFG ex = new GFG();
        ex.Text();
        ex.sum(5, 20);
    }
}

In the C# code above, two void functions Text() and sum() are declared, and then these functions are called from the void main() function.

Output

Conclusion

Void is a keyword used in programming languages like C, C++, and C# to indicate the absence of a value. It is used to declare functions and methods that do not return anything, and specifically as a function parameter in C++. Understanding how to use void correctly is essential for writing efficient and functional code.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.