C Programming: Master the Fundamentals
Chapter 3 / 8· 18 min read· 0 cards

Operators and Expressions

Arithmetic, comparison, logical, and the assignment shortcuts C is known for.

Computing with operators

An operator is a symbol that performs an operation on values. C has a rich set, and they fall into a few clear families. The arithmetic operators are familiar:

int a = 17, b = 5;
printf("%d\n", a + b);   // 22
printf("%d\n", a - b);   // 12
printf("%d\n", a * b);   // 85
printf("%d\n", a / b);   // 3  -- integer division!
printf("%d\n", a % b);   // 2  -- remainder (modulo)

The integer division trap

Look carefully at a / b above: 17 divided by 5 gives 3, not 3.4. This is the single biggest surprise for C beginners. When you divide two integers, C throws away the decimal part entirely — it does integer division. The fractional part vanishes.

To get a real decimal answer, at least one operand must be a floating-point number:

int a = 17, b = 5;
printf("%d\n", a / b);              // 3   -- both ints
printf("%f\n", (float)a / b);       // 3.4 -- a converted to float first
printf("%f\n", 17.0 / 5);           // 3.4 -- 17.0 is already a float

That (float)a is a cast — it temporarily treats a as a float for that calculation. Understanding when C uses integer versus floating-point division will save you hours of debugging mysterious wrong answers.


Comparison operators

These ask true/false questions. In C, the result is an integer: 1 for true, 0 for false (C has no separate boolean type in its classic form):

int x = 10, y = 20;
printf("%d\n", x > y);    // 0 (false)
printf("%d\n", x < y);    // 1 (true)
printf("%d\n", x == 10);  // 1 -- double equals compares
printf("%d\n", x != y);   // 1 -- not equal

As in Python, beware the = versus == trap — and in C it's especially dangerous, because if (x = 5) is valid code that assigns 5 to x and is always true. The compiler often won't stop you. Always double-check you've used == for comparison.


Logical operators

Combine conditions with && (and), || (or), and ! (not):

int age = 25;
int has_license = 1;

if (age >= 18 && has_license) {
    printf("Can drive\n");
}

if (age < 13 || age > 65) {
    printf("Discount ticket\n");
}

if (!has_license) {
    printf("Apply for a license\n");
}

Note the doubled symbols: && not &, || not |. The single versions are different operators (bitwise) that do something else entirely — a classic source of subtle bugs.


Assignment shortcuts and increment

C is famous for its concise shortcuts. Updating a variable based on its current value is so common that C gives you compound operators:

int score = 100;
score += 10;   // same as score = score + 10  -> 110
score -= 5;    // 105
score *= 2;    // 210
score /= 3;    // 70

And the increment/decrement operators add or subtract exactly one — you'll see these constantly, especially in loops:

int i = 5;
i++;    // increment: i is now 6
i--;    // decrement: i is now 5 again

There's a subtle distinction between i++ (use the value, then increment) and ++i (increment, then use) that occasionally matters, but for now just know that i++ is the everyday way to add one. These operators are the building blocks of the loops we'll write in the next chapter, where we finally make programs that repeat and decide.

Reading mode · scroll to read at your own pace

Finished "Operators and Expressions"?

Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.

Was this chapter clear?

Try it yourself — open the Code Playground15+ languages — Python, JavaScript, Java, C++, SQL & more — full IDE-style editor, instant run. Your code is auto-saved per language.