Practice: Introduction to C
20 questions: algorithms to write and repair, the language layers and the compiler, and Visual Studio programs to build, run and repair. Every answer is hidden until you open it.
How to use this page
The questions follow the three lessons of unit 1: what programming is, what C is, and the first program. Questions 1 to 6 are written on paper, 7 to 12 are short answers, and 13 to 20 are done in Visual Studio.
Try each question before you open the answer. For the Visual Studio questions, a correct answer means three things: the file is saved, the build succeeds, and the output is exactly right.
Algorithms
1. Prepare a cup of tea
Write the steps for "prepare a cup of tea" for someone who has never made tea. Number the steps. Include at least one check and a clear end.
Try it yourself first, then open the solution step by step.
2. Draw a simple house
Write instructions that a classmate, who cannot see the picture, can follow to draw a house: a square, a triangle roof, a door and two windows with crosses. Your classmate draws exactly what you write, nothing more.
Try it yourself first, then open the solution step by step.
3. Send a text message
Write the steps for sending the message "Hello There" to a classmate from your phone. Mark every step that is a check.
Try it yourself first, then open the solution step by step.
4. Make it exact
Rewrite each instruction so a computer could follow it exactly.
- Wait a bit.
- Put some water in the kettle.
- Charge the battery for a while.
- Turn the multimeter to volts.
- Check that it works.
Try it yourself first, then open the solution step by step.
5. Measure a battery
Write an algorithm: measure the voltage of a 9 V battery with a multimeter, for someone who has never used one. Number the steps, give exact values, add at least one check, and end clearly.
Try it yourself first, then open the solution step by step.
6. Find the problems
What is wrong with this algorithm? Name three problems.
- Plug in the kettle and switch it on.
- Wait.
- Pour the water into the mug.
Try it yourself first, then open the solution step by step.
From machine code to C
7. Which layer?
Write the layer for each line: machine code, assembly, or high-level (C).
ADD R0, R0, #3total = price * quantity;10110000 01100001printf("Hello, C!\n");MOV R0, #5
Try it yourself first, then open the solution step by step.
8. Be the processor
What number is in register R0 after these three lines run?
MOV R0, #5
ADD R0, R0, #3
ADD R0, R0, #3Try it yourself first, then open the solution step by step.
9. The portability problem
A company has a large program written in the assembly language of one processor. It buys new computers with a different processor. Explain in two sentences what happens, and how writing the program in C would help.
Try it yourself first, then open the solution step by step.
10. Order the chain
Put these in order, from the person to the result: machine code, programmer, compiler, high-level code, the computer runs it.
Try it yourself first, then open the solution step by step.
11. The history of C
- Who created C, where, and in what year?
- Put these events in order: Unix is rewritten in C; Ken Thompson starts Unix on a PDP-7 in assembly; C is created.
- Why did Unix need a new language?
Try it yourself first, then open the solution step by step.
12. Why C for a microcontroller?
A smartwatch has a small microcontroller with little memory. Give two reasons why its firmware is often written in C.
Try it yourself first, then open the solution step by step.
Visual Studio and the first program
13. The working loop
Put the six stages of the working loop in order: BUILD, CREATE, CHECK, EDIT, RUN, SAVE. What do you do when CHECK fails?
Try it yourself first, then open the solution step by step.
14. Predict the output
What does each program print? Write the exact output, line by line.
#include <stdio.h>
int main(void)
{
printf("Status: ");
printf("READY\n");
return 0;
}#include <stdio.h>
int main(void)
{
printf("Hello,\nC!\n");
return 0;
}Try it yourself first, then open the solution step by step.
15. Team display
Create a new project 02_TeamDisplay and write a program whose console shows exactly:
Console Display
Status: READY
Team: <your team name>Try it yourself first, then open the solution step by step.
16. Sensor console, with a fourth line
Change the program from the lesson so the console shows the device status report and, as a fourth line, the name of one sensor:
Sensor Console
Status: READY
Team: Alpha
Sensor: temperatureTry it yourself first, then open the solution step by step.
17. Boot sequence
You joined a firmware team. Every device prints a boot sequence when it is switched on. Write a program whose console shows exactly:
=== DEVICE BOOT v1.0 ===
Power rails ......... UP
Sensor bus .......... READY
Firmware ............ 1.0.3
Self-test ........... PASS
STATUS: OK(In the full version, the device also asks for a unit ID. Reading input comes with scanf in unit 3.)
Try it yourself first, then open the solution step by step.
18. Fix the bugs
This program has two bugs. Find them, say what Visual Studio reports, and fix them.
#include <stdio.h>
int main(void)
{
printf("Status: READY\n")
print("Team: Alpha\n");
return 0;
}Try it yourself first, then open the solution step by step.
19. House style check
This program builds and runs, but it breaks the house style. Find four problems and rewrite it.
#include <stdio.h>
void main() {
printf("Status: READY\n");
printf("Team: Alpha\n");
}Try it yourself first, then open the solution step by step.
20. From memory
Close Visual Studio. Open it again, open your 01_HelloC solution with the .sln file, build and run it. Then create a new project 03_FromMemory and type the Hello, C! program without looking. How long did it take from Create to a successful run?
Try it yourself first, then open the solution step by step.