39

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?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
rlb.usa
  • 14,942
  • 16
  • 80
  • 128
  • 5
    It's important to realize that the term "ANSI C" is commonly (and *incorrectly*) used to refer to the language described by the 1989 ANSI C standard. That standard was replaced by the 1990 ISO C standard (which describes the same language), and has since been superseded by the 1999 and 2011 ISO C standards. ANSI officially adopted all three ISO C standards after they were published. So strictly speaking, "ANSI C" refers to ISO C 2011 -- which is more clearly called "ISO C". Older standards can still be relevant, but it's clearer to refer to them by year (C89/C90, C99, C11). – Keith Thompson Jun 03 '14 at 18:36
  • ANSI C can refer to any US adoption of ISO C... so *ANSI ISO/IEC 9899:2011* is a form of ANSI C... but the usual understanding is ANSI C is *ANSI X3.159-1989* which preceded ISO C – Andrew Nov 29 '21 at 13:23

2 Answers2

92

// comments are not allowed in old (pre 99) C versions, use /**/ (or remove the -ansi, that is a synonym for the C89 standard)

fortran
  • 74,053
  • 25
  • 135
  • 175
  • 1
    Short, accurate and to the point. Best answer so far, and likely the best conceivable. – JUST MY correct OPINION Jun 12 '10 at 06:18
  • 3
    Without `-ansi`, gcc currently supports C89 with GNU-specific extensions; one of those extensions happens to be `//` comments. With `-std=c99`, it attempts to conform to the 1999 ISO C standard, which requires support for `//` comments. – Keith Thompson Apr 24 '13 at 22:13
25

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).

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339