As I noted in a comment, with strings, there's a difference between %8s
and %8.8s
— the latter truncates if the string is longer than 8
. There's another difference; 0
is not a valid modifier for %s
. The %8.8lu
isn't really different from %.8lu
; there's not much difference between %.8lu
and %08lu
, though 0
is older and the .
was added to C90 (so that's pretty old these days). There's a difference between %.8ld
and %08ld
, though, when the values are negative.
Here's some code that illustrates some of the vagaries of the integer formats for printf()
— for both signed and unsigned values. Note that if you have %8.6lu
rather than %8.8lu
(similarly for signed), you get interesting differences.
#include <stdio.h>
static void test_ul(void)
{
char *fmt[] =
{
"%08lu",
"%8.8lu",
"%.8lu",
"%8.6lu",
"%6.8lu",
};
enum { NUM_FMT = sizeof(fmt) / sizeof(fmt[0]) };
unsigned long val[] = { 2413LU, 234512349LU };
enum { NUM_VAL = sizeof(val) / sizeof(val[0]) };
for (int i = 0; i < NUM_FMT; i++)
{
for (int j = 0; j < NUM_VAL; j++)
{
printf("%8s: [", fmt[i]);
printf(fmt[i], val[j]);
puts("]");
}
}
}
static void test_sl(void)
{
char *fmt[] =
{
"%08ld",
"%8.8ld",
"%.8ld",
"%8.6ld",
"%6.8ld",
};
enum { NUM_FMT = sizeof(fmt) / sizeof(fmt[0]) };
long val[] = { +2413L, -2413L, +234512349L, -234512349L };
enum { NUM_VAL = sizeof(val) / sizeof(val[0]) };
for (int i = 0; i < NUM_FMT; i++)
{
for (int j = 0; j < NUM_VAL; j++)
{
printf("%8s: [", fmt[i]);
printf(fmt[i], val[j]);
puts("]");
}
}
}
int main(void)
{
test_ul();
test_sl();
return 0;
}
Output (GCC 7.1.0 on macOS Sierra 10.12.5):
%08lu: [00002413]
%08lu: [234512349]
%8.8lu: [00002413]
%8.8lu: [234512349]
%.8lu: [00002413]
%.8lu: [234512349]
%8.6lu: [ 002413]
%8.6lu: [234512349]
%6.8lu: [00002413]
%6.8lu: [234512349]
%08ld: [00002413]
%08ld: [-0002413]
%08ld: [234512349]
%08ld: [-234512349]
%8.8ld: [00002413]
%8.8ld: [-00002413]
%8.8ld: [234512349]
%8.8ld: [-234512349]
%.8ld: [00002413]
%.8ld: [-00002413]
%.8ld: [234512349]
%.8ld: [-234512349]
%8.6ld: [ 002413]
%8.6ld: [ -002413]
%8.6ld: [234512349]
%8.6ld: [-234512349]
%6.8ld: [00002413]
%6.8ld: [-00002413]
%6.8ld: [234512349]
%6.8ld: [-234512349]