Basic arithmetic of 06-c language

Source: Internet
Author: User
Tags arithmetic arithmetic operators logical operators

First, arithmetic operations

There are 34 types of operators in C, including the common subtraction operation 1. Addition Operation +

L can also represent a plus: +5, +90 2, except that it can do addition operations. Subtraction Operation-

In addition to the subtraction operation, it can also represent symbols:-10,-29 3. Multiplication Operation *

Note the symbol, not X, but * 4. Division Operations/

L NOTE the symbol, not ÷, nor \, but/

l integers in addition to integers, or integers. The value of 1/2 is 0, and this is not One-second 5. Remainder operation%

L What is the remainder: two integers after dividing

L% can only be integers on both sides

L positive and negative depends on the value of% left 6. Watch out.

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

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

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

4> Order of Operations

L-Expressions

L binding (combination direction): 2+3+4

L Priority: 5+4*8-3 7. Exercises

1) What is the operator, no matter how the value of integer variable a changes, the result C is not more than 10

int c = a?10;

2) Prompts the user to enter two integers and outputs an average of two integers

3) Prompt the user to enter a time of the number of seconds, such as 500 seconds to enter 500, and then output the corresponding minutes and seconds, such as 500s is 8分钟20秒 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; 2. Compound Assignment

L Complex Subtraction: A + = 4 + 5; 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 a--,--a, are equivalent to a = A-1

L 5++ is the wrong 2. The difference between ++a and a++

l int a = 10;

U a++; ++a;

u int b = a++; int b = ++a; Iv. sizeof 1. 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 five, relational operations (comparison operations) 1. Conditional judgment

• By default, every sentence that we write in the program is executed in the correct code. But most of the time, we want to execute a piece of code when a certain condition is established.

This can be done using conditional statements, but for the time being we don't study conditional statements, we'll look at some more basic knowledge: How to tell if a condition is not tenable. 2. True and False

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

L How to judge True and false? C language rules, any value is true and false, any non-0 value is "true", only 0 is "false". 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.

L relational operators have only 2 operations: if the condition is true, the result is 1, which is "real", and if the condition is not true, the result is 0, which is "false". 4. 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

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

Evaluates the value of the following expression

L 3 > 4 + 7

L (3>4) + 7

L 5! = 4 + 2 * 7 > 3 = = 106, logical operation

L 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 the 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 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"

3> Operation Process

Always first determine if condition A is established

L If condition A is established, then determine if condition B is established: 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)

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

L If the value of a is 2: First Judge A>3, not set up, stop judgment. So the result is 0.

L Therefore, if the value of a is within the range of (3, 5), the result is 1; otherwise, the result is 0.

5> Note

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

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, for "true", -6 && 0 result is 0, for "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> 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)

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

L If the value of a is 2: First Judge A<3, set up, stop judging. So the result is 1.

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

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

L If the value of a is 6: First Judge A>5, set up, and then reverse the result is 0

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

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

4> Note

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

L 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

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), the result is 0 seven or three mesh operator

N-Mesh operator-three mesh operator

l int a = 5?10:2;

L GET the maximum number in A and b

L GET the largest number in a, B, c

Basic arithmetic of 06-c language

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.