Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable C++ styled comments in gcc while leaving ANSI enabled?

Tags:

c++

c

comments

gcc

This has just come up as a question where I worked so I did a little digging and the answer is a ExpertsExchange one. So I hand you over to the original question asker, Manchung:

I have a project written in pure C which is to be used in embedded system. So, I use pure C to minimize the code size.

When I compile the project, I use the -ansi flag in order to make sure the code complies with the ANSI standard. However, the down side of using this ansi flag is that I am only allowed to use C styled comments (/*comments */). This is giving me a headache when I need to use nested comments.

So, my question is: what switches/flags can I use to allow me to use C++ styled comments (// comments) while keeping the ANSI checking enabled at the same time?

Which pretty much sums my question up too.

like image 880
graham.reeds Avatar asked Nov 10 '08 10:11

graham.reeds


2 Answers

On recent releases of gcc, -ansi is documented as being the same as -std=c89. The new comment syntax is only available with the C99 standard, so -std=c99 would allow it.

There is also -std=gnu89, which is the same as -std=c89 but allowing all gcc extensions (including the C++-style comment syntax, which was a GNU extension long before it was added to the standard).

Also look at the -pedantic flag, which could give you some useful warnings.

References:

  • http://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html
  • http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
like image 192
CesarB Avatar answered Oct 04 '22 15:10

CesarB


If you want to use C++ style comments merely because you want to comment out blocks, and get a headache about nesting /* ... */, you can use this technique:

#if 0
... code ...
#endif

which will actually also do the job.

like image 25
Johannes Schaub - litb Avatar answered Oct 04 '22 16:10

Johannes Schaub - litb