簡體   English   中英

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

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

當我從著名的 KR 書中解決一個問題以從 c 程序中刪除評論時,我在互聯網上搜索以了解可以在 C 程序中插入評論的所有可能情況,但沒有找到該問題的任何完整答案. 所以我在這里添加這個問題的答案。 歡迎反饋。

在 C 程序中插入注釋有幾種方法?

  1. 當單行注釋符號插入一行中的任何位置時,出現在換行符之前的所有內容都是注釋的一部分。

  2. 任何類型的注釋符號都可以出現在雙引號字符串中——因此出現在雙引號中的單行和多行注釋符號不是注釋; 它們是普通文本。

  3. 多行注釋可以放在源代碼中任何可以留空的地方。

  4. 在 C 中,注釋不能嵌套。 在另一個多行注釋中插入多行注釋開始符號將截斷第一個注釋終止符號之后的注釋部分。 檢查#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