C language - issues such as type conversion and priority in expression evaluation

Table of contents

1. Implicit type conversion

2. Arithmetic conversion

​3. Properties of operators


1. Implicit type conversion

C's integer arithmetic operations are always performed with at least the precision of the default integral type.

To achieve this precision, character and short integer operands in expressions are converted to plain integer types before use, a conversion known as integer promotion .

Significance of integer promotion:

The integer operation of the expression must be executed in the corresponding operation device of the CPU. The byte length of the operand of the integer arithmetic unit (ALU) in the CPU is generally the byte length of int, and it is also the length of the general-purpose register of the CPU. Therefore, even if the addition of two char types is actually performed by the CPU, it must first be converted to the standard length of the integer operand in the CPU. It is difficult for a general-purpose CPU (general-purpose CPU) to directly add two 8-bit bytes directly (although there may be such byte addition instructions in machine instructions). Therefore, the various integer values ​​in the expression whose length may be smaller than the length of int must be converted to int or unsigned int before being sent to the CPU for calculation.

 How to carry out plastic surgery?

Integer promotion is promoted according to the sign bit of the data type of the variable

 Shaping promotion for negative numbers

 positive integer boost

 Unsigned plastic promotion, the high bit is filled with 0

Analyze the code below:

 The specific process is as follows:

 Program output:

 Analyze the code below:

 As long as c participates in the expression operation, the plastic promotion will occur, and the expression +c will be promoted, so sizeof(+c) is 4 bytes.

The expression -c will also undergo plastic promotion, so sizeof(-c) is 4 bytes, but sizeof(c) is 1 byte.

2. Arithmetic conversion

If the operands of an operator are of different types , the operation cannot proceed unless one of the operands is converted to the type of the other. The hierarchy below is called ordinary arithmetic conversions.

 
3. Properties of operators


There are three factors that affect the evaluation of complex expressions.

1. Operator precedence

2. Operator associativity

3. Whether to control the order of evaluation.

Which of two adjacent operators is executed first? Depends on their priorities. If both have the same priority, it depends on their associativity. operator precedence

Adjacent operators with higher precedence are evaluated first, and those with lower precedence are evaluated later.

Associativity works when adjacent operators have the same precedence.

Even if we have mastered the precedence and associativity of various operators, we may write back bugs:

int main()
{
	int i = 10;
	i = i-- - --i * (i = -3) * i++ + ++i;
	printf("i = %d\n", i);
	return 0;
}

 This string of code may produce different results under different compilers.

The following code is also an error code, and the results in different compilers are also different:

 The result of ret under the vs compiler is 12, while the value of ret under gcc is 10

Guess you like

Origin blog.csdn.net/m0_73648729/article/details/130669391