Syntax
A line comment is introduced by the character sequence of two consecutive forward slashes (//) and is terminated by the next newline character (\n).
Technical Mechanics
- Lexical Analysis: The
//sequence is a character sequence that introduces a comment. Neither the//delimiter nor the comment itself constitutes a preprocessing token; they are categorized strictly as white-space. - Exclusions: The C standard dictates that the
//sequence carries no special meaning and does not initiate a comment if it appears inside a character constant, a string literal, or an existing block comment (/* ... */). - Nesting: Line comments do not nest. Once the
//sequence successfully initiates a comment, all subsequent characters—including other comment delimiters like/*or//—are treated as part of the comment until the newline character.
Line Splicing (Continuation)
Because the C preprocessor resolves line splicing (Translation Phase 2) before it processes comments (Translation Phase 3), a line comment can be extended to the next physical line using a backslash (\) immediately preceding the newline character.
Standardization
Line comments were not part of the original ANSI C standard (C89/C90), which only supported block comments (/* ... */). They were officially adopted into the language specification with the C99 standard (ISO/IEC 9899:1999), borrowing the syntax from C++.
In strict C89 mode (e.g., compiling with gcc -std=c89 or gcc -ansi), the // sequence is not recognized as a comment delimiter. This leads to distinct lexical parsing behaviors compared to C99:
- An expression like
a // bis parsed as the identifiera, followed by two consecutive division operators (/and/), and the identifierb. This results in a hard syntax error. - An expression like
a //*b*/ cis parsed as the identifiera, followed by a single division operator (/), a block comment (/*b*/), and the identifierc. This evaluates as valid division (a / c). In contrast, under C99 rules, the initial//initiates a line comment, causing the compiler to ignore*b*/ centirely and leaving the expression incomplete.
// as a comment in pre-C99 environments if compiler extensions are active (e.g., using GNU C89 mode via gcc -std=gnu89). In that specific extended mode, adding the -pedantic flag will successfully compile the code but emit a warning regarding the use of C++ style comments.
Tired of Poor C Skills? Fix That With Deep Grasping!Learn More





