When creating a variable or constant in C++, you must specify the corresponding data type, otherwise the variable cannot be allocated memory
1. Integer
What integers do: Integer variables represent data of integer type
There are several ways to represent an integer, the difference is that it occupies different memory space
1.Short takes up 2 bytes of space
The value range is -2^15 ~ -2^15-1
2.int (integer) takes up 4 bytes of space
The value range is -2^31 ~-2^31-1
3.Long occupies space 8 bytes for win 4 bytes for Linux 4 bytes for Linux
The value range is -2^31~-2^31-1
4.long long takes up 8 bytes of space
2.sizeof keyword
Function: The sizeof keyword can count the memory size occupied by the type
Syntax: sizeof (data type/variable)
Case:
short num = 10;
cout <<"short occupies memory space: "<< sizeo(num)<< end1;
cout <<"short occupies memory space: "<< sizeo(short)<< end1;
3. Real type (floating point type)
Role: Used to represent decimals
1. Single precision float 4 bytes 7 significant digits
2. Double precision double 8 bytes 15~16 significant digits
By default, a decimal number is output, which displays 6 significant digits
float f1 = 3.14 3.14 is the default double in this case
Therefore, it needs to be changed to float f1 = 3.14f to force 3.14 to convert 3.14 to single-precision float.
Case
#include<iostream>
using namespace std;
int main() {
float f1 = 3.1415926f;
cost <<"f1 ="<< f1 << end1;
double d1 = 3.1415926;
cost <<"d1 ="<< d1 << end1;
system("pause");
return 0;
The output is:
f1 = 3.14159
d1 = 3.14159
4. Character type
What it does: Character variables are used to display a single character
Syntax: char ch = 'a' ;
Note:
1. When displaying character-type variables, enclose the characters in single quotes, do not use double quotation marks.
2. There can only be one character inside a single quote, not a string.
Character variables in C++ occupy only 1 byte
The character variable does not put the character itself into the memory storage, but puts the corresponding ASCII code into the storage unit.
Case
#include<iostream>
using namespace std;
int main() {
char ch = ' a ' ;
cout << ch << end1;// The result is a
cout << "Memory occupied by char variables" <<
sizeof(char) << end1;// The result is 1
cout << "Character variables correspond to ASCII encodings" <<
(int)ch << end1;// The result is 97
system("pause");
return 0;
ASCII comparison table
ASCII non-printing control characters: The numbers 0-31 on the ASCII table are assigned to control characters that are used to control some peripheral devices like printers.
ASCII printed characters: The numbers 32-126 are assigned to characters that can be found on the keyboard and appear when a printed document has been viewed.
5. Escape characters
Function: Used to represent some ASCII characters that cannot be displayed
Commonly used escape characters are:
Case:
#include<iostream>
using namespace std;
int main() {
The newline character \n
Cost <<"hello world\n";
The backslash \\ simply outputs a backslash
cost <<"\\"<<end1;
The horizontal tab character \t neatly outputs data
cout <<"aaa\thelloworld"<<end1;
cout <<"aa\thelloworld"<<end1;
cout <<"aaaaaa\thelloworld"<<end1;
system("pause");
return 0;
Output result:
hello world
\
aaaa hello world
aa hello world
aaaaaa hello world
6. String type
Function: Used to represent a string of characters
Two styles
1.c-style string: char variable name[] = "string value"
C-style strings should be enclosed in double quotes
2.C++ style string: string variable name = "string value"
C++ style strings need to be added to the header file <string>#include
7. Boolean type bool
What it does: Boolean data types represent true or false values
Two values of type bool: true-true (essentially 1)
false - false (essentially 0)
The bool type occupies 1 byte in size
Case
8. Data Entry
Function: Used for keyboard past data
Keyword: cin
Syntax: cin >> variable
Case: