AnnoyinC
AnnoyinC

Reputation: 496

printf variable char multiple times

I need to print a certain ASCII character, DOUBLE_HORIZONTAL_LINE(205) "═" 20 times. The file is encoded in unicode however, so I need to do something like printf("%c", 205), which is fine, except I can't figure out how to repeat the char. I tried using %1$c, but that just printed "$c" literally.

printf("%1$c%1$c\n", 205); //205 = ASCII '═'

I expected the above code snippet to print ═ twice, instead it prints $c$c.

Do I really need to make a for i<20 printf loop?

Edit: trying to directly printf("═"); will result in ΓòÉ being outputted. Again, because of the unicode-ascii conversion.

Upvotes: 0

Views: 431

Answers (1)

John Zwinck
John Zwinck

Reputation: 249642

You can do this:

char bar[21];
memset(bar, 205, 20);
bar[20] = '\0';
puts(bar);

As a bonus, this is more efficient than printf(), because the entire string is written to stdout at once.

Upvotes: 3

Related Questions