Question about cast

S

spibou

In page 81 of N1124 in footnote 87 we read:

If the value of the expression is represented
with greater precision or range than required
by the type named by the cast (6.3.1.8), then
the cast specifies a conversion even if the type
of the expression is the same as the named type.

Can someone give me an example of what this means ?

Otherwise the relevant section hardly says anything about
the semantics of cast. Assume for example that on some
machine unsigned int is 32 bits and unsigned char is 8
bits. If i is unsigned int what is the value of
(unsigned char) i ? Is it the same as 255 & i but of type
unsigned char as opposed to unsigned int ?

Spiros Bousbouras
 
T

Tom St Denis

In page 81 of N1124 in footnote 87 we read:

If the value of the expression is represented
with greater precision or range than required
by the type named by the cast (6.3.1.8), then
the cast specifies a conversion even if the type
of the expression is the same as the named type.

Can someone give me an example of what this means ?

Simple,

char c = (char)127 * 2;

Tom
 
B

Ben Pfaff

Tom St Denis said:
Simple,

char c = (char)127 * 2;

No. The footnote is attached (in the final standard, in which it
is footnote 86) to the following sentence:

A cast that specifies no conversion has no effect on the
type or value of an expression.86)

The cast in your example specifies a conversion from "int" to
"char", so the footnote does not apply.
 
B

Ben Pfaff

In page 81 of N1124 in footnote 87 we read:

If the value of the expression is represented
with greater precision or range than required
by the type named by the cast (6.3.1.8), then
the cast specifies a conversion even if the type
of the expression is the same as the named type.

Can someone give me an example of what this means ?

The standard example is a machine in which floating-point values
are represented in registers with greater precision than when
those values are stored in memory. On the 80x86, the x87
floating-point registers are 80 bits wide, but usually a "double"
is only 64 bits wide. A cast from double to double must discard
the "extra" bits of precision on such a machine.
 
S

spibou

In page 81 of N1124 in footnote 87 we read:

If the value of the expression is represented
with greater precision or range than required
by the type named by the cast (6.3.1.8), then
the cast specifies a conversion even if the type
of the expression is the same as the named type.

Can someone give me an example of what this means ?

Otherwise the relevant section hardly says anything about
the semantics of cast. Assume for example that on some
machine unsigned int is 32 bits and unsigned char is 8
bits. If i is unsigned int what is the value of
(unsigned char) i ? Is it the same as 255 & i but of type
unsigned char as opposed to unsigned int ?

Noone knows about the semantics of cast then ?? Or
people simply missed the second question ?

If noone here knows do you think it is worthwhile to
ask on comp.std.c ?
 
B

Ben Pfaff

Noone knows about the semantics of cast then ?? Or
people simply missed the second question ?

It seemed like a bit of a silly question. It implies that you
didn't bother to read the standard's extensive section on the
semantics of conversions.
 
K

Keith Thompson

Noone knows about the semantics of cast then ?? Or
people simply missed the second question ?

If noone here knows do you think it is worthwhile to
ask on comp.std.c ?

A cast is simply an operator that specifies a conversion. See section
6.3 of n1124 (or of the C99 standard), particularly 6.3.1.3p2:

Otherwise, if the new type is unsigned, the value is converted by
repeatedly adding or subtracting one more than the maximum value
that can be represented in the new type until the value is in the
range of the new type.

If you have any questions after reading section 6.3, feel free to ask
them.

No, it's not worthwhile to ask in comp.std.c, unless you think there's
a flaw in the standard document itself. Discussion of the standard
document belongs in comp.std.c; discussion of the language it defines
belongs in comp.lang.c.
 

Ask a Question

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.

Ask a Question

Similar Threads

cast question 4
cast musings 19
cast 20
_Bool bitfields and cast / assignment 5
Help with EXT3 Filesystem work 1
void * and explicit cast 22
Question regarding cast 13
A question about program logic... program provided 20

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top