Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Typecasting

Reply
Thread Tools

Typecasting

 
 
mail.dsp@gmail.com
Guest
Posts: n/a
 
      11-20-2008
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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      11-20-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      11-20-2008
On Nov 20, 7:58 am, (E-Mail Removed) 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:(E-Mail Removed)
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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-20-2008
(E-Mail Removed) 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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-20-2008
Andrey Tarasevich wrote:
> (E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList typecasting from binary SQL data =?Utf-8?B?Smlt?= ASP .Net 1 04-11-2005 04:49 PM
Advanced pointer typecasting Robert Street C++ 3 02-21-2004 01:33 AM
Typecasting char pointer to structure venkatesh C++ 1 12-06-2003 06:46 AM
Typecasting operator on simple types vs. classes Nicolay Korslund C++ 7 09-30-2003 04:33 PM
Understanding Typecasting in C++ Kapil Khosla C++ 3 07-20-2003 11:40 AM



Advertisments