printf length modifiers

suggest change

A 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents