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,
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
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,
intis a keyword. Hence, we cannot use
intas 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.
Literals are data used for representing fixed values. They can be used directly in the code. For example:
Let's explore these commonly used literals in C++.
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)
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
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
-0.22E-5 is equal
There are two boolean literals:
Note: Booleans are case-sensitive.
A character literal is created by enclosing a single character inside single quotation marks. For example:
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.
||Single quotation mark|
||Double quotation mark|
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
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