Jump to content

C99

From Wikipedia, the free encyclopedia

Template:Short description Template:About Template:Use dmy dates Template:Missing information Template:C language revisions

File:ISO-IEC-9899-1999-cover.png
Cover of the C99 standards document

C99 (C9X during its development, formally ISO/IEC 9899:1999) is a past version of the C programming language open standard.[1] 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.[2] 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

File:1999 ISO C Concepts.png

C99 is, for the most part, backward compatible with C89, but it is stricter in some ways.[3]

In particular, a declaration that lacks a type specifier no longer has <syntaxhighlight lang="c" inline>int</syntaxhighlight> 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 <syntaxhighlight lang="c" inline>int</syntaxhighlight>. In practice, compilers are likely to display a warning, then assume <syntaxhighlight lang="c" inline>int</syntaxhighlight> and continue translating the program.

C99 introduced several new features, many of which had already been implemented as extensions in several compilers:[4]

  • 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 <syntaxhighlight lang="c" inline>long long int</syntaxhighlight>, optional extended integer types, an explicit Boolean data type (<syntaxhighlight lang="c" inline>_Bool</syntaxhighlight>), and complex types (<syntaxhighlight lang="c" inline>_Complex</syntaxhighlight> type specifier) to represent complex numbers
  • variable-length arrays (although subsequently relegated in C11 to a conditional feature that implementations are not required to support)
  • flexible array members
  • support for one-line comments beginning with <syntaxhighlight lang="c" inline>//</syntaxhighlight> (like that of BCPL, C++ and Java)
  • new library functions, such as snprintf()
  • new 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 upon <syntaxhighlight lang="c" inline>float</syntaxhighlight>, <syntaxhighlight lang="c" inline>double</syntaxhighlight>, or <syntaxhighlight lang="c" inline>long double</syntaxhighlight> arguments, etc.
  • optional support for IEEE 754-1985 floating point
  • designated initializers. For example, initializing a structure by field names: <syntaxhighlight lang="c" inline>struct X p = { .i = 1, .j = 2 };</syntaxhighlight>[5]
  • compound literals. For instance, it is possible to construct structures in function calls: <syntaxhighlight lang="c" inline>foo((struct X) {1, 2})</syntaxhighlight>[6]
  • support for variadic macros (macros with a variable number of arguments)
  • <syntaxhighlight lang="c" inline>restrict</syntaxhighlight> qualification allows more aggressive code optimization, removing compile-time array access advantages previously held by FORTRAN over ANSI C[7]
  • universal character names, which allows user variables to contain other characters than the standard character set: four-digit <syntaxhighlight lang="c" inline>\u0040</syntaxhighlight> or eight-digit hexadecimal sequences <syntaxhighlight lang="c" inline>\U0001f431</syntaxhighlight>
  • keyword <syntaxhighlight lang="c" inline>static</syntaxhighlight> in array indices in parameter declarations[8]: For example, <syntaxhighlight lang="c" inline>void foo(int bar[static 100]);</syntaxhighlight> tells the compiler that every time foo() is called, the passed pointer bar is definitely not null, and points to an array of at least 100 <syntaxhighlight lang="c" inline>int</syntaxhighlight>s.[9][10]

Some C99 features were excluded from C++, notably variable-length arrays. C++ offers sequential data structures like std::vector<T> for similar purposes.

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.[2]

On platforms with IEEE 754 floating point: Template:Unordered list

