EXIT_FAILURE vs exit(1)?

后端 未结 5 1578
旧时难觅i
旧时难觅i 2021-01-30 01:16

What\'s the difference? Which is preferred, or when should I use each one respectively?

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-30 02:11

    exit(1) (usually) indicates unsuccessful termination. However, its usage is non-portable. For example, on OpenVMS, exit(1) actually indicates success.

    Only EXIT_FAILURE is the standard value for returning unsuccessful termination, but 1 is used for the same in many implementations.


    So to summarize:
    If you want to write perfectly portable code use,

    EXIT_FAILURE for failure case. While,
    You can use either exit(0) or EXIT_SUCCESS for success case.

    Note that, EXIT_SUCCESS or 0 are both same.


    Reference:

    C99 Standard: 7.20.4.3 The exit function
    Para 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.

提交回复
热议问题