main() functions return value?

后端 未结 5 1879
夕颜
夕颜 2020-11-30 13:27

Anyone please tell me where the main() function of the \'C\' language returns its value?

相关标签:
5条回答
  • 2020-11-30 13:36

    The main function is at libery to return its value at any point at which it pleases. You simply write:

    return my_return_value;
    

    and it's game over.

    0 讨论(0)
  • 2020-11-30 13:44

    C's main function returns an int... that int goes to the program which executed it (the parent process, if you will) as an exit status code.

    Specifically, on most operating systems, a 0 exit code signifies a normal run (no real errors), and non-zero means there was a problem and the program had to exit abnormally.

    0 讨论(0)
  • 2020-11-30 13:52

    The return value if the main() function is used as the exit status code of the program.

    In a shell you can get the exit status of a program using $?, example:

    ./prog
    exit_status=$?
    
    0 讨论(0)
  • 2020-11-30 13:52

    A general statement is: Function returns a value to the host environment.

    So main() will return value to any program or shell which is hosting that piece of code or to the OS.

    return value 0 is considered as successful execution

    0 讨论(0)
  • 2020-11-30 13:53

    From the C99 Standard:

    5.1.2.2.3 Program termination

    1 If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;

    and then

    7.20.4.3 The exit function

    5 Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

    In short, the return value of main is returned to the host environment in an implementation-defined form.

    0 讨论(0)
提交回复
热议问题