C-Language arithmetic operators

Source: Internet
Author: User
Tags arithmetic operators

The basic arithmetic operators for the C language are the following table:

name symbols Description
Addition operator + Binocular operators, that is, should have two of the amount involved in the addition operation. such as a+b,4+8 and so on. has right-associative nature.
Subtraction operator - Binocular operator. But "-" can also be used as a negative operator, which is a single-mesh operation, such as-x,-5, such as a left-associative.
Multiplication operator * Binocular operator with left-associative.
Division operator / Binocular operator with left-associative. When the participating operands are integer type, the result is integer type, and the decimal is rounded off. If one of the operands is a real type, the result is a double-precision solid.
Remainder operator (modulo operator) % Binocular operator with left-associative. The amount required to participate in the operation is integer and cannot be applied to a float or double type. The result of the remainder operation is equal to the remainder after the division of two numbers, and the result is 0 when divisible.


Binocular operators + and-have the same precedence, their precedence is lower than the precedence of operators *,/and%, while the precedence of operators *,/and% is lower than the precedence of the single-mesh operator + (plus sign) and-(minus).

1#include <stdio.h>2 intMainvoid){3printf"\n\n%d,%d\n", -/7,- -/7);4printf"%f,%f\n",20.0/7,-20.0/7);5     return 0;6}

In this example, the results of 20/7,-20/7 are integer and all decimals are removed. and 20.0/7 and -20.0/7 because of the real number to participate in the operation, so the result is also real type.

#include <stdio.h>int main (void) {    printf ("%d\n", 3%);     return 0 ;}

This example outputs 100 divided by 3 for the remainder 1.

Self-increment, decrement operator

The self-increment 1 operator is recorded as "+ +", whose function is to increment the value of the variable by 1, and the decrement 1 operator as "--", whose function is to reduce the value of the variable from 1.

Since the increment of 1, the self-minus 1 operator is a single-mesh operation with right-binding. These can be in the following ways:

    • The ++i:i is added 1 and then participates in other operations.
    • --I:I 1 and then participate in other operations.
    • After the I++:I participates in the operation, the value of I is increased by 1.
    • After the I--:I participates in the operation, the value of I is reduced by 1.


Error-prone in understanding and use is i++ and I--。 Especially when they are in more complex expressions or statements, they are often difficult to figure out and should therefore be analyzed carefully.

#include <stdio.h>intMainvoid){    intI=8; printf ("%d\n",++i); printf ("%d\n",--i); printf ("%d\n", i++); printf ("%d\n", i--); printf ("%d\n",-i++); printf ("%d\n",-i--); return 0;}

The initial value of I is 8, the 4th row I plus 1 after the output is 9; the output of the 5th line minus 1 is 8, the 6th line outputs I is 8 followed by 1 (9), the 7th line output I is 9 minus 1 (8), the 8th line output 8 is 1 (9), and the 9th line output 9 minus 1 (8).

1#include <stdio.h>2 intMainvoid){3     intI=5, j=5, P,q;4P= (i++) + (i++) + (i++);5Q= (++J) + (++J) + (+ +)j);6printf"p=%d,q=%d,i=%d,j=%d", p,q,i,j);7     return 0;8}

In this program, the p= (i++) + (i++) + (i++) should be understood as three I add, so the P value is 15. I then increment 13 times to add 3 so the last value of I is 8. For the value of Q is not, q= (++j) + (++J) + (++J) should be understood as J to increment 1, and then participate in the operation, because J since 13 times after the value of 8, three 8 added and the last value of 24,j is still 8.

For p= (i++) + (i++) + (i++), the first calculation (i++) + (i++), because it is "post-add", equivalent to 5+5, the result is 10, and then calculate the + + (i++), equivalent to 10+5, the result is 15.
For q= (++J) + (++J) + (++J), first calculate (++j) + (++J), because is "pre-add", to calculate two times ++j, at this time j=7, then add, equivalent to 7+7, the result is 14; then the 14+ (++J), the equivalent of 14+8, the result is 22.

C-Language arithmetic operators

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.