[C] Detailed explanation of operators


Today I will bring you a detailed introduction about C language operators. There are mainly the following types of operators in C language:

1. Arithmetic operator
2. Shift operator
3. Bit operator
4. Assignment operator
5. Unary operator
6. Relational operator
7. Logical operator 8.
Comma expression
9. Conditional operator
9. Next Subscript references, function calls and structure members

Next, I will give you a detailed introduction!

arithmetic operator

+ - * / %

Among them, addition and subtraction are the same as in mathematics, multiplication is represented by * in C language, and the rest are the same as in mathematics.
In the C language, division is divided into integer division and decimal division. Integer division/both times must be integers, while decimal division/two sides only need to have a decimal. For example:

#include <stdio.h>

int main()
{
    
    
	printf("%d\n", 2 / 3);
	printf("%lf\n", 2 / 3.0);
	printf("%lf\n", 2.0 / 3.0);
	printf("%lf\n", 2.0 / 3);
	return 0;
}

Code running result:
insert image description here
There is also a % modulus operator, which is to find the remainder. The operator needs to pay attention to the fact that both sides of the symbol must be integers, not floating point numbers!

shift operator

<< left shift operator
>> right shift operator

The shift operator operates on our binary bits, so I will add a knowledge point here, the original code, the inverse code, and the complement code.
For a signed integer, the most significant bit in binary is the sign bit.
The original code, inverse code and complement code of positive numbers are the same.
The original code of the negative number is written in binary form. The complement code means that the sign bit remains unchanged, and the remaining bits are reversed, and the complement code is equal to the complement code plus 1.

The left shift operator means that all binary bits are shifted to the left, the left is discarded, and 0 is added to the right.
There are two types of right shift operations:

  1. Logical shift fills with 0 on the left and discards on the right
  2. The left side of the arithmetic shift is filled with the sign bit of the original value, and the right side is discarded

code demo

#include <stdio.h>

int main()
{
    
    
	printf("%d\n", 5 >> 1);
	printf("%d\n", 5 << 1);
	return 0;
}

Code running result:
insert image description here
For shift operators, do not move negative digits, this is undefined by the standard.

bitwise operator

& bitwise and
| bitwise or
^ bitwise exclusive or

Bitwise operators also operate on binary bits.
The & operator, the binary digits of the two numbers correspond to 1 is 1, as long as there is 0, it is 0.
The | operator, the binary digits of the two numbers correspond to 0 is 0, as long as there is 1, it is 1.
^ operator, the two numbers have the same bit as 0, and the difference is 1.
Code demo:

#include <stdio.h>

int main()
{
    
    
	printf("%d\n", 1 & 2);
	printf("%d\n", 1 | 2);
	printf("%d\n", 1 ^ 2);
	printf("%d\n", 1 ^ 2 ^ 1);
	printf("%d\n", 1 ^ 1 ^ 2);

	return 0;
}

Running Results
insert image description here
From the running results, we can also see that the ^ operator satisfies the commutative law.

assignment operator (=)

The assignment operator is a great operator that allows you to get a value that you were not happy with before. That is, you can reassign yourself.

compound assignment operator

+=
-=
*=
/=
%=
>>=
<<=
&=
|=
^=

These operators can all be written as composite effects.
For example, a+=b is equivalent to a=a+b.

unary operator

! Logical inverse operation

- negative value

+ positive value

& get address

sizeof the type length of the operand in bytes

~ Bitwise inversion of a number

– prepend, postpend –
++ prepend, postpend++ *
* indirect access operator (dereference operator)
(type) coercion

sizeof (array name) calculates the size of the space occupied by the entire array.
Here we will focus on pre-++ and post-++, – the same as it.
In front of ++, the variable is first added to 1 before use.
After ++, use the value of the variable first, and then add 1.
The dereference operator can get the content pointed to by the pointer.

relational operator

>
>=
<
<=
!= for testing "not equal"

== is used to test for "equality

These relational operators are relatively simple, there is nothing to talk about.

logical operator

&& logical and
|| logical or

&& is true only if both sides are true, otherwise it is false, if the left side is false, the right side will not perform the operation.
|| Both sides are false to be false, and both are true. If the left side is true, the right side will not perform the operation.

comma expression

exp1, exp2, exp3, …expN

Comma expressions are evaluated from left to right, and the value of the last expression is the value of the final expression.
code demo

#include <stdio.h>

int main()
{
    
    
	int num = 0;
	num = (3 + 2, 2 + 4, 5 + 5);
	printf("%d\n", num);
	return 0;
}

operation result
insert image description here

conditional operator

exp1 ? exp2 : exp3

This operator, if exp1 is true, calculates exp2 as the final result, otherwise exp3 is the final result.
For example, we find the maximum value of two numbers:

max = (a > b ? a : b);

Subscript references, function calls and structure members

1.[ ] subscript reference operator
Operand: an array name + an index value
2. ( ) function call operator accepts one or more operands: the first operand is the function name, and the remaining operands are passed parameters to the function.
3. Access the members of a structure
. Structure. Member name.
-> Structure pointer -> member name.

This is the end of today's sharing, thank you for your attention and support.

Guess you like

Origin blog.csdn.net/bushibrnxiaohaij/article/details/131471216