c99

From Wikipedia, the không lấy phí encyclopedia

This article is about the programming language dialect. For other uses, see C99 (disambiguation).

Bạn đang xem: c99

This is missing information about _Pragma(), FP_CONTRACT, CX_LIMITED_RANGE. Please expand the lớn include this information. Further details may exist on the talk page. (November 2020)

Cover of the C99 standards document

C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language 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, replaces C99.

History[edit]

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 lớn evolve, largely during its own standardization effort. Normative Amendment 1 created a new standard for C in 1995, but only lớn correct some details of the 1989 standard and lớn add more extensive tư vấn for international character sets. The standard underwent further revision in the late 1990s, leading lớn 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 lớn as "C99". The international C standard is maintained by the working group ISO/IEC JTC1/SC22/WG14.

Design[edit]

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 int implicitly assumed. The C standards committee decided that it was of more value for compilers lớn diagnose inadvertent omission of the type specifier than vãn lớn silently process legacy code that relied on implicit int. In practice, compilers are likely lớn 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:[4]

  • inline functions
  • intermingled declarations and code: variable declaration is no longer restricted lớn tệp tin 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, and a complex type lớn represent complex numbers
  • variable-length arrays (although subsequently relegated in C11 lớn a conditional feature that implementations are not required lớn support)
  • flexible array members
  • support for one-line comments beginning with //, as in 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 float, double, or long double arguments, etc.
  • improved tư vấn for IEEE floating point
  • designated initializers (for example, initializing a structure by field names: struct point p = { .x = 1, .nó = 2 };)[5]
  • compound literals (for instance, it is possible lớn construct structures in function calls: function((struct x) {1, 2}))[6]
  • support for variadic macros (macros with a variable number of arguments)
  • restrict 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 lớn contain other characters than vãn the standard character set: four-digit \u0040 or eight-digit hexadecimal sequences \U0001f431
  • keyword static in array indices in parameter declarations[8]

Parts of the C99 standard are included in the current version of the C++ standard, including integer types, headers, and library functions. Variable-length arrays are not among these included parts because C++'s Standard Template Library already includes similar functionality.

Xem thêm: zano

IEEE 754 floating-point support[edit]

A major feature of C99 is its numerics tư vấn, and in particular its tư vấn for access lớn 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:

  • float is defined as IEEE 754 single precision, double is defined as double precision, and long double is defined as IEEE 754 extended precision (e.g., Hãng sản xuất Intel 80-bit double extended precision on x86 or x86-64 platforms), or some khuông of quad precision where available; otherwise, it is double precision.
  • The four arithmetic operations and square root are correctly rounded as defined by IEEE 754.
    FLT_EVAL_METHOD float double long double
    0 float double long double
    1 double double long double
    2 long double long double long double
  • Expression evaluation is defined lớn be performed in one of three well-defined methods, indicating whether floating-point variables are first promoted lớn a more precise format in expressions: FLT_EVAL_METHOD == 2 indicates that all internal intermediate computations are performed by mặc định at high precision (long double) where available (e.g., 80 bit double extended), FLT_EVAL_METHOD == 1 performs all internal intermediate expressions in double precision (unless an operand is long double), while FLT_EVAL_METHOD == 0 specifies each operation is evaluated only at the precision of the widest operand of each operator. The intermediate result type for operands of a given precision are summarized in the adjacent table.

FLT_EVAL_METHOD == 2 tends lớn limit the risk of rounding errors affecting numerically unstable expressions (see IEEE 754 design rationale) and is the designed mặc định method for x87 hardware, but yields unintuitive behavior for the unwary user;[9] FLT_EVAL_METHOD == 1 was the mặc định evaluation method originally used in K&R C, which promoted all floats lớn double in expressions; and FLT_EVAL_METHOD == 0 is also commonly used and specifies a strict "evaluate lớn type" of the operands. (For gcc, FLT_EVAL_METHOD == 2 is the mặc định on 32 bit x86, and FLT_EVAL_METHOD == 0 is the mặc định 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 lớn compiler-specific behaviour;[10] such inconsistencies are not permitted in compilers conforming lớn C99 (annex F).

Example[edit]

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

#include <stdio.h>
#include <math.h>
#include <float.h>
#include <fenv.h>
#include <tgmath.h>
#include <stdbool.h>
#include <assert.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;
}

Footnotes:

  1. Compile with: gcc -std=c99 -mfpmath=387 -o test_c99_fp test_c99_fp.c -lm
  2. As the IEEE 754 status flags are manipulated in this function, this #pragma is needed lớn 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 lớn 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 mix.
  5. long double is defined as IEEE 754 double extended or quad precision if available. Using higher precision than vãn required for intermediate computations can minimize round-off error[11] (the typedef double_t can be used for code that is portable under all FLT_EVAL_METHODs).
  6. The main function lớn be evaluated. Although it appears that some arguments lớn this continued fraction, e.g., 3.0, would lead lớn 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 lớn a finite result: IEEE 754 is defined not lớn trap on such exceptions by mặc định and is designed sánh 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 lớn 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 lớn 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 lớn 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 mặc định rounding mode is round lớn 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 lớn diagnose numerical instability.[12] 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[edit]

