Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

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?

like image 858
rlb.usa Avatar asked Feb 08 '10 17:02

rlb.usa


People also ask

How comments can be used in C program?

Comments in C Comments can be used to explain code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Comments can be singled-lined or multi-lined.

What is the difference between comments and /* style comments?

What is the difference between // comments and /* style comments? The double-slash comments (//) expire at the end of the line. Slash-star (/*) comments are in effect until a closing comment mark (*/).


2 Answers

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

like image 20
Sinan Ünür Avatar answered Sep 21 '22 08:09

Sinan Ünür


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

like image 107
fortran Avatar answered Sep 21 '22 08:09

fortran