I have a typedef in an if macro, something like:
#ifdef BLA_BLA
typedef int typeA
#elseif
typedef double tyeA
#endif
printf("%d" , a); printf("%l" , a);
I am wondering what is the best approach when I write a printf for this case? (%d or %l).
I know I can define a fixed string in the macro as well. But is it the best way?
Do you really want to define a type as either integer or floating-point? They’re both numeric, but their behavior is so different in many ways that writing code that works correctly in either case is going to be difficult.
In many cases, you can convert to a type that’s wide enough to cover the ranges of both possible types. A simple example:
More generally, you can convert to
intmax_toruintmax_t, defined in<stdint.h>or<inttypes.h>, using"%jd"or"%ju", respectively.You can almost do the same thing by converting everything to
long double, but that can lose precision for large integer values.