概述
一門 編程語言,不但包括對應的語法,還包括對應的編譯器(或解釋器)、對應的標准庫(或框架庫)和相關工具
對於C語言,我們 使用如下三要素學習它。
語法:C語言標准
編譯器(或解釋器):gcc
庫函數:標准要求的和gcc提供的。
GCC 對C語言標准的的支持情況
https://gcc.gnu.org/onlinedocs/gcc/Standards.html
2.1 C Language
The original ANSI C standard (X3.159-1989) was ratified in 1989 and published in 1990.
最初的ANSI C 標准(X3.159-1989)在1989年正式通過批准並在1990年發布。
This standard was ratified as an ISO standard (ISO/IEC 9899:1990) later in 1990.
這個標准在1990年下半年被批准成為一個國際標准(ISO/IEC 9899:1990)
There were no technical differences between these publications, although the sections of the ANSI standard were renumbered and became clauses in the ISO standard.
盡管ANSI 標准的章節被重新排序成為ISO標准,但是這些標准之間沒有技術方面的差異。
The ANSI standard, but not the ISO standard, also came with a Rationale document.
ANSI 標准還包括一個 Rationale document。
This standard, in both its forms, is commonly known as C89, or occasionally as C90, from the dates of ratification.
ANSI 標准 俗稱為 C89,有時也稱 C90.
To select this standard in GCC, use one of the options -ansi, -std=c90 or -std=iso9899:1990;
GCC 中,通過 下面選項 選擇 ANSI C (c89 c90 iso9899:1990標准) -ansi, -std=c90 或 -std=iso9899:1990;
to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings). See Options Controlling C Dialect.
如果想要 GCC 嚴格的按照選擇的 C語言標准來進行工作,你應該使用 -pedantic 選項,這樣不符合標准的語法會被報告為警告。(或者,如果你想要讓不符合 標准的語法被報告為錯誤,你應該使用 -pedantic-errors 選項)
Errors in the 1990 ISO C standard were corrected in two Technical Corrigenda published in 1994 and 1996. GCC does not support the uncorrected version.
Iso9899:1990 標准里面的錯誤,在隨后的 1994和1996發布的兩個技術勘誤中被更正。GCC 不支持 未更正的標准(即,GCC 里面指定C90標准,指的是1996年技術勘誤后的標准)
An amendment to the 1990 standard was published in 1995. This amendment added digraphs and __STDC_VERSION__
to the language, but otherwise concerned the library.
1995年發布了對C90標准的一個修訂標准。這份修訂標准增加了 雙字符組 和 __STDC_VERSION__
的宏定義。
關於 雙字符組和三字符串 等,參考: https://www.jb51.net/article/148250.htm 緣起 C語言的源程序的最低必須的字符集是基於7位ASCII碼字符集,是 ISO 646-1983 Invariant Code Set 的一個超集。ISO 646最初是1972年頒布的一項國際化的7位ASCII標准,規定了12個字符所對應的 碼位 保持對各國標准開放: # $ @ [ \ ] ^ ` { | } ~ 。 因此法國標准AFNOR NF Z 62010-1982把碼位0x7c(ASCII碼的 | )定義為ù,用法文鍵盤就難以輸入C語言的位或運算符 | ;碼位0x7e(ASCII碼的 ~)定義為 ¨ (即 分音符 ),法文鍵盤就難以輸入C語言的位非運算符 ~ 。 加拿大法語標准CSA Z243.4-1985中把碼位0x5e(ASCII碼的 ^ )在定義為É,導致難以輸入C語言的異或運算符 ^ 。 1994年公布了一項C語言標准的修正案,引入了更具有可讀性的5個雙字符組。這也包括進了 C99 標准
|
This amendment is commonly known as AMD1; the amended standard is sometimes known as C94 or C95. To select this standard in GCC, use the option -std=iso9899:199409 (with, as for other standard versions, -pedantic to receive all required diagnostics).
這個標准 俗稱未 AMD1;這個修訂標准有時 稱為 C94 或 C95。GCC上,使用 選項 -std=iso9899:199409 來選擇這個標准(和其他標准相同,使用 -pedantic 選項來讓不符合標准的語法報告為警告。
A new edition of the ISO C standard was published in 1999 as ISO/IEC 9899:1999, and is commonly known as C99.
新版標准在 1999年發布,稱為C99
(While in development, drafts of this standard version were referred to as C9X.)
(在這個標准的開發階段,這份標准的部分內容也被gcc所支持,GCC對這標准稱為C9X.)
GCC has substantially complete support for this standard version; see https://gcc.gnu.org/c99status.html for details.
GCC 基本上支持C99標准的所有內容。參見 https://gcc.gnu.org/c99status.html 獲得支持情況的詳細信息。
To select this standard, use -std=c99 or -std=iso9899:1999.
使用選項 -std=c99 或者-std=iso9899:1999來選擇這個標准。
Errors in the 1999 ISO C standard were corrected in three Technical Corrigenda published in 2001, 2004 and 2007. GCC does not support the uncorrected version.
C99 標准中的錯誤,在后面的2001、2004、2007年的三個技術勘誤中被修改。GCC 不支持未修改的標准(即指定-std=c99 實際上使用的是 2007年勘誤后的標准)
A fourth version of the C standard, known as C11, was published in 2011 as ISO/IEC 9899:2011. (While in development, drafts of this standard version were referred to as C1X.)
第四版本的C語言標准,C11在2011年發布。(在標准開發階段,GCC使用C1X代指這些標准)
GCC has substantially complete support for this standard, enabled with -std=c11 or -std=iso9899:2011.
GCC 基本上支持所有的C11標准。使用選項 -std=c11 或 -std=iso9899:2011 來選擇這個標准。
A version with corrections integrated was prepared in 2017 and published in 2018 as ISO/IEC 9899:2018; it is known as C17 and is supported with -std=c17 or -std=iso9899:2017;
對C11的更正在2017年集成好,並在2018年發布,稱為C17,通過選項 -std=c17 or -std=iso9899:2017 來選擇這個標准。
the corrections are also applied with -std=c11, and the only difference between the options is the value of __STDC_VERSION__
.
這些更正在使用選項 -std=c11 時,也會被應用。使用 -std=c11 和 -std=c17 的差別僅僅在 宏定義 __STDC_VERSION__ 不同。
A further version of the C standard, known as C2X, is under development; experimental and incomplete support for this is enabled with -std=c2x.
進一步的C標准,GCC代指為C2X 正在開發之中。實驗性的和不完整的支持,通過選項-std=c2x來選擇。
By default, GCC provides some extensions to the C language that, on rare occasions conflict with the C standard. See Extensions to the C Language Family.
通常,GCC 提供C語言的一些擴展,極少情況下,這些擴展和C語言標准有沖突。參見 Extensions to the C Language Family.
Some features that are part of the C99 standard are accepted as extensions in C90 mode, and some features that are part of the C11 standard are accepted as extensions in C90 and C99 modes.
C99 標准中的一些特性在GCC的 C90擴展模式下會被應用。C11標准中的一些特性在gcc的C99擴展模式下會被應用。
Use of the -std options listed above disables these extensions where they conflict with the C standard version selected.
當GCC擴展和C語言標准沖突時,使用上面的 -std 選項選擇對應的C語言標准來禁用擴展。
You may also select an extended version of the C language explicitly with -std=gnu90 (for C90 with GNU extensions), -std=gnu99 (for C99 with GNU extensions) or -std=gnu11 (for C11 with GNU extensions).
也可以 使用 下面選項,選擇一個gcc擴展版本的 C語言標准。
-std=gnu90選擇GCC擴展版本的C90標准。
std=gnu99選擇GCC 擴展版本的 C99 標准。
The default, if no C language dialect options are given, is -std=gnu17.
如果沒有選擇C語言標准,默認選擇是 -std=gnu17
The ISO C standard defines (in clause 4) two classes of conforming implementation.
C標准定義了兩種實現:hosted implementation和freestanding implementation
A conforming hosted implementation supports the whole standard including all the library facilities;
Hosted implementation 需要支持所有標准和(提供)所有的庫。
a conforming freestanding implementation is only required to provide certain library facilities:
freestanding implementation 只需要提供部分庫功能。
those in <float.h>
, <limits.h>
, <stdarg.h>
, and <stddef.h>
;
since AMD1, also those in <iso646.h>
; since C99, also those in <stdbool.h>
and <stdint.h>
; and since C11, also those in <stdalign.h>
and <stdnoreturn.h>
.
freestanding implemantion需要提供 <float.h> <limits.h> <stdargs.h> 和 <stddef.h>
AMD1 標准后,再增加一個 <iso646.h>
C99 標准后,增加<stdbool.h> 和 <stdint.h>
C11 標准后,增加 <stdalign.h> 和 <stdnoreturn.h>
In addition, complex types, added in C99, are not required for freestanding implementations.
另外,C99 中增加的 complex 數據類型,在freestanding implementation 中不要求實現。
The standard also defines two environments for programs,
標准也定義了兩種編程環境 freestanding environment 和hosted environment.
a freestanding environment, required of all implementations
freestanding environment 編程環境,兩種 implementation 都需要提供,
and which may not have library facilities beyond those required of freestanding implementations,
freestanding environment 編程環境中,可能沒有上述 freestanding implementation 規定之外的其他庫功能。
where the handling of program startup and termination are implementation-defined;
這種編程環境下,程序的啟動和結束,都是各自(編譯器)自己是決定如何實現。
and a hosted environment, which is not required,
hosted environment,是非必須提供給的(在freestanding implementation 下就不需要提供)
in which all the library facilities are provided and startup is through a function int main (void)
or int main (int, char *[])
.
在 hosted environment 下,所有標准定義的庫功能都被提供,程序啟動是通過 int main(void) 或 int main(int , char*[]) 。
An OS kernel is an example of a program running in a freestanding environment;
OS 內核是 freestanding environment 環境下運行的C語言程序的一個示例。
a program using the facilities of an operating system is an example of a program running in a hosted environment.
在操作系統上面運行的,使用操作系統提供的功能的程序是 hosted environment 環境下運行的C語言程序。
GCC aims towards being usable as a conforming freestanding implementation, or as the compiler for a conforming hosted implementation.
GCC 項目的目標是 成為一個可用的 freestanding implementation;或者作為一個 hosted implementation 下面的編譯器(工具)。
By default, it acts as the compiler for a hosted implementation, defining __STDC_HOSTED__
as 1
and presuming that when the names of ISO C functions are used, they have the semantics defined in the standard.
默認情況下,GCC 作為 hosted implementation 下面的編譯器來工作。並且定義宏 __STDC_HOSTED__ 為 1,當ISO C函數被使用時,它們具有標准中定義的語義。
To make it act as a conforming freestanding implementation for a freestanding environment, use the option -ffreestanding; it then defines __STDC_HOSTED__
to 0
and does not make assumptions about the meanings of function names from the standard library, with exceptions noted below.
為了讓GCC 作為 freestanding implementation 來工作,提供freestanding environment 編程環境,使用選項 -ffreestanding,GCC會將 宏 __STDC_HOSTED__
定義為 0 ,並且(對標准中定義的函數名)不會假設它具有標准中定義的語義。以下備注的情況例外。
To build an OS kernel, you may well still need to make your own arrangements for linking and startup. See Options Controlling C Dialect.
想要構建一個 OS 內核,你依然需要自己組織內核的連接和啟動。見Options Controlling C Dialect.
GCC does not provide the library facilities required only of hosted implementations,
GCC 不提供只在 hosted implementation中要求的函數。
nor yet all the facilities required by C99 of freestanding implementations on all platforms.
也不是在所有平台上面都提供 C99標准下的 freestanding implementation 規定的函數。
To use the facilities of a hosted environment, you need to find them elsewhere (for example, in the GNU C library). See Standard Libraries.
想要使用 hosted implementation 下定義的函數,你需要從別的庫中找到它們的實現。
Most of the compiler support routines used by GCC are present in libgcc, but there are a few exceptions.
大部分GCC 使用的編譯器支持函數通過libgcc來提供。有若干例外。
GCC requires the freestanding environment provide memcpy
, memmove
, memset
and memcmp
.
GCC 要求 freestanding environment 環境提供 memcpy memmove memset 和 memcmp 函數。
Finally, if __builtin_trap
is used, and the target does not implement the trap
pattern, then GCC emits a call to abort
.
最后,如果使用了 __builtin_trap函數,目標沒有實現trap 模型,GCC 生成一個 abort()的調用。 【可以盡管調用 __builtin_trap() 函數,但是gcc會自己處理,或產生軟件斷點,或產生abort() 調用】
C/C++調試技巧-debugbreak https://www.bilibili.com/read/cv1165694/
|
For references to Technical Corrigenda, Rationale documents and information concerning the history of C that is available online, see https://gcc.gnu.org/readings.html
技術修訂文檔,Rationale documents 和C語言的發展歷史相關的信息、見 https://gcc.gnu.org/readings.html