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

C language -- 7 operators

2022-06-10 21:35:00 Try!

1、 arithmetic operator

Arithmetic operators include + - * / %( Add 、 reduce 、 ride 、 except 、 modulus / Remainder )

#include <stdio.h>
// The operator 
int main()
{
    
	int a = 9 / 2;
	printf("%d\n",a);
	return 0;
}

Put statement int a = 9 / 2;printf("%d\n",a); Change it to float a = 9 / 2; printf("%f\n",a);, Run results from 4 Turn into 4.000000, I can't figure it out 4.5, because C Division in language only calculates quotient , Do not calculate the remainder , I can't work out decimals . therefore 9/2=4, If you want to ask for accurate results , You can change any number at both ends of the division sign to a decimal , You can get accurate results .
 Insert picture description here

2、 The displacement operator

Displacement operators include <<( Move left ) and >>( Move right )

int main()
{
    
	int a = 2;
	int b = a << 1;
	printf("%d\n",b);
	return 0;
}

The running result is 4. Because the shift left operator moves bits .a=2----》 Expressed as binary is 10, because a It is stored in the integer , The memory size occupied by integer is 4 Bytes ,4 Bytes are 32 A bit , therefore :
 Insert picture description here
After moving one bit left , The leftmost 0 The loss of , Fill in zero on the far right , Turn into
00000000 00000000 00000000 00000100, Expressed in decimal system 4.( Shift left , The last one will always fill in zero )

3、 Bit operators

Bit operators include Bitwise AND & Press bit or | Bitwise XOR ^

4、 Assignment operator

Assignment operators include := += -= *= /= &= ^= |= >>= <<=

5、 Monocular operators

! Logical anti operation
- negative
+ Comes at a time
& Address fetch
* Indirect access operators ( Dereference operator )
sizeof The type and length of the operator ( In bytes )
~ To reverse the binary of a number
In front of 、 After –
++ In front of 、 After ++
type Cast

explain : about “a+b” Come on ,“+” Yes 2 Operands , Therefore, it is a binocular operator ; The unary operator has only one operand .
(1) Logical anti operation
stay C There are provisions in the language :0 Said the false , Not 0 It's true .

int main()
{
    
	int a = 10;
	printf("%d\n",!a);
	return 0;
}

The running result is :0. because 10 It's true , so !10 For false , So the result is 0.

int main()
{
    
	int a = 0;
	printf("%d\n",!a);
	return 0;
}

The running result is 1. When the false becomes true , There are many real numbers , The specified result is 1.
“!” The real use of :

int main()
{
    
	if (a)
	{
    
		// When a It's true , What to do 
	}
	if (!a)
	{
    
		// When a For false , What else to do 
	}
	return 0;
}

(2)sizeof Is an operator , Used to calculate the size of a variable or type .

int main()
{
    
	int a = 10;
	printf("%d\n",sizeof(int));
	printf("%d\n", sizeof(a));
	return 0;
}

The result of running is 4, because a The type is int type .
printf("%d\n", sizeof(a)); in sizeof The following parentheses can be omitted , signify sizeof It's the operator , It's not a function . But the parentheses after the function cannot be omitted .sizeof To find variables , Brackets can be omitted , But when it comes to types , Brackets cannot be omitted , Such as :sizeof(int). But usually , Use sizeof When , The brackets are not omitted .
reflection :sizeof Can I calculate the size of an array ?

int main()
{
    
	int arr[10] = {
     0 };
	printf("%d\n",sizeof(arr));
	printf("%d\n", sizeof(arr[0]));
	int sz = sizeof(arr) / sizeof(arr[0]);// Count the number of elements 
	printf("%d\n",sz);
	return 0;
}

The running result is :

40
4
10

Because the defined array contains 10 individual 0 Array of elements , And this 10 individual 0 Each of the zero elements is a int type , Every int Types are 4 Bytes , So for the entire array , Its length is 10x4 be equal to 40 individual . For the first element in the array , Its length is 4 Bytes .
(3) According to the not
“~” Reverse by bit , Invert by bit , Invert all binary bits ,1 Turn into 0,0 Turn into 1.

int main()
{
    
	int a = 0;
	printf("%d\n",~a);
	return 0;
}

The running result is :-1
 Insert picture description here
For positive integers , Its original code 、 Complement code 、 The inverse code is the same .
(4) In front of ++、 After ++

int main()
{
    
	int a = 10;
	int b = ++a;// In front of ++: First ++, After use 
	printf("%d\n",a);
	printf("%d\n", b);
	return 0;
}

The running result is :

11
11

In front of ++: First, let a perform ++ operation , And then put this time a The value is assigned to b, So the running results are 11

int main()
{
    
	int a = 10;
	int b = a++;// After ++: First use , after ++
	printf("%d\n", a);
	printf("%d\n", b);
	return 0;
}

The running result is :

10
11

After ++: The first a The value is assigned to b, then a Re execution ++ operation .
(5) Type is put in brackets –》“( type )” Cast to type

int main()
{
    
	int a = 3.14;
	printf("%d\n",a);
	return 0;
}

When running the program , Although not wrong , But it will warn :warning C4244: “ initialization ”: from “double” The switch to “int”, Possible loss of data
take int a = 3.14; Change it to int a = (int)3.14; It can run correctly .

6、 Relational operator

The relationship operator includes :> >= < <= !=( Used for testing “ It's not equal ”) ==( Used for testing “ equal ”)

7、 Logical operators

Logical operators include &&( Logic and )、||( Logic or )

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

The running result is :1, because 3 And 5 It's all true , So the two do “ Logic and ” The result of the operation is also true .
If the b=5 Change it to b=0, The result is 0
take int c = a && b; Change it to int c = a|| b;, Then the running result is still 1.

8、 Conditional operators ( ternary operators )

exp1?exp2:exp3, Meaning for exp1 establish , The calculation exp2, The result of the whole expression is exp2 Result ; if exp1 Don't set up , Then the result of the whole expression is exp3 Result .

int main()
{
    
	int a = 0;
	int b = 3;
	int max = 0;
	if (a > b)
		max = a;
	else
		max = b;
	printf("%d\n", max);
	return 0;
}

In this code if else Structure can be written :

max = a > b ? a : b;

9、 Comma expression

A comma expression is an expression separated by commas exp1,exp2,...,expN, The characteristic of comma expression is that it will evaluate from left to right , The result of the entire comma expression is the result of the last expression .

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

The running result is a3. After running this code ,a=3+2=5;c=5-4=1;b=c+2=1+2=3

10、 Subscript reference 、 Function calls and structure members

[] () . ->

int main()
{
    
	int arr[10] = {
    1,2,3,4,5,6,7,8,9,10};
	printf("%d\n",arr[5]);
	return 0;
}

In this code arr[5] The square brackets of are the subscript reference operators .
Another is when calling a function , After the function name () Is the function call operator , such as printf("hello\n")

原网站

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