1, Operator
1. Unary operator (only one operand)
! Logical reverse operation
#include <stdio.h> int main() { //In C language, 0 means false and non-0 means true int flag = 0; if (!flag) { printf("hehe\n"); } return 0; }
-Negative + positive
#include <stdio.h> int main() { int a = 10; int b = -a; printf("%d\n", b); return 0; }
The type length (in bytes) of the sizeof operand is not a function but an operator (operator)
#include <stdio.h> int main() { int a = 10; printf("%d\n", sizeof(a));//The unit is byte printf("%d\n", sizeof(int));//Both can be written, using variable name or variable type return 0; }
--And++
#include <stdio.h> int main() { int a = 10; //int b = a + 1; //int b = ++a;// Pre + +, first + +, then use, result a=11,b=11 int b = a++;//Post + +, use first, and then + +, the result is b=10,a=11 printf("a=%d b=%d\n", a, b); return 0; }
Cast type
#include <stdio.h> int main() { int a =(int)3.14; printf("%d", a); return 0; }
2. Relational operators
!= Used to test "inequality"
==Used to test equality
#include <stdio.h> int main() { int a = 10; if (a >=10) { printf("hehe\n"); } return 0; }
3. Logical operators
&&Logic and
|| logical or
#include <stdio.h> int main() { int a = 3; int b = 4; if ((a == 3) && (b == 4)) { printf("hehe\n"); } return 0; }
#include <stdio.h> int main() { int a = 5; int b = 4; if ((a == 3)||(b == 4)) { printf("hehe\n"); } return 0; }
4. Conditional operator (ternary operator)
exp1 ?exp2 : exp3
1 is true, 2 is calculated, 3 is not calculated. exp2 is the result of the entire expression
1 is false, 2 does not count, 3 counts. exp3 is the result of the entire expression
int main() { int a = 3; int b = 5; int m = 0; /*if (a > b) m = a; else m = b;*/ m = (a > b) ? a : b; printf("%d\n", m); return 0; }
5. Comma expression
Comma expression. The expression will be evaluated from left to right. The result of the whole expression is the result of the last expression
#include <stdio.h> int main() { int a = 3; int b = 20; int c = 0; int d = (a -= 3, b += a, c = a - b, b = a - 4); printf("d=%d\n",d); return 0; }
6. Subscript references, function calls, and structure members
#include <stdio.h> int main() { int arr[10] = { 0 }; //0~9 arr[5] = 9;//[] here is the subscript reference operator, and its operands: array name, subscript return 0; }
#include <stdio.h> int get_max(int x, int y) { if (x > y) return x; else return y; } int main() { int m=get_max(3,5);//Function call operator whose operands are: function name, 3,5 printf("%d\n",m); return 0; }
2, Common keywords
- break: commonly used in switch statements and loop statements, such as for, while, do while Loop
- const: used to modify variables
- continue: used in a loop
- default: used in switch statements
- enum: enumeration keyword
- extern: used to declare external symbols
- signed: signed
- Unsigned: unsigned
- typedof: type renaming
#include <stdio.h> unsigned int num = 10; typedef unsigned int uint;//typedef is equivalent to giving an alias to a type, simplifying a complex type, which can be understood as renaming a type uint num2 = 10; int main() { return 0; }
- union: Consortium or common body
- void: none or empty
- Register: register keyword. Registers are integrated into the CPU. The register has the advantages of high speed, high cost and small space. Followed by: Advanced cache, memory, hard disk and network disk. The memory is larger and larger, the speed is slower and slower, and the cost is lower and lower.
int main() { int num = 10;//4 bytes register int num2 = 20;//It is recommended to put 20 in the register return 0; }
- static is used to modify variables and functions:
a. Modified local variables: called static local variables
b. Modify global variables: called static global variables
c. Modifier function: called static function
#include <stdio.h> void test() { int a = 5; a++; printf("%d", a); } int main() { int i = 0; while (i < 10) { test(); i++; } return 0; } //The result is 6666666666
#include <stdio.h> void test() { static int a = 5; a++; printf("%d ", a); } int main() { int i = 0; while (i < 10) { test(); i++; } return 0; } //The result is 6 7 8 9 10 11 12 13 14 15
When static modifies a local variable, the local variable becomes a static local variable. It is out of the local range and will not be destroyed. The next time you enter the function, it still exists because the static modified local variable is stored in the static area. When static modifies a local variable, what actually changes is the storage location of the variable. Originally, a local variable was placed in the stack area, but it still exists outside the scope, and the life cycle is not over.