Danger of Using Slash Star Comment

◀ Importance of Documentation▶ Danger of Using Double Slash Comment
Amazon As I become more experienced in programming, I realize that there is a subtle danger in using /* */ to comment their programs: nested pairs cause problems. Try putting /*, /*, */, */, in your code in this order and see if the program can still be compiled.

The fact is that the first */ blocks everything and leaves whatever after it open for compiling. For example, the following is a program fragment with nested pairs of /* and */:
/*
This program simulates a calculator.
  /*
The calculator’s functions include addition, subtraction, multiplication, division, and modulus. */ by Michael Wen */ int main() { … return 0; }
The seventh line, “by Michael Wen”, is not commented out and is visible to the compiler. Therefore compiling this program will not be successful.

Programmers usually do not use nested comments anyway, but what if you want to compile a specific portion of the code and leave the other portion as comments within which there are several pairs of /* and */?


Here is a wonderful solution: you can use define preprocessor directives. Here is example A:
#ifdefine whatever
Following is code I do NOT want compiled
.
.
.
/*
comments…
*/
Following is code I do NOT want compiled
.
.
.
/*
more comments…
*/.
Following is code I do NOT want compiled
.
.
.
#endif
Here's example B:
#ifndefine whatever Following is code I do want compiled . . . /* comments… */ Following is code I do want compiled . . . /* more comments… */. Following is code I do want compiled . . . #endif
If whatever is defined earlier, compiler compiles everything after #ifdefine whatever and before the corresponding #endif. If whatever is defined, compiler does not compile anything after #ifndefine whatever and before the corresponding #endif.


Assuming whatever is not defined earlier. In example A, the entire code after #ifdefine whatever and before #endif is not compiled. In example B, the entire code after #ifndefine whatever and before #endif is compiled.

Simply inserting and removing the letter ‘n’ in the define directive makes life a lot easier if you want or do not want something compiled.

I am merely pointing out the danger of using slash star comments. You should still use it to comment things out.

Next let’s look at another type of comment!
◀ Importance of Documentation▶ Danger of Using Double Slash Comment

fShare
Questions? Let me know!