As I noticed in the comment, with strings, there is a difference between %8s and %8.8s - the latter truncates if the string is longer than 8 . There is one more difference; 0 not a valid modifier for %s . %8.8lu is actually no different from %.8lu ; there is not much difference between %.8lu and %08lu , although 0 older as well . was added to the C90 (so pretty old these days). There is a difference between %.8ld and %08ld , although when the values ββare negative.
Here is some code that illustrates some of the vagaries of integer formats for printf() - for both signed and unsigned values. Please note that if you have %8.6lu and not %8.8lu (similarly for a signed one), you will 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]
source share