The short answer is yes, the long answer is: not the way you want it.
You can use the% * printf form, which takes a variable width. And, if you use β0β as the value for printing, combined with right-aligned text with zero padding on the left.
printf("%0*d\n", 20, 0);
produces:
00000000000000000000
With my tongue firmly planted in my cheek, I offer this small piece of horror code.
On several occasions, you just need to do things badly to remember why you try so hard the rest of the time.
#include <stdio.h> int width = 20; char buf[4096]; void subst(char *s, char from, char to) { while (*s == from) *s++ = to; } int main() { sprintf(buf, "%0*d", width, 0); subst(buf, '0', '-'); printf("%s\n", buf); return 0; }
synthesizerpatel May 23 '13 at 12:08 a.m.
source share