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.

Upvotes: 5

Views: 4075

Answers (3)

Sourav Ghosh
Sourav Ghosh

Reputation: 134316

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.

Upvotes: 9

Jabberwocky
Jabberwocky

Reputation: 50775

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.

Upvotes: 1

Gopi
Gopi

Reputation: 19864

What you can do is

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

Upvotes: 2

Related Questions