Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

main() ควรคืนค่าอะไรใน C/C++

ค่าที่ main() คืนบ่งบอกสถานะการจบโปรแกรม: 0 = ปกติ (success) non-zero = error/abnormal

ใน C++ main() สามารถไม่เขียน return ได้ จะคืน 0 เริ่มต้น

ไวยากรณ์

int main() {
    ...
    return 0;
}

ตัวอย่าง

#include <stdio.h>
int main() {
    int a = 10;
    char b = 'S';
    float c = 2.88;
    a = a + b; // implicit conversion char to int (ASCII 'S'=83)
    printf("Implicit conversion from character to integer: %d\n", a);
    c = c + a; // implicit conversion int to float
    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() คืน 0 แสดงโปรแกรมรันสำเร็จ