簡體   English   中英

C 標准在哪里定義 else if 語句?

[英]Where does the C standard define the else if statement?

我只是對 C 中的else if語句感到好奇,所以我查看了C99標准,但一無所獲。 然后我看了語法,但又沒有else if

selection_statement
    : IF '(' expression ')' statement
    | IF '(' expression ')' statement ELSE statement
    | SWITCH '(' expression ')' statement
    ;

else if在 C 中聲明, else if是怎樣的。通過閱讀標准,我怎么能注意到它是標准的一部分?

C 2018 6.8.4 說選擇語句可能是“ if ( expression ) statement else statement ”。 C 2018 6.8 說后一個語句可能是“ selection-statement ”,因此它可能是ifif … else語句,這導致語句包含else if

通過聲明語法, else if語句被正式定義為 §6.8.4/1, C18 中if語句的副產品:

句法

1 選擇聲明:

if ( expression ) statement

if ( expression ) statement else statement

來源:C18,第 6.8.4/1 節

后一種形式中的最后一個“語句”描述了另一個if語句可以跟在else


除此之外,您可以在 G.5.1/8 處的代碼示例中的規范性附錄 G 中找到它在 C 標准中使用的一個(當然是非規范性的)示例:

if (isnan(x) && isnan(y)) { ... }
else if ((isinf(a) ||isinf(b)) && isfinite(c) && isfinite(d)) { ... }
else if ((logbw == INFINITY) && isfinite(a) && isfinite(b)) { ... } 

這是在 C18 標准中唯一出現else if語句的else if

所以關於:

通過閱讀標准,我怎么能注意到它是其中的一部分?

至少,盡管示例是非規范的,但它是其中的一部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM