Chapter Fifth Warning Summary

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.