Do you pay attention towards writing main() function

When you first dipped your toes into C programming, I’m sure the very first thing you wrote was a main function—the heart of every C program. But have you ever stopped and wondered: 

👉 Why do some people write main(), some write main(void), while others go with void main(void) or int main(void)?

 👉 And the bigger question—which one is correct? 

In my case, We’ve seen students come up with all these variations. Strangely, none of them raise an error in most compilers.So does that mean all of them are fine? 

Well… yes and no. Let’s dive deeper.

1. main() 

#include <stdio.h>
main() {
printf("Hello, World!\n");
}
✅ Works fine in many compilers. 
❌ But this is relying on defaults—the compiler assumes main() means int main(void),
 but that’s not guaranteed on every platform. 
👉 Rule: Never rely on compiler defaults


2. main(void) 

#include <stdio.h>
main(void) {
printf("Hello, World!\n");
}

Slightly better—now you’re explicitly saying no arguments are expected.
 ❌ Still missing the return type. By default, some compilers may assume int, but 
again don’t depend on that.
 👉 Feels okay, but not perfect. 

3. void main(void)

#include <stdio.h>
void main(void) {
printf("Hello, World!\n");
}
Works on some compilers (especially older ones like Turbo C).
Incorrect by standard—because main should return something to the operating system. 
With void main, you’re leaving the OS clueless about success or failure.
👉 Avoid this, even if your college lab machines still accept it!

4. ✅ int main(void) — The Best Practice

#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0; // Returning 0 means success
}
✅ Theoretically perfect.
✅ Explicitly says main expects no arguments. 
✅ Explicitly returns an integer to the operating system.
👉 This is the correct, portable, professional way to write main.

🎯 Conclusion

The main() function may look like just another boring piece of boilerplate, but it’s actually
 the entry gate of your program. Writing it correctly is like greeting the operating system with a firm handshake.
 So next time you start a program, don’t be casual. Remember: 
💡 The perfect way is int main(void) with a return 0; at the end. 



Previous Post Next Post