Chapter 4: Operators and Expressions
- LO 4.1: Know the various built-in operators of C language
- LO 4.2: Identify bitwise and special operators
- LO 4.3: Determine how arithmetic expressions are evaluated
- LO 4.4: Explain type conversions in expressions
- LO 4.5: Discuss how operator precedence and associativity rules are applied
4.1 Arithmetic Operators
| Operator | Meaning | Example |
|---|---|---|
| + | Addition or unary plus | a + b |
| - | Subtraction or unary minus | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulo division (remainder) | a % b |
Integer Arithmetic
When both operands are integers, the result is an integer. For a=14, b=4:
- a / b = 3 (decimal part truncated)
- a % b = 2 (remainder)
- Sign of result in modulo operation: sign of first operand (dividend)
- -14 % 3 = -2, -14 % -3 = -2, 14 % -3 = 2
Real Arithmetic
Operations involving only real operands. The % operator cannot be used with real operands.
Mixed-mode Arithmetic
If one operand is real and the other is integer, the result is always a real number.
15 / 10.0 = 1.5 15 / 10 = 1 (integer division)
📝 Worked-Out Problem 4.1
Converting days to months and days:
#include <stdio.h>
int main() {
int days, months;
printf("Enter number of days: ");
scanf("%d", &days);
months = days / 30;
days = days % 30;
printf("Months = %d, Days = %d\n", months, days);
return 0;
}
Enter number of days: 75
Months = 2, Days = 15
4.2 Relational Operators
| Operator | Meaning | Example |
|---|---|---|
| < | is less than | a < b |
| <= | is less than or equal to | a <= b |
| > | is greater than | a > b |
| >= | is greater than or equal to | a >= b |
| == | is equal to | a == b |
| != | is not equal to | a != b |
Relational expressions yield 1 (true) or 0 (false).
4.3 Logical Operators
| Operator | Meaning | Example |
|---|---|---|
| && | Logical AND | (a > b) && (x == 10) |
| || | Logical OR | (a < b) || (x != 5) |
| ! | Logical NOT | !(a > b) |
4.4 Assignment Operators
Shorthand assignment operators: op=
| Simple Assignment | Shorthand |
|---|---|
| a = a + 1 | a += 1 |
| a = a - 1 | a -= 1 |
| a = a * (n+1) | a *= n+1 |
| a = a / (n+1) | a /= n+1 |
| a = a % b | a %= b |
4.5 Increment and Decrement Operators
++ adds 1 to the operand, -- subtracts 1.
Prefix vs Postfix:
m = 5; y = ++m; // m = 6, y = 6 (increment then assign) y = m++; // m = 6, y = 5 (assign then increment)
📝 Worked-Out Problem 4.2
Using shorthand operator *= :
#include <stdio.h>
#define N 100
int main() {
int a = 2;
while (a < N) {
printf("%d\n", a);
a *= a;
}
return 0;
}
2
4
16
4.6 Conditional Operator (Ternary)
expression1 ? expression2 : expression3
If expression1 is true (non-zero), expression2 is evaluated; otherwise expression3 is evaluated.
max = (a > b) ? a : b; // max gets the larger of a and b
4.7 Bitwise Operators
| Operator | Meaning |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise exclusive OR (XOR) |
| << | Shift left |
| >> | Shift right |
| ~ | One's complement (NOT) |
These operators work only on integer types.
4.8 Special Operators
Comma Operator
value = (x = 10, y = 5, x + y); // value = 15
Expressions are evaluated left to right; the value of the rightmost expression is returned.
sizeof Operator
int size = sizeof(int); // returns size of int in bytes int bytes = sizeof(x); // size of variable x
📝 Worked-Out Problem 4.3
Illustration of various operators:
#include <stdio.h>
int main() {
int a = 15, b = 10, c, d, e, f;
c = ++a - b;
d = b++ + a;
e = a % b;
f = a / b;
printf("a=%d b=%d c=%d d=%d e=%d f=%d\n", a, b, c, d, e, f);
return 0;
}
a=16 b=11 c=6 d=27 e=5 f=1
4.9 Type Conversions
Implicit Type Conversion
When operands are of different types, the 'lower' type is converted to the 'higher' type:
int i = 10; float f = 3.5; float result = i + f; // i converted to float (10.0), result = 13.5
Explicit Conversion (Casting)
float x = 10.5, y = 3.5; int result = (int)x + (int)y; // result = 10 + 3 = 13
📝 Worked-Out Problem 4.4
Evaluation of expressions with parentheses:
#include <stdio.h>
int main() {
float a = 9, b = 12, c = 3, x, y, z;
x = a - b / 3 + c * 2 - 1;
y = a - b / (3 + c) * (2 - 1);
z = a - (b / (3 + c) * 2) - 1;
printf("x = %f\n", x);
printf("y = %f\n", y);
printf("z = %f\n", z);
return 0;
}
x = 10.000000
y = 7.000000
z = 4.000000
4.10 Operator Precedence and Associativity
| Level | Operators | Associativity |
|---|---|---|
| 1 (Highest) | () [] -> . | Left to right |
| 2 | ! ~ ++ -- + - (unary) * & sizeof (cast) | Right to left |
| 3 | * / % | Left to right |
| 4 | + - | Left to right |
| 5 | << >> | Left to right |
| 6 | < <= > >= | Left to right |
| 7 | == != | Left to right |
| 8 | & | Left to right |
| 9 | ^ | Left to right |
| 10 | | | Left to right |
| 11 | && | Left to right |
| 12 | || | Left to right |
| 13 | ?: | Right to left |
| 14 | = += -= *= /= %= &= ^= |= <<= >>= | Right to left |
| 15 | , | Left to right |
9 - 12 / 3 + 3 * 2 - 1Step 1: 12/3 = 4
Step 2: 3*2 = 6
Step 3: 9 - 4 = 5
Step 4: 5 + 6 = 11
Step 5: 11 - 1 = 10
- Division by zero leads to program termination.
- Floating point comparisons for equality may fail due to rounding errors.
- Integer division truncates the fractional part.
- Using = instead of == in conditions is a common logical error.
Chapter Exercises
Review Questions
- State whether true or false:
a) The expression !(x <= y) is same as x > y.
b) All arithmetic operators have the same level of precedence.
c) The modulus operator % can be used only with integers.
d) An explicit cast can be used to change the expression. - What will be the value of x after: int x = 10, y = 3; x = x / y * y;
- Determine the value of: !(5 + 5 >= 10)
- Which of the following are valid? 25/3%2, 7.5%3, 14%3+7%2
Multiple Choice Questions
- Which of the following is not an arithmetic operator?
a) %
b) /
c) *
d) All are arithmetic operators - Which operator has the lowest precedence?
a) %
b) &&
c) =
d) , - Which operator is used to determine the size of an operand?
a) auto
b) size
c) sizeof
d) &
Debugging Exercises
Find errors, if any:
if (m == 1 & n != 0) printf("OK");
if (x =< 5) printf("Jump");
x = y = z = 0.5, 2.0, -5.75;
y = sqrt(100); // assume math.h included
Programming Exercises
- Write a program that reads a floating-point number and displays the rightmost digit of its integral part.
- Given a four-digit integer, print the sum of its digits (using / and %).
- Write a program to read three values and print the largest using the conditional operator.
- Write a program to compute the roots of a quadratic equation.
- Write a program to swap two variables without using a third variable.
Interview Questions
- What will be the value of x and y after: x = 5; y = x++ + ++x + x++;
- Evaluate the logical expression: !(1 && !(0 || 1))
- What is the difference between | and ||?
- Write code to swap two variables without using a third variable.