C is an old-school language. Don't say things like "I did not expect this to compile because...". In general, most C compilers try as hard as they can to compile something for you, even if it's dangerous. Don't say things like "Shouldn't any value outside the range be rejected by the compiler?". Most C compilers don't reject things just because they're a bad idea -- or even because they might not work.
Most modern compilers will, however, warn you about bad ideas, but even then, you may have to explicitly request such warnings, because in their default mode, the compilers may still be operating under the old-school assumption that the programmer knows what he is doing.
In C, overflow of unsigned
types has guaranteed behavior -- arithmetic is always performed, in effect, modulo the size of the type. Although it's not guaranteed, most processors and most compilers implement the same sort of thing for overflow on signed types as well -- and there's probably a fair amount of code that depends on this. So that's another reason that compiler warnings about signed integer overflow aren't necessarily prominent.
With that said, though, I tried your code under two different compilers. One said "warning: overflow in implicit constant conversion", and the other said "warning: implicit conversion from 'int' to 'signed char' changes value from 32767 to -1".
If your compiler is not giving you warnings like these, you will either want to (a) make sure all warnings are enabled (either with a checkbox in a configuration settings dialog somewhere, or with a command-line option flag like -Wall
), or (b) get a better compiler.