C99
C programming language standard, 1999 revision
C99 (C9X during its development, formally ISO/IEC 9899:1999) is a past version of the C programming language open standard. It extends the previous version (C90) with new features for the language and the standard library, and helps implementations make better use of available computer hardware, such as IEEE 754-1985 floating-point arithmetic, and compiler technology. The C11 version of the C programming language standard, published in 2011, updates C99.
History
After ANSI produced the official standard for the C programming language in 1989, which became an international standard in 1990, the C language specification remained relatively static for some time, while C++ continued to evolve, largely during its own standardization effort. Normative Amendment 1 created a new standard for C in 1995, but only to correct some details of the 1989 standard and to add more extensive support for international character sets. The standard underwent further revision in the late 1990s, leading to the publication of ISO/IEC 9899:1999 in 1999, which was adopted as an ANSI standard in May 2000. The language defined by that version of the standard is commonly referred to as "C99". The international C standard is maintained by the working group ISO/IEC JTC1/SC22/WG14.
Design
C99 is, for the most part, backward compatible with C89, but it is stricter in some ways.
In particular, a declaration that lacks a type specifier no longer has int implicitly assumed. The C standards committee decided that it was of more value for compilers to diagnose inadvertent omission of the type specifier than to silently process legacy code that relied on implicit int. In practice, compilers are likely to display a warning, then assume int and continue translating the program.
C99 introduced several new features, many of which had already been implemented as extensions in several compilers:
- Inline functions
- Intermingled declarations and code: variable declaration is no longer restricted to file scope or the start of a compound statement (block)
- Several new data types, including
long long int, optional extended integer types, an explicit Boolean data type (_Bool), and complex types (_Complextype specifier) to represent complex numbers, with convenience macrosboolandcomplex._Boolwas later renamed toboolin C23. - Variable-length arrays. C11 later changed this to a conditional feature that implementations are not required to support.
- Flexible array members
- Support for one-line comments beginning with
//(like that of BCPL, C++ and Java) - New library functions, such as
snprintf() - New standard library headers, such as
<stdbool.h>,<complex.h>,<tgmath.h>, and<inttypes.h> - Type-generic math (macro) functions, in
<tgmath.h>, which select a math library function based uponfloat,double, orlong doublearguments, etc. - Optional support for IEEE 754-1985 floating point
- Designated initializers, allowing initializing a structure by field names (for example
struct Vec3 v = { .i = 1, .j = 2, .k = 3 };) - Compound literals, allowing constructing
structin function calls (for examplefoo((struct Vec3) {1, 2, 3});) - Support for variadic macros (macros with a variable number of arguments)
restrictqualification allows more aggressive code optimization, removing compile-time array access advantages previously held by FORTRAN over ANSI C- Universal character names, which allows user variables to contain other characters than the standard character set: four-digit
\u0040or eight-digit hexadecimal sequences\U0001f431 - Keyword
staticin array indices in parameter declarations, which indicates that pointer passed to the function is notNULLand has at least the specified amount of elements.. For example,void foo(int bar[static 100]);indicates thatbaris non-null and points to an array of at least 100ints. _Pragma(), a unary preprocessing operator which does the same as#pragma. This was added because#pragmacannot be generated by a#definemacro. It takes a single string literal as its argument (for example_Pragma("something")).#pragma STDC CX_LIMITED_RANGE, a pragma that indicates to the compiler whether it can use fast, standard formulas for operations on complex numbers. When disabled, the compiler performs checks for infinity, NaN, and other edge cases. It may be set toON,OFF, andDEFAULT.
Some C99 features were excluded from C++, notably variable-length arrays. C++ offers sequential data structures like std::vector<T> for similar purposes. Other features were eventually added into C++, such as designated initializers which were added in C++20.
IEEE 754 floating-point support
A major feature of C99 is its numerics support, and in particular its support for access to the features of IEEE 754-1985 (also known as IEC 60559) floating-point hardware present in the vast majority of modern processors (defined in "Annex F IEC 60559 floating-point arithmetic"). Platforms without IEEE 754 hardware can also implement it in software.
FLT_EVAL_METHOD is a macro that specifies the precision and range used by the implementation for floating-point arithmetic operations.
On platforms with IEEE 754 floating point:
FLT_EVAL_METHOD == 2 tends to limit the risk of rounding errors affecting numerically unstable expressions (see IEEE 754 design rationale) and is the designed default method for x87 hardware, but yields unintuitive behavior for the unwary user; FLT_EVAL_METHOD == 1 was the default evaluation method originally used in K&R C, which promoted all floats to double in expressions; and FLT_EVAL_METHOD == 0 is also commonly used and specifies a strict "evaluate to type" of the operands. (For gcc, FLT_EVAL_METHOD == 2 is the default on 32-bit x86, and FLT_EVAL_METHOD == 0 is the default on 64-bit x86-64, but FLT_EVAL_METHOD == 2 can be specified on x86-64 with option -mfpmath=387.) Before C99, compilers could round intermediate results inconsistently, especially when using x87 floating-point hardware, leading to compiler-specific behaviour; such inconsistencies are not permitted in compilers conforming to C99 (annex F).
#pragma STDC FP_CONTRACT is a standard compiler directive that indicates to the compiler whether it may contract multiple floating-point instructions into a single fused multiply-add instruction. When disabled, the compiler strictly evaluates operations separately. It may be set to ON, OFF, or DEFAULT.
Example
The following annotated example C99 code for computing a continued fraction function demonstrates the main features:
Footnotes:
- As the IEEE 754 status flags are manipulated in this function, this
#pragmais needed to avoid the compiler incorrectly rearranging such tests when optimising. (Pragmas are usually implementation-defined, but those prefixed withSTDCare defined in the C standard.) - C99 defines a limited number of expression evaluation methods: the current compilation mode can be checked to ensure it meets the assumptions the code was written under.
- The special values such as NaN and positive or negative infinity can be tested and set.
long doubleis defined as IEEE 754 double extended or quad precision if available. Using higher precision than required for intermediate computations can minimize round-off error (the typedefdouble_tcan be used for code that is portable under allFLT_EVAL_METHODs).- The main function to be evaluated. Although it appears that some arguments to this continued fraction, e.g.,
3.0, would lead to a divide-by-zero error, in fact the function is well-defined at3.0and division by0will simply return a positive infinity that will then correctly lead to a finite result: IEEE 754 is defined not to trap on such exceptions by default and is designed so that they can very often be ignored, as in this case. (IfFLT_EVAL_METHODis defined as2then all internal computations including constants will be performed in long double precision; ifFLT_EVAL_METHODis defined as0then additional care is need to ensure this, including possibly additional casts and explicit specification of constants aslong double.) - As the raised divide-by-zero flag is not an error in this case, it can simply be dismissed to clear the flag for use by later code.
- In some cases, other exceptions may be regarded as an error, such as overflow (although it can in fact be shown that this cannot occur in this case).
__STDC_IEC_559__is to be defined only if "Annex F IEC 60559 floating-point arithmetic" is fully implemented by the compiler and the C library (users should be aware that this macro is sometimes defined while it should not be).- The default rounding mode is round to nearest (with the even rounding rule in the halfway cases) for IEEE 754, but explicitly setting the rounding mode toward positive and negative infinity (by defining
TEST_NUMERIC_STABILITY_UPetc. in this example, when debugging) can be used to diagnose numerical instability. This method can be used even ifcompute()is part of a separately compiled binary library. But depending on the function, numerical instabilities cannot always be detected.
Version detection
A standard macro __STDC_VERSION__ is defined with value 199901L to indicate that C99 support is available. As with the __STDC__ macro for C90, __STDC_VERSION__ can be used to write code that will compile differently for C90 and C99 compilers, as in this example that ensures that inline is available in either case (by replacing it with static in C90 to avoid linker errors).
Implementations
Most C compilers provide support for at least some of the features introduced in C99.
Historically, Microsoft has been slow to implement new C features in their Visual C++ tools, instead focusing mainly on supporting developments in the C++ standards. However, with the introduction of Visual C++ 2013 Microsoft implemented a limited subset of C99, which was expanded in Visual C++ 2015.
Future work
Since ratification of the 1999 C standard, the standards working group prepared technical reports specifying improved support for embedded processing, additional character data types (Unicode support), and library functions with improved bounds checking. Work continues on technical reports addressing decimal floating point, additional mathematical special functions, and additional dynamic memory allocation functions. The C and C++ standards committees have been collaborating on specifications for threaded programming.
The next revision of the C standard, C11, was ratified in 2011. The C standards committee adopted guidelines that limited the adoption of new features that have not been tested by existing implementations. Much effort went into developing a memory model, in order to clarify sequence points and to support threaded programming.
Adapted from the Wikipedia article, CC BY-SA 4.0. Continue on Wikipedia
Go deeper
Ideas this article links to.
- C++202020 edition of the C++ programming language standard
- FortranGeneral-purpose programming language
- C standard libraryStandard library for the C programming language
- Ch (computer programming)Programming language environment
- C dynamic memory allocationDynamic memory management in the C programming language