简体   繁体   中英

C Programming - Multi-line comments

How to ignore the multi-line comment symbols inside another multi-line comment?

Say I want to put the entire code in comments so that I can test other things in my code

/* This is my first comment */
printf("\n This is my first print command");

/* This is my second comment */
printf("\n This is my second print command");

If I do

/* 

/* This is my first comment */
printf("\n This is my first print command");

/* This is my second comment */
printf("\n This is my second print command");

*/

this is creating error.

What you expect here is the multi-line comments to be nested .

Quoting directly from the standard C11 , chapter §6.4.9,

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 footnote,

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

As a workaround, you can use the conditional compilation block as

#if 0
.
.
.
.
#endif 

to have a whole block commented out .

What you can do is

#if 0
/Code that needs to be commented/
#endif

I suppose you want to comment out some code containing itself comments.

You can use conditionnal compilation for this:

#if 0
/* This is my first comment */
printf("\n This is my first print command");

/* This is my second comment */
printf("\n This is my second print command");
#endif

Everything between #if 0 and #endif will be ignored by the compiler, just if it was a comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM