# C language from introduction to earth (Introduction) (detailed explanation of operators p1)

Keywords: C Back-end

catalogue

1. Operator classification:

2. Arithmetic operators

3. Shift operator

3.1 shift left operator

3.2 shift right operator

4. Bitwise operator

5. Assignment operator

6. Monocular operator

6.1 introduction to monocular operators

6.2 sizeof and arrays

//I haven't finished yet!

## 1. Operator classification:

arithmetic operator

Shift operators

Bitwise operators

Assignment operator

unary operator

Relational operator

Logical operator

Conditional Operator

comma expression

Subscript references, function calls, and structure members

## 2. Arithmetic operators

+    -   *   /   % 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. As long as there are floating-point numbers, it is floating-point division.

3. The two operands of the% operator must be integers. Returns the remainder after division.

## 3. Shift operator

< < shift left operator

>>Shift right operator  Note: 1. The operand of shift operator can only be an integer.

2. All moving are binary complements (the computer stores complements).

### 3.1 shift left operator

Shift rule:

Discard on the left and supplement 0 on the right ### 3.2 shift right operator

Shift rule:

First, there are two kinds of shift right operations:

1. Logical shift

Fill the left with 0 and discard the right

2. Arithmetic shift

The left is filled with the sign bit of the original value, and the right is discarded   ps:

1. Our common compiler is right shift

2. For the shift operator, do not move the negative digit, which is not defined in the standard.

For example:

int num = 10;

num>>-1;//error

## 4. Bitwise operator

Bitwise operators are:

&/ / bitwise AND

|/ / bitwise OR

^/ / bitwise XOR

Note: their operands must be integers.   A perverse interview question:

You cannot create a temporary variable (the third variable) to exchange two numbers.

```#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
a = a^b;
b = a^b;
a = a^b;
printf("a = %d b = %d\n", a, b);
return 0; }``` practice:

Write code to realize: find the number of 1 in the binary of an integer stored in memory.

Reference code:

```//Method 1
#include <stdio.h>
int main()
{
int num  = 10;
int count=  0;//count
while(num)
{
if(num%2 == 1)
count++;
num = num/2;
}
printf("Number of 1 in binary = %d\n", count);
return 0;
}
//Is there a problem thinking about such an implementation?```
```//Method 2:
#include <stdio.h>
int main()
{
int num = -1;
int i = 0;
int count = 0;//count
for(i=0; i<32; i++)
{
if( num & (1 << i) )
count++;
}
printf("Number of 1 in binary = %d\n",count);
return 0; }
//Thinking about whether it can be more optimized, we must cycle 32 times.```
```//Method 3:
#include <stdio.h>
int main()
{
int num = -1;
int i = 0;
int count = 0;//count
while(num)
{
count++;
num = num&(num-1);
}
printf("Number of 1 in binary = %d\n",count);
return 0; }
//Is this a good way? The optimization effect is achieved, but it is difficult to think of.```

## 5. Assignment operator

The assignment operator is a great operator that allows you to get a value you were not satisfied with before. That is, you can reassign yourself. ```int weight = 120;//weight
weight = 89;//Assign value if you are not satisfied
double salary = 10000.0;
salary = 20000.0;//Assign values using the assignment operator.```
```Assignment operators can be used continuously, such as:
int a = 10;
int x = 0;
int y = 20; a = x = y+1;//continuous assignment
How does this code feel?
The same semantics, you see:
x = y+1; a = x;
Is this writing more clear, crisp and easy to debug.```

Compound assignor

+=

-=

*=

/=

%=

>>=

<<=

&=

|=

^=

These operators can be written as compound effects.

For example: ## 6. Monocular operator

### 6.1 introduction to monocular operators

!           Logical reverse operation

-           negative

+           positive

sizeof       The type length of the operand in bytes

~           Bitwise negation of a number

--           Front and rear--

++           Front and rear++

*           Indirect access operator (dereference operator)

(type)       Cast type   (there is no sign of absolute value, only function)  Demo code:

```#include <stdio.h>
int main()
{
int a = -10;
int *p = NULL;
printf("%d\n", !2);
printf("%d\n", !0);
a = -a;
p = &a;
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(int));
printf("%d\n", sizeof a);//Is that all right?
printf("%d\n", sizeof int);//Is that all right?
return 0; }``` about sizeof In fact, as we have seen before, we can find the space occupied by variables (types).

### 6.2 sizeof and arrays

```#include <stdio.h>
void test1(int arr[])
{
printf("%d\n", sizeof(arr));//(2)
}
void test2(char ch[])
{
printf("%d\n", sizeof(ch));//(4)
}
int main()
{
int arr = {0};
char ch = {0};
printf("%d\n", sizeof(arr));//(1)
printf("%d\n", sizeof(ch));//(3)
test1(arr);
test2(ch);
return 0; }
Q:
(1),(2)How much is output from the two places?
(3),(4)How much is output from the two places?```
```//++And -- operators
//Pre + + and--
#include <stdio.h>
int main()
{
int a = 10;
int x = ++a;
//First increment a, and then use a, that is, the value of the expression is the value after the increment of A. x is 11.
int y = --a;
//First, self subtract a, and then use a, that is, the value of the expression is the value after self subtraction of A. y is 10;
return 0; }
//Post + + and--
#include <stdio.h>
int main()
{
int a = 10;
int x = a++;
//First use a and then increase it, so that the value of x is 10; Then a becomes 11;
int y = a--;
//First use a and then subtract from it, so that the value of y is 11; Then a becomes 10;
return 0; }```

That's all for today!!!

If you think the author is a little helpful to you!

Just a little praise and attention!!! Of course, subscription is even more desirable!

Finally, thank you for watching!!!

Your support is the greatest driving force for the author to write!!!

See you next time!!!

Posted by fgm on Fri, 12 Nov 2021 13:40:54 -0800