In C language, arithmetic operators are used to perform basic mathematical operations on variables and values. These operators work with both integer and floating-point numbers. Here are the basic arithmetic operators in C:
List of Arithmetic Operators
Addition (
+
):- Adds two operands.
- Example:
int a = 10, b = 5; int result = a + b; // result is 15
Subtraction (
-
):- Subtracts the second operand from the first.
- Example:
int a = 10, b = 5; int result = a - b; // result is 5
Multiplication (
*
):- Multiplies two operands.
- Example:
int a = 10, b = 5; int result = a * b; // result is 50
Division (
/
):- Divides the first operand by the second.
- Example:
int a = 10, b = 5; int result = a / b; // result is 2
- Note: When dividing two integers, the result is also an integer, and any fractional part is discarded.
Modulus (
%
):- Returns the remainder of the division of two integers.
- Example:
int a = 10, b = 3; int result = a % b; // result is 1
- The modulus operator can only be used with integer operands.
Important Points
Division with Floats: When using the division operator with floating-point numbers, it yields a floating-point result:
float a = 10.0, b = 4.0; float result = a / b; // result is 2.5
Operator Precedence: Arithmetic operators follow the standard precedence rules (e.g., multiplication and division are evaluated before addition and subtraction unless parentheses are used).
int result = 10 + 5 * 2; // result is 20, as multiplication happens before addition int result2 = (10 + 5) * 2; // result is 30, due to parentheses
Example Program
#include <stdio.h>
int main() {
int a = 15, b = 4;
printf("Addition: %d\n", a + b); // 19
printf("Subtraction: %d\n", a - b); // 11
printf("Multiplication: %d\n", a * b); // 60
printf("Division: %d\n", a / b); // 3 (integer division)
printf("Modulus: %d\n", a % b); // 3
return 0;
}
In this example, different arithmetic operations are performed and printed on the console.
Summary
- Addition (
+
): Adds two numbers. - Subtraction (
-
): Subtracts the second number from the first. - Multiplication (
*
): Multiplies two numbers. - Division (
/
): Divides the first number by the second. - Modulus (
%
): Finds the remainder after division.
These operators are fundamental in performing calculations in C programs.