Short answer - yes, long answer: not how you want it.
You can use the %* form of printf, which accepts a variable width. And, if you use '0' as your value to print, combined with the right-aligned text that's zero padded on the left..
printf("%0*d\n", 20, 0);
produces:
00000000000000000000
With my tongue firmly planted in my cheek, I offer up this little horror-show snippet of code.
Some times you just gotta 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;
}
printf
format that does what you want), but your comments leave me unsure what you're asking. You talk about a format string and arguments being passed into your function. What function are you asking about? It seems there are things you're not telling us. – Furmanprintf
or similar function, you could add this functionality, but you'd still have to do a loop, which you can still do, as the inputs would be variables, which is perfectly valid! – Turgent