Operators -- small operators are powerful

Posted by sheen.andola on Wed, 22 Dec 2021 21:14:56 +0100

Arithmetic operators:

+ - * / %

\1. In addition to the% operator, several other operators can act on integers and floating-point numbers
%The two operands of the operator must be integers. Returns the remainder after division
%The divisor of the operator cannot be 0, otherwise an error will be reported

#include <stdio.h>
int main()
{
	printf("%d", -99 % 0);
	return 0;
}

2. For the / operator, if both operands are integers, perform integer division As long as there are floating-point numbers, it is floating-point division

#include <stdio.h>
int main()
{
	printf("%d", 3/2);//The printed result is 1. Because it is an integer division, it needs to be printed with% d, and the printed result of% f is 0.0000000
	return 0;
}
#include <stdio.h>
int main()
{
	printf("%f", 3.0/2);//Or 3 / 2.0 or 3.0 / 2.0, the result is 1.500000, and the default precision is 6 bits
//Because it is a floating-point division, use% f. if you use% d, the print result is 0
	return 0;
}

Shift operator:

< < shift left operator
>>Shift right operator

Note: the operands of the shift operator can only be integers, moving binary bits

Inverse complement of original code

First, let's understand a knowledge point

The numbers stored in the computer are complements

**There are three binary representations of an integer: * * original code, inverse code and complement code

We stipulate that the original inverse complement of positive numbers is three in one, which is the same

For the original inverse complement of negative numbers

If int a = -1; Int is four bytes

Original code: binary sequence written directly according to numerical value
00000000000000000000000000000001
Inverse code: the sign bit of the original code remains unchanged, and other bits are reversed by bit
011111111111111111111111111111111110
Complement: inverse code + 1
011111111111111111111111111111111111

< < shift left operator (moving complement)

Discard on the left and fill 0 on the right

#include <stdio.h>
int main()
    int a = 1;
	printf("%d", a << 2);
//00000000000000000000000000000001
//00000000000000000000000000000100
//The left is lost, the right is filled with 0, and the right is shifted by 2 bits, so the printed result is 4
	return 0;
}

>>Shift right operator (moving complement)

The shift right operator has two displacement modes

1. Arithmetic displacement

Discard the number on the right and fill in the original symbol bit on the left

2. Logical displacement

Shift right is lost, and fill 0 on the left

#include <stdio.h>
int main()
{
	int a = -1;
	printf("%d", a >> 2);
//The printed result is still - 1
//It shows that the arithmetic displacement is carried out in this compiler
	return 0;
}

Note: when a is a negative number of signed integer, the result of displacement operation varies with the compiler. In many compilers, logical operation or arithmetic operation will be carried out. Do not shift the negative number. No matter which method is adopted, the portability of the program will be reduced

Note: a < < - 5 standard is not defined, which will produce unknown results, so it should be written as a > > 5

Bitwise operators:

&Bitwise and by (complement binary)
|By bit or by (complement binary) bit and
^Bitwise XOR (complement binary) and

Note: their operands must be integers

&/ / bitwise AND

Rule: 1 and 1 are 1, 1 and 0 and 0 and 0 are both 0

#include <stdio.h>
int main()
{
	int a = 1;
	int b = 2;
	int c = a & b;
	printf("%d", c);//The print result is 0
	return 0;
}
//Binary bit of a 00000000000000000000000000000000000001
//Binary bit of b 000000000000000000000000000000000000000000 10
//Binary bit of c 000000000000000000000000000000000

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-rjuxfyfd-1639971909492) (data: image / GIF; Base64, r0lgodlhaqabapabap / / / wuaaach5baekaaaaaaaaaaaaaaaaabaaaeaaaicraeaow = =)]

|Bitwise OR

Rule: 1 and 1 and 1 and 0 are 1, 0 and 0 are 0

#include <stdio.h>
int main()
{
	int a = 1;
	int b = 2;
	int c = a | b;
	printf("%d", c);//The printed result is 3
	return 0;
}
//Binary bit of a 00000000000000000000000000000000000001
//Binary bit of b 000000000000000000000000000000000000000000 10
//Binary bit of c

^Bitwise XOR

Rule: 1 and 1 and 0 and 0 are 0, only 1 and 0 are 1

