C++ Variables, Literals and Constants

C++ Variables, Literals and Constants

In this tutorial, we will learn about variables in programming and how to create them in C++. We will also learn about literals and constants with the help of examples.

C++ Variables

In programming, variables are containers (memory slots) for storing data. To indicate the storage area, each variable should be given a unique name. For example,

int salary;

Here, salary is a variable of int data type. This means, our variable salary can only store integer values.

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

Here's the syntax for declaring a variable.


Assigning a value to a variable

Let's assign a value to our salary variable.

int salary;
salary = 55;

We can also assign value to a variable during variable declaration.

int salary = 55;

Changing data of a variable

As suggested by the name, variable, we can assign different values to a variable as per our needs. For example,

int salary = 55;
salary = 5500;

Initially, we assigned 55 to salary. Then, we changed its value to 5500.

Rules for Naming a Variable in C++

  • A variable name can have only letters (both uppercase and lowercase letters), digits and underscores.
  • The first letter of a variable should be either a letter or an underscore.
  • Variable names are case-sensitive.
  • Variables cannot be keywords. Keywords are reserved words in C++ that have special meanings. For example, int is a keyword. Hence, we cannot use int as a variable name.

To learn more about keywords, visit C++ Identifiers and Keywords

Note: We should always try to give meaningful names to variables. For example: firstName 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: 1, 2.5, 'c' etc.

Let's explore these commonly used literals in C++.


Integer literals

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.


Floating-point Literals

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

-2.0
0.0000234
-0.22E-5

Note: -0.22E-5 is equal to -0.22*10-5


Boolean Literals

There are two boolean literals:

  1. true
  2. false

Note: Booleans are case-sensitive.


Character Literals

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

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 Sequences Character
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character

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

C++ Constants

If we want to create a variable whose value cannot be changed, we can use the const keyword. This will create a constant. For example,

const double PI = 3.14;

Notice, we have added keyword const while declaring the variable.

Here, PI is a symbolic constant; its value cannot be changed.

const double PI = 3.14;
PI = 2.9;  // Error