English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn C ++in variables, literals, and constants.
In programming, variables are containers (storage areas) used to store data.
To represent the storage area, each variable should be assigned a unique name (identifier). For example,
int age = 14;
In this example, age is a variable of the int data type, and we have assigned an integer value to it14.
Note:The int data type indicates that the variable can only contain integers. Similarly, if it is necessary to store decimal numbers and exponentials, the double data type can be used.
In the next tutorial, we will learn about all data types in detail.
The value of a variable can be changed, therefore it is namedvariable(variable).
int age = 14; // age is 14 age is 17; // age is 17
Variable names can only be letters (A-Z, a-z) and numbers (0-9) or an underscore (_).
Variable names cannot start with a number, the first letter must be a letter or an underscore.
Variable names are case-sensitive.
Variable names cannot beKeywords, to avoid conflicts. For example, int is a keyword used to represent integers.
Variable names can start with an underscore. But this is not a good habit.
Note:We should try to name variables meaningfully. For example, first_name is a better variable name than fn.
Literals are used to represent fixed values. They can be used directly in the code. For example:1,2.5, 'c', etc.
Here,1,2.5and 'c' are literals. Why? You cannot assign different values to these items.
This is C ++List of different literals in programming.
Integers are numeric literals without any fractional or exponential parts (associated with numbers). In C programming, there are three types of integer literals:
Decimal (with10base)
Octal (with8base)
Hexadecimal (with16base)
For example:
Decimal numbers: 0, -9, 22 etc Octal numbers: 021, 077, 033 etc Hexadecimal numbers: 0x7f, 0x2a, 0x521 etc
In C ++In programming, octal numbers start with 0, and hexadecimal numbers start with 0x.
Floating-point literals are numeric literals in fractional or exponential form. For example:
-2.0
0.0000234
-0.22E-5
Note: E-5 = 10-5
Character literals are created by enclosing a single character in single quotes. For example: 'a', 'm', 'F', '}2' and '}' are used.
Sometimes, in C ++In programming, you must use characters that cannot be typed or have special meanings. For example, newline characters (carriage return), tab, question mark, etc.
To use these characters, escape sequences are used.
Escape character | Description |
---|---|
\b | Backspace (BS) |
\f | Form feed (FF) |
\n | Line feed (LF) |
\r | Carriage return (CR) |
\t | Horizontal tab (HT) |
\v | Vertical tab (VT) |
\\\\ | Backslash |
\' | Single quote character |
\" | Double quote character |
\? | Question mark character |
\0 | Null character (NULL) |
String literals are a sequence of characters enclosed in double quotes. For example:
"good" | String literal |
"" | Empty string literal |
" " | String literal with spaces |
"x" | String literal with a single character |
"Earth is round\n" | Print the string with a newline character |
We will be using C ++Learn in detail about strings in the string tutorial.
In C ++In, we can create variables whose values cannot be changed. To do this, we use the const keyword to define a constant. Here is an example:
const int LIGHT_SPEED = 299792458; LIGHT_SPEED = 2500 // Error! LIGHT_SPEED is a constant
Here, we used the keyword const to declare a constant named LIGHT_SPEED. If you try to change the value of LIGHT_SPEED, an error message will be thrown.
You can also use the #define preprocessor directive to create constants. We will be using c++Learn it in detail in the macro tutorial.