First acquaintance with C language 1 (detailed explanation of operators)

content

arithmetic operators

shift operator

bitwise operator

assignment operator

unary operator

relational operator

logical operator

Conditional operator

comma expression

1. Arithmetic operators

+    -    *    /    %

 The arithmetic operators are: plus (+) , minus (-) , multiplication (*) , division (/) , and modulo (%) . Everyone should be very familiar with it, and it is relatively simple. Let me tell you a little bit. I will tell you some of the points that need attention. Plus sign (+) : For addition, you can add integers, characters, and floating-point numbers, as well as the minus sign (-) and the multiplication sign (*) .

The division sign (/) , let's look at the following small example:

3/5 prints the result as 0. Here we will talk about why the result is 0, it should not be 0.6.

3/5, the two operands on both sides of the division sign (/) are integers, so the integer division is performed here, 3/5 is the quotient 0 and the remainder is 3, which is not enough to divide, the quotient is 0, and the remainder is 3. If you replace 3 with 6, the result printed by 6/5 is 1, which is the quotient of 1 and the remainder of 1. This is called integer division

If we don't want to print integers, we want floating-point numbers, let's look at the following small example:

To print a floating-point number, at least one of the operands at both ends of the division sign is a floating-point number, which is the division of floating-point numbers. We get a result of 0.6.

Because one of the two ends of the division sign is a floating point number, we change it to float type. It prints floating point numbers, so use %f.

Modulo (%) :

Both operands of the modulo (%) operator must be integers. Returns the remainder after division.

The above example prints a result of 1.

7 % 3 is the remainder of 7 / 3, and the result of 7 / 3 is the remainder of the quotient 2, so the printed result is 1.

2. Shift operator

(<<) left shift operator (>>) right shift operator

(<<) left shift operator , let's look at the following example:

Shifting a to the left by one bit above is to shift the binary sequence of a by one bit to the left.

The movement rule of the left shift operation is: the left is discarded, and the right is filled with 0.

So after shifting one bit to the left, the value of b is 4.

(>>) right shift operator

There are two right shift operators:

Arithmetic shift right, discard the right, and fill the sign bit on the left.

Logical shift to the right, discarding the right, and adding 0 to the left.

Let's look at the following example:

The binary sequence of a:

00000000  00000000  00000000  00000010

Move one bit to the right:

00000000  00000000  00000000  00000001

Because a is a positive number, whether it is a logical operation or an arithmetic operation, the left side is filled with 0. So the resulting b value is 1.

We can test whether the current compiler uses an arithmetic right shift or a logical right shift. Let's test it with -1:

Negative numbers: -1

we store in memory

Speaking of which, I must give you a knowledge point.

There are three binary representations of integers, namely the original code, the inverse code, and the complement code.

Original code: The binary sequence written directly according to the value is the original code.

Complement code: The sign bit of the original code remains unchanged, and the inverse of other bits is the inverse code.

Complement code: Inverse code +1, which is the complement code.

-1, how do we put it in a? First, we have to write out its original code, then write out its inverse code, and then write out its complement. If -1 is to be stored in memory, it is stored in the form of complement. (This algorithm is for negative numbers, and the original negation of positive integers is the same).

Binary sequence of -1:

Original code: 10000000 00000000 00000000 00000001

Inverse code: 11111111 11111111 11111111 11111110

Complement: 11111111 11111111 11111111 11111111

Now we move -1 one bit to the right. If your current compiler uses an arithmetic right shift, the right side is discarded, and the left side is filled with the original sign bit, then the value you get is still -1. If it is a logical right shift, the right side is discarded. , add 0 to the left, then the value you get is 01111111 11111111 11111111 11111111, which is a relatively large value, and you can calculate it yourself in private.

There is one more thing I want to tell you. We move a one place to the right, and the value of a remains the same. We just put the value of moving a one place to the right into b, and the value of a remains the same.

3. Bit operators

(&) Bitwise AND (|) Bitwise OR (^) Bitwise XOR

