tags:

views:

473

answers:

6

While I was working i came across a code which was written by somebody else. i see a statement as ,

sprintf(o_params->o_file_name,
        "%s_%s_%04.4d_%s_%s.ASC",
        "OUTD", "RM", sequence_no, DateStamp_buf1, TimeStamp_buf1
);

In the above statement, I see %04.4d. Is this a correct format specifier?

The variable sequence_no is static int and it doesn't have decimal.

+1  A: 

%d is the format specifier for integers in printf:

The conversion specifiers and their meanings are:

diouxX  The int (or appropriate variant) argument is converted to signed
        decimal (d and i), unsigned octal (o), unsigned decimal (u), or
        unsigned hexadecimal (x and X) notation.  The letters ``abcdef''
        are used for x conversions; the letters ``ABCDEF'' are used for X
        conversions.  The precision, if any, gives the minimum number of
        digits that must appear; if the converted value requires fewer
        digits, it is padded on the left with zeros.

Assuming, @Kinopiko's interpretation is correct, here is the distinction between %4d versus %4.4d (also showing why %04.4d is not needed and the appropriate warning being emitted by gcc):

#include <stdio.h>

int main(void) {
    int t = 123;
    printf("%%04.4d:\t" "%04.4d\n", t);
    printf("%%4.4d:\t"   "%4.4d\n", t);
    printf("%%04d:\t"     "%04d\n", t);
    printf("%%4d:\t"       "%4d\n", t);
    return 0;
}
C:\Temp> gcc z.c -o z.exe -Wall
z.c: In function 'main':
z.c:5: warning: '0' flag ignored with precision and '%d' printf format
z.c:5: warning: '0' flag ignored with precision and '%d' printf format

C:\Temp> z
%04.4d: 0123
%4.4d:  0123
%04d:   0123
%4d:     123
Sinan Ünür
No, he's asking why there is a `.4` after the `%04`.
Kinopiko
+6  A: 

From the FreeBSD manpage man 3 printf

An optional precision, in the form of a period . followed by an optional digit string. If the digit string is omitted, the precision is taken as zero. This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, the number of digits to appear after the decimal-point for a, A, e, E, f, and F conversions, the maximum number of significant digits for g and G conversions, or the maximum number of characters to be printed from a string for s conversions.

So in this case, %04.4d, the .4 specifies that all four digits of the number should be printed. Of course, the 04 part just pads the number with leading zeros if it is less than 1000. However, in this case, as the above manual page states,

`0' (zero) Zero padding. For all conversions except n, the converted value is padded on the left with zeros rather than blanks. If a precision is given with a numeric conversion (d, i, o, u, i, x, and X), the 0 flag is ignored.

Since surely all four digits would be printed anyway, my guess would be that it was just a leftover or typo or something. This syntax produces compiler warnings with gcc -Wall (see Sinan Unur's example) but it does not seem to be an actual error.

Kinopiko
Yes,this is what my doubt was...if %04d is serving the same purpose why that .4 is added to it?
Vijay Sarathi
+1 for noting that "%04.4d" is more than necessary. In fact, the "0" is ignored: From the sprintf man page: "If a precision is given with a numeric conversion (d, i, o, u, x, and X), the 0 flag is ignored."
Heinzi
Thanks for pointing that out. I've modified the answer.
Kinopiko
A: 

look here: sprintf

.number: For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written ...

It is valid.

Tobias Langner
+1  A: 

"dot whatever" specifies the precision. According to sprintf's man page, this means the following for ints (d):

The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros.

Heinzi
A: 

%04.4d means the corresponding argument is an int, and it should be printed with leading zeros with field width 4 and precision 4.

It appears correct to me (assuming that the output looks the way you want).

John Zwinck
Yes,I agree but why .4 is added to it since 04d serves the same pupose giving the same output.
Vijay Sarathi
A: 

%04.4d looks valid to me. Is it not just a decimal with 4 characters width and 4 precision?

%[flags][width][.precision][length]specifier

See here

link text

pxb
"r"->"4" here I think.
Kinopiko
if %04d is serving the same purpose why that .4 is added to it?
Vijay Sarathi
Thanks Kinopiko, fixed the typo
pxb