C and pointer (pointers on C) -- Chapter 5: operators and expressions
Source: Internet
Author: User
Chapter 5 Operators and expressions
This chapter may be difficult to read if you have never played single-chip microcomputer or assembly, especially in the application of Shift Operators and bit operators. Pay more attention to the understanding of the left and right values.
Summary:
Arithmetic Operators, value assignment operators, Relational operators, and conditional operators.
Short-circuited evalution: such as | this binary operator. If its left operand is true, the right operand is no longer evaluated. The consequence is that if the array subscript of the right operand is invalid, no error is reported.
C does not have an explicit boolean type. A boolean value is expressed by an integer expression. Mixing boolean values and any integer values in an expression may produce errors. To avoid these errors, each variable either represents a Boolean or an integer, and cannot contain two values. Do not perform Boolean tests on integer variables, and vice versa.
C has no Boolean value and is replaced by an integer. 0 is false, and non-0 is true.
So some people often look like this:
# Define false 0
# Define true 1
..
If (flag = true )...
If (FLAG )...
However, this is of little significance. If the flag is set to any integer value, the second pair of statements is not equivalent. It makes sense only when the flag is true or false.
Therefore, if you want flag to represent a Boolean value, you should always set it to 0 or 1.
The left and right values (L-value and R-value) provide a memory location but are not copies. The right value indicates a value, which can be a copy. Therefore, the left value can also be the right value, but the right value cannot be the left value.
For example:
A ++ = B: // wrong
B = A ++; // right
A ++ does not have a memory location. Some people may say that a ++ will inevitably have a memory location, but the result of a ++ is a copy of, its location is not certain. So this is not the left value.
Values of different types must be converted before operation.
If an expression is too dependent on the order of evaluation, try to avoid it. Because different compilers generate different order of evaluation, essentially expressions cannot be transplanted.
Warning:
1. It cannot be transplanted when there is a signed value worth the right shift value operation.
This is a test for the compiler. The unsigned right shift is the same as the left shift. It can be transplanted. However, if there is a right shift operation with a symbol bit, if the symbol bit is 1, all the values shifted to the right are 1. If the sign bit is 0, all values shifted to the right are 0.
2. The number of digits of the shift operation is a negative value.
3. variable lengths vary in consecutive values.
4. Misuse = instead of = for comparison.
It is too easy to make a mistake, so it is best to look at it as long as there is a logical expression.
5. Misuse | substitution |, misuse & substitution &&
Bitwise operator, error in misuse of 80%!
6. Compare different non-zero values used to represent boolean values.
For details, see.
7. the position where the expression value is assigned does not determine the accuracy of expression calculation.
8. Writing results depends on the expressions in the order of evaluation.
See!
Programming tips:
1. Try to replace the if statement with the following conditional operators to simplify the expression.
This brings obvious advantages. When an expression is too long ,? The combination of:; greatly shorten the length.
2. Do not mix integer and boolean values.
Bitwise operators .... Well, there will be time to focus on its applications.
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