C++ 注释
C++ 注释
注释可以用来解释 C++ 代码,并使其更可读。也可以使用替代测试来阻止代码执行。
注释可以是单行的,也可以是多行的。
单行注释
单行注释以两个斜杠开头 (//).
编译器将忽略 // 和行尾之间的任何文本(不会执行)。
本例在代码行之前使用单行注释:
实例
#include <iostream>using namespace std;int main() {// This is a commentcout << "Hello World!";return 0;}
本例在代码行末尾使用一行注释:
实例
#include <iostream>using namespace std;int main() {cout << "Hello World!"; // This is a commentreturn 0;}
C++ 多行注释
多行注释以 /* 开头,以 */ 结尾。
编译器将忽略 /* 和 */ 之间的任何文本:
实例
#include <iostream>using namespace std;int main() {/* The code below will print the words Hello World!to the screen, and it is amazing */cout << "Hello World!";return 0;}
单行注释 or 多行注释?
你想用哪一种取决于你自己。通常情况下,我们用 // 表示简短的注释,用 /* */ 表示更长的注释。