开发者

C format specifier question

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.


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.


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


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.


%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).


%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


%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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