Back to Course

Introduction to C++

0% Complete
0/0 Steps
  1. C++: Basics
    5Topics
    |
    6 Quizzes
  2. C++: Compilation and Execution
    5Topics
    |
    6 Quizzes
  3. C++: Variables
    9Topics
    |
    10 Quizzes
  4. C++: Conditions Operators
    5Topics
    |
    6 Quizzes
  5. C++: Logical Operators
    3Topics
    |
    4 Quizzes
  6. C++: Loops and Errors
    8Topics
    |
    9 Quizzes
  7. C++: Vectors
    4Topics
    |
    3 Quizzes
Lesson 2, Topic 4
In Progress

C++: Comments

Yasin Cakal 30 Oct 2021
Lesson Progress
0% Complete

Sometimes, extensive code may become difficult for the reader or the developer to understand if you return to it. This might also happen during the process of writing the code. Comments are added for the reader, which are solely added for the humans. The compiler completely ignores these comments.

We can add single-line comments or multiple-line comments. These comments may include instructions, annotations, or explanations of the code.

Adding Single-Line Comments:

To add single-line comments, you add // before the comment. This line can be added before or after the code/operation. This is an example for before the operator:

//prints Hello, World! 
std::cout <<"Hello, World!";

Output:

Hello, World!

Following is an example of after the operation:

std::cout <<"Hello, World!";
//prints Hello, World! 
Hello, World!

Multiple Line Comment:

To skip a multiple-line comment from being read by the compiler, /* is added at the start of the comments and */ at the end. An example of this is:

#include <iostream>

int main()
{
    /*These comments are removed in the program. 
std::cout<"This will not be printed on the terminal.";
Let's try this without the commented part of the code."*/
std::cout<<"This is printed on the terminal.";
}

Output:

This is printed on the terminal.

Responses

Leave a Reply

%d bloggers like this: