Section A.1 Compiling a C Program
Python is interpreted: the interpreter reads your program and runs it one statement at a time, which is why you can type a line at the Python prompt and see its result immediately. C is compiled: before anything runs, a compiler translates the whole program into machine instructions for the processor, and only that translation runs. In this course, the STM32CubeIDE compiles your program when you click Build. When you click Run, it copies the result into the STM32C031C6’s flash memory, where the program is stored. We follow that path from the C source to the pin in Subsection 1.4.1.
One consequence, very different from how Python treats most errors, is that a program with a single compile error anywhere in it does not run at all: the build fails, no new program is produced, and the STM32CubeIDE lists what went wrong.
Another consequence is that many mistakes are found at build time, whereas Python reports them only when the faulty line runs. For example, a misspelled variable name produces a
NameError in Python, raised when that line is reached. In C the build fails, because every name has to be declared before it is used (Section A.4), and the compiler treats a misspelled name as a new name that was never declared.
Once the C program is running, however, there is no interpreter or operating system to report a mistake. A Python program that divides by zero or reads past the end of a list stops with a message saying what went wrong and on which line. C does not check for either. On the STM32C031C6 your program is the only thing running, and a mistake of that kind produces a wrong value, or a program that stops responding, with no message at all. There is also no screen to print to yet: printing to your laptop needs the serial connection of Chapter 3. Figuring out what a silent program is actually doing, from the board’s LED (light-emitting diode) and the debugger, is the subject of Chapter 7.
