The road to C++ learning (3): variables

foreword

Variables are basically the foundation of all programming languages ​​and are the basis upon which projects are built.
The following code declares a variable:

// int就是变量的类型
// num就是变量名
// 0就是num的初始值
int num = 0;

In order to facilitate memory and understanding, compare the type and variable name:

  1. It can be imagined as a wooden box with items inside. The crate is the variable name , and the item is the data .
  2. What kinds of items are contained in the wooden box , such as apples, pears, and bananas, correspond to the type of variable , int, short, and long.

1. Variable name

There are the following naming rules for C++ variable names (here directly carry the content of the Chinese version 3.1.1 of the 6th edition of C++ Primer Plus)

  1. Only alpha characters, numbers, underscore (_) can be used in the name
  2. The first character in the name cannot be a number
  3. Distinguish between uppercase and lowercase characters (for example: int A and int a are two different variable names)
  4. Cannot use C++ keywords as names
  5. Names starting with two underscores or an underscore and a capital letter are reserved for implementation (the compiler and the resources it uses). Names beginning with an underscore are reserved to the implementation for use as global identifiers. ( In human terms, it is best not to use a combination similar to _time_stop, _Donut as a variable name )
  6. C++ has no limit on the length of the name, and all characters in the name are meaningful. But some platforms have a length limit

A general variable name can be written like " my_name " or " myName ", and I am used to writing it in the form of myName.

2. Variable type

What are the variable types of C++?
basic data type

// 整型
int,short,long,long long
// 浮点型
float,double,long double
// 字符类型
char
// 布尔类型
bool

There are also types such as enumerations, pointers, arrays, references, classes, etc., which will be introduced step by step later.

2.1 Integer

There are four types of integers: short, int, long, and long long

#include<iostream>

int main() {
    
    
	short var_short = 22;
	int var_int = 123;
	long var_long = 1234l; // 后缀l,表示long
	long long var_long_long = 12345ll; // 后缀ll,表示long long
	return 0;
}

2.1.1 Initialization

#include<iostream>

int main() {
    
    
	// 声明一个int类型的变量,但是不初始化
	int var; // 不建议这样做
	
	// 声明一个int类型的变量并且初始化为22
	int var = 22;
	return 0;
}

There are two ways to declare variables, the first is only declared without initialization; the second is declared and initialized. Do not use the first method , because for C++, if you do not manually initialize, then the value of the variable is a random memory data, which will directly report an error for Visual Studio.

error C4700: 使用了未初始化的局部变量“var”

So different from Java's automatic initialization, if you declare a variable for C++, you must remember to manually initialize the variable.

2.1.2 Range of values

For the types of int, short, long, and long long on a 32-bit operating system, what is the maximum value and the minimum value? How much memory do they occupy?
For the size occupied by an integer, refer to the content of C++ Primer Plus 6th edition Chinese version 3.1.3:

  1. short at least 16 bits
  2. int is at least as long as short
  3. long is at least 32 bits long and at least as long as int
  4. long long is at least 64 bits long and at least as long as long

The memory size occupied by different systems is different, so how do we know the memory size occupied by the current system type? You can use sizeof() to get the occupied memory size.

#include<iostream>

int main() {
    
    
	// windows 10,Visual Studio 2022,Debug模式,x86
	std::cout << sizeof(int) << std::endl;
	std::cout << sizeof(short) << std::endl;
	std::cout << sizeof(long) << std::endl;
	std::cout << sizeof(long long) << std::endl;
	return 0;
}

Output result:

4
2
4
8

So how to determine the maximum and minimum values? It can also be determined by using the tools climits provided by C++ and the macro constants in cstdint (C++11).
Reference API address: Numerical limits in C++ standard library header files

#include<iostream>
#include<climits>

int main() {
    
    
	// windows 10,Visual Studio 2022,Debug模式,x86
	std::cout << "short最小值: " << SHRT_MIN << std::endl;
	std::cout << "short最大值: " << SHRT_MAX << std::endl;
	
	std::cout << "int最小值: " << INT_MIN << std::endl;
	std::cout << "int最大值: " << INT_MAX << std::endl;
	
	std::cout << "long最小值: " << LONG_MIN << std::endl;
	std::cout << "long最大值: " << LONG_MAX << std::endl;
	
	std::cout << "long long最小值: " << LLONG_MIN << std::endl;
	std::cout << "long long最大值: " << LLONG_MAX << std::endl;
	return 0;
}

Output result:

short最小值: -32768
short最大值: 32767

int最小值: -2147483648
int最大值: 2147483647

long最小值: -2147483648
long最大值: 2147483647

long long最小值: -9223372036854775808
long long最大值: 9223372036854775807

