標準

1. Scope

    • 本標準旨在闡述如何解釋用 C 語言所撰寫的程式。

2. Normative references

3. Terms, definitions, and symbols

    • 本標準使用到的名詞、定義和符號。
      • 實參。調用函式時,所傳遞的參數。
      • external appearance or action
        • 標準所規範的行為 (behavior),著重在程序執行後,外部可觀察到的現象。
        • unspecified behavior where each implementation documents how the choice is made
          • 本標準並未規定,由實現 (即編譯器) 決定之行為。如果可以明確以文件表述行為,則屬此類; 否則,則歸類於 unspecified behavior。注意! implementation-defined behavior 並不都會改變程序執行的結果。如 register 關鍵字,只會影響到執行效能。
        • behavior that depends on local conventions of nationality, culture, and language that each implementation documents
          • 根據區域設置 (locale) 而會有不同的行為,如: islower
        • behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements
          • 本標準並未給出規定之行為。比如: 除以零。
        • 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) 並未規定,但不影響其最終結果。
      • unit of data storage in the execution environment large enough to hold an object that may have one of two values
        • 儲存裝置上 (一般為內存) 的一個區塊,其大小可以存下真假值。
      • addressable unit of data storage large enough to hold any member of the basic character set of the execution environment
        • 儲存裝置上 (一般為內存),可以定址的一個區塊,其大小可以存下 char。
      • 〈abstract〉 member of a set of elements used for the organization, control, or representation of data
        • 抽象的說法,字符是用來組織、控制和表示資料。
        • single-byte character 〈C〉 bit representation that fits in a byte
          • 對於 C 而言,一個 byte 可以表示的即為字符。
        • sequence of one or more bytes representing a member of the extended character set of either the source or the execution environment
          • 對擴展字符集中的字符,用一個或多個 byte 加以表示。
        • bit representation that fits in an object of type wchar_t, capable of representing any character in the current locale
          • 可以用 wchar_t 表示的本地字符。
        • multibyte character 算是早期用來表示非英語系的字符集,表示字符所需的 byte 數不固定; wide character 是後來用來表示 Unicode 字符集,表示字符所需的 byte 數固定。
      • region of data storage in the execution environment, the contents of which can represent values
        • object 為儲存裝置上的一個區塊 (一般為內存),其中的內容可以表示成值 (value)。其內容如何解釋成值,端賴於該 object 的型別 (type)。
      • 形參。宣告或定義函式時,寫在參數列的參數。
      • precise meaning of the contents of an object when interpreted as having a specific type

4. Conformance

    • 闡述什麼才是符合 (conformance) 本標準的 C 程序。對於編譯器而言,所有符合本標準的 C 程序必須通過編譯。

5. Environment

    • 編譯和執行 C 程式的環境。
    • 5.1 Conceptual models
      • 5.1.1 Translation environment
        • 編譯環境
      • 5.1.2 Execution environments
        • 執行環境
          • 於裸機 (無作業系統) 上執行 C 程式。程式的啟動和結束皆為 implementation-defined。
          • 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.
          • 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.
            • 循序點之前的所有副作用必須被執行完畢,循序點之後的所有副作用不能發生。因為副作用代表執行環境 (execution environment) 狀態上的改變,循序點可以被視為執行環境 (execution environment) 狀態上的穩定態。

6. Language

型別轉換

  • 小結:
    • integer promotion (整型提升)
      • 當函式形參型別未知,傳遞參數時,會進行整型提升。
      • 在算述表達式裡的類型轉換 (即一般算術轉換) 的過程中,會先對個別運算元進行整型提升,可能轉換成 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 (一般算術轉換)
        • 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。
    • 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.
      • 物件或是函式的返回值,其解釋由其型別決定。此為型別的用途所在。
        • 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
      • 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 階級關係有遞移性 (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。
      • 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 表示,則轉換後的值不變。
      • 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 整型。
    • 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.

未定義行為

其它

陷阱

附錄

外部連結


登录