Home > front end >  Problems with format specifiers
Problems with format specifiers

Time:10-07

I don't understand the meaning of the numbers in declarations like lu. What does it happen to a lu type number if I write it only by %lu? I'm writing such number on a file. This format is used in my code to write a long long int on a file using DBGPRINT

CodePudding user response:

If you type "man printf" into a search engine, the first hit should be a Linux manual page. It says:

0 The value should be zero padded. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversions, the converted value is padded on the left with zeros rather than blanks. If the 0 and - flags both appear, the 0 flag is ignored. If a precision is given with a numeric conversion (d, i, o, u, x, and X), the 0 flag is ignored. For other conversions, the behavior is undefined.

So that tells you that the first 0 is for zero-padding values that are shorter than their field width.

Then we have, a bit further down on the manual page:

Field width An optional decimal digit string (with nonzero first digit) specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given). [...]

So, ld is a conversion specifier that converts a long integer to decimal form, while making sure that any values that need fewer than 2 characters are zero-padded.

  • Related