Basic Arithmetic

Return a value that is the result of applying the left hand operand to the right hand operand, using the associated mathematical operation. Normal mathematical rules of commutation apply (i.e. addition and multiplication are commutative, subtraction, division and modulus 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 \\* dereference operator.

Division Operator

The division operator (/) divides the first operand by the second. If both operands of the division are integers, it will return an integer value and discard the remainder (use the modulo operator % for calculating and acquiring the remainder).

If one of the operands is a floating point value, the result is an approximation of the fraction.

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 modulo operator (%) receives integer operands only, and is used to calculate the remainder after the first operand is divided by the second. 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;
}