コメント は、プリプロセスの間に、単一スペース文字に置き換えられるテキストです。 したがって、コンパイラーはすべてのコメントを無視することになります。
以下のとおり、2 種類のコメントがあります。
コメントは、言語が空白文字を許可する場所であればどこにでも記述できます。 C スタイルのコメントは、他の C スタイルのコメント内ではネストできません。*/ が最初に現れた位置で、各コメントは終了します。
マルチバイト文字は、コメントにも含めることができます。
次のプログラムでは、2 番目の printf() がコメントです。
#include <stdio.h> int main(void) { printf("This program has a comment.¥n"); /* printf("This is a comment line and will not print.¥n"); */ return 0; }
2 番目の printf() は、スペースと同等であるため、このプログラムの出力は次のようになります。
This program has a comment.
次のプログラムの printf() は、コメント区切り文字 (/*、*/) が、ストリング・リテラルの 内側にあるので、コメントではありません。
#include <stdio.h> int main(void) { printf("This program does not have ¥ /* NOT A COMMENT */ a comment.¥n"); return 0; }
このプログラムの出力は、次のようになります。
This program does not have /* NOT A COMMENT */ a comment.
次の例では、コメントは強調表示されています。
/* A program with nested comments. */ #include <stdio.h> int main(void) { test_function(); return 0; } int test_function(void) { int number; char letter; /* number = 55; letter = 'A'; /* number = 44; */ */ return 999; }
test_function では、コンパイラーは、最初の /* から最初の */ までを、 コメントとして読み取ります。 2 番目の */ は、エラーです。 ソース・コードですでにコメントにされている個所をコメントにしないようにするには、 条件付きコンパイルのプリプロセッサー・ディレクティブを使用して、 コンパイラーにプログラムのセクションを迂回させる必要があります。 例えば、上記のステートメントをコメントにする代わりに、ソース・コードを次のように変更します。
/* A program with conditional compilation to avoid nested comments. */ #define TEST_FUNCTION 0 #include <stdio.h> int main(void) { test_function(); return 0; } int test_function(void) { int number; char letter; #if TEST_FUNCTION number = 55; letter = 'A'; /*number = 44;*/ #endif /*TEST_FUNCTION */ }
単一行コメントは、C スタイルのコメント内でネストできます。 例えば、次のプログラムは何も出力しません。
#include <stdio.h> int main(void) { /* printf("This line will not print.¥n"); // This is a single line comment // This is another single line comment printf("This line will also not print.¥n"); */ return 0; }
関連参照