Integral promotions doubt

Discussion in 'C++' started by Peter, Mar 24, 2013.

  1. Peter

    Peter Guest

    As stated in 2003 standard:

    "An rvalue of type char, signed char, unsigned char, short int, or
    unsigned short
    int can be converted to an rvalue of type int if int can represent all
    the values of the source type; otherwise,
    the source rvalue can be converted to an rvalue of type unsigned
    int.".

    When does the final part of the above rule apply? Since int is at
    least as wide as all types listed above, I can't imagine a value which
    could be represented in one of those types, but not in int type. I
    haven't found an appropriate example in any book I've read so far. Can
    you please give me an example showing that the above rule is not
    redundant and there in fact are cases where conversion to int would be
    wrong and conversion to unsigned int must be applied instead?
    Peter, Mar 24, 2013
    #1
    1. Advertising

  2. Peter

    Stefan Ram Guest

    Peter <> writes:
    >When does the final part of the above rule apply? Since int is at
    >least as wide as all types listed above, I can't imagine a value which
    >could be represented in one of those types, but not in int type.


    When sizeof( int )= sizeof( unsigned char ), and CHAR_BITS=16
    after #include <limits>, ( unsigned char )40000ul might not be
    convertible to int.
    Stefan Ram, Mar 24, 2013
    #2
    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. Glen Able
    Replies:
    4
    Views:
    425
    Attila Feher
    Jan 28, 2004
  2. j0mbolar

    type promotions

    j0mbolar, Feb 29, 2004, in forum: C Programming
    Replies:
    5
    Views:
    441
    Keith Thompson
    Mar 1, 2004
  3. jimjim

    integer promotions

    jimjim, Sep 13, 2005, in forum: C Programming
    Replies:
    14
    Views:
    553
    Jack Klein
    Sep 15, 2005
  4. Question about integer promotions

    , Aug 3, 2006, in forum: C Programming
    Replies:
    4
    Views:
    312
    CBFalconer
    Aug 4, 2006
  5. Thurston Manson

    Bit-fields vs integral promotions

    Thurston Manson, Aug 23, 2007, in forum: C Programming
    Replies:
    15
    Views:
    530
    Jun Woong
    Aug 29, 2007
Loading...

Share This Page