C Language Chapter 4

Source: Internet
Author: User
Tags bitwise bitwise operators joins

Directory of this document

    • First, arithmetic operators
    • Second, assignment operator
    • The increment operator and the self-decrement operator
    • Iv. sizeof
    • Five, comma operator
    • Vi. Relational operators
    • Seven, logical operators
    • Eight or three mesh operator
    • Nine, bitwise operators

The basic ability of a computer is computing, so the computational power of a programming language is very important. C is omnipotent because it has not only a rich data type, but also powerful computational power. There are 34 types of operators in C, including common subtraction operations. This is a detailed description of the operators in the C language.

Back to top one, arithmetic operators

Arithmetic operators are very simple, that is, some subtraction operations in elementary mathematics. However, there are some grammatical details to be aware of.

1. Addition operator +
1 int a = 10;2 3 int b = A + 5;

In line 3rd, the addition operator + is used to add, and then assign to the variable B, the value of the final variable B is 15

2. Subtraction operator or negative operator-
1 int b = 10-5;2 3 int a =-10;

1> in line 1th using the subtraction operator-the subtraction operation, and then assigning the difference to the variable B, the value of the final variable B is 5

2> in line 3rd, this-not a subtraction operator, but a negative operator-10 for negative 10

3. Multiplication operator *
1 int b = 10 * 5;

Note: The multiplication operator is not x or X, but an asterisk *. The final value of variable B is 50.

4. Division operator/
1 Double A = 10.0/4;2 double b = 10/4;3  4 printf ("a=%f, b=%f \ n", A, b);

Note: The division operator is not ÷, but a forward slash/

1> 1th in line 10.0 is a floating point type, 4 is an integral type, so the 4 automatic type is promoted to float and then the operation, the value of the last variable B is 2.5

2> the 2nd row of 10 and 4 are integral types, the operation of the computer has a principle: the same data type of the value of the operation, and the result of the operation is still the same data type. As a result, integers, in addition to integers, are still integers, losing fractional parts. The value of the last variable B is 2. To view the results of the output:

3> you can cast an integer to floating-point data if you want the integer to have no loss of precision except for integers

1 Double A = (double) 10/4;2 3 double b = ten/(double) 4;4 5 double c = (double) Ten/(double) 4;6 7 Double d = (double) (1 0/4);
    • As long as there are 1 strong to float data between 10 and 4, the other 1 integers are automatically type promoted to floating-point data. Therefore, the values of variables a, B, and C are all 2.5.
    • The case of variable d is different, the 7th line of code means that the value of (10/4) is calculated first, and then the value of (10/4) is strongly converted to floating-point data. (10/4) The value is 2, the 2 to the floating-point data, that is not 2? So, the value of the variable D is 2.

5. Modulo operator or call remainder operator%

Note: This% is not division sign ÷, it is a take-up operator, or a modulo operator. The remainder is obtained after dividing two integers. For example, the remainder of 5 except for 2 is the remainder of 1,5 except 3 is 2. So there is a principle of using this%: the% must both be integers. The following syntax is incorrect:

1 int a = 5.0 2;

The compiler will direct an error because 5.0 is not an integer.

1> positive Remainder
1 int a = 5 2;2 int b = 2 5;

A simple calculation is available: The value of variable A is 1, and the value of variable B is 2

2> Negative values

The remainder that is calculated by using% is a positive or negative number, which is determined by the dividend on the left, the divisor is a positive number, the remainder is a positive number, and vice versa. So the values of variables a, B, and C are-1, 1,-1, respectively.

6. Sequence of operations 1> arithmetic expressions

A formula that uses arithmetic operators to concatenate data, called an arithmetic expression. such as a + B, 10 * 5 and so on. If the expression is more complex, then pay attention to its order of operations. The order of operations of an expression is based on the union direction and priority of the operator.

2> Combination Direction

The arithmetic operators are bound from left to right. For example, Expression 2+3+4, calculate 2+3 first.

3> Priority level

The higher the priority, the more first the operation, when the priority is the same, the reference combination direction. The following is the precedence ordering of arithmetic operators:

Negative operator (-) > Multiply (*), except (/), modulo (%) operator > plus (+), minus (-) operator

