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.
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 answerHide 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
| Operator | Example | Meaning |
|---|---|---|
= | a = 5 | Assigns 5 to a |
Compound Assignment Operators
These combine an arithmetic operation with assignment:
| Operator | Example | Equivalent To |
|---|---|---|
+= | a += 5 | a = a + 5 |
-= | a -= 5 | a = a - 5 |
*= | a *= 5 | a = a * 5 |
/= | a /= 5 | a = a / 5 |
%= | a %= 5 | a = 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
| Operator | Name | Example | Result |
|---|---|---|---|
&& | Logical AND | a && b | True only if both a and b are true |
|| | Logical OR | a || b | True if at least one of a or b is true |
! | Logical NOT | !a | True if a is false, and vice versa |
Truth Tables
AND (&&)
| A | B | A && B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
OR (||)
| A | B | A || B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
NOT (!)
| A | !A |
|---|---|
| 0 | 1 |
| 1 | 0 |
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, andforconditions
2079 · 5 marks
What is operator associativity? Explain conditional and Logical operators in brief. [5]
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 answerHide 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:
| Expression | Type |
|---|---|
a + b * c | Arithmetic expression |
x > y | Relational expression |
a = 10 | Assignment expression |
a && b | Logical 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:
| Type | Operators | Example |
|---|---|---|
| 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 Type | Operators | Associativity |
|---|---|---|
| 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.
Logical operators
2080 · 5 marks
List out different operators in C. Explain Logical and relational operator. [5]
2081.2 · 5 marks
Give any two types of logical operators. What are formatted and unformatted I/O means? [5]
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]
Make Unit 3 stick
Practice BIT102 with flashcards & quizzes