The Dark Horse programmer-----------Review the C language "basic operation"

Source: Internet
Author: User
Tags bitwise

Basic operations

First, arithmetic operations

There are 34 types of operators in C, including common subtraction operations

1. Addition Operation +

Besides being able to do addition operations, it can also represent a plus sign: +5, +90

2. Subtraction operation-

Besides being able to do subtraction, it can also represent symbols:-10,-29

3. Multiplication Operation *

Note the symbol, not X, but *

4. Division Operations/

Note the symbol, not ÷, nor \, but/

An integer, in addition to an integer. The value of 1/2 is 0, and this is not one-second.

5. Residual operation%

? What is a remainder: two integers after dividing

?% can only be integers on both sides

The positive and negative depend on the value of% left

6. Attention Points

1> Automatic type conversion

int a = 10.6;

int B = 10.5 + 1.7;

Automatic conversion of large type to small type, loss of precision

2> Automatic type Promotion

Double b = 10.5 + 10;

Lift the 10 on the right to double type

Double b = 1.0/2;

Solve the problem of the precision of division

3> Coercion Type conversions

Double A = (double) 1/2;

Double b = (double) (1/2);

4> Order of Operations

Adhesion (combination direction): 2+3+4

? Priority: 5+4*8-3

Second, assignment operation

1. Simple Assignment

The basic assignment operator is "=". His priority level is lower than the other operators, so the operator is often last read. At first, you can

Can think it is "equal", actually not. Its purpose is to assign the value of an expression to an lvalue. An expression or a

An lvalue, or an rvalue value. The so-called lvalue refers to an expression that can be used to the left of an assignment operation. The left value must be able to be modified and cannot be

is a constant. We now use variables as lvalue, and we can see that pointers and references can also be left-valued. For example:

int A, b, C;

A=3;

b=4;

c = (A + b) * (2*a-b)//Draw 14

2. Compound Assignment

The compound assignment operator, also known as an assignment operator with an operation, is called an assignment abbreviation.

For example: i=i+j; can be represented as i+=j; here + = is a compound assignment operator.

There are also 10 such operators, which are:

+ = Plus Assignment

-= minus Assignment

*= Multiply-Assign value

/= in addition to assigning values

%= to seek the remainder assignment value

&= Bitwise AND Assignment

| = Bitwise OR Assignment

^= Bitwise XOR or Assignment

<<= left Shift Assignment

>>= Right Shift Assignment

Note: A multi-assignment expression is formed when the right operand is another assignment expression. For example:

i=j=0; The result I, J values are 0

The general form of composition-assignment expressions

Variable Binocular operator = expression

It is equivalent to

variable = variable operator expression

For example:

A+=5 equivalent to A=a+5

X*=y+7 equivalent to x=x* (y+7)

R%=p equivalent to r=r%p

Iii. self-increasing self-reduction

The increment operator + + increments its operand by 1, and the decrement operator decrements its operand by 1. As shown below:

if (c = ' \ n ')

++NL;

+ + and--these two operators are particularly important in places where they can be used as prefix operators (used in front of variables, such as ++n). Also

Can be used as a suffix operator (used behind a variable, such as n++). In both cases, the effect is to add 1 to the value of the variable N. But

, there is a little difference between them. The expression ++n first increments the value of N by 1 and then uses the value of the variable n, whereas the expression n++ is first used

The value of the variable n, and then increments the value of N by 1. That is, for contexts where the value of the variable n is used, the effect of ++n and n++ is

Different.

If the n value is 5, then

x = n++;

The result of the execution is to set the value of X to 5, and

x = ++n;

Set the value of X to 6. When these two statements are executed, the value of the variable n is 6.

The self-increment and decrement operators can only be used for variables, similar to the expression (I+J) + +, which is illegal. You do not need to use any specific value and only need to

To increment a variable, the prefix method and suffix are the same as the effect.

For example:

if (c = = ' \ n ')

nl++;

In some cases, however, consideration is required.

