C Language Series: Mastering Arithmetic Operators
Welcome back to the C Language Series! In part 14, we're diving deep into one of the most fundamental aspects of any programming language: Arithmetic Operators. These operators are the building blocks for performing mathematical calculations and are essential for virtually any C program you'll write.
What are Arithmetic Operators?
Arithmetic operators in C are symbols that tell the compiler to perform specific mathematical operations on one or more operands (variables or values). They allow you to add, subtract, multiply, divide, find remainders, and even increment or decrement values easily. Understanding these operators is crucial for manipulating numerical data and implementing logic in your programs.
Types of Arithmetic Operators in C
C provides a rich set of arithmetic operators. Let's explore each of them with clear explanations and practical examples.
1. Addition Operator (`+`)
The addition operator performs mathematical addition. It can be used with both integer and floating-point numbers.
Example:
#include <stdio.h>
int main() {
int num1 = 10, num2 = 5;
int sum = num1 + num2;
printf("Sum: %d\n", sum); // Output: Sum: 15
double val1 = 10.5, val2 = 3.2;
double sum_double = val1 + val2;
printf("Sum (double): %.2f\n", sum_double); // Output: Sum (double): 13.70
return 0;
}
2. Subtraction Operator (`-`)
The subtraction operator performs mathematical subtraction. It also works with both integer and floating-point types.
Example:
#include <stdio.h>
int main() {
int num1 = 10, num2 = 5;
int difference = num1 - num2;
printf("Difference: %d\n", difference); // Output: Difference: 5
double val1 = 15.0, val2 = 4.5;
double diff_double = val1 - val2;
printf("Difference (double): %.2f\n", diff_double); // Output: Difference (double): 10.50
return 0;
}
3. Multiplication Operator (`*`)
The multiplication operator calculates the product of two operands. It's used for multiplying numbers.
Example:
#include <stdio.h>
int main() {
int num1 = 10, num2 = 5;
int product = num1 * num2;
printf("Product: %d\n", product); // Output: Product: 50
double val1 = 2.5, val2 = 4.0;
double prod_double = val1 * val2;
printf("Product (double): %.2f\n", prod_double); // Output: Product (double): 10.00
return 0;
}
4. Division Operator (`/`)
The division operator divides the first operand by the second. Its behavior depends on the data types of the operands:
- Integer Division: If both operands are integers, the result is an integer, and any fractional part is truncated (not rounded).
- Floating-point Division: If at least one operand is a floating-point type, the result will be a floating-point number.
Example:
#include <stdio.h>
int main() {
int num1 = 10, num2 = 3;
int quotient_int = num1 / num2;
printf("Integer Division (10 / 3): %d\n", quotient_int); // Output: Integer Division (10 / 3): 3 (fractional part truncated)
double val1 = 10.0, val2 = 3.0;
double quotient_double = val1 / val2;
printf("Floating-point Division (10.0 / 3.0): %.2f\n", quotient_double); // Output: Floating-point Division (10.0 / 3.0): 3.33
int num3 = 10;
double num4 = 3.0;
double mixed_division = num3 / num4; // int divided by double results in double
printf("Mixed Type Division (10 / 3.0): %.2f\n", mixed_division); // Output: Mixed Type Division (10 / 3.0): 3.33
return 0;
}
Important: Division by zero is undefined behavior and will typically cause a runtime error or crash your program. Always ensure the divisor is not zero.
5. Modulus Operator (`%`)
The modulus operator returns the remainder of an integer division. It can only be used with integer operands. It's extremely useful for tasks like checking if a number is even or odd, or for cyclic operations.
Example:
#include <stdio.h>
int main() {
int num1 = 10, num2 = 3;
int remainder = num1 % num2;
printf("Remainder of 10 / 3: %d\n", remainder); // Output: Remainder of 10 / 3: 1
int num3 = 7, num4 = 2;
int remainder2 = num3 % num4;
printf("Remainder of 7 / 2: %d\n", remainder2); // Output: Remainder of 7 / 2: 1 (odd number)
int num5 = 8, num6 = 2;
int remainder3 = num5 % num6;
printf("Remainder of 8 / 2: %d\n", remainder3); // Output: Remainder of 8 / 2: 0 (even number)
return 0;
}
Note: The sign of the result of the modulus operator is implementation-defined for negative numbers, but typically takes the sign of the first operand.
6. Increment Operator (`++`)
The increment operator increases the value of an operand by 1. It can be used in two forms:
- Pre-increment (`++variable`): Increments the value of the variable first, then uses the new value in the expression.
- Post-increment (`variable++`): Uses the current value of the variable in the expression first, then increments the variable's value.
Example:
#include <stdio.h>
int main() {
int count = 5;
int result;
// Post-increment
result = count++;
printf("Post-increment:\n");
printf("Result: %d\n", result); // Output: Result: 5 (count was 5, used in result, then incremented)
printf("Count: %d\n", count); // Output: Count: 6
count = 5; // Reset count
// Pre-increment
result = ++count;
printf("\nPre-increment:\n");
printf("Result: %d\n", result); // Output: Result: 6 (count was incremented to 6, then used in result)
printf("Count: %d\n", count); // Output: Count: 6
return 0;
}
7. Decrement Operator (`--`)
Similar to the increment operator, the decrement operator decreases the value of an operand by 1. It also has pre-decrement and post-decrement forms:
- Pre-decrement (`--variable`): Decrements the value of the variable first, then uses the new value in the expression.
- Post-decrement (`variable--`): Uses the current value of the variable in the expression first, then decrements the variable's value.
Example:
#include <stdio.h>
int main() {
int count = 5;
int result;
// Post-decrement
result = count--;
printf("Post-decrement:\n");
printf("Result: %d\n", result); // Output: Result: 5 (count was 5, used in result, then decremented)
printf("Count: %d\n", count); // Output: Count: 4
count = 5; // Reset count
// Pre-decrement
result = --count;
printf("\nPre-decrement:\n");
printf("Result: %d\n", result); // Output: Result: 4 (count was decremented to 4, then used in result)
printf("Count: %d\n", count); // Output: Count: 4
return 0;
}
Operator Precedence and Associativity
When you have multiple operators in a single expression, their order of evaluation is determined by their precedence and associativity.
- Precedence: Determines which operator is evaluated first. For example, multiplication and division have higher precedence than addition and subtraction.
- Associativity: If operators have the same precedence, associativity determines the order of evaluation (usually left-to-right for arithmetic operators).
Common Precedence (Highest to Lowest for these operators):
- Unary operators: `++`, `--`
- Multiplication, Division, Modulus: `*`, `/`, `%`
- Addition, Subtraction: `+`, `-`
Parentheses `()` can always be used to explicitly control the order of evaluation, overriding default precedence.
Example of Precedence:
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 2;
int result;
// Without parentheses: * and / have higher precedence than +
// result = (5 * 2) + 10 = 10 + 10 = 20
result = a + b * c;
printf("a + b * c: %d\n", result); // Output: a + b * c: 20
// With parentheses: () forces evaluation order
// result = (10 + 5) * 2 = 15 * 2 = 30
result = (a + b) * c;
printf("(a + b) * c: %d\n", result); // Output: (a + b) * c: 30
return 0;
}