C++ Comments



Comments can be used to explain C++ code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Comments can be singled-lined or multi-lined.

Single-line comments start with two forward slashes (//).

/* 
This is a C-like comment. The program determines whether an integer is odd or even.
*/
 #include < iostream >
 using namespace std;
 int main( ) {
 int num;              // This is a C++ single-line comment.
 // read the number  
 cout << "Enter number to be tested: ";
 cin >> num;
 // see if even or odd  
if ((num%2)==0) cout << "Number is even\n"; else cout << "Number is odd\n";
 return 0; 
 }

C++ Multi-line Comments

Multiline comments cannot be nested but a single-line comment can be nested within a multiline comment.
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by the compiler:

Example:
/* The code below will print the words Hello World!
to the screen, and it is amazing */

cout << "Hello World!";


Post a Comment

0 Comments