Connecting Tech Pros Worldwide Help | Site Map

Typecasting

  #1  
Old November 20th, 2008, 07:05 AM
mail.dsp@gmail.com
Guest
 
Posts: n/a
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
  #2  
Old November 20th, 2008, 10:35 AM
Rolf Magnus
Guest
 
Posts: n/a

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.

  #3  
Old November 20th, 2008, 10:45 AM
James Kanze
Guest
 
Posts: n/a

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
  #4  
Old November 20th, 2008, 06:25 PM
Andrey Tarasevich
Guest
 
Posts: n/a

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
  #5  
Old November 20th, 2008, 06:35 PM
Andrey Tarasevich
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Typecasting Pointers on a 64 bit System bwaichu@yahoo.com answers 12 August 8th, 2006 11:05 PM
single-letter typecasting macros - where defined ? jdm answers 3 November 16th, 2005 09:24 PM
Does typecasting consume cpu cycles Vinod Patel answers 11 November 14th, 2005 12:58 PM
Question regarding pointer typecasting and manipulation Arun Prasath answers 2 November 13th, 2005 11:20 PM
Understanding Typecasting in C++ Kapil Khosla answers 3 July 19th, 2005 04:35 PM