Return 0 doesn't make any change in my program.

I was learning C++, and I saw that return 0 indicates main function ran successfully. And non-zero return statements indicate that my program didn't run successfully. But when I use non-zero return statement, it worked completely fine.
1
2
3
4
5
6
#include <iostream>

int main(){
  std::cout << "Hello world" << std::endl;
  return 5;
}

Hello World

Then I searched google and I knew that it can work fine in some operating system. But it will not be much portable. To get portability, I can use **<cstdlib> header and EXIT_FAILURE in return statement. But it also worked fine for some reasons.
1
2
3
4
5
6
7
#include <iostream>
#include <cstdlib>

int main(){
  std::cout << "Hello world" << std::endl;
  return EXIT_FAILURE;
}

Hello world

And it gave me same output... So, what does non-zero/EXIT_FAILURE return statement mean? Am I understanding something wrong?
Last edited on
The return value from main() is effectively a return value to the os. If the program is simply run direct from the command line, then the returned value is not used and is discarded. If the program is run from a batch file or started from another program as a process etc then it's possible to obtain this returned value.

PS Windows
Last edited on
The return value of the main function servers the same purpose as any other function: It enables the caller to do something with the return value.

In case of the main() function it is e.g. the console or some other console programs that may deal with this value.
this means there is no difference between zero and non-zero return statement when I run the program from command line? then what does this mean that "non-zero return statements mean the program didn't run successfully"? what does successfully run and not successfully run mean tho?
> If the program is simply run from the command line, then the returned value is not used and is discarded.

Not necessarily; we could use $? (bash) or %ERRORLEVEL% (windows command line) etc.

Though the most common command line construct is to test if a previous command succeeded. For example:
g++ -std=c++20 -O3 -Wall -Wextra -pedantic-errors main.cpp && ./a.out
In Windows, %ERRORLEVEL% is usually used within a batch file - as mentioned.

You can do this at the command prompt:


myprog
echo %ERRORLEVEL%


but if this was required, it would be done within a batch/shell file.

You can also do things like this - but again usually within a batch/shell


if %ERRORLEVEL% neq 0 echo The last command failed!


Here's a simple version in a shell, the same thing as what JLBorges was saying:

$ progA && progB

If progA returns any thing other than 0 from main(), progB doesn't run.
Using && from the command line is fairly common.
For instance: build a program, and if the build succeeded then run the tests
or: fetch the file from the server, and if it was fetched then process it.
Might be in Linux et al, but not really in Windows AFAIK. IMO this would be done via a batch file in Windows.

Apparently powershell 7.0 has pipeline chain operators, allegedly similar to bash:

https://stackoverflow.com/questions/59038605/is-there-a-powershell-pattern-for-if/59039112#59039112
Yes, but IMO like batch in Windows these are used in shell scripts - not direct at the shell prompt. But I'm not a shell expert. I wrote my last serious batch file over 20 years ago....

Hello rajin100000,

this means there is no difference between zero and non-zero return statement when I run the program from command line?
Yes this is true.

then what does this mean that "non-zero return statements mean the program didn't run successfully"?
This is also true.

what does successfully run and not successfully run mean tho?
Successfully means that there was no problem with the run. Not successfully means that there was some problem detected in the run.

The best example I can think of is:
1
2
3
4
5
6
7
8
9
10
11
const std::string inFileName{ "" };  // <--- Put File name here.

std::ifstream inFile(inFileName);

if (!inFile)
{
    std::cerr << "\n     File " << std::quoted(inFileName) << " did not open.\n";  // <--- Requires header file "<iomanip>".
    //std::cerr << "\n     File \"" << inFileName << "\" did not open.\n";

    return 1;
}

Used in "main" should the if statement become true you print the error message to the screen and leave the program. The 1, could also be any other non (0)zero number. Not only does this mean that there is a problem you can use the number to help locate where it went wrong.

Should the if statement be false you bypass the if statement continuing running your code and, hopefully, have a normal exit from the program.

Give this a try. To the right of your code is a gear icon with the words "Edit & Run". Click on that then press the "Run" button. At the bottom of the lower window it will show the number returned on exit. Beyond that the number is not used for anything.

Andy
Handy Andy wrote:
Beyond that the number is not used for anything.


Have to disagree, as per the posts above.
@TheIdeasMan

You are right. My bad. Should have stated that better. I was interrupted and lost my thought.

I was more referring to the shell program showing the returned value of the program, but the number is not used after that unless you can write a batch file or the equivalent for that operating system to run the program and deal with the returned number.

Andy
> but the number is not used after that unless you can write a batch file or the equivalent
> for that operating system to run the program and deal with the returned number.

You can use it after that in an interactive shell if you know how to; it is really not difficult.
A variable created at the prompt remains in existence until the shell is terminated.


> Apparently powershell 7.0 has pipeline chain operators, allegedly similar to bash

Even the lowly windows cmd.exe has these operators.
It is another matter that many people don't know how to use them and therefore don't use them.
what does successfully run and not successfully run mean tho?
You, the programmer, determine what that means. Perhaps the user entered incorrect input, so instead of trying to run the program with bad data, you simply stop what you're doing and return a non-zero return code.

(It also doesn't reverse time to prevent "Hello world" from being printed. Would be a cool feature.)
Last edited on
Topic archived. No new replies allowed.