日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

exit c+_C / C ++中的exit(0)vs exit(1)与示例

發布時間:2025/3/11 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 exit c+_C / C ++中的exit(0)vs exit(1)与示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

exit c+

exit() is a library function in C/C++ programming language, it is used to terminate the calling process (function) immediately i.e. we can say it is used for the normal program termination and also perform the several cleanup steps.

exit()是C / C ++編程語言中的一個庫函數,它用于立即終止調用進程(函數),即可以說它用于正常程序終止并執行幾個清理步驟。

Syntax:

句法:

void exit(int status);

Parameter(s): status – defines the exit status.

參數: status –定義退出狀態。

There are two status that we can define,

我們可以定義兩種狀態,

ValueMacroDescription
0EXIT_SUCCESSIt defines that the termination is on the success of the program.
1EXIT_FAILUREIt defines that the termination is on the failure of the program.
值 巨集 描述
0 EXIT_SUCCESS 它定義了程序成功的終止。
1個 EXIT_FAILURE 它定義終止是在程序失敗時進行的。

Return value: The return type of the function is void, it returns nothing.

返回值:函數的返回類型為void ,不返回任何內容。

1) exit(0) or exit(EXIT_SUCCESS)

1)退出(0)或退出(EXIT_SUCCESS)

exit(0) is used to terminate the program by specifying the successful execution of the program.

exit(0)用于通過指定程序的成功執行來終止程序。

Syntax:

句法:

exit(0);

2) exit(1) or exit(EXIT_FAILURE)

2)退出(1)或退出(EXIT_FAILURE)

exit(1) is used to terminate the program by specifying the successful execution of the program.

exit(1)用于通過指定程序的成功執行來終止程序。

Syntax:

句法:

exit(1);

Example 1:

范例1:

Here, we are reading two numbers and then dividing them, if the second number is 0 then exiting the program using exit(1) and if the second number is not 0 then performing the division operation and exiting the program using exit(0).

在這里,我們正在讀取兩個數字,然后將它們相除,如果第二個數字為0,則使用exit(1)退出程序;如果第二個數字不為0,則執行除法運算,然后使用exit(0)退出程序。

// C++ program to demonstrate the // example of exit(0) and exit(1)#include <iostream> using namespace std;int main() {int a;int b;cout << "Enter divisor (value of a): ";cin >> a;cout << "Enter dividend (value of b): ";cin >> b;cout << "a: " << a << endl;cout << "b: " << b << endl;if (b == 0) {cout << "Dividend must not be 0..." << endl;// EXIT_FAILUREexit(1);}cout << a << "/" << b << " : " << (float)a / (float)b << endl;// EXIT_SUCCESSexit(0); }

Output:

輸出:

RUN 1: Enter divisor (value of a): 10 Enter dividend (value of b): 3 a: 10 b: 3 10/3 : 3.33333RUN 2: Enter divisor (value of a): 10 Enter dividend (value of b): 0 a: 10 b: 0 Dividend must not be 0...

Example 2:

范例2:

Here, we are opening a file in read-only mode, if the file doe not exit then exiting the program using exit(1), and file exists then closing it and exiting the program using exit(0).

在這里,我們以只讀模式打開文件,如果文件沒有退出,則使用exit(1)退出程序,并且文件存在,然后將其關閉并使用exit(0)退出程序。

// C++ program to demonstrate the // example of exit(0) and exit(1)#include <stdio.h> #include <stdlib.h>int main() {FILE* fPtr;// opening the filefPtr = fopen("hello1.txt", "r");// checking whether file exists or notif (fPtr == NULL) {printf("oops, file does not exist...");// EXIT_FAILUREexit(1);}printf("File opened successfully...");// clsoing the filefclose(fPtr);// EXIT_SUCCESSexit(0); }

Output:

輸出:

oops, file does not exist...

出口(0)和出口(1)之間的區別 (Difference between exit(0) and exit(1))

exit(0)exit(1)
It is used to terminate the program normally by specifying that operation is successful.It is also used to terminate the program normally by specifying that operation is not successful.
The syntax is, exit(0); The syntax is,
0 is the value of EXIT_SUCCESS. Thus, we can also use exit(EXIT_SUCCESS) instead of exit(0).1 is the value of EXIT_FAILURE. Thus, we can also use exit(EXIT_FAILURE) instead of exit(1).
exit(0) is fully portable.exit(1) is not fully portable.
exit(0) defines the clean exit without any error.exit(1) defines that there was an error in the program and the program is terminated.
退出(0) 出口(1)
通過指定操作成功,它通常用于終止程序。 通過指定操作不成功,它也可用于正常終止程序。
語法是 exit(0); 語法是
0是EXIT_SUCCESS的值。 因此,我們也可以使用exit(EXIT_SUCCESS)代替exit(0) 。 1是EXIT_FAILURE的值。 因此,我們也可以使用exit(EXIT_FAILURE)代替exit(1) 。
exit(0)是完全可移植的。 exit(1)并非完全可移植。
exit(0)定義干凈出口,沒有任何錯誤。 exit(1)定義程序中存在錯誤,并且程序終止。

翻譯自: https://www.includehelp.com/cpp-tutorial/exit-0-vs-exit-1-in-c-cpp-with-examples.aspx

exit c+

總結

以上是生活随笔為你收集整理的exit c+_C / C ++中的exit(0)vs exit(1)与示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。