



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Simplified study notes · Data types · Operators · Control flow · Bit manipulation · Enums & FSM · Qualifiers · Functions
A variable is a named place in RAM that stores a value which can change while the program runs. Its data type decides two things: how many bytes it gets, and how the bits are interpreted.
Type Size Format Holds Example char 1 byte %c one character 'A' int 4 bytes %d whole numbers 60 float 4 bytes %f decimals (single precision) 36.6f double 8 bytes %lf decimals (double precision) 3.
char grade = 'A'; int speed = 60; float temp = 36.6f; double pi = 3.1415926535;
printf("%c\n", grade); printf("%d\n", speed); printf("%f\n", temp); printf("%lf\n", pi);
Output
A 60
Extra tip
Signed Unsigned Top bit (MSB) used as the sign bit used as part of the value Range for n bits − 2 n−^1 to 2 n−^1 − 1 0 to 2 n^ − 1 Example with 8 bits −128 to 127 0 to 255
When a result is bigger than the type can hold, overflow happens. For unsigned types the value wraps
unsigned char count = 250; count = count + 10; // 260 does not fit -> wraps around -> count = 4
Correction to the original note
Extra tip Overflow of signed integers is undefined behaviour in C, so avoid it.
2 Constants & Operators
Constants: const vs #define
A constant is a value that cannot change after it is created.
const #define Handled by the compiler the preprocessor What it does real variable, read-only pure text replacement Type checking yes no Scope rules obeys scope none When during compilation before compilation
const int MAX_SPEED = 120; // typed, scoped, checked by the compiler #define LED_PIN 5 // replaced by the text "5" before compiling
for ( int i = 0; i < 5; i++) { if (i == 2) continue ; // skip 2 if (i == 4) break ; // stop the loop at 4 printf("%d ", i); }
Output
4 Bit Manipulation
The mask idea
Goal Code Why it works Set bit n REG |= (1U << n); OR with 1 forces the bit to 1 Clear bit n REG &= ~(1U << n); inverted mask has 0 only at n, so AND clears it Toggle bit n REG ^= (1U << n); XOR with 1 flips the bit; XOR with 0 changes nothing Test bit n if (REG & (1U << n)) AND isolates that bit; non-zero means it is set
Operation Result Explanation Set bit 2 0000 1110 bit 2 becomes 1 Clear bit 3 0000 0010 bit 3 becomes 0 Toggle bit 1 0000 1000 bit 1 flips from 1 to 0
Extract a field: shift it down to bit 0, then mask its width. Modify a field: use a read-modify-write cycle.
1. Clear the field with an inverted, shifted mask. 2. OR the new value in, shifted to the field position.
// Extract val = (REG >> Shift) & Mask;
// Modify (read-modify-write) REG = (REG & ~(Mask << Shift)) | ((Val & Mask) << Shift);
// Example: 3-bit field at offset 4 -> Mask = 0x7, Shift = 4
5 Enumerations & State Machines
Values start at 0 and go up by 1 unless you set them.
typedef enum { SYS_IDLE = 0, SYS_RUNNING = 1, SYS_ERROR = 2 } SystemState_t;
An FSM moves between explicit states when an input event happens. A state transition table lists, for every current state and input, the next state.
Example: a 2-state toggle controlled by a button.
Current state Button pressed? Next state OFF no OFF OFF yes ON ON no ON ON yes OFF
typedef enum { STATE_OFF, STATE_ON } State_t;
void process_button_press(State_t *state, int btn_pressed) { if (!btn_pressed) return ; // no press -> stay in current state
switch (*state) { case STATE_OFF: *state = STATE_ON; break ; case STATE_ON: *state = STATE_OFF; break ; } }
6 Qualifiers & Storage
const qualifier
Marks a variable as read-only. This allows the compiler to keep the data in ROM/Flash instead of RAM, which saves precious RAM.
int add( int a, int b); // declaration int add( int a, int b) { return a + b; } // definition
C always passes by value : the function gets a copy of each argument, so changing the copy never changes the caller’s variable. To modify the original, pass its address (a pointer) and dereference it.
void change_copy( int x) { x = 99; } // changes only the copy void change_orig( int *x) { *x = 99; } // changes the caller's variable
int n = 5; change_copy(n); // n is still 5 change_orig(&n); // n is now 99
A recursive function calls itself until it reaches a base case. Every call adds a stack frame to the microcontroller’s small RAM stack, so deep or endless recursion causes a stack overflow.
Watch out Recursion is restricted or banned in safety-critical embedded code (for example MISRA C ). Use a loop instead.
8 Quick Recap