"C and pointers" study notes (5)

This note was written at two o'clock in the afternoon on the 20th, which should be the 23rd consecutive day of work. It is expected to continue to work for another 10 days. When this YP is over, I must give myself a day off.
I helped my tutor finish the project application a while ago, and I have been free recently, but I don't know why I feel agitated today, and I can't read the paper at all, so I have to come here to code to avoid being caught.


This chapter is about operators and expressions


Shift operator
**Left shift: The leftmost digits of the ** value are discarded, and the extra vacancies on the right are filled with 0s.
**Right shift:** The situation of right shift is special and can be divided into logical shift and arithmetic shift. Logical shift, directly use 0 to fill. The padding number of the arithmetic shift is determined by the leftmost digit of the original value.
For example: 10010110 is shifted right by two bits, and the logical shift result is 00100101. The arithmetic shift result is 11100101.
It is important to note here that whether logical or arithmetic shifts are used depends on the compiler. Once a program uses a signed right shift operation, it is not portable .
Effects are unpredictable


Compound operator
Take += as an example.
The advantage of this operator is that it only evaluates the left operand once, which is more efficient.
For example:

a[2*(y-f(x))] = a[2*(y-f(x))]  +1;
a[2*(y-f(x))]  += 1;

In the first format, the expression used to select the value-added position must be written twice. Since the compiler has no way of knowing whether function F has other functions, its value must be calculated twice, making the program slower.


The unary operator
& operator yields the address of its operand. For example, declare an integer variable and a pointer to the integer variable, then use the & operator to get the address of the variable and assign it to the pointer variable.

int a,*b;

b= &a;

The * operator is an indirect access operator, used with pointers to access the value pointed to by the pointer. In the above example, the value of expression b is the address of variable a, and the value of expression *b is the value of variable a.
The sizeof operator determines the type length of its operand, expressed in bytes.
The (type) operator is called a cast operator and is used to convert the value of an expression to a specified type.
++ operator and – operator: Both operators have prefix and suffix forms.
When using the prefix form, the value of the expression is the increased or decreased value of the operand.
When using the postfix form, the value of the expression is the value before the operand is incremented.
In both cases, the operands are operated on and the result is the same.


Logical operators
conditional operators: accept three operators to control the order of evaluation of subexpressions.

expression1?expression2:expression3

First, calculate the value of expression. If the value is true, the value of the entire expression is the value of expression2, and expression 3 will not be solved; otherwise, the value of the entire expression is expression3.


There is no explicit Boolean type in C. Zero is false and any non-zero value is true. So when judging the Boolean value of a variable, do not test whether the entire variable is true by comparing it with any specific value. value, even if it is TRUE or FALSE.


lvalue and rvalue lvalue
represents an address where the result value can be stored, and rvalue specifies a value. When a computer calculates, the result must be stored somewhere on the machine, but the programmer cannot predict where the result will be stored.

int  a ,*pi;

pi =&a;
*pi=20;

The value of pointer pi is the address of a specific location in memory, and the * operator causes the machine to point to that location. When used as an lvalue, this expression specifies the position at which modification needs to be made. When used as an rvalue, it extracts the value currently stored at this location.


That's all. Other content involving priority and overflow will not be described here.

Guess you like

Origin blog.csdn.net/Wangwenshuaicsdn/article/details/127426720