Arithmetic Operators

suggest change

Basic Arithmetic

These operators yield the result of the corresponding arithmetic operation. Addition and multiplication are commutative; subtraction, division, and remainder are not.

Addition Operator

The addition operator (\+) is used to add two operands together. Example:

#include <stdio.h>

int main(void)
{
    int a = 5;
    int b = 7;

    int c = a + b; /* c now holds the value 12 */

    printf("%d + %d = %d",a,b,c); /* will output "5 + 7 = 12" */

    return 0;
}

Subtraction Operator

The subtraction operator (\-) is used to subtract the second operand from the first. Example:

#include <stdio.h>

int main(void)
{
    int a = 10;
    int b = 7;

    int c = a - b; /* c now holds the value 3 */

    printf("%d - %d = %d",a,b,c); /* will output "10 - 7 = 3" */

    return 0;
}

Multiplication Operator

The multiplication operator (\*) is used to multiply both operands. Example:

#include <stdio.h>

int main(void)
{    
    int a = 5;
    int b = 7;

    int c = a * b; /* c now holds the value 35 */

    printf("%d * %d = %d",a,b,c); /* will output "5 * 7 = 35" */

    return 0;
}

Not to be confused with the unary * dereference operator.

Division Operator

The division operator (/) divides the first operand by the second. If both operands are integers, the result is an integer and the fractional part is discarded. Since C99, that truncation is toward zero. Use the % operator for the remainder.

If either operand has a floating type, the result is an approximation of the quotient.

Example:

#include <stdio.h>

int main (void)
{
    int a = 19 / 2 ; /* a holds value 9   */
    int b = 18 / 2 ; /* b holds value 9   */
    int c = 255 / 2; /* c holds value 127 */
    int d = 44 / 4 ; /* d holds value 11  */
    double e = 19 / 2.0 ; /* e holds value 9.5   */
    double f = 18.0 / 2 ; /* f holds value 9.0   */
    double g = 255 / 2.0; /* g holds value 127.5 */
    double h = 45.0 / 4 ; /* h holds value 11.25 */

    printf("19 / 2 = %d\n", a);    /* Will output "19 / 2 = 9"    */
    printf("18 / 2 = %d\n", b);    /* Will output "18 / 2 = 9"    */
    printf("255 / 2 = %d\n", c);   /* Will output "255 / 2 = 127" */
    printf("44 / 4 = %d\n", d);    /* Will output "44 / 4 = 11"   */
    printf("19 / 2.0 = %g\n", e);  /* Will output "19 / 2.0 = 9.5"    */
    printf("18.0 / 2 = %g\n", f);  /* Will output "18.0 / 2 = 9"      */
    printf("255 / 2.0 = %g\n", g); /* Will output "255 / 2.0 = 127.5" */
    printf("45.0 / 4 = %g\n", h);  /* Will output "45.0 / 4 = 11.25"  */

    return 0;
}

Modulo Operator

The remainder operator (%) takes integer operands and yields the remainder after the first operand is divided by the second. Since C99, (a / b) * b + (a % b) equals a when the division is defined, so the remainder has the sign of the dividend. Example:

#include <stdio.h>

int main (void) {
    int a = 25 % 2;    /* a holds value 1  */
    int b = 24 % 2;    /* b holds value 0  */
    int c = 155 % 5;   /* c holds value 0  */
    int d = 49 % 25;   /* d holds value 24 */

    printf("25 %% 2 = %d\n", a);    /* Will output "25 % 2 = 1"    */
    printf("24 %% 2 = %d\n", b);    /* Will output "24 % 2 = 0"    */
    printf("155 %% 5 = %d\n", c);   /* Will output "155 % 5 = 0"   */
    printf("49 %% 25 = %d\n", d);   /* Will output "49 % 25 = 24"  */

    return 0;
}

Increment / Decrement Operators

The increment (++) and decrement (--) operators modify the object they are applied to. Each has a prefix form (++a, --a) and a postfix form (a++, a--). Both forms update the stored value, but they yield different results: the prefix form yields the value after the update, and the postfix form yields the value before the update. Example:

#include <stdio.h>

int main(void)
{
    int a = 1;
    int b = 4;
    int c = 1;
    int d = 4;

    a++;
    printf("a = %d\n",a);    /* Will output "a = 2" */
    b--;
    printf("b = %d\n",b);    /* Will output "b = 3" */

    if (++c > 1) { /* c is incremented by 1 before being compared in the condition */
        printf("This will print\n");    /* This is printed */
    } else {
        printf("This will never print\n");    /* This is not printed */
    }

    if (d-- < 4) {  /* d is decremented after being compared */
        printf("This will never print\n");    /* This is not printed */
    } else {
        printf("This will print\n");    /* This is printed */
    }
}

As the example for c and d shows, both operators have two forms, as prefix notation and postfix notation. Both have the same effect in incrementing (++) or decrementing (--) the variable, but differ by the value they return: prefix operations do the operation first and then return the value, whereas postfix operations first determine the value that is to be returned, and then do the operation.

Because of this potentially counter-intuitive behaviour, the use of increment/decrement operators inside expressions is controversial.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents