6

I have the following code and it seems to me that it should always enter the true part of the if statement but, beyond 120, it appears to start executing the else clause. Why is that happening?

char x;
for (i=0;i<256;i+=10) {
    x=i;
    if (x==i)
        printf("%d true\n",i);
    else 
        printf("%d false\n",i);
}

The output is:

0 true
10 true
: all numbers from 20 to 100 are true
110 true
120 true
130 false
140 false
: all numbers from 150 to 230 are false
240 false
250 false
4
  • what!? I have no idea what you're asking because I can't read it.
    – wheaties
    Apr 27, 2010 at 14:43
  • 3
    Both Mark and I managed to easily identify what was being asked. The question is just worded poorly, it is none of the things you claim as reasons for closing. Apr 27, 2010 at 14:48
  • 2
    I think the wording can be improved quite easily, but it can't be edited it while it is closed so I'm voting for reopen.
    – Mark Byers
    Apr 27, 2010 at 14:51
  • 1
    Same here - after formatting the code, I think the question is quite obvious Apr 27, 2010 at 14:53

2 Answers 2

13

It's because you are using a signed char and presumably an integer.

The char is overflowing when it reaches 130 (it becomes 130 - 256) but the integer does not overflow. 130 - 256 != 130.

A simple change fixes it so that the result is always true - just add unsigned on the first line:

unsigned char x;
for (int i=0;i<256;i+=10)
{
    x=i;
    if (x==i)
        printf("%d true\n",i);
    else 
        printf("%d false\n",i);
}
1
  • 3
    And if the example code did not take steps of size 10 then it would have been much more obvious what was going on because it would have happened at the 127/128 boundary.
    – nategoose
    Apr 27, 2010 at 18:26
12

Your compiler probably uses a signed char by default when you use the char keyword. The range of a signed char is usually -128 to 127, whereas the range of an int is far greater. More esoteric hardware beyond the typical desktop PC may use other values.

5
  • That's almost certainly correct for this specific case but I'd like to see it made clear that is is for this specific case. Nothing in the C standard mandates an 8-bit char, nor does it guarantee that an integer is bigger than a char. In addition, I think you meant "range of a signed char is -128 to ...".
    – paxdiablo
    Apr 27, 2010 at 15:14
  • 2
    Actually, to avoid any ambiguity, C99 mandates that a char must be at least 8 bit (5.2.4.2.1), and it does guarantee that an int can contain all values from -32767 to +32767. The statement "the range of an int is far greater than -128..127" is technically true. Apr 27, 2010 at 15:31
  • @paxdiablo Thanks, have edited to what I actually meant to say. Yes it's quite easy to assume 8-bit chars. I did, but was grimly aware it may not always be so. I decided these complications were unnecessary at this stage of the op's skill level - and I was going for the quick ++rep fix ;-) Apr 27, 2010 at 15:32
  • Not to mention that the observed behavior strongly suggests 8-bit chars, signed by default (or is that a requirement in C99?). Apr 27, 2010 at 15:50
  • @David char is still either signed or unsigned by default as of C99. Apr 27, 2010 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy