4

i have a problem with printf width modifier in C

example:

char a[] = "o", b[] = "l";

printf("%-3s %s", a, b);

console output gives me 3 spaces between strings, but when I change "o" to "ó", console shows 2 spaces between. Every time i use character like "ł", "ó", "ś" in the string, width modifier is shortened by 1 sign, why this happens?

OS X 10.11 (El Captain)

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
M.Chelm
  • 61
  • 6

1 Answers1

3

"Special" characters as you are showing them need more bytes (char) to be represented in a string. Your arbitrary limit of 3 is not enough, raise it to some suitable value.

In addition, the way and length that such special characters are represented depends on the system. For portable code you should never make such arbitrary assumptions.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177