C Notes
Learn all arithmetic operators in C including addition, subtraction, multiplication, division, and modulus. Understand integer division traps, operator precedence, and type casting with practical examples.
Arithmetic operators are the bread and butter of any C program. Whether you're building a calculator, processing financial data, or implementing game physics, you'll rely on these operators constantly. C provides five fundamental arithmetic operators that work with numeric data types, and understanding their behavior — especially with integers — is crucial for writing bug-free code.
What Are Arithmetic Operators?
Arithmetic operators perform mathematical calculations on operands. In C, we have five arithmetic operators:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 9 - 4 | 5 |
* | Multiplication | 6 * 7 | 42 |
/ | Division | 10 / 3 | 3 (integer) |
% | Modulus (Remainder) | 10 % 3 | 1 |
Addition Operator (+)
The addition operator adds two operands together. It works with all numeric types — integers, floats, doubles, and even characters (since characters are internally stored as ASCII values).
#include <stdio.h>
int main() {
int a = 15, b = 25;
float x = 3.5, y = 2.7;
char ch = 'A'; // ASCII value 65
printf("Integer addition: %d + %d = %d\n", a, b, a + b);
printf("Float addition: %.1f + %.1f = %.1f\n", x, y, x + y);
printf("Char + Int: '%c' + 1 = '%c'\n", ch, ch + 1);
return 0;
}Integer addition: 15 + 25 = 40 Float addition: 3.5 + 2.7 = 6.2 Char + Int: 'A' + 1 = 'B'
Subtraction Operator (-)
The subtraction operator calculates the difference between two operands. It can also be used as a unary operator to negate a value.
#include <stdio.h>
int main() {
int a = 50, b = 30;
int negative = -a; // Unary minus
printf("Subtraction: %d - %d = %d\n", a, b, a - b);
printf("Unary minus: -%d = %d\n", a, negative);
printf("Difference of chars: 'Z' - 'A' = %d\n", 'Z' - 'A');
return 0;
}Subtraction: 50 - 30 = 20 Unary minus: -50 = -50 Difference of chars: 'Z' - 'A' = 25
Multiplication Operator (*)
The multiplication operator multiplies two values. Be cautious with large numbers — integer overflow can happen silently in C.
#include <stdio.h>
int main() {
int length = 12, width = 8;
float price = 49.99, quantity = 3;
printf("Area of rectangle: %d x %d = %d\n", length, width, length * width);
printf("Total cost: %.2f x %.0f = %.2f\n", price, quantity, price * quantity);
// Overflow example
int big = 100000;
printf("Overflow risk: %d * %d = %d\n", big, big, big * big);
return 0;
}Area of rectangle: 12 x 8 = 96 Total cost: 49.99 x 3 = 149.97 Overflow risk: 100000 * 100000 = 1410065408
Notice how 100000 * 100000 should be 10000000000, but since it exceeds the range of int, we get a garbage value. This is integer overflow — a common trap in C.
Division Operator (/) — The Integer Division Trap
This is where many beginners get tripped up. The division operator behaves differently depending on the data types of its operands.
The Rule: When both operands are integers, C performs integer division (truncates the decimal part). When at least one operand is a float/double, it performs floating-point division.
#include <stdio.h>
int main() {
// Integer division - truncates!
printf("Integer: 10 / 3 = %d\n", 10 / 3);
printf("Integer: 7 / 2 = %d\n", 7 / 2);
printf("Integer: 1 / 2 = %d\n", 1 / 2);
// Floating-point division
printf("Float: 10.0 / 3.0 = %f\n", 10.0 / 3.0);
printf("Mixed: 10.0 / 3 = %f\n", 10.0 / 3);
// Type casting to force float division
int a = 10, b = 3;
printf("Cast: (float)10 / 3 = %f\n", (float)a / b);
return 0;
}Integer: 10 / 3 = 3 Integer: 7 / 2 = 3 Integer: 1 / 2 = 0 Float: 10.0 / 3.0 = 3.333333 Mixed: 10.0 / 3 = 3.333333 Cast: (float)10 / 3 = 3.333333
Important: Division by zero with integers causes undefined behavior (usually a runtime crash). With floats, dividing by zero gives inf or nan.
#include <stdio.h>
int main() {
float result = 5.0 / 0.0;
printf("Float division by zero: %f\n", result);
// int crash = 5 / 0; // This would crash the program!
return 0;
}Float division by zero: inf
Modulus Operator (%)
The modulus operator returns the remainder after integer division. It only works with integer operands — using it with floats will give a compilation error.
#include <stdio.h>
int main() {
printf("10 %% 3 = %d\n", 10 % 3); // Remainder of 10/3
printf("15 %% 5 = %d\n", 15 % 5); // Perfectly divisible
printf("7 %% 2 = %d\n", 7 % 2); // Check odd/even
printf("-10 %% 3 = %d\n", -10 % 3); // Negative operand
// Practical: Check if number is even or odd
int num = 17;
if (num % 2 == 0)
printf("%d is even\n", num);
else
printf("%d is odd\n", num);
return 0;
}10 % 3 = 1 15 % 5 = 0 7 % 2 = 1 -10 % 3 = -1 17 is odd
Note: When dealing with negative numbers, the sign of the result depends on the implementation in C89, but in C99 and later, the result takes the sign of the dividend (left operand).
Operator Precedence and Associativity
When multiple arithmetic operators appear in a single expression, C follows a specific order of evaluation:
| Priority | Operators | Associativity |
|---|---|---|
| 1 (Highest) | *, /, % | Left to Right |
| 2 (Lower) | +, - | Left to Right |
#include <stdio.h>
int main() {
int result;
result = 2 + 3 * 4; // 3*4 first, then +2
printf("2 + 3 * 4 = %d\n", result);
result = (2 + 3) * 4; // Parentheses override
printf("(2 + 3) * 4 = %d\n", result);
result = 10 - 4 / 2 + 1; // 4/2=2, then 10-2+1
printf("10 - 4 / 2 + 1 = %d\n", result);
result = 15 % 4 * 2; // Left to right: 15%%4=3, then 3*2
printf("15 %% 4 * 2 = %d\n", result);
return 0;
}2 + 3 * 4 = 14 (2 + 3) * 4 = 20 10 - 4 / 2 + 1 = 9 15 % 4 * 2 = 6
Type Conversion in Arithmetic
When you mix data types in an expression, C automatically promotes the smaller type to the larger type. This is called implicit type conversion or type promotion.
The hierarchy is: char → int → unsigned int → long → float → double → long double
#include <stdio.h>
int main() {
int i = 5;
float f = 2.5;
double d = 3.14;
char c = 'A'; // 65
printf("int + float = %f\n", i + f); // int promoted to float
printf("int + double = %lf\n", i + d); // int promoted to double
printf("char + int = %d\n", c + i); // char promoted to int
// Dangerous: assigning float result to int
int truncated = 9.7 + 0.5;
printf("Truncated: %d\n", truncated);
return 0;
}int + float = 7.500000 int + double = 8.140000 char + int = 70 Truncated: 10
Practical Example: Simple Calculator
Common Mistakes to Avoid
- Integer division when expecting float result — Always cast or use float literals
- Overflow with large multiplications — Use
long longfor big numbers - Using
%with floats — Modulus only works with integers; usefmod()for floats - Division by zero — Always validate the divisor before dividing
- Assuming left-to-right evaluation — Precedence rules may change the order
Interview Questions
Q1: What is the output of printf("%d", 5/2) and why?
The output is 2. Since both operands (5 and 2) are integers, C performs integer division which truncates the decimal part. To get 2.5, at least one operand must be a float: 5.0/2 or (float)5/2.
Q2: What happens when you use the modulus operator with floating-point numbers?
It results in a compilation error. The % operator only works with integer operands in C. For floating-point remainders, use the fmod() function from <math.h>.
Q3: What is the result of -7 % 2 in C99?
The result is -1. In C99 and later, the modulus operation with negative numbers yields a result with the same sign as the dividend (left operand). So -7 % 2 = -1 because -7 = (-3)*2 + (-1).
Q4: How does C handle mixed-type arithmetic expressions?
C performs implicit type promotion. The operand of the smaller type is automatically converted to the larger type before the operation. For example, in int + float, the int is promoted to float before addition.
Q5: Write a program to swap two numbers without using a temporary variable using arithmetic operators.
#include <stdio.h>
int main() {
int a = 10, b = 20;
a = a + b; // a = 30
b = a - b; // b = 10
a = a - b; // a = 20
printf("a = %d, b = %d\n", a, b);
return 0;
}Summary
- C provides five arithmetic operators:
+,-,*,/,% - Integer division truncates the result — this is the most common trap for beginners
- The modulus operator
%works only with integers and returns the remainder - Operator precedence follows BODMAS:
*,/,%are evaluated before+,- - Type promotion automatically converts smaller types to larger types in mixed expressions
- Always guard against division by zero and integer overflow in production code
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Arithmetic Operators in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, operators, arithmetic, arithmetic operators in c programming
Related C Programming Topics