how to use Comments in C++
Hello friends, hope you all are fine and enjoying your lives. Today, I am going to show you How to use comments in C++. In the previous tutorials, we first have a look at Introduction to C++ in which we have discussed a simple c++ program. After that we have discussed How to use Escape Sequence in C++, which is also essential because escape sequence is a very simple concept but is used quite a lot on C++ projects os you must have its knowledge.

Today, we are gonna discuss Comments in C++ i.e. how to use Comments in C++ and why to use Comments in C++. Just like escape sequence, comments in C++ is also very basic concept but its really important and a programmer must know How to use Comments because without it the code becomes really difficult and is quite difficult to debug. So, let's get started with How to use Comments in C++.

How to use comments in C++ ??

  • Let me first explain what are comments in c++.
  • Suppose you are working on some project in C++ and you complete your project and simply forget about it. Then after 2 years, somehow you got the same project and you have to use the same code then you open your code and your face must look like this. :O
  • Because the code is too lengthy and you are not understanding your own code. :)
  • Here comes the benefit of comments.
  • Comments are not part of your code, Instead they are just used to add additional writing about the code from which you can remember what this code is actually doing.
  • Let me add some comments in the Hello World program which we have designed in the Introduction to C++ tutorial.
#include <iostream> // Including Library
 
using namespace std; // Including Namespace Standard
 
int main(void) // Starting the Main Loop
{
     cout<<"Hello World!!!"<<endl; //Printing Hello World in output
     return 0; // Returning 0 value
}
  • Now in the above code you can see I have used additional lines in front of each command.
  • These additional lines are called comments and as the name suggests these are the comments about the code.
  • So, now by simply looking at the code you can understand what each line is doing.
  • Now you must have noticed that before starting each comment, I have used // two forward slashes.
  • So, in C++ whenever you add two forward slashes, the compiler simply ignores everything written in front of these slashes, and these are called comments.
  • Its one way to add a comment in C++.
  • Now, sometimes there's a case when you need to add like four to five comment lines in your code then in order to add // this sign before each line you can simply enclose all the comments in between /* Here's your comment */.
  • Let's have a look at this comment system below:
/*
Hello World Code
Designed by: www.TheEngineeringProjects.com
This code will print Hello Wold on output.
Date: 5/19/2016
*/

#include <iostream> // Including Library
 
using namespace std; // Including Namespace Standard
 
int main(void) // Starting the Main Loop
{
     cout<<"Hello World!!!"<<endl; //Printing Hello World in output
     return 0; // Returning 0 value
}
  • So, in this way you can add the comments in /* */ these signs.
  • You can see in above code that in the start I have given a small explanation of the project and have mentioned who designed it and when designed it.
  • So, you can add such description in the comments in C++.
  • Another benefit of code comes in debugging of your project.
  • You think that soe of your code is not working properly then you can simply comment that code and test the remaining.

So, these are different uses and benefits of comments in C++. I hope you guys get some knowedge out of it. So, that's all for today, will meet you guys in the coming tutorial.