1

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;
}
Kyle Walsh
  • 2,774
  • 4
  • 27
  • 25
  • Can you post the complete errors? – pmg Nov 01 '09 at 16:58
  • @pmg error: expected identifier or ‘(’ before ‘/’ token – Kyle Walsh Nov 01 '09 at 17:48
  • Well '/' is not a C++ style comment character! It should be "\\". So this is a different error altogether. Post the code and the error log (by editing your post *not* in a further comment). It seems to me that the title of the post may no longer reflect your actual problem. – Clifford Nov 01 '09 at 19:53
  • 1
    @Clifford I'm assuming you made a typo there. You aren't really saying that the line '// this is a comment' (removing the single quotes) would not leave a comment, are you? "\\" is most certainly not how you start comments. – Kyle Walsh Nov 01 '09 at 21:41
  • @Clifford Re: the titling of the question, I'll make an edit for proper scope. Thanks. – Kyle Walsh Nov 01 '09 at 21:55

3 Answers3

5

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
  • 43,532
  • 6
  • 101
  • 124
4

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
  • 106,608
  • 13
  • 126
  • 198
3

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765