Triple X 'int main'

Hi guys,

So I have been watching the ‘first program’ video, and noted that Gavin said ‘return 0;’ is telling windows that the program has successfully completed and exits. He goes on to say that any other value would signal an error… Well, I added a different value just to see what the error would look like. I saved, recompiled and executed the program, and it went without a hitch and showed no error.

Any reason why? Just trying to understand what things look like so I can have a broader understanding.


Hi Nic,

In which course are you? I’m asking because this is the Unity subforum, and your thread isn’t tagged.

damn, sorry, I put it in the wrong thread… lemme change that!

It’s used to signal an error. For example you can write a separate test program to ensure your program is all working correctly and if it sees that it didn’t return 0 from main then something must have gone wrong.

ahh right, so it’s like creating a boolean of sorts? if int main returns a 0 it’s True and everything checks out, if it doesn’t then it’s False and something has gone wrong?

So why would int main return a 0 and how would it not return a 0? I don’t see currently how it would return a 0?


on line 19. If it manages to reach that then it returned 0.
Example of not returning 0:

int main()
    if (PI == 3)
        return 100;
    return 0;

So if PI (defined somewhere) is exactly 3 then terminate the program with 100 (the return 0 doesn’t happen).

Ahh right, my understanding was that whatever was above the return statement would generate a 0 and the return statement would look for that and then know that the program has executed properly and exit.

Rather it is simply there to make sure the code executes top down and when the return statement is hit, the program will exit.

Why not just use a simple ‘return’ keyword? Why are we returning something, doesn’t ‘return’ automatically exit the function anyway?

Cool, this makes sense, thanks for the explanation :slightly_smiling_face:
Would the program still exit if it returns 100, or does it need to be a 0?

Because it’s a non-void function. Non-void functions must return a value, and as explained that value is returned to the operating system.

You can see what the program returned with %ERRORLEVEL% on Windows or $? on Unix-like platforms (assuming it’s called “main”)



echo $?

Yes, any return from main terminates the program.

Ahh, great, thanks for the help Dan!

I’ve learned more python than C++, so I’m trying to draw some parallels. Syntax and form are different, but I’m enjoying it still :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms