3 Operators And Expressions

C Programming · Unit 3

Operators and Expressions

Exam-focused notes for Operators and Expressions (C Programming, BIT102): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.

Operators and Expressions in short

This chapter covers the symbols that act on data and the rules for evaluating them. Almost every C statement uses operators, so exact knowledge of precedence is important.

Operators and Expressions: notes

Arithmetic operators

Arithmetic operators do mathematical work.

OperatorMeaningExample
+Addition5 + 2 gives 7
-Subtraction5 - 2 gives 3
*Multiplication5 * 2 gives 10
/Division5 / 2 gives 2
%Remainder5 % 2 gives 1

When both operands are integers, / gives an integer result and drops the fraction. The % operator works only on integers. The increment ++ and decrement -- operators add or subtract 1.

int a = 5;
int b = a++;   /* b = 5, then a = 6 (post-increment) */
int c = ++a;   /* a = 7, then c = 7 (pre-increment) */

Relational operators

Relational operators compare two values. The result is 1 for true and 0 for false. They are <, >, <=, >=, == and !=.

int x = 10, y = 20;
printf("%d %d %d\n", x < y, x == y, x != y);   /* 1 0 1 */

A common mistake is to write = (assignment) instead of == (comparison) inside an if.

Logical operators

Logical operators combine conditions.

  • && (AND): true only if both sides are true.
  • || (OR): true if at least one side is true.
  • ! (NOT): reverses the truth value.

The truth table for two conditions A and B is:

  • A = 0, B = 0 gives AND = 0 and OR = 0.
  • A = 0, B = 1 gives AND = 0 and OR = 1.
  • A = 1, B = 0 gives AND = 0 and OR = 1.
  • A = 1, B = 1 gives AND = 1 and OR = 1.

C uses short-circuit evaluation. In a && b, if a is false then b is not evaluated. In a || b, if a is true then b is skipped. In C, any non-zero value counts as true.

if (age >= 18 && age <= 60)
    printf("Eligible\n");

What this unit covers

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Assignment operators
  • Conditional operators
  • Operator precedence and associativity
  • Comma operator
  • Expressions and expression evaluation

Operator precedence and associativity

2081 · 5 marks

What is operator precedence? Explain assignment and logical operator. [1+4]

Show solved answer

Operator Precedence, Assignment and Logical Operators


Operator Precedence [1 mark]

Operator precedence defines the order (priority) in which operators are evaluated in an expression when multiple operators appear together. An operator with higher precedence is evaluated before an operator with lower precedence.

Example:

2 + 3 * 4  =  2 + 12  =  14

Here, * has higher precedence than +, so multiplication is performed first.


Assignment Operator [2 marks]

The assignment operator is used to assign a value to a variable. The basic assignment operator is =.

Syntax:

variable = expression;

Simple Assignment

OperatorExampleMeaning
=a = 5Assigns 5 to a

Compound Assignment Operators

These combine an arithmetic operation with assignment:

OperatorExampleEquivalent To
+=a += 5a = a + 5
-=a -= 5a = a - 5
*=a *= 5a = a * 5
/=a /= 5a = a / 5
%=a %= 5a = a % 5

Key Points:

  • Assignment is right-to-left associative
  • Multiple assignments are possible: a = b = c = 10; (assigns 10 to all)
  • The right-hand side is evaluated first, then assigned to the left-hand side variable

Logical Operators [2 marks]

Logical operators are used to combine or negate boolean/conditional expressions. They return either true (1) or false (0).

Types of Logical Operators

OperatorNameExampleResult
&&Logical ANDa && bTrue only if both a and b are true
||Logical ORa || bTrue if at least one of a or b is true
!Logical NOT!aTrue if a is false, and vice versa

Truth Tables

AND (&&)

ABA && B
000
010
100
111

OR (||)

ABA || B
000
011
101
111

NOT (!)

A!A
01
10

Example in C:

int a = 5, b = 10, c = 0;

if (a > 0 && b > 0)       // true && true = true
    printf("Both positive");

if (a > 0 || c > 0)       // true || false = true
    printf("At least one positive");

if (!c)                    // !0 = true
    printf("c is zero");

Key Points:

  • && and || use short-circuit evaluation: if the result is determined from the first operand, the second is not evaluated
  • ! is a unary operator (operates on one operand)
  • Logical operators are commonly used in if, while, and for conditions
Open this answer on its own page →

2079 · 5 marks

What is operator associativity? Explain conditional and Logical operators in brief. [5]

Operator associativity defines the order in which operators of the same precedence are evaluated in an expression when they appear together. There are two types: Type Direction Example Operators ------------------------------------ Left-to-right Evaluated left first +, -, , /, % Right-to-left Eva...

Open this answer on its own page →

2078 · 10 marks

