How to find out which ANSI C standard my gcc works with by default?
Asked Answered
W

6

10

I read here it is C90 with extensions. How can I know for sure?

Wellhead answered 17/9, 2011 at 22:45 Comment(0)
H
6

Use the -std= flag to specify a language dialect: c89, c99, c++98, etc. Note that c90 is equivalent to c89.

As a shorthand -ansi causes -std=c89 in C mode and -std=c++98 in C++ mode.

Use -pedantic to enable all diagnostics that the standards require.

The defaults are gnu89 for C and gnu++98 for C++. Consult the manual for detailed descriptions of the various dialects.

Update:

For gcc version greater than 5.0, defualt is gnu11

Hairline answered 17/9, 2011 at 22:47 Comment(8)
Which of these is the default? Is there a option in gcc which tells me see the default version?Wellhead
@Bruce: Added! Note that the C++ default may change eventually :-)Hairline
What do you mean by C mode and C++ mode? Recently I read that GCC 4.8 Aims To Switch To C++ Mode By Default. What does it mean?Decadence
@KerrekSB: If you know, please answer my question posed in my previous comment. I want to know it, on the fly, without creating a topic on it. :DDecadence
@Nawaz: That's something completely different - that refers to the way GCC itself is built. There's been an option to build it as C++ for a long time, but it's always been "experimental".Hairline
@KerrekSB: What does "build it as C++" mean in this context? By "build", I understand compiling, resolving and creating machine code (DLL/EXE/LIB/etc) out of the code, but then "build it as C++" doesn't make sense to me, hence the confusion. Do you mean "program/code it in C++"?Decadence
@Nawaz: It means that the compiler itself will be compiled with g++, not with gcc. I assume that means that the compiler's source code is treated as C++. Does that make sense?Hairline
This answer is now outdated. Release 5.0 changed the default to -std=c11. See my answer.Tamarin
H
11

Try below command and search for std

gcc -v --help | less

It will show you all the available options for your gcc.

Habana answered 28/12, 2012 at 17:56 Comment(2)
But not which option is used by default.Taphouse
On a Unix-like system, this will extract just the relevant bits from gcc: gcc -v --help 2> /dev/null | grep 'Conform.*C standard'Schlep
O
7

Read the manpage. On my computer (OSX 10.7, gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)):

-std=
        Determine the language standard. This option is currently only supported
        when compiling C or C++.

        A value for this option must be provided; possible values are

....

        gnu89
                Default, ISO C90 plus GNU extensions (including some C99 features).

....

        gnu++98
                The same as -std=c++98 plus GNU extensions. This is the default for C++ code.

Oligarchy answered 17/9, 2011 at 22:50 Comment(3)
Works for GCC for me but not for Clang.Pirtle
Ummm man gcc says that there isn't a man page for gcc. Wat?Spoil
I'd prefer to get that information from the compiler itself in case we're working with multiple toolchains and MANPATH isn't set for the compiler in PATH.Nosedive
H
6

Use the -std= flag to specify a language dialect: c89, c99, c++98, etc. Note that c90 is equivalent to c89.

As a shorthand -ansi causes -std=c89 in C mode and -std=c++98 in C++ mode.

Use -pedantic to enable all diagnostics that the standards require.

The defaults are gnu89 for C and gnu++98 for C++. Consult the manual for detailed descriptions of the various dialects.

Update:

For gcc version greater than 5.0, defualt is gnu11

Hairline answered 17/9, 2011 at 22:47 Comment(8)
Which of these is the default? Is there a option in gcc which tells me see the default version?Wellhead
@Bruce: Added! Note that the C++ default may change eventually :-)Hairline
What do you mean by C mode and C++ mode? Recently I read that GCC 4.8 Aims To Switch To C++ Mode By Default. What does it mean?Decadence
@KerrekSB: If you know, please answer my question posed in my previous comment. I want to know it, on the fly, without creating a topic on it. :DDecadence
@Nawaz: That's something completely different - that refers to the way GCC itself is built. There's been an option to build it as C++ for a long time, but it's always been "experimental".Hairline
@KerrekSB: What does "build it as C++" mean in this context? By "build", I understand compiling, resolving and creating machine code (DLL/EXE/LIB/etc) out of the code, but then "build it as C++" doesn't make sense to me, hence the confusion. Do you mean "program/code it in C++"?Decadence
@Nawaz: It means that the compiler itself will be compiled with g++, not with gcc. I assume that means that the compiler's source code is treated as C++. Does that make sense?Hairline
This answer is now outdated. Release 5.0 changed the default to -std=c11. See my answer.Tamarin
T
2

