#if 0

#if is a macro which checks for the condition written aside to it, since ‘0’ represents a false, it means that the block of code written in between ‘#if 0’ and ‘#endif’ will not be compiled and hence can be treated as comments.

So, we can basically say that #if 0 is used to write comments in a program.

Example :
#if 0
int a;
int b;
int c = a + b;
#endif

The section written between “#if 0” and “#endif” are considered as comments.

Questions arises : “/* … */” can be used to write comments in a program then why ”#if 0”?

Answer : It is because, #if 0 can be used for nested comments but nested comments are not supported by “/* … */”

What are nested comments? Nested Comments mean comments under comments and can be used in various cases like :

Let us take an example that you have written a code like below:

Now, someone is reviewing your code and wants to comment this whole piece of code in your program because, he doesn’t feel the need for this piece of code. A common approach to do that will be :

The above is an example of nested comments.The problem with the above code is, as soon as the first “*/” after “/*” is encountered, the comment ends there.
i.e., in the above example, the statement : int d = a-b; is not commented.

This is solved by using “if 0” :

Here, we have used nested comments using #if 0.

Leave a Reply

avatar
  Subscribe  
Notify of