1

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.

Robin
  • 4,717
  • 12
  • 48
  • 81

2 Answers2

2

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.

DrakaSAN
  • 6,955
  • 7
  • 43
  • 85
  • 1
    The year 1990 is already here, international standardization! For those who don't program in "ANSI-C" but in _standard C_, it is perfectly fine to skip the return statement inside `int main (void)`. The year 1999 is already here too! New ISO standard. Or well, it was new, 18 years ago... – Lundin Dec 15 '17 at 12:46
1

int main() simply returns a result code to the OS after the execution of your code. For example, returning 0 usually means a successful run, and returning anything else means there was probably an error. Other than debugging and maybe bug reporting, there really isn't much use for returning an int from the main function. And of course, void means that you don't return anything. Good luck!

user2105505
  • 666
  • 9
  • 18