Here is a question about the code:

int main() { ... system("Pause"); return 0; // имеет ли смысл эта строчка после паузы? } 

Is it necessary to return a value in the main function if a system pause is made? Or is this example considered a bad style code?

  • one
    A function that returns non- void should always return a value! - mega Nov.
  • void is not a return value! This is an operator indicating that the function is accessing a procedure, that is, it returns nothing. - LOLPADT
  • 2
    void is a type specifier, means "nothing" and there are no procedures in the boxes. Here, only functions, and a function that returns void, can also return a value — the same void : void a () {} void b () {return a (); } - mega Nov.

6 answers 6

Yes, the return value makes sense. This came from the philosophy of Unix, where the program informs other programs with its return code whether it worked successfully. 0 is considered an indicator of success, the other values ​​are not.

If your program works "by itself", not in conjunction with others, in principle, you can return anything, but returning zero or not zero is still considered to be a good form depending on whether the program worked successfully or not.

If your program should work together with others, as part of a script, the return of a reasonable exit code (this is the name returned by the main function) is required!

According to the C ++ standard, you can omit the return function in the main function (which is equivalent to return 0 ), but I would not recommend doing this for reasons of good style.

  • Ie the completion of the function MAIN with the construction return 0 is a good style? - LOLPADT
  • 2
    @LOLPADT: yes. - VladD
  • Okay, in that case, I understand everything. - LOLPADT

The main() function returns an int value. Therefore, return needed in any case.

  • C ++ standard allows not to return anything in the function int main(...) - VladD
  • @ delphist007 is optional. The signature of the main function can be declared like this: void main () {} But this is not entirely correct. - LOLPADT
  • @LOLPADT, as far as I know, in gcc this will not compile. - delphist007
  • @VladD, well, at least it is ugly. Bad tone in other words. - delphist007
  • > Therefore, return is needed in any case. If we really find fault with it, then not just return, but return что_то_целочисленное :), but in general - yes, I support! - mega Nov.

The return value is always needed. First of all, nobody pauses. Under Linux, this code is not functional. I suggest just making an input wait. It is possible through getch() or while(!kbhit()) or something else. Note that not all methods mentioned are portable. Secondly, you can easily remove the system call during the program development process and? Thirdly, the program, in my concept, after the system continues execution with the next instruction. You can check it out in practice.

  • one
    A possible solution (in the topic had to) the problem of waiting for input in Windows (instead of all system ("PAUSE"), etc.) atexit ((void (*) ()) getchar); Write at the beginning of the program. It works on exit / return-from-main anywhere. - avp

In addition to what has already been said about the return code, I can add that it is correct (meaningful for the environment) to complete the program in 4 ways.

This is the return statement already considered, the exit () and raise () functions, and the _exit () system call.

The actions for return and exit () call are the same. For details on exit (), _exit () and raise (), see the corresponding man.

This can be illustrated with such a program:

 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> void fin() { puts("atexit func"); } int main (int ac, char *av[]) { atexit(fin); if (!av[1]) _exit(puts("_exit") == EOF); else if (*av[1] == 'x') exit (puts("exit") == EOF); else if (*av[1] == 'r') raise(SIGTERM); return (puts("return") == EOF); } 

But its result

 avp@avp-xub11:~/hashcode$ for i in x 1 '' r; do > echo ./a.out $i; ./a.out $i; echo rc = $?; done ./a.out x exit atexit func rc = 0 ./a.out 1 return atexit func rc = 0 ./a.out _exit rc = 0 ./a.out r Terminated rc = 143 avp@avp-xub11:~/hashcode$ 

The output rc = ... is the same return code with which the program "talks" about how it ended. Traditionally, 0 is successful.

By the way, in windows return codes are also used. For example (portable for Makefile (!))

 myprog && echo Success 

on the command line will display Success only if myprog successfully completed.

  • 2
    > Actions on return and exit () call are the same. This is true in C, but not in C ++. Calling exit () does not call objects destructors, which can lead to unpleasant consequences (for example, unresolved file stream buffers, unclosed network connections, etc.) - skegg
  • @mikillskegg, thanks, did not know. Apparently this is a consequence of the "runtime environment", implemented in C ++. It is strange that they do not do all this through atexit (). - avp
  • 3
    By the way, destructors will not be called only for automatic objects. For external objects, destructors will be called. In general, the problem of safe emergency termination of a program in C ++ is a very interesting topic. In addition, the following int main () try {...} catch (...) {// some code} constructions are acceptable to me as a C ++ scientist to like my inexhaustibility and constant riddles)))) - skegg
  • one
    @mikillskegg, and one of my friends (KTN. Experimental physicist) loved to twist the Rubik's cube. What is characteristic, often collected! - avp
  • one
    C ++ will be more abruptly - skegg

Is it necessary to return a value in the main function if a system pause is made?

Pause and return value are not connected at all. After pressing any key, the pause will be removed and the program will continue, as if nothing had happened. So this condition is superfluous.

Regarding return, I will not repeat, already answered.

     return 0; 

    if this line doesn’t come, then the operating system will still “something” return (“garbage”). some compilers won't even compile.

    ps it's better to choose "what to return" than to return the "garbage", if you do not want to return, declare main as void, but compilers can swear again

    • The garbage will be in C. In C ++ it will return 0. And with void, it can generally give an error. - skegg Nov.