views:

290

answers:

3

I'm using GCC 4.3.3 on Ubuntu 9.04 64-bit and was getting errors using C++-style comments in C code. When I say "by default" in the title, I mean simply invoking gcc test.c

According to the GCC 4.3.3 docs (here), this is supported...yet I got the errors anyway.

These errors went away with a simple -std=c99 addition to my compile string, so my problem is solved. Curious if any GCC experts out there had an explanation for this, as it seems to me to be a clear contradiction to the documentation.

#include <stdio.h>
// this is a comment

int main( void )
{
   return 0;
}
+4  A: 

Quote from http://gcc.gnu.org/onlinedocs/gcc/Standards.html#Standards

The default, if no C language dialect options are given, is -std=gnu89

And // comments are recognized by -std=gnu89

pmg
+5  A: 

It's possible Ubuntu is overriding the default, which should be gnu89. Certainly I don't get that with my copy of GCC 4.3 (on Debian).

% echo '// foo' | gcc-4.3 -x c -c -
% echo '// foo' | gcc-4.3 -std=gnu89 -x c -c -    
% echo '// foo' | gcc-4.3 -std=c89 -x c -c -    
<stdin>:1: error: expected identifier or '(' before '/' token
Nicholas Riley
+2  A: 

By defualt GCC is using C89/90 standard with GCC extensions. Strictly speaking by default it is not adhering to any specific standard, since by default it will not issue any diagnostic messages in situations when such messages are required by the standard. You need to run gcc in -ansi -pedantic mode (possibly also -Wall) in order to make it stick to the standard. And in this case you'll have, once again, C89/90.

AndreyT