C Programs Tutorials | IT Developer
IT Developer

C Programming - C Arithmetic Operators



Share with a Friend

C Programming - C Arithmetic Operators

C Arithmetic Operators

Arithmetic operators in C are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and finding the remainder. These operators work with integer, floating-point, and other numerical data types.

List of Arithmetic Operators

Operator Description Example Result

+

Addition

a + b

Sum of a and b

-

Subtraction

a - b

Difference of a and b

*

Multiplication

a * b

Product of a and b

/

Division

a / b

Quotient when a is divided by b

%

Modulus (remainder)

a % b

Remainder when a is divided by b

Key Characteristics

  1. Addition (+): Adds two numbers.

C

int a = 5, b = 3;

printf("Sum: %d\n", a + b); // Output: 8

  1. Subtraction (-): Subtracts the second number from the first.

C

int a = 5, b = 3;

printf("Difference: %d\n", a - b); // Output: 2

  1. Multiplication (*): Multiplies two numbers.

C

int a = 5, b = 3;

printf("Product: %d\n", a * b); // Output: 15

  1. Division (/):
    • Returns the quotient of two numbers.
    • For integer division, the result is truncated (fractional part is discarded).

C

int a = 5, b = 2;

printf("Quotient: %d\n", a / b); // Output: 2

    • For floating-point numbers, it returns the exact quotient.

C

float a = 5.0, b = 2.0;

printf("Quotient: %.2f\n", a / b); // Output: 2.50

  1. Modulus (%):
    • Returns the remainder of integer division.
    • Works only with integers.

C

int a = 5, b = 3;

printf("Remainder: %d\n", a % b); // Output: 2

Example Program

C

#include <stdio.h>

int main() {

    int a = 10, b = 4;

    float x = 10.5, y = 4.2;

    printf("Addition (int): %d\n", a + b);

    printf("Subtraction (int): %d\n", a - b);

    printf("Multiplication (int): %d\n", a * b);

    printf("Division (int): %d\n", a / b);

    printf("Modulus (int): %d\n", a % b);

    printf("Addition (float): %.2f\n", x + y);

    printf("Subtraction (float): %.2f\n", x - y);

    printf("Multiplication (float): %.2f\n", x * y);

    printf("Division (float): %.2f\n", x / y);

    return 0;

}

Output:

Addition (int): 14

Subtraction (int): 6

Multiplication (int): 40

Division (int): 2

Modulus (int): 2

Addition (float): 14.70

Subtraction (float): 6.30

Multiplication (float): 44.10

Division (float): 2.50

Important Notes

  1. Division with Integers:
    • If both operands are integers, the result is an integer (truncated).

C

int a = 7, b = 3;

printf("%d\n", a / b); // Output: 2

  1. Floating-Point Division:
    • At least one operand must be a floating-point number to retain the decimal part.

C

float a = 7.0, b = 3.0;

printf("%.2f\n", a / b); // Output: 2.33

  1. Modulus Operator:
    • Only applicable to integers.
    • Using % with floating-point numbers results in a compilation error.
  2. Operator Precedence:
    • Multiplication, division, and modulus (*, /, %) have higher precedence than addition and subtraction (+, -).

C

int result = 10 + 5 * 2; // Evaluates as 10 + (5 * 2) = 20

    • Use parentheses to enforce precedence.

C

int result = (10 + 5) * 2; // Evaluates as (10 + 5) * 2 = 30

Applications

  • Arithmetic operators are fundamental in mathematical computations, such as:
    • Solving equations
    • Calculating percentages
    • Building algorithms
    • Data processing and analysis