C compiler and C99 standard - c

Which C compilers can compile C99 compliant source?

If I don't mess up gcc allow you to do so while using -std=gnu99 or -std=c99 option

Related

Which C stardard adopted given the GNU C compiler version?

Given that I know the compiler version:
ANSI C 5.3.0 - GNU C Compiler with options: -lm -lcrypt -O2 -pipe -ansi -DONLINE_JUDGE
How can I know which C standard(C89, C99, C11, C17/18) it adopted If I want to write C program that strictly follows the standard?
How can I know which C standard(C89, C99, C11, C17/18) it adopted
-ansi doesn't mean "give me standard C". It means "give me dinosaur C" and is 100% equivalent to using -std=c89. So if you compiled with -ansi, you are using C89, period.
If you don't use -ansi or -std=... options, then gcc defaults to these versions:
gcc before version 5.0.0 defaults to gnu90.
gcc between version 5.0.0 and up to < 8.0.0 defaults to gnu11.
gcc from 8.0.0 defaults to gnu17.
"gnu" being the C standard + non-standard extensions, in the above cases corresponding to C90, C11 and C17 respectively.
"gnu" mode is not strictly following the standard. In order to do that, you must compile with -std=cxx where xx is your desired version of C. This restricts gcc to a subset only containing standard C features.
But in order to actually make it strict when it comes to reporting compiler errors for invalid use of C, you must do -std=cxx -pedantic-errors.
gcc has a flag that lets you specify the version of the C standard to compile against.
-std=c89 for C89
-std=c99 for C99
-std=c11 for C11
The version you have probably doesn't have support for C17.
There's also:
-std=gnu89 for C89 with GNU extensions
-std=gnu99 for C99 with GNU extensions
-std=gnu11 for C11 with GNU extensions

What is the version of C used in gcc

What is the version of C used in GCC? C99, C11 or C90? I thought was the GCC use C99 but I was mistaken:
for(int i = 0; i < 100; i++){
...
}
error: ‘for’ loop initial declarations are only allowed in C99 mode.
According to 2 Language Standards Supported by GCC:
The default, if no C language dialect options are given, is -std=gnu90; this is intended to change to -std=gnu11 in some future release.
I believe the default is -std=gnu90. You could specify -std=c99 in your compiler flag to support this.
By default gcc uses C90 with GNU extension, this is all covered in the gcc docs Language Standards Supported by GCC, the flag for this would be -std=gnu90:
The default, if no C language dialect options are given, is -std=gnu90; this is intended to change to -std=gnu11 in some future release.
If you want C99 support than you should use -std=c99, although this does not mean gcc will not use extensions, so if you want to receive a warning when gcc using an extension you need to add -pedantic and -pedantic-errors to turn that into an error:
to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings)
Use -std=99
Before C99, you had to define the local variables at the start of a block. C99 imported the C++ feature that you can intermix local variable definitions with the instructions and you can define variables in the for and while control expressions.

Why gcc gives warning: implicit declaration of function qsort_r?

I do include<stdlib.h> where qsort_r is given. And I use gcc -std=c99 -O3 myfun.c -o myfun to compile.
It compiles, links and runs well. I don't know why I got this warning and what is potential risk of this warning ?
BTW, my compiler is gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1)
It does so because you use -std=c99 , there's no qsort_r function in stdlib.h in c99.
Use -std=gnu99 to make the extensions available, or add a #define _GNU_SOURCE to your source files before including the header files.
qsort_r is not supported by C99. Specification says nothing about it.
Language Standards Supported by GCC:
By default, GCC provides some extensions to the C language that on rare occasions conflict with the C standard1. See Extensions to the C Language Family. Use of the -std options listed above will disable these extensions where they conflict with the C standard version selected. You may also select an extended version of the C language explicitly with -std=gnu89 (for C89 with GNU extensions) or -std=gnu99 (for C99 with GNU extensions). The default, if no C language dialect options are given, is -std=gnu89; this will change to -std=gnu99 in some future release when the C99 support is complete. Some features that are part of the C99 standard are accepted as extensions in C89 mode.
1. Emphasis is mine

Are "Statement and Declarations in Expressions" specific to GNU C?

Are Statement and Declarations in Expressions specific to GNU C ? Or this feature is also included in C99 standard ?
It's a GCC extension. (See the GCC docs, e.g. here for gcc 4.3.3, for a full list of GCC extensions; and the C99 spec is available here.)
GCC will warn about such things if you use the -pedantic -std=c99 flags, e.g.:
$ cat foo.c
int main(void)
{
return ({ int a = 0; a; });
}
$ gcc -pedantic -std=c99 -c foo.c
foo.c: In function 'main':
foo.c:3: warning: ISO C forbids braced-groups within expressions
While this is not a C99 standard, this extension is not specific to gcc either.
For instance, the clang compiler and Intel C++ compiler support this extension.
It's a GNU C extension. That's what they mean by "may appear ... in GNU C." (my emphasis)

Why can't I use //-style comments in my C code?

I am using gcc (Ubuntu 4.4.1-4ubuntu9) to compile a program that I'm writing, but it seems to vomit whenever it sees a // comment in my code, saying:
interface.c :##: error: expected expression before â/â token<
Does the gcc compile mode I'm using forbid // comments?
$ gcc -g -ansi -pedantic interface.c structs.h -c -I. -I/home/me/project/h
Why?
// comments are not allowed in old (pre 99) C versions, use /**/ (or remove the -ansi, that is a synonym for the C89 standard)
See C++ comments in GNU compiler documentation.
In GNU C, you may use C++ style comments, which start with // and continue until the end of the line. Many other C implementations allow such comments, and they are included in the 1999 C standard. However, C++ style comments are not recognized if you specify an -std option specifying a version of ISO C before C99, or -ansi (equivalent to -std=c89).
(Emphasis is mine because some of the posts claim that // are not allowed in standard C whereas that is only true for pre-99 standards).

Resources