What is the difference between exit(0) and exit(1) in C?

前端 未结 11 1827
时光取名叫无心
时光取名叫无心 2020-12-22 17:19

Can anyone tell me? What is the difference between exit(0) and exit(1) in C language?

11条回答
  •  伪装坚强ぢ
    2020-12-22 18:15

    The difference is the value returned to the environment is 0 in the former case and 1 in the latter case:

    $ ./prog_with_exit_0
    $ echo $?
    0
    $
    

    and

    $ ./prog_with_exit_1
    $ echo $?
    1
    $
    

    Also note that the macros value EXIT_SUCCESS and EXIT_FAILURE used as an argument to exit function are implementation defined but are usually set to respectively 0 and a non-zero number. (POSIX requires EXIT_SUCCESS to be 0). So usually exit(0) means a success and exit(1) a failure.

    An exit function call with an argument in main function is equivalent to the statement return with the same argument.

提交回复
热议问题