Arithmetic operators in C perform mathematical computations on integer and floating-point values. C provides addition (+), subtraction (-), multiplication (*), division (/), modulo (%), increment (++), and decrement (--). Understanding how integer division truncates and how pre- versus post-increment differ are among the most common sources of bugs for beginners.
C Program for Arithmetic Operators
/* Arithmetic operators in C
* Compile: gcc -ansi -Wall -Wextra arith.c -o arith */
#include <stdio.h>
int main(void)
{
int a = 17, b = 5;
int q, r;
double x = 17.0, y = 5.0;
printf("a = %d, b = %d\n\n", a, b);
/* Basic operators */
printf("Addition: a + b = %d\n", a + b);
printf("Subtraction: a - b = %d\n", a - b);
printf("Multiplication: a * b = %d\n", a * b);
/* Integer division -- truncates toward zero */
q = a / b;
r = a % b;
printf("Division: a / b = %d (integer, truncated)\n", q);
printf("Modulo: a %% b = %d (remainder)\n", r);
printf("Verify: %d * %d + %d = %d\n\n", q, b, r, q * b + r);
/* Float division */
printf("Float div: %.1f / %.1f = %.4f\n\n", x, y, x / y);
/* Increment and decrement */
printf("Pre-increment: ++a = %d\n", ++a);
printf("Post-increment: b++ = %d\n", b++);
printf("After b++: b = %d\n", b);
printf("Pre-decrement: --a = %d\n", --a);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra arith.c -o arith
./arith
Sample Output
a = 17, b = 5 Addition: a + b = 22 Subtraction: a - b = 12 Multiplication: a * b = 85 Division: a / b = 3 (integer, truncated) Modulo: a % b = 2 (remainder) Verify: 3 * 5 + 2 = 17 Float div: 17.0 / 5.0 = 3.4000 Pre-increment: ++a = 18 Post-increment: b++ = 5 After b++: b = 6 Pre-decrement: --a = 17
Operator Reference Table
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 17 + 5 | 22 |
- |
Subtraction | 17 – 5 | 12 |
* |
Multiplication | 17 * 5 | 85 |
/ |
Division (int) | 17 / 5 | 3 (truncated) |
% |
Modulo (remainder) | 17 % 5 | 2 |
++ |
Increment | a++ or ++a | adds 1 to a |
-- |
Decrement | a– or –a | subtracts 1 |
Code Explanation
- Integer division truncates —
17 / 5gives3, not3.4. The fractional part is discarded entirely. To get the decimal result, at least one operand must be adouble:17.0 / 5gives3.4. - Modulo operator
%— gives the remainder after integer division.17 % 5 = 2because 17 = 3×5 + 2. The relationshipa == (a/b)*b + (a%b)always holds. - Pre-increment
++a— incrementsafirst, then uses the new value in the expression.printf("%d", ++a)where a=17 prints18. - Post-increment
b++— uses the current value in the expression, then increments.printf("%d", b++)where b=5 prints5, and only after does b become6. %%in printf — to print a literal%in a printf format string, write%%. A single%would start a format specifier and cause undefined behaviour.
What This Program Teaches
- Integer vs floating-point division — the type of the operands determines whether division truncates. This is one of the most common bugs in C programs.
- Modulo and the division identity — understanding that
a == (a/b)*b + (a%b)helps when extracting digits, checking divisibility, or implementing cyclic counters. - Pre vs post increment — the order of increment vs use matters in expressions. Keeping increment statements on their own line avoids subtle bugs.
- Operator precedence —
*and/bind more tightly than+and-. Use parentheses when in doubt:(a + b) * cis unambiguous.
Related Programs
- Simple Calculator in C Using Switch
- GCD and LCM in C
- Simple Interest in C
- Largest of 3 Numbers in C
- Sum of Digits in C
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.
1 comment on “Arithmetic Operators in C – Complete Guide with Examples”
Sample C Program To Accept A String & Display Number Of Vowels.
Sample C Program To Accept A String & Display Number Of Each Vowels.
Sample C Program To Accept A String & Find Out Whether This Character Is Present In The String.
Sample C Program To Find Out The No. Of Times A Character Is Present In The String.
Sample C Program To Understand Working Of Address Concept.
Sample C Program On Use Of Array & Pointer Concept Together.
Sample C Program On Pointers & 2 – Dimensional Array.
Sample C Program On Strings Into Array Of Pointers.
Sample C Program To Accept & Add Ten Numbers Using Pointers.
Sample C Program To Implement Recursive Algorithm Using Pointers.
Sample C Program With Algorithm To Implement Bubble Sort Using Pointers.
Sample C Program To Implement Selection Sort Using Pointers, Arrays & Functions.