Re: cast unsigned long to long



On 2005-12-27, Keith Thompson <kst-u@xxxxxxx> wrote:
> "jeff" <xiayu02@xxxxxxxxx> writes:
>> unsigned long a = ...;
>> long b = (long)a;
>>
>> while a overflows, what is the result of b?
>
> The cast is unnecessary; the declaration
> long b = a;
> is equivalent, since there's an implicit conversion. (Most casts are
> unnecessary.)
>
> If a conversion to a signed integer type overflows, "either the result
> is implementation-defined or an implementation-defined signal is
> raised" (C99 6.3.1.3p3). I think the permission to raise a signal is
> new in C99; in C90, you just get an implementation-defined result.
> (No, it's not undefined.)

That's not considered "integer overflow"?

An example of undefined behavior is the behavior on integer overflow.

my mistake, it's not

>
> "Implementation-defined" means that your implementation is required to
> document it, but you shouldn't depend on this since it's likely to
> vary from one implementation to another, making your code
> non-portable.
>
> Note that this is different from what happens on artithmetic overflow,
> which invokes undefined behavior for signed types.
>
.



Relevant Pages

  • Re: Factorial
    ... You could cast a Variant variable as a Decimal type ... Variant and CDec a number into it to make it the ... they will produce an overflow error. ... if a Variant variable is assigned a value that was cast ...
    (microsoft.public.vb.general.discussion)
  • Re: Cannot compile with _FILE_OFFSET_BITS = 64
    ... No automatic conversion to 64 bit type is done by compiler. ... the second one might overflow, ... to use a cast to off_t rather than the "LL" suffix. ... Or you could apply the cast to all four constants, ...
    (comp.lang.c)
  • Re: How to cast int to short
    ... Jon Skeet wrote: ... Note that, by default, C# checks for integer overflow when ... performing a cast in this obvious way. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Casting in a where clause
    ... you can't cast -999 as decimal; you get an overflow. ... you need to be confident that the user will never be looking for -999, ... "The answer" is to store numeric data in a numeric datatype. ...
    (microsoft.public.sqlserver.programming)
  • RUS-CERT Advisory 2002-08:01: Incorrect integer overflow detection in C code
    ... Incorrect integer overflow detection in C code ... undefined behavior according to the C standard. ... For unsigned integer types, the C standard later on defines ... However, compilers will perform ...
    (Bugtraq)