📜  C/C++中exit()和break的区别

📅  最后修改于: 2022-05-13 01:54:59.430000             🧑  作者: Mango

C/C++中exit()和break的区别

在本文中,主题是了解 exit() 和 break 之间的区别。

退出()

  • 当用户想退出程序时使用此函数。
  • 它是一个void返回类型的函数,它调用在出口注册的所有函数并终止程序。
  • 文件缓冲区被刷新,流被关闭,临时文件被删除,因此内存被释放。

句法:

void exit(int status);

使用的参数如下:

ValueDescription
EXIT_SUCCESSSuccessful Termination
0Successful Termination
EXIT_FAILUREUnsuccessful Termination

中断()



  • 该函数一般用于瞬间跳出循环。
  • 当执行 break 语句时,它将控制权转移到 switch 或循环后面的语句。

句法:

break;

两个函数之间的表格差异

break() exit()
It is a keywordIt is a pre-defined function.
It doesn’t require any header file.It requires header file stdlib.h
It terminates the loop.It terminates the program.
It is often used only within the loop and switch case statement.It is often used anywhere within the program.
It can be used as a variable name as it is a reserved word in the C language.It is not a reserved word so, it is often used as a variable name.
In a C program, more than one break statement can be executed.In a C program, just one exit function will be executed.

方案一:

下面是一个演示 break 使用的 C 程序:

C++
// C++ program to demonstrate the use
// of break statement
#include 
using namespace std;
 
// Driver Code
int main()
{
    // Local variable definition
    int a = 10;
 
    // While loop execution
    while (a < 20) {
 
        cout <<"value of a:"<< a<< endl;
        a++;
 
        // terminate the loop using
        // break statement
        if (a > 15) {
            break;
        }
    }
 
    cout <<"The break statement executed"
           " when when the value "
           " became "<< a;
 
    return 0;
}
 
//this code is contributed by shivanisinghss2110


C
// C program to demonstrate the use
// of break statement
#include 
 
// Driver Code
int main()
{
    // Local variable definition
    int a = 10;
 
    // While loop execution
    while (a < 20) {
 
        printf("value of a: %d\n", a);
        a++;
 
        // terminate the loop using
        // break statement
        if (a > 15) {
            break;
        }
    }
 
    printf("The break statement executed"
           " when when the value "
           " became %d\n",
           a);
 
    return 0;
}


C++
// C++ program to demonstrate the
// use of exit()
#include 
using namespace std;
 
// Driver Code
int main()
{
    for (int i = 1; i < 5; i++) {
        if (i == 3)
            exit(0);
        cout <<"i = "<< i << "\n";
    }
 
     
 
    for (int j = 9; j > 0; j--) {
        if (j == 5)
            cout <<"j = "<< j;
    }
 
    return 0;
}
 
// This code is contributed by shivanisinghss2110


C
// C program to demonstrate the
// use of exit()
#include 
#include 
 
// Driver Code
int main()
{
    for (int i = 1; i < 5; i++) {
        if (i == 3)
            exit(0);
        printf("i = %d \t", i);
    }
 
    printf("\n");
 
    for (int j = 9; j > 0; j--) {
        if (j == 5)
            printf("j = %d \t", j);
    }
 
    return 0;
}


输出:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
The break statement executed when when the value  became 16

说明:在上面的代码中,break在满足条件时终止while循环,并且在break循环后将执行后面的while循环代码。

方案二:

下面是演示 exit() 使用的 C 程序:

C++

// C++ program to demonstrate the
// use of exit()
#include 
using namespace std;
 
// Driver Code
int main()
{
    for (int i = 1; i < 5; i++) {
        if (i == 3)
            exit(0);
        cout <<"i = "<< i << "\n";
    }
 
     
 
    for (int j = 9; j > 0; j--) {
        if (j == 5)
            cout <<"j = "<< j;
    }
 
    return 0;
}
 
// This code is contributed by shivanisinghss2110

C

// C program to demonstrate the
// use of exit()
#include 
#include 
 
// Driver Code
int main()
{
    for (int i = 1; i < 5; i++) {
        if (i == 3)
            exit(0);
        printf("i = %d \t", i);
    }
 
    printf("\n");
 
    for (int j = 9; j > 0; j--) {
        if (j == 5)
            printf("j = %d \t", j);
    }
 
    return 0;
}
输出:
i = 1     i = 2

说明:在上面的代码中,exit函数执行后,程序终止,之后没有代码被执行。

想要从精选的视频和练习题中学习,请查看C 基础到高级C 基础课程