Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace C style comments by C++ style comments

Tags:

c++

comments

How can I automatically replace all C style comments (/* comment */) by C++ style comments (// comment)?

This has to be done automatically in several files. Any solution is okay, as long as it works.

like image 244
compie Avatar asked Feb 12 '09 16:02

compie


People also ask

How do you comment comments in C?

Single-line comments start with two forward slashes ( // ). Any text between // and the end of the line is ignored by the compiler (will not be executed).

Which styles are used for comments in C++?

Single-line comments (informally, C++ style), start with // and continue until the end of the line. If the last character in a comment line is a \ the comment will continue in the next line. Multi-line comments (informally, C style), start with /* and end with */ .

What are the two types of comments in C and how do they differ?

In C/C++ there are two types of comments :Single line comment. Multi-line comment.

How do you comment multiple lines in C?

You can comment out one or more lines of code in any C/C++ editor view. The leading characters // are added to the beginning of each line when commenting one or more lines of code. You can also block comment multiple lines of code using the characters /* */ .


2 Answers

This tool does the job: https://github.com/cenit/jburkardt/tree/master/recomment

RECOMMENT is a C++ program which converts C style comments to C++ style comments.

It also handles all the non-trivial cases mentioned by other people:

This code incorporates suggestions and coding provided on 28 April 2005 by Steven Martin of JDS Uniphase, Melbourne Florida. These suggestions allow the program to ignore the internal contents of strings, (which might otherwise seem to begin or end comments), to handle lines of code with trailing comments, and to handle comments with trailing bits of code.

like image 194
compie Avatar answered Sep 28 '22 17:09

compie


This is not a trivial problem.

int * /* foo 
  /* this is not the beginning of a comment.

int * */ var = NULL;

What do you want to replace that with? Any real substitution requires sometimes splitting lines.

int * // foo
  // this is not the beginning of a comment.
// int *
var = NULL;
like image 42
Darron Avatar answered Sep 28 '22 18:09

Darron