Signed / Unsigned Conversions

N

Nephi Immortal

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top