Home > Net >  compare const char * string to uint32_t value at compilation
compare const char * string to uint32_t value at compilation

Time:10-20

I have version in 2 different descriptions, one as string and the other as value for example:

static const char * versionStr = "03-October-2022" ;
static const uint32_t versionVal = 20221003 ;

How can I test if they are equal (in compile time) to make sure I didn't update one without the other?

Solution in pure preprocessor smarts, I'm bound to C 98.

Quite a challenge :)

CodePudding user response:

You can't compare them at compile-time. But, what you can do is build up the values using preprocessor macros for the common elements, thus reducing the possibility of making a mistake, eg:

#define c_day           03
#define c_year          2022
#define c_month         10
#define c_month_name    October

#define STRINGIFY(param) #param
#define make_versionStr(y, m, d) (STRINGIFY(d) "-" STRINGIFY(m) "-" STRINGIFY(y))

#define make_versionVal_concat(y, m, d) (y ## m ## d)
#define make_versionVal(y, m, d) make_versionVal_concat(y, m, d)

static const char* versionStr = make_versionStr(c_year, c_month_name, c_day);
static const uint32_t versionVal = make_versionVal(c_year, c_month, c_day);

Online Demo

  • Related