4 ms·
That's undefined behavior, the compiler can do whatever it pleases to. It can even print 666, and be done.
by malkia 14y ago
That's undefined behavior, the compiler can do whatever it pleases to. It can even print 666, and be done.
- AceJohnny2 14y agoNo it's not. I had the same thought, but the author carefully points out from the standard: "The usual arithmetic conversions ensure that two operands to a “+” operator of type signed char are both promoted to signed int before the addition is performed." He does assume sizeof(int) > sizeof(char), which is true on all platforms he has tried. It would be undefined on an AVR or other microcontroller where sizeof(int) == sizeof(char) though.
- malkia 14y agoThanks! Should read the article more carefully.
- nova 14y agoundefined on an AVR or other microcontroller where sizeof(int) == sizeof(char) though. Just noting that ints are 16 bits wide in AVR-GCC, unless you use the -mint8 option which violates C standards.
- JoachimSchipper 14y agoAbsolutely true. Note, though, that sizeof(int) == sizeof(char) is allowed if you have 16-bit chars.
- cperciva 14y ago16-bit chars aren't allowed in modern versions of C -- CHAR_BIT is required to be 8.
- JoachimSchipper 14y agoThanks for the update! (For the curious: C99 or POSIX both require CHAR_BIT == 8.)
- pascal_cuoq 14y agoC99 only requires CHAR_BIT to be at least 8 (5.2.4.2.1:1). POSIX requires it to be exactly 8.
- cperciva 14y agoOops, quite right. I missed the "equal or greater in magnitude" line when I was reading that section.