Which of the following is not a correct C++ comment ?
A/*comment*/
B/*comment*\\
C//'comment*/
D/*//comment/*/
Answer:
B. /*comment*\\
Read Explanation:
C++ Comment Syntax Overview
- Single-line comments: Represented by
//. Any text following this marker on the same line is ignored by the compiler. - Multi-line (Block) comments: Represented by
/* ... */. Everything placed between these delimiters is treated as a comment, allowing for documentation that spans multiple lines or portions of a line.
Key Rules and Constraints
- Nested Comments: C++ does not support nested block comments. Attempting to wrap a block comment within another (e.g.,
/* /* ... */ */) will result in a compilation error because the compiler interprets the first*/as the end of the entire comment block. - Invalid Syntax: Using syntax markers incorrectly, such as mixing styles or omitting delimiters, leads to syntax errors. For example, using
//to start a multi-line block without repeating the marker on each line or attempting to use invalid characters within the comment markers is prohibited. - Compiler Behavior: Comments are treated as white space by the C++ preprocessor and are completely removed before the compilation process begins.
- Best Practices: While
//is widely used for brief descriptions,/* */is often preferred for temporarily commenting out large segments of code during debugging.
