Learning C
C is a general purpose programming language which was created in 1972 by Dennis Ritchie.
While it was originally designed to implement operating systems, the features which made the language ideal for that purpose, also make it ideal for developing software for small resource constrained MCUs.
On this page we will go through how to use C for embedded development.
Hello World - Desktop vs. MCU
When learning any programming language, the first "project" will typically be a small program which prints out "Hello World!". C was designed to give programmers relatively direct access to the target hardware. This was ideal for creating operating systems and indeed most, if not all, serious operating systems are largely implemented in C. This design also make C an ideal language for developing embedded software targeting small MCUs with very limited resources.
Standard UNIX
On a standard UNIX system, that program, implemented in standard C, would typically look something like this:
1 #include <stdio.h>
2
3 int main(void) {
4 printf("Hello World!\n");
5 return 0;
6 }
Let us break down the classic C program line by line.
Line 1 contains a Preprocessor Directive. This essentially instruct the compiler to include a header file called stdio.h (Standard Input/Output). This file in turn contains the blueprint for functions such as printf.
In line 3 follows a function declaration, creating a function named main. Every C program includes a function called main and this function serves as the entry point. The int tell the compiler that this function will return an integer value and the void explain that the function does not need any input arguments. The actual function code is enclosed in a code block using curly brackets.
In line 4 we call the function printf which will print a string on the terminal executing the program. The printf is provided with an argument, which is a string enclosed in double-quotes "Hello World!". The compiler will include this string as a constant.
Finally in line 5, we return the value 0.
Most UNIX like systems have (or bloody should have) a C compiler, typically named cc, so above hello.c can be built into a binary executable thus:
$ cc hello.c -o hello
This will produce a binary hello, which can now be executed:
$ ./hello
Hello World!
Embedded
To be added
Variables & Fixed-Width Types
To be added
Control Flow & Decision Making
To be added
Loops & Iteration
To be added
Functions & Modular Code
To be added
Bitwise Operations Masterclass
To be added
Pointers Demystified
To be added
Arrays & Pointer Arithmetic
To be added
Structs & Memory Alignment
To be added
The volatile Keyword
To be added
Type Casting & Unions
To be added
Function Pointers & Callbacks
To be added
Enums & State Machines
To be added
Dynamic Memory vs. Static Allocation
To be added
Macros, Pragmas & Preprocessor
To be added
Miscellaneous Links
To be added