Dark Horse programmer--c Language--Basic arithmetic

Source: Internet
Author: User

Basic operations are divided into: arithmetic operations, assignment operations, self-increment self-subtraction operations, relational operations, logical operations, and trinocular operations.

First, arithmetic operations

1, addition operation: with "+" sign, you can also indicate a positive number

2, subtraction operation: with "-" sign, you can also indicate negative numbers

3. Multiplication: denoted by "*" instead of "X"

4, division Operation: with "/" instead of "÷" (integer divided by positive number, the result is still an integer)

5, take the remainder operation: the "%" sign, both sides are integers, and the final sign by the% left of the integer decision.

6. Type conversion:

① Automatic type conversion

int a = 10.6;

int B = 10.5 + 1.7;

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

② 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

③ Coercion Type conversions

Double A = (double) 1/2;

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

An example of an arithmetic operation code is as follows:

1#include <stdio.h>2 3 intMain ()4 {5     //1. Basic use of arithmetic operators6     intA =Ten+1+2-3+5;7     8     intb =-Ten;9     Ten     intc =Ten*b; One      A     intD =Ten/2; -      -     //take the remainder operation (modulo operation) the     //% are integers on both sides -     //% the positive and negative results of the residual result are only related to the value on the left of% -     intE =Ten% -3; -printf"%d\n", a); +printf"%d\n", b); -printf"%d\n", c); +printf"%d\n", d); Aprintf"%d\n", e); at      -     //Automatic type conversion (Double->int) -     intm =10.8; -      -     //coercion type conversion (Double->int) -     intn = (int)10.5; inprintf"%d\n", m); -       to      +     //Automatic type lift (int->double) -     Doublex =10.6+6; the  *     Doubley =1/3; $     Panax Notoginseng     Doublez = (Double)3/2; -      theprintf"The value of E is%f\n", z); +      A      the     return 0; +}

The result of the operation is:

Second, assignment operation

1. Simple Assignment

1#include <stdio.h>2 3 intMain ()4 {5     //Simple Assignment6     intA =Ten;7     8     intb;9b=6;Ten     return 0; One}

2. Compound Assignment

1     // Compound Assignment Operation 2     5 // A = a + 5; 3     4     5 // A = a * 5; 5     6     5 6 4 // A = a + (5 + 6 + 4);

Three, self-increment self-reduction operation

Increment operator + +, for example (A++,++a), self-decrement operator,; // A-= 1; a = A-1;

Iv. Relational Operations

1, true and false nature. In C, the condition is set to "true", the condition is not set as "false", and "0" is stated as false, and any other non-0 value represents true.

2, the operation result of the relational operation: The result of the relational operation is only two, namely true and false. Condition is true, default value is 1, false, default value is 0

3. Special attention:

① relational operators have lower precedence than arithmetic operators

The precedence of the = =,! = in the ② relational operator is equal,<, <=, >, >=, and the precedence of the former is lower than the second

The binding order of ③ relational operators is left to right.

The relational operation code is as follows:

1#include <stdio.h>2 3 intMain ()4 {5   6     intA =Ten;7     8     intb = One;9     Ten     //the condition is set to return 1, really One     //if the condition is not established, return 0, False A     intresult = B >=A; -      -printf"%d\n", result); the       -     -}

Five, logical operation

1. Logic and (notation &&)

"Conditional a&& condition BB"

When both condition A and condition B are true, the result is real, that is, the value is 1, otherwise false

For example

1     Logic and Conditions 1 && conditions 22     3      int a  =  ten>3  7<6; 4     5     int 0 Ten ; 6     7     printf ("a=%d\n", a);

2. Logical OR (notation | | )

"Condition a| | Condition B "

When condition A and condition B are established, the result is true, that is, the value is 1, otherwise false

For example

1 //logic or Condition 1 | | condition 22     3     4     intA =Ten;5     intb =Ten;6     7     intc = (a<5) || (b++-Ten);8     9     //A = tenTen     //B = One One     //C = 0 Aprintf"a=%d, b=%d, c=%d\n", A, B, c);

3, logical non (symbol!) )

“! Condition a "

The condition A is reversed: if condition A is set, the result is false, that is, the value is 0, otherwise it is true.

1  // logical non! condition 2     // returns 0 if the condition is true, and returns 1 if the condition is not true 3     4     5     6    7     int a =!! Ten ; 8     9     printf ("a=%d\n", a); Ten    One     

Six or three mesh operator

Trinocular operator, operation format: Condition? Value 1: Value 2

1 // Three mesh operator  condition? value 1: Value 223int a =!  - 9  the ; 4 5 printf ("a=%d\n", a);

Dark Horse programmer--c Language--Basic arithmetic

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.