Fundamentals 4 min read

Mastering C Comment Syntax: Hidden Pitfalls and Correct Usage

This article explains how C's /* */ and // comment delimiters work, shows edge cases where they appear inside strings, macros, or line continuations, and provides concrete code examples illustrating correct and incorrect usages along with the compiler errors they may trigger.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering C Comment Syntax: Hidden Pitfalls and Correct Usage

In C and C++ source files, the two‑character sequences /* and // start block and line comments respectively, unless they occur inside character constants, string literals, or already‑opened comments. The compiler scans characters after the opening marker until it finds the matching closing marker or a newline.

Example 1

Both /* and // can be part of a string literal without starting a comment:

char *sl1 = "s/*l1";
char *sl2 = "s//l2";

Example 2

Placing // inside an #include directive creates undefined behavior because the preprocessor treats it as the start of a comment: #include "//some.h" The compiler typically reports an error and aborts compilation:

fatal error: //some.h: No such file or directory
compilation terminated.

Example 3

A line that begins with // is a comment, even if the comment text contains the sequence */:

//*/

Example 4

Block comment delimiters can be embedded inside an expression without affecting the surrounding code: double radius = D/*diameter*/ / 2; which is equivalent to:

double radius = D / 2;

Example 5

If a backslash (\) at the end of a line joins two logical lines and the backslash follows a comment marker, the result is a two‑line comment. For example:

//\
int i;

or

/\
int i;

Both are interpreted as a single commented line:

//int i;

Example 6

When // appears inside a block comment ( /* … */), it is treated as ordinary comment content and ignored: /*//*/ int i; which is effectively:

int i;

Example 7

Conversely, if // starts a line comment, any /* or */ that follows is ignored as part of the comment:

int m = n //**/o
+ p;

Resulting in:

int m = n
+ p;

Example 8

Using the token‑pasting operator ## with two forward slashes does not produce a valid preprocessing token and causes a syntax error:

#define glue(x,y) x##y
glue(/,/) int k;

The compiler reports:

error: pasting "/" and "/" does not give a valid preprocessing token
 glue(/,/) int k;
      ^
note: in definition of macro ‘glue’
 #define glue(x,y) x##y
               ^
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

programmingCcommentssyntaxcode
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.