void is a special data type in C that
indicates no value or no type. It is mainly used
in three situations:
- As a function return type when no value is returned.
- As a function parameter list to indicate that the function accepts no arguments.
- As a generic pointer (
void *) that can point to any data type.
Uses of void in C
| Usage | Description |
|---|---|
| Function Return Type | Used when a function does not return any value. |
| Function Parameters | Used to indicate that a function accepts no arguments. |
| Void Pointer | Acts as a generic pointer that can point to any data type. |
1. Using void as a Function Return Type
If a function performs some task but does not return any value to the calling
function, its return type is declared as void.
#include
void greet()
{
printf("Welcome to C Programming!");
}
int main()
{
greet();
return 0;
}
Output
Welcome to C Programming!
In this example, the greet() function performs an action but does not
return any value.
2. Using void in Function Parameters
Writing void inside the parameter list indicates that the function
does not accept any arguments.
#include
int display(void)
{
printf("No arguments are passed.");
return 0;
}
int main()
{
display();
return 0;
}
Output
No arguments are passed.
The function display(void) cannot receive any arguments from the
calling function.
3. Using void * (Void Pointer)
A void * is called a generic pointer because it can
store the address of any data type.
#include
int main()
{
int number = 100;
void *ptr = &number;
printf("%d", *(int *)ptr);
return 0;
}
Output
100
void * pointer cannot be dereferenced directly. It must first be
converted (typecast) to the appropriate pointer type.
Function Returning void vs int
| Declaration | Description |
|---|---|
void sum(int a, int b); |
Does not return any value. |
int sum(int a, int b); |
Returns an integer value. |
Difference Between void and int
| Feature | void |
int |
|---|---|---|
| Represents | No value | Integer value |
| Can Store Data | No | Yes |
| Used as Function Return Type | Yes | Yes |
| Can Be Used as Pointer Type | Yes (void *) |
Yes (int *) |
Common Mistakes
Avoid These Mistakes
- Thinking
voidis an empty variable. - Dereferencing a
void *without type casting. - Returning a value from a
voidfunction. - Passing arguments to a function declared as
function(void). - Using
void main()in standard C programs.
Best Practices
- Use
voidonly when no value is required. - Always typecast a
void *before dereferencing it. - Use
int main()instead ofvoid main(). - Use
function(void)to explicitly indicate no parameters.
Prerequisites
Before Learning This Topic
- Basic understanding of C data types.
- Knowledge of functions in C.
- Basic understanding of pointers.
- Familiarity with function declaration and definition.
Interview Questions
- What is
voidin C? - What are the three main uses of the
voidkeyword? - What is a
void *pointer? - Why can't a
void *be dereferenced directly? - What is the difference between
void display()andint display()? - What is the purpose of writing
function(void)? - Is
void main()recommended in Standard C? Why?
Key Takeaway
The void keyword in C represents the absence of a value or data
type. It is primarily used as a function return type, to specify that a
function accepts no parameters, and as a generic pointer
(void *) that can point to any data type. Understanding these
three uses is essential for writing modular, reusable, and standard C
programs.