Aral Code

Basics Lesson 5 of 16

C Operators

Arithmetic, assignment, increment and decrement, comparison and logical operators in C, plus the ternary operator and how C treats true and false as numbers.

Operators are the symbols that compute and compare values. C's operators were copied by many later languages, so learning them here pays off in Java, JavaScript, C++, C# and PHP too.

Arithmetic

#include <stdio.h>

int main(void) {
    int a = 17, b = 5;
    printf("%d + %d = %d\n", a, b, a + b);
    printf("%d - %d = %d\n", a, b, a - b);
    printf("%d * %d = %d\n", a, b, a * b);
    printf("%d / %d = %d\n", a, b, a / b);
    printf("%d %% %d = %d\n", a, b, a % b);
    return 0;
}

Output

17 + 5 = 22
17 - 5 = 12
17 * 5 = 85
17 / 5 = 3
17 % 5 = 2

% (remainder) works only with integers. For powers and square roots, use pow() and sqrt() from <math.h>.

Assignment and increment

#include <stdio.h>

int main(void) {
    int score = 10;
    score += 5;    /* 15 */
    score -= 3;    /* 12 */
    score *= 2;    /* 24 */
    score /= 4;    /* 6 */
    score++;       /* 7 */
    printf("Score: %d\n", score);

    int x = 5;
    int y = x++;   /* y = 5, then x = 6 */
    int z = ++x;   /* x = 7, then z = 7 */
    printf("x=%d y=%d z=%d\n", x, y, z);
    return 0;
}

Output

Score: 7
x=7 y=5 z=7

Comparison operators

In C, a comparison gives an int: 1 for true and 0 for false.

#include <stdio.h>

int main(void) {
    int grade = 82;
    printf("%d\n", grade >= 75);
    printf("%d\n", grade == 100);
    printf("%d\n", grade != 100);
    return 0;
}

Output

1
0
1

In conditions, C treats 0 as false and any other number as true.

Logical operators

#include <stdio.h>

int main(void) {
    double average = 91.5;
    int lowest = 84;
    int age = 65;

    printf("With honors: %d\n", average >= 90 && lowest >= 85);
    printf("Senior or minor: %d\n", age >= 60 || age < 18);
    printf("Not senior: %d\n", !(age >= 60));
    return 0;
}

Output

With honors: 0
Senior or minor: 1
Not senior: 0

&& (and), || (or) and ! (not). They short-circuit: in a && b, if a is false, b is never evaluated.

The ternary operator

#include <stdio.h>

int main(void) {
    int n = 27;
    printf("%d is %s\n", n, n % 2 == 0 ? "even" : "odd");
    int max = (12 > 9) ? 12 : 9;
    printf("max = %d\n", max);
    return 0;
}

Output

27 is odd
max = 12

Math functions

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void) {
    printf("%.2f\n", sqrt(144));
    printf("%.0f\n", pow(2, 10));
    printf("%.0f %.0f %.0f\n", ceil(4.1), floor(4.9), round(4.5));
    printf("%d\n", abs(-7));
    return 0;
}

Output

12.00
1024
5 4 5
7

On Linux, add -lm when compiling programs that use <math.h>: gcc prog.c -o prog -lm. abs() for integers comes from <stdlib.h>, so that header is included too; modern compilers refuse to call a function whose header is missing.

Precedence

From high to low: (), then ! ++ --, then * / %, then + -, then comparisons, then &&, then ||, then assignment. Use parentheses whenever it isn't obvious: (a + b) / 2.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does printf("%d", 5 > 3); print?
2. Which value counts as false in a C condition?
3. What does if (x = 5) do?
4. Which header provides sqrt and pow?

By Sir Reyel · Last updated 2026-12-04