For example, the expression 4+5*8/-2 is evaluated in the following order:-, *,/, +, and the final result is-16

4> parentheses

If you need to calculate a lower priority first, you can use parentheses () to enclose the parentheses with the highest precedence!

    • For example 4+5*8-2 The default calculation order is: *, +,-
    • If you want to perform the addition operation first, you can write this: (4+5) *8-2, the final result is not the same

Back to top two, assignment operators

There are two types of assignment operators: the simple assignment operator and the compound assignment operators.

1. Simple assignment operator =1> simple usage

In fact, this equals = has been seen from the beginning of the argument, its function is to assign the right value to the left.

1 int a = 10 + 5;

The assignment operators are combined in a right-to-left direction and have a lower precedence than arithmetic operators. Therefore, the Equals = Right addition operation is performed before the result is assigned to the variable to the right of the equal sign. The value of the last variable a is 15.

2> Continuous Assignment
1 int A, b;2 3 A = b = 10;
    • The variables a, b, of type int are defined in line 1th, respectively.
    • The 3rd line of code means: Assign 10 to the variable B, and then assign the value of variable B to a. So the value of the last variable A and B is 10.

3> Use note

equals = The Left can only be a variable, not a constant! Constants are immutable, how can they be assigned again? The following syntax is incorrect:

1 10 = 10 + 5;

2. Compound assignment operator
    • + = Plus assignment operator. such as a + = 3+2, equivalent to a = A + (3+2)
    • -= minus assignment operator. such as a-= 3+2, equivalent to A = A-(3+2)
    • *= multiply the assignment operator. such as a *= 3+2, equivalent to a = A * (3+2)
    • /= In addition to the assignment operator. such as a/= 3+2, equivalent to A = A/(3+2)
    • %= the value of the remainder assignment operator. such as a%= 3+2, equivalent to a = a% (3+2)

Back to top three, the increment operator and the decrement operator 1. Introduction
    • + + auto-increment operator. such as A++,++a, are equivalent to a = a+1
    • --the self-decrement operator. such as a--,--a, are equivalent to a = A-1

Note: It is wrong to write a 5++ because 5 is a constant.

The difference between 2.++a and a++

1> when using ++a and a++ alone, they are no region

1 int a = 10;2 a++;
1 int a = 10;2 ++a;

The effect of the above two pieces of code is to get a value of +1, the last a value is 11

2> the following situation, ++a and a++ are different.

1 int a = 10;2 3 int b = ++a;
1 int a = 10;2 3 int b = a++;

The result of the execution of the above two pieces of code is different.

    • The 1th code: ++a means to perform a +1 operation on a, and then assign a value to B. So the last value of a and B is 11.
    • The 2nd code: a++ means that the value of a is copied out a copy, and then a to perform +1 operations, so a becomes 11, but the copied value or the 10,a++ operation is finished, the copied value 10 is assigned to B, so the value of the last variable B is 10, the value of variable A is 11

The difference between--a and a--is the same.

3> to see a more tricky example

1 int a = 10;2 3 a = a++;

A lot of people at a glance, think that the value of the last a should be 11, in fact, the value of the last A is 10. It has been said that the role of a++, and here is the same. First copy the value of a, and then a +1 operation on a, so a becomes 11, but the copied value or the 10,a++ operation is finished, and then the copy of the value of 10 is assigned to a, so the last variable a value is 10

Go back to the top four, sizeof

* sizeof can be used to calculate the number of bytes of memory for a variable or a constant, a data type.

int size = sizeof;p rintf ("10 Bytes:%d", size);

Output: 10 is data of type int, in 64bit compiler environment, int type needs to occupy 4 bytes

* sizeof has 3 different forms

    • sizeof (variable \ constant)
sizeof (+); char c = ' a '; sizeof (c);
    • sizeof variable \ constant
sizeof 10;char C = ' a '; sizeof C;
    • sizeof (data type)
sizeof (float);

Note that it is not possible to write sizeof float;

Back to top five, comma operators

* The comma operator is primarily used for connection expressions, such as:

1 int a = 9;2 int b = 10;3 4 A = a+1, B = 3*4;

* An expression concatenated with the comma operator is called a comma expression, and its general form is:

Expression 1, expression 2, ..., expression n

