# Simple example, take the operator

catalogue

1. Arithmetic operator

+: Sum

*: multiply

/ : Seeking business and saving surplus

% : Residual quotient

2. Shift operator

Shift left operator:

Positive number:

Negative number:

Shift right operator:

3. Bitwise operator

&   Two 1 out of 1, one 0 out of 0

|   If there is 1, take 1, and if there is no 1, take 0

^ The same is 0 and the difference is 1

4. Assignment operator

5. Monocular operator

! Logical reverse operation

sizeof   Type byte size

~Bitwise inversion

++And--

*   Indirect access operator

(type)   Cast type

6. Relational operators

7. Logical operators

8. Conditional operator

9. Comma expression

10. Subscript references, function calls, and structure members

1. [] subscript reference operator

2. () function call operator

3. Visit members of a structure

# 1. Arithmetic operator

+    -    *   /    %

## +: Sum

```#include <stdio.h>
int main()
{
int a = 5;
int b = 5;
int c = 0;
c = a + b;
printf("%d\n",c);
return 0;
}```

```#include <stdio.h>
int main()
{
int a = 8;
int b = 5;
int c = 0;
c = a - b;
printf("%d\n",c);
return 0;
}```

## *: multiply

```#include <stdio.h>
int main()
{
int a = 8;
int b = 5;
int c = 0;
c = a*b;
printf("%d\n",c);
return 0;
}```

## / : Seeking business and saving surplus

```#include <stdio.h>
int main()
{
int a = 8;
int b = 5;
int c = 0;
c = a/b;
printf("%d\n",c);
return 0;
}```

## % : Residual quotient

```#include <stdio.h>
int main()
{
int a = 8;
int b = 5;
int c = 0;
c = a%b;
printf("%d\n",c);
return 0;
}```

Note:  % The two operands of the operator must be integers.

# 2. Shift operator

< < shift left operator

>>Shift right operator

Note: the moving is the complement of digital binary, which is output with the original code         Original code - > inverse code symbol bit unchanged

## Shift left operator:

Discard on the left and supplement 0 on the right

### Positive number:

```#include <stdio.h>
int main()
{
int a = 3;
int b=a << 1;
printf("%d\n",b);
return 0;
}```

analysis:

### Negative number:

```#include <stdio.h>
int main()
{
int a = -3;
int b=a << 1;
printf("%d\n",b);
return 0;
}```

analysis:

## Shift right operator:

The compiler decides:

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

For positive numbers, whether logical shift or arithmetic shift, because the sign bit of positive numbers is 0, the two shifts have the same effect on positive numbers. Here we mainly analyze negative numbers.

My compiler has been modified   Arithmetic shift

Note: for shift operators, do not move negative digits, which is not defined in the standard.

```int num = 3;
num>>-1;      //I won't support it
```

# 3. Bitwise operator

&     Bitwise AND

|     Bitwise OR

^     Bitwise XOR

Note: their operands must be integers, which are binary complements of direct logarithms.

## &   Two 1 out of 1, one 0 out of 0

```#include <stdio.h>
int main()
{
int a = -3;
int b = 5;
int c = a&b;
printf("%d\n",c);
return 0;
}```

## |   If there is 1, take 1, and if there is no 1, take 0

```#include <stdio.h>
int main()
{
int a = -3;
int b = 5;
int c = a|b;
printf("%d\n",c);
return 0;
}```

## ^ The same is 0 and the difference is 1

```#include <stdio.h>
int main()
{
int a = -3;
int b = 5;
int c = a ^ b;
printf("%d\n", c);
return 0;
}```

# 4. Assignment operator

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

These are all compound operators

Chestnuts:

```	int a = -3;
int b = 5;
a = a + b;     //Here a the result is 2```

Can be changed to

```	int a = -3;
int b = 5;
a += b;```

Other operators take this as an example, and so on.

# 5. Monocular operator

## ! Logical reverse operation

```int i=0;
scanf("%d",&i);
if(i!=3)              //Here i is not equal to 3
{
printf("hr\n");
}

printf("%d\n", !2);   //If the output 0 is not true, it means false 0
printf("%d\n", !0);   //Output 1 is true if it is not false```

+     -  Represents the positive and negative of a number, which we omit

