Dark Horse programmer--c Language Basic use Calculation

Source: Internet
Author: User

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 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 right of the ten 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

An expression

Adhesion (combination direction): 2+3+4

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 no more than ten

solution int c = a%10;

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

1#include <stdio.h>2  intMain ()3{intX/*define the first number*/  4     intY/*Define a second number*/  5printf"enter two numbers separated by a space: \ n");/*User Tips*/  6scanf"%d%d", &x, &y);/*read two numbers through the keyboard*/    /*Output Results*/ 7printf"The average of these two numbers is%d\n", (x + y)/2); 8     return 0; 9}

3) Prompt the user to enter a time of the number of seconds, such as the input of about five seconds ,and then output the corresponding minutes and seconds, such as 500s is 8 minutes - seconds

1#include <stdio.h>2 intMain ()3 {    4     //1. Prompt user to enter time5printf"Please enter a time value (seconds): \ n");6     7     //2. Time to receive user input8     intTime ;9scanf"%d", &Time );Ten      One     //3. Convert to corresponding minutes and seconds A     intminute = time/ -;//minutes -     intSecond = time% -;//seconds -      theprintf"%d seconds =%d minutes%d seconds \ n", time, minute, second); -      return 0; -}

Two, assignment operation 1. 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 is a=a+4+5

Iii. self-increment by 1. Simple to use

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

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

5++ is wrong.

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

int a = 10;

a++; ++a;

int b = a++; equals the original value of a

int b = ++a; equals a plus a value

Four, 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

Shaping is 4 bytes, double is 8 bytes, float4 bytes, char is 1 bytes

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

Any value is true or FALSE, and any value other than 0 is "true" and only 0 is "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 result is 0

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

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

5. Exercises

Evaluates the value of the following expression

3 > 4 + 7 Results 0

(3>4) + 7 results 7

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

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

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

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

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>5, 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

Form: Condition? Value 1: Value 2

Required to compare values of a and B, and to deposit large numbers in C such as C=a>b?a:b

Compares the value of the A,b,c three number and deposits the maximum value in D

abmax= (a>b) a:b;

D=abmax>c? ABMAX:C;

   

Dark Horse programmer--c Language Basic use Calculation

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.