The C Preprocessor
#include, #define macros and conditional compilation run before the compiler sees your code.
Easy
#define F_CPU 16000000UL
#define SQUARE(x) ((x) * (x)) // parenthesise everything!
#ifdef DEBUG
#define LOG(msg) printf("%s\n", msg)
#else
#define LOG(msg)
#endif
#ifndef SENSOR_H // include guard
#define SENSOR_H
/* declarations */
#endif
Macros are plain text substitution: SQUARE(i++) increments i twice. Prefer static inline functions and const variables when possible.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.