🚀 Alguni Start learning

Unit 7: Types & Sizes

Turning one kind into another.

Unit 7 of 11 in C programming for kids. Its 5 lessons are Changing Types, How Big Is It?, The Maths Library, Printing Neatly and Type Master — below is everything each one explains, and a question or two from it to try.

Every sample on this page was compiled and run with gcc before it shipped, and prints exactly what it says it prints.

This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.

🔄 Changing Types

The division surprise, explained

Two whole numbers always divide to a whole number in C — the rest is thrown away. Putting (double) in front of one tells C to do the sum with decimals instead. That is a cast.

C

#include <stdio.h>

int main() {
    printf("%d\n", 7 / 2);
    printf("%.2f\n", (double)7 / 2);
    return 0;
}

It prints

3
3.50

Casting the other way chops

Turning a decimal into an int does not round — it throws the fraction away, however close to the next number it was.

C

#include <stdio.h>

int main() {
    double price = 9.99;
    printf("%d\n", (int)price);
    printf("%d\n", (int)-9.99);
    return 0;
}

It prints

9
-9

One cast is enough

Once either side is a decimal, C converts the other one for you. So you never need to cast both.

C

#include <stdio.h>

int main() {
    int total = 7, count = 2;
    printf("%.1f\n", (double)total / count);
    return 0;
}

It prints

3.5

Try it yourself

Why does (double)7 / 2 give 3.50 but (double)(7 / 2) gives 3.00?

  • They are the same
  • In the second, the whole-number division happens first and 3 is all that is left to convert
  • Brackets are not allowed
  • The cast fails

What does this print?

C

#include <stdio.h>

int main() {
    int a = 9, b = 4;
    printf("%d\n", a / b);
    printf("%.2f\n", (double)a / b);
    return 0;
}

Answer them in the app

📏 How Big Is It?

Every type takes up room

A char is one byte, an int is four, a double is eight. sizeof tells you, and it is one of the things that makes C different from Python — you can see the machine underneath.

C

#include <stdio.h>

int main() {
    printf("%d\n", (int)sizeof(char));
    printf("%d\n", (int)sizeof(int));
    printf("%d\n", (int)sizeof(double));
    return 0;
}

It prints

1
4
8

An array is all of its boxes together

sizeof on an array gives the room the whole thing takes: five ints at four bytes each is twenty.

C

#include <stdio.h>

int main() {
    int nums[5];
    char word[10];
    printf("%d\n", (int)sizeof(nums));
    printf("%d\n", (int)sizeof(word));
    return 0;
}

It prints

20
10

The famous trick for array length

C never tells you how long an array is. But the whole size divided by the size of one item is exactly the count — the idiom every C programmer knows.

C

#include <stdio.h>

int main() {
    int nums[] = {3, 1, 4, 1, 5};
    int length = sizeof(nums) / sizeof(nums[0]);
    printf("%d\n", length);
    return 0;
}

It prints

5

Try it yourself

Why does that trick stop working inside a function that was passed the array?

  • It keeps working
  • The function only receives a pointer, so sizeof measures the pointer, not the array
  • Arrays cannot be passed
  • sizeof is not allowed in functions

What does this print?

C

#include <stdio.h>

int main() {
    char letters[4];
    printf("%d\n", (int)sizeof(letters));
    printf("%d\n", (int)(sizeof(letters) / sizeof(letters[0])));
    return 0;
}

Answer them in the app

📐 The Maths Library

Borrowing more tools

C keeps most maths outside the language, in math.h. Include it and you get sqrt and pow.

C

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

int main() {
    printf("%.1f\n", sqrt(16.0));
    printf("%.1f\n", pow(2.0, 10.0));
    return 0;
}

It prints

4.0
1024.0

These give decimals, always

sqrt and pow work in double, so print them with %f and not %d — the wrong specifier gives nonsense rather than an error.

C

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

int main() {
    double root = sqrt(9.0);
    printf("%.2f\n", root);
    printf("%d\n", (int)root);
    return 0;
}

It prints

3.00
3

Distance from zero

abs lives in stdlib.h and works on whole numbers. It turns a negative into a positive and leaves the rest alone.

C

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

int main() {
    printf("%d\n", abs(-7));
    printf("%d\n", abs(7));
    return 0;
}

It prints

7
7

Try it yourself

Why does C need #include <math.h> when Python just has math.sqrt?

  • C is older
  • C only knows what you tell it about — the include says what names exist
  • math.h is faster
  • Python needs it too

Answer it in the app

📊 Printing Neatly

Making columns line up

A number between % and the letter is the width — the least room the value may take. Values are pushed to the right, which is what makes a table line up.

C

#include <stdio.h>

int main() {
    printf("%5d|\n", 42);
    printf("%5d|\n", 7);
    printf("%5d|\n", 12345);
    return 0;
}

It prints

   42|
    7|
12345|

A minus sign pushes left

%-5d uses the same room but lines the value up on the left instead.

C

#include <stdio.h>

int main() {
    printf("%-5d|\n", 42);
    printf("%-5s|\n", "hi");
    return 0;
}

It prints

42   |
hi   |

Decimal places

A dot and a number says how many decimal places. Without it, %f always shows six.

C

#include <stdio.h>

int main() {
    printf("%f\n", 3.5);
    printf("%.2f\n", 3.14159);
    printf("%.0f\n", 3.7);
    return 0;
}

It prints

3.500000
3.14
4

Try it yourself

How do you print an actual percent sign?

  • \%
  • %%
  • %s with "%"
  • You cannot

What does this print?

C

#include <stdio.h>

int main() {
    printf("%.1f%%\n", 99.55);
    return 0;
}

Answer them in the app

🏆 Type Master

Try it yourself

What does this print?

C

#include <stdio.h>

int main() {
    printf("%d\n", (int)3.99);
    printf("%.1f\n", (double)5 / 2);
    return 0;
}

Answer it in the app