# C Language Introduction: 06. Basic operations

Source: Internet
Author: User

first, arithmetic operations

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

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 or an integer. The value of 1/2 is 0, and this is not one-second.

5. Residual operation%

What is the remainder: two integers after dividing

% can only be integers on both sides

Positive and negative depending on% left value

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

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) Forced type conversion

Double A = (double) 1/2;

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

(4) Sequence of operations

An expression

Priority: 5+4*8-3

7. Exercises

(1) When what is an 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秒

second, assignment operation1. Simple Assignment

Operation procedure for int a = 10 + 5;

The operation process of a = b = 10;

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

2. Compound Assignment

Complex subtraction: A + = 4 + 5;

Iii. self-increasing self-reduction1. Simple to use

+ + 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

5++ is wrong.

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

int a = 10;

a++; ++a;

int b = a++; int b = ++a;

Iv. sizeof1. Role

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

v. Relational operations (comparison 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 execute a piece of code when a certain condition is established.

This situation can be done using conditional statements, but we do not study conditional statements for the time being, first look at some more basic knowledge: How to judge a condition is not tenable.

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 the judgment condition "true and false".

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.

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

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

5. Exercises

Evaluates the value of the following expression

3 > 4 + 7

(3>4) + 7

5! = 4 + 2 * 7 > 3 = = 10

VI. Logical Operation

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 result of a logical operation is only 2: "True" is 1, "false" is 0

1.&& Logic and

(1) Use the format

"Condition a && condition B"

(1) Result of operation

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 the format

"Condition A | | Condition B "

(2) Result of operation

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 the format

"! Condition a"

(2) Result of operation

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

seven or three mesh operator

N-Mesh operator-three mesh operator

int a = 5?10:2;

Get the maximum number in A and b

Get the largest number in a, B, c

Introduction to the C language: 06. Basic Arithmetic

Related Keywords:
Related Article

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.