Difference between int main() and void main() in C

1. Introduction

In C programming, the main() function serves as the entry point for program execution. However, there's a common debate between using int main() and void main(). These two forms differ in what they signify about the program's execution and its interaction with the operating system.

2. Key Points

1. int main() indicates that the function returns an integer value.

2. void main() indicates that the function does not return a value.

3. The return value of int main() is a status code that the execution environment can use.

4. void main() is often used in educational examples but is not standard in most C environments.

3. Differences

int main() void main()
Returns an integer value to the operating system. Does not return a value.
Standard and portable across different platforms. Not standard and may not be portable.
Commonly returns 0 for successful execution. Lacks a mechanism to indicate the status of program execution.

4. Example

// Example using int main()
#include <stdio.h>

int main() {
    printf("Hello, World using int main()\n");
    return 0;
}

// Example using void main()
#include <stdio.h>

void main() {
    printf("Hello, World using void main()\n");
}

Output:

Hello, World using int main()
Hello, World using void main()

Explanation:

1. In the int main() example, the program prints a message and returns 0, indicating successful execution.

2. In the void main() example, the program prints a message but does not return a status code.

5. When to use?

- Use int main() for compliance with the C standard, ensuring portability and proper interaction with the operating system.

- Avoid using void main() in professional or cross-platform environments, as it's non-standard and may lead to undefined behavior.

Difference between malloc() and calloc()?

Difference between Local Variable and Global Variable in C

Difference between Global and Static Variables in C

Difference Between Call by Value and Call by Reference in C

Difference Between getch() and getche() in C

Difference between printf() and sprintf() in C

Difference between Arrays and Pointers in C

Difference between Structure and Union in C

Difference Between Stack and Heap Memory Allocation in C

Difference Between Macro and Function in C

Difference between = and == in C

Difference Between for loop and while loop in C

Difference Between Linked List and Array in C

Difference between fgets() and gets() in C

Difference between ++i and i++ in C

Difference between struct and typedef struct in C

Difference between int main() and void main() in C

Difference between Character Array and String in C

Difference between break and continue in C

Difference between exit() and return in C

Comments