Lesson 3: Arithmetic Operations – Making Your Variables Work for You!


Lesson 3

Lesson 3: Arithmetic Operations – Making Your Variables Work for You!


Welcome back, coding warrior! 🏹

So far, you’ve managed to create variables, store data in them, and even print them out like a proud parent showing off a school report. But there’s one crucial thing missing: making these variables work. Because, let’s be honest, it’s not enough to just store numbers — you need to crunch them, twist them, and make them do your bidding! And that’s where arithmetic operations come into play.

Ready to turn your variables into a math factory? Grab your calculator (just kidding, you won’t need it… I hope), and let’s jump in!


Arithmetic Operations in C

Imagine arithmetic operations as a set of tools for working with numbers. Whether it’s adding up your daily coffee intake, calculating the amount of sleep you didn’t get, or just trying to figure out how many pizzas you can afford this weekend, arithmetic operations will do the job.

Here’s a quick rundown of the basic operations we’ll be using:

  • Addition (+) – Adds two numbers together. Like putting sugar in your coffee… or a little more sugar… and a bit more…
  • Subtraction (-) – Takes one number away from another. Like realizing you overspent on that pizza and subtracting from next month’s budget.
  • Multiplication (*) – Multiplies two numbers. Perfect for figuring out how many snacks you’ll need for a movie marathon!
  • Division (/) – Divides one number by another. But be careful, division in C can be tricky — it likes to drop those decimals!
  • Modulus (%) – Gives you the remainder after division. Ideal for finding out if you still have that one leftover cookie in a pack.

Let’s see these in action!


Creating a File: operations.c

  1. Open your favorite text editor (or nano if you’re in a terminal mood).
  2. Create a new file called operations.c.
  3. Copy and paste the following code into operations.c:
#include <stdio.h>

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

    // Performing basic arithmetic operations
    int sum = a + b;       // Addition
    int difference = a - b; // Subtraction
    int product = a * b;   // Multiplication
    int quotient = a / b;  // Division (truncates decimal part)
    int remainder = a % b; // Modulus

    // Printing the results
    printf("Sum: %d\n", sum);           // Output: 13
    printf("Difference: %d\n", difference); // Output: 7
    printf("Product: %d\n", product);       // Output: 30
    printf("Quotient: %d\n", quotient);     // Output: 3
    printf("Remainder: %d\n", remainder);   // Output: 1

    return 0;
}

Compiling and Running the Program

  1. Open your terminal and navigate to the folder where operations.c is saved.
  2. Compile it using:
gcc operations.c -o operations
  1. Run the program:
./operations

If everything goes according to plan, you should see the following output:

Sum: 13
Difference: 7
Product: 30
Quotient: 3
Remainder: 1

Look at that! Your variables are now doing math like they were born for it. Well done! But let’s not stop here…


Introducing: Increment and Decrement Operators

Time to meet some shortcuts that programmers love: increment (++) and decrement (--) operators. These little rascals are used to quickly increase or decrease a variable’s value by 1.

Here’s how they work:

  • x++ – Increases x by 1. Imagine it like adding an extra cup of coffee to your day.
  • x-- – Decreases x by 1. Like deciding to sleep an hour less… wait, don’t actually do that!

Let’s add a few lines to operations.c to see them in action.


Modifying operations.c

  1. Open operations.c again.
  2. Add the following code before return 0;:
    a++; // Incrementing 'a' by 1
    b--; // Decreasing 'b' by 1

    printf("Incremented a: %d\n", a); // Output: 11
    printf("Decremented b: %d\n", b); // Output: 2

Recompiling and Running Again

  1. Recompile:
gcc operations.c -o operations
  1. Run:
./operations

You should see:

Incremented a: 11
Decremented b: 2

Pretty neat, huh? But that’s just the beginning. Let’s wrap up by talking about some common pitfalls you might encounter when working with arithmetic operations in C.


Common Pitfalls and Gotchas

  1. Integer Division Woes: When you divide two integers, C doesn’t care about the decimals. If you try 7 / 2, you’ll get 3 instead of 3.5. To fix this, use float or double types.

  2. Order of Operations: Just like in school math, C follows a specific order: multiplication and division before addition and subtraction. Use parentheses () if you want to change the order!

  3. Modulus with Negative Numbers: The % operator can behave unexpectedly with negative numbers. For example, -5 % 3 will give you -2… yep, C has a funny way of handling leftovers!


Homework Challenge: Build Your Own Calculator!

Let’s see if you can put these arithmetic operations to good use. Create a program that:

  1. Takes two numbers as input.
  2. Asks the user which operation they want to perform: addition, subtraction, multiplication, division, or modulus.
  3. Prints the result!

Bonus Points: Add error handling for division by zero (trust me, that one can ruin your day).


Final Thoughts

Today, you’ve made your variables work for you by mastering arithmetic operations. But C has a few more tricks up its sleeve. Join me in Lesson 4, where we’ll explore the dark, mysterious world of control structures. Get ready to make decisions, handle conditions, and perhaps even play a few games along the way! 🎮✨


See also