An expression is a formula that combines operands (like variables and constants) with operators (like +
or *
) to produce a value. An arithmetic expression is a type of expression that results in a numeric value, such as an int
or a float
.
For example, price * 1.05
is an arithmetic expression. Here, price
and 1.05
are operands, and *
is the operator.
Arithmetic Operators in C
C provides five basic arithmetic operators for performing calculations.
Operator |
Name |
Description |
Example (if a=10, b=3) |
|
Addition |
Adds two operands. |
|
|
Subtraction |
Subtracts the second operand from the first. |
|
|
Multiplication |
Multiplies two operands. |
|
|
Division |
Divides the first operand by the second. |
|
|
Modulo |
Computes the remainder of an integer division. |
|
Important
Note on Division (/
):
·
Integer
Division: If both operands are
integers, the result is an integer, and any fractional part is truncated
(discarded). For example, 10 / 3
evaluates to 3
.
·
Floating-Point
Division: If at least one of the
operands is a floating-point type (float
or double
), the result is a floating-point number. For example,
10.0
/ 3
evaluates to 3.333333
.
Note on Modulo
(%
): The
modulo operator can only be used with integer types.
Types of Arithmetic Expressions
1. Integer Expressions: An expression where all operands are integer types. The resulting
value is always an integer. int x = 10, y = 5; int result = x * y; //
Result is 50
2. Floating-Point (Real) Expressions: An expression where all operands are floating-point
types. The resulting value is a float
or double
. float a = 2.5, b = 1.5; float result = a +
b; // Result is 4.0
3. Mixed-Mode Expressions: An expression containing a mix of integer and
floating-point operands. In this case, C performs implicit type conversion,
promoting the "lower" type (int
) to the "higher" type (float
or double
) before the calculation. The result is always a
floating-point type. int p = 5; float q = 2.0; float result = p / q; // p
is promoted to 5.0f; result is 2.5
Example Program
This program demonstrates the different operators and expression types.
C
#
int main() {
int a =
15, b =
4;
float x =
2.5f;
// --- Integer Expressions ---
printf(
"--- Integer Arithmetic ---\n");
printf(
"Addition (a + b): %d\n", a + b);
// 15 + 4 = 19
printf(
"Subtraction (a - b): %d\n", a - b);
// 15 - 4 = 11
printf(
"Multiplication (a * b): %d\n", a * b);
// 15 * 4 = 60
printf(
"Division (a / b): %d\n", a / b);
// 15 / 4 = 3 (truncated)
printf(
"Modulo (a %% b): %d\n", a % b);
// 15 % 4 = 3 (remainder)
// --- Mixed-Mode Expressions ---
printf(
"\n--- Mixed-Mode Arithmetic ---\n");
// 'a' is promoted to a float (15.0f) before the operation
printf(
"Addition (a + x): %f\n", a + x);
// 15.0 + 2.5 = 17.5
printf(
"Division (a / x): %f\n", a / x);
// 15.0 / 2.5 = 6.0
return
0;
}
Operator precedence in C defines the order in which operators in a complex expression are evaluated. For operators with the same precedence, associativity determines the evaluation order.