Signed / Unsigned Conversions

Discussion in 'C++' started by Nephi Immortal, Nov 9, 2011.

  1. I read C++ Primer 4th edition book. I look the topic: 5.12.2. The
    Arithmetic Conversions

    I create three variables to do arithmetic. Some variables have mixed
    signed type and unsigned type.

    signed char a = 1;
    signed char b = 2;
    signed char c = a + b;

    All variables are signed type. No cast conversion is needed.

    unsigned char a = 1;
    signed char b = 2;
    signed char c = a + b;

    The variable a is unsigned type and the variable b is signed type.
    Is b converted from signed type to unsigned type before it is added to
    a? The variable a is converted from unsigned type to signed type and
    store it into variable c. Correct?

    signed char a = 1;
    signed char b = 2;
    unsigned char c = a + b;

    Both variable a and b do not need casting conversion because they are
    signed type. After addition is done, a is converted from signed type
    to unsigned type before stores into variable c.
    Nephi Immortal, Nov 9, 2011
    #1
    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. Anupam

    int to unsigned int conversions

    Anupam, Dec 3, 2003, in forum: C Programming
    Replies:
    4
    Views:
    363
    Christian Bau
    Dec 3, 2003
  2. Replies:
    10
    Views:
    699
    Jasen Betts
    Aug 5, 2005
  3. Charles Sullivan

    Signed <-> Unsigned conversions.

    Charles Sullivan, Aug 26, 2010, in forum: C Programming
    Replies:
    17
    Views:
    642
    BartC
    Aug 27, 2010
  4. Navaneeth
    Replies:
    8
    Views:
    677
    Keith Thompson
    Jan 1, 2011
  5. pozz
    Replies:
    12
    Views:
    726
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page