tags:

views:

123

answers:

1

I am getting the following warning when compiling some legacy C code on Ubuntu Karmic, using gcc 4.4.1

The warning is:

src/filename.c:385: warning: '0' flag ignored with precision and ‘%i’ gnu_printf format

The snippet which causes the warning to be emitted is:

 char     buffer[256] ;
 long     fnum ;
 /* some initialization code here ... */
 sprintf(buffer, "F%03.3i.DTA", (int)fnum);  /* <- warning emitted here */

I think I understand the warning, but I would like to check in here to see if I am right, and also the (definite) correct way of resolving this.

+2  A: 

From the printf(3) man page:

   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 you can have either a zero fill or a minimum number of digits, but not both.

Ignacio Vazquez-Abrams
Nothing more to say. RTFM.
Clifford