Generally speaking, whether you want to change careers to become a C++ development engineer, or qualified C++ development engineer, you need to understand and data C++ basics. C++ variable types:

Learn more about C++ variable types

What is a variable? Variables are derived from mathematics, which is an abstract concept in computer language that can store calculation results or represent values.

In computer programming languages, a variable is the name of the memory space provided for data in the program. That is, whenever you create a variable in the program, regardless of whether it is used later or not, the compilation system automatically creates a space in memory for the value you want to save, and the variable is the name we give that space. Let’s create a variable named num and assign it to 10.

intnum=10;

How are variables used in a program? Declare variables in a program:

Format 1: Data type Variable name 1, variable name 2, variable name 3… ;

Format 2: Data type variable name = value;

intnum1,num2,num3; // declare variable num1num2num3 intnum=10; // declare the variable num and assign it to 10

Copy code Type: What actually happens after the [CPP] variable is created? Once a variable is created, it creates a corresponding space in memory, whether it is used or not. The size of this space varies according to different data types. The name of the variable is the name of the space in which the value of the variable is stored.

What are the variable types? The basic data types in C++ are: integer, floating point, character, and Boolean.

Integer type (signed and unsigned) : The integer type stores some integers, for example, -1-2-30123…… , but because of the size range of values, integer types are divided into the following types.

1, int type: int.

The Int type contains 4 bytes and 32 bits. The first digit from left to right is the sign bit. 0 represents a positive number and 1 represents a negative number. So int represents the data range: – (2^31) — (2^31) -1, i.e. -2147483648 — 2147483647.

2. Short type: short.

The short type consists of 2 bytes and 16 bits. The first digit from left to right is the sign bit. 0 represents a positive number and 1 represents a negative number. So the range of data represented by short is: – (2^15) — (2^15) -1, i.e. -32768 — 32767.

3, long int type: long

Long is the same type as int when compiled to 32-bit; It is different when the compiled system is 16 – or 64-bit. It is generally accepted that long>=int

4. Long integer type: longlong.

The Longlong type has a total of 8 bytes and 64 bits. The first digit from left to right is the sign bit. 0 represents a positive number and 1 represents a negative number. So the range of data represented by longlong is: – (2^63) – (2^63) -1.

Floating point types: floating point types hold decimals. Floating point types fall into three categories: single-precision float, double, and longdouble

1, single-precision floating point type: float

Float occupies 4 bytes, a total of 32 bits. 1 is the sign bit (0 represents a positive number, 1 represents a negative number), 8 is the exponential bit, and 23 is the mantissa bit.

Float The value ranges from -3.4028234661038 to 3.4028234661038

8-bit indices can represent -128 to 127

Significant digits are at most all ones, approximately multiplied by two

So the maximum of the whole number is 2 times 2^127, which is about 3.4 times 10^38

Float precision range

Float has 23 mantras, 2^23=8388608, a total of seven digits, so the precision of float is in the range of 6-7 decimal places, 6 is absolute precision.

2. The double floating-point type: double

Double holds 8 bytes, 64 bits in total. 1 is the sign bit (0 represents a positive number, 1 represents a negative number), 11 is the exponential bit, and 52 is the mantissa bit. Double Value range: -1.7976931348623158 x 10308 to 1.7976931348623158 x 10308Copy the code

An 11-bit index can represent -1024 to 1023

Significant digits are at most all ones, approximately multiplied by two

So the maximum value of this whole number is 2 times 2^1023, which is approximately 1.7976931348623158*10308

Doublet precision range

2^52 is a 16-digit number, so the accuracy of a double is between 15 and 16 decimal places.

Long precision floating point type: long double

Long double holds 12 bytes, a total of 96 bits

Long double The value ranges from -1.2E4932 to 1.2E4932

Long doubles are accurate in the range of 19-20 decimal places, with 19 being absolute.

Char (the char type can also be signed or unsigned) : A character type stores characters such as letters, numbers, and characters. Each character takes up 1 byte, a total of 8 bits. Because they are symbols such as letters, they are still stored in computer memory as integer values, that is, as ASCII codes.

Similarly, the first bit from left to right is the sign bit (0 represents a positive number and 1 represents a negative number). Therefore, the value range is -2^7 -2^ 7-1.

When it comes to character types, there is one more thing to note: “escape characters” : they are used for special formatting control.

4. Boolean type: bool A Boolean type is a logical data type with only two values:

True indicates true and false indicates false.

The corresponding values are: true represents 1, false represents 0.

#include<iostream>
using namespace std;
int main(a){
  bool flag = true;    // Declare the variable flag and assign it to true
  bool flag1 = false; // Declare the variable flag1 and assign it to false
  return 0;
 }
Copy the code

C++ learning materials are free of charge