Connecting Tech Pros Worldwide Forums | Help | Site Map

Typecasting

mail.dsp@gmail.com
Guest
 
Posts: n/a
#1: Nov 20 '08
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

Rolf Magnus
Guest
 
Posts: n/a
#2: Nov 20 '08

re: Typecasting


mail.dsp@gmail.com wrote:
Quote:
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
Guest
 
Posts: n/a
#3: Nov 20 '08

re: Typecasting


On Nov 20, 7:58 am, mail....@gmail.com wrote:
Quote:
Consider following:
unsigned char i= 0;
Quote:
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
Guest
 
Posts: n/a
#4: Nov 20 '08

re: Typecasting


mail.dsp@gmail.com wrote:
Quote:
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
Guest
 
Posts: n/a
#5: Nov 20 '08

re: Typecasting


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