Part 1 — Chapter 5

Chapter 5: Operators in C

Operators are symbols that tell the compiler to perform an operation on one or more values called operands. Based on how they work, operators in C are classified into nine distinct categories:

1. Arithmetic Operators

Used to perform mathematical calculations on numeric data. These operators follow standard mathematical precedence rules and are commonly used in computations.

2. Relational Operators

Used to compare two values. They evaluate a relationship between operands and always produce a boolean result (true or false), which is internally represented as 1 or 0.

3. Logical Operators

Used to combine or modify conditions. These operators work on boolean results and are mainly used to control the flow of programs through decision-making.

4. Assignment Operators

Used to store values in variables. They can also combine assignment with other operations to simplify expressions and reduce code length.

5. Increment and Decrement Operators

Used to increase or decrease the value of a variable by one. Their behavior depends on whether the operation happens before or after the value is used in an expression.

6. Bitwise Operators

Used to perform operations directly on the binary representation of data. These operators work at the bit level and are important in system-level and embedded programming.

7. Conditional (Ternary) Operator

A compact decision-making operator that selects one of two outcomes based on a condition. It is the only operator in C that uses three operands.

8. Special Operators

Used for specific purposes such as determining memory size (sizeof), accessing addresses/values through pointers, accessing structure/union members, and converting data types.

9. Unary Operators

Operate on a single operand. These operators generally have higher priority and are used for operations like value modification, negation, or address handling.

Operator Reference Table

Assumed Values: a = 10, b = 3, x = 5, i = 5

Operator Description Example
Arithmetic Operators
+Addition
10 + 3 → 13
-Subtraction
10 - 3 → 7
*Multiplication
10 * 3 → 30
/Division
10 / 3 → 3
%Modulus (Remainder)
10 % 3 → 1
Relational Operators
==Equal to
5 == 5 → 1 (True)
!=Not equal to
5 != 3 → 1 (True)
>Greater than
10 > 3 → 1 (True)
<Less than
10 < 3 → 0 (False)
>=Greater or equal
10 >= 10 → 1 (True)
<=Less or equal
3 <= 5 → 1 (True)
Logical Operators
&&AND (Both must be true)
(1>0) && (5>2) → 1
||OR (Either must be true)(1>2) || (5>2) → 1
!NOT (Reverse result)
!(5 == 5) → 0
Assignment Operators
=Assign value
x = 10 → x is 10
+=Add and assign
x += 5 → x is 15
-=Subtract and assign
x -= 2 → x is 8
*=Multiply and assign
x *= 2 → x is 16
Increment / Decrement
++Increment by 1
i++ → i becomes 6
--Decrement by 1
i-- → i becomes 4
Bitwise Operators
&Bitwise AND
5 & 3 → 1
|Bitwise OR
5 | 3 → 7
^Bitwise XOR
5 ^ 3 → 6
~Bitwise NOT
~5 → -6
<<Left Shift
1 << 2 → 4
>>Right Shift
8 >> 1 → 4

Hands-on: C Operator Demonstration

operators.c
C
#include <stdio.h>

int main()
{
    int a = 10, b = 3;
    int x = 5;
    int i = 5;

    printf("===== OPERATORS IN C =====\n\n");

    // 1. Arithmetic Operators
    printf("Arithmetic Operators:\n");
    printf("a + b = %d\n", a + b);    // 13
    printf("a - b = %d\n", a - b);    // 7
    printf("a * b = %d\n", a * b);    // 30
    printf("a / b = %d\n", a / b);    // 3
    printf("a %% b = %d\n\n", a % b); // 1

    // 2. Relational Operators
    printf("Relational Operators:\n");
    printf("a > b  = %d\n", a > b);   // 1 (True)
    printf("a == b = %d\n", a == b);  // 0 (False)
    printf("a < b  = %d\n\n", a < b); // 0 (False)

    // 3. Logical Operators
    printf("Logical Operators:\n");
    printf("(a > b && b > 0) = %d\n", (a > b && b > 0)); // 1
    printf("(a < b || b > 0) = %d\n", (a < b || b > 0)); // 1
    printf("!(a == b) = %d\n\n", !(a == b));             // 1

    // 4. Assignment Operators
    printf("Assignment Operators:\n");
    printf("Initial x = %d\n", x);
    x += 3;
    printf("x += 3 → %d\n", x);       // 8
    x *= 2;
    printf("x *= 2 → %d\n\n", x);     // 16

    // 5. Increment / Decrement
    printf("Increment / Decrement:\n");
    printf("Initial i = %d\n", i);
    printf("i++ = %d\n", i++);        // 5 (Uses current value first)
    printf("After i++ → %d\n", i);    // 6
    printf("++i = %d\n\n", ++i);      // 7 (Increments first)

    // 6. Bitwise Operators
    printf("Bitwise Operators:\n");
    printf("a & b  = %d\n", a & b);   // 2
    printf("a | b  = %d\n", a | b);   // 11
    printf("a ^ b  = %d\n", a ^ b);   // 9
    printf("a << 1 = %d\n", a << 1);  // 20
    printf("a >> 1 = %d\n", a >> 1);  // 5

    return 0;
}
Console Output
===== OPERATORS IN C =====

