1

When we use printf we can write this :

int i = 1;
printf("%06d" i);

Then I got 000001, the 0 fill the space

So is there a way a I can use other character like - to fill the space instead of 0?

Lidong Guo
  • 2,817
  • 2
  • 19
  • 31

1 Answers1

3

It does not look like that is an option with functions of the printf family: your choices there are zeros '0' or spaces ' '. If you must pad with another character, you need to perform the conversion yourself.

One way of doing this would be using sprintf to print into a 6-character buffer with space padding, then replacing spaces with dashes '-', like this:

char buf[12]; // 11 for the number plus one for the terminator
sprintf(buf, "%6d", i);
char *p = buf;
while (*p == ' ') {
    *p++ = '-';
}
printf("%s", buf);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523