Fog Creek Software
Discussion Board




Is this a language feature?

When I coerce an illegal value from an unsigned char into a signed char on a Visual C++ compiler, it girdles around the range.

For eg.

#include <stdio.h>

int main(void)
{

    signed char c;
    unsigned char d;

    d=128;
    c=d;
    printf("%d, %d", c,d);

}

Is this a language design/feature? Do some compilers also give you an overflow?

Sathyaish Chakravarthy
Friday, April 16, 2004

In general, the C (and C++) language does not check for integer overflow in the name of efficiency. This also happens with shorts, ints, long, etc.

So I guess you'd call it a language "feature", but many people would argue about the "feature" part!

And, of course, the actual point where it wraps varies depending on the size of the type involved. The definitions in the standard do not mandate a size, only a minimum. So char must be at least 8 bits, but could be more. Short must be at least 16 bits, but could be more. etc. I once worked on a DSP system where ALL types were 32 bit.

Chris Tavares
Saturday, April 17, 2004

>...but many people would argue about the "feature" part!

For want of a better substitute, I meant a design issue. And yes, you say.


>And, of course, the actual point where it wraps varies depending on the size of the type involved.

Thanks very much, Chris.

Sathyaish Chakravarthy
Saturday, April 17, 2004

*  Recent Topics

*  Fog Creek Home