exit
在/usr/include/stdlib.h中,C語言定義了2種常規退出狀態(exit status),分別是EXIT_FAILURE和EXIT_SUCCESS:
...
/* We define these the same for all machines.
Changes from this to the outside world should be done in `_exit'. */
#define EXIT_FAILURE 1 /* Failing exit status. */
#define EXIT_SUCCESS 0 /* Successful exit status. */
...
從網絡引用如下示例,如果不存在data.txt文件,則退出狀態爲失敗,退出值爲1;如果存在data.txt文件,則輸出“Normal Return”,退出狀態爲成功,退出值爲0。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp = fopen("data.txt","r");
if (fp == NULL)
{
fprintf(stderr,"fopen() failed in file %s at line # %d\n", __FILE__,__LINE__);
exit(EXIT_FAILURE);
}
/* 正常進程持續至此。 */
fclose(fp);
printf("Normal Return\n");
return EXIT_SUCCESS;
}
用戶可以自定義不同的退出值,但是要注意取值範圍,取值範圍爲0~255,超過這個範圍,就會截取二進制退出值的低8位字符,可以理解爲最終得到退出值與256的餘數。示例如下,當設置返回值爲256時,實際卻返回0:
exit_256.c
#include<stdlib.h>
int main(void)
{
exit(256);
}
編譯執行
# gcc exit_256.c -o exit_256
# ./exit_256
# echo $?
0
一般慣例是保留128及更高的狀態值,以用於特殊目的。值128用於指示在子過程中無法執行另一個程序。該約定只是建議,非必須遵守。
return
在C語言中,函數定義包含返回類型、函數名和參數,如下圖所示:
根據是否需要返回值與是否需要參數,可以將函數分爲4類:無參數無返回值、有參數無返回值、無參數有返回值和有參數有返回值。
當函數返回類型爲void時,函數無返回值,函數定義中無需使用return語句;當函數返回類型不爲void時,函數有返回值,函數定義中需要使用return語句,並且return類型必須與函數返回類型一致。下面示例來自網站GeeksforGeeks,在示例中,Print()函數返回void類型的空值,所以不適合使用return語句,如果使用,return後不應該有值,否則會報錯。
// C code to show using return
// statement in void return type function
#include <stdio.h>
// void method
void Print()
{
printf("Welcome to GeeksforGeeks");
// void method using the return statement
return;
}
// Driver method
int main()
{
// Calling print
Print();
return 0;
}
如果將Print()函數中的return改爲exit,程序會在執行完Print()函數直接退出,不會跳轉回main函數。修改後的程序如下,執行程序時不打印Print()函數後的語句,程序退出值爲1:
#include <stdio.h>
#include <stdlib.h>
// void method
void Print()
{
printf("Now in Print function.\n");
// void method using the exit statement
exit(EXIT_FAILURE);
}
// Driver method
int main()
{
// Calling print
Print();
printf("Now in main function.\n");
return EXIT_SUCCESS;
}
return除了可以返回數值,還可以返回指針。根據網絡資料,編寫下面小程序,比較2個字符串的長短,輸出較短的字符串:
#include<stdio.h>
char *smallstr( char s1[], char s2[] )
{
int i;
i = 0;
while ( s1[i] != '\0' && s2[i] != '\0' )
i++;
if ( s1[i] == '\0' )
return ( s1 );
else
return ( s2 );
}
int main(void)
{
char char1[]="Hello python";
char char2[]="Hello C";
char *pointer=char1;
int number=0;
pointer=smallstr(char1,char2);
for(;pointer[number] != '\0';number++)
{
printf("%c",pointer[number]);
}
printf("\n");
}
編譯運行,可得到結果“Hello C”。
區別
個人總結exit和return的區別如下:
- 退出程度不同。雖然都是退出並返回值,但exit退出整個程序,return退出當前函數。
- 返回值類型不同。exit只返回簡單的數值,return稍複雜,可以返回數值、指針、結構體等格式。return返回結構體的示例,見C - Function returning structure。
參考文檔
[1]GNU.Exit Status[EB/OL].https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html,2020-01-01.
[2]Fruderica.EXIT_SUCCESS, EXIT_FAILURE[EB/OL].https://zh.cppreference.com/w/c/program/EXIT_status,2017-12-09.
[3]Chinmoy Lenka.return statement in C/C++ with Examples[EB/OL].https://www.geeksforgeeks.org/return-statement-in-c-cpp-with-examples/?ref=rp,2020-01-01.
[4]Shivani Baghel 1.C function argument and return values[EB/OL].https://www.geeksforgeeks.org/c-function-argument-return-values/,2020-01-01.
[5]corob-msft,Saisang,Mikejo5000,NextTurn,ghogen.Return Type[EB/OL].https://docs.microsoft.com/en-us/cpp/c-language/return-type?view=vs-2019,2016-11-04.