J
jeff
unsigned long a = ...;
long b = (long)a;
while a overflows, what is the result of b?
Thank you.
long b = (long)a;
while a overflows, what is the result of b?
Thank you.
unsigned long a = ...;
long b = (long)a;
while a overflows, what is the result of b?
Thank you.
unsigned long a = ...;
long b = (long)a;
while a overflows, what is the result of b?
jeff said: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.)
unsigned long a = ...;
long b = (long)a;
while a overflows, what is the result of b?
Thank you.
undefined.
jeff said:
a can't overflow. If the value of a exceeds LONG_MAX, the value of b is
undefined.
Under
C89/90, the result was always an implementation-defined value, and
never undefined behavior.
Jack Klein said:'a' is an unsigned long, and unsigned types can't overflow. I suppose
the question you are really asking is, what happens if 'a' contains a
value larger than LONG_MAX.
In that case, either 'b' receives an implementation-defined value or
an implementation-defined signal is raised.
Do you know if it is implementation-defined which one will happen? So an
implementation has to specify that either there will _always_ be an
implementation defined value or _always_ an implementation-defined
signal?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.