No version of gcc fully conforms to any ANSI or ISO C standard by default. The default is always equivalent to -std=gnuNN, supporting the given standard with GNU-specific extensions. Prior to version 5 (and going back a number of years), the default was -std=gnu90. Starting with version 5, the default is -std=gnu11.

There have been three ISO C standards: C90, C99, and C11. (C95 was a minor amendment to C90.) The first ANSI C standard was published in 1989, and is known as C89; it describes the same language as the ISO C90 standard.

To find out the default language version for the gcc you have running, type

 info gcc

search for the phrase "The default".

This assumes that you have the gcc documentation installed, that the documentation shown by info gcc matches the version of gcc you're running, and that the wording is similar enough that searching for "The default" will find the correct information. None of these is guaranteed. Older gcc manuals may be organized differently.

More reliably, run

gcc --version

to see what version you're running, then visit the GCC online documentation page and read the documentation for the version you're running.

More simply, if you have a version before 5.0, the default is -std=gnu89; otherwise the default is -std=gnu11. (That may change in a future release, but only some time after a new ISO C standard is published and the gcc maintainers have had time to implement it and decide to make it the default. Don't hold your breath.)

Or you can avoid the question altogether by specifying the version you want using the -std=... command-line option.

Tamarin answered 4/7, 2016 at 23:9 Comment(0)
E
0

You can also check one of the predefined Macros, for example whether your GCC asks for ANSI C (STRICT__ANSI) by default (i.e. without any cli arguments).

Erse answered 17/9, 2011 at 22:51 Comment(1)
No version of gcc is fully conforming to any ANSI/ISO standard by default.Tamarin
S
0

You can use the value of the __STDC_VERSION__ macro found with

echo | gcc -D -dM - | grep STDC

Use STDC and not the full __STDC_VERSION__ because __STDC_VERSION__ would not be defined under C90.

Note that this will not tell you if GNU extensions are supported or not. They are normally enabled and would require specific gcc options such as -std=c17 -pedantic -pedantic-errors to disable. Full explanation of GCC's behavior with respect to how command-line arguments affect the application of standards during compilation is well beyond the scope of this answer.

(Yes, there is another, older, answer that alludes to this with a link-only answer. This answer is meant to provide an actual example of how to extract the information along with supporting quotes from standards documents.)


Supporting Documentation

Per (draft) C11 6.10.8.1 Mandatory macros, paragraph 1:

The following macro names shall be defined by the implementation:

...

__STDC_VERSION__
   The integer constant 201ymmL.

Footnote 178 further explains

This macro was not specified in ISO/IEC 9899:1990 and was specified as 199409L in ISO/IEC 9899/AMD1:1995 and as 199901L in ISO/IEC 9899:1999. The intention is that this will remain an integer constant of type long int that is increased with each revision of this International Standard.

The draft C 23, err, 24 standard(warning: PDF!) has this:

6.10.9.1 Mandatory macros

1 The following macro names shall be defined by the implementation:

...

__STDC_VERSION__ The integer constant 202311L.

with footnote 227 adding that the history of __STDC_VERSION__ can be found in Annex M of the standard:

See Annex M for the values in previous revisions. The intention is that this will remain an integer constant of type long int that is increased with each revision of this document.

The earliest documented version is

...

M.6 First Edition, Amendment 1

Major changes in the amendment to the first edition (__STDC_VERSION__ 199409L) included

...

Surveillance answered 3/8, 2024 at 21:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.