Reputation: 2341
#include <stdio.h>
int main(void)
{
char test = 0x80;
printf("%c\n", test); /* To remove the "unused" warning */
return 0;
}
I understand that a character is guaranteed to be one byte. 0x80 is also one byte. So why then do I get the following error/warning?
error: overflow in implicit constant conversion [-Werror=overflow]
In my case it's an error because I'm treating warnings as errors.
0x80 is the minimum value for which this warning/error appears. If I change it to 0x7F, this compiles fine. I used ideone.com with the 'C99 strict' option to compile the code. It reported using gcc-4.7.2.
Upvotes: 3
Views: 744
Reputation: 21
The variable test is defined as char which defaults to signed char. range of which is 0 ~ 127 (7 bits). The last bit is reserved for sign. Maybe the error is due to trying to print a character with negative value(0x80 = -128).
Try with unsigned char for test and check if you get same error.
Upvotes: 2
Reputation: 437336
The char
type is signed in your compiler, so even though it does have 8 bits of information it cannot store values greater than 127 (0x7f).
The header <limits.h>
defines macros that let you determine the signedness and range limits for integral types, including char
.
Upvotes: 13