Home C C arithmetic operators with code examples

C arithmetic operators with code examples

In C programming, arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus.

Arithmetic operators work with numerical values and produce a result based on the operation.

1. Basic Arithmetic Operators

Here are the primary arithmetic operators in C:

Operator Symbol Description Example
Addition + Adds two values a + b
Subtraction Subtracts the second value from the first a – b
Multiplication * Multiplies two values a * b
Division / Divides the first value by the second a / b
Modulus % Returns the remainder of division a % b

Each operator can be used with integer and floating-point data types, but the behavior of division and modulus differs based on the type.

2. Examples of Each Arithmetic Operator

Example: Basic Arithmetic Operations

#include <stdio.h>

int main() {
    int a = 15, b = 4;

    printf("Addition: %d + %d = %d\n", a, b, a + b);
    printf("Subtraction: %d - %d = %d\n", a, b, a - b);
    printf("Multiplication: %d * %d = %d\n", a, b, a * b);
    printf("Division: %d / %d = %d\n", a, b, a / b); // Integer division
    printf("Modulus: %d %% %d = %d\n", a, b, a % b); // Modulus operator

    return 0;
}

In this example:

  • Addition (+): Adds a and b.
  • Subtraction (-): Subtracts b from a.
  • Multiplication (*): Multiplies a and b.
  • Division (/): Performs integer division, so 15 / 4 results in 3, discarding the decimal part.
  • Modulus (%): Returns the remainder of 15 / 4, which is 3.

Output:

Addition: 15 + 4 = 19
Subtraction: 15 - 4 = 11
Multiplication: 15 * 4 = 60
Division: 15 / 4 = 3
Modulus: 15 % 4 = 3

3. Division with Floating-Point Numbers

When dividing floating-point numbers, the result includes the fractional part.

#include <stdio.h>

int main() {
    float x = 15.0;
    float y = 4.0;
    float result = x / y;

    printf("Floating-point Division: %.2f / %.2f = %.2f\n", x, y, result);

    return 0;
}

In this example:

  • Division with float types preserves the decimal, resulting in 3.75.

Output:

Floating-point Division: 15.00 / 4.00 = 3.75

4. Modulus Operator with Integers

The modulus operator % works with integers and returns the remainder of a division.

It is not used with floating-point numbers.

#include <stdio.h>

int main() {
    int num1 = 29;
    int num2 = 6;

    int remainder = num1 % num2;

    printf("Remainder of %d divided by %d is: %d\n", num1, num2, remainder);

    return 0;
}

In this example:

  • 29 % 6 gives 5 as the remainder.

Output:

Remainder of 29 divided by 6 is: 5

5. Compound Assignment Operators

Compound assignment operators combine an arithmetic operation with assignment.

They provide a shorthand for common expressions.

Operator Example Equivalent to
+= a += b a = a + b
-= a -= b a = a – b
*= a *= b a = a * b
/= a /= b a = a / b
%= a %= b a = a % b

Example: Compound Assignment Operators

#include <stdio.h>

int main() {
    int num = 10;

    num += 5; // Equivalent to num = num + 5
    printf("After += : %d\n", num);

    num -= 3; // Equivalent to num = num - 3
    printf("After -= : %d\n", num);

    num *= 2; // Equivalent to num = num * 2
    printf("After *= : %d\n", num);

    num /= 4; // Equivalent to num = num / 4
    printf("After /= : %d\n", num);

    num %= 3; // Equivalent to num = num % 3
    printf("After %%= : %d\n", num);

    return 0;
}

Output:

After += : 15
After -= : 12
After *= : 24
After /= : 6
After %= : 0

6. Using Arithmetic Operators in Expressions

Arithmetic operators can be combined in expressions.

The operator precedence determines the order of operations, similar to standard math rules (PEMDAS: Parentheses, Exponents, Multiplication/Division, Addition/Subtraction).

Example: Operator Precedence in Expressions

#include <stdio.h>

int main() {
    int a = 10;
    int b = 5;
    int c = 2;
    int result;

    result = a + b * c; // Multiplication has higher precedence
    printf("Result without parentheses: %d\n", result);

    result = (a + b) * c; // Parentheses override precedence
    printf("Result with parentheses: %d\n", result);

    return 0;
}

In this example:

  • b * c is evaluated first in a + b * c because multiplication has higher precedence.
  • Adding parentheses with (a + b) * c changes the evaluation order.

Output:

Result without parentheses: 20
Result with parentheses: 30

7. Increment (++) and Decrement (–) Operators

The increment (++) and decrement (–) operators increase or decrease a variable’s value by one. They can be used in prefix or postfix form.

Operator Example Description
++ ++a Prefix increment: Increments a, then uses the value
++ a++ Postfix increment: Uses a's value, then increments
–a Prefix decrement: Decrements a, then uses the value
a– Postfix decrement: Uses a's value, then decrements

Example: Increment and Decrement Operators

#include <stdio.h>

int main() {
    int num = 10;

    printf("Original value: %d\n", num);

    printf("Post-increment: %d\n", num++); // Uses num, then increments
    printf("After post-increment: %d\n", num);

    printf("Pre-increment: %d\n", ++num); // Increments num, then uses it

    printf("Post-decrement: %d\n", num--); // Uses num, then decrements
    printf("After post-decrement: %d\n", num);

    printf("Pre-decrement: %d\n", --num); // Decrements num, then uses it

    return 0;
}

In this example:

  • num++ increments after printing, so num’s original value is printed first.
  • ++num increments num before printing, so the incremented value is shown immediately.

Output:

Original value: 10
Post-increment: 10
After post-increment: 11
Pre-increment: 12
Post-decrement: 12
After post-decrement: 11
Pre-decrement: 10

8. Summary of Arithmetic Operators

Operator Description Example
+ Addition a + b
Subtraction a – b
* Multiplication a * b
/ Division (integer or floating-point) a / b
% Modulus (remainder) a % b
++ Increment (prefix or postfix) ++a, a++
Decrement (prefix or postfix) –a, a–

Complete Example: Calculator with Arithmetic Operators

#include <stdio.h>

int main() {
    int num1, num2;
    char operator;

    printf("Enter first number: ");
    scanf("%d", &num1);

    printf("Enter an operator (+, -, *, /, %%): ");
    scanf(" %c", &operator);

    printf("Enter second number: ");
    scanf("%d", &num2);

    switch (operator) {
        case '+':
            printf("Result: %d\n", num1 + num2);
            break;
        case '-':
            printf("Result: %d\n", num1 - num2);
            break;
        case '*':
            printf("Result: %d\n", num1 * num2);
            break;
        case '/':
            if (num2 != 0)
                printf("Result: %d\n", num1 / num2);
            else
                printf("Error: Division by zero\n");
            break;
        case '%':
            if (num2 != 0)
                printf("Result: %d\n", num1 % num2);
            else
                printf("Error: Division by zero\n");
            break;
        default:
            printf("Error: Invalid operator\n");
    }

    return 0;
}

In this calculator program:

  • The user selects an operator, and switch is used to perform the corresponding arithmetic operation.
  • Error checking prevents division or modulus by zero.

This tutorial covers all basic arithmetic operators in C and demonstrates how to use them in various programming contexts.

You may also like