Preliminary C language - 4. Detailed explanation of operators

        The previous two games have been written, so let's continue our study of C language related knowledge. Next, this article will introduce some operators in C language. Although these operators are "inconspicuous", they are of great use. , learning them well is what we should do, so let’s not talk nonsense, let’s start today’s study.


content

operator classification

1. Arithmetic operators

2. Shift operator

        2.1 Left shift operator

         2.2 Right shift operator

3. Bitwise operators

4. Assignment operator

        4.1 Compound assignment operators

5. Unary operator

6. Relational Operators

7. Logical Operators

8. Conditional operator

9. Comma Expression

10. Subscript references, function calls, and structure members

11. Expression evaluation

        11.1 Implicit type conversions

        11.2 Arithmetic conversions

        11.3 Attributes of Operators

 Conclusion:


operator classification

Operators include:

  1. arithmetic operators
  2. shift operator
  3. bitwise operator
  4. assignment operator
  5. unary operator
  6. relational operator
  7. logical operator
  8. Conditional operator
  9. comma expression
  10. Subscript references, function calls, and structure members

1. Arithmetic operators

+    -    *    /    %

Arithmetic operators include the above: addition, subtraction, multiplication, division and remainder.

        Among these operators, there is nothing to say about addition, subtraction and multiplication. It is the same as normal arithmetic, but the division here should be paid attention to. Is it still the original division?

According to normal arithmetic, the result of dividing 2 by 3 should be a loop of 0.6 6, but is this really the case?

The final result is 0, someone will say, this result should be a floating point number, then replace %d with %f and let's take a look at the result.

 

Unfortunately, this is not the desired 0.666666, so why? Because the two variables a and b are integers, the integer operation is performed here. What if you just want to print a floating point number? Here you only need to cast any number on both sides of the operator to a floating-point number. In short, there must be a floating-point number on the left and right.

There is another way of writing:

Although the printing here is not what we want, it is because the precision of float is not so high, but it does not affect, we only need to know how to use this operator to avoid some errors.

Let's move on to ' / ':

The result should be 4.5, but it is 4 in the end. The integer calculation here will not be automatically rounded. Combined with the remainder, it can be seen that ' / ' only calculates the part of the quotient, and obtains an integer.

 So to summarize:

1. In addition to the ' % ' operator, several other operators can act on integers and floating-point numbers.
2. For the ' / ' operator if both operands are integers, perform integer division. And as long as there are floating-point numbers, floating-point division is performed.
3. Both operands of the ' % ' operator must be integers. Returns the remainder after division.


2. Shift operator

<< Left shift operator
>> Right shift operator
Note: The operand of the shift operator can only be an integer.

Next, these operators will involve binary related content.

        2.1 Left shift operator

The shift operator does not change the original value of a.

Why is the result of b here 20?

         2.2 Right shift operator

Shift rules:
First, there are two types of right-shift operations:
1. Logical shift
: fill with 0 on the left, discard on the right;
2. Arithmetic shift
: fill with the sign bit of the original value on the left, discard on the right

 

 

Note: For shift operators, do not shift negative bits, this is undefined by the standard.
Such as:

    int num = 10;
    num >> -1;


3. Bitwise operators

Bit operators are divided into:

// by (binary) bitwise AND

|    // Or by (binary) bit

^    // XOR by (binary) bits

Note: their operands must be integers.

& bitwise AND

| Bitwise AND

 ^ bitwise XOR

It is also possible to swap two variables without intermediate variables using operators:

In this way, a and b are exchanged. 


4. Assignment operator

Assignment, as the name suggests, is to give a value

int a = 10;
a = 2;

The "=" here is not equal to arithmetic, it means assignment.

        4.1 Compound assignment operators

+=
-=
*=
/=
%=
>>=
<<=
&=
|=
^

For an example: +=

x = x + 10; is equivalent to x += 10;


5. Unary operator

! logical inverse operator

- negative value

+ positive value

& get address

sizeof        operand type length in bytes

