Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of multi-line comment warnings in C?

Tags:

I'm working on a C file for a homework assignment and I thought it might help the graders if I made my answers visible like so:

//**********|ANSWER|************\\ //blah blah blah, answering the //questions, etc etc 

and found when compiling with gcc that those backslash characters at the end of the first line seemed to be triggering a "multi-line comment" warning. When I removed them, the warning disappeared. So my question is twofold:

a) how exactly does the presence of the backslash characters make it a "multi-line comment", and
b) why would a multi-line comment be a problem anyway?

like image 290
superexcellent12 Avatar asked Sep 30 '13 23:09

superexcellent12


People also ask

What is the symbol for multi-line comment?

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

What is comment line in C programming?

In programming, comments are hints that a programmer can add to make their code easier to read and understand. For example, #include <stdio.h> int main() { // print Hello World to the screen printf("Hello World"); return 0; }

What is used to write multi-line comments?

Multi-line comments (informally, C style), start with /* and end with */ .

What is single-line and multi-line comment?

Single-line comments begin with a double hyphen ( - - ) anywhere on a line and extend to the end of the line. Multi-line comments begin with a slash-asterisk ( /* ), end with an asterisk-slash ( */ ), and can span multiple lines.


2 Answers

C (since the 1999 standard) has two forms of comments.

Old-style comments are introduced by /* and terminated by */, and can span a portion of a line, a complete line, or multiple lines.

C++-style comments are introduced by // and terminated by the end of the line.

But a backslash at the end of a line causes that line to be spliced to the next line. So you can legally introduce a comment with //, put a backslash at the end of the line, and cause the comment to span multiple physical lines (but only one logical line).

That's what you're doing on your first line:

//**********|ANSWER|************\\ 

Just use something other than backslash at the end of the line, for example:

//**********|ANSWER|************// 

Though even that is potentially misleading, since it almost looks like an old-style /* .. */ comment. You might consider something a little simpler:

/////////// |ANSWER| //////////// 

or:

/**********|ANSWER|************/ 
like image 113
Keith Thompson Avatar answered Sep 20 '22 13:09

Keith Thompson


The compiler simply tells you that you might have inadvertently commented-out the next line of code by ending the previous comment line with \, which is a line continuation character in C. This causes the second line to get concatenated with the first. This in turn makes the // comment to actually comment-out both original lines. In your case it is not a problem, since the next line is a comment as well.

But if the next line was not intended to be a comment, then you might have ended up with "weird behavior": compiler ignoring the second line for no apparent reason. The situation is often complicated by the fact that some syntax-highlighting code editors do not detect this situation and fail to highlight the next line as a comment.

Generally, for this specific reason, it is not a good idea to abuse the \ character as code level. Use it only if you really have to, i.e. only if you really want to stitch several lines into one.

like image 34
AnT Avatar answered Sep 21 '22 13:09

AnT