Comments in C++
Comments
are explanatory statements that are included in programs. All programming
languages allow for some form of comments. Comments in C++ starts with “//” these two forward slashes indicate
that these are comments and C++ compiler will ignore these lines. Comments are
used to help others understand that part of the code or they can be used to
explain some point or function of that piece of code.
// This is a comment.
int a = 5; //initializing a’s value with 5
A comment
can also start with /* and ends with the */ this is also used for multiline
comments. For example
#include
using namespace std;
int main() {
/*this program
just
Prints Hello World
*/
cout << "Hello World";
return 0;
}
While the
code above is compiled by the compiler, it will ignore the anything written
between /* */. And will print following line.
Hello World
Comments
can also be nested as compiler is just going to ignore them. But remember if
you are using /* you should also close the comment with */ otherwise all the
code below will be considered as comment by the compiler. For example:
/* here is an example of // nested
Comments */
Comments
Post a Comment