#include <stdio.h>
int main()
{
	int a = 1;
	int b = 2;
	int c = a ^ b;
	printf("%d", c);//The printed result is 3
	return 0;
}
//Binary bit of a 00000000000000000000000000000000000001
//Binary bit of b 000000000000000000000000000000000000000000 10
//Binary bit of c

Assignment operator:

=(+ = - = * = / =% = > > = < = & = | = ^ =) compound assignment assignment

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

int a = 1;

a=20;//Assign a value

Assignment operators can be used continuously:

int a = 10;
int x = 0;
int y = 20;
a = x = y+1;//continuous assignment 
//Assign the value of y+1 to x, and then assign x to a

However, in order to facilitate debugging, we usually open the assignment to write

int a = 10;
int x = 0;
int y = 20;
x = y+1;
a = x;//This code looks much simpler

The compound assignment character + =, and other effects are similar. I won't introduce it too much

int x = 10;
x = x+10;
x += 10;//Make the code look more concise

Monocular operator:

! Logical reverse operation
-Negative value
+Positive value
&Get address
Type length of sizeof operand in bytes
~Bitwise negation of a number
– front, rear –
++Front and rear++
*Indirect access operator (dereference operator)
(type) cast

! Logical reverse operation

Change true (non-0) to false (0), and false (0) to true (non-0, default to 1)

#include <stdio.h>
int main()
{
	int a = !0;
	printf("%d", a);//The result is 1
	return 0;
}
#include <stdio.h>
int main()
{
	int a = !1;
	printf("%d", a);//The result is 0
	return 0;
}

-, + negative, positive

-1 is a negative number and + 1 is a positive number. The positive and negative numbers are usually omitted

&Get address and * indirect access operator (dereference operator)

#include <stdio.h>
int main()
{
	int a = 10;
	printf("%p\n", &a);//%p prints the address, & A gets the address of a, and prints the address of A
	int* pa = &a;amount to int*pa; pa=&a;there*Is the definition of a pointer, a binocular operator
    //It means to define an int type pointer, and then assign a value to p with the address of a;
	*pa = 20;//*The * of pa is the dereference operator. Find the address of a through * pa, and then assign it to 20
	printf("%d", *pa);//Print out as 20
	return 0;
}

Type length of sizeof operand in bytes

Its return type is an unsigned integer

#include <stdio.h>
int main()
{
	int a;
	printf("%d\n", sizeof a);//The size of the operand can be calculated without parentheses, and the print result is 4
	printf("%d\n", sizeof(int));//Brackets must be used when calculating the size of data type, and the print result is 4
	return 0;
}//This just shows that sizeof is not a function

Let's take a closer look at the sizeof operator

#include <stdio.h>
void test1(int arr[])
{
printf("%d\n", sizeof(arr));//The result is 4 or 8, because the array parameter is equivalent to passing the address of the first element of the array
}//It is equivalent to passing a pointer int*arr, so it is 4 or 8
void test2(char ch[])
{
printf("%d\n", sizeof(ch));//The result is 4 or 8
}
int main()
{
int arr[10] = {0};
char ch[10] = {0};
printf("%d\n", sizeof(arr));//The result is 40. Because the int type is 4 bytes and the element is 10, the array size is 40
printf("%d\n", sizeof(ch));//The result is 10. Because the char type is 1 byte and the elements are 10, the array size is 10
test1(arr);
test2(ch);
return 0;
}

~The binary bit negation of a number (the binary bit of a complement)

All bits are reversed bit by bit, including sign bits

#include <stdio.h>
int main()
{
	int a = 1;
	int b = ~a;
	printf("%d\n",b);//The result is - 2,
    //The complement of a is 00000000000000000000000001
    //b after inversion is 11111111111111111111111111111111111111111111111111111111110
    //The value of the original code of b is 1000000000000000000010
	printf("%d\n", a);//The result is 1, indicating that inversion does not change the value of a
	return 0;
}

– front, rear –
++Front and rear++

The former is to add 1 (or subtract 1) before use, and the latter is to use first and then add 1 (or subtract 1)

#include <stdio.h>
int main()
{
	int a = 1;
	int b = 1;
	printf("%d\n", ++a);//The result is 2, because the pre + + is added by 1 before use
	a = 1;//Assign a to 1 because + + changes the value of A
	printf("%d\n", a++);//The result is 1 because post + + is used first and then added by itself
	printf("%d\n", --b);//The result is 0, because the leading -- is to subtract 1 before using
	b = 1;//Assign a to 1 because -- changes the value of A
	printf("%d\n", b--);//The result is 1, because the post - is to use the hand to subtract 1 first
	return 0;
}

