I’m trying to cast the value 247 to a char in C++ (Visual Studio). I’m getting a Debug Assertion Failed error, saying unsigned(c+1) should be less than or equal to 256. I don’t understand where this is coming from since the value 247 falls in that range.
Asked
Active
Viewed 235 times
1
-
6When you have a question about code please include a [mcve] of that code in the question – 463035818_is_not_an_ai Jul 05 '21 at 12:36
-
Also, this will depend on the `/J` switch - `char` can be signed or unsigned in Visual Stduio. Do you use the `/J` switch? (we can't see that from just the example) – MSalters Jul 05 '21 at 12:38
-
I doubt that your casting is causing the assertion. – molbdnilo Jul 05 '21 at 13:01
-
Based on the description of the problem and the code provide, I speculatively surmise there is a bug in the code. – Eljay Jul 05 '21 at 13:03
-
1247 falls in the range of an `unsigned char`, not in the range of a `char`. – Damien Jul 05 '21 at 13:25
1 Answers
-2
unsigned char
range is 0 .. 255
char
range is -128 to 127

ChrisBD
- 9,104
- 3
- 22
- 35
-
1That's not right. `signed char` range is -128 to 127; `char` range is implementation-defined. (Strictly speaking, even that's not right: the implementation is free to use larger ranges. But nobody does.) – TonyK Jul 05 '21 at 17:23
-
We're looking at Microsoft C++ compiler in Visual Studio here, in which case it uses a single octet (byte) for a char and it is signed by default unless the compiler switch `/J` is used. – ChrisBD Jul 06 '21 at 06:31
-
-