This is because int
is always signed.
char
doesn't have to.
char
can be signed or unsigned, depending on implementation. (there are three distinct types - char
, signed char
, unsigned char
)
But what's the problem? I can just use values from 0 to 127. Can this hurt me silently?
Oh, yes it can.
//depending on signedess of char, this will
//either be correct loop,
//or loop infinitely and write all over the memory
char an_array[50+1];
for(char i = 50; i >= 0; i--)
{
an_array[i] = i;
// if char is unsigned, the i variable can be never < 0
// and this will loop infinitely
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…