Hislordship
In this tutorial, we will learn about variables, literals, and constants in C++ with the help of examples.
C++ VariablesIn 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,
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 suggests that the variable can only hold integers. Similarly, we can use the double data type if we have to store decimals and exponential.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.
int age = 14; // age is 14 age = 17; // age is 17Literals 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.
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:
For example:
Decimal: 0, -9, 22 etc Octal: 021, 077, 033 etc Hexadecimal: 0x7f, 0x2a, 0x521 etcIn C++ programming, octal starts with a 0, and hexadecimal starts with a 0x
A floating-point literal is a numeric literal that has either a fractional form or an exponent form. For example:-2.0
00234
-0.22E-5
Note: E-5 = 10-5A character literal is created by enclosing a single character inside single quotation marks. For example: 'a', 'm', 'F', '2', '}' etc.
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.
Stay tune for more tutorials coming up soonHislordship is a young talented young man who seeks to explore more in this tech industry. Currently React and Django developer