C++ comments line

In this tutorial, we will learn about C++ comments, why we use them, and how to use them with the help of examples.

C++ comments are hints that a programmer can add to make their code easier to read and understand. They are completely ignored by C++ compilers.

There are two ways to add comments to code:

// – Single Line Comments

/* */ -Multi-line Comments


Single Line Comments

In C++, any line that starts with // is a comment. For example,

// declaring a variable
int a;

// initializing the variable 'a' with the value 2
a = 2;

Here, we have used two single-line comments:

  • // declaring a variable
  • // initializing the variable 'a' with the value 2

We can also use single line comment like this:

int a;    // declaring a variable

Multi-line comments

In C++, any line between /* and */ is also a comment. For example,

/* declaring a variable
to store salary to employees
*/
int salary = 2000;

This syntax can be used to write both single-line and multi-line comments.


Using Comments for Debugging

Comments can also be used to disable code to prevent it from being executed. For example,

#include <iostream>
using namespace std;
int main() {
   cout << "some code";
   cout << ''error code;
   cout << "some other code";

   return 0;
}

If we get an error while running the program, instead of removing the error-prone code, we can use comments to disable it from being executed; this can be a valuable debugging tool.

#include <iostream>
using namespace std;
int main() {
   cout << "some code";
   // cout << ''error code;
   cout << "some other code";

   return 0;
}

Pro Tip: Remember the shortcut for using comments; it can be really helpful. For most code editors, it’s Ctrl + / for Windows and Cmd + / for Mac.


Why use Comments?

If we write comments on our code, it will be easier for us to understand the code in the future. Also, it will be easier for your fellow developers to understand the code.

Note: Comments shouldn’t be the substitute for a way to explain poorly written code in English. We should always write well-structured and self-explanatory code. And, then use comments.

As a general rule of thumb, use comments to explain Why you did something rather than How you did something, and you are good.

C++ Type Conversion

In this tutorial, we will learn about the basics of C++ type conversion with the help of examples.

C++ allows us to convert data of one type to that of another. This is known as type conversion.

There are two types of type conversion in C++.

  1. Implicit Conversion
  2. Explicit Conversion (also known as Type Casting)

Implicit Type Conversion

The type conversion that is done automatically done by the compiler is known as implicit type conversion. This type of conversion is also known as automatic conversion.

Let us look at two examples of implicit type conversion.


Example 1: Conversion From int to double

// Working of implicit type-conversion

#include <iostream>
using namespace std;

int main() {
   // assigning an int value to num_int
   int num_int = 9;

   // declaring a double type variable
   double num_double;
 
   // implicit conversion
   // assigning int value to a double variable
   num_double = num_int;

   cout << "num_int = " << num_int << endl;
   cout << "num_double = " << num_double << endl;

   return 0;
}

Run Code

Output

num_int = 9
num_double = 9

In the program, we have assigned an int data to a double variable.

num_double = num_int;

Here, the int value is automatically converted to double by the compiler before it is assigned to the num_double variable. This is an example of implicit type conversion.


Example 2: Automatic Conversion from double to int

//Working of Implicit type-conversion

#include <iostream>
using namespace std;

int main() {

   int num_int;
   double num_double = 9.99;

   // implicit conversion
   // assigning a double value to an int variable
   num_int = num_double;

   cout << "num_int = " << num_int << endl;
   cout << "num_double = " << num_double << endl;

   return 0;
}

Run Code

Output

num_int = 9
num_double = 9.99

In the program, we have assigned a double data to an int variable.

num_double = num_int;

Here, the double value is automatically converted to int by the compiler before it is assigned to the num_int variable. This is also an example of implicit type conversion.

Note: Since int cannot have a decimal part, the digits after the decimal point is truncated in the above example.


Data Loss During Conversion (Narrowing Conversion)

As we have seen from the above example, conversion from one data type to another is prone to data loss. This happens when data of a larger type is converted to data of a smaller type.

Data loss in C++ if a larger type of data is converted to a smaller type.
Possible Data Loss During Type Conversion

C++ Explicit Conversion

When the user manually changes data from one type to another, this is known as explicit conversion. This type of conversion is also known as type casting.

There are three major ways in which we can use explicit conversion in C++. They are:

  1. C-style type casting (also known as cast notation)
  2. Function notation (also known as old C++ style type casting)
  3. Type conversion operators

C-style Type Casting

As the name suggests, this type of casting is favored by the C programming language. It is also known as cast notation.

The syntax for this style is:

(data_type)expression;

For example,

// initializing int variable
int num_int = 26;

