#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Operators

Arithmetic Operators in C Programming

What are Arithmetic Operators?

Arithmetic operators perform mathematical operations on numeric values.

List of Arithmetic Operators

  • + — addition
  • - — subtraction
  • * — multiplication
  • / — division
  • % — modulus (remainder), works only with integers
int a = 10, b = 3;
printf("%d", a + b);  // 13
printf("%d", a % b);  // 1

Note on Integer Division

When both operands of / are integers, the result is truncated toward zero — 7 / 2 gives 3, not 3.5.

A Complete Program You Can Run

The program below uses every arithmetic operator and shows how the result changes when one operand is a floating-point value.

#include <stdio.h>

int main(void) {
    int a = 17, b = 5;
    double x = 17.0;

    printf("a + b  = %d\n", a + b);         /* 22  */
    printf("a - b  = %d\n", a - b);         /* 12  */
    printf("a * b  = %d\n", a * b);         /* 85  */
    printf("a / b  = %d\n", a / b);         /* 3   */
    printf("a %% b  = %d\n", a % b);        /* 2   */
    printf("x / b  = %.2f\n", x / b);       /* 3.40 */
    printf("(double)a / b = %.2f\n", (double)a / b);  /* 3.40 */
    return 0;
}

Two details are worth noticing. To print a literal percent sign inside printf, you write %%. And casting one operand to double before dividing is the standard way to get a decimal answer from two integers.

Precedence and Associativity

When an expression contains several operators, C follows fixed rules to decide which part is calculated first.

LevelOperatorsAssociativity
HigherUnary + -, ++ --Right to left
Middle* / %Left to right
Lower+ - (binary)Left to right

So 2 + 3 * 4 is 14, not 20. Left-to-right associativity means 20 / 5 * 2 is evaluated as (20 / 5) * 2, giving 8. When in doubt, add parentheses; they cost nothing and make your intent clear to readers.

Increment, Decrement and Compound Assignment

C has shortcuts for changing a variable by itself. i++ and ++i both add one, but they differ in the value they produce inside an expression.

int i = 5;
int p = i++;   /* p = 5, then i becomes 6 (post-increment) */
int q = ++i;   /* i becomes 7, then q = 7 (pre-increment)  */

int total = 100;
total += 20;   /* same as total = total + 20 -> 120 */
total *= 2;    /* 240 */
total %= 7;    /* 240 % 7 -> 2 */

Never modify the same variable twice in one expression, as in i = i++ + ++i;. The C standard leaves such code undefined, so different compilers can give different answers.

Traps Beginners Fall Into

  • Negative remainders. Because division truncates toward zero, -7 % 3 is -1, not 2. The sign of the result follows the left operand.
  • Division by zero. Dividing an integer by zero is undefined behaviour and often crashes the program. Check the divisor first.
  • Integer overflow. If int values grow beyond their maximum, signed overflow is undefined. Use long long for large products, such as multiplying two big counts.
  • Using % on floats. The modulus operator only accepts integers. For double values, use fmod() from math.h.
  • Casting too late. (double)(a / b) still performs integer division first. Cast an operand, not the result.

Quick self-check: predict the output of 10 / 4, 10 % 4, 10.0 / 4 and -10 % 4, then run them. The answers are 2, 2, 2.5 and -2.

Ready to master real-world C Programming development?

Learn C Programming hands-on with mentor-led, live sessions.

Explore Course