# Explain the operators in detail and lead you to the top (Part 2)

catalogue

1. Monocular operator

2. Relational operators

3. Logical operators

4. Conditional operator

5. Comma expression

6. Subscript references, function calls, and structure members

7. Implicit type conversion

8. Operator properties

# 1. Monocular operator

The ternary operator (?:) known before refers to having three operands

For example: 3 + 5

Where + is an operator

3 is the left operand

5 is an operand

+Is a binocular operator

So what is a unary operator, that is, one with only one operand

Our common operators are: Here, let's introduce sizeof, ~, + + and--

1.sizeof

Sizeof is often used to calculate the length of types, such as array types, int, char, short, etc. Meanwhile, sizeof is an operator, not a function, so the following parentheses can be omitted. But you can't save when calculating the type length, which is a syntax requirement.

Here are some common uses of sizeof

```#include<stdio.h>
int main()
{
//sizeof
int a = 10;
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(int));
printf("%d\n", sizeof(float));
printf("%d\n", sizeof(double));

int arr = { 1,2,3,4,5,6,7 };
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(int));//Calculates the length of the entire array

printf("%d\n", sizeof a);
//printf("%d\n", sizeof int); Writing like this will report an error

return 0;
}```

The calculated results are as follows: 2.~

~The main function of is to reverse the binary bit of a number, but similarly, the original code is printed out at last

```#include<stdio.h>
int main()
{
int a = 0;
//~Reverse by (binary complement in memory)
//00000000000000000000000000000000
//11111111111111111111111111111111111111 - complement
//11111111111111111111111111111111111110 - inverse code
//1000000000000001 - original code -- > - 1

printf("%d\n", ~a);

return 0;
}```

The operation results are as follows: Here is a ~ application:

How to restore a number

```#include<stdio.h>
int main()
{
int a = 10;
a |= (1 << 2);
printf("%d\n", a);
a &= ~(1 << 2);
printf("%d\n", a);
return 0;

//If you only want to change the penultimate number, that is, 0 to 1, you can do the following
//00000000000000000000000000001010
//000000000000000000000000000 1 < < 2 (realized by a and 1 < < 2 XOR)
//00000000000000000000000000001110
//11111111111111111111111111111111011 ~ (1 < < 2) (to restore a, change the penultimate number to 0)
//00000000000000000000000000001010
}```

3. + + and--

The main attention here is divided into pre and post

Pre + + / -: Add / subtract before use

Post + + / -: use first and then add / subtract a it will be recorded every time it is added. It has been added twice in total, so it is 3.

For b, the value of a is used first, and then + +, so it is 1

To c, a has actually become 2, and c first adds 1 to a and then its value, so c is 3.

4. * (dereference operator)

```#include<stdio.h>
int main()
{
int a = 10;
int* pa = &a;
*pa = 20;//Access a's address through *
printf("%d\n", a);
printf("%d\n", *pa);//At this point, * pa is the value of a

int* px = &*pa;//px accesses * pa through *, that is, a
*px = 30;
printf("%d\n", a);
return 0;
}``` # 2. Relational operators # 3. Logical operators

(only focus on the true and false variables) Look at a 360 written test question and judge the result of the program output.  Let's first look at the first one. A is used first and then used + +. Here a is 0, and if it is 0, it is false. Then don't look at the later ones. It's still the original result, but a is changed++ Let's look at the second. For example, I want Zhang San or Li Si to come to my office. If Zhang San comes, it doesn't matter if Li Si doesn't come. The same is true here. A uses + + first and then + +, then a=0, which means that no one comes and then continues. b uses + + first and then, b=3. It's true, that is, when someone comes, it doesn't matter whether the latter comes or not Therefore, d will not proceed, and the final output is 1334

(to make a brief summary: & & --- the left operand is false and the right operand is not calculated      ||--- (the left operand is true and the right operand is not calculated)

# 4. Conditional operator

(also called ternary operator -- > Exp1? Exp2: EXP3)

```#include<stdio.h>
int main()
{
int a = 10;
int b = 20;
int max = 0;
/*if (a > b)
max = a;
else
max = b;*/

max = (a > b ? a : b);//Equivalent to the code above
return 0;
}```

# 5. Comma expression

Comma expressions are multiple expressions separated by commas.
Comma expressions are executed from left to right. The result of the entire expression is the result of the last expression.
First, let's analyze a code:
What is the c output below?
```    int a = 1;
int b = 2;
int c = (a > b, a = b + 10, a, b = a + 1);```

Let's look at the formula in parentheses. A > b and a have no effect on a and B, which can be ignored directly. Then a=b+10, where a becomes 12, and finally b=a+1=13. So the result is 13

It is not difficult to find that the result of the last expression works at last.

# 6. Subscript references, function calls, and structure members

1. [   ] Subscript reference operator

Operand: an array name + an index value 2. () 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.
```#include<stdio.h>

{
printf("*************************\n");
printf("******    hello    ******\n");
printf("******   friends   ******\n");
printf("*************************\n");
}

int main()
{
return 0;
}```

3. Visit members of a structure

. structure. Member name

->Structure pointer - > member name

```#include<stdio.h>

struct Stu
{
char name;
int age;
double score;
};

int main()
{
struct Stu s = { "zhangsan",20,85.5 };
//.
printf("%s %d %.1lf\n", s.name, s.age, s.score);
//->
struct Stu* ps = &s;
printf("%s %d %.1lf\n", (*ps).name, (*ps).age, (*ps).score);
printf("%s %d %.1lf\n", ps->name, ps->age, ps->score);
return 0;
}```

The output results of these three cases are the same. Interested students can try it by themselves!

# 7. Implicit type conversion

For example:

```#include<stdio.h>
int main()
{
short s = 20;
int a = 5;
printf("%d\n", sizeof(s = a + 4));
printf("%d\n", s);
return 0;
}```

The output of sizeof is 2, in which a of int type is converted to short type.

# 8. Operator properties

There are three factors that affect the evaluation of complex expressions:
1. Operator priority
2. Associativity of operators
3. Control evaluation sequence
Which two adjacent operators execute first depends on their priority. If they have the same priority, it depends on their combination.
// The evaluation part of the expression is determined by the priority of the operator.
// expression 1
a * b + c * d + e * f

When calculating, because * has higher priority than +, it can only be guaranteed that * is calculated earlier than + but the priority is not

Can decide the third * Than the first + Early execution.
// expression 2
c + -- c ;
As above, the priority of operators can only determine self subtraction-- Operation in + But we can't get it   It is known that the left operand of the + operator is evaluated before or after the right operand, so the result is unpredictable and ambiguous
of
// code 3- illegal expression
int main ()
{
int i = 10 ;
i = i -- - -- i * ( i = - 3 ) * i ++ + ++ i ;
printf ( "i = %d\n" , i );
return 0 ;
}
When we write code, we will inevitably have a headache for some small details. Only when we master these knowledge can we avoid these embarrassing small problems. I hope the detailed explanation of operators can help you!

Tags: C R Language html5

Posted on Wed, 01 Dec 2021 18:46:45 -0500 by Guardian-Mage