Arithmetic Operators

Introduction

Arithmetic operators are symbols used to perform mathematical calculations in C programming. These operators allow you to perform basic mathematical operations like addition, subtraction, multiplication, division, and finding remainders. They are fundamental building blocks for any program that involves numerical computations.

C provides a complete set of arithmetic operators that work with different numeric data types including integers, floating-point numbers, and characters (since characters have numeric ASCII values).

Key Concepts

Binary Operators: Most arithmetic operators require two operands Unary Operators: Some operators work with single operands
Operator Precedence: Order in which operations are performed Type Conversion: How different data types interact in operations

Basic Arithmetic Operators

Addition (+)

#include <stdio.h>
int main() {
    int a = 10, b = 5;
    int sum = a + b;
    
    printf("%d + %d = %d\n", a, b, sum);
    
    float x = 3.5, y = 2.2;
    float result = x + y;
    printf("%.1f + %.1f = %.1f\n", x, y, result);
    
    return 0;
}

Subtraction (-)

#include <stdio.h>
int main() {
    int a = 15, b = 7;
    int difference = a - b;
    
    printf("%d - %d = %d\n", a, b, difference);
    
    // Can result in negative numbers
    int result = b - a;
    printf("%d - %d = %d\n", b, a, result);
    
    return 0;
}

Multiplication (*)

#include <stdio.h>
int main() {
    int a = 6, b = 4;
    int product = a * b;
    
    printf("%d * %d = %d\n", a, b, product);
    
    // With floating point
    float price = 19.99;
    int quantity = 3;
    float total = price * quantity;
    printf("%.2f * %d = %.2f\n", price, quantity, total);
    
    return 0;
}

Division (/)

#include <stdio.h>
int main() {
    // Integer division
    int a = 17, b = 5;
    int quotient = a / b;  // Result: 3 (fractional part discarded)
    printf("%d / %d = %d\n", a, b, quotient);
    
    // Floating point division
    float x = 17.0, y = 5.0;
    float result = x / y;
    printf("%.1f / %.1f = %.2f\n", x, y, result);
    
    // Mixed division
    float mixed = (float)a / b;  // Type casting
    printf("(float)%d / %d = %.2f\n", a, b, mixed);
    
    return 0;
}

Modulus (%)

#include <stdio.h>
int main() {
    int a = 17, b = 5;
    int remainder = a % b;
    
    printf("%d %% %d = %d\n", a, b, remainder);
    
    // Check if number is even or odd
    int number = 23;
    if (number % 2 == 0) {
        printf("%d is even\n", number);
    } else {
        printf("%d is odd\n", number);
    }
    
    return 0;
}

Operator Precedence and Associativity

Precedence Rules

#include <stdio.h>
int main() {
    int result;
    
    // Multiplication and division have higher precedence than addition and subtraction
    result = 10 + 5 * 2;  // Result: 20 (not 30)
    printf("10 + 5 * 2 = %d\n", result);
    
    // Use parentheses to change order
    result = (10 + 5) * 2;  // Result: 30
    printf("(10 + 5) * 2 = %d\n", result);
    
    // Left to right associativity for same precedence
    result = 20 / 4 * 2;  // Result: 10 (not 2.5)
    printf("20 / 4 * 2 = %d\n", result);
    
    return 0;
}

Unary Arithmetic Operators

Unary Plus (+) and Unary Minus (-)

#include <stdio.h>
int main() {
    int a = 5;
    int b = -a;  // Unary minus
    int c = +a;  // Unary plus (rarely used)
    
    printf("a = %d\n", a);
    printf("-a = %d\n", b);
    printf("+a = %d\n", c);
    
    return 0;
}

Increment (++) and Decrement (—)

#include <stdio.h>
int main() {
    int x = 5;
    
    printf("Original x: %d\n", x);
    
    // Pre-increment
    printf("++x: %d\n", ++x);  // x becomes 6, then printed
    printf("x after pre-increment: %d\n", x);
    
    // Post-increment  
    printf("x++: %d\n", x++);  // x is printed (6), then becomes 7
    printf("x after post-increment: %d\n", x);
    
    // Pre-decrement
    printf("--x: %d\n", --x);  // x becomes 6, then printed
    
    // Post-decrement
    printf("x--: %d\n", x--);  // x is printed (6), then becomes 5
    printf("Final x: %d\n", x);
    
    return 0;
}

