C++ Comments

Comments are important part of C++ language. You should learn to write comments from the beginning, because it's a good habit for a programmer.

Let's take a look on the Hello World program. Have you noticed that a part of lines of below code is just the description of the program:

Try It

//include a header file from Standard Library
#include <iostream>
using namespace std;
//the work of the program starts from function called  main
int main()
	//use standard (console) to output message "Hello World"
	cout << "Hello  world" << endl;
	//wait for user to press a key
	//return a value to the system when program finish its execution successfully
	return 0;

Single line Comment

Any words in a line after "//" are ignored by the compiler. So if you want to write a comment about 'what is this code doing' you have to write it in the following way:

//any text that describes your code

The "//" symbols is used to write one line comment.

Multiline Comments

Sometimes long comments are required in the code. For this purpose the following characters are used:

/*start line
	This is a multiline comment
	Anything  between start (/ *) and end (* /)
	will be ignored by the compiler
end line */

If you want to write a multiline comment you have to write "/*" at the start of the comment. After this you can write as many number of lines of description. When you are done with writing multiline comment then you have to close multiline comment. It is done by the "*/" symbol.

Why comments are important?

Do not think that comments are not important for your program. When you write small programs then you will be able to remember all the things you have written and the purpose of that. But when your programs grow in size then comments will be really helpful to understand what your lines of code doing.

Another benefit of using comments is that it makes your program much more clear for people who will read your code. At enterprise level the software becomes too big and multiple teams work on it. If comments are not present then it becomes really difficult to understand the coding logic.

C++ Online Test - Check Your Rank across the World

C++ Interview Questions

Next > < Prev
Scroll to Top