輸入限定符

型別限定符是描述有關型別的其他語義的關鍵字。它們是型別簽名的組成部分。它們既可以出現在宣告的最頂層(直接影響識別符號),也可以出現在子級別(僅與指標相關,影響指向的值):

關鍵詞 備註
const 防止宣告物件的突變(通過出現在最頂層)或防止指向值的突變(通過出現在指標子型別旁邊)。
volatile 通知編譯器宣告的物件(在最頂層)或指向的值(在指標子型別中)可能由於外部條件而改變其值,而不僅僅是程式控制流的結果。
restrict 優化提示,僅與指標相關。宣告意圖在指標的生命週期中,不會使用其他指標來訪問同一個指向的物件。

關於儲存類說明符(staticexternautoregister),型別修飾符(signedunsignedshortlong)和型別說明符(intchardouble 等)的型別限定符的順序未強制執行,但是好的做法是將它們按上述順序排列:

static const volatile unsigned long int a = 5; /* good practice */
unsigned volatile long static int const b = 5; /* bad practice */

頂級資格

/* "a" cannot be mutated by the program but can change as a result of external conditions */
const volatile int a = 5;

/* the const applies to array elements, i.e. "a[0]" cannot be mutated */    
const int arr[] = { 1, 2, 3 };

/* for the lifetime of "ptr", no other pointer could point to the same "int" object */
int *restrict ptr;

指標子型別資格

/* "s1" can be mutated, but "*s1" cannot */
const char *s1 = "Hello";

/* neither "s2" (because of top-level const) nor "*s2" can be mutated */
const char *const s2 = "World";

/* "*p" may change its value as a result of external conditions, "**p" and "p" cannot */
char *volatile *p;

/* "q", "*q" and "**q" may change their values as a result of external conditions */
volatile char *volatile *volatile q;