Basic Operators

Loading concept...

🧮 C Language: Basic Operators

Your Calculator Inside the Computer!


🎯 The Big Picture

Imagine you have a magic calculator that can do math, remember things, and even change one type of number into another. That’s exactly what operators do in C programming!

Think of operators like special buttons on this magic calculator:

  • + button adds things
  • = button stores results
  • ++ button counts up by one

Let’s explore each button on our magic calculator! 🚀


🔢 Arithmetic Operators

Your Basic Math Buttons

These are the buttons you already know from math class!

int a = 10;
int b = 3;

int sum = a + b;      // 13 (Addition)
int diff = a - b;     // 7  (Subtraction)
int prod = a * b;     // 30 (Multiplication)
int quot = a / b;     // 3  (Division)
int rem = a % b;      // 1  (Remainder)

🍕 Pizza Analogy

Operator Symbol Example Pizza Story
Add + 5 + 3 = 8 You have 5 slices, friend gives 3 more
Subtract - 5 - 3 = 2 You have 5 slices, eat 3
Multiply * 5 * 3 = 15 5 friends each get 3 slices
Divide / 6 / 3 = 2 6 slices shared by 3 friends
Modulus % 7 % 3 = 1 7 slices, 3 friends → 1 leftover!

⚠️ Important Note on Division

int result = 7 / 2;   // Result: 3 (not 3.5!)
// Integer division throws away decimal

When dividing integers, the decimal part is chopped off like cutting off the crust!


⬆️ Increment & Decrement Operators

The +1 and -1 Shortcuts

Instead of writing count = count + 1, C gives us a shortcut!

int count = 5;

count++;    // Now count = 6 (add 1)
count--;    // Now count = 5 (subtract 1)

🎮 Pre vs Post: The Tricky Part

graph TD A[count = 5] --> B{Which operator?} B -->|++count| C[First add, then use<br/>Result: 6] B -->|count++| D[First use, then add<br/>Use 5, becomes 6]

Real Example:

int x = 5;
int a = ++x;  // x becomes 6, a gets 6
// Pre-increment: Add FIRST

int y = 5;
int b = y++;  // b gets 5, then y becomes 6
// Post-increment: Use FIRST, add LATER

🍪 Cookie Jar Story

  • ++jar: First add cookie, then tell me how many
  • jar++: First tell me how many, then add cookie

📥 Assignment Operators

The Storage Button

The = sign in C is not “equals” like in math. It means “store this value”!

int score = 0;      // Store 0 in score
score = 100;        // Now store 100 in score

🎒 Backpack Analogy

Think of a variable as a backpack with a name tag:

int backpack = 5;   // Put 5 toys in backpack
backpack = 10;      // Empty it, put 10 toys

The = operator:

  1. Takes what’s on the right
  2. Stores it in the box on the left
int a = 5;
int b = 10;
a = b;        // a now holds 10
// b gave a copy, b still has 10!

⚡ Compound Assignment Operators

The Super-Shortcuts

Why write long code when you can write short code?

Long Way Shortcut Meaning
x = x + 5 x += 5 Add 5 to x
x = x - 5 x -= 5 Subtract 5 from x
x = x * 5 x *= 5 Multiply x by 5
x = x / 5 x /= 5 Divide x by 5
x = x % 5 x %= 5 Remainder of x÷5

🎮 Video Game Points

int points = 100;

points += 50;   // Bonus! Now 150
points -= 25;   // Lost some! Now 125
points *= 2;    // Double points! Now 250
points /= 5;    // Split 5 ways! Now 50

Think of += as saying “add more to what I already have”!


🔄 Type Casting

Shape-Shifting Numbers

Sometimes you need to change a number’s type. Like turning a whole cookie into cookie crumbs!

Implicit Casting (Automatic)

int whole = 5;
float decimal = whole;  // 5 becomes 5.0
// C does this automatically!

Explicit Casting (You Control It)

float pi = 3.14159;
int rounded = (int)pi;  // rounded = 3
// You told C to chop off decimals!

int a = 5, b = 2;
float result = (float)a / b;  // 2.5
// Without cast: 5/2 = 2 (integer!)
graph TD A[int 5] -->|automatic| B[float 5.0] C[float 3.7] -->|"#40;int#41;"| D[int 3] E["Bigger type ← automatic"] F["Smaller type ← manual cast"]

🧃 Juice Box Analogy

  • int → float: Pouring juice into bigger cup (safe!)
  • float → int: Squeezing into smaller cup (some spills!)

🔬 Integer Promotions

The Behind-the-Scenes Magic

When you mix small number types, C secretly promotes them to int before calculating!

Why Does This Happen?

char a = 10;    // char is small (1 byte)
char b = 20;    // char is small (1 byte)

// When you do math...
int result = a + b;
// C promotes both to int, then adds!

The Promotion Ladder

graph TD A[char / short] -->|promoted to| B[int] B -->|if needed| C[unsigned int] C -->|if needed| D[long] D -->|if needed| E[float] E -->|if needed| F[double]

🎢 Roller Coaster Height

Think of it like a roller coaster:

  • char and short are too short to ride alone
  • They must stand on a box (int) to reach the math ride!
char small = 100;
char tiny = 50;

// Behind the scenes:
// int temp1 = (int)small;  // promoted!
// int temp2 = (int)tiny;   // promoted!
// int result = temp1 + temp2;

int sum = small + tiny;  // Works: 150

⚠️ Watch Out for Overflow!

char c = 200;      // char usually holds -128 to 127
// or 0 to 255 if unsigned

unsigned char uc = 200;
unsigned char uc2 = 100;
int result = uc + uc2;  // = 300 (promoted to int)

🎯 Quick Summary

Operator Type Examples Remember
Arithmetic + - * / % Basic math buttons
Increment ++ -- +1 or -1 shortcut
Assignment = Store value (not equals!)
Compound += -= *= /= %= Math + store in one step
Type Cast (int) (float) Shape-shift numbers
Promotion automatic Small types grow to int

💡 Golden Rules

  1. Division of integers = no decimals

    7 / 2 = 3  // Not 3.5!
    
  2. Pre vs Post increment matters

    ++x  // Add first, use later
    x++  // Use first, add later
    
  3. Assignment goes right to left

    a = b = c = 5;  // All become 5!
    
  4. Cast when mixing types

    (float)5 / 2 = 2.5  // Cast first!
    
  5. Small types auto-promote to int

    char + char = int (internally)
    

🚀 You Did It!

Now you know all the basic operators in C! These are the building blocks for everything you’ll do in programming.

Remember: Operators are just buttons on your magic calculator. The more you practice, the faster you’ll press them!

“Every expert was once a beginner who didn’t give up.” 🌟

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.