2
int main()
{
    return -1;
}

Taking above simple code for example, in main, if no error, return 0; if there's an error, return -1. But why g++ doesn't show any error or message after execution? Whereas in Xcode, it shows "Program ended with exit code: 255" that I can tell something wrong. or maybe the question is: in g++, how can I tell main with some error and returning non-zero value?

Thanks.

Bonnie
  • 23
  • 3
  • So... you just compile your program like `g++ main.cpp`? – StoryTeller - Unslander Monica Apr 07 '19 at 07:18
  • 4
    g++ only compiles your program. The -1 is returned when you run it. – interjay Apr 07 '19 at 07:20
  • 4
    In a shell, the variable `$?` has the exit status of the last command to run. So `./a.out; echo $?` – Shawn Apr 07 '19 at 07:23
  • Returning 0 on success and other values on error is a convention. There is no definition of that happens after `main()` returns some value. Typically, what happens depends on how the program is run (e.g. if a shell script runs the program, it can capture the returned value from `main()`, and do whatever it likes when it receives any particular returned value. – Peter Apr 07 '19 at 07:44
  • Does this help answer your question? https://stackoverflow.com/questions/5149228/return-value-range-of-the-main-function – John Zwinck Apr 07 '19 at 07:45
  • Note that returning a negative value from main is rarely what you want. You often want to just use the macros `EXIT_SUCCESS` and `EXIT_FAILURE` as your return value. – Jesper Juhl Apr 07 '19 at 07:56
  • @Peter Thanks; so this is just a convention. When executing main, it only means returning a value no matter what value is. When an error happens, we just throw a message and making the returned value stands for an error. From the perspective of main, it only means finishing execution and return a value. Am I understanding correctly? – Bonnie Apr 10 '19 at 04:37
  • Thank y' all for the help, really appreciate it. – Bonnie Apr 10 '19 at 04:46
  • @Bonnie - more or less. From the perspective of `main()` it is returning a value, and the value returned is irrelevant. It is the environment that receives the returned value that interprets the value. Naturally, the programmer may attach some meaning to each chosen return value in a manner that is (hopefully) consistent with that the host environment does with it. – Peter Apr 10 '19 at 07:24
  • Thanks @Peter for the detailed explanation. – Bonnie Apr 19 '19 at 04:53

1 Answers1

2

(elaborating what was already hinted in comments)

C++ is a (usually) compiled language. (Although there exist interpreters as well, XCode with clang or gcc do not belong to them.)

The source code (as exposed by OP) is compiled into machine code executable by the CPU. (Usually, it is the CPU the compiler runs on but it might be for another as well → cross-compiling.) Machine code is a binary code (hardly readable by the average user) which is the only code which can be directly "interpreted" by the CPU.

The produced executable has to be started using specific functions of the OS (e.g. fork() and exec()). A shell (e.g. bash) is able to do this but it can be programmed into other programs as well.

IDEs (like XCode or VisualStudio) provide commands (buttons) for compiling and starting. A "Play" button checks whether the source code has been changed since last compiling. If so, it is compiled again and the outcome stored as executable file. (Otherwise, the compile might be skipped.) Afterwards (assuming there wasn't any compile error) the IDE fork()s a process to exec()s the executable file (i.e. the compiled program).

Once, a program terminates it returns a number which can be received in the parent process (which started the program). If the parent process was e.g. bash then the return code is stored in a variable which can be retrieved afterwards.

echo $?

prints the returned value of last terminated child process.

An IDE may report the returned value in some kind of log window. In VisualStudio, it's the Output window. As OP described in the question, it seems to be similar in XCode.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • There are a number of C++ interpreters. One, for example, is Cling, which is built on top of Clang and LLVM libraries. It can parse everything that Clang can, plus some additional specific (interpreter-specific) extensions. – Peter Apr 07 '19 at 07:53
  • @Peter Oha. (I was not aware of it.) However, at least, XCode with clang or gcc shouldn't belong to them AFAIK. – Scheff's Cat Apr 07 '19 at 07:55
  • @Scheff Thanks, this is much clear. Yes, Xcode and gcc use different libs. My understanding: we let main return a value, it will be auto saved to a hidden variable so that we need to 'echo' it. There's no syntax error here but we need to add some handling if '-1' defined as an error code. – Bonnie Apr 10 '19 at 04:18
  • @Bonnie If you compile your application with `gcc` on command line (e.g. in `bash`), it's just compiled but not run. `return -1;` is just a statement "baked" into your executable code. If you run it (in `bash`) it will return with this code. To make it visible, the `echo $?` command can be used. `$?` is a `bash` variable providing the exit code of last called program. In XCode, there is a compile command and a run command. The run command compiles the source code if necessary and starts your application (on success). If program terminates, XCode outputs the returned exit code. – Scheff's Cat Apr 10 '19 at 05:23
  • Thanks @Scheff. It seems they both handle the returned value, -1, the same. – Bonnie Apr 19 '19 at 05:02