Chapter 3, Operators in C
This chapter focuses on the precedence between C arithmetic operators, assignment operators, relational operators, logical operators, and operators.
When we were in elementary school, we learned various operations, such as addition, multiplication, and blending. The operators used operators such as +, -, ×, and so on.
There are also operators in C, but the writing may be different. Let me talk about what is the operation in C language? The operation in C language is the process of operating and processing data. So what about the operator? The operator is the way to specify the operation.
So what are the operators in C?
※ Arithmetic operator
※ Assignment operator
※ Relational operator
※ Logical Operators
※ Trinocular operator
The basic arithmetic operators for C language are as follows:
Note in the division operation:
If the two numbers divided are integers , the result is also an integer , the fractional part is omitted, such as 8/3 = 2; and one of the two numbers is a decimal , and the result is a decimal , such as: 9.0/2 = 4.500000 .
Note in the remainder operation:
This operation is only suitable for the remainder operation with two integers , such as: 10%3 = 1; and 10.0%3 is wrong; the symbol after the operation depends on the sign of the modulo, such as (-10)%3 = -1; and 10% (-3) = 1.
Note: There is no operator in C language, and you can’t use arithmetic symbols such as ×, ÷.
Another: About the self-increase and decrement operators, will be explained in the next section.
Auto Increment and Decrement operators
The auto-increment operator is “++”, its function is to increment the value of the variable by 1; the decrement operator is “–“, its function is to decrement the value of the variable by 1. They are often used in loops. The self-incrementing and decrementing operators have the following forms:
Note: Both a++ and ++a are equivalent to a=a+1. After the expression is executed, the value of a is incremented by 1, whether a– or –a is equivalent to a=a-1. , the value of a is reduced by 1 after the expression is executed.
Please analyze the following code output:
Little friends, don’t rush to see the results of the run, think about what the results are?
In the previous section, I touched the simple assignment operator “=”, such as:
So what is the assignment operator in C?
The assignment operators in C are divided into simple assignment operators and compound assignment operators . We have already touched the simple assignment operator “=” before, let’s talk about the compound assignment operator:
The compound assignment operator is constructed by adding other operators before the simple assignment “=”, such as +=, -=, *=, /=, %=.
Look at a small example:
Analysis: Define the integer variable a and assign it to 3, a += 5; this formula is equivalent to a = a + 5; add the variables a and 5 and assign them to a
Note: There is no space between the operator and the equal sign in the compound operator.
The symbols explained in this section should be used in life. If we go to the mall to buy things, we will compare the prices. Is it equal? Which one is expensive and which one is cheap.
There is also such a comparison in the C language program, this time you need to use the relational operator.
Here are the relational operators in C:
The values of the relational expressions are “true” and “false”, which are represented by the integers 1 and 0 in the C program. Such as:
The result is:
Note: >=, <=, ==, != There must be no spaces between such symbols.
In mathematics we have seen a formula like 7<x<100, which means that x is greater than 7 and x is less than 100.
It is not acceptable to write the range value of a variable in the program. The computer can’t understand such a formula, so how can the computer understand it? The logical operators are used here.
Let’s take a look at the logical operators in C:
Then the calculation of the previous formula is a computer that can be read: x>7 && x<100;
There are also two types of logical operations, “true” and “false”, which are represented by integers 1 and 0 in C language. The evaluation rules are as follows:
1) AND operation (&&)
The result is true when both variables participating in the operation are true , otherwise it is false. For example: 5>=5 && 7>5, the operation result is true;
2) OR operation (||)
As long as one of the two variables participating in the operation is true , the result is true . When both quantities are false, the result is false. For example: 5>=5||5>8, the operation result is true;
3) Non-computing (!)
When the variable participating in the operation is true , the result is false ; when the amount of participation is false , the result is true . For example: !(5>8), the result of the operation is true.
When we shop, we often say that if the item is larger than the amount of money, it will not be bought. The condition is more than how much, and the result is not to buy.
So can the above mentioned in the C language be expressed? Of course it is ok, but in C language, you need to use a symbol, this symbol is the trinocular operator.
The trinocular operator in C: “?:”, the format is:
Expression 1 ? Expression 2: Expression 3;
The execution process is:
It is first judged whether the value of the expression 1 is true, if it is true, the expression 2 is executed; if it is false, the expression 3 is executed.
Write the above example in code:
Q: The priority is Shenma?
A: Priority is the order of operations in the operation.
Then let’s take a look at the order of the various arithmetic symbols we have learned!
The priority level 1 has the highest priority, and the priority level 10 has the lowest priority level.
You don’t have to go to the priority order of the deadlock operator, remember that the highest priority level will be invincible in development~ Take a look at the following example:
Parsing: The highest level of operators in C is (), so the execution is:
1. Calculate the results of a>3 and a+3 first, and calculate the formula as 1*a-14%3;
2. Calculate the results of 1*a and 14%3 again, and calculate the formula 11-2;
3. Finally, the calculated result is 9.