Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A line comment in C is a lexical construct that instructs the compiler to ignore all text starting from the comment delimiter up to the end of the physical or logical line. During Translation Phase 3 of the compilation process, the source file is decomposed into preprocessing tokens and sequences of white-space characters. At this stage, the entire comment is identified as white-space and replaced by a single space character before syntactic analysis begins.

Syntax

A line comment is introduced by the character sequence of two consecutive forward slashes (//) and is terminated by the next newline character (\n).
// [text to be ignored by the compiler]
int x = 5; // The comment can also follow a valid statement

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.
char* path = "http://example.com"; /* The // inside the string is not a comment */
/* Block comment containing // which is ignored */
// Line comment containing /* which is ignored

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.
// This is a single logical line comment that spans \
across multiple physical lines in the source code \
because of the backslash line-continuation character.
int y = 10;

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 // b is parsed as the identifier a, followed by two consecutive division operators (/ and /), and the identifier b. This results in a hard syntax error.
  • An expression like a //*b*/ c is parsed as the identifier a, followed by a single division operator (/), a block comment (/*b*/), and the identifier c. This evaluates as valid division (a / c). In contrast, under C99 rules, the initial // initiates a line comment, causing the compiler to ignore *b*/ c entirely and leaving the expression incomplete.
The compiler will only accept // 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.
Master C with Deep Grasping Methodology!Learn More