The operation of the comma expression is: From left to right, evaluates expression 1, evaluates expression 2, ..., and evaluates expression n

* The comma operator is also an operator, so it also has an operation result. The value of the entire comma-expression is the value of the last expression

1 int a = 2;2 int b = 0;3 int c;4 5 c = (++a, a *= 2, B = A * 5); 6 7 printf ("C =%d", c);

The result of ++a is 3,a *= 2 results for 6,b = A * 5 result is 30. As a result, the output is:

It is important to note that the right expression is wrapped in parentheses (), if not enclosed in parentheses, that is:

1 c = ++a, a *= 2, b = A * 5;2 printf ("c =%d", c);

The output will be: because C = ++a is also part of the comma expression and is independent of the subsequent a *= 2 and B = A * 5

Back to top six, relational operator 1. " True "and" false "

1> by default, the correct code that we write in the program is executed. But most of the time, we want to execute a piece of code when a condition is set. For example, this interface:

If the user clicks the Register button, we execute the "Jump to registration Screen" code, if the user clicked the login button, we will execute "jump to the Login Interface" code. If the user does not do anything, the preceding two-segment code is not executed. In this case, you can use conditional statements, but instead of learning conditional statements, let's take a look at some more basic knowledge: How to judge a condition as not being true. If this is not judged, what code is executed.

2> in C language, the condition is called "true", the condition is not established called "false", therefore, the judgment condition is established, is the judgment condition "true and false". How to judge the true and false? The C language stipulates that any value other than 0 is "true" and only 0 is "false". In other words, 108, 18, 4.5, 10.5, etc. are all "true" and 0 are "false".

2. Simple use of relational operators

The C language also provides some relational operators that can be used to compare the size of two numeric values.

    • < less than. Like A<5.
    • <= is less than or equal to. Like a<=5.
    • > Greater than. Like A>5.
    • >= is greater than or equal to. Like a>=5.
    • = = equals. Like a==5.
    • ! = is not equal to. Like a!=5.

Relational operators result in only 2 of the results: if the condition is true, the result is 1, or "true", and if the condition is not true, the result is 0, or "false".

1 int a1 = 5 > 4; 3 int a2 = 5 < 4; 0

3. Use of relational operators note

The precedence of the = =,! = in the 1> relational operator is equal,<, <=, >, >=, and the precedence of the former is lower than the second

For example 2==3>1: First Count 3>1, the condition is established, the result is 1. The 2==1 is recalculated, the condition is not tenable, the result is 0. So the result of 2==3>1 is 0.

The 2> relationship operator is in the direction of "left to right"

For example 4>3>2: First Count 4>3, the condition is established, the result is 1. Compared with 2, namely 1>2, the condition is not tenable, the result is 0. So the result of 4>3>2 is 0.

The precedence of the 3> relational operator is less than the arithmetic operator

For example 3+4>8-2: Calculates the 3+4 first and the result is 7. The calculation is 8-2 and the result is 6. Finally, the 7>6 is calculated and the condition is set, the result is 1. So the result of 3+4>8-2 is 1.

Back to top seven, logical operators

Sometimes, we need to set up a number of conditions at the same time to execute a piece of code, such as: the user only input QQ and password, to execute the login code, if only entered QQ or only entered a password, you can not execute the login code. In this case, we are going to use the logical operators provided by the C language.

The C language provides 3 logical operators:&& (logic and), | | (logical OR),! (logical not). Note: These are English characters and do not write in Chinese. As with relational operators, the result of a logical operation is only 2: "True" is 1, "false" is 0

1.&& logic and 1> use format

"Condition a && condition B"

2> operation Results

Only if both condition A and condition B are established, the result is 1, which is true, and the rest is 0, or false. Therefore, if a condition A or condition B is not tenable, the result is 0, that is, "false"

3> Operation Process
    • Always first determine if condition A is established
    • If condition A is established, then the condition B is determined: If condition B is established, the result of "condition a && condition B" is 1, i.e. "true", if condition B is not established, the result is 0, i.e. "false"
    • If condition A is not established, it will not be judged whether the condition B is established: Since condition A is not established, the result of condition a && condition B must be 0, that is, "false", regardless of condition B.
4> Example

