Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 955
Amninder Singh Avatar asked Dec 19 '22 11:12

Amninder Singh


1 Answers

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.

like image 178
Sourav Ghosh Avatar answered Jan 04 '23 00:01

Sourav Ghosh