“This is the 11th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

๐ŸŽ“ arithmetic operator

๐Ÿ–Š addition operator (“+”)

  • Also known as the binocular operator, that is, there should be two variables involved in the operation, with right associativity. Description: Add two operands.

    #include<stdio.h> int main(void) { printf(“Arabic numerals = %d”,1+1); return 0; }

Arabic Numerals = 2

๐Ÿ–Š subtraction operator (“-“)

  • Also called the binocular operator, but “-” can also be used as a negative operator, in which case it is the unary operator. Description: Subtract the second operand from the first operand.

    #include<stdio.h> int main(void) { printf(“Arabic numerals = %d”,1-1); return 0; }

Arabic Numerals = 0

๐Ÿ–Š multiplication operator (“*”)

  • Also known as the binocular operator, has left associativity. Description: Multiply two operands.

    #include<stdio.h> int main(void) { printf(“Arabic numerals = %d\n”,2 * 2); return 0; }

Note: in math, multiplication (x) is used, while in our programming, it is the asterisk (*).

Arabic Numerals = 4

๐Ÿ–Š division operator (“/”)

  • Also known as the binocular operator, has left associativity. When the amount of computation involved is integer, the result is also integer, minus the decimal. If one of the operations is real, the result is double – precision real. Description: Numerator divided by denominator.

Note: in math we divide (รท), but in our programming we have a slash (/).

The following code to demonstrate: division demonstration

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main(void)
{
	int a = 10;
	int a1 = 10;
	int b = 3;
	float b1 = 3.0;
	int c = a / b;
	float c1 = a / b1;
	printf("Take integer = %d\n", c);
	printf("Take decimal = %lf\n",c1);

	return 0;
}
Copy the code

The above ๐Ÿ‘† code compilation and execution results:

โ€‹

๐Ÿ–Š fetch modulo operator (“%”)

  • Description: Divide the numerator by the denominator, e.g. 8%3 = 2

Note: The modulo operator can only modulo integers, not decimals.

The modulo operator exercise: print out the number from 100 to 200 that can be divided by 2, and count the total number of printings. Code examples are shown below:

#include <stdio.h>
int main(void)
{
	int i;
	int Count = 0;
	for (i = 100; i <= 200; i++)
	{
		if (i % 2 == 0)
		{
			printf("%d  ", i);
			Count++;
		}
	}
    printf("\nCount = %d\n", Count);
    return 0;
}
Copy the code

The above ๐Ÿ‘† code compilation and execution results:

โ€‹

๐ŸŽ“ shift operator

  • The binary left-shift operator (“<<“) that moves all bits of an operand to the left by several bits (the left bits are discarded and the right bits are filled with zeros).
  • Binary right shift operator (“>>”), all the binary bits of a number of right shift several bits, positive left complement 0, negative left complement 1, the right discard. Arithmetical right shifts fill the highest digit, logical right shifts fill the 0. Note: we usually use the arithmetical right shift method!

For example, A = 2, A<<1, the resulting number is: 4

  • 00000000000000000000000000000010
  • 00000000000000000000000000000100

For example, if A = 2, A>>1, the resulting number is: 1

  • 00000000000000000000000000000010
  • 00000000000000000000000000000001

Note: in bitwise moving, we want to move the normal number of digits, you can’t move 100 that’s a big problem

  • If that doesn’t make sense here, check out this post: Source, Inverse and complement _ ze ๅฅ€ blog -CSDN blog

โš  : For shift operators, do not move negative digits, which are not defined in the standard.

  • Such as:

    int num = 2; num << -5; / / error

๐ŸŽ“ bit operator

๐Ÿ–Š bitwise and operator (“&”)

  • Binocular operator. Its function is that the corresponding binary bits of the two numbers involved in the operation are matched. Only when the corresponding two binary bits are both 1, the result bit is 1; otherwise, it is 0. The complement mode that participates in the operation appears.

algorithm

0, 0 = 0; 1 = 0 0 &; 1 & 0 = 0; 1 &1 = 1;Copy the code

For example: 9 & 5, the code looks like this:

#include <stdio.h>
int main(void)
{
	int a = 9;                //00001001 - 9
	int b = 5;                //00000101 - 5
	int c = a & b;            //00000001 - 1
	printf("number = %d\n", c);
    return 0;
}
Copy the code

  • Result: Number = 1

๐Ÿ–Š the bitwise or operator (” | “)

  • Binocular operator. Its function is to participate in the operation of two numbers corresponding to the binary phase or. If either of the two corresponding binary digits is 1, the result bit is 1. Both numbers involved in the operation appear as complement codes.