Iv. sizeof

2. Use

Used to calculate the number of bytes of memory for a variable or a constant, a data type.

2. Basic form

? sizeof (variable \ constant)

? sizeof variable \ constant

? sizeof (data type)

? cannot be the sizeof data type

Five, relational operations

1. Conditional judgment

? By default, every line of code that we write in the program will be executed. But most of the time, we want to set up a situation

Execution of a section of code

2. True and False

In C language, the condition is called "true", the condition is not established is called "false", therefore, the judgment condition is established, is to judge the condition

"True and false".

How to judge the true and false? C language rules, any value is true and false, any non-0 value is "true", only 0 is "false". So

In other words, 108, 18, 4.5, 10.5, etc. are all "true" and 0 are "false".

3. Relationship Comparison

It is often compared in development, such as the size of the cards in the game of landlords. You can compare the size of two values by using the relational operator.

The result of the relational operators is only 2: If the condition is true, the result is 1, which is true, and if the condition is not true, the result is

is 0, which means "false".

4. Use note

The precedence of the = =,! = in the relational operator equals,<, <=, >, >=, and the precedence of the former is lower than the latter: 2==3>1

The associative direction of the relational operator is "left to right": 4>3>2

? The precedence of the relational operator is less than the arithmetic operator: 3+4>8-2

VI. Logical operation

The result of a logical operation is only 2: "True" is 1, "false" is 0

1.&& Logic and

1> using formats

"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. So

, condition A or condition B as long as one does not hold, 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 set, the result is 0, i.e." false "

If condition A is not established, it will not be judged whether the condition B is established: Because condition A is not established, regardless of condition B, the condition

A && condition B "Results are definitely 0, which is" false "

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 in the direction of "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 not

That's right. 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. Results of 5 && 4, for example

is 1, "true"; -6 && 0 result is 0, "false"

2.| | Logical OR

1> using formats

"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";

If both condition A and condition B are not established, 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 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 the condition B is determined: If condition B is established, the condition a | | The result of condition B is 1, which is

"True", if condition B is not established, 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.

So, if the value of a is in 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 | | Results of 4

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> using formats

“! 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, i.e. "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, and then reverse the result is 0

If the value of a is 2: First Judge A>3, not set, and then reverse the result is 1

Therefore, if the value of a is greater than 5, the result is 0; otherwise, the result is 1.

4> Note

? You can use logical non-operators 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 logically not! The result of the operation is 0,

The 0 value is logically 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 > && > | |

? 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 calculate 1 && 0, the equation 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

The expression 4>3 &&!-5>2 equivalent to (4>3) && ((! ( -5)) > 2) with a result of 0?

Seven or three mesh operation

The three-mesh operation is also called the conditional operation

The conditional operator is the only operator with 3 operands, and is sometimes called a ternary operator. For conditional expression B?x:y, the bar is calculated first

b, and then make a judgment. If the value of B is true, the value of x is computed, the result of the operation is the value of x, otherwise, the value of y is computed as Y

The value. A conditional expression never evaluates both X and Y. The conditional operator is left-associative, that is, the calculation is grouped from left to right

。 For example, A?b:c?d:e will be executed as (A?B:C) d:e.

< expression 1>?< expression 2>:< expression 3>; "?" The meaning of the operator is: the value of expression 1 is evaluated first, and if true, the expression 2 is executed,

and returns the result of expression 2; If the value of expression 1 is false, an expression of 3 is executed and the result of expression 3 is returned.

Example:

int a=2;

int c=3;

int b= (a>c)? A:c;

printf "b=%d B"

So the result is B is 3.

int a=1,b=2,z;

Z=a>b?a: (A>B?A:B);

cout "Z=%d z"

The result of this output is: z:2

int a=1,b=2,z;

Z=a>b?a:b;//went to the brackets

cout "Z=%d z"

The result of this output is: z:2

The direction of execution in the trinocular operation is right-to-left.

The Dark Horse programmer-----------Review the C language "basic operation"

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.