**Note: * * this operator will change the value of the operand

(type) cast

#include <stdio.h>
int main()
{
	int a = 3.14;
	printf("%d\n", a);//The result is 3. Although it can be compiled normally, a warning will appear
	return 0;
}

When we use int a = (int)3.14;

Although the result is still 3, the warning will disappear

Relational operators:

> >= < <= != ==

Compares the size of two operands

Note: be careful not to write = = and = inversely during programming

Logical operators:

&&Logic and
||Logical or

&&Logic and

The result is true only if the operands on both sides of the operator are true

#include <stdio.h>
int main()
{
	printf("%d\n", 1&&0);//The result is 0
	return 0;
}
#include <stdio.h>
int main()
{
	printf("%d\n", 1&&2);//The result is 1
	return 0;
}

It controls the evaluation order

#include <stdio.h>
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);//The printed results are 1,2,3,4
  return 0;
}

This is because when a + + & + + B, a is already 0, and the subsequent instructions do not continue to be executed

||Logical or

As long as one side is true, the result is true. It is false only when both sides are false

#include <stdio.h>
int main()
{
	printf("%d\n", 1||0);//The result is 1
	return 0;
}
#include <stdio.h>
int main()
{
	printf("%d\n", 0||0);//The result is 0
	return 0;
}

It can also control the evaluation order

#include <stdio.h>
int main()
{
	int i = 0, a = 1, b = 2, c = 3, d = 4;
    i = a++||++b||d++;
	printf("a = %d\nb = %d\nc = %d\nd = %d\n", a, b, c, d);//The print results are 2,2,3,4
	return 0;
}

When the current a + + |+ + B, a + + is true, and the subsequent instructions are not executed

Conditional operator:

exp1 ? Exp2: EXP3 (also known as ternary operator)

Execute exp2 when exp1 is true, otherwise execute exp3

#include <stdio.h>
int main()
{
	int a = 1;
	int b = 2;
	printf("%d\n",a>b?a:b);//The result is 2. Since a > b is false, execute B
	return 0;
}

Comma expression:

exp1, exp2, exp3, ...expN

Comma expressions are multiple expressions separated by commas
Comma expression, executed from left to right. The result of the entire expression is the result of the last expression

#include <stdio.h>
int main()
{
	int a = 1;
	int b = 2;
	int c = (a += a, b += a, c = a + b);
	printf("%d\n", c);//The result is 6 and the result is c=a+b
	return 0;
}

Subscript references, function calls, and structure members:

[] subscript reference operator

() function call operator

To access members of a structure:

**. Structure** member name
**->Structure pointer - > * * member name

[] subscript reference operator

#include <stdio.h>
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	printf("%d\n", arr[1]);//Two [] don't mean the same thing
	//The first is the separator when creating an array. The second is the reference operator in the following table, which is used to find the elements of the array through the subscript of the array
	//[] has two operands, array name and subscript
	return 0;
}

() function call operator

Accept one or more operands. The first operand is the function name, and the remaining operands are the parameters passed to the function

printf("%d",10);//printf is the function name of the first operand

To access members of a structure:

. Structure member name

#include <stdio.h>
struct person
{
	char name[20];
	int age;
	float weight;
    //Name, age and weight are structure member names (structure variables)
};//Create a structure

int main()
{
	struct person jake = { "jake",25,65.5 };//jake is the structure variable name
	printf("%s %d %.1f", jake.name, jake.age, jake.weight);
    //Through the structure variable name Structure member name to access
    //The printed result is jake 25 65.5
	return 0;
}

->Structure pointer - > member name

#include <stdio.h>
struct person
{
	char name[20];
	int age;
	float weight;
};//Create a structure

int main()
{
	struct person jake = { "jake",25,65.5 };
	struct person* p = &jake;//Create a structure pointer and store the address of structure jake in p
	printf("%s %d %.1f", p->name, p->age, p->weight);//name,age,weight of the object pointed to by p
    //The printed result is jake 25 65.5
	return 0;
}

Topics: C C++ Back-end