Avatar ·

What is unsigned char in C++ and how is it different from regular char?

Hello, colleagues! I started learning C++ and encountered some confusion. In textbooks, I constantly come across the `char` and `unsigned char` types. At first glance, they both store characters, but I feel there is a difference, and it's important. I wrote a simple example: ```cpp #include int main() { char c = 255; unsigned char uc = 255; std::cout << "char: " << (int)c << std::endl; std::cout << "unsigned char: " << (int)uc << std::endl; return 0; } ``` On my system (Windows, Visual Studio), the first output gives -1, and the second gives 255. I expected both to be 255. Why is that? And in what cases should I use `unsigned char` instead of regular `char`? I've read that `char` can be signed or unsigned depending on the compiler, but that sounds somehow vague.