C++ Comments


This tutorial will teach us about C++ comments, their purpose, and how to use them using examples.

C++ code can be made more readable and explainable by using comments. In testing alternate code, it can also be used to stop execution. You can use one or more lines for your comments.

A programmer can add C++ comments as suggestions to make their code simpler to read and comprehend. C++ compilers completely disregard them.

Code comments can be added in one of two ways:

  • // – Single Line Comments
  • /* */ – Multi-line Comments

Do you prefer single-or multi-line comments?
You can choose whichever you want to use. We typically use /for brief comments and /* */for longer ones.

C++ Multi-line Comments

Comments with more than one line begin with /* and end with */.

The compiler will ignore any text between /* and */:

/* declaring a variable
to store amount for GeektoCode
int amount = 8500;

C++ Single-line comments

Beginning with two forward slashes (//), single-line comments.

The compiler ignores any text that appears between / and the end of the line (will not be executed). Before each line of code in this example, a single-line comment is used:

// declaring a variable 
// GeektoCode
int x;

// initializing the variable 'x' with the value 55
x = 55;

Note: Comments shouldn’t be used as a stand-in for an English-language explanation of poorly written code. Always write clear, self-explanatory code when writing programs. Afterward, use the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *