Getting started with iOS development☞C language (operator), ios Operator
Arithmetic Operators:
% Precautions for the modulo (remainder) OPERATOR:
1) the operand of the modulo operator can only be an integer.
2) the positive and negative effects of the modulo operation depend on the positive and negative values of the first operand.
3) if the left side of the modulo operation is smaller than the right side, the result is the left side.
Remainder (modulo operation): 5% 3 = 2;-5% 3 =-2; 5%-3 = 2; -5%-3 =-2 (the remainder is the same as the dividend !)
2% 9 = 2;
Notes for arithmetic operations:
Int a = 10.6;
Int B = 10.5 + 1.7;
Int B = 10.5 + 10; // upgrade the 10 on the right to the double type.
Automatically converts large data types to small ones, leading to loss of precision.
(Values of the same data type can be computed (such as addition operations), and the calculation result is still the same data type. The system automatically performs an "auto-type upgrade" Operation on the types that occupy less memory)
Float a = 10 + 3.45f; // upgrade the int value to float.
Int B = 'A' + 32; // char is upgraded to int
Double c = 10.3f + 5.7; // float is upgraded to double
Double B = 1.0/2;
(When a large range of data is assigned to a small range of variables, the system will automatically perform a forced type conversion operation, which is easy to lose precision)
Double a = (double) 1/2;
Double B = (double) (1/2 );
Int I = 10.7; // System Conversion
Int I = (int) 10.7; // self-forced conversion
Int a = 198l; // long to int
Char B = 65; // int to char
Int c = 19.5f; // float to int
The combination direction of the value assignment operator is: from right to left, and the priority is lower than that of the arithmetic operator.
The value assignment operator has the lowest priority among all operators except the comma operator.
The difference between auto-increment and auto-increment is as follows:
- If ++ is written before the variable, the variable auto-increment is first used and then the result after auto-increment is involved in the calculation.
- If ++ is written after the variable, the variable value is involved in the calculation before the variable auto-increment.
- To sum up one sentence: ++ first, First Auto-increment, then calculation, and ++ first, then auto-Increment
++ A and a ++:
Int B = ++ a; // equivalent to a = a + 1; B =;
Int B = a ++; // equivalent to B = a; a = a + 1;
Calculation rule: First left ++, then assign a value, and then right ++.
Sizeof () OPERATOR:
Sizeof can be used to calculate the memory bytes occupied by a variable, a constant, or a data type.
Note: sizeof is an operator rather than a function.
The format calculated using sizeof: sizeof (variable/constant/data type );
Sizeof has three forms:
- Sizeof (variable \ constant)
- Sizeof variable \ constant
- Sizeof (data type)
Example:
# Include <stdio. h>
Int main (){
Char c = 'a ';
Printf ("% lu, % lu \ n", sizeof (10), sizeof (c); // 4, 1
Printf ("% lu, % lu \ n", sizeof 10, sizeof c); // 4, 1
Printf ("% lu \ n", sizeof (int); // 4
Return 0;
}
Comma operator and comma expression
Example: a = a + 1, B = 3*4
It generally takes the form of expression 1, expression 2 ,... ..., Expression n
The operation procedure of a comma expression is: Calculate expression 1 first, then expression 2, and then calculate expression n in sequence.
The value of the entire comma expression is the value of the last expression.
// Comma Operator
# Include <stdio. h>
// The comma operator is similar to the addition, subtraction, multiplication, and Division operator. The comma operator takes the value of the last expression.
Int main (){
Int num, num1;
Num =; // the priority of the comma operator is lower than that of the value assignment operator.
Num1 = (); // increases the priority of the comma operator. Values are assigned by commas.
Printf ("num = % d num1 = % d \ n", num, num1); // num = 4 num1 = 7
Return 0;
}
Relational operators
<Less than Operator
<= Less than or equal to the operator
> Greater than Operator
> = Operator greater than or equal
= Equals Operator
! = Not equal to operator
There are only two types of return values for Relational operators: True, false. 1 (true), and 0 (false)
Note (priority ):
- (Arithmetic Operators) have a higher priority than (Relational operators)
For example, 3 + 4> 8-2; // The result is 1; equivalent to: (3 + 4)> (8-2 );
- The Union direction of Relational operators is "from left to right"
For example, 4> 3> 2; // The result is 0.
- (<,>,<=, >=) The priority is greater than (== ,! =)
For example, 2 = 3> 1; // The result is 0; equivalent to: 2 = (3> 1 );
Int a = 3> 4 + 7; // The result is 0.
Int B = (3> 4) + 7; // The result is 7.
Int c = 5! = 4 + 2*7> 3 = 10; // The result is 0.
Logical operators
& Logic and, | logic or ,! Non-logical
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)
- 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, 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 ".
- 5 | 4: The result is 1, which is "true";-6 | 0: The result is 1, which is "true"; 0 | 0: The result is 0, is "false ".
- ! 5 ,! 6.7 ,! -The result of 9 is 0 ,! The result of 0 is 1.
& | All features short circuit:
If the previous expression can determine the final result, it will not calculate the next expression!
# Include <stdio. h> Int main (){ // Short-circuit features of logical operators & | Int a = 3, B = 4, c = 5, r; R = (a> B) & (++ B> c ); Printf ("% d, % d \ n", a, B, c, r); // 3, 4, 5, 0 R = (a <B ++) | (B <c ++ ); Printf ("% d, % d \ n", a, B, c, r); // 3, 5, 5, 1 // Non-short-circuit logical operators & and | A = 3; B = 4; c = 5; R = (a> B) & (B> ++ c ); Printf ("% d, % d \ n", a, B, c, r); // 3, 4, 6, 0 R = (a <B) | (B <c ++ ); Printf ("% d, % d \ n", a, B, c, r); // 3, 4, 7, 1 Return 0; } |
Priority:
The priority of logical operators is: parentheses ()> minus sign->! > Arithmetic Operators> Relational operators >&>||
Conditional operators (three-object operators "? : "): Expression 1? Expression 2: expression 3
If expression 1 is true, the Operation Result of the Three-object operator is the value of expression 2. Otherwise, the operation result is the value of expression 3.
Single Object OPERATOR: Only one operand (-1 ;)
Binary OPERATOR: the operand has two (1-2 ;)
Three-object OPERATOR: three operands (5> 4? 1: 0)
When we are not sure about the priority of some operators, we can use () to specify the priority.