1. Signed right shift operation is not portable
2. The number of bits of the shift operation is a negative, undefined
3. Successive assignments vary in length, causing the value of the variable to be truncated.
#include <stdio.h>int main () {char chr_num;int int_num; Truncate int_num = Chr_num = 100000;return 0;}
Compiler hints:
4. Conditional statements do not use = instead of = =
5. Do not use | Instead of | | & instead of &&
6. Compare the different values used to represent the Boolean values of non-0 .
#define FALSE 0#define true 1//if flag is an arbitrary integer value, then the following two statements are not equivalent if (flag = = TRUE) {}if (flag) {}
avoid mixing integer values and Boolean values .
7. The position of the expression assignment does not determine the calculation precision of the expression.
If the operands of an operator are of different types, the operand cannot be performed unless one of the operands is converted to the type of the other operand.
Ordinary arithmetic conversion long doubledouble float unsigned long intlong intunsigned intint
If an operand is ranked low, it is first converted to the type of another operand, and then the operation is performed.
For example, on a 16-bit machine
int a = 5000;int b = 100;long C = A * b;
Overflow will occur and the result of a * B needs to be converted to a long integer.
int a = 5000;int b = 100;long c = (long) a * b;
8. Writing the result depends on the evaluation order of the expression
The order of evaluation of an expression is affected by three factors:
Precedence of Operators
The associativity of Operators
Whether the operator controls the order of execution
The precedence of the operator determines which of the adjacent operators are executed first, and if the precedence is equal, then the associativity determines the order of execution,
However, these do not completely determine the order of evaluation of the expression, as long as the compiler does not violate the precedence and binding rules, it is free to determine the order of evaluation of complex expressions. Therefore, if the result of an expression depends on the order of evaluation, it is inherently non-portable and avoids use.
Chapter Fifth Warning Summary