I. Arithmetic Operations
There are 34 operators in C language, including common addition, subtraction, multiplication, division, and so on.
1. addition operation +
L addition operations can be performed in addition to the opening, and the positive signs are + 5 and + 90.
2. subtraction-
L subtraction can also represent symbols:-10 and-29.
3. Multiplication operation *
Note that the symbol is not X, *
4. division operation/
L pay attention to the symbols. They are not symbols, nor \,/
L an integer except an integer or an integer. The value of 1/2 is 0, which is not 1/2
5. remainder operation %
L what is remainder? The remainder after division of two integers
L % the two sides can only be integers
L positive and negative are determined by the value on the left of %.
6. Notes
1> automatic type conversion
Int A = 10.6;
Int B = 10.5 + 1.7;
Automatically converts large data types to small ones, leading to loss of precision.
2> automatic type upgrade
Int B = 10.5 + 10;
Increase 10 on the right to the double type.
Double B = 1.0/2;
Solve the Division precision Problem
3> forced type conversion
Double A = (double) 1/2;
Double B = (double) (1/2 );
4> Operation Sequence
L Expression
L combination (integration direction): 2 + 3 + 4
L priority: 5 + 4*8-3
7. Exercise
1) when? When an operator is used, no matter how the value of integer variable A changes, the result C cannot exceed 10.
Int c =? 10;
2) prompt the user to enter two integers and output the average of the two integers
3) prompt the user to enter the number of seconds for a time. For example, if 500 seconds is used, enter 500, and then output the corresponding minutes and seconds. For example, seconds is 8 minutes and 20 seconds.
Ii. Assignment operation 1. Simple assignment
L int A = 10 + 5; Operation Process
L a = B = 10; calculation process
L The left side of the equal sign cannot be a constant, for example, 10 = 11;
2. compound assignment
L complex addition, subtraction, multiplication, and Division remainder: A + = 4 + 5;
Iii. Auto-increment and auto-increment: 1. Easy to use
L ++ auto-incrementing operator. For example, a ++ and ++ A are equivalent to a = a + 1.
L auto-subtraction operator. Such as a --, -- A, are equivalent to a = A-1
L 5 ++ is incorrect
2. Differences between ++ A and A ++
L int A = 10;
U a ++; ++;
U int B = A ++; int B = ++;
Iv. Role of sizeof1.
It is used to calculate the memory bytes occupied by a variable, a constant, and a data type.
2. Basic Form
L sizeof (variable \ constant)
L sizeof variable \ constant
L sizeof (data type)
L cannot be sizeof Data Type
V. relational operations (comparison operations) 1. Conditional judgment
L by default, every correct code written in the program will be executed. But most of the time, we want to execute a piece of code only when a condition is true.
L in this case, you can use conditional statements. However, we do not study conditional statements for the moment. First, let's look at some more basic knowledge: how to judge whether a condition is invalid.
2. true and false
L in C language, the condition is true, and the condition is false. Therefore, whether or not the condition is true or false ".
L how can we determine whether it is true or false? The C language specifies that all values are false. Any non-0 values are true, and only 0 values are false ". That is to say, 108,-18, 4.5, and-10.5 are all "true", while 0 is "false ".
3. Link comparison
It is often compared during development, such as the card size in the landlords game. You can use Relational operators to compare the sizes of two values.
L Relational operators have only two types of operation results: if the condition is true, the result is 1, that is, "True". If the condition is not true, the result is 0, that is, "false ".
4. Usage notes
L =,! in Relational operators ,! = Has the same priority. <, <=,>,> = has the same priority, and the former has a lower priority than the latter: 2 = 3> 1
L The Union direction of Relational operators is "from left to right": 4> 3> 2
L Relational operators have priority less than arithmetic operators: 3 + 4> 8-2
5. Exercise
Calculate the value of the following expression
L 3> 4 + 7
L (3> 4) + 7
L 5! = 4 + 2*7> 3 = 10
Vi. logical operations
L sometimes, we need to execute a certain piece of code when multiple conditions are set up at the same time. For example, a user can execute the login code only after entering the QQ and password at the same time, if you only enter QQ or password, you cannot execute the logon code. In this case, we need to use the logical operators provided by the C language.
L there are only two logical operations: "true" is 1, and "false" is 0.
1. & logic and
1> Format
"Condition A & Condition B"
2> operation result
Only when Condition A and Condition B are set, the result is 1, that is, "True". In other cases, the result is 0, that is, "false ". Therefore, if either Condition A or condition B is invalid, the result is 0, which is false"
3> operation process
Always first judge whether Condition A is true
L if condition A is true, then judge whether Condition B is true. If Condition B is true, the result of Condition A & Condition B is 1, that is, true ", if Condition B is not true, the result is 0, that is, "false"
L if condition A is not true, it will not judge whether Condition B is true: Because Condition A is no longer true, regardless of Condition B, the result of "Condition A & Condition B" must be 0, that is, "false"
4> example
The combination direction of logic and is "from left to right ". For example, expression (A> 3) & (A <5)
L if the value of a is 4: First Judge A> 3, then judge a <5, also true. Therefore, the result is 1.
L if the value of a is 2: Judge A> 3 first. If not, stop the judgment. Therefore, the result is 0.
L therefore, if the value of a is within the range of (3, 5), the result is 1. Otherwise, the result is 0.
5> note
L if you want to determine whether the value of A is within the range of (3, 5), do not write it as 3 <A <5, because the link operator's combination direction is "from left to right ". For example, if a is 2, it calculates 3 <A, that is, 3 <2. The condition is not true and the result is 0. Compare with 5, that is, 0 <5. The condition is true and the result is 1. Therefore, the result of 3 <A <5 is 1 and the condition is true. That is to say, when the value of A is 2, the value of a is within the range of (3, 5. This is obviously incorrect. The correct judgment method is: (A> 3) & (A <5)
L c language: any non-0 value is true, and only 0 is false ". Therefore, logic and values are also applicable to numerical values. For example, the result of 5 & 4 is 1, which is "true"; the result of-6 & 0 is 0, which is "false"
2. | logical or
1> Format
"Condition A | Condition B"
2> operation result
If Condition A or condition B is set to true (both Condition A and Condition B are set), the result is 1, that is, true "; if neither Condition A nor Condition B is true, the result is 0, which is false ".
3> operation process
Always first judge whether Condition A is true
L if condition A is true, Condition B will not be judged again: Because Condition A is true, regardless of Condition B, the result of "Condition A | Condition B" must be 1, that is, "true"
L if condition A is not true, then judge whether Condition B is true. If Condition B is true, the result of "Condition A | Condition B" is 1, that is, "true ", if Condition B is not true, the result is 0, that is, "false"
4> example
The logic or combination direction is "from left to right ". For example, expression (A <3) | (A> 5)
L if the value of a is 4: first judge a <3, not true; then judge a> 5, not true. Therefore, the result is 0.
L if the value of a is 2: first judge a <3, true, Stop Judgment. Therefore, the result is 1.
L therefore, if the value of a is within the range of (-∞, 3) or (5, + ∞), the result is 1. Otherwise, the result is 0.
5> note
C language: any non-0 value is true, and only 0 is false ". Therefore, it is logical or suitable for numeric values. For example, the result of 5 | 4 is 1, which is "true"; the result of-6 | 0 is 1, which is "true"; the result of 0 | 0 is 0, false"
3 .! Non-logical
1> Format
"! Condition"
2> operation result
In fact, Condition A is reversed: If Condition A is true, the result is 0, that is, "false". If Condition A is not true, the result is 1, that is, "true ". That is to say, it is true or false.
3> example
The combination direction of logic is "from right to left ". For example, expression! (A> 5)
L if the value of a is 6: First Judge A> 5, then the result is 0.
L if the value of a is 2: Judge A> 3 first. If it is not true, the result after the reverse operation is 1.
Therefore, if the value of A is greater than 5, the result is 0. Otherwise, the result is 1.
4> note
L logical non-operators can be used consecutively for multiple times :! (4> 2) the result is 0, which is "false ",!! (4> 2) the result is 1, which is "true ",!!! (4> 2) the result is 0, which is false"
L c language: any non-0 value is true, and only 0 is false ". Therefore, perform logical non-0 values! The calculation results are all 0, and the logic of the 0 value is not! The calculation result is 1 .! 5 ,! 6.7 ,! -The result of 9 is 0 ,! The result of 0 is 1.
4. Priority
L the priority order of logical operators is: parentheses ()> minus sign->! > Arithmetic Operators> Relational operators >&>||
L expression! (3> 5) | (2 <4) & (6 <1): Calculate first! (3> 5), (2 <4), (6 <1), the result is 1, and the formula becomes 1 | 1 & 0, and then 1 & 0, formula 1 | 0. The final result is 1.
L expression 3 + 2 <5 | 6> 3 is equivalent to (3 + 2) <5) | (6> 3). The result is 1.
L expression 4> 3 &&! -5> 2 is equivalent to (4> 3 )&&((! (-5)> 2), the result is 0.
7. Three-object Operator
N object operator-> 3 object Operator
L int A = 5? 10: 2;
L obtain the maximum number in A and B.
L obtain the maximum number in A, B, and C.