#include <stdio.h>
#define PREC 4
int main()
{
long l = 12;
printf("%PRECld", l);
}
How would I be able to use PREC
to define the precision wanted? i.e. the expected output would be 0012
. Currently printing %PRECld
.
To expand the macro to make it part of a compile time format string, you'll need a stringification macro (yes, the two layers are necessary; if you only defined #define STR(x) #x
, STR(PREC)
would insert the string "PREC"
, not "4"
as you want):
#define _STR(x) #x
#define STR(x) _STR(x)
Then you can use it like so:
printf("%0" STR(PREC) "ld", l);
That will be expanded by the preprocessor to:
printf("%0" "4" "ld", l);
and since adjacent string literals are treated as a single string, it ends up compiling as if you directly typed:
printf("%04ld", l);
For a runtime solution, you use *
to set precision dynamically from an argument, with no need for a stringification macro:
printf("%0*ld", PREC, l);
In theory, this is slightly slower at runtime, since it needs to parse a more complex format string, pass and receive more arguments, etc., but in practice I/O is so expensive that the cost of a little CPU fiddling is trivial in comparison.
Note the added 0
in both versions, to make it pad with zeroes, not spaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With