1. Initial value and type conversion of variables

1. Initial value and type conversion of variables

1.1. Assign initial values to variables

In programs, it is often necessary to assign initial values to variables in order to use them. There can be multiple methods in language programs, and the method of assigning initial values during definition is called initialization. The general form for assigning initial values in variable descriptions is:

Type descriptor variable 1=value 1, variable 2=value 2; For example:

int a = b = c = 5;
float x = 32, y = 3f, z = 0.75;
char ch1 = 'K', ch2 = 'P';

It should be noted that continuous assignment is not allowed in the description, such as a=b=c=5, which is illegal.

1.2. Conversion of Variable Types

The data type of variables can be converted. There are two methods of conversion, one is automatic conversion, and the other is forced conversion.

Automatic conversion occurs when mixing quantities of different data types and is automatically completed by the compilation system. Automatic conversion follows the following rules:

  1. If the types of computation involved are different, convert them to the same type first, and then perform the computation.

  2. Convert in the direction of increasing data length to ensure accuracy does not decrease. When performing operations on int and long types, first convert the int quantity to long type before performing the operation.

  3. All floating-point operations are performed with double precision, and even expressions containing only float single precision operations must be converted to double before the operation can be performed.

  4. When char and short types participate in operations, they must first be converted to int type.

  5. In the assignment operation, when the data types of the quantities on both sides of the assignment number are different, the type of the quantity on the right side of the assignment number will be converted to the type of the quantity on the left side. If the length of the data type on the right is long on the left, some data will be lost, which will reduce accuracy. The lost data will be rounded forward by rounding.

1.3. Force type conversion

Mandatory type conversion is achieved through type conversion operations. Its general form is: (type descriptor) (expression) Its function is to force the operation result of the expression to the type represented by the type descriptor. For example: (float) a Convert a to real (int) (x+y) Convert the result of x+y to integer. When using cast conversion, the following issues should be noted:

  1. Both type descriptors and expressions must be enclosed in parentheses (individual variables may not be enclosed in parentheses). For example, writing (int) (x+y) as (int) x+y converts x to an int type and then adds it to y.

  2. Whether it is forced conversion or automatic conversion, it is only a temporary conversion of the data length of the variable for the needs of this operation, and does not change the type defined for the variable in the data description.