Unit 5: Operators and Expressions in C | B. Sc Year: III

🧭 Introduction

In C programming, operators are symbols that perform operations on data, and expressions are combinations of operators, variables, and constants that produce a result.

This unit is extremely important because:

  • Almost every C program uses operators

  • Logical thinking and problem-solving depend on expressions

  • Many short and long exam questions come directly from this unit

  • Understanding operator precedence avoids logical errors

Learning operators and expressions makes your programs correct, efficient, and readable.


🔑 Key Concepts (Explained Simply)


1️⃣ Arithmetic Operators

Arithmetic operators perform basic mathematical operations.

OperatorMeaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (remainder)

Example:

int a = 10, b = 3;
int sum = a + b;
int remainder = a % b;

✅ Used for totals, averages, and formulas.


2️⃣ Unary Operators

Unary operators work with only one operand.

Types:

Increment (++) and Decrement (--)

int x = 5;
x++;   // x becomes 6

Unary Minus (-)

int a = -10;

Logical NOT (!)

if (!(a > b)) { ... }

Bitwise NOT (~)

unsigned int x = 5;
int y = ~x;

3️⃣ Relational Operators

Relational operators compare two values and return true (1) or false (0).

OperatorMeaning
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
==Equal to
!=Not equal

Example:

if (a > b) {
    printf("a is greater");
}

4️⃣ Logical Operators

Logical operators combine conditions.

OperatorMeaning
&&Logical AND
`
!Logical NOT

Example:

if (a > 0 && b > 0) {
    printf("Both positive");
}

✅ Widely used in decision-making statements.


5️⃣ Assignment Operators

Assignment operators assign values to variables.

Basic Assignment:

x = 10;

Compound Assignment:

OperatorExampleMeaning
+=x += 5x = x + 5
-=x -= 3x = x - 3
*=x *= 2x = x * 2
/=x /= 4x = x / 4

6️⃣ Increment and Decrement (Pre & Post)

Pre-Increment:

int a = 5;
int b = ++a;   // a = 6, b = 6

Post-Increment:

int a = 5;
int b = a++;   // b = 5, a = 6

✅ Exam tip: Always explain pre vs post with examples.


7️⃣ Conditional (Ternary) Operator

Short form of if–else.

condition ? value_if_true : value_if_false;

Example:

int max = (a > b) ? a : b;

✅ Common short-answer exam question.


8️⃣ Bitwise Operators

Operate at the bit level.

OperatorMeaning
&Bitwise AND
``
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Right shift

Example:

int a = 5, b = 3;
int c = a & b;

✅ Mostly theoretical in exams.


9️⃣ Comma Operator

Allows multiple expressions in one statement.

int a = 1, b = 2, c = 3;
int result = (a++, b++, c++);

✅ Value of expression = last expression.


🔟 Expressions in C

An expression combines:

  • Variables

  • Constants

  • Operators

Example:

int result = a + b * 2;

✅ Always follow operator precedence rules.


1️⃣1️⃣ Operator Precedence

Determines order of evaluation.

Common order (High → Low):

  1. ()

  2. Unary operators: ++ -- !

  3. * / %

  4. + -

  5. < > <= >=

  6. == !=

  7. &&

  8. ||

  9. =

Example:

int x = 10 + 5 * 2;   // result = 20

✅ Use parentheses to avoid confusion.


1️⃣2️⃣ Type Conversion (Typecasting)

Implicit Conversion: happens automatically.

int a = 5;
float b = 2.5;
float c = a + b;

Explicit Conversion (Typecasting):

float x = 3.14;
int y = (int) x;

✅ Typecasting is a frequent exam topic.


🧩 Step-by-Step Example Programs


✅ Program 1: Modulo Operator

#include <stdio.h>

int main() {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    printf("Remainder = %d", a % b);
    return 0;
}

✅ Program 2: Conditional Operator

#include <stdio.h>

int main() {
    int a, b, max;
    scanf("%d %d", &a, &b);
    max = (a > b) ? a : b;
    printf("Maximum = %d", max);
    return 0;
}

✅ Program 3: Typecasting

#include <stdio.h>

int main() {
    float x = 5.6;
    int y = (int)x;
    printf("Integer value = %d", y);
    return 0;
}

📝 Important Exam Questions

  1. Define arithmetic and unary operators

  2. Explain relational and logical operators with examples

  3. Difference between pre and post increment

  4. What is conditional operator?

  5. Explain operator precedence

  6. What is typecasting? Give example

  7. Write a C program using modulo operator


🎯 Exam Tips / Key Points

  • Always write syntax + example

  • Use tables for differences

  • Parentheses improve clarity and fetch extra marks

  • Explain precedence order clearly

  • Mention true = 1, false = 0


✅ Short Summary

Unit 5 explains how C programs perform operations and evaluations using operators and expressions. You learned:

  • Categories of operators

  • How expressions are evaluated

  • Operator precedence and associativity

  • Type conversion and typecasting

  • Exam-oriented C programs

This unit builds your logical foundation for mastering control statements next.

Netra Koirala

Netra Koirala

Computer Science Educator

Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.

Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.

Featured Post

Grade 10 Computer Science: Specification Grid & Model Questions

Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...

Followers