Discussion in 'C Programming' started by spibou@gmail.com, Aug 3, 2006.

1. ### Guest

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
, Aug 3, 2006

2. ### Tom St DenisGuest

wrote:
> 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
Tom St Denis, Aug 3, 2006

3. ### Ben PfaffGuest

"Tom St Denis" <> writes:

> wrote:
>> 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;

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.
--
"The lusers I know are so clueless, that if they were dipped in clue
musk and dropped in the middle of pack of horny clues, on clue prom
night during clue happy hour, they still couldn't get a clue."
--Michael Girdwood, in the monastery
Ben Pfaff, Aug 3, 2006
4. ### Ben PfaffGuest

writes:

> 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.
--
"Give me a couple of years and a large research grant,
and I'll give you a receipt." --Richard Heathfield
Ben Pfaff, Aug 3, 2006
5. ### Guest

wrote:

> 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
, Aug 4, 2006
6. ### Ben PfaffGuest

writes:

> wrote:
>
>> 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 ?

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.
--
A competent C programmer knows how to write C programs correctly,
a C expert knows enough to argue with Dan Pop, and a C expert
expert knows not to bother.
Ben Pfaff, Aug 4, 2006
7. ### Keith ThompsonGuest

writes:
> wrote:
>> 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

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.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson, Aug 4, 2006