* [[wp>ISO/IEC JTC 1/SC 22]] * ISO/IEC 聯合技術委員會 (Joint Technical Committee, JTC),小組委員會 (Subcommittee, SC),工作組 (Working Group, WG)。 * WG 14 負責 C 語言,WG 21 負責 C++ 語言。 * [[http://blog.codingnow.com/2010/01/the_new_c_standard_1_2.html|《The New C Standard》的新版下载]] * [[http://www.coding-guidelines.com/cbook/cbook1_2.pdf|The New C Standard]] * 第 157 頁,對照 [[http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf|C99]] 逐句 (sentence by sentence) 閱讀。 * [[http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1169.pdf|Embedded C standard]] * [[https://isocpp.org/std/the-standard|The Standard : Standard C++]] * C++ 標準對某些術語有給出較精確的解釋。 * [[http://www.waterlike.com.tw/bookdata.asp?NO=TP3C136043|征服 C 指針 (日)前橋和彌]] * [[http://eli.thegreenplace.net/2007/11/24/the-context-sensitivity-of-cs-grammar|The context sensitivity of C's grammar]] * [[http://trevorjim.com/c-and-cplusplus-are-not-context-free/|C and C++ are not context free]] * 注意! C 語言不是上下文無關的語法。 ====== 標準 ====== * [[http://c0x.coding-guidelines.com/|C0X: searching and table of contents]] * [[http://www.lysator.liu.se/c/ANSI-C-grammar-l.html|ANSI C grammar, Lex specification]] * [[http://www.lysator.liu.se/c/ANSI-C-grammar-y.html|ANSI C Yacc grammar]] ===== 1. Scope ===== * [[http://c0x.coding-guidelines.com/1..html|1. Scope]] * 本標準旨在闡述如何解釋用 C 語言所撰寫的程式。 ===== 2. Normative references ===== * [[http://c0x.coding-guidelines.com/2..html|2. Normative references]] * 本標準所參考到的其它標準。 ===== 3. Terms, definitions, and symbols ===== * [[http://c0x.coding-guidelines.com/3..html|3. Terms, definitions, and symbols]] * 本標準使用到的名詞、定義和符號。 * [[http://c0x.coding-guidelines.com/3.3.html|3.3 argument]] * 實參。調用函式時,所傳遞的參數。 * [[http://c0x.coding-guidelines.com/3.4.html|3.4 behavior]] * external appearance or action * 標準所規範的行為 (behavior),著重在程序執行後,外部可觀察到的現象。 * [[http://c0x.coding-guidelines.com/3.4.1.html|3.4.1 implementation-defined behavior]] * unspecified behavior where each implementation documents how the choice is made * 本標準並未規定,由實現 (即編譯器) 決定之行為。如果可以明確以文件表述行為,則屬此類; 否則,則歸類於 unspecified behavior。注意! implementation-defined behavior 並不都會改變程序執行的結果。如 register 關鍵字,只會影響到執行效能。 * [[http://c0x.coding-guidelines.com/3.4.2.html|3.4.2 locale-specific behavior]] * behavior that depends on local conventions of nationality, culture, and language that each implementation documents * 根據區域設置 ([[wp>Locale|locale]]) 而會有不同的行為,如: [[http://www.cplusplus.com/reference/cctype/islower/|islower]]。 * [[http://c0x.coding-guidelines.com/3.4.3.html|3.4.3 undefined behavior]] * behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements * 本標準並未給出規定之行為。比如: 除以零。 * [[http://c0x.coding-guidelines.com/3.4.4.html|3.4.4 unspecified behavior]] * use of an unspecified value, or other behavior where this International Standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance * 本標準提供兩種以上之可能之行為。帶有 undefined behavior 的 C 程式被視為不正確 (erroneous); 而帶有 unspecified behavior 的 C 程式被視為正確 (correct),只是行為不定。比如: 運算式 b + c 中的運算元,b 和 c 何者先被計算 (evaluate) 並未規定,但不影響其最終結果。 * [[http://c0x.coding-guidelines.com/3.5.html|3.5 bit]] * unit of data storage in the execution environment large enough to hold an object that may have one of two values * 儲存裝置上 (一般為內存) 的一個區塊,其大小可以存下真假值。 * [[http://c0x.coding-guidelines.com/3.6.html|3.6 byte]] * addressable unit of data storage large enough to hold any member of the basic character set of the execution environment * 儲存裝置上 (一般為內存),可以定址的一個區塊,其大小可以存下 char。 * [[http://c0x.coding-guidelines.com/3.7.html|3.7 character]] * 〈abstract〉 member of a set of elements used for the organization, control, or representation of data * 抽象的說法,字符是用來組織、控制和表示資料。 * [[http://c0x.coding-guidelines.com/3.7.1.html|3.7.1 character]] * single-byte character 〈C〉 bit representation that fits in a byte * 對於 C 而言,一個 byte 可以表示的即為字符。 * [[http://c0x.coding-guidelines.com/3.7.2.html|3.7.2 multibyte character]] * sequence of one or more bytes representing a member of the extended character set of either the source or the execution environment * 對擴展字符集中的字符,用一個或多個 byte 加以表示。 * [[http://c0x.coding-guidelines.com/3.7.3.html|3.7.3 wide character]] * bit representation that fits in an object of type wchar_t, capable of representing any character in the current locale * 可以用 wchar_t 表示的本地字符。 * [[https://msdn.microsoft.com/en-us/library/z207t55f.aspx|Multibyte and Wide Characters]] * multibyte character 算是早期用來表示非英語系的字符集,表示字符所需的 byte 數不固定; wide character 是後來用來表示 Unicode 字符集,表示字符所需的 byte 數固定。 * [[http://c0x.coding-guidelines.com/3.14.html|3.14 object]] * region of data storage in the execution environment, the contents of which can represent values * object 為儲存裝置上的一個區塊 (一般為內存),其中的內容可以表示成值 (value)。其內容如何解釋成值,端賴於該 object 的型別 (type)。 * [[http://c0x.coding-guidelines.com/3.15.html|3.15 parameter]] * 形參。宣告或定義函式時,寫在參數列的參數。 * [[http://c0x.coding-guidelines.com/3.17.html|3.17 value]] * precise meaning of the contents of an object when interpreted as having a specific type ===== 4. Conformance ===== * [[http://c0x.coding-guidelines.com/4..html|4. Conformance]] * 闡述什麼才是符合 (conformance) 本標準的 C 程序。對於編譯器而言,所有符合本標準的 C 程序必須通過編譯。 ===== 5. Environment ===== * [[http://c0x.coding-guidelines.com/5..html|5. Environment]] * 編譯和執行 C 程式的環境。 * 5.1 Conceptual models * 5.1.1 Translation environment * 編譯環境 * 5.1.2 Execution environments * 執行環境 * [[http://c0x.coding-guidelines.com/5.1.2.1.html|5.1.2.1 Freestanding environment]] * 於裸機 (無作業系統) 上執行 C 程式。程式的啟動和結束皆為 implementation-defined。 * [[http://c0x.coding-guidelines.com/5.1.2.2.html|5.1.2.2 Hosted environment]] * [[http://c0x.coding-guidelines.com/5.1.2.2.1.html|5.1.2.2.1 Program startup]] * 定義程式入口點為 main。 * [[http://c0x.coding-guidelines.com/5.1.2.2.2.html|5.1.2.2.2 Program execution]] * [[http://c0x.coding-guidelines.com/5.1.2.2.3.html|5.1.2.2.3 Program termination]] * 定義 main 執行結束之後的返回值。 * [[http://c0x.coding-guidelines.com/5.1.2.3.html|5.1.2.3 Program execution]] * The semantic descriptions in this International Standard describe the behavior of an abstract machine in which issues of optimization are irrelevant. * 本標準所述之語意,解釋一個抽象機器 (模型) 應有之行為,其中不牽涉到優化相關議題。 * Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. * [[wp>Side effect (computer science)]] * [[http://stackoverflow.com/questions/16221359/side-effects-in-c|Side effects in C]] * 最簡單的說法,副作用 (side effect) 改變了執行環境 (execution environment) 的狀態 (比如:賦値於某個變數)。 * At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place. * [[http://blog.tinlans.org/2010/08/06/sequence-point/|sequence point]][(http://lists.cs.uiuc.edu/pipermail/llvmdev/2011-April/039626.html)] * [[http://akaedu.github.io/book/ch16s03.html|3. Side Effect与Sequence Point]] * 循序點之前的所有副作用必須被執行完畢,循序點之後的所有副作用不能發生。因為副作用代表執行環境 (execution environment) 狀態上的改變,循序點可以被視為執行環境 (execution environment) 狀態上的穩定態。 ===== 6. Language ===== * 6. Language * 語言規範 * 6.1 Notation * 6.2 Concepts * [[http://c0x.coding-guidelines.com/6.2.2.html|6.2.2 Linkages of identifiers]] * An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage. * 將識別字和特定物件 (指佔有內存空間的變數) 或函式進行綁定的過程,稱為 linkage。基本上透過 extern 和 static 決定識別字的 linkage。此處的 linkage 為語言上的概念。 * 一般透過鏈結器實現 linkage。 * [[http://c0x.coding-guidelines.com/6.2.4.html|6.2.4 Storage durations of objects]] * An object has a storage duration that determines its lifetime. * 物件存在於存儲器 (一般指內存) 的生命週期。基本上透過 static 決定物件的 storage duration。 * If an object is referred to outside of its lifetime, the behavior is undefined. * 在物件合法的生命週期以外,存取該物件的行為被視為 undefined ([[http://stackoverflow.com/questions/4824342/returning-a-local-variable-from-function-in-c|returning a local variable from function in C]])。 * [[http://c0x.coding-guidelines.com/6.2.5.html|6.2.5 Types]] * The meaning of a value stored in an object or returned by a function is determined by the type of the expression used to access it. * 物件或是函式的返回值,其解釋由其型別決定。此為型別的用途所在。 * Types are partitioned into object types (types that fully describe objects), function types (types that describe functions), and incomplete types (types that describe objects but lack information needed to determine their sizes). * 基本型別分為底下三類: * object type: 變數。 * function type: 函式。 * incomplete type: 大小不明的型別。 * Any number of derived types can be constructed from the object, function, and incomplete types, as follows: * 衍生型別 (derived type) 可以由 object、function 和 incomplete type 透過以下方式生成: * array type * structure type * union type * function type * pointer type * A pointer type may be derived from a function type, an object type, or an incomplete type, called the referenced type. * T * 為 pointer type,其中的 T 被稱為 referenced type (注意字尾的 ed,代表被參考到的)。這裡所提及的 referenced type 不同於 C++ 中的 reference type。 * 6.2.6 Representations of types * 定義各型別基本的表達 (實現) 方式。 * [[http://c0x.coding-guidelines.com/6.3.html|6.3 Conversions]] * 型別轉換規則。 * 6.3.1 Arithmetic operands * 6.3.2 Other operands * [[http://c0x.coding-guidelines.com/6.3.2.1.html|6.3.2.1 Lvalues, arrays, and function designators]] * An lvalue is an expression with an object type or an incomplete type other than void; * 左值 (lvalue) 可以透過 & 取得其位址。 * [[http://c0x.coding-guidelines.com/6.3.2.2.html|6.3.2.2 void]] * The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, and implicit or explicit conversions (except to void) shall not be applied to such an expression. * void 表達式 (比如返回 void 的函式調用) 不可做任何用途。 * [[http://c0x.coding-guidelines.com/6.3.2.3.html|6.3.2.3 Pointers]] * A pointer to void may be converted to or from a pointer to any incomplete or object type. * void * 可以被轉型成 T *。 * [[http://c0x.coding-guidelines.com/6.4.html|6.4 Lexical elements]] * 定義前處理器和編譯器需要處理的符號 (token)。 * [[http://c0x.coding-guidelines.com/6.4.4.html|6.4.4 Constants]] * The value of a constant shall be in the range of representable values for its type. * 可以這樣理解,如果無後綴的整數常量,其值超過 long long int (整數型別中最大者) 所能表示的範圍,則視作違反此條規則。 * [[http://c0x.coding-guidelines.com/6.4.4.1.html|6.4.4.1 Integer constants]] * The type of an integer constant is the first of the corresponding list in which its value can be represented. * 十進位且無後綴的整數常量,其型別視該整數常量從 int、long int 和 long long 中挑選。 * [[http://c0x.coding-guidelines.com/6.4.4.2.html|6.4.4.2 Floating constants]] * An unsuffixed floating constant has type double. * [[http://c0x.coding-guidelines.com/6.4.4.3.html|6.4.4.3 Enumeration constants]] * An identifier declared as an enumeration constant has type int. * [[http://c0x.coding-guidelines.com/6.4.4.4.html|6.4.4.4 Character constants]] * An integer character constant has type int. * 字元常量其型別為 int。 * If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int. int i = '\xFF'; // 底層實現 (即編譯器) 預設為 signed char。 printf("sizeof: %d", i ); // 輸出 -1。 * [[http://c0x.coding-guidelines.com/6.5.html|6.5 Expressions]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#expression)] * An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. * 表達式由運算子和運算元組成。 * [[http://c0x.coding-guidelines.com/6.5.1.html|6.5.1 Primary expressions]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#primary-expression)] * 以下的順序依照優先級,由大至小 ([[http://en.cppreference.com/w/c/language/operator_precedence|C Operator Precedence]])。 * [[http://c0x.coding-guidelines.com/6.5.2.html|6.5.2 Postfix operators]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#postfix-expression)] * 陣列索引和函式調用屬於此類。 * [[http://c0x.coding-guidelines.com/6.5.2.1.html|6.5.2.1 Array subscripting]] * [[http://c0x.coding-guidelines.com/6.5.2.2.html|6.5.2.2 Function calls]] * [[http://c0x.coding-guidelines.com/6.5.2.3.html|6.5.2.3 Structure and union members]] * [[http://c0x.coding-guidelines.com/6.5.2.4.html|6.5.2.4 Postfix increment and decrement operators]] * [[http://c0x.coding-guidelines.com/6.5.2.5.html|6.5.2.5 Compound literals]] * [[http://c0x.coding-guidelines.com/6.5.3.html|6.5.3 Unary operators]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#unary-expression)] * [[http://c0x.coding-guidelines.com/6.5.3.1.html|6.5.3.1 Prefix increment and decrement operators]] * [[http://c0x.coding-guidelines.com/6.5.3.2.html|6.5.3.2 Address and indirection operators]] * address operator: & * indirection operator: * * [[http://c0x.coding-guidelines.com/6.5.3.3.html|6.5.3.3 Unary arithmetic operators]] * [[http://c0x.coding-guidelines.com/6.5.3.4.html|6.5.3.4 The sizeof operator]] * [[http://c0x.coding-guidelines.com/6.5.4.html|6.5.4 Cast operators]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#cast-expression)] * [[http://c0x.coding-guidelines.com/6.5.5.html|6.5.5 Multiplicative operators]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#multiplicative-expression)] * * / % * [[http://c0x.coding-guidelines.com/6.5.6.html|6.5.6 Additive operators]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#additive-expression)] * + - * [[http://c0x.coding-guidelines.com/6.5.7.html|6.5.7 Bitwise shift operators]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#shift-expression)] * >> << * [[http://c0x.coding-guidelines.com/6.5.8.html|6.5.8 Relational operators]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#relational-expression)] * > < >= <= * [[http://c0x.coding-guidelines.com/6.5.9.html|6.5.9 Equality operators]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#equality-expression)] * [[http://c0x.coding-guidelines.com/6.5.10.html|6.5.10 Bitwise AND operator]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#and-expression)] * [[http://c0x.coding-guidelines.com/6.5.11.html|6.5.11 Bitwise exclusive OR operator]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#exclusive-or-expression)] * [[http://c0x.coding-guidelines.com/6.5.12.html|6.5.12 Bitwise inclusive OR operator]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#inclusive-or-expression)] * [[http://c0x.coding-guidelines.com/6.5.13.html|6.5.13 Logical AND operator]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#logical-and-expression)] * [[http://c0x.coding-guidelines.com/6.5.14.html|6.5.14 Logical OR operator]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#logical-or-expression)] * [[http://c0x.coding-guidelines.com/6.5.15.html|6.5.15 Conditional operator]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#conditional-expression)] * ? : * [[http://c0x.coding-guidelines.com/6.5.16.html|6.5.16 Assignment operators]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#assignment-expression)] * = *= /= %= += -= <<= >>= &= ^= |= * [[http://c0x.coding-guidelines.com/6.5.17.html|6.5.17 Comma operator]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#expression)] * The left operand of a comma operator is evaluated as a void expression; * comma 運算子的左運算元,其運算結果最後會被捨棄。 * [[http://c0x.coding-guidelines.com/6.6.html|6.6 Constant expressions]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#constant-expression)] * A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be. * 編譯時期可以計算出值的表達式,稱為 constant expression。constant expression 可以出現在需要常量 (constant) 的地方。 * [[http://c0x.coding-guidelines.com/6.7.html|6.7 Declarations]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#declaration)] * The declaration specifiers consist of a sequence of specifiers that indicate the linkage, storage duration, and part of the type of the entities that the declarators denote. * declaration specifier 包含 specifier (指示符)[(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#type-specifier)] 和 qualifier (限定符)[(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#type-qualifier)],一般寫法如下: // storage-class-specifier type-qualifier type-specifier. static const int * declarator (宣告子)[(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#declarator)] * [[http://c0x.coding-guidelines.com/6.7.1.html|6.7.1 Storage-class specifiers]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#storage-class-specifier)] * typedef、extern、static、auto 和 register。 * [[http://c0x.coding-guidelines.com/6.7.2.html|6.7.2 Type specifiers]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#type-specifier)] * void、char、short、int、long、float、double、signed 和 unsigned。 * [[http://c0x.coding-guidelines.com/6.7.2.1.html|6.7.2.1 Structure and union specifiers]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#struct-or-union-specifier)] * [[http://c0x.coding-guidelines.com/6.7.2.2.html|6.7.2.2 Enumeration specifiers]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#enum-specifier)] * [[http://c0x.coding-guidelines.com/6.7.3.html|6.7.3 Type qualifiers]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#type-qualifier)] * const、restrict 和 volatile。 * [[http://c0x.coding-guidelines.com/6.7.4.html|6.7.4 Function specifiers]] * inline * [[http://c0x.coding-guidelines.com/6.7.5.html|6.7.5 Declarators]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#declarator)] * [[http://c0x.coding-guidelines.com/6.7.5.1.html|6.7.5.1 Pointer declarators]] * [[http://c0x.coding-guidelines.com/6.7.5.2.html|6.7.5.2 Array declarators]] * [[http://c0x.coding-guidelines.com/6.7.5.3.html|6.7.5.3 Function declarators (including prototypes)]] * [[http://c0x.coding-guidelines.com/6.7.6.html|6.7.6 Type names]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#type-name)] * 由一般型別、指針、陣列和函式構成的型別。 * [[http://c0x.coding-guidelines.com/6.7.7.html|6.7.7 Type definitions]] * typedef * [[http://c0x.coding-guidelines.com/6.7.8.html|6.7.8 Initialization]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#initializer)] * designator (指定子),即等號左邊的識別字。 typedef struct MY_TYPE { boolean flag; short int value; double stuff; } MY_TYPE; void function(void) { MY_TYPE a = { .flag = true, .value = 123, .stuff = 0.456 }; ^^^^^ designator } * [[http://stackoverflow.com/questions/330793/how-to-initialize-a-struct-in-ansi-c|How to initialize a struct in ANSI C]] * [[http://c0x.coding-guidelines.com/6.8.html|6.8 Statements and blocks]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#statement)] * A statement specifies an action to be performed. * 述句代表所要做的動作,可以是單純的表達式述句,或是控制流述句。 * A block allows a set of declarations and statements to be grouped into one syntactic unit. * compound statement 即為一個 block,語法上視作一個單元。 * [[http://c0x.coding-guidelines.com/6.8.1.html|6.8.1 Labeled statements]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#labeled-statement)] * 用於 goto 或是 switch 述句。 * [[http://c0x.coding-guidelines.com/6.8.2.html|6.8.2 Compound statement]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#compound-statement)] * [[http://c0x.coding-guidelines.com/6.8.3.html|6.8.3 Expression and null statements]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#expression-statement)] * 表達式 (expression) 後接分號 (;),即為表達式述句。 * [[http://c0x.coding-guidelines.com/6.8.4.html|6.8.4 Selection statements]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#selection-statement)] * 即 if-else 和 switch 述句。 * A selection statement selects among a set of statements depending on the value of a controlling expression. * controlling expression 即 if 和 switch 中的 conditional expression。 * [[http://c0x.coding-guidelines.com/6.8.4.1.html|6.8.4.1 The if statement]] * [[http://c0x.coding-guidelines.com/6.8.4.2.html|6.8.4.2 The switch statement]] * [[http://c0x.coding-guidelines.com/6.8.5.html|6.8.5 Iteration statements]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#iteration-statement)] * 即 while、do-while 和 for 述句。 * [[http://c0x.coding-guidelines.com/6.8.5.1.html|6.8.5.1 The while statement]] * [[http://c0x.coding-guidelines.com/6.8.5.2.html|6.8.5.2 The do statement]] * [[http://c0x.coding-guidelines.com/6.8.5.3.html|6.8.5.3 The for statement]] * [[http://c0x.coding-guidelines.com/6.8.6.html|6.8.6 Jump statements]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#jump-statement)] * 即 goto、continue、break 和 return 述句。 * [[http://c0x.coding-guidelines.com/6.8.6.1.html|6.8.6.1 The goto statement]] * [[http://c0x.coding-guidelines.com/6.8.6.2.html|6.8.6.2 The continue statement]] * [[http://c0x.coding-guidelines.com/6.8.6.3.html|6.8.6.3 The break statement]] * [[http://c0x.coding-guidelines.com/6.8.6.4.html|6.8.6.4 The return statement]] * [[http://c0x.coding-guidelines.com/6.9.html|6.9 External definitions]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#external-declaration)] * There shall be no more than one external definition for each identifier declared with internal linkage in a translation unit. * 經過前處理 (preprocess) 之後的文本,即為 translation unit。 * [[http://c0x.coding-guidelines.com/6.9.1.html|6.9.1 Function definitions]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#function-definition)] function-definition: declaration-specifiers declarator declaration-listopt compound-statement ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^ void foo(int i) { ... } * [[http://c0x.coding-guidelines.com/6.9.2.html|6.9.2 External object definitions]][(http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#declaration)] * If the declaration of an identifier for an object has file scope and an initializer, the declaration is an external definition for the identifier. * 有初始化的變數宣告 (declaration),即為定義 (definition)。 * A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. * 未初始化的全域變數,即為 tentative definition。 int gFoo; // tentative definition static int gBAR; // tentative definition * [[http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka16744.html|How does the compiler handle multiple definitions of global variables?]] * [[http://stackoverflow.com/questions/3095861/about-tentative-definition|About Tentative definition]] * [[http://c0x.coding-guidelines.com/6.10.html|6.10 Preprocessing directives]] * 定義前處理器指示符。 ====== 型別轉換 ====== * [[http://akaedu.github.io/book/ch15.html|15. 数据类型详解]] * [[http://akaedu.github.io/book/ch15s01.html|1. 整型]] * [[http://akaedu.github.io/book/ch15s02.html|2. 浮点型]] * [[http://akaedu.github.io/book/ch15s03.html|3. 类型转换]] * 小結: * integer promotion (整型提升) * [[http://c0x.coding-guidelines.com/6.3.1.1.html|6.3.1.1 Boolean, characters, and integers]] * 當函式形參型別未知,傳遞參數時,會進行整型提升。 * 在算述表達式裡的類型轉換 (即一般算術轉換) 的過程中,會先對個別運算元進行整型提升,可能轉換成 int 或是 unsigned int。 unsigned char c1 = 255, c2 = 2; int n = c1 + c2; ^^ ^^ unsigned char unsigned char | | int int * If an int can represent all values of the original type, the value is converted to an int; * 如果運算元皆可被 int 所表示,則其型別皆轉換至 int; 若否,轉換至 unsigned int。(整型提升) * If both operands have the same type, then no further conversion is needed. * 進行過整型提升之後,若運算元型別相同,型別轉換終止。(一般算術轉換) * usual arithmetic conversion (一般算術轉換) * [[http://c0x.coding-guidelines.com/6.3.1.8.html|6.3.1.8 Usual arithmetic conversions]] * Otherwise, the integer promotions are performed on both operands. * 在此條規則之前,優先考慮運算元為浮點型別的情況。此條規則針對運算元皆為整數型別的情況,這時,整型提升 (integer promotion) 參與規則。 * Then the following rules are applied to the promoted operands: * 以下規則應用在已進行過整型提升的運算元。 * If both operands have the same type, then no further conversion is needed. * 進行過整型提升之後,若運算元型別相同,型別轉換終止; 若否,則依照整型 rank 的規則,做進一步的型別轉換。 * 基本上是從低 rank 轉換至高 rank。 * [[http://c0x.coding-guidelines.com/6.2.5.html|6.2.5 Types]] * The meaning of a value stored in an object or returned by a function is determined by the type of the expression used to access it. * 物件或是函式的返回值,其解釋由其型別決定。此為型別的用途所在。 * [[wp>C data types]] * arithmetic types: 算術型別 * integer types: 整數型別 * floating types: 浮點型 * scalar types: 標 (純) 量型別 * arithmetic types * pointer types * aggregate types: 聚合型別 (不包含 union types,因為 union types 同一時間只能取得其中一個成員) * arrary types * struct types * 6.2.6 Representations of types * [[http://c0x.coding-guidelines.com/6.2.6.1.html|6.2.6.1 General]] * [[http://c0x.coding-guidelines.com/6.2.6.2.html|6.2.6.2 Integer types]] * [[http://c0x.coding-guidelines.com/6.2.7.html|6.2.7 Compatible type and composite type]] * [[http://c0x.coding-guidelines.com/6.3.html|6.3 Conversions]] * [[http://c0x.coding-guidelines.com/6.3.1.1.html|6.3.1.1 Boolean, characters, and integers]] * Every integer type has an integer conversion rank defined as follows: * integer conversion rank 定義整型之間的階級關係,此關係決定 usual arithmetic conversion (一般算術轉換) 的結果。 * No two signed integer types shall have the same rank, even if they have the same representation. * 任意兩個 signed integer type 不可以有相同的階級,即使兩者在編譯器內部皆使用相同實現 (即佔用相同的 bit 數)。 * The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision. * 一個 signed integer type 的階級必須大於任一個其精準度小於前者的其它 signed integer type。 * The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char. * long long int > long int > int > short int > signed char * char 是否為 signed 或是 unsigned 由編譯器內部實現決定,所以這裡必須指明是 signed char。 * The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any. * 任一個 unsigned integer type,其階級必須等同於對應的 signed integer type。 * The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width. * 標準所定義的 integer type,其階級必須大於編譯器自行擴展且有相同長度的 extended integer type。 * The rank of char shall equal the rank of signed char and unsigned char. * char、signed char 和 unsigned char 皆有相同階級。 * The rank of _Bool shall be less than the rank of all other standard integer types. * _Bool 的階級必須小於所有其它標準所定義的 integer type。 * The rank of any enumerated type shall equal the rank of the compatible integer type (see 6.7.2.2). * 任何 enumerated type 其階級必須等同於對應相容的 integer type。 * For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 has greater rank than T3. * integer type 階級關係有遞移性 ([[wp>Transitive relation|transitive]])。 * If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int.These are called the integer promotions. All other types are unchanged by the integer promotions. * 如果表達式中的運算元,其値落在 int 可表達範圍之內,則轉型成 int; 若否,則轉型成 unsigned int。前述兩種情況皆稱為 integer promotion (整型提升)。 * The integer promotions are applied only: as part of the usual arithmetic conversions, to certain argument expressions, to the operands of the unary +, -, and ~ operators, and to both operands of the shift operators, as specified by their respective subclauses. * 說明 integer promotion 發生時機。注意! 有些情況下,不施行 integer promotion。 * [[http://c0x.coding-guidelines.com/6.3.1.3.html|6.3.1.3 Signed and unsigned integers]] * When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged. * 一整型 T1 轉換至另一整型 T2 ,若原值可以被 T2 表示,則轉換後的值不變。 * [[http://c0x.coding-guidelines.com/6.3.1.8.html|6.3.1.8 Usual arithmetic conversions]] * Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. * usual arithmetic conversion 主要決定運算結果之型別,並視情況轉換運算元之型別,以符合運算結果之型別。 * Otherwise, the integer promotions are performed on both operands. * 如果運算元皆為整型,則先做 integer promotion。 * If both operands have the same type, then no further conversion is needed. * 經過 integer promotion 之後,如果運算元皆為相同型別,則結束。 * Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank. * 如果運算元其整型皆為 signed 或是 unsigned,rank 較小的一方轉換成 rank 較大的一方之型別。 * Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type. * 如果運算元中,unsigned 整型的 rank 大於等於另一個 signed 整型,則後者轉型成前者。 * Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type. * 如果運算元中,signed 整型可以表達 unsigned 整型,則後者轉型成前者。 * Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type. * 如果運算元中,signed 整型無法表達 unsigned 整型,則兩者皆轉型成 signed 整型相對應的 unsigned 整型。 * [[http://c0x.coding-guidelines.com/6.5.html|6.5 Expressions]] * Some operators (the unary operator ~, and the binary operators <<, >>, &, ^, and |, collectively described as bitwise operators) are required to have operands that have integer type. These operators yield values that depend on the internal representations of integers, and have implementation-defined and undefined aspects for signed types. * [[https://www.securecoding.cert.org/confluence/display/c/INT02-C.+Understand+integer+conversion+rules|INT02-C. Understand integer conversion rules]] * [[https://www.iar.com/Support/resources/articles/the-behavior-of-integral-types/|The behavior of integral types]] * [[http://www.informit.com/articles/article.aspx?p=686170|C Language Issues for Application Security]] ====== 未定義行為 ====== * [[http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html|What Every C Programmer Should Know About Undefined Behavior #1/3]] * [[http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html|What Every C Programmer Should Know About Undefined Behavior #2/3]] * [[http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html|What Every C Programmer Should Know About Undefined Behavior #3/3]] * [[http://blog.tinlans.org/2010/08/06/sequence-point/|sequence point]] * [[http://akaedu.github.io/book/ch16s03.html|3. Side Effect与Sequence Point]] * GCC 開啟 ''-Wall'' 和 ''-Wextra''。 * [[https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Warning-Options.html|3.8 Options to Request or Suppress Warnings]] * [[http://developerblog.redhat.com/2014/10/16/gcc-undefined-behavior-sanitizer-ubsan/|GCC Undefined Behavior Sanitizer]] ====== 其它 ====== * [[http://stackoverflow.com/questions/11438794/is-the-size-of-c-int-2-bytes-or-4-bytes|Is the size of C “int” 2 bytes or 4 bytes?]] * C 標準只規範 int 可以表示的最大和最小值,因此 int 的大小至少為 2 byte。 * [[http://stackoverflow.com/questions/19260052/sign-extension-in-c|sign extension in C]] * [[https://gcc.gnu.org/onlinedocs/gcc/C-Implementation.html|4 C Implementation-Defined Behavior]] * C 在 GCC 上的 implementation-defined。 * [[https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html|Integers implementation]] ====== 陷阱 ====== * [[http://stackoverflow.com/questions/50605/signed-to-unsigned-conversion-in-c-is-it-always-safe|Signed to unsigned conversion in C - is it always safe?]] * [[http://luse.blogspot.tw/2009/05/signedsigned.html|Signed不Signed就是問題所在]] unsigned int u = 1234; int i = -5678; unsigned int result = u + i; // i 會被轉換成 unsigned int,其值不再是負數。 * [[https://www.securecoding.cert.org/confluence/display/c/FLP30-C.+Do+not+use+floating-point+variables+as+loop+counters|FLP30-C. Do not use floating-point variables as loop counters]] * [[http://stackoverflow.com/questions/16595668/any-risk-of-using-float-variables-as-loop-counters-and-their-fractional-incremen|Any risk of using float variables as loop counters and their fractional increment/decrement for non “==” conditions?]] * [[http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html|What Every Computer Scientist Should Know About Floating-Point Arithmetic]] * [[http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c|Are pointers and arrays equivalent in C?]] * 指針和陣列在 C 中是不等價的 ([[http://c-faq.com/aryptr/aryptr2.html|Question 6.2]])。兩者可通用的地方,在於陣列索引 (array indexing) 和指針運算 (pointer arithmetic) ([[http://c-faq.com/aryptr/aryptrequiv.html|Question 6.3]]),和參數傳遞 ([[http://c-faq.com/aryptr/aryptrparam.html|Question 6.4]])。 * [[https://www.eskimo.com/~scs/cclass/int/sx9.html|Chapter 23: Two-Dimensional (and Multidimensional) Arrays]] * [[http://stackoverflow.com/questions/4648914/why-we-have-both-jagged-array-and-multidimentional-array|Why we have both jagged array and multidimentional array?]] * C 只支持 array of arrays (陣列的陣列),不支援 multidimensional array (多維陣列)。 ====== 附錄 ====== * A1: Lexer * [[http://www.lysator.liu.se/c/ANSI-C-grammar-l.html|ANSI C grammar, Lex specification]] * A2: Parser * [[http://www.lysator.liu.se/c/ANSI-C-grammar-y.html|ANSI C Yacc grammar]] ====== 外部連結 ====== * [[http://c-faq.com/index.html|comp.lang.c FAQ list]] * [[https://www.securecoding.cert.org/confluence/display/c/SEI+CERT+C+Coding+Standard|SEI CERT C Coding Standard]]