# Detailed explanation of C language operators

Keywords: Python Java C C++

Pay attention to one button three times (don't be sure next time)

Hello, bald men. Let's study operators together today!

# Key points of this chapter

1. Introduction to various operators.
2. Expression evaluation

# 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

## Arithmetic operation

+ - * / %

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.

## Shift operators

Shift left operator<<
Shift right operator > >

Shift left operator shift rule:

Discard on the left and supplement 0 on the right What does it mean that there is no change in its own value?
For example:

```int a=10;
int b=a+10;
```

Do you think a has become 20? Obviously not. A is still 10
If you want to change num, you should write num = num < < 1;

Shift right operator shift rule:
First, there are two kinds of shift right operations:

1. The logical shift is filled with 0 on the left and discarded on the right
2. The left side of the arithmetic shift is filled with the sign bit of the original value, and the right side is discarded be careful:
For shift operators, do not move negative digits, which is not defined by the standard.
For example:

```int num = 10;
num>>-1;//error
```

## Bitwise operators

```Bitwise operators are:
& //Bitwise AND
| //Bitwise OR
^ //Bitwise XOR
Note: their operands must be integers.
```

Practice:
A disgusting 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^b^b=a
a = a^b;//a^b^a=b(a doesn't change, b becomes a)
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.

```#include <stdio.h>
int main()
{
int num = -1;
int i = 0;
int count = 0;//count
for(i=0; i<32; i++)
{
if( ((num>>i)&1) == 1 )
count++;
}
printf("Number of 1 in binary = %d\n",count);
return 0;
}
```
```#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;
}
```

Two methods, the second is not easy to think of

## 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.

## Compound assignor

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

These operators can be written as compound effects. For example:

```int x = 10;
x = x+10;
x += 10;//Compound Assignment
//The same is true for other operators. This is more concise
```

## unary operator

What are the 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
```
```printf("%d\n", sizeof(a));					Feasible!
printf("%d\n", sizeof(int));				Feasible!
printf("%d\n", sizeof a);//Is that all right? Feasible!
printf("%d\n", sizeof int);//Is that all right? no way!
```

### 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?
Tips: sizeof()Is it an array name or an address
```

## Relational operator

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

These relational operators are relatively simple and have nothing to say, but we should pay attention to some pitfalls in the use of operators.
Warning: in the process of programming = = and = are written incorrectly, resulting in errors.

## Logical operator

What are the logical operators

```&& Logic and
|| Logical or
```

Distinguishing logic and bitwise and distinguishing logic or and bitwise OR

```1&2----->0
1&&2---->1
1|2----->3
1||2---->1
```

360 written questions

```#include <stdio.h>
int main()
{
int i = 0, a = 0, b = 2, c = 3, d = 4;
i = a++ && ++b && d++;
//i = a++||++b||d++;
printf("a = %d\nb = %d\nc = %d\nd = %d\n", a, b, c, d);
return 0;
}
```

Resolution:
&&From left to right, when one in front is false, then the operation will not be carried out later, because if one in logic and is false, the whole is false, and all truth is true;
||From left to right, when one in front is true, no operation will be performed later, because if one in logic or is true, it is all true.

## Conditional Operator

```exp1 ? exp2 : exp3
```

？ Which of the left and right is true, then: who is behind

## comma expression

```exp1, exp2, exp3, ...expN
```

Comma expressions are evaluated from left to right, and the whole expression is the result of the rightmost expression

## Subscript references, function calls, and structure members

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

```int arr;//Create array
arr = 10;//Practical subscript reference operator.
[ ]The two operands of are arr And 9.
```

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

```#include <stdio.h>
void test1()
{
printf("hehe\n");
}
void test2(const char *str)
{
printf("%s\n", str);
}
int main()
{
test1(); //Utility () as a function call operator.
test2("hello bit.");//Utility () as a function call operator.
return 0;
}
```

3. Visit members of a structure

. structure. Member name
->Structure pointer - > member name

```#include <stdio.h>
struct Stu
{
char name;
int age;
char sex;
double score;
}；
void set_age1(struct Stu stu)
{
stu.age = 18;
}
void set_age2(struct Stu* pStu)
{
pStu->age = 18;//Structure member access
}
int main()
{
struct Stu stu;
struct Stu* pStu = &stu;//Structure member access
stu.age = 20;//Structure member access
set_age1(stu);
pStu->age = 20;//Structure member access
set_age2(pStu);
return 0;
}
```

# Expression evaluation

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

## Implicit type conversion

The integer arithmetic operation of C is always performed at least with the precision of the default integer type.
To achieve this precision, the characters and short operands in the expression are converted to normal integers before use. This conversion is called integer promotion.
Significance of integer lifting:

The integer operation of the expression shall be executed in the corresponding operation device of the CPU. The byte length of the operand of the integer operator (ALU) in the CPU is generally the byte length of int and the length of the general register of the CPU.
Therefore, even if the two char types are added, they actually have to be converted to the standard length of integer operands in the CPU when executed by the CPU. General purpose CPU (general purpose CPU) is difficult to directly realize the direct addition of two 8-bit bytes (although there may be such byte addition instructions in machine instructions). Therefore, all integer values whose length may be less than int in the expression must be converted to int or unsigned int before they can be sent to the CPU for operation.

```//Example 1
char a,b,c;
...
a = b + c;
```

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

How to carry out overall improvement?

Integer promotion is promoted according to the sign bit of the data type of the variable

```//Shaping and lifting of negative numbers
char c1 = -1;
variable c1 Binary bit of(Complement)There are only 8 bits in:
1111111
because char Is signed char
Therefore, when shaping and lifting, the high supplementary symbol bit is 1
The result of the promotion is:
11111111111111111111111111111111
//Positive integer lifting
char c2 = 1;
variable c2 Binary bit of(Complement)There are only 8 bits in:
00000001
because char Is signed char
Therefore, when shaping and lifting, the high supplementary symbol bit is 0
The result of the promotion is:
00000000000000000000000000000001
//Unsigned shaping lifting, high complement 0
```

Let's take an example

```int main()
{
char c = 1;
printf("%u\n", sizeof(c));		//1
printf("%u\n", sizeof(+c));		//4
printf("%u\n", sizeof(!c));		//1
return 0;
}
```

Why is the result like this? As long as C participates in the expression operation, it will be raised. If expression + C, it will be raised. Therefore, sizeof(+c) is 4 bytes. The expression - C will also be raised, so sizeof(-c) is 4 bytes, but sizeof(c) is 1 byte, but! C is a byte.

## Arithmetic conversion

If the operands of an operator are of different types, the operation cannot be performed unless one of the operands is converted to the type of the other operand. The following hierarchy is called ordinary arithmetic conversion.

```long double
double
float
unsigned long int
long int
unsigned int
int
```

If the type of an operand is low in the above list, it must be converted to the type of another operand before performing the operation.

Warning: but the arithmetic conversion should be reasonable, or there will be some potential problems

```float f = 3.14;
int num = f;//Implicit conversion, there will be precision loss
```

## Properties of the operator

There are three factors that affect the evaluation of complex expressions.

1. operator precedence
2. Associativity of operators
3. Whether to control the evaluation order.

Which of the two adjacent operators should be executed first? Depends on their priorities. If the two have the same priority, it depends on their combination.

Operator precedence **Summary: * * if the expression we write cannot determine the unique calculation path through the properties of the operator, there is a problem with this expression.

That's all for this issue. I'll see you next issue
If there is any mistake, welcome to communicate with us
It will continue to output in the future, and continue to pay attention to Zhou Wang

Remember to pay attention to the third company

Posted by nahydy on Sat, 25 Sep 2021 11:56:53 -0700