Radha Gogia
Radha Gogia

Reputation: 795

why can't we nest one comment inside another comment?

/*
/**hiiii**/
*/

Say I nest the comments like this , so what is wrong in this ? At the starting we have /* and then some string inside it and then finally we have at the end */ , so what is the mistake here ?

Upvotes: 1

Views: 3403

Answers (6)

chqrlie
chqrlie

Reputation: 144715

C comments introduced by /* do not nest. This decision was made by the original designers more than 40 years ago and all C Standards make this mandatory. Although some older compilers supported nesting such comments with a command line switch, it is highly recommended to adhere to standard practices and consider that comments end at the first occurrence of the characters */.

Note that / and * can be separated by an escaped linefeed (a \ followed by a newline:

int i = 3;  /\
* This is a comment (SO syntax coloring is not perfect ;-) *\
/ printf("%d\n", i); // prints 3

Also note that it is a bad idea to comment code by turning it into a comment with /* */. If your code contains C comments, this will fail and may even fail silently:

int i = 3, j = 6;
/* printf("debug: i=%d\n", i);  /* check the value of i */
printf("debug: j=%d\n", j);  // check the value of j */

In the code above, the second printf is not commented.

You can imagine even more tricky situations:

/*
// list all C source files in subdirectories
system("ls */*.[ch]");
*/

There are two effective ways to comment a block of code:

  • Use #if 0 / #endif preprocessor directives. These can be nested and easily uncommented by changing the #if 0 into #if 1.

  • Insert // at the beginning of each line in the block. This can be nested, is the sense that you can comment a larger block by inserting // at the beginning of each line again.

Upvotes: 1

chux
chux

Reputation: 153447

Code can effectively have nested comment behavior with #if 0. So extending /* */ behavior is not needed.

#if 0

   blah blah 

#if 0
   blah blah 

#endif

   blah blah 

#endif

Upvotes: 4

Sourav Ghosh
Sourav Ghosh

Reputation: 134316

The C standard is quite explicit about it. /*...*/ style comments do not nest, because the language syntax does not allow nesting.

Quoting C11, chapter 6.4.9, Comments, (emphasis mine)

Except within a character constant, a string literal, or a comment, the characters /* introduce a comment. The contents of such a comment are examined only to identify multibyte characters and to find the characters */ that terminate it.83)

and, the related footnote,

83) Thus, /* ... */ comments do not nest.

To elaborate, as in your case,

/*                  <-- Comments starts here
/**hiiii**/         <-- Found the ending */, so comment ends.
*/                  <-- problem here....
  • In the first line, /* starts the comment.
  • In the second line starting /* does not start a new comment, as mentioned earlier.
  • In the second line ending, */ terminates the comment. So, in third line, */ produces the error.

Upvotes: 6

pmg
pmg

Reputation: 108978

why can't we nest one comment inside another comment?

Because language designers took pity on language implementors.

It is very easy to parse unnestable comments; it is somewhat less easy to parse nestable comments.

// pseudo code for unnestable comments
is the character a '*'? YES => Is the next one a '/'? YES END COMMENTS
else STILL INSIDE A COMMENT

Upvotes: 4

haccks
haccks

Reputation: 106012

After seeing /* compiler looks for */. Once it find the comment closer */, it's all done. Everything from /* to first */ regarded as comment. So,

/*
/**hiiii**/ 

makes a comment.

The second */ is without preceding /* and therefore causes an error.

Upvotes: 1

Jacobr365
Jacobr365

Reputation: 846

The */ at the end of the /**hiiii**/ line is ending the comment.

Do this:

/*
//*Hiiii*
*/

Upvotes: 1

Related Questions