For faster navigation, this Iframe is preloading the Wikiwand page for C99.

C99

C99(之前名称为C9X)是ISO/IEC 9899:1999的非正式名称,是C语言标准的一个版本[1]。C99延伸了C90的内容,加入了C语言及标准函式库的新功能,让一些语言的实现(例如IEEE 754-1985英语IEEE_754-1985浮点数,以及编译器技术)可以更妥善的利用电脑的硬件[2]。后来在2011年发行的C11取代了此版本的C语言标准。

历史

ANSI的标准确立后,C语言的规范在一段时间内没有大的变动,然而C++在自己的标准化建立过程中继续发展壮大。《标准修正案一》在1994年为C语言建立了一个新标准,但是只修正了一些C89标准中的细节和增加更多更广的国际字符集支持。不过,这个标准引出了1999年ISO 9899:1999的发表。它通常被称为C99。C99被ANSI于2000年3月采用。

设计

在C99中包括的特性有:

  • 增加了对编译器的限制,比如源始码每行要求至少支持到 4095 字节,变量名函数名的要求支持到 63 字节(extern 要求支持到 31)。
  • 增强了预处理功能。例如:
    • 支持取可变参数 #define Macro(...) __VA_ARGS__
    • 使用的时候,允许省略参数,被省略的参数会被扩展成空串。
    • 支持//开头的单行注释(这个特性实际上在C89的很多编译器上已经被支持了)
  • 增加了新关键字restrict, inline, _Complex, _Imaginary, _Bool
    • 支持long long, long double _Complex, float _Complex 等类型
  • 支持不定长的数组,即数组长度可以在运行时决定,比如利用变量作为数组长度。声明时使用 int a[var] 的形式。不过考虑到效率和实现,不定长数组不能用在全局,或 structunion
  • 变量声明不必放在语句块的开头,for语句提倡写成for(int i=0;i<100;++i) 的形式,即i只在for语句块内部有效。
  • 允许采用type-name{xx,xx,xx}这样类似C++的构造函数的形式构造匿名的结构体,即复合文字特性。
  • 初始化结构体的时候允许对特定的元素赋值,形式为:
    struct test{int a[3]b;} foo[] = { [0].a = {1}, [1].a = 2 };
    
    struct test{int a, b, c, d;} foo = { .a = 1, .c = 3, 4, .b = 5 }; // 3,4 是对 .c,.d 赋值的
    
  • 格式化字符串中,利用 \u 支持 unicode 的字符。
  • 支持 16 进制的浮点数的描述。
  • printf, scanf 的格式化串增加了对 long long int 类型的支持。
  • 浮点数的内部数据描述支持了新标准,可以使用 #pragma 编译器指令指定。
  • 除了已有的 __LINE____FILE__ 以外,增加了__func__得到当前的函数名。
  • 允许编译器化简非常数的表达式。
  • 修改了/%处理负数时的定义,这样可以给出明确的结果,例如在C89中-22 / 7 = -3, -22 % 7 = -1,也可以-22 / 7= -4, -22 % 7 = 6。 而C99中明确为 -22 / 7 = -3, -22 % 7 = -1,只有一种结果。
  • 取消了函数返回类型默认为 int 的规定。
  • 允许在 struct 的最后定义的数组不指定其长度,写做 [](flexible array member)。
  • const const int i 将被当作 const int i 处理。
  • 增加和修改了一些标准头文件,比如定义 bool 的 <stdbool.h> ,定义一些标准长度的int<inttypes.h>,定义复数的<complex.h>,定义宽字符的<wctype.h>,类似于泛型的数学函数<tgmath.h>,浮点数相关的<fenv.h>。在<stdarg.h>增加了va_copy用于复制...的参数。<time.h>里增加了struct tmx,对struct tm做了扩展。
  • 输入输出对宽字符以及长整数等做了相应的支持。[3]

版本检测

标准宏__STDC_VERSION__被定义为值199901L来指示能够获得C99支持。就像__STDC__宏用于C90,__STDC_VERSION__可被用来书写对于C90和C99编译器进行不同编译的代码,下面的例子确保了inline在二者情况下都可获得(通过在C90中将其替代为static来避免连接器错误):

#if __STDC_VERSION__ >= 199901L
  /* "inline" is a keyword */
#else
# define inline static
#endif

实现

用于x86架构GCC,Clang和其它一些商业编译器(如IAR Systems,ICC)现支持C99的全部特性[4],而微软Borland支持C99的部分特性[5]

参考资料

  1. ^ ISO/IEC 9899:1999 - Programming languages - C. Iso.org. 2011-12-08 [2014-04-08]. (原始内容存档于2016-10-10). 
  2. ^ 存档副本 (PDF). [2020-07-03]. (原始内容存档 (PDF)于2017-06-22). 
  3. ^ ISO/IEC 9899:1999 (PDF). [2020-06-10]. (原始内容存档 (PDF)于2018-01-27). 
  4. ^ GCC从3.0版本开始正式支持C99(-std=c99),详情见
  5. ^ Peter Bright. C99 acknowledged at last as Microsoft lays out its path to C++14. Ars Technica. 29 June 2013 [9 January 2015]. (原始内容存档于2020-07-02). 

延伸阅读

外部链接

前任者:
C89 / C90 / "ANSI C"
C语言标准 继任者:
C11
{{bottomLinkPreText}} {{bottomLinkText}}
C99
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?