<syntaxhighlight lang="c" inline>FLT_EVAL_METHOD == 2</syntaxhighlight> 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;[11] <syntaxhighlight lang="c" inline>FLT_EVAL_METHOD == 1</syntaxhighlight> was the default evaluation method originally used in K&R C, which promoted all floats to double in expressions; and <syntaxhighlight lang="c" inline>FLT_EVAL_METHOD == 0</syntaxhighlight> is also commonly used and specifies a strict "evaluate to type" of the operands. (For gcc, <syntaxhighlight lang="c" inline>FLT_EVAL_METHOD == 2</syntaxhighlight> is the default on 32 bit x86, and <syntaxhighlight lang="c" inline>FLT_EVAL_METHOD == 0</syntaxhighlight> is the default on 64 bit x86-64, but <syntaxhighlight lang="c" inline>FLT_EVAL_METHOD == 2</syntaxhighlight> 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;[12] such inconsistencies are not permitted in compilers conforming to C99 (annex F).

Example

The following annotated example C99 code for computing a continued fraction function demonstrates the main features:

<syntaxhighlight lang=C line highlight="9,11,13,15,21,23,25,36,42">

  1. include <assert.h>
  2. include <fenv.h>
  3. include <float.h>
  4. include <math.h>
  5. include <stdio.h>
  6. include <stdbool.h>
  7. include <tgmath.h>

double compute_fn(double z) { // [1]

   #pragma STDC FENV_ACCESS ON  // [2]
   assert(FLT_EVAL_METHOD == 2);  // [3]
   if (isnan(z)) {  // [4]
       puts("z is not a number");
   }
   if (isinf(z)) {
       puts("z is infinite");
   }
   long double r = 7.0 - 3.0 / (z - 2.0 - 1.0 / (z - 7.0 + 10.0 / (z - 2.0 - 2.0 / (z - 3.0)))); // [5, 6]
   feclearexcept(FE_DIVBYZERO);  // [7]
   bool raised = fetestexcept(FE_OVERFLOW);  // [8]
   if (raised) {
       puts("Unanticipated overflow.");
   }
   return r;

}

int main(void) {

   #ifndef __STDC_IEC_559__
   puts("Warning: __STDC_IEC_559__ not defined. IEEE 754 floating point not fully supported."); // [9]
   #endif
   #pragma STDC FENV_ACCESS ON
   #ifdef TEST_NUMERIC_STABILITY_UP
   fesetround(FE_UPWARD);                   // [10]
   #elif TEST_NUMERIC_STABILITY_DOWN
   fesetround(FE_DOWNWARD);
   #endif
   printf("%.7g\n", compute_fn(3.0));
   printf("%.7g\n", compute_fn(NAN));
   return 0;

} </syntaxhighlight>

Footnotes:

  1. Compile with: <syntaxhighlight lang="bash" class="" style="" inline="1">gcc -std=c99 -mfpmath=387 -o test_c99_fp test_c99_fp.c -lm</syntaxhighlight>
  2. As the IEEE 754 status flags are manipulated in this function, this <syntaxhighlight lang="c" inline>#pragma</syntaxhighlight> is needed to avoid the compiler incorrectly rearranging such tests when optimising. (Pragmas are usually implementation-defined, but those prefixed with STDC are defined in the C standard.)
  3. 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.
  4. The special values such as NaN and positive or negative infinity can be tested and set.
  5. <syntaxhighlight lang="c" inline>long double</syntaxhighlight> is defined as IEEE 754 double extended or quad precision if available. Using higher precision than required for intermediate computations can minimize round-off error[13] (the typedef <syntaxhighlight lang="c" inline>double_t</syntaxhighlight> can be used for code that is portable under all FLT_EVAL_METHODs).
  6. 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 at 3.0 and division by 0 will simply return a +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. (If FLT_EVAL_METHOD is defined as 2 then all internal computations including constants will be performed in long double precision; if FLT_EVAL_METHOD is defined as 0 then additional care is need to ensure this, including possibly additional casts and explicit specification of constants as long double.)
  7. 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.
  8. 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).
  9. __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).
  10. 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 + and - infinity (by defining TEST_NUMERIC_STABILITY_UP etc. in this example, when debugging) can be used to diagnose numerical instability.[14] This method can be used even if compute_fn() 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 <syntaxhighlight lang="c" inline>inline</syntaxhighlight> is available in either case (by replacing it with <syntaxhighlight lang="c" inline>static</syntaxhighlight> in C90 to avoid linker errors).

<syntaxhighlight lang=C>

  1. if __STDC_VERSION__ >= 199901L
 /* "inline" is a keyword */
  1. else
  2. define inline static
  3. endif

</syntaxhighlight>

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.[15] However, with the introduction of Visual C++ 2013 Microsoft implemented a limited subset of C99, which was expanded in Visual C++ 2015.[16]