```#include <stdio.h>
int main()
{
int b = 5;
printf("%p\n",&b);        //Take the address of b
return 0;
}```

## sizeof   Type byte size

```#include <stdio.h>
int main()
{
int a = -10;
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(int));
printf("%d\n", sizeof a);       //a feasible without brackets
//printf("%d\n",sizeof int);     // It is wrong to write a type without parentheses
return 0;
}```

sizeof and array:

```include <stdio.h>
void test1(int arr[])
{
printf("%d\n", sizeof(arr));//  The first address of the array is passed in, with a size of 4 / 8
}
void test2(char ch[])
{
printf("%d\n", sizeof(ch));//  The incoming address is still the first address, which is essentially a pointer. The size of the pointer is 4 / 8
}
int main()
{
int arr[10] = {0};
char ch[10] = {0};
printf("%d\n", sizeof(arr));//  The output is 10 integers with a byte size of 40
printf("%d\n", sizeof(ch));//  The output is a byte size of 10 characters
test1(arr);
test2(ch);
return 0;
}
```

## ~Bitwise inversion

Bitwise inversion is an operation on the binary complement of a number, 0 - > 1,1 - > 0

Note: the whole binary value sequence, including sign bits, shall also be inverted

## ++And--

Front:

```#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:

```#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;
}```

## *   Indirect access operator

```#include <stdio.h>
int main()
{
int a = -10;
int* p = &a;
*p = 5;                //You can directly access the content in the a address and operate it
printf("%d\n",a);
return 0;
}```

## (type)   Cast type

Chestnuts:

```#include <stdio.h>
int main()
{
int a = 50;
int b = 14;
double ave = 0;
ave = a / b;              //Not cast
printf("%lf\n",ave);
return 0;
}```

`	ave = (double)a / b;`

# 6. Relational operators

>      >=     <      <=     != Used to test "inequality"           ==   Used to test equality

Pay attention to the small details "= =" and "=" to see clearly. It's a pit

# 7. Logical operators

&&     Logic and           If the left is false, the execution will not continue

||         Logical or           If the left is true, the execution will not continue

```#include <stdio.h>
int main()
{
int a = 5;
if (a > 6 && a < 8)     //  &&The left side is false, do not continue
{
printf("AA\n");
}
else                     //Execute the statements in else directly
printf("BB\n");
return 0;
}```

```#include <stdio.h>
int main()
{
int a = 5;
if (a > 1 || a < 3)        //  ||If the left side is true, directly stop the execution to the right, ignoring the condition that the right side is false
{
printf("AA\n");
}
else
printf("BB\n");
return 0;
}```

# 8. Conditional operator

Also known as the trinocular operator

exp1 ? exp2 : exp3

Expression 1 is true and expression 2 is evaluated

Expression 1 is false, expression 3 is evaluated

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

# 9. Comma expression

exp1, exp2, exp3, ...expN

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

```#include <stdio.h>
int main()
{
int a = 1;
int b = 2;
int c = (a > b, a = b + 10, a, b = a + 1);
printf("%d\n", c);
return 0;
}```

From left to right, regardless of true or false, the result is the result of the last expression

# 10. Subscript references, function calls, and structure members

## 1. [] subscript reference operator

Operand: an array name + an index value

```#include <stdio.h>
int main()
{
int arr[10] = { 1, 2, 3, 4, 5, 6, 7 };
printf("%d\n", arr[2]);
return 0;
}```

Note: the subscript starts from 0, so the subscript of 3 in the arr array is 2

## 2. () function call operator

```#include <stdio.h>
void test()
{
printf("hehe\n");
}
int main()
{
test();            //() is a function call operator.
return 0;
}```

## 3. Visit members of a structure

.             Structure. Member name

->           Structure pointer - > member name

```#include <stdio.h>

struct book
{
int price;
char name[20];
};

int main()
{
struct book b1 = {30,"c language"};
b1.price = 50;
printf("%d\n",b1.price);
return 0;
}```

Take the address and use the pointer:

```	struct book*p = &b1;
printf("%s\n",p->name);```

Tags: C Back-end

Posted on Sun, 28 Nov 2021 09:54:14 -0500 by wwwapu