gcc支持的C语言标准(-std选项)

gcc支持多种版本的C标准,比如C90(ISO1990)、C99(ISO 1999)和C11(ISO2011),除此之外,GCC还会支持一些GCC特有的扩展。分别是:


gnu89/gnu90: ISO C90 + GCC extension
gnu99: ISO C99 + GCC extension
gnu11: ISO C11 + GCC extension

支持的C语言标准可以使用-std 进行选择,常用的(非全部)选项如下:


-std=c11                    Conform to the ISO 2011 C standard
-std=c89                    Conform to the ISO 1990 C standard
-std=c90                    Conform to the ISO 1990 C standard
-std=c99                    Conform to the ISO 1999 C standard


-std=gnu11                  Conform to the ISO 2011 C standard with GNU extensions
-std=gnu89                  Conform to the ISO 1990 C standard with GNU extensions
-std=gnu90                  Conform to the ISO 1990 C standard with GNU extensions
-std=gnu99                  Conform to the ISO 1999 C standard with GNU extensions

默认情况下如果不指明-std选项,GCC会使用-std=gnu11作为默认支持的C语言版本,也就是C11标准加上GCC extension的组合。

C标准从C95开始引入了一个宏 __STDC_VERSION__ 来表示当前支持的ISO C语言标准版本。比如在我的平台上使用gcc可以查看默认使用的C标准:


gcc -E -dM - </dev/null

结果是gcc默认使用的C11标准,实际上这个宏只是表示ISO的标准,GCC实际默认使用的是gnu11(C11 + GCC extension)如下所示:


#define __STDC_VERSION__ 201112L

可以使用如下命令查看gcc 使用c99时对应的宏:


gcc -std=c99 -E -dM - </dev/null

结果不出所料,为199901L,如下所示:


 #define __STDC_VERSION__ 199901L

注意这个宏只在C99以及之后版本才会有,C90标准制定时还没有定义该宏,所以使用-std=c90是看不到该宏的。

发布了234 篇原创文章 · 获赞 78 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/rikeyone/article/details/95970499