[Study Notes] [C Language] arithmetic operations, learning notes arithmetic operations

Source: Internet
Author: User

[Study Notes] [C Language] arithmetic operations, learning notes arithmetic operations

There are 34 operators in C language, including common addition, subtraction, multiplication, division, and so on.

1. addition operation +

In addition to addition, the addition operation can also represent the positive signs: + 5, + 90

2. subtraction-

The subtraction operation can also represent symbols-10 and-29.

3. Multiplication operation *

Note that the symbol is not x, *

4. division operation/

Note that the symbol is neither forward nor \,/

An integer is an integer except an integer. The value of 1/2 is 0, which is not 1/2

5. remainder operation %

What is remainder? The remainder after division of two integers

% Both sides can only be integers

Positive and negative are determined by the value on the left of %.

6. Notes

1> automatic type conversion

Int a = 10.6;

Int B = 10.5 + 1.7;

Automatically converts large data types to small ones, leading to loss of precision.

2> automatic type upgrade

Int B = 10.5 + 10;

Increase 10 on the right to the double type.

Double B = 1.0/2;

Solve the Division precision Problem

3> forced type conversion

Double a = (double) 1/2;

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

4> Operation Sequence

Expression

Combination (integration direction): 2 + 3 + 4

Priority: 5 + 4*8-3

 

Operator priority (from high to low)

Priority

Description

Operator

1

Brackets

(), []

2

Plus or minus sign

+ ,-

3

Auto-increment and auto-increment, not

++ ,--,!

4

Multiplication, division, and remainder

*,/, %

5

Addition and subtraction

+ ,-

6

Shift operation

<,>,>

7

Size relationship

>,>=, <, <=

8

Equality

= ,! =

9

Bitwise AND

&

10

Bitwise OR

^

11

By bit or

|

12

Logic and

&&

13

Logic or

|

14

Conditional operation

? :

15

Value assignment

=, + =,-=, * =,/=, % =

16

Bit value assignment

& =, | =, <=, >>=, >>>> =

If you want to change the operation sequence in a program, you can use ().

7. course code

1 # include <stdio. h> 2 3 int main () 4 {5/* 1. 6 int a = 10 + 1 + 2-3 + 5; 7 8 int B =-10; 9 10 int c = 10 * B; 11 12 int d = 10/2; 13 14 // remainder operation (modulo operation) 15 // % both sides are integers 16 // % the positive and negative result of the remainder result is only related to the value on the left of % 17 int e = 10%-3; 18 printf ("% d \ n", e); 19 */20 21/* 22 // automatic type conversion (double-> int) 23 int a = 10.8; 24 25 // forced type conversion (double-> int) 26 int B = (int) 10.5; 27 printf ("% d \ n", ); 28 */29 30 // automatic type increase (int-> double) 31 double c = 10.6 + 6; 32 33 double d = 1/3; 34 35 double e = (double) 3/2; 36 37 printf ("the value of e is % f \ n", e); 38 39 40 return 0; 41}

 

1 # include <stdio. h> 2/* 3: the user is prompted to enter the number of seconds of a time. For example, if the value is 500 in 500 seconds, the corresponding minutes and seconds are output, for example, 96546546 S is 8 minutes, 20 seconds, 4 */5 int main () 6 {7/* 8 // set the value of c to 9 int a = within 10; 10 11 int c = a % 10; 12 */13 14 // 1. prompt the user to enter the time 15 printf ("Please enter a time value (seconds): \ n"); 16 17 // 2. time of receiving user input 18 int time; 19 scanf ("% d", & time); 20 21 // 3. convert to the corresponding minute and second 22 int minute = time/60; // minute 23 int second = time % 60; // seconds 24 25 printf ("% d seconds = % d minutes % d seconds \ n", time, minute, second); 26 27 return 0; 28}

 

Related Article

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.