2 Data types

2.1 the integer

There are about four types in C++ that can represent integers, depending on the space they take up:

The data type Space occupied (in bytes)
short 2
int 4
long Windows(4), Linux(4), 64-bit systems (8)
long long 8

Although my system is 64-bit, I have tried to write a long with only 4 bytes, probably because of VS. This isn’t particularly stable, so try not to use long after that.

2.2 The sizeof keyword

Function: You can calculate the amount of space taken up by a data type.

Syntax: sizeof(data type/variable) unit: byte

According to the previous experiments, sizeof keyword can not only count the space occupied by the basic data type, but also determine the structure and class by sizeof. As for the static objects in the class, methods, such as how to calculate the space. You can keep it for a while and look at it later.

2.3 floating-point

Floating-point variables are divided into two types: float and double

The data type Space occupied (in bytes) Significant number range
float 4 seven
double 8 15 to 16

By default, the compiler treats a decimal as a double-precision floating-point number. When we create a single-precision variable, we can add the ‘f’ suffix to get a single-precision constant.

By default, printing a decimal will show six significant digits.

There are two ways to represent a decimal:

  1. Decimal counting method: that is, use the decimal point to express, like 3.14159.
  2. Scientific counting: similar to 3e2, divided by e. E is preceded by significant digits, followed by significant digits

2.4 character

Function: Displays a single character.

Although the data stored in the CHAR type is ASCII code in nature, once the computer recognizes that the data is CHAR, it will print it as if it were the character itself. So CHAR itself can be added and subtracted just like normal data.

Syntax: char ch = ‘a’;

There are two things to note: first, characters need to be enclosed in single quotes, and second, only one character can be placed in single quotes.

With a cast, you can either input ASCII or output ASCII.

Output ASCII

You can convert the characters that you want to print ASCII to integers and then print them.

int main() {
    char ch = 'A';
    cout << (int)ch << endl;

    return 0;

Enter the ASCII

ASCII code can be output directly, like this:

int main(){
    char ch = 97;
    cout << ch;
    return 0;

So that’s going to print an A.

If we want to type an ASCII code through the keyboard and then convert it to a character, we can’t. Because typing ASCII will be treated as a character, for example if we type 97, then 9 will be treated as the first character typed into the input stream.

In this case, we need to use an integer to get input, and then cast it to a char.

int main(){
    int ch;
    cin >> ch;
    cout << (char)ch;
    return 0;

Important ASCII code

A 65

a 97

All capital letters plus 32 will become lowercase.

2.5 Escape characters

Function: used backslash ‘\’ to indicate some ASCII characters that cannot be displayed

More commonly used escape characters:

\n A newline
\ \ The backslash
\t Horizontal TAB

2.6 the string

Represents a whole string of characters, and there are two styles of strings.

C-style string

Syntax: char variableName[size] = “…” ;

You can specify the size of the string, or you can leave the size blank, so that the system will automatically count the length of the string and get a size.

For example:

int main(){ using namespace std; char hello[] = "Hello, World!" ; cout << hello << endl; cin.get(); return 0; }

It is worth mentioning that the sizeof keyword is also valid for C-style strings. If you let the system count the length automatically, you will get the length of the string plus 1 (because the string will end with a null character). But if you specify the size of the string, you get the size of the string.

C++ Style String

Syntax: string stringName = “…”

Need to include a <string> header file

Automatic generation, and a variety of operations more convenient.

#include<string> using namespace std; int main() { string str = "Hello, World!" ; cout << str << '\n'; return 0; }

2.7 Boolean types

Function: Indicates true or false

There are only two values: true false but they are essentially 1 and 0

Syntax: bool variableName = true/false

Unlike other languages, the default output for a Boolean value is 0 or 1.

2.8 Data input

Function: Input data through the keyboard

Syntax: cin >> variableName;