当前位置:网站首页>Nesting of C language annotations

Nesting of C language annotations

2022-06-12 14:18:00 zou_ albert

Mainstream C/C++/Java/C# Other languages , The annotation syntax is designed not to be nested .
Such as : /* xxx /* yyy */ zzz */ It's illegal. .
reason :
Notes are used to write descriptive language , Nesting makes readability worse .

But in fact , Annotations are not just used to write annotations , It is also commonly used to mask code blocks . When you need to comment out a large program , Still use this annotation ( /* … * /) How about ?
The answer, of course, is : Not good. !!!
If the program to be annotated has already been annotated with this annotator , Then another annotation like this may cause problems . Some compilers are detecting "/*" Will look for the nearest " */" Combined with it .

So how to annotate large programs ?
C In language

  #if 0
  #endif

Such a statement can accomplish this task .

This is a precompiled statement , The compiler automatically recognizes the precompiled code , Judge if Is it true , If set up , Just put ****1 Code retention ,****2 Comment out the code , This is done by the compiler . This will not affect the compilation and migration of the program , Such as :

#if 0****1
/*..........*/#else****2
/*..........*/#endif

Source of problem :
《C Traps and defects 》

practice 1-1.
some C The compiler allows nested comments . Please write a test program , requirement : Whether for compilers that allow nested annotations , Sea lions do not allow nested annotation compilers , The program can be compiled normally ( No error message appears ), But the results of program execution in these two cases are different .

Tips : In a string enclosed in double quotation marks , annotator /* Part of a string , The double quotation marks in the comments are part of the comments

Thoughts

  1. To form nested annotations , This string sequence must contain :/* /**/*/.
  2. First, let's discuss :/*/**/ . For compilers that do not allow nested annotations , This is a complete comment ; For compilers that allow nested annotations , The following characters are part of the comment .
  3. Let's add :"* /", Turn it into :/* /**/"*/".
  4. For compilers that do not allow nested annotations : The above string is equivalent to the string :"*/". Compilers that allow nested comments cannot be compiled at this time ( There is a double quotation mark left )
  5. So we followed it with a comment and a double quotation mark :/* /**/"* /"/*" .
  6. For compilers that allow nested annotations : The above string is equivalent to "/* " ; For compilers that do not allow nested annotations : The above string is equivalent to " * /" / *"( Comment terminator caused by parentheses , Followed by an open comment
  7. Finally, simply let the last comment end :/* /** /"* /"/*"/**/
  8. So if nested comments are allowed , The above string is equivalent to "/*" , If nested annotations are not allowed, they are equivalent to " */"

Reference link :https://blog.csdn.net/weixin_33859693/article/details/117036478

原网站

版权声明
本文为[zou_ albert]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010513031494.html