I. Arithmetic Operations
There are 34 operators in C language, including common addition, subtraction, multiplication, division, and so on.
1. addition operation +
In addition to addition, the addition operation can also represent the positive signs: + 5, + 90
2. subtraction-
The subtraction operation can also represent symbols-10 and-29.
3. Multiplication operation *
Note that the symbol is not X, *
4. division operation/
Note that the symbol is neither forward nor \,/
An integer is an integer except an integer. The value of 1/2 is 0, which is not 1/2
5. remainder operation (modulo operation) %
What is remainder? The remainder after division of two integers
% Both sides can only be integers
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
Expression
Combination (integration direction): 2 + 3 + 4
Priority: 5 + 4*8-3
Ii. Assignment operation 1. Simple assignment
Int A = 10 + 5; Operation Process
A = B = 10; calculation process
The left side of the equal sign cannot be a constant, for example, 10 = 11;
2. compound assignment
Complex addition, subtraction, multiplication, and Division remainder: A + = 4 + 5;
Iii. Auto-increment and auto-increment: 1. Easy to use
++ Auto-increment operator. For example, a ++ and ++ A are equivalent to a = a + 1.
, -- A, are equivalent to a = A-1
5 ++ is incorrect
2. Differences between ++ A and A ++
Int A = 10;
A ++; ++;
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
Sizeof (variable \ constant)
Sizeof variable \ constant
Sizeof (data type)
Sizeof data type // incorrect
V. relational operations (comparison operations) 1. Conditional judgment
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.
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
In C language, the condition is true, and the condition is false. Therefore, whether or not the condition is true or false ".
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.
There are only two types of Relational operators: 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
=,! In Relational operators ,! = Has the same priority. <, <=,>,> = has the same priority, and the former has a lower priority than the latter: 2 = 3> 1
The Union direction of Relational operators is "from left to right": 4> 3> 2
The priority of Relational operators is less than that of arithmetic operators: 3 + 4> 8-2
Vi. logical operations
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.
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
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"
If Condition A is not true, Condition B will not be judged whether it 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)
If the value of A is 4: First Judge A> 3, then judge a <5, also true. Therefore, the result is 1.
If the value of A is 2: Judge A> 3 first. If not, stop the judgment. Therefore, the result is 0.
Therefore, if the value of a is within the range (3, 5), the result is 1. Otherwise, the result is 0.
5> note
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 Union direction of Relational operators 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)
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
If Condition A is true, Condition B will not be judged whether it is true. Because Condition A is true, regardless of Condition B, the result of "Condition A | Condition B" must be 1, that is, "true"
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)
If the value of A is 4: first judge a <3, not true; then judge a> 5, not true. Therefore, the result is 0.
If the value of A is 2: first judge a <3, true, Stop Judgment. Therefore, the result is 1.
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)
If the value of A is 6: First Judge A> 5, then it is true, and then the inverse result is 0.
If the value of A is 2: Judge A> 3 first. If it is not true, the result after the inverse is 1.
Therefore, if the value of A is greater than 5, the result is 0. Otherwise, the result is 1.
4> note
You can use the logical non-operator for multiple consecutive 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"
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
The priority of logical operators is: parentheses ()> minus sign->! > Arithmetic Operators> Relational operators >&>||
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.
Expression 3 + 2 <5 | 6> 3 is equivalent to (3 + 2) <5) | (6> 3). The result is 1.
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
What are the three-object operator conditions? Value 1: Value 2
Obtain the maximum number in A and B.
Obtain the maximum number in A, B, and C.