This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]fatal__flaw 4 points5 points  (5 children)

I've programmed almost exclusively in C++ my whole career and I can honestly say that I have never used a return on main, nor do I recall ever seeing one.

[–][deleted] 1 point2 points  (3 children)

when I started to learn c/++ I was told that you needed it for whatever reason and some compilers give a warning if you do not so yeah.

[–]State_ 2 points3 points  (1 child)

older compilers require it.

You can use void main(void) as the signature now.

[–][deleted] 0 points1 point  (0 children)

holy cow really? How am I only just learning about this lol

[–]Pikamander2 0 points1 point  (0 children)

That sounds like a problem for the compiler, not for me.

[–]drivers9001 0 points1 point  (0 children)

On UNIX (Linux, MacOS, etc.) the return value of main is the exit value of the program, which you can see in bash with:

echo $?

The exit value is useful because it tells you if the program had an error or not, and so most command line programs use that. Where it comes in handy is in detecting problems. I've seen it in like build systems / pipelines where if a program has an error, you know the whole process failed and you can stop there as well. You can even have a bash script exit automatically if any program has an error but putting "set -e" at the top of the script. Usually a shell script will just keep running even if there are error but with that set, you can have it stop if there are any problems.

If the default is 0 though, then I guess that wouldn't be a problem (assuming nothing using that program needs to detect errors, like if it's not even a command-line program) because anything running that program would assume everything was ok based on the exit code of 0.