What is the difference between void and int in C programming language?
In the C language, both void and int are keywords used to declare the return type of functions.
- Void: void is used to indicate that a function does not return any value. When the return type of a function is void, there is no need to use the return statement to return a value within the function.
void printHello() {
printf("Hello World\n");
}
- int is used to indicate that a function returns an integer value. When the return type of a function is int, the function needs to use a return statement to return an integer value.
int add(int a, int b) {
return a + b;
}
In conclusion,
- “void” indicates that a function does not return a value; in other words, it has no return value.
- “int” denotes a function that returns an integer value.
- Using void allows you to omit the return statement in a function, whereas using int requires a return statement to return a value.