CPP Programming

All Languages C CPP JAVA HTML CSS JAVASCRIPT PYTHON

Comments In CPP

Comments are portions of the code ignored by the compiler which allow the user to make simple notes in the relevant areas of the source code. Comments come either in block form or as sibngle lines.

Single-line comments :

start with // and continue until the end of the line. If the last character in a comment line is a \ the comment will continue in the next line.

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:

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:

The "//" symbols is used to write one line comment.
//any text that describes your code

//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
	cin.ignore();
	//return a value to the system when program finish its execution successfully
	return 0;
}


Multi-line comments :

start with /* and end with */.

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.

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.