C Programming - Beginner to Brilliance / Basics of C Programming

Operators in C

An operator is a symbol that tells the compiler to perform a specific operation on one, two, or more values.

We’ll break them into 7 main categories:

Category

Description

➕ Arithmetic

Basic math operations

📏 Relational

Compare values

🔍 Logical

True/False-based conditions

🧩 Assignment

Assign values to variables

🔧 Unary

Work with only one operand

🧮 Bitwise

Manipulate individual bits

🧪 Miscellaneous

sizeof, ternary, comma, etc.


➕ Arithmetic Operators

These perform basic math:

Operator

Description

Example

Result

+

Addition

5 + 3

8

-

Subtraction

9 - 4

5

*

Multiplication

6 * 2

12

/

Division

10 / 3

3 (int result)

%

Modulus (Remainder)

10 % 3

1

⚠️ Note:

  • If both operands are int, division results in integer division.

  • % works only on integers.

📌 Example:

int a = 10, b = 3;
printf("%d", a / b);       // prints 3
printf("%d", a % b);       // prints 1

✏️ Expression:

int result = 10 + 2 * 3 - 4 / 2;

🧠 Operator Precedence & Associativity in C

📌 Arithmetic Operators (Follow BODMAS)

BODMAS stands for:

  • Brackets

  • Orders (Exponents – not in C directly, but with pow())

  • Division

  • Multiplication

  • Addition

  • Subtraction

In C, arithmetic expressions follow similar precedence:

Precedence

Operator

Description

Associativity

1 (High)

()

Parentheses (Brackets)

Left to Right

2

* / %

Multiplication, Division, Modulus

Left to Right

3

+ -

Addition, Subtraction

Left to Right


🧮 Example Applying BODMAS/Precedence:

int result = 10 + 2 * 3 - 4 / 2;

Step-by-step using precedence:

= 10 + 6 - 2         // First: 2*3 = 6 and 4/2 = 2
= 16 - 2             // Next: 10 + 6
= 14                 // Finally: 16 - 2

✅ Final Result: 14


🔢 Full C Operator Precedence Table (Grouped)

Precedence

Operators

Description

Associativity

1 (Highest)

(), [], ->, .

Function call, array subscript, structure/union member

Left to Right

2

++ (prefix), -- (prefix), + (unary), - (unary), !, ~, * (dereference), & (address), sizeof, (type)

Unary operators

Right to Left

3

*, /, %

Multiplication, division, modulus

Left to Right

4

+, -

Addition, subtraction

Left to Right

5

<<, >>

Bitwise shift left, right

Left to Right

6

<, <=, >, >=

Relational operators

Left to Right

7

==, !=

Equality operators

Left to Right

8

&

Bitwise AND

Left to Right

9

^

Bitwise XOR

Left to Right

10

|

Bitwise OR

Left to Right

11

&&

Logical AND

Left to Right

12

||

Logical OR

Left to Right

13

?:

Ternary conditional

Right to Left

14

=, +=, -=, *=, /=, %= , &=, ^=, =, <<=, >>=

Assignment operators

Right to Left

15 (Lowest)

,

Comma operator

Left to Right


📏 Relational Operators (Comparison)

Used in conditions (if, while, etc.), return either:

  • 1 (true)

  • 0 (false)

Operator

Meaning

Example

Output

==

Equal to

x == y

true if x is equal to y

!=

Not equal

x != y

true if x is NOT equal to y

>

Greater than

x > y

true if x is greater than y

<

Less than

x < y

true if x is less than y

>=

Greater or Equal

x >= y

true if x ≥ y

<=

Less or Equal

x <= y

true if x ≤ y

📌 Example:

int x = 5, y = 10;
printf("%d", x < y);  // prints 1 (true)
printf("%d", x == y); // prints 0 (false🔍 **3. Logical Operators**

🔍 Logical Operators

Used to combine multiple conditions:

Operator

Meaning

Example

Result

&&

Logical AND

x > 0 && x < 10

true only if both are true

||

Logical OR

x > 0 || x < 10

true if even one is true

!

Logical NOT

!condition

reverses true/false

📌 Example:

int age = 20;
if (age > 18 && age < 30) {
    printf("You are young!");
}

🧩 Assignment Operators

Used to assign values to variables:

Operator

Meaning

Equivalent To

=

Assign

a = 5;

+=

Add & assign

a += 3; → a = a + 3

-=

Subtract & assign

a -= 2;

*=

Multiply & assign

a *= 4;

/=

Divide & assign

a /= 2;

%=

Modulo & assign

a %= 2;

📌 Example:

int a = 5;
a += 2; // Now a is 

🔧 Unary Operators

These work on only one operand:

Operator

Description

Example

++

Increment

x++ or ++x

--

Decrement

x-- or --x

-

Negation

-x

!

Logical NOT

!x

Prefix vs Postfix (++x vs x++)

int x = 5;
printf("%d", x++);  // prints 5, then x becomes 6
printf("%d", ++x);  // x becomes 7, then prints 7

🧮 Bitwise Operators (Advanced, but powerful)

Used for bit-level operations, useful in:

  • Embedded systems

  • Hardware programming

  • Encryption algorithms

Operator

Name

Meaning

&

AND

Sets bit to 1 if both bits are 1

|

OR

OR

^

XOR

Sets bit to 1 if bits are different

~

NOT

Inverts all bits

<<

Left Shift

Shifts bits left (×2)

>>

Right Shift

Shifts bits right (÷2)

📌 Example:

int a = 5;                 // binary: 00000101
int b = 3;                  // binary: 00000011
printf("%d", a & b);        // 00000001 → 1

🧪 Miscellaneous Operators

🧱 sizeof Operator

Returns the size (in bytes) of a data type or variable.

printf("%d", sizeof(int));  // Usually prints 4

❓ Ternary Operator

A shortcut for if-else.

📌 Syntax:

condition ? expression_if_true : expression_if_false;

📌 Example:

int age = 18;
char *result = (age >= 18) ? "Adult" : "Minor";

⛓️ Comma Operator

Allows multiple expressions in one line:

int a = (b = 3, b + 2);  // b is 3, a becomes 5