Hello, I’m Liang Tang.
Today is the fourth part of the EasyC++ series, we are going to talk about integers in C++. Those who want a better viewing experience can visit the Github repository.
Welcome to star, welcome to PR
The integer
An integer is an integer, corresponding to a decimal.
While many languages can only represent one type of integer (such as Python), C++ provides several different types depending on the range of integers.
The basic integer types in C++ include char, short, int, and long. In C++ 11, long long was added. Long long is not supported in some compilers and __int64 is supported. A separate article will explain and supplement this later.
The char type has some special properties and is usually used as a character rather than an integer. In addition, there are both signed and unsigned versions of each type, so there are a total of 10 types.
Short, int, long and long long
All four types are integers, the only difference being the range. The range of these four types in C++ is not fixed due to the underlying hardware. There is no way to unify the types because of compatibility with different types of systems and hardware.
To avoid inconvenience, C++ provides a flexible standard that ensures a minimum range for each type.
- Short at least 16 bits
- Int is at least as long as short
- Long is at least 32 bits long and at least as long as int
- Long Long is at least 64-bit and at least as long as long
Bits and bytes
The basic unit of computer memory is the bit. A bit has only two values, 0 and 1, and can be thought of as a switch. There are 256 different combinations of 8-bit bits, i.e. 28=2562^8=25628=256. So an 8-bit bit can represent either 0-255 or -128-127.
With each additional bit, the range that can be represented doubles. Therefore, 16 bits can represent 65536 values, while 32 bits can represent 4294672296 values. 64-bit bits are larger and approximately represent 1.8∗10191.8 * 10^{19}1.8∗1019. This is large enough to represent all the stars in the Milky Way.
An 8-bit binary bit is a byte, which is the smallest unit of storage in a computer. 1024 bytes are called 1KB, 1024 KB is called 1MB, and 1024MB is 1GB.
Generally, in operating systems, there is a minimum length, which is usually determined by the number of bits in the CPU. On common operating systems such as Linux, Windows, and MacOS, int and long are 32 bits, short are 16 bits, and long long is 64 bits.
In addition to the usual assumptions, there are some tools available in C++ to look at. The sizeof function, for example, looks at the number of bytes a variable takes up. This function can take either the type of the variable or the variable itself. If the variable type is passed in, the result is the amount of memory occupied by the variable of that type, and if the variable itself, the amount of memory occupied by the variable itself.
Note that parentheses are required when viewing the object as a variable type, and are optional if the object is a variable itself.
cout << sizeof(int) << endl;
int a = 3;
cout << sizeof a << endl;
Copy the code
The output of both couts is 4.
In addition to sizeof, C++ provides a number of constants. INT_MAX, LONG_MAX, and so on. As the name implies, the values of these constants are the maximum values of each type. C++ primer says that these constants are stored in the header file climits, but I tried and found that they can be accessed without the header, so they may already be built-in variables.
There is a maximum as well as a minimum, such as INT_MIN, LONG_MIN, etc. Personally, I don’t think it’s used very often, so I’ll just go over primer if you need to.
Initialize the
We have just introduced the way variables are declared, like this:
int a, b;
Copy the code
But we can combine a variable declaration statement with an assignment statement and initialize it at the same time. Such as:
int a = 3;
char c = 'c';
Copy the code
Of course, this is just the most basic way to initialize things, especially when it comes to object orientation.
Unsigned type
As mentioned earlier in the introduction to bits and bytes, for example, 8-bit binary bits can represent either 0-255 or -128-127. That’s the difference between signed and unsigned.
If we are sure that the integers we want to store are non-negative and we want to have a larger range, we can use the unsigned modifier to modify these types. Such as:
unsigned short ushort;
unsigned int uint;
unsigned long ulong;
unsigned long long ull;
Copy the code
Where unsigned int can be shortened to unsigned, the other uses are the same as signed integers.