A standard macro __STDC_VERSION__ is defined with value 199901L lớn indicate that C99 tư vấn is available. As with the __STDC__ macro for C90, __STDC_VERSION__ can be used lớn 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 lớn avoid linker errors).

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

Implementations[edit]

Most C compilers provide tư vấn for at least some of the features introduced in C99.

Xem thêm: tải phần mềm hack

Historically, Microsoft has been slow lớn implement new C features in their Visual C++ tools, instead focusing mainly on supporting developments in the C++ standards.[13] However, with the introduction of Visual C++ 2013 Microsoft implemented a limited subset of C99, which was expanded in Visual C++ năm ngoái.[14]

Compiler Level of support C99 compatibility details
Acorn C/C++ Partial The official documentation states that "most" compiler features are supported, along with "some" of the library functions.
AMD x86 Open64 Compiler Suite Mostly Has C99 tư vấn equal lớn that of GCC.[15]
cc65 Partial Full C89 and C99 tư vấn is not implemented, partly due lớn platform limitations (MOS Technology 6502). There is no tư vấn planned for some C99 types lượt thích _Complex and 64-bit integers (long long).[16]
Ch Partial Supports major C99 features.[17]
Clang Mostly Supports all features except C99 floating-point pragmas.[18]
CompCert Mostly 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).[19]
cparser Full Supports C99 features.[20]
C++ Builder Only in 64-bit mode, since latter is CLang fork
[citation needed]
Digital Mars C/C++ Compiler Partial Lacks tư vấn for some features, such as <tgmath.h> and _Pragma.[21]
GCC Mostly As of July 2021, standard pragmas and IEEE 754/IEC 60559 floating-point tư vấn 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.[22] GCC's 4.6 and 4.7 releases also provide the same level of compliance.[23][24] Partial IEEE 754 tư vấn, even when the hardware is compliant: some compiler options may be needed lớn avoid incorrect optimizations (e.g., -std=c99 and -fsignaling-nans), but full tư vấn of directed rounding modes is missing even when -frounding-math is used.[25]
Green Hills Software Full
IBM C for AIX, V6 [26] and XL C/C++ V11.1 for AIX [27] Full
IBM Rational logiscope Full Until Logiscope 6.3, only basic constructs of C99 were supported. C99 is officially supported in Logiscope 6.4 and later versions.[28]
The Portland Group PGI C/C++ Full
IAR Systems
Embedded Workbench
Mostly Does not tư vấn UCN (universal character names). Compiler for embedded targets, such as ARM, Coldfire, MSP430, AVR, AVR32, 8051, ... No x86 targets.
Intel C++ compiler Mostly
[citation needed]
Microsoft Visual C++ Partial[14] Visual C++ 2012 and earlier did not tư vấn C99.[29][30][31]
Visual C++ 2013 implements a limited subset of C99 required lớn compile popular open-source projects.[32][33]
Visual C++ năm ngoái 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).[14]
Visual C++ 2019 (16.6) adds opt-in tư vấn for a C99 conformant preprocessor.[34]
Open Watcom 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.[35]
Pelles C Full Supports all C99 features.[36]
Portable C compiler Partial Working towards becoming C99-compliant.[citation needed]
Sun Studio Full[37]
The Amsterdam Compiler Kit No[citation needed] A C99 frontend is currently under investigation.[citation needed]
Tiny C Compiler Partial Does not tư vấn complex numbers.[38][39] Variable Length Arrays are supported but not as arguments in functions[citation needed]. The developers state that "TCC is heading toward full ISOC99 compliance".[40]
vbcc Partial

Future work[edit]

Since ratification of the 1999 C standard, the standards working group prepared technical reports specifying improved tư vấn 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.[41] 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 lớn clarify sequence points and lớn tư vấn threaded programming.

See also[edit]

  • C++23, C++20, C++17, C++14, C++11, C++03, C++98, versions of the C++ programming language standard
  • Compatibility of C and C++
  • C++ Technical Report 1
  • Floating point, for further discussion of usage of IEEE 754 hardware

