Detailed explanation of operators (c language)

Introduction:
Hello, everyone! My name is sy. Today I will share with you a detailed explanation of operators in C language. We know that a distinctive feature of C language is the application of expressions. Expressions are formulas that represent how to calculate values, and operators are used to construct expressions. Basic tools, C language has a very rich set of operators, including arithmetic operators, relational operators, logical operators, assignment operators, bit operation operators, increment and decrement operators, conditional operators, comma operators, pointers operators, byte count operators, cast operators, and special operators. This article explains each operator in great detail, let’s take a look~! !

1. Arithmetic operators (including +, -, *, /, %)

·1. Arithmetic operator is an operator widely used in many programming languages ​​including C language. This type of operator can perform addition, subtraction, multiplication, division and remainder.
·2. Both addition and multiplication operators are binary operators and require two operands, while unary operators only require one operand: i=+1; i=-1; (usually + does nothing)
*3. It should be noted that except for the % operator, other binary arithmetic operators allow the operands to be both integers and floating point numbers, and a mixture of the two is also possible. The remainder operator % requires the operand to be an integer. If one of the two numbers is not an integer, the compilation will not pass.
*4. Operator '/' When both operands are integers, operator '/' will discard the fractional part to intercept the result. So the result of 1/2 is 0 instead of 0.5.

//当int型操作数和float型操作数混合在一起时,运算结果是float型的
	int a = 7;
	float b = 3.5;
	printf("%f\n", a + b);
	printf("%f\n", a/b);

The operation result is as follows

2. Relational operators (including >, <, ==, >=, <=, !=)

·1. The result of a C language expression is 0 (false) or 1 (true). For example, the value of the expression 3<5 is 1, and the value of 3>5 is 0; relational operators are also suitable for comparing floating point numbers. , also allows comparison of mixed operands, such as: the value of 1<2.5 is 1;
·2. The priority of relational operators is lower than that of arithmetic operators, for example: the expression i+j<k-1 means (i +j)<(k-1). Relational operators are left associative.
*3. Note: i<j<k is legal in C language, but this expression is not testing whether j is between i and k (correct expression: i<j&&j<k), because the < operator is left It is combined, so the expression is equivalent to (i<j)<k. In other words, it first checks whether i is less than j, and then uses the result of the comparison (1 or 0) to compare it with k.
*4. Note: "Equal" is two adjacent =, not one =. It is worth noting that the == and != operators have lower precedence than the greater than less than relational operators.

As shown in the picture

3. Logical operators (!, &&, ||)

·1. ! , &&, || are respectively logical negation, logical AND, and logical OR (where! is a unary operator, &&, || are binary operators). The result produced by logical operators is 0 or 1, and any non-zero value is operated on. Numbers are treated as true values, and any zero value is treated as a false value. The rules are as follows:
·2. If the value of the expression is 0, then! The result of expression is 1;
·3. If the values ​​of expression 1 and expression 2 are both non-zero, then the result of expression 1 && expression 2 is 1;
· 4. If the value of expression 1 or expression 2 If any one of the values ​​is (or is) non-zero, then expression 1 || expression 2 evaluates to 1.
*5. Operators && and || both perform "short-circuit" calculations on the operands. The lvalue is calculated first, and then the rvalue is calculated. If the value of the expression can be derived only from the value of the left operand, there is no need to calculate the right value. Value; for example: (i != 0) && (i/j> 0), if i is not equal to zero, the rvalue is calculated. If i is equal to zero, there is no need to calculate *6.
operator ! It is right associative, && and || are left associative.

As shown in the picture

4. Assignment operators (=, +=, -=, *=, /=, %=, &=, |=, ^=, >>=, <<=)

·1. Expression a=b (b can be a constant, variable or a more complex expression); if the types of a and b are different, then the value of b will be converted to the type of a when the assignment operation occurs.
For example: int i = 0;
i = 6.66f //i is 6 at this time.
·2. The assignment operator can concatenate multiple assignments, for example: i = j = k = 0;
·3. The operator = is right associative, so the above expression is equivalent to i = (j = (k = 0 ));
·4. The assignment operator requires its left operand to be an lvalue . The lvalue represents an object, not a constant or the result of calculation. The variable is an lvalue, and expressions such as 10 or 2*i are not lvalues. .
·5. Compound assignment: i += 2; equivalent to i = i + 2;

5. Bit operation operators (<<, >>, ~, &, ^, |)

