Operators (operators) for beginners in C language

Table of contents

Arithmetic operators: + - * / %

Shift operators: >> <<

Bitwise operators: & | ^   

Assignment operators: = += -= /= %= >>= <<= |= &=  

 Unary operator: ! - + & sizeof ~ -- ++ * . (type)

Relational operators: > >= < <= != ==

 Logical operators: && ||

 Conditional operator: exp1?exp2:exp3

Comma expression: exp1,exp2,exp3...expn

Subscript reference, function call, structure member operator: [ ] () . ->


  First of all, speaking of operators, there are a series of operators such as arithmetic operators, assignment operators, relational operators, unary operators, etc. Next, I will give you a brief understanding of various operators and give your own What you need to understand and what you need to pay attention to, then, let’s start learning!


Arithmetic operators: + - * / %

Arithmetic operators are the above five types, most of which have been seen by everyone, but especially pay attention to multiplication (*) and division (/), not ✖ and ➗ in our mathematics, just pay attention to this point. Then there is the remainder (%). This symbol means to take the remainder of the result. For example, 7/3=2 with a remainder of 1, then 7%3 is equal to 1. Do you understand? This is the content of the arithmetic operator.


Shift operators: >> <<

Shift operators include >> and <<, where >> is a right shift operator, and << is a left shift operator. Note that both left shift and right shift here refer to the movement of binary bits, because all Information is stored in binary form.

Speaking of binary, it refers to a string of numbers consisting of 32 0s or 1s, for example, a=3: 00000000...00000011, the operation process is: 1*2^1+1*2^0=3(^ means how many power)

a<<1, it becomes: 00000000...00000110, the result is: 1*2^2+1*2^1=6

a>>1, it becomes: 00000000...00000001, the result is: 1*2^0=1

This is the shift operator.


Bitwise operators: & | ^   

Among them, & is bitwise AND, | is bitwise OR, ^ is bitwise XOR, these three are also binary operators, and then explain the specific usage

&: If both x and y are 1, get 1; if either x or y is 0, or both are 0, get 0, abbreviated as: both are 1, then 1

|: If x or y is 1, or both are 1, then 1 is obtained; if both x and y are 0, then 0 is obtained, abbreviated as: 1 is 1

^: If the value of x or y is different, it will get 1; if the two values ​​are the same, it will get 0, abbreviated as: the same is 0, the different bit is 1

Here is an example so you can clearly see the difference.

 int a=6 0···00110
int b=11 0···01011
a&b 0···00010
a|b 0···01111
a^b 0···01101

Assignment operators: = += -= /= %= >>= <<= |= &=  

You can see that there are many, many assignment operators, and the above are just some of them. You can notice that we all know =, but symbols such as += and -= have never been seen before, and they are actually very simple.

a+=2 means a=a+2

b-=3 means b=b-3

And so on...

In particular, note that = here is an assignment operator , which will be confused with == in the following relational operators , which will be explained in detail in the following relational operators.


 Unary operator: ! - + & sizeof ~ -- ++ * . (type)

Speaking of unary operators, as the name implies, they are operators with only one operand, and corresponding to them are binocular operators, that is, those with two operands. For example, a++, ++ is a unary operator; a+ b, + is the binocular operator, and the function of each operator will be described in detail next, so learn it carefully.

Here is a concept first, in C language 0 means false, non-zero means true

! It is a logical inverse operation. For example, int a = 0 (0 means false), then !0 means true, and add ! , true becomes false, and false becomes true .

-, +: Attention, here -, + means negative value, positive value , not the meaning of addition and subtraction, such as +2, -3, etc.

&, * .: address operator, indirect access operator (decomposition operator) This symbol will be discussed in detail in the pointer part later, so stay tuned! !

sizeof: is an operator that calculates the type length of the operand (the unit is byte), for example:

The printed results are as follows

 As for why the length of the int type is 4, please pay attention to the data type part of the first introduction to C language I posted earlier.

~: Reverse the binary of a number bit by bit , change the original 0 to 1, and 1 to 0, for example, the binary of 1: 0...00000001, then after ~, it will become: 1...11111110, it's ok

--, ++: ++ and -- can roughly understand the bits +1, -1, and the ++ and -- here are divided into two types: front and back. The front is used first, and the latter is ++ or -- , the post-position is ++ or --, and then used . What does it mean specifically, see the explanation below.

 Please pay attention, the value of a given above is 1, and then print a++ is the front ++, then according to our formula, the front ++ is used first, and then ++, because it is used first, the one printed first is a Value 1, followed by ++, the next print is the value 2 after ++.