References[edit]

  1. ^ "ISO/IEC 9899:1999 - Programming languages - C". Iso.org. 8 December 2011. Retrieved 8 April 2014.
  2. ^ a b "IEEE 754 Support in C99" (PDF). grouper.ieee.org. Archived from the original (PDF) on 28 October 2017. Retrieved 15 July 2021.
  3. ^ "Standards - Using the GNU Compiler Collection (GCC)". Gcc.gnu.org. Retrieved 8 April 2014.
  4. ^ "C Dialect Options - Using the GNU Compiler Collection (GCC)". Gcc.gnu.org. 6 May 2009. Retrieved 8 April 2014.
  5. ^ "Using the GNU Compiler Collection (GCC): Designated Initializers". gnu.org. Retrieved 18 September 2019.
  6. ^ "Using the GNU Compiler Collection (GCC): Compound Literals". gnu.org. Retrieved 31 January 2016.
  7. ^ Ulrich Drepper (23 October 2007). "What every programmer should know about memory". LWN.net. Retrieved 3 April 2015.
  8. ^ ISO/IEC 9899:1999 specification, TC3 (PDF). p. 119, § 6.7.5.3 Function declarators (including prototypes) para. 7.
  9. ^ Doug Priest (1997). "Differences Among IEEE 754 Implementations".
  10. ^ Jack Woehr (1 November 1997). "A conversation with William Kahan".
  11. ^ William Kahan (11 June 1996). "The Baleful Effect of Computer Benchmarks upon Applied Mathematics, Physics and Chemistry" (PDF).
  12. ^ William Kahan (11 January 2006). "How Futile are Mindless Assessments of Roundoff in Floating-Point Computation?" (PDF).
  13. ^ Peter Bright (29 June 2013). "C99 acknowledged at last as Microsoft lays out its path lớn C++14". Ars Technica. Retrieved 9 January 2015.
  14. ^ a b c Brenner, Pat. "What's New for Visual C++ in Visual Studio 2015". Microsoft Developer Network. Retrieved 27 April 2015.
  15. ^ "Using the x86 Open64 Compiler Suite" (PDF). Developer.amd.com. Archived (PDF) from the original on 24 January 2022. Retrieved 2 March 2022.
  16. ^ "cc65 - a freeware C compiler for 6502 based systems". Retrieved 14 September 2011.
  17. ^ "C/C++ interpreter Ch C99 features". SoftIntegration, Inc. 15 February 2008. Retrieved 15 February 2008.
  18. ^ "Clang Compiler User's Manual". Retrieved 14 October 2017.
  19. ^ "The CompCert C verified compiler documentation and user's manual (Version 3.10)". 19 November 2021. Retrieved 3 March 2022.
  20. ^ "libfirm homepage". Retrieved 4 February 2014.
  21. ^ "C Language Implementation - Digital Mars". Retrieved 14 September 2011.
  22. ^ "Status of C99 features in GCC". Free Software Foundation, Inc. 28 July 2021. Retrieved 13 August 2021.
  23. ^ "Status of C99 features in GCC 4.6". Free Software Foundation, Inc. 23 May 2013. Retrieved 23 May 2013.
  24. ^ "Status of C99 features in GCC 4.7". Free Software Foundation, Inc. 23 May 2013. Retrieved 23 May 2013.
  25. ^ "Semantics of Floating Point Math in GCC". đôi mươi July 2018. Retrieved 12 August 2018.
  26. ^ "IBM C for AIX, V6.0 Now Supports the C99 Standard". 2 July 2002. Retrieved 31 January 2016.
  27. ^ "IBM - XL C/C++ for AIX". Retrieved 31 January 2016.
  28. ^ "IBM Rational Logiscope tư vấn for C99 standard - United States". 24 February 2012. Retrieved 31 January 2016.
  29. ^ "Reader Q&A: What about VC++ and C99?". Sutter’s Mill. 3 May 2012. Retrieved 31 January 2016.
  30. ^ "A.27 Use of C99 Variable Length Arrays". Microsoft. Retrieved 31 January 2016.
  31. ^ "Microsoft lớn C99 Developers: Use ISO C++". InfoQ. Retrieved 31 January 2016.
  32. ^ "C99 library tư vấn in Visual Studio 2013". Microsoft. 19 July 2013. Retrieved 31 January 2016.
  33. ^ "C++11/14 STL Features, Fixes, And Breaking Changes In VS 2013". Blogs.msdn.com. 28 June 2013. Retrieved 8 April 2014.
  34. ^ "Announcing full tư vấn for a C/C++ conformant preprocessor in MSVC". Microsoft. 27 March 2020. Retrieved 17 September 2020.
  35. ^ "C99 compliance in Open Watcom". Archived from the original on 3 May 2015. Retrieved 25 September 2015.
  36. ^ "Pelles C Overview". January 2013. Archived from the original on 13 March 2022. Retrieved 2 March 2022.
  37. ^ "Sun Studio 12: C Compiler 5.9 Readme". Sun Microsystems, Inc. 31 May 2007. Retrieved 23 September 2012.
  38. ^ "Tiny C Compiler Reference Documentation". Retrieved 31 January 2016.
  39. ^ According lớn 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]
  40. ^ "TCC : Tiny C Compiler". Retrieved 31 January 2016.
  41. ^ "Standards - Using the GNU Compiler Collection (GCC)". Gcc.gnu.org. Retrieved 8 April 2014.

Further reading[edit]

  • Cheng, Harry (1 March 2002). "C99 & Numeric computing". Dr. Dobb's Journal.
  • Seebach, Peter (24 March 2004). "Open source development using C99". developerWorks. IBM.
  • Rationale for International Standard—Programming Languages—C Revision 5.10, April-2003, Rationale for C99

External links[edit]

  • C Language Working Group WG14 Documents
  • N1256 - final draft of C99 + TC1 + TC2 + TC3 - dated September 7, 2007
  • New things in C9X
  • Features of C99