C language Simple subtraction Operation _c language

Source: Internet
Author: User

The C language can also perform subtraction operations, but the operational notation is slightly different from that in mathematics, as shown in the following table.

Addition Subtraction Multiplication Division Find the remainder
Mathematical + - X ÷ No
C language + - * / %

Plus, minus the same as in math, multiplication, division different, and the other C language to find the remainder of the operator.

Let's look at a piece of code first:

#include <stdio.h>
#include <stdlib.h>
int main ()
{
 int a=12;
 int b=100;
 float c=8.5;
 int m=a+b;
 float N=b*c;
 Double p=a/c;
 int q=b%a;
 printf ("m=%d, N=%f, P=%lf, q=%d\n", M, N, p, q);
 System ("pause");
 return 0;
}

Output results:

m=112, n=850.000000, p=1.411765, q=4

You can also have numbers directly involved in the operation:

#include <stdio.h>
#include <stdlib.h>
int main ()
{
 int a=12;
 int b=100;
 float c=8.9;
 int m=a-b; The variable participates in
 the operation int n=a+239;//has the variable also has the number
 double p=12.7*34.3;//The number directly participates in
 the operation printf ("m=%d, n=%d, p=%lf\n", M, N, p); 
   printf ("m*2=%d, 6/3=%d, m*n=%ld\n", M*2, 6/3, m*n);
 System ("pause");
 return 0;
}

Output results:

m=-88, n=251, p=435.610000

m*2=-176, 6/3=2, m*n=-22088

For division, it should be noted that the divisor cannot be 0, so a statement such as int a=3/0 is wrong.

Shorthand for subtraction

Let's take a look at an example:

#include <stdio.h>
#include <stdlib.h>
int main ()
{
 int a=12;
 int b=10;
 printf ("a=%d\n", a);
 a=a+8;
 printf ("a=%d\n", a);
 A=a*b;
 printf ("a=%d\n", a);
 System ("pause");
 return 0;
}

Output results:

A=12

A=20

a=200

First output a original value of a=a+8; equivalent to replace the value of the original A with a a+8 value, so the second output 20; the third time, replace the value with the A*b value, the second time

So it's 200.

In C, an expression a=a#b can be abbreviated to A#=B, #表示 + 、-、 *, or any one of the operators in%.

In the above example, the a=a+8 can be abbreviated to A+=8;,A=A*B, and can be abbreviated to a*=b;.

The following shorthand form is also correct:

int a = ten, B =;
A + + 10; Equivalent to A = a + ten;
A *= (b-10); The equivalent of a = A * (b-10);
A-= (A+20); Equivalent to A = A-(A+20);

Note: A#=b is only a shorthand and does not affect efficiency.

The above is C language addition, subtraction, multiplication, in addition to the basic operation of the remainder, there is a need for friends can refer to.

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.