<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: 4.4 &#8212; Type conversion and casting</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/</link>
	<description></description>
	<pubDate>Wed, 20 Aug 2008 08:36:24 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Shankar</title>
		<link>http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-23136</link>
		<dc:creator>Shankar</dc:creator>
		<pubDate>Fri, 08 Aug 2008 10:10:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-23136</guid>
		<description>I find that if nValue1 and nValue2 are char and they are set to the max value (127), then assigning nValue1 + nValue2 to fValue results in the correct value (254) instead of -2. Similarly also for short int, but not for int. With int, unless at least one is type cast to float, the answer is -2. This behavior I find with Visual Studio 2005.</description>
		<content:encoded><![CDATA[<p>I find that if nValue1 and nValue2 are char and they are set to the max value (127), then assigning nValue1 + nValue2 to fValue results in the correct value (254) instead of -2. Similarly also for short int, but not for int. With int, unless at least one is type cast to float, the answer is -2. This behavior I find with Visual Studio 2005.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Learn C++ - &#187; 4.3 &#8212; File scope and the static keyword</title>
		<link>http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-13459</link>
		<dc:creator>Learn C++ - &#187; 4.3 &#8212; File scope and the static keyword</dc:creator>
		<pubDate>Tue, 29 Apr 2008 04:42:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-13459</guid>
		<description>[...] 2007      Prev/Next Posts   &#171; 4.2 &#8212; Global variables &#124; Home &#124; 4.4 &#8212; Type conversion and casting &#187;     Tuesday, June 19th, 2007 at 5:46 [...]</description>
		<content:encoded><![CDATA[<p>[...] 2007      Prev/Next Posts   &laquo; 4.2 &#8212; Global variables | Home | 4.4 &#8212; Type conversion and casting &raquo;     Tuesday, June 19th, 2007 at 5:46 [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-9131</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Sat, 08 Mar 2008 20:46:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-9131</guid>
		<description>Modulus is an integer operation and can not be used with doubles.</description>
		<content:encoded><![CDATA[<p>Modulus is an integer operation and can not be used with doubles.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: faraz</title>
		<link>http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-9108</link>
		<dc:creator>faraz</dc:creator>
		<pubDate>Sat, 08 Mar 2008 06:44:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-9108</guid>
		<description>i have a problem i want to do get input from user then i want to do the multiplication,division,subtraction,addititon,and modulus by using type double.How i will do modulus with double variable any one can provide me the code</description>
		<content:encoded><![CDATA[<p>i have a problem i want to do get input from user then i want to do the multiplication,division,subtraction,addititon,and modulus by using type double.How i will do modulus with double variable any one can provide me the code</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chad Bernier</title>
		<link>http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-8798</link>
		<dc:creator>Chad Bernier</dc:creator>
		<pubDate>Mon, 03 Mar 2008 02:26:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-8798</guid>
		<description>so in his case, two wrongs made a right?</description>
		<content:encoded><![CDATA[<p>so in his case, two wrongs made a right?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-6833</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Tue, 29 Jan 2008 21:55:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-6833</guid>
		<description>The issue is in how you've written your program.  This is a tricky one.

For example, if you do this:
&lt;pre&gt;
cout &lt;&lt; 5 - 10u;
&lt;/pre&gt;

You get 4294967291.

If you do this:
&lt;pre&gt;
int x= 5u - 10;
cout &lt;&lt; x;
&lt;/pre&gt;

You get -5.

So what's the difference?  The answer has to do with the way types are promoted.  When the compiler encounters 5 - 10u, it promotes 5 to an unsigned value, and 5u - 10u produces the result 4294967291 (unsigned).  However, it's worth noting that 4294967291u and -5 have the exact same bit pattern -- the interpretation depends entirely on whether the value is treated as signed or unsigned.

Because 5 - 10u produces an unsigned value, cout treats it as an unsigned value, and prints 4294967291 as the result.

However, in your case, you've assigned this unsigned value back to a signed integer.  Because x is signed, when you print x in the next statement, cout prints the value as if it were signed, which is why it prints -5.

So the ultimate answer is that the statement as written is true.  You've simply cast 4294967291 unsigned back to a signed integer and printed that value, which is the value you were intuitively expecting anyway. :)</description>
		<content:encoded><![CDATA[<p>The issue is in how you&#8217;ve written your program.  This is a tricky one.</p>
<p>For example, if you do this:</p>
<pre>
cout < < 5 - 10u;
</pre>
<p>You get 4294967291.</p>
<p>If you do this:
</pre>
<pre>
int x= 5u - 10;
cout < < x;
</pre>
<p>You get -5.</p>
<p>So what&#8217;s the difference?  The answer has to do with the way types are promoted.  When the compiler encounters 5 - 10u, it promotes 5 to an unsigned value, and 5u - 10u produces the result 4294967291 (unsigned).  However, it&#8217;s worth noting that 4294967291u and -5 have the exact same bit pattern &#8212; the interpretation depends entirely on whether the value is treated as signed or unsigned.</p>
<p>Because 5 - 10u produces an unsigned value, cout treats it as an unsigned value, and prints 4294967291 as the result.</p>
<p>However, in your case, you&#8217;ve assigned this unsigned value back to a signed integer.  Because x is signed, when you print x in the next statement, cout prints the value as if it were signed, which is why it prints -5.</p>
<p>So the ultimate answer is that the statement as written is true.  You&#8217;ve simply cast 4294967291 unsigned back to a signed integer and printed that value, which is the value you were intuitively expecting anyway. :)</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Allen01</title>
		<link>http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-6830</link>
		<dc:creator>Allen01</dc:creator>
		<pubDate>Tue, 29 Jan 2008 21:05:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-6830</guid>
		<description>When I build and compile the code below, the output is -5. You mention that:

 "This hierarchy can cause some interesting issues. For example, you might expect the expression 5u - 10 to evalute to -5 (5u means 5 as an unsigned integer). But in this case, the signed integer (10) is promoted to an unsigned integer, and the result of this expression is the unsigned integer 4294967291!"

Is the hierarchy issue compiler specific? I am using VC   2005 Express.

&lt;PRE&gt;
#include "stdafx.h"
#include &lt;iostream&gt;

int main()
{
using namespace std;
int x;
x = 5u - 10;
cout &lt;&lt; x;
return 0;
}
&lt;/PRE&gt;</description>
		<content:encoded><![CDATA[<p>When I build and compile the code below, the output is -5. You mention that:</p>
<p> &#8220;This hierarchy can cause some interesting issues. For example, you might expect the expression 5u - 10 to evalute to -5 (5u means 5 as an unsigned integer). But in this case, the signed integer (10) is promoted to an unsigned integer, and the result of this expression is the unsigned integer 4294967291!&#8221;</p>
<p>Is the hierarchy issue compiler specific? I am using VC   2005 Express.</p>
<pre>
#include &#8220;stdafx.h&#8221;
#include <iostream>

int main()
{
using namespace std;
int x;
x = 5u - 10;
cout < < x;
return 0;
}
</pre></iostream></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhishek</title>
		<link>http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-5730</link>
		<dc:creator>Abhishek</dc:creator>
		<pubDate>Wed, 09 Jan 2008 03:46:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-5730</guid>
		<description>Thanks...</description>
		<content:encoded><![CDATA[<p>Thanks&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-5713</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Tue, 08 Jan 2008 16:13:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-5713</guid>
		<description>The purpose of static_cast is to do normal conversion between types without runtime checks.  C++ provides several other types of casts, including dynamic_cast, reinterpret_cast, and const_cast, all of which have different behavior.

The problem with C-style casting is that it may use a static_cast, reinterpret_cast, or const_cast (or a combination thereof) depending on the situation, and we're not always sure which one(s) it's going to pick.  That's why using static_cast is safer -- we're always sure what it's going to do, and if we try to do something it wasn't meant for, it will give us a warning.

Here's an example of a C-style cast:

&lt;pre&gt;
const char *strMsg="Can't touch this!";
unsigned char *ptr=(unsigned char*)strMsg;
&lt;/pre&gt;

Not only is this casting a char to an unsigned char, it's also tossing away the const.  I think it's doing a reinterpret_cast and a const_cast combination.  In any case, you can see that this is dangerous.  Let's try using a C++ style static_cast instead:

&lt;pre&gt;
const char *strMsg="Can't touch this!";
unsigned char *ptr=static_cast&lt;unsigned char*&gt;(strMsg);
&lt;/pre&gt;

When we try to compile this, the compiler will complain:

&lt;pre&gt;
C:\Test.cpp(46) : error C2440: 'static_cast' : cannot convert from 'const char *' to 'unsigned char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
&lt;/pre&gt;

In other words, the C++ style casts give you more control over what type of casting you want to perform, and will warn you if you do something that violates the constraints on the cast.  On the other hand, C-style casts will try to convert whatever you throw at it, regardless of whether it really makes sense to be doing so.  This is dangerous and can lead to unexpected behavior.</description>
		<content:encoded><![CDATA[<p>The purpose of static_cast is to do normal conversion between types without runtime checks.  C++ provides several other types of casts, including dynamic_cast, reinterpret_cast, and const_cast, all of which have different behavior.</p>
<p>The problem with C-style casting is that it may use a static_cast, reinterpret_cast, or const_cast (or a combination thereof) depending on the situation, and we&#8217;re not always sure which one(s) it&#8217;s going to pick.  That&#8217;s why using static_cast is safer &#8212; we&#8217;re always sure what it&#8217;s going to do, and if we try to do something it wasn&#8217;t meant for, it will give us a warning.</p>
<p>Here&#8217;s an example of a C-style cast:</p>
<pre>
const char *strMsg="Can't touch this!";
unsigned char *ptr=(unsigned char*)strMsg;
</pre>
<p>Not only is this casting a char to an unsigned char, it&#8217;s also tossing away the const.  I think it&#8217;s doing a reinterpret_cast and a const_cast combination.  In any case, you can see that this is dangerous.  Let&#8217;s try using a C++ style static_cast instead:</p>
<pre>
const char *strMsg="Can't touch this!";
unsigned char *ptr=static_cast<unsigned char*>(strMsg);
</unsigned></pre>
<p>When we try to compile this, the compiler will complain:</p>
<pre>
C:\Test.cpp(46) : error C2440: 'static_cast' : cannot convert from 'const char *' to 'unsigned char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
</pre>
<p>In other words, the C++ style casts give you more control over what type of casting you want to perform, and will warn you if you do something that violates the constraints on the cast.  On the other hand, C-style casts will try to convert whatever you throw at it, regardless of whether it really makes sense to be doing so.  This is dangerous and can lead to unexpected behavior.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhishek</title>
		<link>http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-5706</link>
		<dc:creator>Abhishek</dc:creator>
		<pubDate>Tue, 08 Jan 2008 10:02:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/#comment-5706</guid>
		<description>&lt;b&gt;This paragraph(and anything after it)went over my head::&lt;/b&gt;

"The C style cast can be inherently misused, because it will let you do things that may not make sense, such as getting rid of a const or changing a data type without changing the underlying representation. C   introduces a new casting operator called static_cast. A static cast works similarly to the C style cast, except it will only do standard type conversions, which reduces the potential for inadvertant misuse"
================================================
I didn't get the difference b/w Cpp style cast,C style cast and static_cast............I think all of them do the same thing...only the syntax is different.????
================================================</description>
		<content:encoded><![CDATA[<p><b>This paragraph(and anything after it)went over my head::</b></p>
<p>&#8220;The C style cast can be inherently misused, because it will let you do things that may not make sense, such as getting rid of a const or changing a data type without changing the underlying representation. C   introduces a new casting operator called static_cast. A static cast works similarly to the C style cast, except it will only do standard type conversions, which reduces the potential for inadvertant misuse&#8221;<br />
================================================<br />
I didn&#8217;t get the difference b/w Cpp style cast,C style cast and static_cast&#8230;&#8230;&#8230;&#8230;I think all of them do the same thing&#8230;only the syntax is different.????<br />
================================================</p>
]]></content:encoded>
	</item>
</channel>
</rss>
