80

When I ask to see the current version of cc I get this.

$ cc --version
cc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ 

What I would like to know is which of c89, c90, c99 or c11 is being used.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
ncmathsadist
  • 4,684
  • 3
  • 29
  • 45
  • 22
    C89 and C90 are exactly the same language. The 1989 ANSI C standard and the 1990 ISO C standard differ only in some of the (non-normative) introductory material and the renumbering of the sections. The C99 and C11 standards were originally released by ISO and then adopted by ANSI. (The term "ANSI C" still tends to refer to C89/C90, though officially C11 is the current ANSI standard.) – Keith Thompson Feb 06 '13 at 19:35

7 Answers7

105

This is explained in depth in the gcc manual, available (if it's installed) by typing info gcc or online here. The relevant section of the current manual is here, but it may or may not correspond to the version you're using.

Some of this information has changed since I posted this answer in 2013, and will continue to change. If you're reading this around 2023, the current version is probably -std=gnu17 (C17 with GNU-specific extensions; C17 is a minor update to C11). You should check the documentation for the version you're using. The C23 standard has not yet been released, but should be out Real Soon Now. I can't predict when gcc will switch to -std=gnu23 as its default. If you're reading this in the distant future, let us know how things turned out.

gcc releases from 3.0 to 4.9.4 default to -std=gnu89 or -std=gnu90.
gcc releases from 5.5 to 10.4 default to -std=gnu11 (they skipped -std=gnu99, though you can still specify it).
gcc releases 11.3 and 12.2 default to std=gnu17.


By default, gcc does not conform to any of the ANSI/ISO C standards. The current default is equivalent to -std=gnu17, which is the 2017 standard with GNU-specific extensions. (Some diagnostics required by the language standard are not issued.) Earlier releases of gcc have defaulted to -std=gnu90 or -std=gnu11.

If you want standard conformance, you can use any of the following:

-std=c90 -pedantic
-std=c99 -pedantic
-std=c11 -pedantic
-std=c17 -pedantic

-std=c90 can also be spelled -ansi, -std=c89, or -std=iso9899:1990.

-std=iso9899:199409 supports the C90 standard plus the 1995 amendment, which added a few minor features (all of which are also in C99).

-std=c99 can also be spelled -std=c9x or -std=iso9899:1999 (the name c9x was used before the standard was published). C99 support is not quite complete, but it's close.

-std=c11 can also be spelled -std=c0x or -std=iso9899:2011 (the name c0x was used before the final standard was published; it was wrongly assumed that x would not exceed 9). C11 support is also incomplete; the current status is summarized here.

The -pedantic option causes gcc to print required diagnostics for violations of constraints and syntax rules. In some cases, those diagnostics are merely warnings -- and there's no easy way to distinguish between those warnings and other warnings that aren't required by the language. Replace -pedantic by -pedantic-errors to cause gcc to treat language violations as fatal errors.

A quick history of the standard:

  • C89 was the first official C standard, published by ANSI in 1989.
  • C90 was the ISO version of the standard, describing exactly the same language as C89. ANSI officially adopted ISO's version of the standard. There were two Technical Corrigenda, correcting some errors.
  • C95 was an amendment to C90, adding a few features, mainly digraphs and wide character support. As far as I know, a merged version was never published.
  • C99 was issued by ISO in 1999. There were three Technical Corrigenda.
  • C11 was issued by ISO in 2011. There has been one Technical Corrigendum, fixing the definitions of __STDC_VERSION__ and __STDC_LIB_EXT1__.
  • C17 was issued by ISO in 2017, and was only a minor update to C11.
  • C23 will be issued in 2023. (As I write this, it's close to being finalized, but the document is still being edited.)

ANSI did not issue its own versions of the 1999 or later standards, adopting the ISO standards instead.

N1256 is a freely available draft of the C99 standard, with the 3 Technical Corrigenda merged into it.

N1570 is a freely available draft of the C11 standard. There are some minor differences between it and the published C11 standard, plus one Technical Corrigendum. For more details, see my answer to this question.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 6
    Regarding differences between N1570 and C11, see http://stackoverflow.com/q/8631228/95580. There were no differences: they even forgot to update `__STDC_VERSION__` and `__STDC_LIB_EXT1__`! (This was fixed in Cor 1:2012; see [stackoverflow.com/q/13914050/95580](http://stackoverflow.com/q/13914050/95580).) – J. C. Salomon Mar 19 '13 at 22:17
  • 1
    @J.C.Salomon: In fact there was one semantically significant change. I've updated the last paragraph of my answer to link to the same question you linked to, and to my answer. – Keith Thompson Feb 22 '15 at 19:24
  • @KeithThompson can we dare to ask for a 2021 update? C17 and perhaps "C23, so far". Thanks. – Chef Gladiator Apr 17 '21 at 12:33
  • 1
    @ChefGladiator It's still "C2X" at the moment. Not sure if 2023 was the original intended year, but I've seen no sign of them aiming for that one in particular. – AJM May 05 '21 at 10:59
  • 1
    @ChefGladiator The default for GCC 9.4.0 (Ubuntu default) is gnu11. GCC 12.1.0 has gnu17 by default. (see https://gcc.gnu.org/onlinedocs/gcc-12.1.0/gcc/Standards.html#C-Language) – c21253 Jul 14 '22 at 00:55
  • The current default C version as of gcc 13.0.1 is `-std=gnu17`, which is C17 plus GNU extensions. C17 has no new features over C11; it's just a collation of corrections to defects in the C11 standard. These corrections are also applied when gcc targets C11. – Hydrargyrum Feb 06 '23 at 00:12
  • 1
    @Hydrargyrum Not exactly current, since gcc 13 hasn't been released yet, but it should be out Real Soon Now. – Keith Thompson Feb 06 '23 at 05:45
38

useful information from info gcc for gcc6 and https://gcc.gnu.org/onlinedocs/gcc-5.4.0/gcc/Standards.html#Standards for gcc5

gcc version 6.3.1 - 10.1.0

2.1 C Language
==============
 The default, if no C language dialect options are given, is
'-std=gnu11'.

2.2 C++ Language
================
 The default, if no C++ language dialect options are given, is
'-std=gnu++14'.

gcc version 5.4.0

2.1 C Language
==============
The default, if no C language dialect options are given, is -std=gnu11

2.2 C++ Language
================
The default, if no C++ language dialect options are given, is -std=gnu++98

For C, default mode remains std=gnu11, but for C++ it has jumped from std=gnu++98 to std=gnu++14

Varun Garg
  • 2,464
  • 23
  • 37
20

Minimal test program

If you feel like finding it out empirically without reading any manuals.

c.c

#include <stdio.h>

int main(void) {
#ifdef __STDC_VERSION__
    printf("__STDC_VERSION__ = %ld \n", __STDC_VERSION__);
#endif
#ifdef __STRICT_ANSI__
    puts("__STRICT_ANSI__");
#endif
    return 0;
}

Test with:

#!/usr/bin/env bash
for std in c89 c99 c11 c17 gnu89 gnu99 gnu11 gnu17; do
  echo $std
  gcc -std=$std -o c.out c.c
  ./c.out
  echo
done
echo default
gcc -o c.out c.c
./c.out

Outcome:

c89
__STRICT_ANSI__

c99
__STDC_VERSION__ = 199901
__STRICT_ANSI__

c11
__STDC_VERSION__ = 201112
__STRICT_ANSI__

c17
__STDC_VERSION__ = 201710
__STRICT_ANSI__

gnu89

gnu99
__STDC_VERSION__ = 199901

gnu11
__STDC_VERSION__ = 201112

gnu17
__STDC_VERSION__ = 201710

default
__STDC_VERSION__ = 201710

Conclusion: gnu17 is used by default:

For an explanation of -std=gnu* vs -std=c* see also: What are the differences between -std=c++11 and -std=gnu++11?

C++

main.cpp

#include <iostream>

int main(void) {
#ifdef __cplusplus
    std::cout << __cplusplus << std::endl;
#endif
#ifdef __STRICT_ANSI__
    std::cout << "__STRICT_ANSI__" << std::endl;
#endif
    return 0;
}

Test with:

#!/usr/bin/env bash
for std in c++98 c++11 c++14 c++17 gnu++98 gnu++11 gnu++14 gnu++17; do
  echo $std
  g++ -std=$std -o cpp.out cpp.cpp
  ./cpp.out
  echo
done
echo default
g++ -o cpp.out cpp.cpp
./cpp.out

Outcome:

c++98
199711
__STRICT_ANSI__

c++11
201103
__STRICT_ANSI__

c++14
201402
__STRICT_ANSI__

c++17
201703
__STRICT_ANSI__

gnu++98
199711

gnu++11
201103

gnu++14
201402

gnu++17
201703

default
201402

Conclusion: gnu++14 is the default:

  • __cplusplus: macro defined by the C++ standard including on C++98 onwards

Tested on Ubuntu 18.10, GCC 8.2.0. GitHub upstream.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • What's the difference between `gnu17` and `c17`? Is there much difference? – Ravi Raj Jul 24 '20 at 10:59
  • @RaviRaj `gnu*` is `c*` + all GNU language extensions, there must be a few dozen of them, `c*` is strict ANSI, see also: https://stackoverflow.com/questions/10613126/what-are-the-differences-between-std-c11-and-std-gnu11 – Ciro Santilli OurBigBook.com Jul 24 '20 at 12:26
3

The first line will give your GCC version (4.7.2)

(Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2

When you compile your code, you can specify which C/C++ revision you want to use, by adding -std=c99 or -std=c99 ...

Note gnu89 is used by default.

ablm
  • 1,265
  • 10
  • 19
1

One thing to be aware of, the -std= option to gcc can not be used to "sandbox" the compiler into not supporting constructs from later versions of standard C. This is true with or without -pedantic

You can not depend upon on gcc -std=c89 -pedantic to give you errors or warnings if you try to compile using some C99 code constructs. In some cases it will, in others it will not. For example, it will happily compile code that uses the %zu format specifier in a printf() call, even though it wasn't added until C99.

Randy Howard
  • 2,165
  • 16
  • 26
  • For that specific example, it is because `printf` is a library function and technically, the compiler doesn't care. Although many compilers do have a lint check for the format string, diagnosing problems here isn't required. – Zan Lynx Jul 18 '20 at 05:04
1

I found a way to query C++ version and adapted it for C:

gcc -dM -E -x c  /dev/null | grep -F __STDC_VERSION__
anton_rh
  • 8,226
  • 7
  • 45
  • 73
-2

Default gcc command is the GNU dialect of ISO C90 (including some C99 features). This is the default for C code.