当前位置:网站首页>C language operators

C language operators

2022-07-06 20:32:00 farewell12345

Catalog

Operator classification

One 、 arithmetic operator

Two 、 Shift operator

3、 ... and 、 Bit operators

Four 、 Assignment operator

5、 ... and 、 Monocular operators

6、 ... and 、 Relational operator

7、 ... and 、 Logical operators

8、 ... and 、 Conditional operators

Nine 、 Comma expression

Ten 、 Subscript reference 、 Function calls and structure members

Implicit type conversion

One 、 Improve the overall shape

Two 、 Arithmetic conversion

The properties of the operator


Operator classification

One 、 arithmetic operator

+ - * / %

/: The result of dividing two integers is rounded , for example 3/2 = 1, To get two numbers divided by decimals, at least one of them must be a decimal

%: The two operands of the modulo operator must be integers

Two 、 Shift operator

<<: Shift left operator , Move the binary bit of the operand one bit to the left , The left value is discarded , Right end patch 0

>>: Shift right operator , Move the binary bit of the operand one bit to the right , There are two moving rules

1. Arithmetic shift right : The value on the right is discarded , Fill in the original sign bit on the left , Commonly used arithmetic shift right

2. Logical shift right : The value on the right is discarded , Left complement 0

3、 ... and 、 Bit operators

&: Bitwise AND , According to the binary phase of the operand and , If it's all true, it's true

|: Press bit or , By the binary phase of the operand or , One is true is true

^: Bitwise XOR , XOR by the binary bit of the operand , True and false are different from each other

Bit operators must operate on integers

Bit operators can achieve some magical functions :

1. Calculate the integer corresponding to binary 1 The number of

// Calculate the integer corresponding to binary 1 The number of 
int main()
{
	int a = 0;// Integer to calculate 
	int i = 0;// Count the number of digits 
	int b = 0;// It means that this person is 0 still 1
	int ret = 0;// Store results 
	scanf("%d", &a);
	for (i = 0; i < 32; i++)
	{
		b = a & 1;
		a = a >> 1;
		ret += b;
	}
	printf("%d\n", ret);
	return 0;
}

2. Change the value of a bit in a binary number

// Will be the last 5 Binary bits are composed of 0 Turn into 1
int main()
{
	int a = 10;
	//a Binary system :00000000000000000000000000001010
	a = a | (1 << 4);
	//00000000000000000000000000001010
	//00000000000000000000000000010000
	// Phase or 
	//00000000000000000000000000011010
	printf("%d\n", a);// Output 26
	return 0;
}
// Will be the last 5 Binary bits are composed of 0 Turn into 1
int main()
{
	int a = 10;
	//a Binary system :00000000000000000000000000001010
	a = a & ~(1 << 3);
	//00000000000000000000000000001010
	//00000000000000000000000000001000
	// According to the not 11111111111111111111111111110111
	// Meet each other 
	//00000000000000000000000000000010
	printf("%d\n", a);// Output 2
	return 0;
}

3. Exchange the value of two integers , The third variable... Cannot be used

a^0 = a,a^a = 0, therefore a^b^b = a

int main()
{
	int a = 3;
	int b = 5;
	a = a ^ b;
	b = a ^ b;
	a = a ^ b;
	printf("%d %d\n", a, b);
	return 0;
}

there a^b It is equivalent to a string of keys ,a^(a^b) = b, b^(a^b) = a

Four 、 Assignment operator

Direct assignment :=

Compound assignment :+=、  -=、 *=、 /=、 <<=、 >>= 、%=、&=、|=、^=

5、 ... and 、 Monocular operators

!: Logical anti operation

-: negative

+: Comes at a time

&: Address fetch

sizeof: The type length of the operands , The unit is byte

~: According to the not

--: In front of : Calculate first and then call , After : Call first and then calculate

++: ditto

*: Dereference operator

( type ): Cast

6、 ... and 、 Relational operator

>、 >=、 < 、<= 、!=、 ==

Be careful := It's assignment ,== It's the judgment of equality

7、 ... and 、 Logical operators

&&: Logic and , Yes and , It must be true to be true

||: Logic or , Yes or relationship , If it is true, it is true

Logical and and logical or judge from left to right , When an expression is judged, there is a result , The following statements are no longer executed

example :

int main()
{
	int i = 0;
	int a = 0;
	int b = 2;
	int c = 3;
	int d = 4;
	i = a++ && ++b && d++;
	printf("a = %d\nb = %d\nc = %d\nd = %d\n", a, b, c ,d);
	return 0;
}

The output is 1 2 3 4, because a++ After ,a by 0, 0 It is false to sum with any number , The result has been judged , hinder ++b and d++ No more execution

8、 ... and 、 Conditional operators

That is, the trinocular operator , The format is exp1 ? exp2 : exp3

The execution process is judged first exp1 Is it true , If it is true , perform exp2, If it's false , perform exp3

Nine 、 Comma expression

Is multiple expressions separated by commas , From left to right , The result of the entire expression is the result of the last expression

Ten 、 Subscript reference 、 Function calls and structure members

Subscript reference operator []: Arrays are commonly used , The subscripts of the elements in the array are from 0 Start , Such as arr[4] Represents the second in the array 5 Elements .

Function call operator ()

Structure member access operator : The first way is ., The format is : Structure . Member name . The second way is ->, The format is structure pointer -> Member name  

Implicit type conversion

One 、 Improve the overall shape

C Integer arithmetic operations in languages are always performed at least with the precision of the default integer type , To get this accuracy , Characters and short operands in expressions are converted to normal integers before use , This transformation is called Improve the overall shape .

Usually found in char and short In the operation of type data , Before operation, put char or short Convert to int Type then participates in the operation , Finally, according to the type of output results, we can see whether to truncate .

Integer promotion is promoted according to the sign bit of the data type of the variable .

Two 、 Arithmetic conversion

If the operands of an operator belong to different types , Then unless one of the operands is converted to the type of the other operand , Otherwise, the operation cannot be carried out . The following hierarchy is called ordinary arithmetic conversion .

// From top to bottom, from high to low

long double

double

float

unsigned long int

long int

unsigned int

int

Such as 5/2.0f, When calculating, first set the integer 5 Convert to float Type then participates in the operation

The properties of the operator

There are three factors that affect the evaluation of complex expressions

1. The priority of the operator

2. The associativity of operators

3. Whether to control the order of evaluation

  Let's look at the priority first , If the priority is the same , Look at their combination .

原网站

版权声明
本文为[farewell12345]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131207469086.html