views:

380

answers:

6

I am using gcc (Unbuntu 4.4.1-4unbuntu9) 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?

+24  A: 

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

fortran
Short, accurate and to the point. Best answer so far, and likely the best conceivable.
JUST MY correct OPINION
+1  A: 

// is a C++ feature, and probably not available in ANSI-C mode. Try surrounding the comment with the C-style /* ... */ comment marker.

Andrew Medico
Given your use of "probably" and "try" it sounds like you are guessing, which I don't think is helpful in this case.
Bryan Oakley
+1  A: 

// is not ansi-C compliant.

Stefan Kendall
Someone really needs to read their ANSI spec. It's been legal C for 11 years.
JUST MY correct OPINION
+3  A: 

// comments are actually a C++ feature in origin, which is why -ansi disables them.

chaos
They are valid C too, for over the past 10 years.
Roger Pate
Yeah. Edited to express what I meant more precisely.
chaos
+15  A: 

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
For some of us, "Standard C" was standardized long before '99. Always wise to specify exactly which revision of the standard! :-)
Brian Knoblauch
@Brian: So... what we need is a standard... standard.
yodaj007
A: 

Judging by the output it looks like the compiler is choking on the foreign letter 'a' separated by a slash followed by another foreign 'a'. It could be looking for a comment and gives up. Try removing that comment and retry compiling again.

Hope this helps, Best regards, Tom.

tommieb75
That `â` thing is actually a separate, unrelated output encoding issue. The `â`s are not actually in his code.
chaos
^ This is exactly what is happening.
rlb.usa