โ C Arithmetic Operators โ Perform Mathematical Calculations in C
๐งฒ Introduction โ What Are Arithmetic Operators in C?
Arithmetic operators in C are symbols used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus. These operators form the foundation of numeric computation and are frequently used in expressions, loops, and real-world calculations.
๐ฏ In this guide, youโll learn:
- The list of arithmetic operators in C
- Syntax and usage examples
- Common issues like integer division and precedence
- How to use arithmetic operators in real-world expressions
โ List of Arithmetic Operators
Operator | Name | Description | Example (a = 10 , b = 3 ) |
---|---|---|---|
+ | Addition | Adds two operands | a + b = 13 |
- | Subtraction | Subtracts second operand from first | a - b = 7 |
* | Multiplication | Multiplies two operands | a * b = 30 |
/ | Division | Divides first operand by second | a / b = 3 |
% | Modulus | Returns remainder of division | a % b = 1 |
๐งช Examples of Arithmetic Operator Usage
#include <stdio.h>
int main() {
int a = 15, b = 4;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b); // Integer division
printf("Modulus: %d\n", a % b); // Remainder
return 0;
}
Output:
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3
Modulus: 3
โ ๏ธ Integer vs Floating-Point Division
In C, when both operands are integers, division returns an integer result, truncating the decimal part.
โ Example:
int a = 5, b = 2;
float result = a / b; // result = 2.0 (not 2.5!)
To get a decimal result, use type casting:
float result = (float)a / b; // result = 2.5
๐ Operator Precedence and Associativity
Arithmetic operators follow precedence:
Precedence | Operators | Associativity |
---|---|---|
High | * / % | Left to Right |
Low | + - | Left to Right |
โ Example:
int result = 5 + 2 * 3; // result = 11
Use parentheses to control precedence:
int result = (5 + 2) * 3; // result = 21
๐ Summary โ Recap & Next Steps
Arithmetic operators are fundamental in C programs for performing calculations and building logic. Mastering how they work with integers and floating-point numbers is essential for creating robust and bug-free code.
๐ Key Takeaways:
- Arithmetic operators include
+
,-
,*
,/
,%
- Integer division truncates decimals
- Use casting to ensure float division
- Precedence matters โ use parentheses to control evaluation order
โ๏ธ Real-World Relevance:
Used in billing systems, statistics, sensors, financial tools, and any application where math or formula-based logic is needed.
โ Frequently Asked Questions (FAQ)
โ What is the difference between /
and %
?
โ
/
performs division and returns the quotient, while %
returns the remainder of integer division.
โ Does C support exponentiation with ^
?
โ No. ^
is a bitwise XOR operator in C. Use the pow()
function from <math.h>
for exponentiation:
#include <math.h>
double result = pow(2, 3); // 8.0
โ Can arithmetic operators be used with float
and double
?
โ Yes. All arithmetic operators work with both integers and floating-point numbers. Just be mindful of type casting and precision.
โ What happens in division by zero?
โ Undefined behavior. Division by zero will crash your program or cause an exception. Always check for b != 0
before performing division.
โ What is the result of 7 / 2
in C?
โ Since both operands are integers, the result is an integer:
int result = 7 / 2; // result = 3
Use float casting for decimal output:
float result = (float)7 / 2; // result = 3.5
Share Now :