2 data types
C++ stipulates that when creating a variable or constant, the corresponding data type must be specified, otherwise memory cannot be allocated to the variable.
The meaning of the existence of the data type: to allocate the appropriate memory space to the variable.
2.1 Integer
**Function:** Integer variables represent data of integer type.
There are several ways to represent integer types in C++, the difference lies in the different memory space occupied.
type of data | take up space | Ranges |
---|---|---|
short (short integer) | 2 bytes | (-215~215-1) |
int (integer) | 4 bytes | (-231~231-1) |
long (long integer) | Window is 4 bytes; Linux is 4 bytes (32-bit), 8 bytes (64-bit) | (-231~231-1) |
long long | 8 bytes | (-263~263-1) |
#include <iostream> using namespace std; int main() { //1. Short integer (-32768~32767) short num1 = 32768; //2. Integer int num2 = 10; //3. Long integer long num3 = 10; //4, long integer long long num4 = 10; cout << "num1 = " << num1 << endl; cout << "num2 = " << num2 << endl; cout << "num3 = " << num3 << endl; cout << "num4 = " << num4 << endl; system("pause"); return 0; }
2.2 sizeof keyword
**Function:** Use the sizeof keyword to count the memory size occupied by the data type.
Syntax: sizeof( datatype/variable)
Example:
int main() { //You can use sizeof to find the memory size of the data type //Syntax: sizeof( datatype/variable) short num1 = 10; cout << "short Occupied space memory:" << sizeof(short) << endl; int num2 = 10; cout << "int Occupied space memory:" << sizeof(int) << endl; cout << "long Occupied space memory:" << sizeof(long) << endl; cout << "long long Occupied space memory:" << sizeof(long long) << endl; system("pause"); return 0; }
Integer size comparison:
short < int <= long <= long long
2.3 Real (float)
**Function:** is used to represent decimals.
There are two types of floating-point variables:
- single precision float
- double precision double
The difference between the two is the range of significant digits represented.
type of data | take up space | valid number range |
---|---|---|
float | 4 bytes | 7 significant digits |
double | 8 bytes | 15~16 significant digits |
Example:
int main() { //1. Single precision float //2. Double precision double float f1 = 3.1415926f; cout << "f1 = " << f1 << endl; double d1 = 3.1415926; cout << "d1 = " << d1 << endl; //Statistics float and double occupy memory space cout << "float Occupy memory space:" << sizeof(float) << endl; cout << "double Occupy memory space:" << sizeof(double) << endl; //Scientific notation float f2 = 3e2; // 3 * 10 ^ 2 cout << "f2 = " << f2 << endl; float f3 = 3e-2; cout << "f3 = " << f3 << endl; system("pause"); return 0; }
In c++, whether it is single precision or double precision, output a decimal and display up to 6 significant figures.
Output result:
f1 = 3.14159 d1 = 3.14159 float Occupied memory space: 4 double Occupied memory space: 8
2.4 Character type
**Function:** The character variable is used to display a single character.
Syntax: char ch = 'a'
Note 1: When displaying character variables, enclose the characters in single quotes, not double quotes
Note 2: There can only be one character in single quotes, not a string
Character variables in C++ and C occupy only one byte.
Character variables do not store the character itself in memory, but store the corresponding ASCII code into the storage unit.
Example:
int main() { //1. How to create a character variable char ch = 'a'; cout << ch << endl; //2. The size of the memory occupied by the character variable cout << sizeof(char) << endl; //3. Common errors of character variables //char ch2 = "b"; //When creating a character variable, use single quotes //char ch2 = 'abcdef'; //When creating a character variable, there can only be one character in the single quote //4. Character variables correspond to ASCII encoding // a - 97 // A - 65 cout << (int)ch << endl; system("pause"); return 0; }
2.5 Escape characters
**Function:** Used to represent some ASCII characters that cannot be displayed.
At this stage, our commonly used escape characters are: \n \ \t
escape character | meaning | ASCII code value (decimal) |
---|---|---|
\a | alarm | 007 |
\b | Backspace (BS) moves the current position to the previous column | 008 |
\f | Form feed, move the current position to the beginning of the next page | 012 |
\n | newline, move the current position to the beginning of the next line | 010 |
\r | Enter, move the current position to the beginning of this line | 013 |
\t | Horizontal tab (skip to next TAB position) | 009 |
\v | vertical tabulation | 011 |
\\ | Represents a backslash character "\" | 092 |
int main() { //escape character //newline character \n cout << "Hello World\n" << endl; //backslash "\" cout << "\\" << endl; //The horizontal tab character \t function: the data can be output neatly cout << "aaa\tbbb" << endl; cout << "aa\tbbb" << endl; system("pause"); return 0; }
It can be seen that both \n and endl are used for line breaks. When both are used, two line breaks appear.
2.6 String type
**Function:** Used to represent a string of characters
two styles
1. C-style strings
char variablename[] = "string value"
Example:
int main() { //1. C-style strings char str[] = "Hello World"; cout << str << endl; system("pause"); return 0; }
Notice:
- C-style strings are enclosed in double quotes.
- To add square brackets char str[] = "Hello World";
2. C++ style strings
string variable name = "string value"
Example:
int main() { //1. C-style strings char str[] = "Hello World"; cout << str << endl; //2. C++ style strings string str2 = "Hello C++"; cout << str2 << endl; system("pause"); return 0; }
Notice:
- When using string here, you need to introduce the header file #include <string>, but the 2019 version and later do not need to add
2.7 Boolean type bool
Role: Boolean data type represents a true or false value
The bool type has only two values:
- true - true (essentially 1)
- false - false (essentially 0)
The bool type occupies one byte.
Example:
int main() { //1. Create bool data type bool flag = true; cout << flag << endl; bool flag2 = false; cout << flag2 << endl; //2. View the memory size occupied by bool cout << "bool Types occupy memory space:" << sizeof(bool) << endl; system("pause"); return 0; }
2.8 Data input
Role: used to get data from the keyboard
keyword: cin
Syntax: cin >> variable
Example:
int main() { //1. Integer int a = 0; cout << "Please give integer variable a Assignment:" << endl; cin >> a; cout << "Integer variable a = " << a << endl; //2. Floating point float f = 3.14f; cout << "Please give float variable f Assignment:" << endl; cin >> f; cout << "floating point variable f = " << f << endl; //3. Character type char ch = 'a'; cout << "Please give character variable ch Assignment:" << endl; cin >> ch; cout << "character variable ch = " << ch << endl; //4. String type string str = "kevin"; cout << "Please give string variable str Assignment:" << endl; cin >> str; cout << "string variable str = " << str << endl; //5. Boolean type bool flag = false; cout << "Please give boolean type variable flag Assignment:" << endl; cin >> flag; cout << "boolean variable flag = " << flag << endl; //system("pause"); return 0; }