Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Comments in C++

Tags:

c++

comments

This should be a common problem and possibly similar to some question here but i am looking foe the best way to comment out multiple lines (rather methods ) in C++ which have comments within them .I did check out some posts on SO but couldnt get the full details on using something like if #0 .

I did check out this post Nested comments in Visual C++? but I am not on the Windows platform.

like image 326
Manish Avatar asked Jul 23 '11 00:07

Manish


People also ask

What is nested comments in C?

A nested comment is a comment inside another comment. In Java you can't nest a multiline comment into another multiline comment, however you can nest a single line comment into a multiline comment (but does this make sense?) /* This is a comment // this is a nested comment This is another comment */

Does C allow nested comments?

In C, multi-line comments, /* and */, do not nest. Some guides go so far as to recommend that code sections must never be commented and that if code is to be temporarily disabled one could resort to using an #if 0 directive. See #if 0 to block out code sections.

What does nested comments mean?

When someone comments on a previous comment, a “nest” is created. The first comment shows normally, but then the replies to the comment are categorized directly underneath the original comment.

Why are nested comments?

Nested comments are usually a sign that you are using comments wrong. The most common example is commented-out code that contains comments itself, and the fix is to remove the code instead of commenting it out.


1 Answers

You are almost correct; essentially it is being suggested to "if-def" the section of code out. What you want to do is use the precompiler directive #if to block the code for you. Ex below shows that I want to ignore everything between the if and endif.

#if 0
/* Giant comment
 it doesn't matter what I put here */

// it will be ignored forever.
#endif

To answer your question in general though; there is not a way to have compound comments, i.e.

/* 
  /* */ <--- this closes the first /* 
*/ <--- this dangles.
like image 167
Suroot Avatar answered Oct 15 '22 06:10

Suroot