The combination of logic and direction is "from left to right". such as Expressions (a>3) && (a<5)

    • If the value of a is 4: First Judge A>3, set up, and then Judge A<5, also established. So the result is 1.
    • If the value of a is 2: First Judge A>3, not set up, stop judging. So the result is 0.
    • Therefore, if the value of a is within the range of (3, 5), the result is 1; otherwise, the result is 0.
5> Note
    • If you want to determine if the value of a is within the range of (3, 5), you must not write 3<a<5, because the relationship operator is "left to right". For example A is 2, it will first calculate 3<a, that is, 3<2, the condition is not set, the result is 0. Compared with 5, namely 0<5, the condition is set up, the result is 1. Therefore, the result of 3<a<5 is 1, the condition is established, that is, when a is a value of 2 o'clock, the value of a is within the range of (3, 5). This is obviously wrong. The correct way to judge is: (a>3) && (a<5)
    • C language provisions: Any non-0 value is "true", only 0 is "false". Therefore logic and also apply to numerical values. For example, 5 && 4 result is 1, "true", -6 && 0 result is 0, "false"

2.| | Logical OR 1> use format

"Condition A | | Condition B "

2> operation Results

When condition A or condition B is set up (also including condition A and condition B), the result is 1, which is true, and only if condition A and condition B are not set, the result is 0, which is "false".

3> Operation Process
    • Always first determine if condition A is established
    • If condition A is established, it will not be judged whether the condition B is established: Because condition A has been established, regardless of condition B, "condition A | | The result of condition B must be 1, which is "true."
    • If condition A is not established, then determine if condition B is true: If condition B is established, "condition A | | Condition B "results in 1, that is," true ", if condition B is not set, the result is 0, i.e." false "
4> Example

The logical or binding direction is "from left to right". such as expressions (a<3) | | (a>5)

    • If the value of a is 4: First Judge A<3, not set up, and then Judge A>5, also does not establish. So the result is 0.
    • If the value of a is 2: First Judge A<3, set up, stop judging. So the result is 1.
    • Therefore, if the value of a is within the range (-∞, 3) or (5, +∞), the result is 1; otherwise, the result is 0.

5> Note

C language provisions: Any non-0 value is "true", only 0 is "false". So logic or also applies to numeric values. such as 5 | | 4 The result is 1, for "true";-6 | | 0 The result is 1, for "true"; 0 | | The result of 0 is 0, which is "false"

3.! Logical non-1> use format

“! Condition a "

2> operation Results

In fact, the condition A is reversed: if condition A is established, the result is 0, that is, "false"; if condition A is not established, the result is 1, that is, "true". That is to say: true false, false become true.

3> Example

The logical non-union direction is "from right to left". Like an expression! (a>5)

    • If the value of a is 6: First Judge A>5, set up, and then reverse the result is 0
    • If the value of a is 2: First Judge A>3, not set, and then take the result of the reverse is 1
    • Therefore, if the value of a is greater than 5, the result is 0; otherwise, the result is 1.
4> Note
    • Logical non-operators can be used multiple times:! (4>2) The result is 0, is "false",!! (4>2) The result is 1, is "true",!!! (4>2) The result is 0, which is "false"
    • C language provisions: Any non-0 value is "true", only 0 is "false". Therefore, a non-0 value is not logical! The result of the operation is 0, the value of 0 is logical non! The result of the operation is 1.! 5,!6.7 、!-9 results are 0,!0 results of 1

4. Priority level

The order of precedence for logical operators is: parentheses () > minus sign->! > Arithmetic operators > Relational operators > && > | |

    • An expression! (3>5) | | (2<4) && (6<1): Calculate first! (3>5), (2<4), (6<1), the result is 1, the equation becomes 1 | | 1 && 0, then 1 && 0, the formula becomes 1 | | 0, the final result is 1.
    • Expression 3+2<5| | 6>3 equivalent to ((3+2) < 5) | | (6>3) with a result of 1
    • Expression 4>3 &&!-5>2 equivalent to (4>3) && ((! ( -5)) > 2) with a result of 0

