C++ Variables, Literals and Constants: In this tutorial, we will find out about variables, literals, and constants in C++ with the help of examples.
In this article, you will learn-
C++ Variables
In programming, a variable is a container (storage area) to hold data.
To show the capacity region, every variable ought to be given a unique name (identifier). For instance,
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 proposes that the variable can just hold numbers. So also, we can use the double data type if we need 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, henceforth 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: 1, 2.5, ‘c’ etc.
Here, 1, 2.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) with no fragmentary or exponential part. There are three types of integer literals in C programming:
decimal (base 10)
octal (base 8)
hexadecimal (base 16)
For instance:
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 can’t be typed or has special significance in C++ programming. For instance, newline (enter), tab, question mark, and so on.
In order to use these characters, escape sequences are used.
Escape Sequences | Characters |
\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 |
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 make factors whose worth can’t be changed. For that, we use const catchphrase. 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.
Please feel free to give your comment if you face any difficulty here.