Dark Horse programmer -04-Basic Arithmetic

Source: Internet
Author: User

First, arithmetic operations

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

1. addition Operation +2. Subtraction operation-3. Multiplication operation. Division Operations/

l integers in addition to integers, or integers. The value of 1/2 is 0, and this is not one-second.

5. Residual operation%

L What is the remainder: two integers after dividing

L% can only be integers on both sides

L Positive and negative depending on% left value

6. Attention Points

1> Automatic type conversion

int B = 10.5 + 1.7;

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

2> Automatic type Promotion

int 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 b = (double) (1/2);

Two, assignment operation 1. Simple assignment

l INT a = 10 + 5; The operation process

The operation process of l A = b = 10;

L The left side of the equals sign cannot be a constant, such as 10 = 11;

Iii. self-increment by 1. Simple to use

L + + auto-increment operator. such as A++,++a, are equivalent to a = a+1

L decrement operator. such as AA, are equivalent to a = A-1

Four, Sizeof1. Role

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

2. Basic form

l sizeof (variable \ constant)

l sizeof variable \ constant

L sizeof (data type)

L cannot be the sizeof data type

V. Relational operations (comparison operations) 1. True

L in C language, the condition is called "true", the condition is not established is called "false", therefore, the judgment condition is established, is the judgment condition "true and false".

2. Use note

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

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

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

VI. Logical operation

The result of the 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. Therefore, if a condition A or condition B is not tenable, the result is 0, that is, "false"

L 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> 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, and only if condition A and condition B are not set, the result is 0, which is "false".

3>c language Rules: 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> 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, that is, "true". That is to say: true false, false become true.

3>c language Rules: 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 the logical operators is: parentheses () > minus sign->! > Arithmetic operators > Relational operators > &&> | |

L-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.

L-expression 3+2<5| | 6>3 equivalent to ((3+2) < 5) | | (6>3) with a result of 1

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

Seven or three mesh operator

N-Mesh operator-three mesh operator

l int a = 5?10:2;

Dark Horse programmer -04-Basic Arithmetic

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.