c-guide.md
July 1, 2021 · View on GitHub
C Specific Advice
Object Oriented Programming in C
C does not support real OOP but the following rules can help to transfer some ideas of OOP to C.
-
Think of a file as a class: Functions that appear in the header file are public, while functions that only appear in the
.cfiles are private. -
Create namespaces: use a capital prefix before each “public” function name to indicate their belonging to a certain source file. For example, in
led.cuseLED_switchOn( redLed );instead ofswitchOn(redLed) -
Use structs to encapsulate attributes: Instead of individual variables, group variables that would belong to a class in a struct. You can then pass a reference of this struct as the first argument to a function.
Further
- Constants: for constants prefer
constover preprocessor#define - Booleans: Use
boolfromstdbool.hwhenever you have a boolean value - Sizeof: use
sizeofon the variable; not the type