~ Reverse the binary bitwise of a number

-- Front, Rear--

++ pre, post ++

* Indirect access operator (dereference operator)

(type name) cast

Let’s introduce it here, and we can explain it later when we use it, so that the effect may be better.


6. Relational Operators

>

>=

<

<=

!= for "not equal"

== for "equal"

Here is to pay attention to the usage of "==" and "=".


7. Logical Operators

&& logical AND

|| logical or

This is to distinguish between these two and the difference between bit operators, here are two.

int main()
{
    int i = 0,a=0,b=2,c =3,d=4;
    i = a++ && ++b && d++;
    //i = a++||++b||d++;
    printf("a = %d\n b = %d\n c = %d\nd = %d\n", a, b, c, d);
    return 0;
}

Let's look at the example of logical AND. What is the print result?

 Some people may think that the result is wrong, but this is the correct result, so why is this?

The use of && logical and is from left to right, and each expression must be true to calculate to the end, otherwise it will be like a here, post ++ is used first plus 1, so at the beginning a = 0, the expression is false, so the expression after a will not be evaluated.

|| The use rule of logical OR is that as long as one expression is true in the expression, it will not be calculated further. At the beginning, a is false, and the next b is true, so the following d will not be calculated. 


8. Conditional operator

Also called ternary operator

exp1 ? exp2 : exp3

a > b ? a : b;

Convert it:

if (a > b)

        (execute a if a > b)

else

        (otherwise do b)


9. Comma Expression

(exp1, exp2, exp3, …expN)

Comma expressions, executed in order from left to right. The result of the entire expression is the result of the last expression.

int a = 1;
int b = 2;
int c = (a>b, a=b+10, a, b=a+1);

The final result c is 13


10. Subscript references, function calls, and structure members

1. [ ] Subscript reference operator
Operand: an array name + an index value

2. ( ) The function call operator
accepts one or more operands: the first operand is the function name, and the remaining operands are the parameters passed to the function

3. Access a member of a structure

.struct.membername

-> Structure pointer -> member name

11. Expression evaluation

The order in which expressions are evaluated is determined in part by the precedence and associativity of operators.
Likewise, the operands of some expressions may need to be converted to other types during evaluation.

        11.1 Implicit type conversions

C integer arithmetic operations are always performed with at least the precision of the default integer type.
To achieve this precision, character and short operands in expressions are converted to ordinary integers before use, a conversion called integer promotion.

Example:

char a, b, c;                                                                                                                                a = b + c;

The values ​​of b and c are promoted to ordinary integers, and then the addition operation is performed. After the addition operation is completed, the result is truncated and stored in a.

//The integer promotion of negative numbers
char c1 = -1;
there are only 8 bits in the binary bit (complement) of the variable c1:
1111111
Because char is a signed char
, when the integer is promoted, the high-order supplementary sign bit is 1
The result after boosting is: Complement: 11111111111111111111111111111111

//Positive integer promotion
char c2 = 1;
there are only 8 bits in the binary bit (complement) of the variable c2:
00000001
Because char is a signed char
, when the integer is promoted, the high-order supplementary sign bit is 0
The result after boosting is:
00000000000000000000000000000001

//Unsigned integer promotion, high-order fill with 0

        11.2 Arithmetic conversions

        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 following hierarchy is called ordinary arithmetic transformations .

high        -level long double
                   double
                   float
                   unsigned long int
                   long int
                   unsigned int                                                                                                         
low-level        int

       If the type of an operand is lower in the above list, the operation is performed first by converting to the type of the other operand.
Warning:
But arithmetic conversions have to be reasonable, otherwise there will be some potential problems, and there may be a loss of precision.

        11.3 Attributes of Operators

There are three factors that influence the evaluation of complex expressions.
1. The priority of the operator
2. The associativity of the operator
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.


 Conclusion:

        After the year is over, let's continue to study hard.

Guess you like

Origin blog.csdn.net/m0_64607843/article/details/122665466