🚀 Alguni Start learning

Unit 1: Hello, C

Closer to the machine.

Unit 1 of 11 in C programming for kids. Its 4 lessons are The Recipe, Labelled Boxes, Showing Values and Hello Machine — 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 is free for ever, because the first two units of every track are. Try it in the app.

📜 The Recipe

C needs a wrapper

In Python you just wrote print. C sits much closer to the machine, so it needs a little scaffolding first. #include <stdio.h> fetches the printing tools, and every C program starts running inside main.

C

#include <stdio.h>

int main() {
    printf("Hello!\n");
    return 0;
}

It prints

Hello!

What is compiling?

Python read your code and ran it straight away. C works differently: a compiler translates your whole program into machine code *first*, and only then do you run it. That extra step is why C programs are so fast — the translating already happened.

New lines are not free

Python's print gave you a new line for nothing. printf does not — you have to ask for one with \n. Forget it and everything runs together.

C

printf("A");
printf("B\n");

It prints

AB

Try it yourself

Which line actually puts something on the screen?

C

#include <stdio.h>

int main() {
    printf("Hi\n");
    return 0;
}
  • #include <stdio.h>
  • int main() {
  • printf("Hi\n");
  • return 0;

So what does a compiler do?

  • Runs your code one line at a time
  • Turns your whole program into machine code before it runs
  • Makes the text colourful
  • Saves the file to disk

Answer them in the app

🏷️ Labelled Boxes

Every box says what fits

Python let a box hold anything. In C you must say up front what kind of thing goes in: int for whole numbers, float for numbers with a decimal point, char for a single character in single quotes.

C

int score = 10;
float price = 2.5;
char grade = 'A';

Whole numbers stay whole

This one surprises everybody. Divide two int values and C throws away the remainder — it does not round. So 7 / 2 is 3, not 3.5.

C

printf("%d\n", 7 / 2);

It prints

3

Try it yourself

Which type would you use for the number of lives left?

  • float
  • int
  • char
  • text

What is wrong with this line?

C

x = 5;
  • Nothing, it is fine
  • It never says what type x is
  • It is missing a comma
  • 5 is too small

Answer them in the app

🔢 Showing Values

Holes in the sentence

To print a value you leave a placeholder and hand the value in afterwards. %d for an int, %f for a float, %c for a char.

C

int cats = 5;
printf("I have %d cats\n", cats);

It prints

I have 5 cats

Floats print long

A float shown with %f comes out with six digits after the point unless you ask for fewer. It looks odd at first, but it is just C being precise.

C

printf("%f\n", 2.5);

It prints

2.500000

Try it yourself

Which placeholder shows a whole number?

  • %f
  • %c
  • %d
  • %s

What does this print?

C

int n = 8;
printf("Level %d done\n", n);

Answer them in the app

🏆 Hello Machine

Try it yourself

What does this print?

C

int a = 10;
int b = 3;
printf("%d\n", a / b);

Which of these is a complete, working C program?

  • printf("Hi\n");
  • int main() { printf("Hi\n"); return 0; }
  • #include <stdio.h>\nint main() { printf("Hi\n"); return 0; }
  • #include <stdio.h>

Answer them in the app