Difference between "int main()" and "int main(void)" in C/C++?

In C programming, you might notice two different ways to declare the main function: int main() and int main(void). While both are valid, there is a subtle but important difference in how C treats them.

Syntax

int main()
int main(void)

Key Difference

In C, int main() means the function can accept any number of arguments, while int main(void) explicitly specifies that the function takes no arguments. In C++, both forms are equivalent and mean "no arguments".

Example 1: Function Without void

When a function is declared without void, C allows it to be called with any number of arguments −

#include <stdio.h>

void my_function() {
    printf("Function called\n");
}

int main() {
    /* This will compile successfully in C */
    my_function(10, "Hello", "World");
    return 0;
}
Function called

Example 2: Function With void

When a function is explicitly declared with void, C enforces that no arguments can be passed −

#include <stdio.h>

void my_function(void) {
    printf("Function called\n");
}

int main(void) {
    /* This will cause compilation error */
    my_function(10, "Hello", "World");
    return 0;
}
[Error] too many arguments to function 'my_function'

Comparison

Declaration C Behavior C++ Behavior Recommendation
int main() Accepts any arguments No arguments Avoid in C
int main(void) No arguments No arguments Preferred

Best Practice Example

For clarity and consistency, always use void when your function takes no parameters −

#include <stdio.h>

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

int main(void) {
    greet();
    return 0;
}
Hello, World!

Conclusion

While both int main() and int main(void) are valid in C, using int main(void) is technically better as it explicitly declares that the function takes no arguments. This prevents accidental function calls with unwanted parameters and makes your code more readable and maintainable.

Updated on: 2026-03-15T10:35:22+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements