]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/doc/gcc/language-standards-supported-by-gcc/c-language.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / gcc / language-standards-supported-by-gcc / c-language.rst
1 ..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6 .. index:: C standard, C standards, ANSI C standard, ANSI C, ANSI C89, C89, ANSI X3.159-1989, X3.159-1989, ISO C standard, ISO C, ISO C90, ISO/IEC 9899, ISO 9899, C90, ISO C94, C94, ISO C95, C95, ISO C99, C99, ISO C9X, C9X, ISO C11, C11, ISO C1X, C1X, ISO C17, C17, ISO C2X, C2X, Technical Corrigenda, TC1, Technical Corrigendum 1, TC2, Technical Corrigendum 2, TC3, Technical Corrigendum 3, AMD1, freestanding implementation, freestanding environment, hosted implementation, hosted environment, __STDC_HOSTED__, std, ansi, pedantic, pedantic-errors
7
8 C Language
9 **********
10
11 The original ANSI C standard (X3.159-1989) was ratified in 1989 and
12 published in 1990. This standard was ratified as an ISO standard
13 (ISO/IEC 9899:1990) later in 1990. There were no technical
14 differences between these publications, although the sections of the
15 ANSI standard were renumbered and became clauses in the ISO standard.
16 The ANSI
17 standard, but not the ISO standard, also came with a Rationale
18 document.
19 This standard, in both its forms, is commonly known as :dfn:`C89`, or
20 occasionally as :dfn:`C90`, from the dates of ratification.
21 To select this standard in GCC, use one of the options
22 :option:`-ansi`, :option:`-std=c90` or :option:`-std=iso9899:1990` ; to obtain
23 all the diagnostics required by the standard, you should also specify
24 :option:`-pedantic` (or :option:`-pedantic-errors` if you want them to be
25 errors rather than warnings). See :ref:`c-dialect-options`.
26
27 Errors in the 1990 ISO C standard were corrected in two Technical
28 Corrigenda published in 1994 and 1996. GCC does not support the
29 uncorrected version.
30
31 An amendment to the 1990 standard was published in 1995. This
32 amendment added digraphs and ``__STDC_VERSION__`` to the language,
33 but otherwise concerned the library. This amendment is commonly known
34 as :dfn:`AMD1`; the amended standard is sometimes known as :dfn:`C94` or
35 :dfn:`C95`. To select this standard in GCC, use the option
36 :option:`-std=iso9899:199409` (with, as for other standard versions,
37 :option:`-pedantic` to receive all required diagnostics).
38
39 A new edition of the ISO C standard was published in 1999 as ISO/IEC
40 9899:1999, and is commonly known as :dfn:`C99`. (While in
41 development, drafts of this standard version were referred to as
42 :dfn:`C9X`.) GCC has substantially
43 complete support for this standard version; see
44 https://gcc.gnu.org/c99status.html for details. To select this
45 standard, use :option:`-std=c99` or :option:`-std=iso9899:1999`.
46
47 Errors in the 1999 ISO C standard were corrected in three Technical
48 Corrigenda published in 2001, 2004 and 2007. GCC does not support the
49 uncorrected version.
50
51 A fourth version of the C standard, known as :dfn:`C11`, was published
52 in 2011 as ISO/IEC 9899:2011. (While in development, drafts of this
53 standard version were referred to as :dfn:`C1X`.)
54 GCC has substantially complete support
55 for this standard, enabled with :option:`-std=c11` or
56 :option:`-std=iso9899:2011`. A version with corrections integrated was
57 prepared in 2017 and published in 2018 as ISO/IEC 9899:2018; it is
58 known as :dfn:`C17` and is supported with :option:`-std=c17` or
59 :option:`-std=iso9899:2017` ; the corrections are also applied with
60 :option:`-std=c11`, and the only difference between the options is the
61 value of ``__STDC_VERSION__``.
62
63 A further version of the C standard, known as :dfn:`C2X`, is under
64 development; experimental and incomplete support for this is enabled
65 with :option:`-std=c2x`.
66
67 By default, GCC provides some extensions to the C language that, on
68 rare occasions conflict with the C standard. See :ref:`c-extensions`.
69 Some features that are part of the C99 standard
70 are accepted as extensions in C90 mode, and some features that are part
71 of the C11 standard are accepted as extensions in C90 and C99 modes.
72 Use of the
73 :option:`-std` options listed above disables these extensions where
74 they conflict with the C standard version selected. You may also
75 select an extended version of the C language explicitly with
76 :option:`-std=gnu90` (for C90 with GNU extensions), :option:`-std=gnu99`
77 (for C99 with GNU extensions) or :option:`-std=gnu11` (for C11 with GNU
78 extensions).
79
80 The default, if no C language dialect options are given,
81 is :option:`-std=gnu17`.
82
83 The ISO C standard defines (in clause 4) two classes of conforming
84 implementation. A :dfn:`conforming hosted implementation` supports the
85 whole standard including all the library facilities; a :dfn:`conforming
86 freestanding implementation` is only required to provide certain
87 library facilities: those in ``<float.h>``, ``<limits.h>``,
88 ``<stdarg.h>``, and ``<stddef.h>`` ; since AMD1, also those in
89 ``<iso646.h>`` ; since C99, also those in ``<stdbool.h>`` and
90 ``<stdint.h>`` ; and since C11, also those in ``<stdalign.h>``
91 and ``<stdnoreturn.h>``. In addition, complex types, added in C99, are not
92 required for freestanding implementations.
93
94 The standard also defines two environments for programs, a
95 :dfn:`freestanding environment`, required of all implementations and
96 which may not have library facilities beyond those required of
97 freestanding implementations, where the handling of program startup
98 and termination are implementation-defined; and a :dfn:`hosted
99 environment`, which is not required, in which all the library
100 facilities are provided and startup is through a function ``int
101 main (void)`` or ``int main (int, char *[])``. An OS kernel is an example
102 of a program running in a freestanding environment;
103 a program using the facilities of an
104 operating system is an example of a program running in a hosted environment.
105
106 .. index:: ffreestanding
107
108 GCC aims towards being usable as a conforming freestanding
109 implementation, or as the compiler for a conforming hosted
110 implementation. By default, it acts as the compiler for a hosted
111 implementation, defining ``__STDC_HOSTED__`` as ``1`` and
112 presuming that when the names of ISO C functions are used, they have
113 the semantics defined in the standard. To make it act as a conforming
114 freestanding implementation for a freestanding environment, use the
115 option :option:`-ffreestanding` ; it then defines
116 ``__STDC_HOSTED__`` to ``0`` and does not make assumptions about the
117 meanings of function names from the standard library, with exceptions
118 noted below. To build an OS kernel, you may well still need to make
119 your own arrangements for linking and startup.
120 See :ref:`c-dialect-options`.
121
122 GCC does not provide the library facilities required only of hosted
123 implementations, nor yet all the facilities required by C99 of
124 freestanding implementations on all platforms.
125 To use the facilities of a hosted
126 environment, you need to find them elsewhere (for example, in the
127 GNU C library). See :ref:`standard-libraries`.
128
129 Most of the compiler support routines used by GCC are present in
130 :samp:`libgcc`, but there are a few exceptions. GCC requires the
131 freestanding environment provide ``memcpy``, ``memmove``,
132 ``memset`` and ``memcmp``.
133 Finally, if ``__builtin_trap`` is used, and the target does
134 not implement the ``trap`` pattern, then GCC emits a call
135 to ``abort``.
136
137 For references to Technical Corrigenda, Rationale documents and
138 information concerning the history of C that is available online, see
139 https://gcc.gnu.org/readings.html