It seems that your compiler by default considers type char
like type signed char
. In this case CHAR_MIN
is equal to SCHAR_MIN
and in turn equal to -128
while CHAR_MAX
is equal to SCHAR_MAX
and in turn equal to 127
(See header <limits.h>
)
According to the C Standard (6.2.5 Types)
15 The three types char, signed char, and unsigned char are
collectively called the character types. The implementation shall
define char to have the same range, representation, and behavior as
either signed char or unsigned char
For signed types one bit is used as the sign bit. So for the type signed char
the maximum value corresponds to the following representation in the hexadecimal notation
0x7F
and equal to 127. The most significant bit is the signed bit and is equal to 0.
For negative values the signed bit is set to 1 and for example -128
is represented like
0x80
When in your program the value stored in char reaches its positive maximum 0x7F
and was increased it becomes equal to 0x80
that in the decimal notation is equal to -128
.
You should explicitly use type unsigned char
instead of the char
if you want that the result of the program execution did not depend on the compiler settings.
Or in the printf statement you could explicitly cast type char
to type unsigned char
. For example
printf("%d\n", ( unsigned char )myArray[i]);
Or to compare results you could write in the loop
printf("%d %d\n", myArray[i], ( unsigned char )myArray[i]);