简体   繁体   English

C程序中可以添加多少注释?

[英]How many ways can comments be added in a C program?

When I was solving a problem from the famous KR book to remove comments from a c program I have searched over the internet to know all the possible cases where comments can be inserted in a C program and didn't find any complete answer of this question.当我从著名的 KR 书中解决一个问题以从 c 程序中删除评论时,我在互联网上搜索以了解可以在 C 程序中插入评论的所有可能情况,但没有找到该问题的任何完整答案. So here I am adding the answer to this question.所以我在这里添加这个问题的答案。 feedback is welcomed.欢迎反馈。

How many ways are there to insert comments in a C program?在 C 程序中插入注释有几种方法?

  1. When a single line comment symbol is inserted anywhere in a line everything that appears before a newline is part of the comment.当单行注释符号插入一行中的任何位置时,出现在换行符之前的所有内容都是注释的一部分。

  2. Any type of comment symbol can appear inside a double-quoted string — so single-line and multi-line comment symbols appearing inside double quotes are not a comment;任何类型的注释符号都可以出现在双引号字符串中——因此出现在双引号中的单行和多行注释符号不是注释; they are normal text.它们是普通文本。

  3. Multi-line comments can be placed anywhere in the source code where you can put a blank.多行注释可以放在源代码中任何可以留空的地方。

  4. In C, comments cannot be nested.在 C 中,注释不能嵌套。 Inserting a multi-line comment start symbol inside another multiple line comment will truncate the parts of comments that come after the first comment terminating symbol.在另一个多行注释中插入多行注释开始符号将截断第一个注释终止符号之后的注释部分。 Check the example of #4 to understand clearly.检查#4的示例以清楚地理解。

#include <stdio.h>
#define TABSIZE /*false comment*/ 8

// this is a demo program. /* comment here */           XD example of #1

int main()
{
    printf("#1  :- /* multiple */ and // single \n"); // example of #2

    char str[40] = "/* multiple */ and // single"; // example of #2
    printf("#2  :- %s\n", str);

    char one /* example of #3 */ = 'x'; // example of #3
    char two /* example of #3 */ = 'y'; // example of #3
    printf("#3  :- %c and %c\n", one, two);

    return /*example of #3 */ 0; // example of #3

    /*(start1) this is a comment /*(start2) trying to-      -- example of #4
     insert an nested comment here */
    //  (e2) this part is not considered as comment */(e1).
    //  so to successfully run the program this part of the
    //  comment  is handled by single line comment symbol '//'.
    //  remove all the '//' to see what happens.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM