String Padding in C

It might be helpful to know that printf does padding for you, using %-10s as the format string will pad the input right in a field 10 characters long

printf("|%-10s|", "Hello");

will output

|Hello     |

In this case the – symbol means “Left align”, the 10 means “Ten characters in field” and the s means you are aligning a string.

Printf style formatting is available in many languages and has plenty of references on the web. Here is one of many pages explaining the formatting flags. As usual WikiPedia’s printf page is of help too (mostly a history lesson of how widely printf has spread).

Leave a Comment