printf length modifiers
suggest changeA length modifier changes the type expected by a conversion specifier:
| Modifier | Integer conversions | Floating conversions |
|---|---|---|
hh |
promoted signed char or unsigned char |
— |
h |
promoted short or unsigned short |
— |
l |
long or unsigned long |
no change for printf floating conversions |
ll |
long long or unsigned long long |
— |
j |
intmax_t or uintmax_t |
— |
z |
size_t or its corresponding signed type |
— |
t |
ptrdiff_t or its corresponding unsigned type |
— |
L |
— | long double |
Examples:
printf("%zu\n", sizeof object);
printf("%td\n", end - begin);
printf("%lld\n", value);
printf("%Lf\n", long_double_value);
The modifier must match both the conversion and the argument type. For example, %lu expects unsigned long, not merely any unsigned integer with the same width.
printf and scanf do not assign identical meanings to every modifier. In particular, %lf in scanf expects double *, whereas %f expects float *; for printf, both %f and %lf consume a promoted double.
Compilers may provide additional nonstandard modifiers. Avoid them unless the program intentionally targets that implementation.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents