Detailed explanation of operators, take you to the forefront (Part 2)

content

1. Unary 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. Attributes of operators


1. Unary operator

The ternary operator (?:), which I learned before, means that there are three operands.

Example: 3+5

where + is an operator

3 is the left operand

5 is an operand

+ is a binary operator

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

Our common operators are:

 Here we introduce sizeof, ~, ++ and -- in detail

1.sizeof

sizeof is often used to calculate the length of types, such as array types, int, char, short, etc. At the same time, sizeof is an operator, not a function, so the following parentheses can be omitted. However, it cannot be omitted when calculating the length of the type, which is a grammatical 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[10] = { 1,2,3,4,5,6,7 };
	printf("%d\n", sizeof(arr));
	printf("%d\n", sizeof(int[10]));//计算整个数组的长度

	printf("%d\n", sizeof a);
	//printf("%d\n", sizeof int);这样写是会报错的


	return 0;
}

The calculated results are as follows:

2.~

The main function of ~ is to invert the binary bit of a number, but in the same way, the original code is printed at the end

#include<stdio.h>
int main()
{
    int a = 0;
	//~按(内存中补码的2进制)位取反
	//00000000000000000000000000000000
	//11111111111111111111111111111111 - 补码
	//11111111111111111111111111111110 - 反码
	//10000000000000000000000000000001 - 原码 --> -1

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

	return 0;
}

The results are as follows:

 Here is an application of ~:

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;

	//如果只想把倒数第三个数,也就是0改为1,可以进行以下操作
	//00000000000000000000000000001010
	//00000000000000000000000000000100   1<<2(用a与1<<2异或来实现)
	//00000000000000000000000000001110
	//11111111111111111111111111111011   ~(1<<2)(要想将a复原,就是再将倒数第三个数变为0)
	//00000000000000000000000000001010
}

3.++ and --

The main point here is to divide the front and rear

Prefix ++/--: add/subtract before use

Rear ++/--: use first plus/minus 

Each time a is added, it will be recorded, and it is added twice in total, so it is 3.

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

By the time c, a has actually become 2, and c is to add 1 to a and then use its value, so c is 3.

4. * (dereference operator)

#include<stdio.h>
int main()
{
    int a = 10;
	int* pa = &a;
	*pa = 20;//通过*来访问a的地址
	printf("%d\n", a);
	printf("%d\n", *pa);//此时的*pa就是a的值

	int* px = &*pa;//px通过*来访问*pa,也就是访问a
	*px = 30;
	printf("%d\n", a);
    return 0;
}

2. Relational Operators

 3. Logical Operators

(only pay attention to the true and false of the variable)

Let's take a look at a 360 written test question and judge what the output of the program is.

 

Let's look at the first one first, a is used first and then ++, where a is 0, if it is 0, it is false, then don't read the following, it is still the original result, but a is ++ 

 Let’s look at the second type. Let’s use an analogy first. I want Zhang San or Li Si to come to my office. If Zhang San comes, it doesn’t matter if Li Si comes or not. The same is true here. Use a first and then + +, then a=0, it means that no one comes, and no one comes, continue to proceed, b is to use ++ first, b=3, if it is true, it means that someone has come. It doesn't matter, so d is no longer performed, and the final output result is 1334.

 ( To make a small summary: &&--- the left operand is false, the right side is not calculated ||--- the left operand is true, the right side 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);//与上面那段代码等价
    return 0;
}

5. Comma Expression

Comma expressions are multiple expressions separated by commas.
Comma expressions are executed sequentially 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 c in the output below?
    int a = 1;
	int b = 2;
	int c = (a > b, a = b + 10, a, b = a + 1);

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

It's not hard to see that what matters in the end is the result of the last expression.

6. Subscript references, function calls, and structure members

1. [ ] subscript reference operator

Operands: an array name + an index value

 2. ( ) 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.
#include<stdio.h>

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

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

 3. Access a member of a structure

.struct.membername _

-> Structure pointer -> member name

#include<stdio.h>

struct Stu
{
    char name[10];
    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, and interested students can try it out for themselves!

7. Implicit type conversion

E.g:

#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 result of sizeof is 2, where a of type int is converted to type short.

8. Attributes of operators

There are three factors that influence the evaluation of complex expressions:
1. Operator precedence
2. Operator associativity
3. Whether to control the evaluation order
Which of two adjacent operators is executed first depends on their precedence. If both have the same priority, it depends on their associativity.
// The evaluation part of the expression is determined by the precedence of the operator.
// expression1 _
a * b + c * d + e * f

 When calculating, since * has a higher priority than + , it can only be guaranteed that the calculation of * is earlier than + , but the priority is not

Can decide that the third * is executed earlier than the first + .
// expression 2
c + -- c ;
As above, the priority of the operator can only determine that the operation of the subtraction-- is in front of the operation of + , but we have no way to know whether the left operand of the + operator is obtained before or after the right operand is evaluated, So the results are 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 headaches for some small details. Only when we are proficient in these knowledge can we avoid these embarrassing small problems. I hope the detailed explanation of operators can help you!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324324391&siteId=291194637
Recommended