Programming Tips: Operators and Expressions

Operators and Expressions in C Language 

Operator classification: arithmetic operators, shift operators, bit operators, assignment operators, unary operators, relational operators, logical operators, conditional operators,
Comma expressions, subscript references, function calls, and structure members
Arithmetic operator:  +-* /%
      1. In addition to the % operator, several other operators can act on integers and floating-point numbers.
      2. The more than / operator performs integer division if both operands are integers. And as long as there are floating-point numbers, floating-point division is performed.
      3. The two operands of the % operator must be integers, and the remainder after the integer is returned.
Shift operators: << left shift operator >> right shift operator  
      1. Left shift operator shift rule: discard on the left, fill with zeros on the right
      2. Right shift operator shift rules: ① Logical shift: fill with zeros on the left, discard on the right
                                           ② Arithmetic shift: the left side is filled with the original symbol of the value, and the right side is discarded
     (Note: For shift operators, do not shift negative bits, this is undefined by the standard.)
Bitwise operators: & (bitwise AND) | (bitwise OR) ^ (bitwise exclusive OR)
     (Note: their operands must be integers.)
Assignment operators: = += -= *= /= %= >>= <<= &= |=
Unary operator: ! (logical inverse operation) - (negative value) + (positive value) & (address) sizeof (the type length of the operand, in bytes)
                   ~ (bit-wise inversion of a number's binary number) -- (prepend, append --) ++ (prepend, append ++) * (indirect access operator, dereference operator)
                   (type) cast
Relational operators: > >= < <= != (for testing "not equal") == (for testing "equal")

Logical operators: && logical and || logical or
                The difference between logical AND and bitwise AND: 1&2---------->0 1&&2------------>1
                Difference between logical or and bitwise or: 1 | 2------------>3 1 | | 2------------>1
Conditional operator: exp1 ? exp2 : exp3 (use conditional expressions to find the larger of two numbers)
Comma expressions: exp1, exp2, exp3, …expN
                Comma expressions are multiple expressions separated by commas.
                Comma expressions, executed in order from left to right. The result of the entire expression is the result of the last expression.
Subscript reference, structure member of function call
1. [ ] Operand: an array name + an index value
2. ( ) function call operator
        Accepts one or more operands: the first operand is the function name, and the remaining operands are the arguments passed to the function.
3. Access a member of a structure : .structure.membername
                                     -> Structure pointer -> member name
Expression evaluation : The order in which expressions are evaluated is determined in part by the precedence and associativity of operator 1. Likewise, the operands of some expressions may need to be converted to other types during evaluation.
Implicit type conversions: C integer arithmetic operations are always performed with at least the precision of the default integer type. In order to obtain this precision, the character and short operands of expressions are converted to ordinary integers before use. This conversion is called [integer promotion].
Arithmetic conversion: If the various operators 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 following hierarchy is called ordinary arithmetic transformations.
long double     double     float     unsigned int     int
If the type of an operand ranks lower in the above list, the operation is performed after conversion to the type of the other operand.
(Note: But the arithmetic conversion has to be reasonable, otherwise there will be some potential problems.)
operator properties
There are three factors that influence the evaluation of complex expressions.
1. Operator precedence
2. Operator associativity
3. Whether to control the evaluation order
Which of two adjacent operators is executed first? depending on their priority. If both have the same priority, it depends on their associativity.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325362178&siteId=291194637