Then let's take a look at the situation of ++a, how to use ++ first and then use it

 You can see through observation that the first printing, first ++, a+1 becomes 2, and then printing, so the printed value is 2, and the second printing is naturally the value 2 after ++.

--Similar to the situation of ++, you can think about it and practice it on the computer. Here I want to emphasize again that you must practice more on the computer xdm. Only by using the computer can you master the knowledge better! !

(type): Mandatory type conversion , such as the example in the figure below

Observe the above picture, a is of float type at first, and then define an int type b, and convert a to int, 1.25 will become an integer 1, so the printed b is 1.

The above is the whole content of the unary operator. 


Relational operators: > >= < <= != ==

The >,<,>=,<= here are known to everyone, so I won’t explain too much. The latter != means not equal, and == means equal

Let’s talk about the difference between = and == here. You can clearly see that = is an assignment operator and == is a relational operator through classification. If we want to indicate who is equal to whom in the code , we should use it It is a relational operator ==. If you want to express the assignment of a number to another number , you will naturally use =. Can you tell it clearly?


 Logical operators: && ||

&&: Logical AND
1. When the left side of && is false, the judgment on the right side will no longer be performed, and the result is false;
2. When the left side of && is true, the right side is judged, and the right side is false, and the result is false; the right side is true, the result is true ;

 As shown in the figure above, it is about logic and. You can observe the results first, think about why, and then read the explanation below to see if you have the same idea.

First of all, in a = ++a && b++, ++a on the left side of && is true, so the expressions on the left and right sides must be executed. After ++a, a is 2, and after b++, b is 3. Both left and right is true, so the value of a is finally 1, so the first result printed is 1 3;

Then look at the second one, in c = --a && ++b, --a on the left side of && is 0, and the left side is false, so there is no need to execute the right side, so a becomes 0, and b remains 3 , since the left side of && is false, the result is false, so c is 0, so the printed result is 0 3 0.

Here is an important reminder that if the left side of the && operator is false, the right side does not need to be executed . This is an error-prone point , keep this in mind! !

||: Logical or
1. When the left side of || is true, the judgment on the right side will no longer be performed, and the result is true.
2. When the left side of || is false, the right side is judged. If the right side is false, the result is false; if the right side is true, the result is true

 The above picture is related to the operation of logic or, as usual, you should first calculate to see if it is the same as the answer, and then read the explanation below.

First look at a = ++a || b++, the left side of || is ++a is equal to 2, if it is true, then the right judgment b++ will not be made, and the result is true, that is, a is 1 in the end, while b remains unchanged, it is still 2, so the print out is 1 2.

Then look at c = --a || ++b, || on the left is --a is equal to 0, which is false, so both sides make judgments, the left is 0, and the right ++b is 3, so the result is true, that is, c is true, so c is equal to 1, so the printed result is 0 3 1.

For the logical or || operator, it should be noted that if the left side is true, then the right side does not need to be judged . This is also a point that is easy to ignore . Be sure to pay attention to it!

This is the specific explanation of logical and and logical or operators. I think everyone has a good understanding of it (◦˙▽˙◦)


 Conditional operator: exp1?exp2:exp3

exp1?exp2:exp3 means that if exp1 is established, exp2 is calculated, and the result of the entire expression is the result of exp2

If exp1 is not established, calculate exp3, and the result of the entire expression is the result of exp3

To give a simple example:
 

int a = 5;

int b = 2;

int c = a>b?a:b;

The above means that a>b is established, so the value of c is a, so c is equal to 5.

Of course, have you noticed that the conditional operator has three operands, so it belongs to the ternary operator ?


Comma expression: exp1,exp2,exp3...expn

The comma expression means that several expressions are connected by commas, and the result of the whole expression is the result of the last expression , but the previous expressions must be calculated again, because the previous expressions may affect the subsequent expressions the result

For example, the above screenshot

 In the figure, there is a conditional expression after d, first calculate a+=5, calculate a=15, then calculate b+=a, calculate b=35, finally calculate c=a+b, calculate c=15+35=50, so finally The result of d is 50.

Observe the above formula, the result of the last expression depends on the result of the previous expression, so even if the result of the whole expression is the result of the last expression, it must be calculated in order.


Subscript reference, function call, structure member operator: [ ] () . ->

The subscript reference operator: [ ] is often used for arrays, for example:

int arr[5]={ 0 };
arr[4] = 5;

