Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Multi-line comments using backslash

Tags:

Can // style comments be continued to the next line by using a back slash, like multi-line macros? E.g.

// here is a comment \
   and this is more comments \
const char* x = "hello";  // this line of "code" is actually still a comment
int x = 5; // and now an actual line of code
like image 847
ggg Avatar asked Aug 14 '11 20:08

ggg


People also ask

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 /* */ .

Is there a symbol for multiple line comments?

/* */ (multiline comment) Multiline comments are used for large text descriptions of code or to comment out chunks of code while debugging applications. Comments are ignored by the compiler.

Which symbol is used for multi time comment?

Rather, they exist to demonstrate specific concepts in a concise manner. The /* and */ pair of symbols denotes a C-style multi-line comment. Everything in between the symbols is ignored.


2 Answers

Yes. Lines terminated by a \ are spliced together with the next line very early in the process of translation. It happens at phase 2 of translation, before comment removal and before preprocessor has a chance to do its work.

Comment recognition and removal takes place at phase 3. For this reason you can turn a // comment into what looks like a multi-line comment by using the \. This usually fools most syntax-highlighting source code parsers.

Preprocessor works at phase 4.

This all means that you can "multiline" virtually anything using the \, including comments and preprocessor directives

#\
d\
e\
f\
i\
n\
e \
ABC \
int i

int main() {
A\
B\
C = 5;
}

P.S. Please note that the terminating \ does not introduce any whitespace into the spliced line. This should be taking onto account when writing multi-line comments using the \ feature. For example, the following comment

// to\
get\
her

stands for the single word "together" and not for three separate words "to get her". Obviously, incorrect use of \ in comments might drastically obfuscate and even distort their intended meaning.

like image 108
AnT Avatar answered Oct 08 '22 02:10

AnT


Here's an excellent reason not to do this. The following program prints "This will appear".

#include <iostream>
int main()
{
    std::cout << "This "
    // A comment ... \ 
    << "will appear"
    // Another comment ... \
    << ", but this won't"
    << std::endl;
}

Why? Because the first \ is followed by a blank, and so it's just part of the comment, not a line-splicing character. The program's behavior can quietly and significantly change due to invisible trailing white space.

An even better reason not to do this: g++ gets it wrong, even with -pedantic. When I compile this program using g++ the output is just "This"; the trailing white space after the first \ is ignored. In my opinion this is how it should work, but it's not what the language standard says. (Line splicing happens in translation phase 2. I suppose one might argue that the trailing blanks could be deleted in phase 1, but I'm not convinced that that's a valid argument -- and I don't know whether the gcc authors have actually made that argument.) In any case, g++ 4.5.2 and Sun CC version 5.5 disagree with each other.

If you want multi-line comments, either use /* ... */, or insert a // at the beginning of each line. I prefer the latter, because it's much easier to tell that a given line is part of the comment. (Actually it's multiple single-line comments.) Any decent editor should let you do this without typing // N times for N lines. Or, if you're commenting out a block of code, use #if 0 ... #endif.

like image 24
Keith Thompson Avatar answered Oct 08 '22 02:10

Keith Thompson