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

Function Specifier ใน C คืออะไร

ใน C และ C++ มี Function Specifiers ใช้ระบุคุณสมบัติของฟังก์ชัน C++ มี inline specifier ใน C มี _Noreturn specifier ใช้บอกว่าฟังก์ชันนั้นจะไม่ return ค่ากลับ (โปรแกรมจะจบหรือ exit ภายในฟังก์ชัน)

ตัวอย่างปกติ

#include <stdio.h>

int myAdd(int a, int b) {
    return a + b;
}

int main() {
    int x = 10, y = 20;
    printf("The value is: %d\n", myAdd(x, y));
}

ผลลัพธ์

The value is: 30

ถ้าใช้ _Noreturn จะแสดง warning และโปรแกรมจบด้วย error

ตัวอย่างใช้ _Noreturn

#include <stdio.h>

_Noreturn int myAdd(int a, int b) {
    return a + b;
}

int main() {
    int x = 10, y = 20;
    printf("The value is: %d\n", myAdd(x, y));
}

ผลลัพธ์ (Warning)

[Warning] function declared 'noreturn' has a 'return' statement
[Warning] 'noreturn' function does return