Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any setfill() alternative for C?

In C++:

int main()
{
    cout << setfill('#') << setw(10) << 5 << endl;

    return 0;
}

Outputs:

#########5

Is there any setfill() alternative for C? Or how to do this in C without manually creating the string?

like image 611
Donotalo Avatar asked Dec 22 '22 22:12

Donotalo


1 Answers

 int x= 5; 
 printf("%010d",x);

will output : 0000000005

Now if you really want '#' instead of '0' you'll have to replace them manually in the string.

Maybe :

char buf[11], *sp = buf; 
snprintf(buf, 11, "%10d", x); 
while( (sp = strchr(sp, ' ')) != '\0'){ *sp = '#'; }
puts(buf); 
like image 80
Ben Avatar answered Jan 25 '23 23:01

Ben