It depends on how perverted your comments are. I have an scc program to remove C and C ++ comments. I also have a test file for it, and I tried GCC (4.2.1 on MacOS X) with options in the currently selected answer - and GCC does not seem to cope with some of the badly killed comments in a use case.
NB: This is not a real life problem - people donβt write such awful code.
Consider (a subset of 36 of 135 lines) a test case:
/\ *\ Regular comment *\ / The regular C comment number 1 has finished. /\ \/ This is not a C++/C99 comment! This is followed by C++/C99 comment number 3. /\ \ \ / But this is a C++/C99 comment! The C++/C99 comment number 3 has finished. /\ \* This is not a C or C++ comment! This is followed by regular C comment number 2. /\ */ This is a regular C comment *\ but this is just a routine continuation *\ and that was not the end either - but this is *\ \ / The regular C comment number 2 has finished. This is followed by regular C comment number 3. /\ \ \ \ * C comment */
On my Mac, the output from GCC ( gcc -fpreprocessed -dD -E subset.c ):
/\ *\ Regular comment *\ / The regular C comment number 1 has finished. /\ \/ This is not a C++/C99 comment! This is followed by C++/C99 comment number 3. /\ \ \ / But this is a C++/C99 comment! The C++/C99 comment number 3 has finished. /\ \* This is not a C or C++ comment! This is followed by regular C comment number 2. /\ */ This is a regular C comment *\ but this is just a routine continuation *\ and that was not the end either - but this is *\ \ / The regular C comment number 2 has finished. This is followed by regular C comment number 3. /\ \ \ \ * C comment */
Output from 'scc':
The regular C comment number 1 has finished. /\ \/ This is not a C++/C99 comment! This is followed by C++/C99 comment number 3. /\ \ \ / But this is a C++/C99 comment! The C++/C99 comment number 3 has finished. /\ \* This is not a C or C++ comment! This is followed by regular C comment number 2. The regular C comment number 2 has finished. This is followed by regular C comment number 3.
Output from 'scc -C' (which recognizes double slash comments):
The regular C comment number 1 has finished. /\ \/ This is not a C++/C99 comment! This is followed by C++/C99 comment number 3. The C++/C99 comment number 3 has finished. /\ \* This is not a C or C++ comment! This is followed by regular C comment number 2. The regular C comment number 2 has finished. This is followed by regular C comment number 3.
SCC source code is now available on GitHub
The current version of SCC is 6.60 (from 2016-06-12), although Git versions were created 2017-01-18 (in the US / Pacific time zone). The code is available on GitHub at https://github.com/jleffler/scc-snapshots . You can also find snapshots of previous releases (4.03, 4.04, 5.05) and two preliminary releases (6.16, 6.50) - all of them are marked as release/x.yz
The code is still mainly developed under RCS. I am still working on how I want to use stderr.c or a similar mechanism to work with shared libraries like stderr.c and stderr.h (which can also be found at https://github.com/jleffler/soq ).,
Version 6.60 SCC attempts to understand the constructs of C ++ 11, C ++ 14, and C ++ 17, such as binary constants, numerical punctuation, raw strings, and hexadecimal floating point numbers. The default mode is C11. (Note that the value of the -C flag - mentioned above - switched between version 4.0x, which is described in the main body of the response, and version 6.60, which is currently the latest version.)