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.
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.
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.
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
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.
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....
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.
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.
> 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.)