// declaring double variable
double num_double;

// converting from int to double
num_double = (double)num_int;

Function-style Casting

We can also use the function like notation to cast data from one type to another.

The syntax for this style is:

data_type(expression);

For example,

// initializing int variable
int num_int = 26;

// declaring double variable
double num_double;

// converting from int to double
num_double = double(num_int);

Example 3: Type Casting

#include <iostream>

using namespace std;

int main() {
    // initializing a double variable
    double num_double = 3.56;
    cout << "num_double = " << num_double << endl;

    // C-style conversion from double to int
    int num_int1 = (int)num_double;
    cout << "num_int1   = " << num_int1 << endl;

    // function-style conversion from double to int
    int num_int2 = int(num_double);
    cout << "num_int2   = " << num_int2 << endl;

    return 0;
}

Run Code

Output

num_double = 3.56
num_int1   = 3
num_int2   = 3

We used both the C style type conversion and the function-style casting for type conversion and displayed the results. Since they perform the same task, both give us the same output.


Type Conversion Operators

Besides these two type castings, C++ also has four operators for type conversion. They are known as type conversion operators. They are:

  • static_cast
  • dynamic_cast
  • const_cast
  • reinterpret_cast

We will learn about these casts in later tutorials.


Recommended Tutorials:

  • C++ string to int and Vice-versa
  • C++ string to float, double and Vice-versa

C++ Variables ,Constants and literals

In this tutorial, we will learn about variables, literals, and constants in C++ with the help of examples.

C++ Variables

In programming, a variable is a container (storage area) to hold data.

To indicate the storage area, each variable should be given a unique name (identifier). For example,

int age = 14;

Here, age is a variable of the int data type, and we have assigned an integer value 14 to it.

Note: The int data type suggests that the variable can only hold integers. Similarly, we can use the double data type if we have to store decimals and exponentials.

We will learn about all the data types in detail in the next tutorial.

The value of a variable can be changed, hence the name variable.

int age = 14;   // age is 14
age = 17;       // age is 17


Rules for naming a variable

  • A variable name can only have alphabets, numbers and the underscore _.
  • A variable name cannot begin with a number.
  • Variable names cannot begin with an uppercase character.
  • A variable name cannot be a keyword. For example, int is a keyword that is used to denote integers.
  • A variable name can start with an underscore. However, it’s not considered a good practice.

Note: We should try to give meaningful names to variables. For example, first_name is a better variable name than fn.


C++ Literals

Literals are data used for representing fixed values. They can be used directly in the code. For example: 12.5'c' etc.

Here, 12.5 and 'c' are literals. Why? You cannot assign different values to these terms.

Here’s a list of different literals in C++ programming.


1. Integers

An integer is a numeric literal(associated with numbers) without any fractional or exponential part. There are three types of integer literals in C programming:

  • decimal (base 10)
  • octal (base 8)
  • hexadecimal (base 16)

For example:

Decimal: 0, -9, 22 etc
Octal: 021, 077, 033 etc
Hexadecimal: 0x7f, 0x2a, 0x521 etc

In C++ programming, octal starts with a 0, and hexadecimal starts with a 0x.


2. Floating-point Literals

A floating-point literal is a numeric literal that has either a fractional form or an exponent form. For example:

-2.0

0.0000234

-0.22E-5

Note: E-5 = 10-5


3. Characters

A character literal is created by enclosing a single character inside single quotation marks. For example: 'a''m''F''2''}' etc.


4. Escape Sequences

Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C++ programming. For example, newline (enter), tab, question mark, etc.

In order to use these characters, escape sequences are used.

Escape SequencesCharacters
\bBackspace
\fForm feed
\nNewline
\rReturn
\tHorizontal tab
\vVertical tab
\\Backslash
\'Single quotation mark
\"Double quotation mark
\?Question mark
\0Null Character

5. String Literals

A string literal is a sequence of characters enclosed in double-quote marks. For example:

"good"string constant
""null string constant
" "string constant of six white space
"x"string constant having a single character
"Earth is round\n"prints string with a newline

We will learn about strings in detail in the C++ string tutorial.


C++ Constants

In C++, we can create variables whose value cannot be changed. For that, we use the const keyword. Here’s an example:

const int LIGHT_SPEED = 299792458;
LIGHT_SPEED = 2500 // Error! LIGHT_SPEED is a constant.

Here, we have used the keyword const to declare a constant named LIGHT_SPEED. If we try to change the value of LIGHT_SPEED, we will get an error.

A constant can also be created using the #define preprocessor directive. We will learn about it in detail in the C++ Macros tutorial.