Lesson 4
Lesson 4
Lesson 4
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,
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.
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) 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)
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.
5. String Literals
A string literal is a sequence of characters enclosed in double-quote marks. For example:
C++ Constants
In C++, we can create variables whose value cannot be changed. For that, we use the const keyword. Here's an
example:
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.
1. C++ int
The int keyword is used to indicate integers. Its size is usually 4 bytes. Meaning, it can store values from -2147483648
to 2147483647.
For example,
4. C++ wchar_t
Wide character wchar_t is similar to the char data type, except its size is 2 bytes instead of 1. It is used to represent
characters that require more memory to represent them than a single char.
For example,
Note: There are also two other fixed-size character types char16_t and char32_t introduced in C++11.
5. C++ bool
The bool data type has one of two possible values: true or false.
Booleans are used in conditional statements and loops (which we will learn in later chapters).
For example,
6. C++ void
The void keyword indicates an absence of data. It means "nothing" or "no value". We will use void when we learn about
functions and pointers.
Note: We cannot declare variables of the void type.
Scientific Numbers
A floating point number can also be a scientific number with an "e" to indicate the power of 10:
Example
#include <iostream>
using namespace std;
int main () {
float f1 = 35e3;
double d1 = 12E4;
cout << f1 << "\n";
cout << d1;
return 0;
}
Boolean Types
A boolean data type is declared with the bool keyword and can only take the values true or false.
When the value is returned, true = 1 and false = 0.
Example
#include <iostream>
using namespace std;
int main() {
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun << "\n";
cout << isFishTasty;
return 0;
}
C++ Character Data Types
Character Types
The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':
Example:
#include <iostream>
using namespace std;
int main () {
char a = 65, b = 66, c = 67;
cout << a;
cout << b;
cout << c;
return 0;
}
int main() {
string greeting = "Hello, Merry Christmas";
cout << greeting;
return 0;
}