Back to top eight or three mesh operator 1. N-Mesh operator
    • Like a logical non (!), minus sign (-) a symbol that joins only one data, called the "monocular operator", such as! 5,-5.
    • Like arithmetic operators, relational operators, and logical operators, the minus sign that joins two of data, called binocular operators, such as 6+7, 8*5, 5>6, 4 && 0,
    • And so on, an operator that joins 3 data, which should be called the trinocular operator

2. Three mesh operator

The C language provides a unique three-mesh operator: the conditional operator.

1> using formats

An expression a? Expression B: Expression C

2> operation Results

If expression A is set to true, the result of the conditional operator is the value of expression B, otherwise, the value of the expression C

3> Combination direction and priority
    • Precedence order: arithmetic operator > Relational operator > Conditional operator > Assignment operator
    • The binding direction of the conditional operator is "right-to-left"
1 int a = 3>4? 4+5:5>4? 5+6:6>7+1;

The above code is equivalent to

1 int a = (3>4)? (4+5): ((5>4)? (5+6): (6> (7+1)));

Just a little bit easier.

1 int a = 0? 9: (1? 11:0);

Continue to simplify to

1 int a = 0? 9:11;

So the value of a is 11.

Back to top nine, bitwise operators

The so-called bitwise operation is the operation of each bits. The C language provides a total of 6 bitwise operators, which can only operate on integers,:& bitwise AND, | bitwise OR, ^ bitwise XOR, << shift left, >> right shift, ~ reverse.

1.& Bitwise AND

1> Use form: integer A & integer b

2> function: The binary phase corresponding to the integers a and B. Only the corresponding two binaries are 1 o'clock, the result bit is 1, otherwise 0. The number of participating operations appears in a complementary fashion.

3> Example: 9&5, in fact, is 1001&101=1, so 9&5=1

4> Law:

    • The result of the same integer phase & is the integer itself. Like 5&5=5.
    • The results of multiple integers & are independent of order. Like 5&6&7=5&7&6.

2.| Bitwise OR

1> Use form: integer A | Integer b

2> function: The binary phase or the corresponding integer A and B. As long as the corresponding two binary has one for 1 o'clock, the result bit is 1, otherwise 0. The number of participating operations appears in a complementary fashion.

3> Example: 9|5, in fact, is 1001|101=1101, so 9|5=13

4> Law:

    • The result of the same integer phase | is the integer itself. Like 5|5=5.
    • Multiple integer Phase | Results are independent of the order. Like 5|6|7=5|7|6.

3.^ Bitwise XOR OR

1> Use form: integer a ^ integer b

2> function: integer A and b correspond to binary differences or. When the corresponding binary is different (not the same), the result is 1, otherwise 0. The number of participating operations appears in a complementary fashion.

3> Example: 9^5, in fact, is 1001^101=1100, so 9^5=12

4> Law:

    • Binary, with 1 ^ will be reversed, and 0-phase ^ to maintain the position
    • The result of the same integer ^ is 0. Like 5^5=0.
    • The results of multiple integers ^ are independent of the order. Like 5^6^7=5^7^6.
    • Thus concludes: A^b^a = b

4.~ Reverse

1> ~ is a single-mesh operator with right-associative, form of use: ~ Integer A

2> function: Reverse the binary of integer a (0 change to 0)

3> Example: For example, in fact is ~ (0000 0000 0000 0000 0000 0000 0000 1001) = (1111 1111 1111 1111 1111 1111 1111 0110), so ~9=-10

5.<< left Shift

1> << is binocular operator, using form: integer a<< positive n

2> function: All the binary of the whole number a left n bits, high drop, low 0. The n-bit left shift is actually multiplied by 2 of the n-th side.

3> Example: 3<<4,3 would have been 0000 0011, shifted 4 bits to the left and became 0011 0000, so 3<<4 = 48 = 3 * 24

4> need to note is: Because the left shift is discarded the highest bit, 0 is the lowest bit, so the sign bit will also be discarded, the result value left out may change the positive and negative

6.>> Right Shift

1> >> is binocular operator, using form: integer a>> positive n

2> function: All the binaries of integer A are shifted to the right n bits, keeping the sign bit constant. The right-shift n-bit is actually divided by 2 of the n-th side.

3> Example: 32>>3,32 would have been 0010 0000, moved 3 bits to the right and became 0000 0100, so 32>>3 = 4 = 32/23

C Language Chapter 4

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.