Compiler Level of support C99 compatibility details
Acorn C/C++ Template:Partial The official documentation states that "most" compiler features are supported, along with "some" of the library functions.
AMD x86 Open64 Compiler Suite Template:Okay Has C99 support equal to that of GCC.[17]
cc65 Template:Partial Full C89 and C99 support is not implemented, partly due to platform limitations (MOS Technology 6502). There is no support planned for some C99 types like _Complex and 64-bit integers (long long).[18]
Ch Template:Partial Supports major C99 features.[19]
Clang Template:Okay Supports all features except C99 floating-point pragmas.[20]
CompCert Template:Okay A certified compiler, formally proved correct. Supports all features except C99 complex numbers and VLA, and minor restrictions on switch statements (no Duff's device).[21]
cparser Template:Yes Supports C99 features.[22]
C++ Builder Template:Okay
Template:Citation needed
Digital Mars C/C++ Compiler Template:Partial Lacks support for some features, such as <tgmath.h> and _Pragma.[23]
GCC Template:Okay Template:As of, standard pragmas and IEEE 754/IEC 60559 floating-point support are missing in mainline GCC. Additionally, some features (such as extended integer types and new library functions) must be provided by the C standard library and are out of scope for GCC.[24] GCC's 4.6 and 4.7 releases also provide the same level of compliance.[25][26] Partial IEEE 754 support, even when the hardware is compliant: some compiler options may be needed to avoid incorrect optimizations (e.g., -std=c99 and -fsignaling-nans), but full support of directed rounding modes is missing even when -frounding-math is used.[27]
Green Hills Software Template:Yes
IBM C for AIX, V6 [28] and XL C/C++ V11.1 for AIX [29] Template:Yes
IBM Rational logiscope Template:Yes Until Logiscope 6.3, only basic constructs of C99 were supported. C99 is officially supported in Logiscope 6.4 and later versions.[30]
The Portland Group PGI C/C++ Template:Yes
IAR Systems
Embedded Workbench
Template:Partial Does not support UCN (universal character names). Compiler for embedded targets, such as ARM, Coldfire, MSP430, AVR, AVR32, 8051, ... No x86 targets.
Intel C++ compiler Template:Okay
Template:Citation needed
Microsoft Visual C++ Template:Partial[16] Visual C++ 2012 and earlier did not support C99.[31][32][33]
Visual C++ 2013 implements a limited subset of C99 required to compile popular open-source projects.[34][35]
Visual C++ 2015 implements the C99 standard library, with the exception of any library features that depend on compiler features not yet supported by the compiler (for example, <tgmath.h> is not implemented).[16]
Visual C++ 2019 (16.6) adds opt-in support for a C99 conformant preprocessor.[36]
Open Watcom Template:Partial Implements the most commonly used parts of the standard. However, they are enabled only through the undocumented command-line switch "-za99". Three C99 features have been bundled as C90 extensions since pre-v1.0: C++ style comments (//), flexible array members, trailing comma allowed in enum declaration.[37]
Pelles C Template:Yes Supports all C99 features.[38]
Portable C compiler Template:Partial Working towards becoming C99-compliant.[39]
Sun Studio Template:Yes[40]
The Amsterdam Compiler Kit Template:NoTemplate:Citation needed A C99 frontend is currently under investigation.Template:Citation needed
Tiny C Compiler Template:Partial Does not support complex numbers.[41][42] Variable Length Arrays are supported but not as arguments in functionsTemplate:Citation needed. The developers state that "TCC is heading toward full ISOC99 compliance".[43]
vbcc Template:Partial

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.[44] 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.

See also

References

Template:Reflist

Further reading

|CitationClass=web }}

Template:S-start Template:S-bef Template:S-ttl Template:S-aft

{{safesubst:#if:|||} }}{{safesubst:#invoke:Check for unknown parameters|check|unknown=Template:Main other|preview=Page using Template:End with unknown parameter "_VALUE_"|ignoreblank=y| html | 1 }}

Template:CProLang Template:ISO standards

bg:C (език за програмиране)#C99

  1. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  2. ↑ 2.0 2.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  3. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  4. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  5. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  6. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  7. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  8. ↑ Template:Cite book
  9. ↑ "What is the purpose of static keyword in array parameter of function like "char s[static 10"?"].
  10. ↑ "‘static’ With Array Parameters? Oh My!".
  11. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  12. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  13. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  14. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  15. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  16. ↑ 16.0 16.1 16.2 {{#invoke:citation/CS1|citation |CitationClass=web }}
  17. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  18. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  19. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  20. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  21. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  22. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  23. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  24. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  25. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  26. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  27. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  28. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  29. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  30. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  31. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  32. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  33. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  34. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  35. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  36. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  37. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  38. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  39. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  40. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  41. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  42. ↑ According to the project's TODO list complex types are the only missing C99 feature. Variable Length Arrays have been added in TCC 0.9.26 [1]
  43. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  44. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}