Typecasting

Discussion in 'C++' started by mail.dsp@gmail.com, Nov 20, 2008.

  1. Guest

    Consider following:
    unsigned char i= 0;

    Now when we perform "++i" Is "i" typecasted into "int", "++" operator
    increment its value and finally typecasted into "unsigned char"?


    Thanks in advance
    --
    Daya
    , Nov 20, 2008
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > Consider following:
    > unsigned char i= 0;
    >
    > Now when we perform "++i" Is "i" typecasted into "int", "++" operator
    > increment its value and finally typecasted into "unsigned char"?


    You mean "converted", not "typecasted". But yes, for all integer arithmetic
    operations where all operands are small enough to fit in int, those operands
    are converted to int first.
    Rolf Magnus, Nov 20, 2008
    #2
    1. Advertising

  3. James Kanze Guest

    On Nov 20, 7:58 am, wrote:
    > Consider following:
    > unsigned char i= 0;


    > Now when we perform "++i" Is "i" typecasted into "int", "++"
    > operator increment its value and finally typecasted into
    > "unsigned char"?


    It depends, sort of. First of all, there's no "typecasting"
    involved; typecasting is an explicit conversion, and any
    conversions here are implicit. But type promotion will occur;
    if an int can represent all possible values of unsigned char,
    the unsigned char will be promoted to int; otherwise, it will be
    promoted to unsigned int. The arithmetic will be performed on
    the promoted type, and the results will be converted to the
    target type.

    For such simple operations, it doesn't matter, but in more
    complicated cases, it could.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Nov 20, 2008
    #3
  4. wrote:
    > Consider following:
    > unsigned char i= 0;
    >
    > Now when we perform "++i" Is "i" typecasted into "int", "++" operator
    > increment its value and finally typecasted into "unsigned char"?


    Close, but not exactly. '++i' is, by definition, a shorthand for 'i +=
    1', which in turn is a shorthand for 'i = i + 1' (aside from some
    details irrelevant in this case). The latter expression is evaluated by
    promoting 'i' to 'int', calculating 'i + 1' and then converting the
    result back to 'unsigned char'.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 20, 2008
    #4
  5. Andrey Tarasevich wrote:
    > wrote:
    >> Consider following:
    >> unsigned char i= 0;
    >>
    >> Now when we perform "++i" Is "i" typecasted into "int", "++" operator
    >> increment its value and finally typecasted into "unsigned char"?

    >
    > Close, but not exactly. '++i' is, by definition, a shorthand for 'i +=
    > 1', which in turn is a shorthand for 'i = i + 1' (aside from some
    > details irrelevant in this case). The latter expression is evaluated by
    > promoting 'i' to 'int', calculating 'i + 1' and then converting the
    > result back to 'unsigned char'.


    Also, on a relatively exotic system with the range of 'unsigned char'
    exceeding the positive range of 'int', 'i' will get promoted to
    'unsigned int' instead of 'int'

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 20, 2008
    #5
    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. =?Utf-8?B?Smlt?=

    ArrayList typecasting from binary SQL data

    =?Utf-8?B?Smlt?=, Apr 11, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    507
    Bruce Barker
    Apr 11, 2005
  2. Kapil Khosla

    Understanding Typecasting in C++

    Kapil Khosla, Jul 19, 2003, in forum: C++
    Replies:
    3
    Views:
    7,340
    John Harrison
    Jul 20, 2003
  3. Nicolay Korslund
    Replies:
    7
    Views:
    458
    Nicolay Korslund
    Sep 30, 2003
  4. venkatesh
    Replies:
    1
    Views:
    8,123
    lallous
    Dec 6, 2003
  5. Robert Street

    Advanced pointer typecasting

    Robert Street, Feb 20, 2004, in forum: C++
    Replies:
    3
    Views:
    6,938
    Robert Street
    Feb 21, 2004
Loading...

Share This Page