Is this possible with printf?

I understand that with a large string, you can print the first few characters with:

printf(".5s\n",string);

and with the bottom line, you can place it in space:

printf("% 5s\n",string);

Is there a way to reach both of them at once? those. fill it with 0 or a space if it's short, and truncate it if it's long?

+4
source share
1 answer

Yes, you can simply combine it with this:

printf("%5.5s\n", string);

So, if your line is 1, the output is:

    1
//^ 4 Spaces here

And if your line is 123456, the output is:

12345
   //^ 6 doesn't get displayed

Also for more information about printf()and as a reference, see this link: http://www.cplusplus.com/reference/cstdio/printf/

+11
source

All Articles