4. Comments in C++

Comments are used to document our code. They explain why we wrote a specific code. This will help us understand the code in the future and also help others to understand our code.

Comment are not instructions or statement, they are ignored by the compiler at compile time.

There is 2 type of comments in C++ :

1 - Single line comment

This comment is usually used to explain a statement and is placed at the top of the statement or at it's right, after the semi-colon.

This comment is placed after 2 slash (//) .

For example :

int main(){

    // to print hello world to the screen
    std::cout << "hello world" << std::endl; 


    std::cout << "hello world" << std::endl;  // to print hello world to the screen 
    return 0;
}

2 - Multi-line comment

This comment is usually used to explain a statement or a bunch of code but in multiple line. It can be placed at the top of the instructions it explains or at the end, after the semi-colon.

This comment is placed inside /* and */

For example :

/*
   This file 
   contains code 
   for example of multi-line comment.
*/

int main(){
    std::cout << "hello world" << std::endl; /*Another multi line comment*/
    return 0;
}