Search code examples
cprogram-entry-point

Difference between void main and int main


I have seen many programs that has either int main() or void main(). I know both are used as the starting point of a program. I am just confused of when to use one and avoid another. I would really appreciate if you could explain it to me.


Solution

  • In the current ISO standard, it doesn't change anything, use what the other developers you work with expect. (credits to Lundin to remind me of that)

    For ANSI C, int main means you will end your program with return 0; (or other value, 0 is the standard for "everything's fine").

    void main will allow you to skip that line, and have some other effect, basically, depending on compiler, you may not be able to access argc and argv since the main take 0 arguments.

    Although it doesn't do a lot of bad, it's better in my opinion to use int main, so you don't have to worry about the side effect. It's also the norm in ANSI C.