TTT
TTT

Reputation: 186

How to left-align with snprintf

I am writing some C code and have been formatting strings like this

int buffer[8];
snprintf(buff, 8, "%3d", someNumberVariable);
my_lcd_stringout(buffer);

This formats any texts that grows in size to be pushed to the left. For example, imagine the _ character is a space:

"__7__"
"_74__"
"748__"

Does anybody know how I can reverse this pushback? so that my increasing numbers are formatted this way as they grow (without leaving the characters there when they shrink of course):

"__7__"
"__74_"
"__748"

Upvotes: 0

Views: 521

Answers (1)

ikegami
ikegami

Reputation: 386321

" %-3d"

The minus makes it left-aligned.

Upvotes: 3

Related Questions