return, exit, and break in C language

return, exit, break, continue in C language. When the first two functions are used, they usually bring the status code when the program exits. In standard C, there are two macros, EXIT_SUCCESS and EXIT_FAILURE, located in /usr/include/stdlib.h.
It is defined like this:
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0

1. The role of the exit function and its difference from the _exit() function.
According to the definition in the library file: exit is a library function, exit(1) means to exit the program after an error occurs, and exit(0) means to exit normally.
The exit function in stdlib.h is defined like this: void exit(int status);
this system call is used to terminate a process, no matter where in the program, as long as exit is executed, the process will terminate the process from run.
When it comes to the exit system call, we need to mention another system call. The _exit, _exit() function is located in unistd.h. Compared with exit(), the _exit() function has the simplest function and directly terminates the process. Release the memory space used by it, and destroy the data structure in the memory, and exit() is to check the status of the file before the process exits, and write the contents of the file buffer back to the file.
Below we illustrate the above situation by combining the function of printf, which operates the buffer: 1. exit terminates the process.
/* exit1.c */
#include <stdlib.h>
#include <stdio.h>

main()
{ printf("output begin/n"); exit(0); printf("output end/n"); } Execute gcc -o exit1 exit1.c to generate exit1, execute, only output begin will be printed. 2. exit writes the contents of the buffer back to the file. Corresponding to each open file, there is a buffer in the memory. Every time the file is read, several more records will be read, so that the next time the file is read, it can be read directly from the buffer of the memory. When writing a file, it is only written to the buffer in the memory, and when certain conditions are met (reaching a certain number, encountering a specific character (such as newline/n and end-of-file EOF)), then write the file in the buffer The content of the file is written into the file at one time. We know that void exit(int status); exit() is used to terminate the execution of the current process normally, and return the parameter status to the parent process, and all the buffer data of the process will be automatically written back and saved. Close unclosed files. #include <stdlib.h> #include <stdio.h>










main()
{ printf("output begin/n"); printf("content in buffer"); exit(0); } $ ./exit1 output begin content in buffer printf() will convert according to the parameter format string and Formats data, then writes the result to standard output until an end-of-string ('/0') occurs. It can be seen that exit saves the statement without a newline character to the annotation output file. 3. _exit() will not perform the operation of cleaning up the I/O buffer. _exit() is used to immediately end the execution of the current process, return the parameter status to the parent process, and close the unclosed file. This function will not return after calling, and will pass the SIGCHLD signal to the parent process, and the parent process can obtain the end status of the child process by the wait function. #include <stdio.h> #include <unistd.h>











main()
{ printf("output begin/n"); printf("content in buffer/n"); _exit(0); } $ ./exit2 output begin is actually because the second printf statement does not meet a specific condition , they are only stored in the buffer. At this time, we use the _exit() function to directly close the process, and the data in the buffer will be lost.






Compared with exit, return is mainly used to provide the return value of the function, while continue and break statements are mostly used in loops. Many friends may confuse these three statements with exit, and they are not clear about the distinction between these three statements.

2. The difference and connection between exit and return.

Of course, in process operations, we need to distinguish between exit and return.
1.exit() ends the current process/current program/in the entire program, as long as exit is called (the current process or the entire program in main)
2.return() is the return of the current function, of course, if it is in the main function main, naturally ends the current process, if not, it returns to the previous call. When there are multiple processes, if sometimes it is necessary to detect whether the previous process exits normally, it is necessary to use the return value of the previous process, and so on.
Process environment and process control (1): The start and termination of the process
exit can return any integer less than 256. The different values ​​returned are mainly for different processing by the caller.
Individual processes are returned to the operating system. If it is multi-process, it is returned to the parent process. Call waitpid() and other functions in the parent process to get the exit status of the child process for different processing. Let the caller make corresponding processing according to the corresponding return value. In general, exit() means that the current process returns control to the program that called the program, and the return value in parentheses tells the calling program the operation of the program state.
Process termination:
The termination of a C program is divided into two types: normal termination and abnormal termination.

Normal termination is divided into: return, exit, _exit, _Exit, pthreade_exit
Abnormal middle refers to: abort, SIGNAL, thread response cancellation
Mainly talk about the first four types of normal termination, namely the exit series functions.
The difference between the return function and the exit function:

1. exit is used to end the program at any time during the running of the program, and the parameters of exit are returned to the OS. The exit function is also implicitly called when the main function ends. When the exit function runs, it will first execute the function registered by the atexit() function, and then do some self-cleaning work, while flushing all output streams, closing all open streams, and closing the temporary created by the standard I/O function tmpfile() document. exit is to end a process, it will delete the memory space used by the process, and return the error information to the parent process, and return is to return the function value and exit function 2, return is at the language level, it represents the return of the
call stack; and exit It is at the system call level, which indicates the end of a process.
3. In a function with a return value, the function of the return statement is to provide the return value of the entire function, and end the current function and return to the place where it was called. The return statement can also be used in functions that do not return a value, for example, when an error is detected, the execution of the current function is terminated early and returned. Generally, the program execution is completed until the end of main(). If you want to do something at the end of the program, you can try to use this function.
example:

void test1(void)
{
printf("exit test1/n");
}

void test2(void)
{
printf("exit test2/n");
}

int main()
{
atexit(test1);
atexit(test2);
printf("exit main/n");
return 0;
}

3. The difference and connection between break and continue.

The break statement is usually used in loop statements and switch statements. When break is used in the switch statement switch,
the program can jump out of switch and execute the statement after switch; if there is no break statement, it will become an endless loop and
cannot exit.
When the break statement is used in the do-while, for, and while loop statements, it can make the program terminate the loop and execute the
statement after the loop. Usually, the break statement is always combined with the if statement. That is, when the condition is met, it will jump out of the loop.
#include <stdio.h>
main()
{ int i=0; char c; while(1) { c='/0'; while(c!=13&&c!=27) { c=getchar(); printf( "%c/n",c); } if(c=27) break; i++; printf("the no is %d/n",i); } printf("End/n"); } c=' /0' is to attach the initial value to the variable C. When the keyboard receives the Enter or ESC key, it starts to accept characters and print them to the terminal. If the keyboard receives the ESC key, it exits the while loop directly. Notice:



















1) The break statement has no effect on the conditional statement of if-else.
2) In a multi-level loop, a break statement only jumps one level outward.

The role of the continue statement is to skip the remaining statements in the loop and forcibly execute the next loop. The continue
statement is only used in loop bodies such as for, while, and do-while, and is often used together with the if conditional statement to speed up the loop.
main()
{ char c; while(c!=13) /* Loop if it is not a carriage return */ { c=getch(); if(c==0X1B) continue; /* If the Esc key is not output, it will proceed Next loop */ printf("%c/n", c); } }








Both continue and break can be used with all three loop structures provided by C. continue will cause the loop to jump out of the remaining statements in the loop body, and then judge whether the loop condition is true, and then choose whether to enter the loop body again. The function of break is to jump out of the loop. If the break is located in an inner loop in the loop nest, it only jumps out of the inner loop.

 

Guess you like

Origin blog.csdn.net/yangwenchao1983/article/details/6289150