C Basics: Variables, Types, Control Flow, Functions
Embedded → Embedded C Language Foundations
The floor everything else is built on
A microcontroller runs plain C. Every driver you'll ever read comes down to three things: variables/types, control flow, functions. The video covers all of it in depth — the quick references and interactive piece below are for after you've watched it, or instead of it.
Programming in C — Jenny's Lectures CS IT (Introduction through Loops)
Loading…
Types, sized
| Type (stdint.h) | Bits | Typical firmware use |
|---|---|---|
| uint8_t | 8 | one GPIO pin's on/off state |
| uint16_t | 16 | a 12-bit ADC reading |
| uint32_t | 32 | a tick counter, a whole register's value |
Plain
int's size isn't guaranteed across compilers — stdint.h's named-width types are why a driver header full of uint32_t is being exact about register width, not just styling.Branch vs. repeat
| Construct | Use it when |
|---|---|
| if / else | one condition, two paths |
| switch | dispatching on a fixed set of states (e.g. an ISR's flag) |
| for | iteration count known up front |
| while | repeat until a condition changes (e.g. polling a busy bit) |
switch (uart_status) {
case UART_IDLE: start_next_byte(); break;
case UART_BUSY: wait_more(); break;
default: break;
}
while (!(status_reg & BUSY_CLEAR_BIT)); // real polling loop, not a bugFunctions: hide the register mess behind a name
void gpio_set_pin(uint8_t pin, uint8_t value) {
if (value) GPIOA_ODR |= (1u << pin);
else GPIOA_ODR &= ~(1u << pin);
}Try it — step through a for-loop, watch the state change
Line-by-line trace
→ int sum = 0;
for (int i = 1; i <= 4; i++) {
sum += i;
}i
—
sum
0
Why this works · explanation & key points
This is exactly what a debugger's single-step does: pause before each line, show you the current values, then advance. Click through and watch i and sum update one line at a time.
Practice
Open Learn-C.org's "Variables and Types" in-browser editor and work through it alongside the "Conditions", "For loops" and "Functions" chapters. Then rewrite the
uart_status switch above from scratch without looking, and add a fourth case of your own choosing.Completion saves automatically after you read through to the end.
Next lesson →