C++ Multi-line comments using backslash

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.

Leave a Comment