In C, we like to use #define
or const
globals to avoid hard-coding values and strings.
How can we avoid hard-coding strings that need to be formatted? Consider, for instance, building a filepath to an i2c bus in linux:
snprintf (dev, size, "/dev/i2c-%u", bus_no)
I don't like the idea of adding a #define I2C_BUS_FMT "/dev/i2c-%u"
to the top of the file (or the header file), because when you read snprintf (dev, size, I2C_BUS_FMT, bus_no)
you can't see how many and of what type the format specifiers are... you need to take it by faith that it accepts a single unsigned int or go check.
The same problem applies to const
globals.
How can I avoid hard-coding format strings without moving important information away from the place where it is used?