jankli
jankli

Reputation: 41

c and formatted output

char *star="*";
int space=5;

printf("%5s",star);

I want to give 5 spaces to my star so it should look on command line like | *|

But the space quantity must be a variable so users can decide.

I tried printf("%%ds",i,s); not worked. Thank you.

Upvotes: 0

Views: 101

Answers (1)

Mario The Spoon
Mario The Spoon

Reputation: 4859

printf("%*s", space, star);

or

printf( "%*.*s", space, space, star);

then you will always print max. 5 chars.

hth

Mario

Upvotes: 3

Related Questions