Type Conversion in Arithmetic

Implicit Type Conversion

#include <stdio.h>
int main() {
    int i = 10;
    float f = 3.5;
    double d = 2.7;
    
    // int + float = float
    float result1 = i + f;
    printf("int + float: %.2f\n", result1);
    
    // float + double = double
    double result2 = f + d;
    printf("float + double: %.2f\n", result2);
    
    return 0;
}

Explicit Type Conversion (Casting)

#include <stdio.h>
int main() {
    int a = 17, b = 5;
    
    // Integer division
    int int_result = a / b;
    printf("Integer division: %d\n", int_result);
    
    // Cast to get floating point result
    float float_result = (float)a / b;
    printf("Float division: %.2f\n", float_result);
    
    // Cast result back to int
    int truncated = (int)float_result;
    printf("Truncated result: %d\n", truncated);
    
    return 0;
}

Practical Examples

Simple Calculator

#include <stdio.h>
int main() {
    float num1, num2, result;
    char operator;
    
    printf("Enter first number: ");
    scanf("%f", &num1);
    printf("Enter operator (+, -, *, /, %%): ");
    scanf(" %c", &operator);
    printf("Enter second number: ");
    scanf("%f", &num2);
    
    switch(operator) {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            if (num2 != 0) {
                result = num1 / num2;
            } else {
                printf("Error: Division by zero!\n");
                return 1;
            }
            break;
        case '%':
            if ((int)num2 != 0) {
                result = (int)num1 % (int)num2;
            } else {
                printf("Error: Division by zero!\n");
                return 1;
            }
            break;
        default:
            printf("Invalid operator!\n");
            return 1;
    }
    
    printf("%.2f %c %.2f = %.2f\n", num1, operator, num2, result);
    return 0;
}

Temperature Converter

#include <stdio.h>
int main() {
    float celsius, fahrenheit, kelvin;
    
    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);
    
    // Convert to Fahrenheit: F = C * 9/5 + 32
    fahrenheit = celsius * 9.0 / 5.0 + 32;
    
    // Convert to Kelvin: K = C + 273.15
    kelvin = celsius + 273.15;
    
    printf("%.2f°C = %.2f°F = %.2fK\n", celsius, fahrenheit, kelvin);
    
    return 0;
}

Area and Perimeter Calculator

#include <stdio.h>
int main() {
    float length, width, area, perimeter;
    
    printf("Enter length of rectangle: ");
    scanf("%f", &length);
    printf("Enter width of rectangle: ");
    scanf("%f", &width);
    
    area = length * width;
    perimeter = 2 * (length + width);
    
    printf("Rectangle dimensions: %.2f x %.2f\n", length, width);
    printf("Area: %.2f square units\n", area);
    printf("Perimeter: %.2f units\n", perimeter);
    
    return 0;
}

Common Mistakes and Solutions

Division by Zero

// Wrong - no check for division by zero
int result = a / b;  // Dangerous if b is 0

// Correct - always check
if (b != 0) {
    int result = a / b;
} else {
    printf("Error: Division by zero!\n");
}

Integer Division Issues

// Problem - integer division loses fractional part
int a = 5, b = 2;
float result = a / b;  // Result: 2.0 (not 2.5)

// Solution - cast to float
float result = (float)a / b;  // Result: 2.5

Modulus with Floating Point

// Wrong - modulus doesn't work with float
float a = 5.5, b = 2.0;
float result = a % b;  // Compilation error

// Correct - use fmod() function
#include <math.h>
float result = fmod(a, b);

Important Points

  • Division by Zero: Always check for zero before division or modulus operations
  • Integer Division: Be aware that integer division truncates the result
  • Precedence: Remember operator precedence rules or use parentheses
  • Type Conversion: Understand how different data types interact
  • Overflow: Large calculations may exceed data type limits
  • Modulus Limitation: Modulus operator only works with integers

Summary

Arithmetic operators are essential tools for performing mathematical calculations in C programming. The basic operators (+, -, *, /, %) handle most computational needs, while unary operators (++, —) provide convenient shortcuts for common operations. Understanding operator precedence, associativity, and type conversion rules is crucial for writing correct expressions. Key considerations include handling division by zero, understanding integer vs floating-point arithmetic, and being aware of data type limitations. These operators form the foundation for building calculators, mathematical applications, and any program requiring numerical computations.


Part of BCA Programming with C Course (UGCOA22J201)