C

printf format check

extern int my_printf (void *my_object, const char *my_format, ...) __attribute__ ((format (printf, 2, 3)));

volatile

  • volatile uint8_t *foo; - tells the compiler the memory being pointed to is volatile
    • uint8_t volatile * p_ledreg = 0x10000000; - In the above code, the variable p_legreg is a pointer to a volatile 8-bit unsigned register located at address 0x10000000.
  • uint8_t * volatile foo; - mark the pointer itself as volatile

compile time asserts

  • Can be used in local block without compiler warning:
  • Cannot be used outside functions (i.e. header)
#define COMPILE_TIME_ASSERT(pred)   switch(0){case 0:case pred:;}   // NOLINT
  • can be used in headers
  • compiler show warnings if used locally (typedef not used)
#ifndef CTASSERT                /* Allow lint to override */
#define CTASSERT(x)             _CTASSERT(x, __LINE__)
#define _CTASSERT(x, y)         __CTASSERT(x, y)
#define __CTASSERT(x, y)        typedef char __assert ## y[(x) ? 1 : -1]
#endif

MAX_VALUE

#define IS_TYPE_SIGNED(a) ((a-1) < 0)
#define MAX_VALUE_UNSIGNED(a) (((unsigned long long)1 << \
        (sizeof(a) * CHAR_BIT)) - 1)
#define MAX_VALUE_SIGNED(a) (MAX_VALUE_UNSIGNED(a) >> 1)
#define MAX_VALUE(a) (IS_TYPE_SIGNED(a) ? \
        MAX_VALUE_SIGNED(a) : MAX_VALUE_UNSIGNED(a))

sizeof

#define member_size(type, member) sizeof(((type *)0)->member)