Unit 6: Text in C
Letters, one at a time.
Unit 6 of 11 in C programming for kids. Its 5 lessons are Letters Are Numbers, Words Are Arrays, Walking Through Words, Asking the User and Word 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 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.
🔡 Letters Are Numbers
A char really is a number
A char holds one character in single quotes — but underneath it is just a small number. Print it with %c to see the letter, or %d to see the number.
C
char grade = 'A';
printf("%c\n", grade);
printf("%d\n", grade);
It prints
A 65
You can do maths on letters
Because a char is a number, adding 1 moves to the next letter. The alphabet is laid out in order, so this actually works.
C
char c = 'A';
printf("%c\n", c + 1);
printf("%d\n", 'b' - 'a');
It prints
B 1
Try it yourself
What is the difference between 'A' and "A"?
- Nothing
- 'A' is one character; "A" is text, which is a whole array
- Double quotes are faster
- 'A' only works in printf
What does this print?
C
char c = 'x';
printf("%c\n", c + 2);
Answer them in the app
📇 Words Are Arrays
There is no string type
C has no string. A word is an array of chars — and C quietly adds a hidden 0 on the end so it knows where the word stops.
C
char name[] = "Ada";
printf("%s\n", name);
printf("%c\n", name[0]);
It prints
Ada A
The hidden ending marker
"Ada" takes four boxes, not three: A, d, a, and a zero. That zero is how every C function knows where to stop — forget it and things go badly wrong.
C
char name[] = "Ada";
printf("%d\n", name[3]);
It prints
0
Changing a letter
It is an array, so you can change any letter by its position, exactly as with numbers.
C
char word[] = "cat";
word[0] = 'b';
printf("%s\n", word);
It prints
bat
Try it yourself
How many boxes does char w[] = "cat"; use?
- 3
- 4
- 5
- It depends
What does this print?
C
char word[] = "hello";
word[0] = 'j';
printf("%s\n", word);
Answer them in the app
🚶 Walking Through Words
Stopping at the zero
To visit every letter you loop until you hit that hidden zero. This pattern is everywhere in C.
C
char word[] = "cat";
for (int i = 0; word[i] != '\0'; i++) {
printf("%c-", word[i]);
}
printf("\n");
It prints
c-a-t-
Letting C count for you
strlen counts the letters, not including the ending zero. It needs #include <string.h>.
C
#include <string.h>
char word[] = "dragon";
printf("%d\n", (int) strlen(word));
It prints
6
Try it yourself
What does this print?
C
char word[] = "banana";
int count = 0;
for (int i = 0; word[i] != '\0'; i++) {
if (word[i] == 'a') {
count++;
}
}
printf("%d\n", count);
What would happen if the ending zero were missing?
- Nothing, C works it out
- The loop would run past the end of the word into whatever is next in memory
- It would print faster
- C would add one automatically
Answer them in the app
⌨️ Asking the User
Reading what they type
scanf reads what the player types. It needs an & before the name — you are telling it *where to put* the answer, which is why the pointers unit came first.
C
int n;
scanf("%d", &n);
printf("%d\n", n * 2);
It prints
If the player types 4, this prints 8
Try it yourself
Why does scanf need the & but printf does not?
- It is just how it is spelled
- printf only reads the value; scanf has to WRITE into your box, so it needs its address
- & makes it faster
- printf needs one too
Answer it in the app
🏆 Word Machine
Try it yourself
What does this print?
C
char w[] = "code";
printf("%c%c\n", w[0], w[3]);
What does this print?
C
char w[] = "abc";
for (int i = 0; w[i] != 0; i++) {
printf("%c", w[i] + 1);
}
printf("\n");
Answer them in the app