(&) bitwise AND , we look at the following example:

    int a = 3;
    int b = 5;
    //& → is the (binary) bitwise AND
    //a → 0000000000000000000000000000011
    //b → 0000000000000000000000000000000000000000000101
    //The corresponding binary bitwise AND, as long as there is 0, the result It is 0, both are 1 and the result is 1
    //The result after bitwise AND → 0000000000000000000000000000001
    int c = a & b;
    printf("%d\n", c);//The print result is 1

(|) bitwise OR , we look at the following example:

    int a = 3;
    int b = 5;
    //| → bitwise OR
    //a → 000000000000000000000000000011     //
    b → 0000000000000000000000000000000000000000000000000000000001 Then
it is 1, and the result of two simultaneous 0s is 0
    //The result after bitwise OR → 000000000000000000000000000111
    int c = a | b;
    printf("%d\n", c);//The print result is 7

(^) Bitwise XOR , we look at the following example:

    int a = 3;
    int b = 5;
    //^ → XOR by (binary) bits
    //a → 0000000000000000000000000000011
    //b → 00000000000000000000000000000000000000000000101
    //The corresponding binary bits are XORed by bit, the same is 0, The difference is 1
    //The result after bitwise XOR → 000000000000000000000000000110
    int c = a ^ b;
    printf("%d\n", c);//The print result is 6

The operands of these three operators must all be integers.

4. Assignment operator

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

(=) assignment operator 

An equal sign (=) is called assignment, and two equal signs (==) are called judgment

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

reassign.

int weight = 120;//weight

weight = 89;//If you are not satisfied, assign a value

double salary = 10000.0; salary = 20000.0;//Use the assignment operator to assign a value.

Assignment operators can be used consecutively, for example:

int a = 10;

int x = 0;

int y = 20;

a = x = y+1;//Continuous assignment (not recommended to write in this form, open and write, concise and easy to understand)

x = y+1;

a = x;

compound assignment operator

int x = 10;

x = x+10;//Simplify it can be written as follows

x += 10;//Compound assignment 

These two ways of writing are completely equivalent.

5. Unary operator

! Logical inverse operation

- negative value

+ positive value

sizeof operand type length in bytes

 ~ Reverse the binary bitwise of a number

-- Front, Rear--

++ pre, post ++

(!) Logical inverse operator 

The operand has only one operator called the unary operator

Let's look at an example:

Only hehe will be printed here because !flag is false.

(!) The logical inverse operator can make false become true, and true become false.

We can test it,

int flag = 0; is false
printf("%d", !flag); The result we print is 1.

Otherwise, the flag is 1, and the !flag print result is 0.

True we use 1, false we use 0.

 - negative value

positive and negative meaning. Look at the small example:

The result of a print is -10.

+ positive value

The writing a = +a is ignorable and useless, so it is omitted.

sizeof operand type length in bytes

Calculates the size of memory occupied by a type or variable.

for example:

The type of a is int, and sizeof(a) can calculate the size of the space occupied by a, in bytes. The two ways of writing in the above example are equivalent, and the print result is 4. We can also write it as sizeof a, without the parentheses. This surface sizeof is an operator, not a function.

sizeof can also calculate the size of an array, see example

 The array arr is of char type, the size of the space occupied by the char type is 1 byte, and the arr array has ten elements, so the result printed above is 10, in bytes. The above two printing methods are equivalent.

Let's look at another example:

What do you think the value printed in the above example should be? I won't sell it to everyone, the printed results are 2 and 5 respectively. Why, although a + 2 is an int type value, s will not become larger because such a value space is placed, and sizeof seeks the size of the space occupied by s, so the first print result is 2 . Why is the second 5, because the expression inside sizeof is not involved in the operation. So the value of a + 2 will not be put into s, and the value of s has not changed.

~ Reverse the binary bitwise of a number