algorithm

0 | 0 = 0; 0 | 1 = 1; 1 | 0 = 1; 1 | 1 = 1;Copy the code

For example: 9 & 5, the code looks like this:

#include <stdio.h>
int main(void)
{
	int a = 9;                //00001001 - 9
	int b = 5;                //00000101 - 5
	int c = a | b;            //00001100 - 12
	printf("number = %d\n", c);
    return 0;
}
Copy the code

  • Result: Number = 1

๐Ÿ–Š bitwise xOR operator (“^”)

  • Two values involved in the operation, if the two corresponding bits are the same, the result is 0, otherwise 1.

algorithm

0 ^ 0 = 0; 0 ^ 1 = 1; 1 ^ 0 = 1; 1 ^ 1 = 0;Copy the code

For example: swapping a = 9 and b = 5 cannot create a variable to swap! The code looks like this:

#include <stdio.h> int main(void) { int a = 9; int b = 5; Printf (" a = %d b = %d\n", a, b); a = a ^ b; // 1001 ^ 0101 = 1100 (12) b = a ^ b; // 1100 ^ 0101 = 1001 (9) a = a ^ b; // 1100 ^ 1001 = 0101 (5) printf(" a = %d b = %d\n", a, b); return 0; }Copy the code

As we can see from the above code, bitwise xor (^) can be achieved by swapping two numeric values without creating temporary variables.

  • Before switching: A = 9 b = 5 Newline A = 5 b = 9
  • Note: Bitwise operators must all be integers!

๐ŸŽ“ assignment operator

  • The assignment operator is a great way to get a value that you weren’t happy with before. That is, you can reassign to yourself. Consecutive assignments are: right-to-left (mixes โ€ข_โ€ข). Of course, if successive assignments don’t make sense to you, you can also separate them. Such as:
#include <stdio.h> int main(void) { int a = 10; int b = 20; int c = 30; a = b = c + 2; Printf (" continuous assignment :%d\n", a); b = c + 2; a = b; Printf (" separate assignment :%d\n", a); return 0; }Copy the code

The results of successive and separate assignments are the same, but the steps are different!

Continuous assignment The syntax is supported, but it does not support continuous assignment. It would be better if we could disassemble it.

=

Simple assignment operator that assigns the value of the right-hand operand to the left-hand operand

C = A + B will assign the value of A + B to C

+ =

The add and assign operator assigns the result of adding the right-hand operand to the left-hand operand

C plus A is the same thing as C is equal to C plus A

– =

The subtract and assignment operator assigns the result of subtracting the right-hand operand from the left-hand operand to the left-hand operand

C minus A is the same thing as C minus A

* =

Multiply and assign the result of multiplying the right-hand operand by the left-hand operand to the left-hand operand

C times A is the same thing as C times A

/ =

The division and assignment operator assigns the result of dividing the left operand by the right operand to the left

C over A is the same thing as C is equal to C over A

% =

Modulo and assignment operator, find the modulo of the two operands and assign to the left-hand operand

C %= A is the same thing as C = C % A

< < =

Left shift and assignment operator

C <<= 2 is the same as C = C << 2

> > =

Shift right and assign operator

C >>= 2 is the same as C = C >> 2

& =

The bitwise and and assignment operator

C&2 is the same thing as c&2

^ =

Bitwise xOR and assignment operator

C to the 2 is the same thing as C to the 2

| =

Bitwise or and assignment operator

C C = C | of the | = 2 is equal to 2

As long as it is in line with the logic of grammar can be used in this way!

Note: โ‘  (“=”) is assigned, โ‘ก (“==”) is equal sign judgment!

๐ŸŽ“ unary operator

