[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Typecasting

mail.dsp

11/20/2008 6:58:00 AM

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
4 Answers

Rolf Magnus

11/20/2008 10:34:00 AM

0

mail.dsp@gmail.com 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.

James Kanze

11/20/2008 10:38:00 AM

0

On Nov 20, 7:58 am, mail....@gmail.com 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:james.kanze@gmail.com
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

Andrey Tarasevich

11/20/2008 6:23:00 PM

0

mail.dsp@gmail.com 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

11/20/2008 6:26:00 PM

0

Andrey Tarasevich wrote:
> mail.dsp@gmail.com 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