Printf specifiers
One anti-pattern in C is that format-specifiers for printf. These require a lot of effort when creating multi-platform code (ie 32-bit and 64-bit)
Base type specifiers
C2 changes the specifiers to allow only the following types:
%c
- print character%d
- print decimal number%x
/%X
- print hexadecimal number%o
- print octal number%f
- print floating-point number%p
- print pointer%s
- print string%%
- print %
The %d
specifier is used for all integer numbers (i8, i64, u8, u64, bool, etc). The C2
compiler will automatically use the correct one for the type. Likewise the %f
specifier can be used for both f32 and f64.
Never worry about %llu
, %ld
or %"PRIu64"
again!
Other options
Next to that, the printf specifier format still allows all other C options like size and alignment. It supports the full format of:
%[flags][width][.precision][size]type
for example:
printf("%-4s %06d %7.3f", text, number, float_number);
Attribute
To allow checking of the format, a function must be marked with the printf_format attribute.