Analysis of C ++ exit and return and exitreturn

Source: Internet
Author: User
Tags exit in

Analysis of C ++ exit and return and exitreturn
【Abstract]

This article analyzes the similarities and differences between exit and return in C ++ from the code form, common methods, related concepts, call relationships, and comparison.


[Common form]

Exit (0): The program runs normally and exits;
Exit (1): exit the program due to abnormal operation;
Return (): return function. If it is in the main function, it will exit the function and return a value.

[Related concepts]
  • The return statement can be used to make the branch program being executed return to call its method. It indicates that the current function is aborted and the operation permission is returned to the caller. If it is in the main function, the Operation permission is returned to the operating system.
If you want to exit a program that meets a certain condition, you can use return to return the code. Otherwise, if you do not write this code, the program will automatically return the code when it is executed, you can give it a try on your machine. But as a good programming habit, every function has a Return Statement, which is more readable and conducive to program maintenance!
The return command returns a specified data to the main function, and ends the execution of the function. The return command does not have to return a value.

  • The exit () function is used to end the program at any time during the program running process. The exit parameter state is returned to the operating system. The return value 0 indicates that the program ends normally, and the non-0 indicates that the program ends abnormally. The exit function is also called implicitly at the end of the main function. When the exit function is run, it will first execute the function registered by the atexit () function, and then perform some self-cleaning work, refresh all output streams, close all open streams, and close temporary files created using the standard I/O function tmpfile.

[Other]

Each process returns a value. When a process starts, the system starts a startup function that uses the main function:
Int nMainRetVal = main ();
After exiting from the main function, start the function and call the exit function and pass nMainRetVa to it. Therefore, the exit function is called at any time. Normally, the main function does not call the exit function, but returns 0; the return value is nMainRetVal, exit then receives this value as the parameter. Therefore, normally exit with exit (0.
If your program encounters an exception, you can call exit (1) in the main function to forcibly exit the program and terminate the process. If the value is not 0, the system exits abnormally.

[Comparative analysis]
1. return returns the function value, which is a keyword. exit is a function.
2. return indicates the language level, which indicates the return of the call stack, and exit indicates the system call level, which indicates the end of a process.
3. return indicates the exit of the function (return); exit indicates the exit of the process.
4. return is provided in C language, and exit is provided by the operating system (or provided in the function library ).
5. return is used to end the execution of a function and transfer the execution information of the function to other calling functions. The exit function is used to exit the application and delete the memory space used by the process, return a state of the application to the OS, which identifies some running information of the application. This information is related to the machine and the operating system. Generally, 0 indicates that the application Exits normally, if the value is not 0, the system exits abnormally.
6. The call of return and exit in non-main functions is very effective, but the call of return and exit in main functions is very vague. In most cases, they are consistent.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.