Question about cast

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
    #1
    1. Advertising

  2. Tom St Denis 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 ?


    Simple,

    char c = (char)127 * 2;

    Tom
    Tom St Denis, Aug 3, 2006
    #2
    1. Advertising

  3. Ben Pfaff Guest

    "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
    #3
  4. Ben Pfaff Guest

    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
    #4
  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
    ask on comp.std.c ?
    , Aug 4, 2006
    #5
  6. Ben Pfaff Guest

    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
    #6
  7. 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
    > 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.

    --
    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
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Question: Invalid Cast Exception Error

    VB Programmer, Oct 28, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    1,343
    VB Programmer
    Oct 28, 2003
  2. Christopher Benson-Manica

    Quick cast style question

    Christopher Benson-Manica, Apr 30, 2004, in forum: C++
    Replies:
    15
    Views:
    523
    Jake Montgomery
    May 4, 2004
  3. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,055
    Dan Pop
    Feb 10, 2004
  4. EvilRix
    Replies:
    8
    Views:
    615
    Martin Dickopp
    Feb 14, 2004
  5. Pavel
    Replies:
    7
    Views:
    508
    Pavel
    Sep 19, 2010
Loading...

Share This Page