##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# exit

From cppreference.com
< c‎ | program

Defined in header `<stdlib.h>` |  |
void exit( int exit_code ); |  |

Causes normal program termination to occur.

Several cleanup steps are performed:

- functions passed to atexit are called.
- all C streams are flushed and closed
- files created by tmpfile are removed
- control is returned to the host environment. If `exit_code` is EXIT_SUCCESS, an implementation-defined status, indicating successful termination is returned. If `exit_code` is EXIT_FAILURE, an implementation-defined status, indicating unsuccessful termination is returned. In other cases implementation-defined status value is returned.

### [edit] Parameters

exit_code | - | exit status of the program

### [edit] Return value

(none)

### [edit] Example

Run this code

    #include <stdio.h>      /* FILE, fopen, fclose, fprintf, printf */
    #include <stdlib.h>     /* exit */

    int main () {

        FILE *fp;
        fp = fopen("data.txt","r");
        if (fp == NULL) {
           fprintf(stderr, "error opening file data.txt in function main()\n");
           exit(1);
        }

        /* Normal processing continues here. */
        fclose(fp);
        printf("Normal Return\n");
        return 0;
    }

Output:

    error opening file data.txt in function main()

### [edit] See also

abort | causes abnormal program termination (without cleaning up) (function) [edit]
atexit | registers a function to be called on exit() invocation (function) [edit]
quick_exit (C99) | causes normal program termination without completely cleaning up (function) [edit]
C++ documentation for exit

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c/program/exit&oldid=67214"