-7

How to cout the return value of the main function "main()"? I just want to check if the return value of main function is really "0" when it is successfully executed.

Thank you!

OSUCowboy
  • 57
  • 2
  • 6
  • 1
    Rename `main()` to `notmain()`. Add a new `main()` function that calls `notmain()`, and prints the value it returns. – Sam Varshavchik Sep 04 '16 at 18:44
  • You decide the return value, not the compiler. Also,it is untraditional and uncanny to call `main()` – Arnav Borborah Sep 04 '16 at 18:44
  • 3
    `echo $?` from the shell usually serves you well – πάντα ῥεῖ Sep 04 '16 at 18:44
  • That's platform specific. The C++ Standard doesn't describe a Universe outside of C++ (or main). Besides, returning 0 on success is a **convention**. Not everyone likes that convention. – IInspectable Sep 04 '16 at 18:44
  • 1
    Send the value to `cout` before you return? How is this even a question, really? – MrEricSir Sep 04 '16 at 18:44
  • @MrEricSir: How does that verify which value is passed to the OS? – IInspectable Sep 04 '16 at 18:52
  • The same way you cout other variables? You are always responsible for what main returns. You modify the return value according to what happens in your program. – rlam12 Sep 04 '16 at 18:53
  • If I rename main() to notmain() which is a normal function, I know how to print its return value. I tried to cout the main, but it returned the result of the function called in the main the first time. And the second time I tried, it directly became an error... But thank you! – OSUCowboy Sep 04 '16 at 23:34
  • Does this answer your question? [What should main() return in C and C++?](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – user894319twitter Jul 27 '21 at 04:02

5 Answers5

0

Do you mean you want to test that the following returns 0?

int main() {
  int rc = doSomething();
  return rc;
}

You can do it like this:

int main() {
  int rc = doSomething();
  std::cout << rc << "\n";  // Just print it out before returning
  return rc;
}

But it's better to check the return value in the shell from where you run your application:

On UNIX/Linux (in shell):

 > ./my_app
 > echo $?
 0

On Windows (in command prompt):

> my_app.exe
> echo %ERRORLEVEL%

Note that in C++ you are not allowed to intercept the call to main() or call it yourself.

3.6.1 Main function

The function main shall not be used within a program.

So you should not attempt to wrap the main() call and print its return value. Even if that works, it would not be portable (not to mention you'd actually end up running your application twice).

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • "So what you want is impossible" `std::cout << the_return_value; return the_return_value;`. – juanchopanza Sep 04 '16 at 19:12
  • "In C++ you can't intercept the call to main() or call it yourself." no you can do that , see my code in my answer – Hassen Dhia Sep 04 '16 at 19:16
  • No, I don't mean to print out the result of the function inside a main(). Every main() returns a zero at the end, if it is successfully run. I want to print out this returned value "0". – OSUCowboy Sep 04 '16 at 23:36
  • 1
    @HassenDhia: There are lots of things you can do. Some of them are well defined, others are undefined. Calling `main` is undefined. – IInspectable Sep 05 '16 at 09:28
0

You can use a unix command to check the return value of the previously executed command, in this case your c program.

echo $?

The shell variable $? is the return code of the previous command.

How to find the return value of last executed command in UNIX?

If you want to check the value within the code, just cout the value right before the return.

Community
  • 1
  • 1
camstu
  • 51
  • 1
  • 5
0

main is special, mostly because you can't call it yourself. The value that it returns is passed back to the operating system, which does whatever is appropriate. The C++ standard can't dictate how the OS treats various return values, so it provides an abstraction: you can use return EXIT_SUCCESS to indicate success, and you can use return EXIT_FAILURE to indicate failure. (You can also use return 0 to mean return EXIT_SUCCESS; the program's runtime system has to handle this correctly). The values of the macros EXIT_SUCCESS and EXIT_FAILURE are suitable for the OS that the program was compiled for. While 0 is the typical value for EXIT_SUCCESS, it's certainly possible for an OS to require a different value, and EXIT_SUCCESS will have the appropriate value for that OS. So even if you could check the value returned from main it wouldn't tell you much, without also knowing what value the OS expects from a successful program.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
0

Type the other function after main(), then type main(); in that function One problem though, main will be the first function to be called automatically. Although, i'm sure there is a way to fix it. This is how you call main or return main. Hope this helps.

int main() {

int i = 5;
cout << i;
}

int second() {
 main();
}
-2

Welcome to Stack Overflow!

If it's 0, then it's 0. You are the one who specify the return value of your own function. If you are unsure, please add code and/or problem, which is bothering you.

SuperPrower
  • 111
  • 1
  • 12