The array name and subscript of its operand !

Function call operator: ()

int my_min(int x,int y)
{
    return x>y?y:x;
}

int main()
{
    int min =my_min(5,8);
    printf("%d",min);
    return 0;
}

The () after my_min in the figure is a typical function call operator

In the example it has three operands: function name , 5 , 8

Struct member operator: . ->

Structural member operators will appear in large numbers in the structures we will learn later, so we will explain them in the structure learning, so stay tuned ٩(๑•̀ω•́๑)۶


 integer promotion

Integer arithmetic in C is always performed with the precision of the default integer type . (that is, the type isa type of number smaller than int )

To achieve this precision, character and short integer operands in expressions are converted to normal integer types before being used . This conversion is called integer promotion.

In integer promotion, the sign bits of characters and short integers will be filled up to 32 bits. For signed numbers, the highest sign bit will be filled (0 means positive, 1 means negative); for unsigned numbers In other words, just add 0

Here are a few examples to illustrate integer promotion:

eg1:

Let's take a look at this example and calculate what the answer is.

Maybe some students will blurt out 132 after seeing this question, what's the point?

Then congratulations, you have successfully jumped into the pit of integer improvement! So how is the answer calculated? Specifically, it should be calculated like this:

First of all, observe that both a and b are of char type, which is smaller than int type, so integer promotion is required when performing arithmetic operations!

First look at a: 5, the binary sequence of 5 in memory is:

5 is the same as the original inverse complement of positive numbers, both are: 00000000 00000000 00000000 00000101

But it is a number of char type, so there is only one byte, that is, 8 bits , and only 00000101 can be left.

Then look at b:127, the binary sequence in memory is:

127 is also the same as the original inverse complement of positive numbers, both are: 00000000 00000000 00000000 01111111

b is also a number of char type, so like a, only 8 bits can be reserved , that is, 01111111

Then look at char c = a + b; as mentioned above, the characters and short integer operands in the expression are converted to ordinary integers before use, so a and b will be promoted to ordinary integers Type, 32 bits need to be filled, so how to fill it, as mentioned above, because a and b are both signed numbers, so the sign bit is complemented, and the sign bit can be observed to be 0, so add 0 to complete 32 bits ,as follows:

a:00000000 00000000 00000000 00000101

b:00000000 00000000 00000000 01111111

Carry out the operation of c=a+b, after the addition is: 00000000 00000000 00000000 10000100

Because c is also a char type, so after 8 bits are reserved: 10000100

Afterwards, the %d type (signed integer) is printed , so integer promotion is also required, c is a signed number, and the sign bit is promoted by 1, namely:

11111111 11111111 11111111 10000100

Because the complement code is stored in the memory, and the sign bit of the complement code of c is 1, it can be known that c is a negative number, so the original code is equal to the complement code -1 and then reversed,

Calculate the original code of c: 10000000 00000000 00000000 01111100

It is calculated that c is -124, so whether it is correct or not, you can see from the calculation results below:

 This is the first example of integer promotion, have you mastered it?

Here is another example to prove that there is integer promotion in C language, as shown below:

I learned before that the char type occupies 1 byte, the short type occupies 2 bytes, and the int type occupies 4 bytes. Then observe the above case, a is char type, b is short type, so of course a occupies 1 word Section, b occupies 2 bytes, but why +a and -b become 4 bytes? In fact, integer promotion has also occurred here, because there is an arithmetic operator +-, and the integer is promoted to int type , so Of course it takes up 4 bytes!

Everyone understands the knowledge of plastic improvement! 


arithmetic conversion

The integer promotion mentioned above is performed on operands smaller than int type, then the arithmetic conversion mentioned next is operands larger than int type .

Arithmetic conversion means that if the operands of an operator are of different types, the operation cannot be performed unless one of the operands is converted to another number type .

The usual arithmetic type conversions are as follows:

long double

double

float

unsigned long int

long int

unsigned int

int

Just remember that when operands of different types are encountered during the operation, the types can be converted from bottom to top .

The methods of arithmetic conversion and integer promotion are similar, please refer to the knowledge of integer promotion to understand it.\(^o^)/~


 

It's finally over. The above is the whole content of the operators of C language for the first time. Thank you for watching. See you in the next blog.

ヾ( ̄▽ ̄)Bye~Bye~

 (◦˙▽˙◦)    (◦˙▽˙◦)

Guess you like

Origin blog.csdn.net/m0_64411530/article/details/122180646