Void Keyword


In C programming, the 'void' keyword is used to indicate that a function does not return a value or that a pointer does not have a specified type.

It is commonly used in function declarations and pointer declarations to denote the absence of a specific data type.

Here's an example of using the 'void' keyword in function declarations:


void printMessage() {
  printf("Hello, World!\n");
}

int main() {
  printMessage();
  return 0;
}

    

In this example, the 'printMessage()' function is declared with a return type of 'void', indicating that it does not return a value.

The function simply prints a message to the console. Inside the 'main()' function, the 'printMessage()' function is called to execute its code.

Since the function has a 'void' return type, no value is expected or used after the function call.

The 'void' keyword is also used in pointer declarations when you want to create a generic pointer that can point to any type of data. For example:


void* genericPtr;

    

In this case, the 'genericPtr' is a pointer of type 'void*', which can be used to store the address of any data type.

However, the actual value and size of the pointed data cannot be determined directly using the 'void*' pointer. It needs to be explicitly cast to the appropriate data type before accessing or manipulating the data.

The 'void' keyword is an essential part of C programming, allowing you to indicate the absence of a return value or to create generic pointers for flexible data handling.

Loading...

Search