See example:

    // ~ bitwise inversion of the binary bits of a number
    //negative numbers are stored in memory as complement,
    //-1's complement 111111111111111111111111111111
    // ~bitwise inversion
    // the value of a 11111111111111111111111111111111 
    // b's value 000000000000000000000000000000
    int a = -1;
    int b = ~a;
    printf("%d\n", b);//result is 0

(The binary digits in this article are all 32 bits. If there is one less or more than one digit, it is purely the author's hand shaking)

-- Front, Rear--

++ pre, post ++

See example:

Prefix ++, first ++, then use. .

Post ++, use first, then ++.

-- Front and rear -- the same thing.

Another example:

6. Relational Operators

> greater than operator

>= greater than or equal operator

< less than operator

<= Less than or equal operator

!= is used to test for "not equal"

== is used to test for "equality"

See some examples below:

a == b;
a != b;
a < b;
a > b;
a <= b;
a >= b;

 Relational expressions usually return 0or 1, denoting true or false. In C, 0it means false, and all non-zero values ​​mean true. For example, 20 > 12return 1, 12 > 20return 0. Note that the equality operator ==and the assignment operator =are two different operators, so don't confuse them.

Relational expressions are often used in ifOR whileconstructs.

f (x == 3) {   printf("x is 3.\n"); }

 Sometimes, you might accidentally write the following code, which works, but is prone to unexpected results.

if (x = 3) ...

 In the above example, the original intention was x == 3, but it was accidentally written x = 3. This formula represents xassignment to a variable 3, and its return value is 3, so the ifjudgment is always true.

To prevent this error, some people prefer to write variables to the right of the equals sign.

if (3 == x) ...

 In this case, if the ==error is written as =, the compiler will report an error.

// error 
if (3 = x) ...

Another mistake to avoid is that multiple relational operators should not be used together.

i < j < k

In the above example, two less than operators are used consecutively. This is a valid expression and will not throw an error, but it usually fails to achieve the desired result, that is, it is not guaranteed that jthe value of the variable is between iand k. Because the representation operator is evaluated from left to right, the following expression is actually executed.

(i < j) < k

 In the above formula, or is i < jreturned , so the final or is compared with the variable . If you want to determine whether the value of a variable is between and , you should use the following notation.0101kjik

i < j && j < k

7. Logical Operators

&& logical AND

|| logical or

 See the following example:

 &&: AND operator (true if both expressions are true, false otherwise).

||: OR operator (true if at least one expression on either side is true, false otherwise).

For logical operators, any non-zero value represents true, and a zero value represents false. For example, 5 || 0will return 1, 5 && 0will return0

The logical operator also has a feature that it always evaluates the expression on the left first, then the expression on the right, and this order is guaranteed. If the expression on the left satisfies the conditions of the logical operator, the expression on the right is no longer evaluated. This condition is called a "short circuit".

for example:

Because a++ is followed by ++, so use it first, then ++, and a is 0. According to the short-circuit effect, if the value before && is false, the expression after it will not be calculated, so the final result is 1, 2, 3, 4.

This time we changed the value of a to 1, because a is true, according to the short-circuit effect, if the value in front of || is true, the expression after it will not be calculated, so the final result is 2, 3, 3, 5 .

8. Conditional operator

exp1 ? exp2 : exp3 exp (meaning of expression)

Conditional operators are also called ternary operators.

( exp1 ? exp2 : exp3 ) The whole is also an expression.

Meaning: if the result of expression 1 is true, expression 2 evaluates, expression 3 does not, and the result of expression 2 is the result of the entire expression. If the result of expression 1 is false, expression 2 does not count, expression 3 evaluates, and the result of expression 3 is the result of the entire expression. Let's look at the following example:

9. Comma Expression

Comma expressions are evaluated from left to right, and the result of the entire expression is the result of the last expression.

So say c=5, a=a+3 is 6, b=a-4 is 2, c+=5 is 10, so we print d as 10. You can't just count the result of the last expression, it must be done from left to right once.

Guess you like

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