What is an expression and operator? Discuss operator associativity in C.Write a program to find factorial of a given integer using recursion.[5+5]

Show solved answer

Expression, Operator, Operator Associativity in C + Factorial Using Recursion


(a) Expression and Operator

Expression

An expression in C is a valid combination of operands (variables, constants) and operators that evaluates to a single value.

Examples:

ExpressionType
a + b * cArithmetic expression
x > yRelational expression
a = 10Assignment expression
a && bLogical expression

Operator

An operator is a special symbol that instructs the compiler to perform a specific mathematical, logical, or relational operation on one or more operands.

Types of Operators in C:

TypeOperatorsExample
Arithmetic+, -, *, /, %a + b
Relational<, >, <=, >=, ==, !=a > b
Logical&&, ||, !a && b
Assignment=, +=, -=, *=a = 5
Unary++, --, -a++
Bitwise&, |, ^, ~a & b
Conditional? :a>b ? a : b

Operator Associativity

Associativity defines the direction of evaluation when two or more operators of the same precedence appear in a single expression. It resolves ambiguity in such expressions.

There are two types:

  • Left to Right (L to R): Evaluation proceeds from left side to right side
  • Right to Left (R to L): Evaluation proceeds from right side to left side

Associativity Table:

Operator TypeOperatorsAssociativity
Unary++, --, !, - (unary)Right to Left
Arithmetic (high)*, /, %Left to Right
Arithmetic (low)+, -Left to Right
Relational<, >, <=, >=Left to Right
Equality==, !=Left to Right
Logical AND&&Left to Right
Logical OR||Left to Right
Assignment=, +=, -=, *=Right to Left

Example 1 (Left to Right):

10 - 4 - 2
= (10 - 4) - 2    // Left to Right
= 6 - 2
= 4

Example 2 (Right to Left):

a = b = c = 5;
// Evaluated as: a = (b = (c = 5))
// c = 5 first, then b = 5, then a = 5

Conclusion: Associativity ensures that expressions with operators of equal precedence are evaluated in a well-defined, unambiguous order.


(b) Program to Find Factorial Using Recursion

Concept of Recursion

Recursion is a programming technique where a function calls itself repeatedly to solve a smaller sub-problem, until a base condition is reached that stops further calls.

Factorial Definition:

factorial(n) = n × factorial(n-1),   for n > 1
factorial(0) = 1                      (base case)
factorial(1) = 1                      (base case)

C Program

#include <stdio.h>

// Recursive function to calculate factorial
int factorial(int n) {
    if (n == 0 || n == 1)       // Base condition
        return 1;
    else
        return n * factorial(n - 1);  // Recursive call
}

int main() {
    int num;
    printf("Enter a positive integer: ");
    scanf("%d", &num);

    if (num < 0)
        printf("Factorial is not defined for negative numbers.");
    else
        printf("Factorial of %d = %d\n", num, factorial(num));

    return 0;
}

Sample Output

Enter a positive integer: 5
Factorial of 5 = 120

Trace of Recursive Calls for n = 5

factorial(5)
  = 5 × factorial(4)
        = 4 × factorial(3)
              = 3 × factorial(2)
                    = 2 × factorial(1)
                          = 1  (base case)
                    = 2 × 1 = 2
              = 3 × 2 = 6
        = 4 × 6 = 24
  = 5 × 24 = 120

Final Result: 5! = 120


Key Point: Every recursive function must have a base case to prevent infinite recursion and stack overflow.

Open this answer on its own page →

Logical operators

2080 · 5 marks

List out different operators in C. Explain Logical and relational operator. [5]

The different types of operators in C are: 1. Arithmetic Operators (+, -, , /, %) 2. Relational Operators (<, , <=, =, ==, !=) 3. Logical Operators (&&, , !) 4. Assignment Operators (=, +=, -=, =, /=) 5. Increment/Decrement Operators (++, --) 6. Bitwise Operators (&, , ^, ~, <<, ) 7. Conditional ...

Open this answer on its own page →

2081.2 · 5 marks

Give any two types of logical operators. What are formatted and unformatted I/O means? [5]

Note: No specific reference notes were found for this topic. The answer below is based on standard C programming curriculum as taught in TU BSc CSIT. --- Logical operators are used to combine or negate conditional expressions and return a true (1) or false (0) result. - Returns true only if both ...

Open this answer on its own page →

Relational operators

0 · 5 marks

What are relational operators and arithmetic operators? Write a C code to check if a given number is odd or even. [5]

Relational operators are used to compare two values or expressions. They return either true (1) or false (0) based on the comparison. Operator Meaning Example ---------------------------- == Equal to a == b != Not equal to a != b Greater than a b < Less than a < b = Greater than or equal to a = b...

Open this answer on its own page →