2.1.3 Data type overflow

When you use a certain type of data, the maximum or minimum value of the data type has been reached, but if you continue to increase or decrease the data, it will cause the data to reach the reverse maximum or minimum value.
No nonsense, directly on the code:

#include<iostream>
#include<climits>

int main() {
    
    
	int max_var = INT_MAX;
	std::cout << "int最大值: " << max_var << std::endl;
	max_var = max_var + 1; // 模拟溢出
	std::cout << "int最大值溢出: " << max_var << std::endl;

	int min_var = INT_MIN;
	std::cout << "int最小值: " << min_var << std::endl;
	min_var = min_var - 1; // 模拟溢出
	std::cout << "int最小值溢出: " << min_var << std::endl;
	return 0;
}

output:

int最大值: 2147483647
int最大值溢出: -2147483648

int最小值: -2147483648
int最小值溢出: 2147483647

The code for data type overflow warns us to consider the judgment of boundary problems when dealing with basic data types to prevent overflow.

2.1.4 Unsigned types

The unsigned type is the unsigned type unsigned in the four integer types of int, short, long, and long long, such as the short type.

#include<iostream>
#include<climits>

int main() {
    
    
	unsigned short var = USHRT_MAX;
	std::cout << var << std::endl;
	var = var + 1; // 模拟溢出
	std::cout << var << std::endl;
	return 0;
}

output:

65535
0

The original short type is -32768 to +32767. The unsigned type short sacrifices negative numbers and only takes positive numbers. The range is 0~65535. The unsigned (unsigned) type only represents positive numbers.

2.2 Floating point

The floating point types are mainly float, double, long double. The difference between them and the integer type is that there is a decimal point. The code is as follows:

#include<iostream>
#include<climits>

int main() {
    
    
	float var_f = 1.234f;
	float var0_f = 3.14e+5; // 3.14乘以10000,科学计数法
	double var_d = 2.345;
	long double var_ld = 2.365234;
	std::cout << var0_f << std::endl;
	return 0;
}

2.2.1 Range of values

#include<iostream>

int main() {
    
    
	// windows 10,Visual Studio 2022,Debug模式,x86
	std::cout << sizeof(float) << std::endl;
	std::cout << sizeof(double) << std::endl;
	std::cout << sizeof(long double) << std::endl;
	return 0;
}

output:

4
8
8

2.2.2 Maximum and minimum values

Maximum and minimum values ​​of float, double, long double:

#include<iostream>
#include<cfloat>

int main() {
    
    
	// windows 10,Visual Studio 2022,Debug模式,x86
	std::cout << "float最小值: " << FLT_MIN << std::endl;
	std::cout << "float最大值: " << FLT_MAX << std::endl;

	std::cout << "double最小值: " << DBL_MIN << std::endl;
	std::cout << "double最大值: " << DBL_MAX << std::endl;

	std::cout << "long double最小值: " << LDBL_MIN << std::endl;
	std::cout << "long double最大值: " << LDBL_MAX << std::endl;
	return 0;
}

output:

float最小值: 1.17549e-38
float最大值: 3.40282e+38
double最小值: 2.22507e-308
double最大值: 1.79769e+308
long double最小值: 2.22507e-308
long double最大值: 1.79769e+308

2.3 Character type

The character type is char, and the code is as follows:

#include<iostream>

int main() {
    
    
	// windows 10,Visual Studio 2022,Debug模式,x86
	char ch0 = 'a';
	char ch1 = 'A';
	char ch2 = 97;
	char ch3 = 65;
	std::cout << ch0 << std::endl;
	std::cout << ch1 << std::endl;
	std::cout << ch2 << std::endl;
	std::cout << ch3 << std::endl;
	return 0;
}

output:

a
A
a
A

The focus is on ch2 and ch3, char data can be assigned integers, and 97 and 65 represent a and A in ASCII. Through this feature of the char type, for some algorithm problems that use the char type, it can be solved by using the integer type with addition and subtraction.

#include<iostream>

int main() {
    
    
	// windows 10,Visual Studio 2022,Debug模式,x86
	char ch = 97;
	for (int i = 0; i <= 25; i++) {
    
    
		std::cout << ch << std::endl;
		ch = ch + 1;
	}
	return 0;
}

2.4 Boolean

The Boolean type is very simple, just two values. 0 and non-zero, 0 represents false, non-zero (usually 1) represents true.

#include<iostream>

int main() {
    
    
	// windows 10,Visual Studio 2022,Debug模式,x86
	bool flag0 = true;
	std::cout << flag0 << std::endl;
	bool flag1 = false;
	std::cout << flag1 << std::endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/RQ997832/article/details/123817695