1. Operator classification:
1. Arithmetic operators + - * /%
2. Shift operator < < > >
3. Bit operator & |^
4. Assignment operator = + = - =
5. Monocular operator sizeof! + + –
6. Relational operator > > = < < =! ===
7. Logical operator & &||
8. Conditional operator?:
9. Comma expression,
10. Subscript references, function calls, and structure members [] - > ()
2. Arithmetic operator
plus+ reduce- ride* except/ Surplus%
+, -, * are very simple, but pay attention to / and%
- In addition to the% operator, several other operators can act on integers and floating-point numbers.
- For the / operator, if both operands are integers, perform integer division. As long as there are floating-point numbers, it is floating-point division.
int ret =9/2; Integer division double tmp=9/2.0 Floating point division
- %The two operands of the operator must be integers. Returns the remainder after division.
3. Shift operator
3.1 operator Basics
- Shift operator, which moves binary bits (complement)
- There are three binary representations of integers: original code, inverse code and complement code
- Positive integer - the original code, inverse code and complement code are the same
- negtive integer
1. Source code - a binary sequence written directly according to the positive and negative of a number
2. Inverse code - the sign bit of the original code remains unchanged, and other bits are reversed by bit
3. Complement - inverse + 1 - warning ⚠ : For shift operators, do not move negative digits, which is not defined by the standard. And the operands should be for integers.
3.2 shift left operator < <: discard on the left and fill 0 on the right
int a=5; int b=a<<1; //Original 0000000000000101 //Anti 0000000000000101 //Supplement 0000000000000101 //Move the binary bit of a one bit to the left, and the first 0 is discarded, followed by //Fill 0 in the vacant position, as shown in the figure below //Note: in this process, the value of a does not change, just an operation on a
3.3 shift right operator > >
1. Logical shift: fill the left with 0 and discard the right
int a=-1; int b=a>>1; //11111111111111111111111111111111 //Shift obtained //01111111111111111111111111111111
2. Arithmetic: the left side is filled with the sign bit of the original value, and the right side is discarded. In most cases, the compiler gives priority to this
int a=-1; int b=a>>1; //11111111111111111111111111111111 //Shift obtained //11111111111111111111111111111111
4. Bit operator
- Bit operators have
& //Bitwise AND | //Bitwise OR ^ //Bitwise XOR Note: their operands must be integers
- &By bit and, binary bits are 1, which is 1. If there is one 0 or more, it is 0;
int main() { int a=3; int b=-2; int c=a&b; //000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 //11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110 - 2 //000000000000000000000000000000000000000000000000 10 bitwise and result //The result is the complement code, which is displayed on the screen only when it is translated into the original code }
- |Bitwise or binary, as long as there is 1, it is 1
int main() { int a=3; int b=-2; int c=a&b; //000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 //11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110 - 2 //11111111111111111111111111111111111111111111111 bitwise or result
- ^The same XOR binary bit is 0, and the difference is 1
int main() { int a=3; int b=-2; int c=a&b; //000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 //11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110 - 2 //11111111111111111111111111111111111111101 result of bitwise XOR
- The application of bitwise operators to exchange two variables
//Method 0 is a temporary variable method commonly used to exchange variables int main() { int a=10; int b=90; int tmp=0; tmp=a; a=b; b=tmp; } //The integer variable of method 1 has the maximum upper limit. If the value is large, overflow will occur int main() { int a=10; int b=90; a=a+b; b=a-b; a=a-b; return 0; } //Method 2 int main() { int a=10; int b=90; a=a^b; b=a^b; a=a^b; } Although method 2 is relatively simple, it is less used in the process because of its low readability and lower efficiency than method 0. Understanding of method 2: a and b XOR gets the password, b And password XOR can be translated a,a And password continue XOR Can translate the original b. a^a=0 0^a=a a^a^b=b a^b^a=b So XOR supports the commutative law
5. 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.
int weight = 120;//weight weight = 89;//Assign value if you are not satisfied double salary = 10000.0; salary = 20000.0;//Assign values using the assignment operator. Assignment operators can be used continuously, such as: int a = 10; int x = 0; int y = 20; a = x = y+1;//continuous assignment First y+1 Value assigned to x,Again x Value assigned to a. Low readability Split synonymous sentences: this writing method is clearer and easy to debug x=y+1; a=x;
6. Match assignment
Briefly introduce some assignment operators
+= -= *= /= %= >>= <<= &= |= ^=
These operators can be written as compound effects. For example:
int a=10; a=a>>1; a>>=1;//Compound Assignment int x = 10; x = x+10; x += 10;//Compound Assignment //The same is true for other operators. This is more concise
7. Unary operator: an operator with only one operand
- Some monocular operators
! Logical reverse operation - negative - positive & Get address 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
- ! Logical reverse operation
!Logic operates inversely, turning truth into falsehood and falsehood into reality int main() { int a=0; int b=!a; //!a=1 if(a) //a is true print Hello { printf("Hello"); } if(!a) //a is false print Hello { printf("Hello"); } }
- *Dereference operator
int main() { int a=10; itn *p=&a; int b=*p; *p=20 } //For any variable, it consists of its space and values in space //The left value uses space and the right value uses content //*p on the right points to the value in the space, * p on the left points to the space
- sizeof
//1. sizeof is an operator, not a function //2. sizeof is the memory size of the calculated variable or type creation variable, in bytes //It has nothing to do with what data is stored in memory int main() { int a; printf("%d",sizeof(a));//4 printf("%d",sizeof a);//4 //When it is a variable, it can be omitted. It is the memory size of a, indicating that sizeof is not a function printf("%d",sizeof(int));//4 printf("%d",sizeof int)//4 some compilers support this approach int b=5; short s=10; printf("%d",sizeof(s=a+2));//2 printf("%d",s);//10 Under normal circumstances int Put values of type short Truncation occurs in the type sizeof in Because it's putting the value short Type s So the calculated value is 2. sizeof The internal expression does not participate in the operation, so s The value of is still 10 }
Explain the expression and operation inside sizeof in detail: the function of sizeof is implemented during compilation. When the compiler sees that the type of s depends on short, it gets the result of 2, that is, there is no chance to perform the action of a+2 during the linking process. Therefore, the expression inside sizeof does not participate in the operation.
- Explain sizeof and arrays
#include <stdio.h> void test1(int arr[]) { printf("%d\n", sizeof(arr));//(2) 4 } void test2(char ch[]) { printf("%d\n", sizeof(ch));//(4) 4 } int main() { int arr[10] = {0}; char ch[10] = {0}; The array list is placed separately sizeof Inside, The array name represents the entire array, The size of the entire array is calculated in bytes printf("%d\n", sizeof(arr));//(1) 40 printf("%d\n", sizeof(ch));//(3) 10 When passing an array parameter, the array name will be downgraded to the address of the first element passed, which is essentially a pointer. sizeof The calculated pointer size is 4 in 32 bits and 8 in 64 bits test1(arr); test2(ch); return 0; }
- ~Bitwise inversion of binary bits
//It can be used with other operators to make binary bit changes. int a=0; //00000000000000000000000000000000 ~a; //11111111111111111111111111111111
- ++And –
int main() { //Post + +, use first, then++ int a=10; int b=a++; printf("%d",a);//11 printf("%d",b);//10 } int main() { //Pre + +, first + +, then use int a=10; int b=a++; printf("%d",a);//11 printf("%d",b);//11 } //--It is also divided into pre -- and post -- with the same usage as + +. //Note: + + and -- do not write too complex expressions, such as the following code: int main() { int a=1; int b=(++a)+(++a)+(++a); return 0; }
- () cast
int main() { int a=3.14//The default floating-point number written out is double //Writing this directly may result in data loss //Correct writing int a=(int)3.14; }
8. Relational operator
When comparing variables of the same type, the comparison of time and address is meaningless > >= < <= != 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: during programming== and=Mistakes caused by careless writing.
9. Logical operator
&& Logic and(Table and) int main() { int a=3; int b=5; //Logic and only focus on true and false. If a is true and b is true, the whole is true //If one of a and b is false, it is false int c=a&&b; } || Logical or(Table or) int main() { int a=0; int b=5; //If one of a and b is true, the expression is true int c=a||b; } True topic #include <stdio.h> int main() { int i = 0,a=0,b=2,c =3,d=4; i = a++ && ++b && d++;//1 2 3 4 First use, a Use the value of 0. Because the front is false, the expression as a whole is false and has not arrived yet ++b Part, now with 0&&d++,Is the whole, the front is false, the whole is false, d++No implementation. i = a++||++b||d++//1 3 3 4 a++,Use the original first a The value of, a False, continue to++b,See true or false printf("a = %d\n b = %d\n c = %d\nd = %d\n", a, b, c, d); return 0; } #include <stdio.h> int main() { int i =0,a=1,b=2,c =3,d=4; i = a++||++b||d++; Run to a++,First use a The value of 1 is true, so the whole a++||++b For real, Get result 1,++b Skipped, then 1||d++,1 The result of the expression is true, and the whole expression is true, resulting in result 1, d++Also skipped printf("a = %d\n b = %d\n c = %d\nd = %d\n", a, b, c, d); //2 2 3 4 return 0; }
10. Conditional operator
- exp1? Exp2: EXP3 is also the only ternary operator. If the result of the expression is true, the result of expression 2 is the result of the overall expression; otherwise, the result of expression 3 is the result of the overall expression. It can be regarded as an if else selection statement.
Convert to conditional expression if (a > 5) b = 3; else b = -3; a>5?b=3:b=-3; Find the maximum of two numbers int main() { int a=10; int b=20; int m=((a>b)?(a):(b));//Enclosed in parentheses, it has high readability and safe syntax }
11. Comma expression
exp1, exp2, exp3, ...expN
Comma expressions are multiple expressions separated by commas. Comma expression, executed from left to right. The result of the entire expression is the result of the last expression.
/Code 1 int a = 1; int b = 2; int c = (a>b, a=b+10, a, b=a+1);//comma expression c How much is it? 13 //Code 2 if (a =b + 1, c=a / 2, d > 0) What really plays a role of judgment is d>0 Is it established //Code 3 a = get_val(); count_val(a); while (a > 0) { //Business processing a = get_val(); count_val(a); } If you use a comma expression, override: while (a = get_val(), count_val(a), a>0) { //The original code redundancy problem is solved //Business processing }
12. Subscript references, function calls, and structure members
1. [] subscript reference operator
Operand: an array name + an index value
int arr[10];//Create array arr[9] = 10;//Practical subscript reference operator. arr[9]->*(arr+9)->*(9+arr)->9[arr] //Syntax supports the form of 9[arr], but it is generally not written in the form of 9[arr]. []The two operands of are arr And 9 printf("%p---%p",&arr[8],arr+8);//These two addresses are the same //arr+i is the address of the element with subscript i in array arr
2. () function call operator
Accept one or more operands: the first operand is the function name, and the remaining operands are the parameters passed to the function.
#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. Access to structure members
. usage: structure. Member name
->Usage: structure pointer - > member name
//Custom data type struct Book { char name[10]; float price; char id[10]; }; void Print1(struct Book b) { printf("title:%s\n",b.name); printf("Price:%f\n",b.price); printf("Book number:%s\n",b.id); } void Print2() { printf("title:%s\n",b->name); printf("Price:%f\n",b->price); printf("Book number:%s\n",b->id); } int main() { struct Book b={"C book","55.5f","c21415215"}; b.name="data structure";//Wrong writing. name is the address of the first element strcpy(b.name,"data structure");//Using strcpy to achieve change Print1(b); Print2(&b);