Let’s know about C++ Variables, Literals and Constants
Categories: Programming C++
Let’s know about C++ Variables, Literals and Constants
In this page, we will learning about variables, literals, and constants in C++ with examples.
C++ Variables
In programming, a variable is a box (storage vicinity) to keep facts.
to indicate the storage place, every variable must receive a unique name (identifier). as an example,
int age = 18; |
here, age is a variable of the int information type, and we've got assigned an integer value 18 to it.
we are able to find out about all of the records sorts in detail in the subsequent educational.
The value of a variable can be changed, consequently the call variable.
int age = 18; // age is 18
age = 21; // age is 21
Guidelines for naming a variable
- A variable name can most effectively have alphabets, numbers, and the underscore _.
- A variable call can't begin with a range of.
- it's miles a preferred practice to begin variable names with a lowercase character. for instance, name is most popular to name.
- A variable call cannot be a key-word. for instance, int is a key-word that is used to indicate integers.
- A variable call can begin with an underscore. but, it's now not considered a great exercise.
C++ Literals
Literals are statistics used for representing fixed values. They may be used immediately within the code. as an instance: 1, 2.5, 'c' and so on.
here, 1, 2.5 and 'c' are literals. Why? You can not assign special values to these phrases.
here is a listing of different literals in C++ programming.
1. Integers
An integer is a numeric literal(related to numbers) without any fractional or exponential part. There are three forms of integer literals in C programming:
decimal (base 10)
octal (base 8)
hexadecimal (base sixteen)
as an instance:
Decimal: 0, -nine, 22 and many others
Octal: 021, 077, 033 etc
Hexadecimal: 0x7f, 0x2a, 0x521 etc
In C++ programming, octal begins with a zero, and hexadecimal begins with a 0x.
2. Floating-point Literals
A floating-point literal is a numeric literal that has both a fractional shape or an exponent shape. for instance:
-2.0
0.0000234
-0.22E-5
word: E-5 = 10-5
3. Characters
A individual literal is created by way of enclosing a single individual internal single citation marks. as an instance: 'a', 'm', 'F', '2', '}' and so on.
4. get away Sequences
every now and then, it is vital to use characters that can't be typed or has unique that means in C++ programming. for instance, newline (enter), tab, query mark, and many others.
a good way to use those characters, get away sequences are used.get away Sequences Characters
\b Backspace |
\f Form feed |
\n Newline |
\r Return |
\t Horizontal tab |
\v Vertical tab |
\\ Backslash |
\' Single quotation mark |
\" Double quotation mark |
\? Query mark |
\0 Null individual |
5. String Literals
A string literal is a series of characters enclosed in double-quote marks. as an example:
"properly" string regular |
" " null string constant |
" " string regular of six white area |
"x" string consistent has a single |
"Moon is so far\n" prints string with a newline |
we can find out about strings in elements in the C++ string academic.
C++ Constants
In C++, we will create variables whose cost cannot be changed. For that, we use the const key-word. here's an instance:
const int LIGHT_SPEED = 299792458; |
LIGHT_SPEED = 2500 // mistakes! LIGHT_SPEED is steady. |
Right here, we've used the keyword const to claim a constant named LIGHT_SPEED. If we attempt to trade the value of LIGHT_SPEED, we will get an blunder.
A constant also can be created the use of the #define preprocessor directive.