Centering strings with printf()

printf by itself can’t do the trick, but you could play with the “indirect” width, which specifies the width by reading it from an argument. Lets’ try this (ok, not perfect)

void f(char *s)
{
        printf("---%*s%*s---\n",10+strlen(s)/2,s,10-strlen(s)/2,"");
}
int main(int argc, char **argv)
{
        f("uno");
        f("quattro");
        return 0;
}

Leave a Comment