2.6 OPERATORS AND EXPRESSIONS

                                        

     Programming in C    



OPERATORS AND EXPRESSIONS IN C

( Pro Tip : Visit the Diagram first at bottom to better Understand First)

An operator is a symbol that specifies the mathematical, logical, relational, or bitwise operation to be performed on operands.
An expression is a combination of variables, constants, and operators that evaluates to a single value.

When an expression is evaluated, it may produce:

  • a final value, and

  • side effects, which are permanent changes in program state (such as assignment, increment, or decrement).

Thus, C expressions differ from mathematical expressions because they may cause side effects.


Classification of Operators in C




C operators are classified as shown in Fig. 8.16 into:

  • Arithmetic Operators

  • Assignment Operators

  • Relational Operators

  • Equality Operators

  • Logical Operators

  • Bitwise Operators

  • Conditional (Ternary) Operator

  • Comma Operator

  • Other Operators (sizeof, membership, indirection)


Types of Operators in C 





1. Arithmetic Operators

  • Unary: +, -, ++, --

  • Binary: +, -, *, /, %

  • Ternary: ?: (conditional)

2. Assignment Operators

  • Simple Assignment: =

  • Compound Assignment: +=, -=, *=, /=, %= , &=, ^=, |=

3. Relational Operators

>, <, >=, <=

4. Equality Operators

==, !=

5. Logical Operators

&&, ||, !

6. Bitwise Operators

&, |, ~, ^, <<, >>

7. Others

  • , (comma)

  • * (indirection)

  • . and -> (membership)


1 Arithmetic Operators in C

Arithmetic operators are used to perform numeric computations.
They are classified into binary, unary, and ternary operators.

1.1 Binary Arithmetic Operators 

OperatorOperationExampleResult
+Addition12 + 4.916.9
-Subtraction3.98 – 4–0.02
*Multiplication2 * 3.46.8
/Division9 / 2.04.5
%Remainder13 % 31

Important Points

  • If both operands are integers, result is integer

  • If any operand is real, result is real

  • Integer division truncates the remainder
    Example:
    9/2 → 4, –9/2 → –4

  • % operator:

    • Works only on integers

    • Returns remainder

    • Cannot be applied to float or double

Division by Zero

  • Illegal operation

  • Results in run-time error

  • Behaviour is undefined

Overflow

Occurs when result exceeds storage capacity.
Outcome is machine dependent and undefined.

1.2 Unary Operators

Unary Minus (–)

  • Negates the operand

  • Does not modify original value

Increment and Decrement Operators

  • ++ (increment)

  • -- (decrement)

Rules

  • Operand must be a variable

  • Cannot be applied to constants or expressions

1.3 Pre and Post Increment / Decrement

Prefix (++a / --a)

  • Value is changed first

  • Then used in expression

Postfix (a++ / a--)

  • Value is used first

  • Then modified

Example

b = ++a; // pre-increment

Result: a = 6, b = 6

b = a++; // post-increment

Result: a = 6, b = 5

Important Note

  • Order of evaluation of function arguments is compiler-dependent

  • Expressions like:

printf("%d %d", x++, ++x);

have undefined behaviour


2 Relational Operators in C

Relational operators are used to compare two numeric values.
They evaluate to:

  • 1 → true

  • 0 → false

Relational Operators 

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

Important Points

  • Operands must be numeric

  • Characters are allowed (ASCII values)

  • Strings must not be compared using relational operators

    • Use strcmp() instead


3 Logical Operators in C

Logical operators are used to form logical expressions.

Logical Operators 

OperatorMeaning
!Logical NOT
&&Logical AND
**

Truth Rules

  • Non-zero → true

  • Zero → false

Short-Circuit Evaluation

  • In &&, if left operand is false, right operand is not evaluated

  • In ||, if left operand is true, right operand is not evaluated


4 Bitwise Operators in C

Bitwise operators operate on individual bits of integer operands.

Bitwise Operators 

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

Key Points

  • Operands must be integers

  • Bits shifted left/right by specified positions

  • Vacated bits are filled with 0

  • Generally used with unsigned types


 5 Conditional (Ternary) Operator

The conditional operator has three operands.

Syntax

expression1 ? expression2 : expression3
  • If expression1 is true, expression2 is evaluated

  • Else, expression3 is evaluated

  • Only one expression is evaluated

Example

min = (m < n ? m : n);

Equivalent to if–else construct.


6 Comma Operator

The comma operator allows multiple expressions to be evaluated from left to right.

  • Value of last expression is returned

  • Ensures sequence control

Example

j = (i += 1, i += 2, i + 3);

Final value assigned to j is 6

Applications

  • Used in for loops

  • Ensures complete evaluation order

  • Acts as a sequence point


7 sizeof Operator

The sizeof operator returns the size (in bytes) of a data type or expression.

Characteristics

  • Compile-time operator

  • Machine dependent

  • Accepts type or expression

Example

sizeof(int) sizeof(1.55) sizeof("HELLO")

8 Expression Evaluation – Precedence and Associativity

Precedence

  • Determines which operator is evaluated first

Associativity

  • Determines direction of evaluation when precedence is same

Key Points

  • Unary operators > Binary operators > Ternary operator

  • Assignment has low precedence

  • Comma operator has lowest precedence

  • Parentheses override precedence

Example

n = 52 * 79;

Evaluation:

  • * first → 5 – 14 – 9

  • Left-to-right

  • Final result: –18


Conclusion

Operators and expressions form the core of computation in C.
Understanding operator types, side effects, precedence, and associativity is essential to write correct, portable, and efficient programs. Improper use may lead to undefined behaviour, making careful usage critical in C programming.








📘 Reference

Prepared strictly from
“Computer Fundamentals and Programming in C”
Pradip Dey & Manas Ghosh
Oxford University Press, Second Edition



Aivette-COI
Learn smart. Write clean. Score high.

With love 🤍
Aivette

Comments

Popular posts from this blog

22IMC7Z2 - CONSTITUTION OF INDIA

1.1 Historical Background of the Indian Constitution: The Company Rule & The Crown Rule

1.3 Preamble and Salient Features of the Indian Constitution