Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      exit

      From cppreference.com
      <c‎ |program
       
       
      Program support utilities
      Program termination
      exit
      (C99)
      Unreachable control flow
      Communicating with the environment
      Memory alignment query
      Signals
      Signal types
      Non-local jumps
      Types
       
      Defined in header<stdlib.h>
      void exit(int exit_code);
      (until C11)
      _Noreturnvoid exit(int exit_code);
      (since C11)
      (until C23)
      [[noreturn]]void exit(int exit_code);
      (since C23)

      Causes normal program termination to occur.

      Several cleanup steps are performed:

      • functions passed toatexit are called, in reverse order of registration
      • all C streams are flushed and closed
      • files created bytmpfile are removed
      • control is returned to the host environment. Ifexit_code is zero orEXIT_SUCCESS, an implementation-defined status indicating successful termination is returned. Ifexit_code isEXIT_FAILURE, an implementation-defined status indicating unsuccessful termination is returned. In other cases implementation-defined status value is returned.

      Contents

      [edit]Notes

      The functions registered withat_quick_exit are not called.

      The behavior is undefined if a program callsexit more than once, or if it callsexit andquick_exit

      The behavior is undefined if during a call to a function registered withatexit, the function exits withlongjmp.

      Returning from thethe main function, either by areturn statement or by reaching the end of the function, executesexit(), passing the argument of the return statement (or0 if implicit return was used) asexit_code.

      [edit]Parameters

      exit_code - exit status of the program

      [edit]Return value

      (none)

      [edit]Example

      Run this code
      #include <stdio.h>#include <stdlib.h> int main(void){FILE*fp=fopen("data.txt","r");if(fp==NULL){fprintf(stderr,"error opening file data.txt in function main()\n");       exit(EXIT_FAILURE);}fclose(fp);printf("Normal Return\n");returnEXIT_SUCCESS;}

      Possible output:

      error opening file data.txt in function main()

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 7.22.4.4 The exit function (p: 256)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.22.4.4 The exit function (p: 351-352)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.20.4.3 The exit function (p: 315-316)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.10.4.3 The exit function

      [edit]See also

      causes abnormal program termination (without cleaning up)
      (function)[edit]
      registers a function to be called onexit() invocation
      (function)[edit]
      causes normal program termination without completely cleaning up
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/program/exit&oldid=154433"

      [8]ページ先頭

      ©2009-2025 Movatter.jp