Chapter 4: Operators and Expressions

📌 Learning Objectives
  • 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

OperatorMeaningExample
+Addition or unary plusa + b
-Subtraction or unary minusa - b
*Multiplicationa * b
/Divisiona / b
%Modulo division (remainder)a % b

Integer Arithmetic

When both operands are integers, the result is an integer. For a=14, b=4:

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;
}
Output:
Enter number of days: 75
Months = 2, Days = 15

4.2 Relational Operators

OperatorMeaningExample
<is less thana < b
<=is less than or equal toa <= b
>is greater thana > b
>=is greater than or equal toa >= b
==is equal toa == b
!=is not equal toa != b

Relational expressions yield 1 (true) or 0 (false).

4.3 Logical Operators

OperatorMeaningExample
&&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 AssignmentShorthand
a = a + 1a += 1
a = a - 1a -= 1
a = a * (n+1)a *= n+1
a = a / (n+1)a /= n+1
a = a % ba %= 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;
}
Output:
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

OperatorMeaning
&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;
}
Output:
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;
}
Output:
x = 10.000000
y = 7.000000
z = 4.000000

4.10 Operator Precedence and Associativity

LevelOperatorsAssociativity
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
💡 Evaluation of Expression:
9 - 12 / 3 + 3 * 2 - 1
Step 1: 12/3 = 4
Step 2: 3*2 = 6
Step 3: 9 - 4 = 5
Step 4: 5 + 6 = 11
Step 5: 11 - 1 = 10
⚠️ Common Pitfalls:
  • 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

  1. 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.
  2. What will be the value of x after: int x = 10, y = 3; x = x / y * y;
  3. Determine the value of: !(5 + 5 >= 10)
  4. Which of the following are valid? 25/3%2, 7.5%3, 14%3+7%2

Multiple Choice Questions

  1. Which of the following is not an arithmetic operator?
    a) %
    b) /
    c) *
    d) All are arithmetic operators
  2. Which operator has the lowest precedence?
    a) %
    b) &&
    c) =
    d) ,
  3. 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

  1. Write a program that reads a floating-point number and displays the rightmost digit of its integral part.
  2. Given a four-digit integer, print the sum of its digits (using / and %).
  3. Write a program to read three values and print the largest using the conditional operator.
  4. Write a program to compute the roots of a quadratic equation.
  5. Write a program to swap two variables without using a third variable.

Interview Questions

  1. What will be the value of x and y after: x = 5; y = x++ + ++x + x++;
  2. Evaluate the logical expression: !(1 && !(0 || 1))
  3. What is the difference between | and ||?
  4. Write code to swap two variables without using a third variable.