typestar

Types, casts, and sizeof in C

Converting between C's numeric types explicitly.

long conversions(void) {
    int whole = 7;
    double half = whole / 2.0;
    int floored = (int) half;
    char letter = (char) (65 + whole);

    size_t int_size = sizeof(int);
    long big = (long) whole * 1000000;
    return big + floored + letter + int_size;
}

How it works

  1. Dividing by 2.0 promotes the result to double.
  2. A cast like (int) truncates toward zero.
  3. sizeof reports a type's size in bytes.

Keywords and builtins used here

The run, in numbers

Lines
10
Characters to type
238
Tokens
66
Three-star pace
80 tpm

At the three-star pace of 80 tokens a minute, this run takes about 50 seconds.

Type this snippet

Step 2 of 7 in Variables & types, step 2 of 35 in Language basics.

← Previous Next →