English 中文(简体)
C Formatpecifier question
原标题:C format specifier question
  • 时间:2009-10-20 13:22:24
  •  标签:

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
);

在上述发言中,见%04.4d。 这是一种正确的模式吗?

变量sequence_no is static int and it doesn t have decimal.

最佳回答

http://www.freebsd.org/cgi/man.cgi?query=printf&apropos=0&sektion=3&manpath=FreeBSD+7.2-RELEASE&format=html

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.

因此,%04.4d,.4具体规定,所有四位数字都应印制。 当然,<代码>04部分只是按低于1,000的正数排列。 然而,如上述人工页面所示,在这种情况下,

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

既然所有四位数肯定都会转印,那么我的猜测就是,这只是一个左边或打字。 该星号为:<代码>gcc-Wall的汇编器警报。 (见Sinan Unur的例子),但似乎没有实际错误。

问题回答

“不管怎样”都具体规定了准确性。 根据手印页,这是指ts(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.

%04.4d 是指对应的论点是暗中,应当用头零印制,实地为4和精准4。

我认为,这似乎是正确的(假设产出看着你想要的方式)。

>::

转换光谱仪及其含义是:

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.

假设“Kinopiko”的解读是正确的,此处是<条码>%4d 和<条码>%4.4d<>的区别。 (另见为什么不需要<代码>%04.4d,并通过<代码>gcc发出适当警告):

#include <stdio.h>

int main(void) {
    int t = 123;
    printf("%%04.4d:	" "%04.4d
", t);
    printf("%%4.4d:	"   "%4.4d
", t);
    printf("%%04d:	"     "%04d
", t);
    printf("%%4d:	"       "%4d
", 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




相关问题
热门标签