main()在C/C++中應該返回什麼?
main()函式的返回值表示程式是如何退出的。程式正常退出時,返回值為零。如果程式碼有錯誤、故障等,將以非零的值退出。
在C++語言中,main()函式可以沒有返回值。預設情況下,它會返回零。
以下是C語言中main()函式的語法:
int main() {
….
return 0;
}以下是C語言中main()函式的一個示例:
示例
#include <stdio.h>
int main() {
int a = 10;
char b = 'S';
float c = 2.88;
a = a+b;
printf("Implicit conversion from character to integer : %d\n",a);
c = c+a;
printf("Implicit conversion from integer to float : %f\n",c);
return 0;
}輸出
Implicit conversion from character to integer : 93 Implicit conversion from integer to float : 95.879997
在上面的程式中,main函式有業務邏輯。有三個變數a、b和c,其中a儲存a和b的和。變數c儲存c和a的和。main函式返回0。
a = a+b;
printf("Implicit conversion from character to integer : %d\n",a);
c = c+a;
printf("Implicit conversion from integer to float : %f\n",c);
return 0;
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP