Comments in C++
Comments in C++
Here, in this section, we will discuss comments in C++. Comments are used to insert meaningful descriptions or notes where ever required in the code so as to enhance readability and user understanding of the program.
- Comments are statements in the program that are ignored i.e not executed by the compiler
- Comments can be used anywhere, at any point, and any number of times in the program
Single Line Comments
They are used with 2 backslashes (//) everything after (//)is ignored by the compiler within that lineProgram to demonstrate single Line comments
Run
#include <iostream> using namespace std; int main() { int a = 2, b = 3; // a, b are variables of type int cout << a + b; //cout is present in std namespace return 0; }
Output
5
- Single line comments are always applicable in a line. if you don’t use a backslash in the next line they are treated as normal code and may result in an error
- So you need to use //es in each line separately
Example :
Run
#include<iostream> using namespace std; int main() { cout << "Hello"; // this will print Hello this is not commented // this is commented }
Output
Compile time error
MultiLine Comments
- The start with /* and end with */ .ie the block of code that falls between /* and */ is commented
- They are also called as block comments
Run
#include <iostream> using namespace std; /* Multiline comments in C++*/ int main() { cout << "Demo"; /* This is a multiline comment Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.*/ }
Output
Demo
Single line comments cannot be closed in between i.e once you use a single line comment again you cannot write normal code within that line, whereas multiline comments can be closed in between and again you can write code within the same line
Example
Run
#include <iostream> using namespace std; /* Multiline comments in C++*/ using namespace std; int main() { cout << "Hello "; /*multiline comment bw statements in same line*/ cout << "World"; //you add statements in the same line when using single line comment }
Output
Hello
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment