0

I would like to know why this piece of code printf char c infinitely:

char c; for (c = 0; c< 256; c++) printf("Char c=%c\n", c); 

Thanks

8
  • 3
    Because a char value is always less than 256 (provided CHAR_BIT is 8 heh heh). Commented Nov 21, 2016 at 20:34
  • char holds values from 0-255 (or -128-127 when considering the sign bit) ,, so 255+1 wraps back around to 0,, so your condition c<256 is always true. Commented Nov 21, 2016 at 20:34
  • 1
    Change the %c to %d, and the answer should be clear. Commented Nov 21, 2016 at 20:35
  • @yano: Neither is true unless we know the target platform. Commented Nov 21, 2016 at 20:44
  • @Olaf True.. made the assumption based on his observation what I said is true for his target platform Commented Nov 21, 2016 at 20:47

2 Answers 2

3

Assuming CHAR_BIT is 8 (which it almost always is), a char object cannot represent a value of 256; signed char will max out at 127, while unsigned char will max out at 255.

Signed case

When c is 127 and you add 1, the value "overflows". Because one's-complement and sign-magnitude representations are still a thing on some oddball architectures, the exact result can vary. For two's-complement, it will "wrap around" to -128, whereas for one's-complement it wraps around to -127, and for sign-magnitude it becomes -0.

All of these values are less than 256, so the condition c < 256 is always true.

Because the result can vary based on the platform, the C language definition places no requirements on the compiler to handle signed integer overflow in any particular way - the behavior is left undefined, and any result is equally "correct" as far as the language is concerned. A reasonably smart compiler might be able to detect that the condition will always evaluate to true and issue a warning, and it would be free to do that as far as the language definition is concerned. Or not.

Unsigned case

Unlike the signed case, unsigned integer overflow is well-defined; if c is 255 and you add 1, then the result wraps around back to 0. Again, 0 is less than 256, so c < 256 will always be true.

Sign up to request clarification or add additional context in comments.

Comments

1

Because the char data type is only one byte long, and therefore can only hold the values 0-255.

So when c is 255 and you do c++, it becomes 0. Thus, c is always < 256 in the loop.

4 Comments

This assumes that a plain char is unsigned, which is almost never the case.
@David Strictly speaking, chat is exactly one byte and can hold CHAR_BIT bits, which is at least 8.
@user3386109: It is so on all ARM systems which are AAPCS compatible and many others. It also is true for many embedded MCUs, e.g. MSP430. Somming up all thoses systems, one can very well say most systems use unsigned char!
A byte is not guaranteed to be an octet. A 9 bit signed char would show the same behaviour. Also signed integer overflow invokes undefined behaviour, which can result in much worse behaviour.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.