๐Ÿ–Š (“!” Logical reverse operation

Description: Turn the false into the true, and the true into the false. So,! Is a unary operator that has only one operand.

int lis = 1; Printf (" logic is true - %d\n",lis); Printf (" logic false - %d",! lis);Copy the code

  • Logic true = 1
  • Logic false = 0

๐Ÿ–Š positive (“+”) and negative (“-“)

Function: As the name implies, “+” is a positive number, and “-” is a negative number

int a = 10;
a = -a;    // a = -10
a = +a;    // a = 10
Copy the code

๐Ÿ–Š (“&”) takes the address operator

Description: Address is the number of each byte in the memory area. The address is used to find variables by the number of the memory area and then assign the number of its own memory area to the pointer

To print out data in hexadecimal format, %p —- represents hexadecimal data output.

Note: fetching the address is not just fetching the address, this little symbol (“&”) actually does โ‘ข things

  1. One is bitwise and: 1 & 5.
  2. One is to fetch the address.
  3. Another declaration reference, equivalent to defining a variable alias.

๐Ÿ–Š (“*”) dereference operator

Description: Dereferencing a pointer returns the object to which the pointer points, assigning a value to the result of dereferencing. That is, assign to the object to which the pointer points

Pointer variables are used to store addresses

int a = 20; //(1) int *pa = &a; //(2) *pa = 30; / / (3)Copy the code

  1. A Allocates 4 bytes in memory!
  2. Pa specifies that the execution object is of type int!
  3. The *pa dereference operator finds address A by dereferencing the address inside (*pa).

โ™ฆ (sizeof) The type length of the operand

Description: Actually get the storage space occupied by the data in memory, in bytes

int a = 1; printf("%d\n",sizeof(a)); / / (1) printf (" % d \ n ", sizeof (int)); / / (2) printf (" % d \ n ", the sizeof a); / / 3.Copy the code

All three of these are ok.

Note that sizeof is an operator, not a function.

Of course sizeof can also calculate the sizeof an array. Such as:

char arr[10] = {0}; 
Copy the code

An array contains 10 elements, and each element of the array is of type CHAR. A char is a byte. The unit is byte, but it can also be an array type.

Extension: sizeof in parentheses does not take part in the operation!

๐Ÿ–Š (“~”) is reversed by bit

  • Description: For a binary number 0 becomes 1 and 1 becomes 0. Note: the complement is reversed by bit.
int a = -1; / / the original code: 100000000000000000000000000000001 / / radix-minus-one complement: 111111111111111111111111111111110 / / complement: 111111111111111111111111111111111 int  b = ~a; / / / / b = 0 to fetch the assignment / / b (a) take the: 000000000000000000000000000000000Copy the code

๐ŸŽ“ increment and decrement operator

  • There are also two special operators **”++” and “–“** in C. The increment and decrement operators increment and decrement variables by one, respectively. The increment and decrement operators can be placed before or after a variable, which is called a prefix and a suffix. The usage method is as follows:
--operator; // Decrement the prefix operator--; // The decrement suffix operator ++operator; // Increment the prefix operator--; // Autosuffixed operatorCopy the code

In these examples, it doesn’t matter where the operator is before or after, because the result is the same. Minus 1, minus 1, minus 1, plus 1.

Note: Inside the expression, as part of the operator, the two may be used differently. If the operator precedes the variable, the variable increments or decays before participating in the expression. If the operator comes after the variable, the increment or decrement of the variable is completed after the variable has participated in the expression operation.

  1. The increment/decrement suffix operators are: ++/–, used first, then ++/–.
  2. The increment/decrement prefix operators are preceded by ++/–, followed by ++/–.
#include <stdio.h> int main(void) { int a = 1; int b = a++; // autosuffixed operator int d = 1; int c = ++d; // Increment prefix operator printf(" increment prefix operator :%d\n",b); Printf (" increment prefix operator :%d\n", c); return 0; }Copy the code

โ€‹

โ™ฆ (Type) Forcible type conversion

  • Description: Convert a variable from one type to another data type.
Int a = 3.14;Copy the code

โ€‹

The compiler will say warning when I cast!

Int a = (int) 3.14;Copy the code

The compiler does not generate warning, indicating that there is nothing wrong with our program.

๐ŸŽ“ relational operator

The relational operator is used to compare two values and returns a true or false value. Whether a knitted or false value is returned depends on the operator used in the expression. Where the true value is binary (1) and the false value is binary (0), knitting means that the specified relation is valid, and the false value means that the relation specified by the expression is not valid.

>(greater than) >=(greater than or equal to) <(less than) <=(less than or equal to)! == == == ==Copy the code

The relational operators here are easy to understand, but there are a few things to note:

  • In programming, “==” is equal to, and “= “is assignment.
  • “==” does not compare two strings to each other using an equal sign. Use STRCMP

๐ŸŽ“ Logical operator

  • Note: non-0 is true, 0 is false.

๐Ÿ–Š (“&&”) is called the logic and operator

If neither operand is zero, the condition is true. [and]. The following code looks like this:

When true, print: The expression is true

#include <stdio.h> int main(void) { int a = 1; int b = 1; If (a &&b) printf(" result of expression is true \n"); Else printf(" expression result false \n"); return 0; }Copy the code

When false: Prints: Expression result is false

#include <stdio.h> int main(void) { int a = 0; int b = 1; If (a &&b) printf(" result of expression is true \n"); Else printf(" expression result false \n"); return 0; }Copy the code

If both a and B are 0, then the expression is false

๐Ÿ–Š (” | | “) called the Boolean or operator

The condition is true if either of the operands is non-zero. The meaning of [or]. The following code looks like this:

When true, print: The expression is true

#include <stdio.h> int main(void) { int a = 1; int b = 0; If (a | | b) printf (" expression result is true \ n "); Else printf(" expression result false \n"); return 0; }Copy the code

When false: Prints: Expression result is false

#include <stdio.h> int main(void) { int a = 0; int b = 0; If (a | | b) printf (" expression result is true \ n "); Else printf(" expression result false \n"); return 0; }Copy the code

If both a and B are 1, then the expression is true

๐Ÿ–Š (“!” ) is called a logical non-operator

Used to reverse the logical state of the operand. If the condition is true, the logical nonoperator makes it false. For example:

#include <stdio.h> int main(void) { int a = 0; int b = 0; if (! (a | | b)) printf (" expression result is true \ n "); Else printf(" expression result false \n"); return 0; }Copy the code

For example, the above code should have printed expression result false, but it printed expression result true. That’s what logical nonoperators are for, making false values true and vice versa. Note: The precedence of the operator is not logical (!). Is the highest priority at the top.

๐ŸŽ“ Conditional operator

exp1 ? Exp2: exp3 are expressions 1? Expression 2: Expression 3Copy the code

  • The conditional operator is also known as the ternary operator, which has three numbers. Here’s an example:
if(a>b)
{
    max = a;
}
else
{
    max = b;
}
Copy the code

However, C provides a simpler method called the conditional operator, which has the syntax:

Expression 1? Expression 2: Expression 3

The conditional operator is the only ternary operator in C. Its evaluation rule is: if the value of expression 1 is true, the value of expression 2 is used as the value of the entire conditional expression; otherwise, the value of expression 3 is used as the value of the entire conditional expression. Conditional expressions are commonly used in assignment statements.

If else above is equivalent to:

max = (a>b) ? a : b;
Copy the code

If a>b is true, a is assigned to Max, otherwise b is assigned to Max.

โ™ฆ Comma expression

Format: exp1, exp2, exp3,... expnCopy the code

  • A comma expression is an expression separated by commas. Comma expression, executed from left to right, the result of the entire expression is the result of the last expression. This is a very special expression, as follows:
#include <stdio.h>
int main(void)
{
	int a, b=0, c=0;
	int d = (c = 1,a = 1, b -= 2, c += 2);
	printf("%d\n", d);

    return 0;
}
Copy the code

The result from the above example is: **3, ** the result may be affected by the previous code!

Let’s do an exercise that looks at autoincrement and subtraction and comma expressions

#include<stdio.h>
int main(void)
{
	int a, b, c;
	a = 4;
	c = ++a;
	b = ++c, c++, ++a, a++;
	b += a++ + c;
	printf("a = %d -- b = %d -- c = %d\n", a, b, c);
	return 0;
}
Copy the code

Note in the code above: the rules for comma expressions, and the difference between pre – and post-increment operators.

๐ŸŽ“ subscript reference operator

The subscript reference operator is the operator that accesses the subscripts of an array, starting at 0, and so on

For example:

Let's say I take the fifth element of the array name. Int arr [10] =,2,3,4,5,6,7,8,9,10 {1};Copy the code

printf("arr = %d\n",arr[4]);
Copy the code

The โ‘ค element of the array name is found by accessing the index 4 of the array name, as shown above. The square brackets **[]** are subscript reference operators, which find the element of the array name by subscript, and access a specific element.

๐ŸŽ“ function call operator

The () function call operator accepts one or more operands: the first operand is the function name, and the remaining operands are the arguments passed to the function. The parameters of a function are divided into two kinds, the first kind: actual parameters and the second kind: formal parameters.

The arguments that are actually passed to a function are called actual arguments. Arguments can be constants, variables, expressions, functions, and so on. No matter what type of quantity the arguments are, they must have certain values in order to be passed to the parameters when the function is called.

Formal parameters are the variables in parentheses after the function name. They are called formal parameters because they are instantiated only when the function is called (allocating memory units) and only open up memory immediately after the call. Formal parameters are destroyed automatically when the function call is complete. So formal parameters are only valid in functions! The declaration period has a limited range.

For example, Add() is used to Add and subtract ๐Ÿ‘‡

#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int Add(int a, int b) { return a + b; } int main(void) { int a = 0; int b = 0; Printf (" Please enter two numbers :"); scanf("%d %d", &a, &b); int ret = Add(a, b); printf("sum = %d\n", ret); return 0; }Copy the code

  • The code above may run as follows: enter 5, 5, result: 10

๐ŸŽ“ structure member access operator


.** structure **.** Member name, access to the content.Copy the code
  • -> Structure pointer -> member name, pointing to the content of the object

    .

Structs are the same as any other basic data type, such as int or char except that they can be made into whatever data type you want. For future use. In real projects, structures are abundant. Developers often use structs to encapsulate properties to form new types. The function of structure in function is not simple, its main function is encapsulation. The advantage of encapsulation is that it can be reused. Let the user not care what this is, but just use it according to the definition.

Structure basics: These values are called member variables, and each member of a structure can be a different type of variable.

  • I don’t need to describe the structure any more, but you can read this article. Blog.csdn.net/weixin_5263… https://blog.csdn.net/weixin_52632755/article/details/119980420

This article is a structural article written by a blogger, which is a comprehensive explanation of structural knowledge ๐ŸงŠ

๐Ÿช Implicit type conversion

C’s integer arithmetic operators are always at least as accurate as the default integer type. Note: int(integer)

To achieve this precision, both characters and short integer operators in expressions must be converted to integer promotion before they can be used.

๐ŸŽ— Negative integer shaping

char a = -1;
Copy the code

The binary complement of variable A has only eight bits, because 1 byte = 8 bits. Complement, 1111, 1111

The char in the expression needs to be raised before it is used, and the high bit is filled with the symbol bit, that is, 1. So the result of the promotion is: complement 1111 1111 1111 1111 1111 1111 1111 1111

๐ŸŽ— positive plastic

char b = 1;
Copy the code

The binary complement of variable B has only eight bits, because 1 byte = 8 bits. Complement, 0000, 0001

Since char is a signed char, when the integer is promoted, the high level is supplemented by the sign bit, which is 0. So the result is: notice, original, inverse, complement (positive numbers are the same) 0000 0000 0000 0000 0001

๐ŸŽ— shaping lift

#include<stdio.h>
int main(void)
{
	int c = 0xFF;
	char b = 0xF4;
	short a = 0xFE;

	if (a == 0xF1)
		printf("Yes\n");
	if (b == 0xF4)
		printf("Yes1\n");
	if (c == 0xFF)
		printf("Yes2\n");
	return 0;
}
Copy the code

Compiler run result: Yes2

Therefore, it can be concluded from the above example that variable B and variable A are char and short types, so the integer promotion occurs, and the value is also improved, so the if is not performed to determine the contents.

๐Ÿ“ญ Priority size of operands

  1. The precedence of the operator determines the combination of items in the expression. This affects how an expression is evaluated. Some operators have higher precedence than others; for example, multiplication and division operators have higher precedence than addition and subtraction operators.
  2. For example, x = 7 + 3*2. Here, x is assigned 13, not 20, because the operator * has a higher priority than +, so multiply 3*2 first, then add 7.
  3. The following table lists the operators in order of priority, with operators with higher priority appearing at the top and operators with lower priority appearing at the bottom. In an expression, the higher-priority operator is evaluated first.

Note: In the expression often occur such a situation, for example: to do a+b, and then multiply the result with C, accidentally write the expression is a+b* C. Since *(times) takes precedence over +, the multiplication operator is performed first, which is obviously not the desired result. So what should we do at this point? You can use parentheses “()” to raise the + level so that it evaluates first to get the result you expect.

  • Note: the parentheses “()” have the highest precedence in the operator!

category

The operator

associativity

The suffix

() [] ->. ++ –

From left to right

One yuan

+ -! ~ ++ – – (type) * & sizeof

From right to left

,

* / %

From left to right

Add and subtract

+ –

From left to right

shift

<< >>

From left to right

Relationship between

< <= > > =

From left to right

equal

= =! =

From left to right

With the AND

&

From left to right

An exclusive or XOR

^

From left to right

OR the OR

|

From left to right

Logic AND the AND

&&

From left to right

Logic OR the OR

||

From left to right

conditions

? :

From right to left

The assignment

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

From right to left

The comma

.

From left to right

Here’s a question about operator precedence

#include<stdio.h>
int main(void)
{
	int i = 10;
	int j = 20;
	int k = 3;
	k *= i + j;
	printf("k = %d\n", k);
	return 0;
}
Copy the code

Some of you might think that k is equal to 50, but I did the same thing at the beginning, and as I ran it, I realized that k was equal to 90. At that time, I did not understand why this was, but it turned out to be a matter of priority

Note: here + takes precedence over *=! Of course, the above code itself is not very good because it is not readable. **k *= (I +k); ** This increases the readability of code

Code word is not easy, if it is helpful to you, but also please support duck ๐Ÿ’–

โ€‹