C99 not default C- version for GCC? - c

Why does not GCC compile the C99 by default? I mean why is it necessary to add --std=c99 flag
everytime a code in C99 is written?

Edit: As of GCC 5, -std=gnu11 is the default. See Porting to GCC 5.
See C Dialect Options, gnu89 is the default.
`gnu89'
GNU dialect of ISO C90 (including some
C99 features). This is the default for
C code.
As #tsv mentioned, ISO C99 is not fully supported yet:
`c99'
`c9x'
`iso9899:1999'
`iso9899:199x'
ISO C99. Note that this standard is not yet fully supported; see http://gcc.gnu.org/c99status.html for more information. The names `c9x' and `iso9899:199x' are deprecated.
And also:
`gnu99'
`gnu9x'
GNU dialect of ISO C99. When ISO C99 is fully implemented in GCC, this will become the default. The name `gnu9x' is deprecated.

Perhaps because it still isn't fully implemented - see C99 status.
It also could be argued C99 features haven't been widely adopted, although that's something of a circular argument.

Use the command c99 to compile C programs.
The current POSIX standard specifies the command c99, so it should be available in most Unix-like systems.

The reason is that default configurations of gcc take a really long time to be changed, since every time a default configuration is changed, it can potentially break the compilation of valid programs (in this case valid c89 programs which are invalid in c99). Starting with gcc 5.0, the default C standard used by gcc will be gnu11, which is c11 with gnu extensions (see here):
The default mode for C is now -std=gnu11 instead of -std=gnu89.

Related

Changing clang to compile in C11 or higher?

I'm following a tutorial on learning C. I'm currently using a Mac with clang 11.0.3. It is in C99 by default (I'm getting the error: warning: implicit declaration of function 'of' is invalid in C99
[-Wimplicit-function-declaration].)
I'm using xcode 11.5.
How do I update/change the default of clang to C11 or higher?
You can look up the documentation with man clang on your system or online on the Clang website (https://clang.org → “Getting started” → “User's Manual” link on the left).
clang supports the -std option, which changes what language mode clang uses. The supported modes for C are c89, gnu89, c99, gnu99, c11, gnu11, c17, gnu17, c2x, gnu2x, and various aliases for those modes. If no -std option is specified, clang defaults to gnu17 mode.
Note that since you're seeing C99 as the language version, your IDE is likely passing a -std option already. I don't know where to control compiler options in Xcode but Setting C compile flags in xcode might help.
The Clang documentation is not always complete or clear. Many features of Clang mimic GCC, and GCC often has better documentation. The section on C dialect options in the GCC manual has more information about -std and its values. You need to be careful because Clang doesn't always work exactly like GCC, but the GCC documentation is often helpful as long as you remember that it may not always apply to Clang.
None of this will help you anyway because switching from C99 to C11 wouldn't fix this particular error. Implicit function declarations are a deprecated feature in C89 which was made invalid in C99, and of is not a new keyword in C11, so either your code is bad-style C89 or more likely there's a typo and of should not be a function name.

What is the difference between the "c99" and "gcc" commands with appropriate flags?

Up until today I always read on the Internet how gcc is the best compiler for C (at least for the student level of programing, followed closely by Clang).
However in "21st Century C" Mr Ben Klemens suggests that c99 is better(?) than running gcc -std=c99 (actual line is [page 11]: everybody else switched to C99 being the default a long
time ago...)
I wasn't able to find anything on the subject of c99 compiler, so my question is:
Is there any difference between those commands and if there are, which one is better?
EDIT: The standard C99 is clearly metioned in the paragraph, however from the beginning the suggested method of compiling is the command:
gcc erf.c -o erf -lm -g -Wall -O3 -std=gnu11
However on page 11 the author states:
The POSIX standard specifies that c99 be present on your system, so the
compiler-agnostic version of the above line would be:
c99 erf.c -o erf -lm -g -Wall -O3
This seems to suggest there is a difference in those 2 commands. I wasn't able to find any additional info nor was it clear to me from the text, what the second line is exactly (no man page for c99 on my Cygwin either).
C99 is the 1999 edition of the ISO C standard. It replaced the 1990 standard, and has been (officially, at least) replaced by the 2011 standard.
What you're asking about, though, is the c99 command (I've updated your question's title to clarify that).
POSIX specifies a c99 command. The requirements are documented here. It is "an interface to the standard C compilation system".
On typical Linux systems, the c99 command /usr/bin/c99 is a small shell script that invokes the gcc commmand. It invokes gcc with the -std=c99 option. It also checks whether the user has already specified an equivalent option, so it doesn't use the same option twice. If an incompatible option has been given, such as c99 -std=c90, it terminates with an error message.
Given such an implementation, the command
c99 [args]
is exactly equivalent to
gcc -std=c99 [args]
As I mentioned above, the C99 standard has been officially superseded by the C11 standard. gcc version 5 (the current latest release is 5.3.1) has reasonably good support but not 100% complete support for C11. POSIX has not (yet) specified a c11 command.
There's nothing wrong with using the C99 standard if you don't need C11-specific features -- or even the C90 standard if you don't need C99-specific features.
In my PDF copy of the book, the discussion about using c99 instead of gcc -std=c99 seems to be on page 10, not 11.
And what is being discussed is not that c99 is "better" than gcc, but that you might be able to more easily use C99-standard compiler features with the c99 command, since you don't then need to know the specific option to enable C99 features or whether the default for the compiler is C99 or C89.
On my system, the command c99 is just an alias or link for gcc that has the -std=c99 set by default (and complains if a non-C99 standard is specified with the -std= option). I imagine that or something similar is true on most systems with a c99 compiler command.
In fact, on my system c99 is a link to a shell script:
#! /bin/sh
# Call the appropriate C compiler with options to accept ANSI/ISO C
# The following options are the same (as of gcc-3.3):
# -std=c99
# -std=c9x
# -std=iso9899:1999
# -std=iso9899:199x
extra_flag=-std=c99
for i; do
case "$i" in
-std=c9[9x]|-std=iso9899:199[9x])
extra_flag=
;;
-std=*|-ansi)
echo >&2 "`basename $0` called with non ISO C99 option $i"
exit 1
;;
esac
done
exec gcc $extra_flag ${1+"$#"}
Try c99 --version on a typical Linux box. You will get the version and name of the compiler which is gcc.
c99 is just a shortcut to the c99 compliant compiler on your machine. That way you don't have to care about the actual compiler used. POSIX also requires some common command line options the compiler has to understand. If that is gcc, it shall enable c99 compliant features. This should be identical to gcc -std=c99.
gcc provides additional features which are enabled by default [1] when called by its native name and by the -std=gccXX option in addition to the CXX standard. For older versions, some of these extensions became part of the next C standard either directly or with slightly different syntax. A typical and appreciated extension for C90 is support for C++-style line-comments:
// this is not allowed in pure C90
For c99/gnu99 things are less obvious, but might still add some usefull features.
On other POSIX systems, e.g. Unix, you may find a different compiler. It shall still be available by the name c99.
Note that the current and only valid C standard is C11 since 2011. So if you want to use the new features (e.g. atomics, thread-support), you have to deviate from the pure POSIX-path. Yet it is likely POSIX might be updated some day.
[1] The default version of the C standard depends on the version of gcc. pre5 used C90, while gcc 5.x uses C11.

Should I use "-ansi" or explicit "-std=..." as compiler flags?

I've read that ANSI C is not exactly the same as ISO C and compilers may differ in interpretation of what "-ansi" is about. (gcc maps it to C90, clang maps it to C89) At the moment I would tend to use "-std=..." over "-ansi" as then it is explicitly shown which standard is used. As I am specifically interested in compiling on Linux, Windows and MAC, I fear some compilers could not understand "-std=..." but "-ansi". So are there any pros and cons for using the one over the other?
If you want the compiler to enforce the 1989 ANSI C standard, or equivalently the 1990 ISO C standard (they describe exactly the same language), you can safely use either -ansi or -std=c89.
The name -ansi is, strictly speaking, incorrect; it refers to the 1989 ANSI C standard, but ANSI itself considers that standard to be obsolete; it was replaced by the 1999 ISO C standard (which ANSI officially adopted shortly after it was released) which itself either has been, or soon will be, replaced by the new 2011 ISO C standard. But changing the meaning of the -ansi option would break too many Makefiles and build scripts.
The gcc 4.7 and later versions also recognize -std=c90 as a synonym for -std=c89. gcc 4.7 was released in March 2012, so -std=c90 is reasonably portable unless you need to allow for older versions of gcc.
-std=c99 enforces (most of) the 1999 ISO C standard. Since Microsoft in particular doesn't support C99 (even after all these years), using this option means the compiler won't warn you about use of C99-specific features that might not be supported elsewhere. gcc's C99 support is documented here.
gcc 4.7 has partial support for the new ISO C 2011 standard, with -std=c11. That support has improved in later releases, but is not yet complete. gcc C11 status is documented here, and is said to be similar to the level of C99 support.
There are more options, and a number of aliases for the ones I've mentioned; for example, the option -std=c9x was added before the 1999 ISO standard was finalized, and it's still supported; similarly, -std=c1x is a synonym for -std=c11.
I believe that clang is intended to be as compatible as possible with gcc, so it should support the same options with the same meanings (except perhaps for some of the newer ones, depending on which versions of gcc and clang you're using).
The gcc manual has the full details, with one section describing the supported standards and another specifying the various -ansi and -std=... options. The links are to the 4.7 version. You can also run info gcc (if you have the GNU info command and the gcc documentation installed), or you can see multiple versions of the manual here.
If you're going to use compilers other than gcc (and compilers that aim to be gcc-compatible), you'll have to read their documentation to find out how to enforce various versions of the C standard.
-ansi and -std= compiler flags may be shared by other compilers but they are gcc flags.
As of now -ansi is equivalent to -std=c89 in gcc but this may1) change in the future so I suggest you to use -std=c89 over -ansi. Indeed ISO c99 for example has also been ratified by ANSI.
You should note that c89 and c90 are essentially the same C Standard. c89 is the ANSI name while c90 is the ISO name.
From gcc page:
There were no technical differences between these publications, although the sections of the ANSI standard were renumbered and became clauses in the ISO standard. This standard, in both its forms, is commonly known as C89, or occasionally as C90, from the dates of ratification.
1) As noted by Keith Thompson in the comments, even though it's probably unlikely as it would break many build scripts.

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

I read here it is C90 with extensions. How can I know for sure?
Try below command and search for std
gcc -v --help | less
It will show you all the available options for your gcc.
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.
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.
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).
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.

compiling with c89 and c99 does gcc 4.4.2 default to c99?

I am using gcc 4.4.2 on linux
I am just wondering does gcc automatically default to compiling with c99 as its the latest standard?
How can I specify if I want to compile with c89 or c99?
Many thanks for any advice,
One reason not to use C99 by default is because this standard is not fully implemented in the compiler yet. However, I believe that you can set the default mode when compiling gcc, so if the choice of standards really matters for you, it's better always to pass the option.
The options are -std=c99 and -std=c89 respectively.
From the gcc(1) man page:
-std=
Determine the language standard. This option is currently only
supported when compiling C or C++.
....
c99
c9x
iso9899:1999
iso9899:199x
ISO C99. Note that this standard is not yet fully supported;
see <http://gcc.gnu.org/gcc-4.4/c99status.html> for more
information. The names c9x and iso9899:199x are deprecated.
gnu89
GNU dialect of ISO C90 (including some C99 features). This is
the default for C code.
gnu99
gnu9x
GNU dialect of ISO C99. When ISO C99 is fully implemented in
GCC, this will become the default. The name gnu9x is
deprecated.

Resources