·1. The shift operators include left shift (<<) and right shift (>>), which can shift the binary bits of an integer left or right. Their operands can be any integer type (including char) , the shift operator performs integer promotion on both operands, and the return value type is the promoted type of the left operand.
·2. The value of i << j is the result of shifting the binary bits in i to the left by j bits. Each time one bit is overflowed from the leftmost end of i, and a 0 bit is added to the rightmost end of i; i >> j The value is the result of shifting the binary bits in i right by j bits.
·3. It should be noted that the right shift operator is divided into two types: arithmetic right shift and logical right shift. Arithmetic right shift: discard the right side and fill in the original sign bit on the left. If the original sign bit is 0, fill it with 0, and if it is 1, fill it. 1; Logical right shift: discard the right side and directly add 0 to the left side.
·4. The shift operator has a lower priority than the arithmetic operator. i << 2+1 is equivalent to i << (2+1)
· 5.~, &, ^, | are bitwise negation, bitwise AND, bitwise XOR, and bitwise OR respectively. Among them, ~ is a unary operator, and the others are binary operators. ~ is to invert each bit of the operand, that is, replace each 1 with a 0 and replace a 0 with a 1; & performs logic on the corresponding bits of the two operands. Similar to operations, operators ^ and | perform logical OR operations on two operands. The difference is that when both operands are 1, ^ produces 0 and | produces 1.

As shown in the picture
As shown in the picture

6. Increment and decrement operators (++, - -)

·"Auto-increment" (add 1) "Auto-decrement" (subtract 1), ++, - - operators can be used as prefixes (++i, - -i) or as suffixes (i++, i- -) , the prefix is ​​incremented by 1 before use, and the suffix is ​​incremented by 1 after use.

    int i = 1;
	printf("i is %d\n", ++i);
	printf("i is %d\n", i);
	int j = 1;
	printf("j is %d\n", j++);
	printf("j is %d\n", j);

operation result

7. Conditional operator (?:)

· Conditional expression: expression 1 ? Expression 2 : Expression 3 , which is the only ternary operator in C language. This expression can be interpreted as: if expression 1 is true, then expression 2 is executed , otherwise expression 3 is executed .
* Note: Conditional operators have lower precedence than all other operators except assignment operators.

8. Comma operator ( ,, )

·1. Comma expression: expression 1 , expression 2 , the value of comma expression must be realized in two steps. The first step is to calculate the value of expression 1. The second step is to calculate the value of expression 2. Put this value as the value of the entire expression.
·2. It should be noted that the value of expression 1 is not completely discarded. If the result of expression 1 has an impact on expression 2, then its value still works, but the final value is the value of expression 2.

9. Pointer operators (&, *)

·1. If you want to find the address of a variable, you can use & (address operator). If x is a variable, then &x is the address of x in memory. In order to obtain access to the object pointed to by the pointer, you can use * (indirect addressing operator). If p is a pointer, *p represents the object currently pointed to by p.
·2. When initializing a pointer variable, use the & operator to assign the address of a variable to it. For example:

    int i,j, * p;//方法一
	p = &i;
	int* pa = &j;//方法二

· Once the pointer variable points to the object, you can use the * operator to access the content stored in the object. For example, if p points to i, then the value of i can be displayed, for example:

printf("%d\n", *p)

10. Operator for finding the number of bytes (sizeof operator)

·1.sizeof expression: sizeof ( type name ), the sizeof operator allows the program to obtain the space required to store the value of the specified type. Its value is an unsigned integer, representing the number of bytes required to store the value belonging to the type name. . The expression sizeof(char) evaluates to 1, and the compiler itself can usually determine the value of the sizeof expression.
·2. When displaying the sizeof value, please note that the type of the sizeof expression is size_t, and size_t must be an unsigned integer. In C99, printf can directly display the type value of size_t without the need for forced conversion.

printf("Size of int:%zd", sizeof(int));//输出:Size of int:4

11. Forced type conversion operator ((type name))

· C language treats ( type name ) as a unary operator. The priority of unary operators is higher than that of binary operators. (unsigned int) time (NULL), (unsigned int) is forced type conversion.

12. Special operators ([ ], -->, ., ())

·1.[ ] is the array subscript index operator, which can obtain individual elements in the array. The operator in the following table requires two operands, one operand is the array name, and the other is an integer.
·2.——>operator and . operator are often used when structures access member variables. When the address of object b is passed in to the Print function, the structure pointer is used to receive it when the print function is implemented, and --> is used to point to the member variable when printing. When no address is passed in, you can also directly use the member access operator. to access.
·3. Function name (formal parameter list), which requires at least 1 operand. The function name and parameter list can be set according to user needs.

struct Student
{
    
    
	char name[20];
	int age;
};
void Print(struct Student* pb)
{
    
    
	printf("%s %d\n", pb->name, pb->age);
}
int main()
{
    
    
	struct Student b = {
    
    "小明", 20};
	printf("%s %d\n", b.name, b.age);
	Print(&b);
	return 0;
}

13. Attached: Operator priority table

Insert image description here
Thank you friends for reading, and you are welcome to leave a message in the comment area~! ! !
Insert image description here

Guess you like

Origin blog.csdn.net/m0_74475605/article/details/131977460