Variables, Literals and Constants
In this tutorial, we will learn about variables, declaration of variables, variable rules, literals, literal-types, and constants in C++ with the help of examples.
C++ Variables
A variable is a name given to a memory location. It is used to store data.
The value stored in a variable can be changed or reused during the program execution.
Let's see the syntax to declare a variable
// Declaring a Single variable
type variable_list;
// Declaring Multiple variables
type variable1_list, variable2_list, variable2_list;
To indicate the storage area, each variable should be given a unique name (identifier).
For example,
int apple = 10;
Here, apple is a variable of the int
data type, and we have assigned an integer value 10 to it.
Note: The int
data type suggests that the variable can only hold integer value. Similarly, if we have to store decimals and exponentials we can use the double
data type.
We will learn about all the data types in detail in the later tutorial.
The value of a variable can be changed, hence the name variable.
Example:-
int num = 10; // number is 10
num = 14; // number is 14
Rules for naming a variable
1) A variable name can consist of alphabets (both uppercase and lowercase), numbers and the underscore _
.
2) A variable name cannot begin with a number.
3) No spaces or special characters are allowed.
4) Variable names cannot begin with an uppercase letter.
5) Variable names are case-sensitive (uppercase and lowercase letters are different).
6) A variable name cannot be a keyword. For example, int
is a keyword that is used to denote integers.
7) 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/fname.
C++ Literals
Literals is an object that representing fixed values. They can be used directly in the code. For example: 1
, 5.9
, 'a'
etc.
Here, 1
, 5.9
and 'a'
are literals. Why? You cannot assign different values to these terms.
1) The constant value can be named differently as literal.
2) for Example:- Const int value=15
assigns constant integer value 15 to value and it is an integer leteral.
3) In this tutorial, we will go through an explanation of each of the different types of literals in C++.
Here's a list of different literals in C++ programming.
- Integer literal
- Float literal
- Character literal
- Escape Sequences
- String literal
- Boolean literal
1. Integers literal
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:
1) decimal (base 10)
2) octal (base 8)
3) hexadecimal (base 16)
For example:
Decimal: 0, -7, 22 etc Octal: 041, 087, 043 etc Hexadecimal: 0x2f, 0x5a, 0x261 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:
-5.1
, 0.000432
, -0.62E-2
Note: E-2 = 10-2
3. Characters literal
A character literals are used to represent and store a character inside single quotes.
A character literal is created by enclosing a single character inside single quotation marks. For example: 'a'
, 's'
, 'L'
, '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 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:
"God" |
string constant |
"" |
null string constant |
" " |
string constant of six white space |
"x" |
string constant having a single character |
"We love the Earth\n" |
prints string with a newline |
We will learn about strings in detail in the C++ string tutorial.
6. Boolean Literals
- Boolean literal represents Boolean constant value.
- This literal type can take only two Boolean values.
1. True
2. False
// For representing boolean literal const bool bool_literal1 = True; const bool bool_literal2 = False;
C++ Constants
1) In C++, we can create variables whose value cannot be changed.
2) Constant must have to be initialized at the time of creating it.
3) For that, we use the const
keyword.
4) Constants can be any of the data types.
5) It is considered best practice to define constants using only upper-case names.
Constant Definition in C++
There are two different ways to define constants in C++.
1) By using const keyword.
2) By using #define preprocessor.
For Example:-
const int NUMBER = 299;
NUMBER = 250 // Error! NUMBER is a constant.
Here, we have used the keyword const
to declare a constant named
NUMBER
. If we try to change the value of
NUMBER
, 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.
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Variables, Constants & Literals in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ Scope of Variables
.