</>CodexBash
C
CBeginner9 min

Operators and expressions

Arithmetic and integer division, remainder with negative numbers, increment and decrement, comparisons, logical operators with short-circuiting, and precedence.

Taking a number apart with % and /step 1/6
4
0
7
1
n = 4071digit = -
Start with n = 4071. n % 10 gives the last digit; n / 10 (// in Python) drops it.

Arithmetic#

OperatorMeaningWith the ints 7 and 2
+add9
-subtract5
*multiply14
/divide3 (the fraction is dropped)
%remainder1

There is no power operator. pow(2, 10) from <math.h> returns a double; for whole powers, a loop that multiplies is exact.

Division and remainder#

With two integers, / keeps only the whole part, rounding towards zero: 7 / 2 is 3 and -7 / 2 is -3.

% gives the remainder, and its sign follows the left side: -7 % 2 is -1, not 1. If a problem allows negative numbers, take that into account, or work with the absolute value.

Together, / 10 and % 10 take a number apart digit by digit:

int n = 4071;
while (n > 0) {
    printf("%d\n", n % 10);   // 1, 7, 0, 4
    n /= 10;
}

Increment and decrement#

int i = 5;
i++;        // i is 6
i--;        // i is 5

i++ and ++i both add one. The difference shows only when the expression's value is used: i++ gives the old value, ++i the new one. Keep increments on their own line and the difference never matters.

Assignment shorthand#

total += x;     // total = total + x
total -= x;
total *= 2;
total /= 10;
total %= 7;

Comparisons#

==, !=, <, >, <=, >= produce 1 for true and 0 for false.

The classic C bug is = where == was meant:

if (x = 5) { ... }    // assigns 5 to x, then the condition is 5: always true

Compile with -Wall and gcc warns about it.

Logical operators#

if (age >= 18 && has_id) { ... }     // and
if (day == 6 || day == 7) { ... }    // or
if (!done) { ... }                   // not

&& and || stop as soon as the answer is known. That makes this safe, because the array is read only when i is in range:

if (i < n && arr[i] == target) { ... }

Swap the two halves and it can read past the end of the array.

Precedence#

Multiplication, division and remainder happen before addition and subtraction; comparisons come after arithmetic; && comes before ||.

2 + 3 * 4          // 14
(2 + 3) * 4        // 20
a < b && b < c     // fine: comparisons happen first

Use brackets whenever a reader might hesitate. Bitwise operators (&, |, ^) bind more loosely than comparisons, so x & 1 == 0 does not mean what it looks like; write (x & 1) == 0.

Key points#

  • Integer / drops the fraction; % takes the sign of the left side.
  • / 10 and % 10 peel digits off a number.
  • ++ and += are shorthand; keep increments on their own line.
  • == compares, = assigns; -Wall catches the mix-up.
  • && and || short-circuit, so put the safety check first.

Next: reading input, and strings, which in C are arrays of characters.

Practise it

The same idea, in the order it is worth meeting it.

  1. Not startedReads the same backwardsEasy
  2. Not startedTurn the digits aroundEasy
Create a free account to track your progress