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

I use gcc (Ubuntu 4.4.1-4ubuntu9) to compile the program I am writing, but it seems to vomit whenever it sees // a comment in my code, saying:

 interface.c :##: error: expected expression before Γ’/Γ’ token< 

Does gcc compile mode use I deny // comments?

 $ gcc -g -ansi -pedantic interface.c structs.h -c -I. -I/home/me/project/h 

Why?

+23
c
Feb 08 '10 at 17:33
source share
3 answers

// comments are not allowed in older (up to 99) versions of C, use /**/ (or remove -ansi , which is synonymous with the C89 standard)

+60
Feb 08 '10 at 17:35
source share

See C ++ comments in the GNU compiler documentation.

In GNU C, you can use C ++ style comments that start with // and continue to the end of the line. Many other C implementations allow such comments, and they are included in the C # 1999 standard . However, C ++ style comments are not recognized if you specify the -std option to specify the ISO C version prior to C99 or -ansi (equivalent to -std=c89 ).

(My emphasis is because some posts claim that // not allowed in standard C, whereas this is true only for pre-99 standards).

+20
Feb 08 '10 at 17:36
source share

// comments are actually C ++ signs in origin, so -ansi disables them.

+4
Feb 08 '10 at 17:35
source share



All Articles