C語言注釋

C語言中的注釋用於提供有關代碼行的資訊,它被廣泛用於記錄代碼(或對代碼功能實現的說明)。在C語言中有兩種類型的注釋,它們分別如下 -

  • 單行注釋
  • 多行注釋

1.單行注釋

單行注釋由雙斜杠//表示,下麵我們來看看一個單行注釋的例子。創建一個原始檔案:single_line_comments.c,代碼如下 -

#include <stdio.h>
#include <conio.h>
void main(){
    // 這是一個注釋行,下麵語句列印一個字串:"Hello C"
    printf("Hello C"); // printing information
    // 這是另一個注釋行,下麵語句求兩個變數的值

    int a = 10, b = 20;
    int c = 0;
    c = a + b;
    printf("The sum of a+b is :%d", c);
}

執行上面示例代碼,得到以下結果 -

Hello C
The sum of a+b is :30
請按任意鍵繼續. . .

2.多行注釋

多行注釋由斜杠星號/* ... */表示。它可以佔用許多行代碼,但不能嵌套。語法如下:

/*
code
to be commented
line 3
line n...
*/

下麵下麵來看看看C語言中的多行注釋的例子。

創建一個原始檔案:multi_line_comments.c,代碼如下 -

#include <stdio.h>
#include <conio.h>
void main() {

    /*printing
    information*/
    printf("Hello C\n");
    /*
     多行注釋示例:

     下麵代碼求兩個數的乘積,
     int a = 10, b =20;
     int c = a * b;
    */
    int a = 10, b = 20;
    int c = a * b;
    printf("The value of (a * b) is :%d \n", c);
}

執行上面示例代碼,得到以下結果 -

Hello C
The value of (a * b) is :200
請按任意鍵繼續. . .

上一篇: C語言運算符 下一篇: C語言轉義序列