1

I can easily do "char c = 219;" and it outputs the █ char when i print c. No problem there. But when I do

char colorlist[5] = {32, 176, 177, 178, 219};
printf("%c", colorlist[4]);

It doesn't work. I want, that f.e. if I type printf("%c", colorlist[4]);, it should print █.

But I get an error while initializing. How can I initialize chars in an initializer list by their corresponding number?

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
FireFuro99
  • 357
  • 1
  • 5
  • 18
  • 2
    It's compiler-specific if `char` is a signed or unsigned type. In your case it seems that `char` is signed, which means four of your values are to large (since a signed 8-bit integer usually have values from -128 to +127). – Some programmer dude Dec 09 '18 at 20:22
  • Instead of `219`, does `'\xDB'` work? – Eljay Dec 09 '18 at 20:23
  • "xDB was not declared in this scope". So no, it does not work. I am on Windows, maybe that's why. – FireFuro99 Dec 09 '18 at 20:25
  • @Someprogrammerdude Thank you sir! I changed it to unsigned int colorlist[5] and now it works! Thank you very much! – FireFuro99 Dec 09 '18 at 20:26
  • You have not given much context for this question. Things to consider: (a) Use `unsigned char` instead of `char`, so that you can represent the values you desire. (b) Use octal or hexadecimal escape sequences for characters, such as `'\260'` or `'\xb0'` for 176. (This will have implementation-defined behavior for the mapping from values outside the range of `char` to representable values.) (c) If the values represent characters to be printed, rather than numerical data (e.g., color codes embedded in a string of characters, like an ANSI escape sequence), use character constants, not numbers. – Eric Postpischil Dec 09 '18 at 20:28
  • @EricPostpischil I've already got the solution, as you can see If you look at my answers, but thanks anyway. Maybe your information will help someone in the future :). And escape sequences don't work with windows afaik. – FireFuro99 Dec 09 '18 at 22:51

0 Answers0