Skip to content Skip to sidebar Skip to footer

Comments in C++

Comments are used to add information or explain what the piece of code does without changing the code’s functionality. They’re also helpful for programmers who don’t speak English well. Comments ignore the additional information as we use comments in our source code that do not get printed, executed, or compiled by the compiler. Thus, they do not return values and get displayed on the output screen but are still written inside the code to help understand it better when read by someone.

In C++, we have C-style comments.

Before we understand the syntax, we must know, There are two types of commenting styles.

  1. Single line comments
  2. Multi-line comments

Single-Line Comment

Single-Line Comments written in a single line are inline comments that one can add in a single line. Programmers can use single-line comments to document a snippet of code or explain an algorithm. Two backslashes ( // ) are the Single-Line Comment indicator, or it’s simply the syntax.

Syntax

// This is A Single Line Comment

Multi-line comments

Multi-Line Comments are comments occupies more lines of code. We use multi-line comments when we want to add information about the lines of code or explain something to someone else who reads our code. Multi-line comments are also helpful for documenting APIs (Application Programming Interfaces) and explaining how they work. A backslash with an asterisk afterward ( /* ) indicates the beginning, and an asterisk and a backslash afterward ( */ ) indicates the end of the Multi-Line Comments. 

Syntax

/*
 * 
 * This is a Multi-Line Comment in the block of code
 * 
 */

Now, after knowing all about comments, we must also know their importance.

Comments – Why should we use them?

Well, all programming gods have said that if you want to increase the readability of your code or have the intention of updating it in the future, the block of comments is the only way to help you in need of the hour as they’re more preferred than individual lines.

Comments are primarily used for documentation, as they are not part of the language, and the compiler removes them during pre-processing, which happens before compilation. It would be best if you always use them, as it’s recommended to use comments to document functions, classes, variables, constants, etc. I should also use comments to explain what a function does, what a class means, etc.

Shortcuts for Comments

There’s a cute little shortcut for normal comments on the piece of code using Single-Line Comment.

CTRL + /

or if you are a user of MAC, then the keyboard shortcut for you guys is:

COMMAND + /

Now, if you want to uncomment any code statement, you can use the same shortcut, and voila, all your work gets easy peasy.

Note: These comment shortcuts are subject to the IDE you use as it depends on the IDE, but most of the time, they’re available on almost all IDE.

Conclusion

Don’t forget to subscribe to us on FacebookInstagram & or Twitter. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂

Leave a comment

Sign Up to Our Newsletter

Be the first to know the latest updates