http://www.lemoda.net/c/printf-left-justify/index.html
This example program demonstrates how to left-justify output in printf
.
#include <stdio.h> int main () { int x = 345; const char * y = "monkeys"; /* Demonstrate with numbers. */ printf ("<%d> is not justified. ", x); printf ("<%5d> is right-justified. ", x); printf ("<%-5d> The minus sign makes it left-justified. ", x); /* Demonstrate with strings. */ printf ("'%s' is not justified. ", y); printf ("'%10s' is right-justified. ", y); printf ("'%-10s' is left-justified using a minus sign. ", y); return 0; }
It outputs the following:
<345> is not justified. < 345> is right-justified. <345 > The minus sign makes it left-justified. 'monkeys' is not justified. ' monkeys' is right-justified. 'monkeys ' is left-justified using a minus sign.