Bytebook Compiler

Getting Started

Operators & Expressions

Arithmetic, increment, relational, logical, bitwise and ternary operators — plus precedence and type casting, which examiners love.

7 min read7 runnable examples5-question quiz

Operators are symbols that perform operations on values (called operands). a + b uses the + operator on operands a and b.

Arithmetic operators

Operator Meaning Example Result
+ Addition 17 + 5 22
- Subtraction 17 - 5 12
* Multiplication 17 * 5 85
/ Division 17 / 5 3 (integer division!)
% Remainder (modulus) 17 % 5 2
C
#include <stdio.h>

int main(void) {
    int a = 17, b = 5;
    printf("%d / %d = %d\n", a, b, a / b);         // integers: the fraction is dropped
    printf("%d %% %d = %d\n", a, b, a % b);
    printf("17.0 / 5 = %.1f\n", 17.0 / 5);         // one double operand: real division
    printf("-17 / 5 = %d, -17 %% 5 = %d\n", -17 / 5, -17 % 5);
    return 0;
}
Output
17 / 5 = 3
17 % 5 = 2
17.0 / 5 = 3.4
-17 / 5 = -3, -17 % 5 = -2

Increment and decrement

++ adds 1 and -- subtracts 1. The position matters when the result is used in the same expression:

  • Pre-increment ++x: increase first, then use the new value.
  • Post-increment x++: use the old value, then increase.
C
#include <stdio.h>

int main(void) {
    int x = 5, y;
    y = x++;            // y gets 5, then x becomes 6
    printf("x = %d, y = %d\n", x, y);
    y = ++x;            // x becomes 7, then y gets 7
    printf("x = %d, y = %d\n", x, y);
    return 0;
}
Output
x = 6, y = 5
x = 7, y = 7

Assignment operators

x += 3 is shorthand for x = x + 3. The same works for -=, *=, /=, %=, &=, |=, ^=, <<= and >>=.

Relational and logical operators

Relational operators compare values and produce 1 (true) or 0 (false): ==, !=, <, >, <=, >=.

Logical operators combine conditions:

Operator Name True when
&& AND both sides are true
|| OR at least one side is true
! NOT the operand is false
C
#include <stdio.h>

int main(void) {
    int marks = 78, attendance = 82;
    printf("marks >= 40: %d\n", marks >= 40);
    printf("eligible: %d\n", marks >= 40 && attendance >= 75);
    printf("needs help: %d\n", marks < 40 || attendance < 75);
    printf("!eligible: %d\n", !(marks >= 40 && attendance >= 75));
    return 0;
}
Output
marks >= 40: 1
eligible: 1
needs help: 0
!eligible: 0

C uses short-circuit evaluation: in a && b, if a is false, b is never evaluated. In a || b, if a is true, b is skipped. This lets you write safe checks like if (d != 0 && n / d > 2).

Bitwise operators

Bitwise operators work on the individual bits of integers. They are fast and appear in systems code and interviews.

Operator Name Example (12 = 1100, 10 = 1010) Result
& AND 12 & 10 8 (1000)
| OR 12 | 10 14 (1110)
^ XOR 12 ^ 10 6 (0110)
~ NOT (one operand) ~12 -13
<< Left shift 12 << 1 24
>> Right shift 12 >> 2 3
C
#include <stdio.h>

int main(void) {
    int a = 12, b = 10;
    printf("a & b = %d\n", a & b);
    printf("a | b = %d\n", a | b);
    printf("a ^ b = %d\n", a ^ b);
    printf("a << 1 = %d (same as a * 2)\n", a << 1);
    printf("a >> 2 = %d (same as a / 4)\n", a >> 2);
    printf("13 is %s\n", (13 & 1) ? "odd" : "even");   // lowest bit tells odd/even
    return 0;
}
Output
a & b = 8
a | b = 14
a ^ b = 6
a << 1 = 24 (same as a * 2)
a >> 2 = 3 (same as a / 4)
13 is odd

The ternary operator

condition ? value_if_true : value_if_false is a compact if-else that produces a value:

C
#include <stdio.h>

int main(void) {
    int a = 14, b = 29;
    int max = (a > b) ? a : b;
    printf("Larger number: %d\n", max);
    printf("%d is %s\n", a, a % 2 == 0 ? "even" : "odd");
    return 0;
}
Output
Larger number: 29
14 is even

Precedence and associativity

When an expression has several operators, precedence decides which runs first, and associativity breaks ties. Highest first:

Level Operators Associativity
1 () [] -> . x++ x-- left to right
2 ++x --x ! ~ -x (type) *p &x sizeof right to left
3 * / % left to right
4 + - left to right
5 << >> left to right
6 < <= > >= left to right
7 == != left to right
8–10 & then ^ then | left to right
11–12 && then || left to right
13 ?: right to left
14 = += -= … right to left
C
#include <stdio.h>

int main(void) {
    printf("%d\n", 2 + 3 * 4);       // * before +  -> 14
    printf("%d\n", (2 + 3) * 4);     // brackets first -> 20
    printf("%d\n", 20 / 4 * 2);      // same level, left to right -> 10
    printf("%d\n", 10 - 4 - 3);      // left to right -> 3
    return 0;
}
Output
14
20
10
3

Type conversion and casting

When you mix types, C implicitly converts the smaller type to the larger one (int → double in 5 / 2.0). To force a conversion yourself, use a cast: (type) value.

C
#include <stdio.h>

int main(void) {
    int total = 437, count = 5;
    float wrong = total / count;            // int division happens first: 87
    float right = (float)total / count;     // cast first: 87.4
    printf("wrong = %.2f, right = %.2f\n", wrong, right);

    double price = 99.99;
    int rupees = (int)price;                // casting to int truncates
    printf("rupees = %d\n", rupees);
    return 0;
}
Output
wrong = 87.00, right = 87.40
rupees = 99

Test yourself

5 questions · pick an answer to check it instantly

  1. 1

    What is the value of 7 / 2 * 2 in C?

  2. 2

    After int x = 3; int y = x++ + 2;, what are x and y?

  3. 3

    What does 10 & 6 evaluate to?

  4. 4

    In if (n != 0 && 100 / n > 5), what happens when n is 0?

  5. 5

    Which expression gives 2.5?