Operators and expressions
Key points of this chapter
1. Introduction to various operators.
2. Evaluation of expression.
Operator
Classification:
- Arithmetic operator
- Shift operators
- Bitwise operators
- Assignment operator
- unary operator
- Relational operator
- Logical operator
- Conditional Operator
- comma operator
- Subscript references, function calls, and structure members
Arithmetic operator
+ - * / %
1. In addition to the% operator, several other operators can act on integers or floating-point numbers.
2. For the / operator, if both operands are integers, perform integer division. Floating point division is performed if only one end has a floating point number.
The two operands of the 3.% operator must be integers and return the remainder after integer division.
Shift operators
<< Shift left operator >> Shift right operator
int main() { int a = 2; //Move the binary bit of a one bit to the left int b = a << 1; //Move the binary bit of a one bit to the right int c = a >> 1; printf("b = %d\n",b); printf("c = %d\n",c); return 0; }
Warning: shift cannot move negative digits;
Bitwise operators
& Bitwise AND | Bitwise OR ^ Bitwise XOR Note: their operands must be integers
int main() { //3 - 00000000000000000000000000000011 //5 - 00000000000000000000000000000101 int a = 3; int b = 5; //&- by (binary) bits and int c = a & b; // |- Press (binary) or int d = a | b; // ^- exclusive or by (binary) bit // XOR the corresponding binary bit // Rule: the same is 0 and the difference is 1 int e = a ^ b; printf("c = %d\n",c); printf("d = %d\n",d); printf("e = %d\n",e); return 0; }
Exercise: write code to realize: find the number of 1 in the binary of an integer stored in memory.
Assignment operator
int weight = 100; weight = 89;
Compound operator
+= -= *= /= %= &= |= ^= >>= <<=
int a = 10; a = a + 10; a += 10;//Compound Assignment
unary operator
Unary operator - only one operand
! 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
sizeof is an operator, not a function
int main() { short s = 5; int a = 10; //The expression in sizeof parentheses does not participate in the operation! printf("%d\n",sizeof(s = a + 2)); printf("%d\n",s); int a = 10; int arr[10] = {0}; printf("%d\n",sizeof(arr));//The unit is byte printf("%d\n",sizeof(int [10]));//40 - int [10] is the type of arr array return 0; }
int main() { int a = 4; //00000000000000000000000000000100 - 4 //01111111111111111111111111111011 //00000000000000000000000000000100 //00000000000000000000000000000101 - 5 a = ~a; printf("%d\n",a); return 0; }
//&Get address
int main() { int a = 10; printf("%p\n",&a);// &- get address operator int* pa = &a;//pa is used to store addresses - pa is a pointer variable *pa = 20; //Dereference operator - Introduction to access operators printf("%d\n",a);//20 return 0; }
Relational operator
> >= < <= != //Used to test "inequality" == //be used for
Logical operator
&& Logic and || Logical or
Distinguishing logic and bitwise and distinguishing logic or and bitwise OR
1&2 ---> 0 1&&2 ---> 1 1|2 ---> 3 1||2 ---> 1
Characteristics of logic and or:
If a=0 in a & & b, b does not participate in the operation.
If a=1 in a | b, b does not participate in the operation.
int main() { int i = 0, a = 0, b = 2, c = 3, d = 4; i = a++ && ++b && d++; printf("a = %d\nb = %d\nc = %d\nd = %d\n",a,b,c,d); return 0; }
Ternary operator (conditional operator)
exp1 ? exp2 : exp3
practice:
int main() { int a = 0; int b = (a > 5 ? 1 : -1); printf("%d\n",b); return 0; }
comma expression
Comma expressions are multiple expressions separated by commas. You want to evaluate from left to right, but the result of the entire expression is the result of the last expression.
int main() { int a = 5; int b = 3; int c = 0; //Comma expression - evaluates from left to right, but the result of the entire expression is the result of the last expression int d = (c = 1, a = c + 3,b = a - 4,c += 5); printf("d = %d\n",d); return 0; }
// a = get_val(); count_val(a); while(a>0){ a = get_val(); count_val(a); } //If you use a comma expression, override: while(a = get_val(),count_val(a),a>0){ }
Subscript references, function calls, and structure members
1. [] subscript reference operator
Operand: an array name + an index value
int main() { int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; printf("%d\n",arr[4]);// [] - is the subscript reference operator // The operands of [] are 2: arr, 4 return 0; }
2. () function call operator
Receive one or more operands: the first operand is the function name, and the remaining operands are the parameters passed to the function.
3. Visit members of a structure
. structure. Member name
->Structure pointer - > member name
//Create a structure struct Book { //Member of structure (variable) char name[20]; char id[20]; int price; }; int main() { struct Book b = {"C language","C20102",55}; struct Book* pb = &b; //Structure variable name. Member name printf("title:%s\n",b.name); printf("Book No.:%s\n",b.id); printf("Price:%d\n",b.price); //Structure pointer - > member name printf("title:%s\n", pb->name); printf("Book No.:%s\n", pb->id); printf("Price:%d\n", pb->price); return 0; }
Expression evaluation
Implicit type conversion
The shaping arithmetic operation of C is always performed at least with the precision of the default integer type.
To achieve this precision, the characters and short operands in the expression are converted to normal integers before use, which is called integer promotion.
How to improve integer?
Integer promotion is carried out according to the sign bit of the data type of the variable
Examples of integer lifting:
int main() { char a = 3; //00000000000000000000000000000011 //00000011 - a char b = 127; //00000000000000000000000001111111 //01111111 - b char c = a + b; //00000000000000000000000000000011 //00000000000000000000000001111111 //00000000000000000000000010000010 //100000010 - c //11111111111111111111111111100000010 - complement //11111111111111111111111100000001 - inverse code //100000000000001111110 - original code //111111110 //It is found that both a and b are char types, and neither of them reaches the size of an int //There will be plastic lifting printf("%d\n",c); // -126 return 0; }
Arithmetic conversion
Properties of the operator
There are three factors that affect the evaluation of complex expressions.
- operator precedence
- Tuberculousness of operators
- Control evaluation order