Arithmetic Operators:
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1

Relational Operators:
a > b  = 1
a == b = 0
a < b  = 0

Logical Operators:
(a > b && b > 0) = 1
(a < b || b > 0) = 1
!(a == b) = 1

Assignment Operators:
Initial x = 5
x += 3 → 8
x *= 2 → 16

Increment / Decrement:
Initial i = 5
i++ = 5
After i++ → 6
++i = 7

Bitwise Operators:
a & b  = 2
a | b  = 11
a ^ b  = 9
a << 1 = 20
a >> 1 = 5

Operator Precedence & Associativity

Precedence: Determines which operator is evaluated first in an expression (e.g., multiplication before addition). These rules ensure that complex expressions are evaluated consistently.

Associativity: Determines the direction of evaluation (Left-to-Right or Right-to-Left) when operators have the same priority level. This is crucial for understanding how chained operations are resolved.

When multiple operators appear in a single expression, C uses Precedence to decide which operation happens first. If two operators have the same precedence, Associativity determines the order.

Operator Precedence Table
Operator Description Associativity
() Function call Left-to-Right
[] Array subscript Left-to-Right
. Structure member access Left-to-Right
-> Structure pointer access Left-to-Right
++ Postfix increment Left-to-Right
-- Postfix decrement Left-to-Right
++ Prefix increment Right-to-Left
-- Prefix decrement Right-to-Left
+ Unary plus Right-to-Left
- Unary minus Right-to-Left
! Logical NOT Right-to-Left
~ Bitwise NOT Right-to-Left
(type) Type cast Right-to-Left
* Dereference Right-to-Left
& Address-of Right-to-Left
sizeof Size of variable/type Right-to-Left
* Multiplication Left-to-Right
/ Division Left-to-Right
% Modulus Left-to-Right
+ Addition Left-to-Right
- Subtraction Left-to-Right
<< Left shift Left-to-Right
>> Right shift Left-to-Right
< Less than Left-to-Right
<= Less than or equal Left-to-Right
> Greater than Left-to-Right
>= Greater than or equal Left-to-Right
== Equal to Left-to-Right
!= Not equal to Left-to-Right
& Bitwise AND Left-to-Right
^ Bitwise XOR Left-to-Right
| Bitwise OR Left-to-Right
&& Logical AND Left-to-Right
|| Logical OR Left-to-Right
?: Conditional (ternary) Right-to-Left
= Assignment Right-to-Left
+= Add and assign Right-to-Left
-= Subtract and assign Right-to-Left
*= Multiply and assign Right-to-Left
/= Divide and assign Right-to-Left
%= Modulus and assign Right-to-Left
<<= Left shift assign Right-to-Left
>>= Right shift assign Right-to-Left
&= Bitwise AND assign Right-to-Left
^= Bitwise XOR assign Right-to-Left
|= Bitwise OR assign Right-to-Left
, Comma operator Left-to-Right

Practice Exercise

Solve: (5 + 3) * 2 > 10 || 8 / 2 == 4 && 5 != 2;

Answer: 1 (True)

View Technical Breakdown
1. (5 + 3) → 8 (Parentheses)
2. 8 * 2 → 16 (Arithmetic)
3. 8 / 2 → 4 (Arithmetic)
4. 16 > 10 → 1 (Relational)
5. 4 == 4 → 1 (Equality)
6. 5 != 2 → 1 (Equality)
7. 1 && 1 → 1 (Logical AND)
8. 1 || 11 (Logical OR)

Common Beginner Mistakes

→ Assignment vs Equality: Using = (Assignment) when you meant == (Equality check). This is the most frequent source of logic bugs.

→ Precedence Neglect: Forgetting operator priority rules. When in doubt, always use parentheses () to explicitly define evaluation order.

→ Division by Zero: Attempting to divide a number by zero. In C, this leads to undefined behavior or immediate program termination.

→ Modulus Constraints: Attempting to use the % operator with float or double types; the modulus operator is strictly for integers.

→ Logical vs Bitwise: Confusing && (Logical AND) with & (Bitwise AND). While they may seem similar, their operations and results are fundamentally different.


Why Important for Embedded Engineers

In firmware development, operators are your primary tools for hardware control. You use them for sensor data comparison, loop counters, and low-level bit manipulation.

if(temp > 50 && fan_status == OFF) {
    turn_on_fan();
}
← Chapter 4: Variables and Data Types Chapter 6: if / else →