<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Efficient C Tips #6 &#8211; Don&#8217;t use the ternary operator</title>
	<atom:link href="http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/</link>
	<description>Thoughts on embedded systems by Nigel Jones</description>
	<lastBuildDate>Mon, 06 Sep 2010 14:57:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Doug</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-1310</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Wed, 28 Jul 2010 00:20:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-1310</guid>
		<description>My personal favorite &quot;do not use unless absolutely necessary&quot; construct is modulo. Modulo looks simple and elegant when written, but try it out -- write a simple modulo construct, compile it and look at the assembly output. It&#039;s horrific. Think the problem through and simplify it instead. I haven&#039;t yet found a modulo solution I couldn&#039;t implement much more simply and completely by rethinking the problem, resulting in code that&#039;s as accurate, uses less memory, and runs quicker.</description>
		<content:encoded><![CDATA[<p>My personal favorite &#8220;do not use unless absolutely necessary&#8221; construct is modulo. Modulo looks simple and elegant when written, but try it out &#8212; write a simple modulo construct, compile it and look at the assembly output. It&#8217;s horrific. Think the problem through and simplify it instead. I haven&#8217;t yet found a modulo solution I couldn&#8217;t implement much more simply and completely by rethinking the problem, resulting in code that&#8217;s as accurate, uses less memory, and runs quicker.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Greg Nelson</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-132</link>
		<dc:creator>Greg Nelson</dc:creator>
		<pubDate>Wed, 24 Feb 2010 19:47:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-132</guid>
		<description>The printf() comment reminds me of something else... Nigel, since you said you are often interested in people&#039;s weird coding conventions, this one might fit the bill.  Want to guess why I write:char tmp1[10];char tmp2[10];char tmp3[10];char final[50];snprintf(tmp1, &quot;%7.1f&quot;, f1);snprintf(tmp2, &quot;%7.1f&quot;, f2);snprintf(tmp3, &quot;%7.1f&quot;, f3);snprintf(final, &quot;%s %s %s&quot;, tmp1, tmp2, tmp3);&lt;b&gt;Answer:&lt;/b&gt;Because one of the compilers/libraries (still haven&#039;t found the culprit) used for our code can&#039;t handle multiple double arguments to a varargs call!If I do it the conventional way with &quot;%7.1f %7.1f %7.1f&quot; instead, I either get garbage output, or the program hangs.My guess is this has something to do with poor floating point emulation and/or different ways of stacking doubles versus ints/ptrs/etc.</description>
		<content:encoded><![CDATA[<p>The printf() comment reminds me of something else&#8230; Nigel, since you said you are often interested in people&#39;s weird coding conventions, this one might fit the bill.  Want to guess why I write:char tmp1[10];char tmp2[10];char tmp3[10];char final[50];snprintf(tmp1, &quot;%7.1f&quot;, f1);snprintf(tmp2, &quot;%7.1f&quot;, f2);snprintf(tmp3, &quot;%7.1f&quot;, f3);snprintf(final, &quot;%s %s %s&quot;, tmp1, tmp2, tmp3);<b>Answer:</b>Because one of the compilers/libraries (still haven&#39;t found the culprit) used for our code can&#39;t handle multiple double arguments to a varargs call!If I do it the conventional way with &quot;%7.1f %7.1f %7.1f&quot; instead, I either get garbage output, or the program hangs.My guess is this has something to do with poor floating point emulation and/or different ways of stacking doubles versus ints/ptrs/etc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bandit</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-131</link>
		<dc:creator>bandit</dc:creator>
		<pubDate>Tue, 26 Jan 2010 01:50:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-131</guid>
		<description>I use them usually only for printf():printf( &quot;%s&quot;, (bletch ? &quot;foo&quot; : &quot;bar&quot;) );otherwise I use if() else for clarity. I normally dofoo = 0;if( bar )   foo = 1;because this handles the default case &amp;&amp; the exceptional case.</description>
		<content:encoded><![CDATA[<p>I use them usually only for printf():printf( &quot;%s&quot;, (bletch ? &quot;foo&quot; : &quot;bar&quot;) );otherwise I use if() else for clarity. I normally dofoo = 0;if( bar )   foo = 1;because this handles the default case &amp;&amp; the exceptional case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ashleigh</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-130</link>
		<dc:creator>ashleigh</dc:creator>
		<pubDate>Tue, 26 Jan 2010 01:16:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-130</guid>
		<description>I&#039;ve seen the same as Nigel - often the ternary operator is worse.I still use it where it improves readability of the source and I don&#039;t  care about performance.I guess the lesson is - think about what you write. Write with maintenance, readability AND performance in mind. Know what the compiler does (ie go in with your eyes open), and then write whats best for the situation you are handling.</description>
		<content:encoded><![CDATA[<p>I&#39;ve seen the same as Nigel &#8211; often the ternary operator is worse.I still use it where it improves readability of the source and I don&#39;t  care about performance.I guess the lesson is &#8211; think about what you write. Write with maintenance, readability AND performance in mind. Know what the compiler does (ie go in with your eyes open), and then write whats best for the situation you are handling.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-129</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Wed, 20 Jan 2010 16:34:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-129</guid>
		<description>Just ran a test with HC08 and CodeWarrior -- ternary operator uses 4 more bytes than if/else in one particular test case.</description>
		<content:encoded><![CDATA[<p>Just ran a test with HC08 and CodeWarrior &#8212; ternary operator uses 4 more bytes than if/else in one particular test case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-128</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Mon, 30 Nov 2009 12:05:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-128</guid>
		<description>I guess I&#039;ll just point to my previous comment. I would add, that as compilers improve (even cheap ones) this should become less of an issue. Indeed, unless I&#039;m writing a time critical piece of code I&#039;ll use the operator that more naturally fits the problem.</description>
		<content:encoded><![CDATA[<p>I guess I&#39;ll just point to my previous comment. I would add, that as compilers improve (even cheap ones) this should become less of an issue. Indeed, unless I&#39;m writing a time critical piece of code I&#39;ll use the operator that more naturally fits the problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Egge</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-127</link>
		<dc:creator>Brian Egge</dc:creator>
		<pubDate>Sun, 29 Nov 2009 22:04:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-127</guid>
		<description>&lt;a href=&quot;http://www.theeggeadventure.com/wikimedia/index.php/Ternary_Operator_in_C&quot; rel=&quot;nofollow&quot;&gt;I&#039;ve found the ternary operator creates the same assembly as the control structure&lt;/a&gt;.  I think with modern, optimizing compilers, it really a matter of syntaxtic preference.</description>
		<content:encoded><![CDATA[<p><a href="http://www.theeggeadventure.com/wikimedia/index.php/Ternary_Operator_in_C" rel="nofollow">I&#39;ve found the ternary operator creates the same assembly as the control structure</a>.  I think with modern, optimizing compilers, it really a matter of syntaxtic preference.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: victorh</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-126</link>
		<dc:creator>victorh</dc:creator>
		<pubDate>Thu, 19 Feb 2009 21:09:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-126</guid>
		<description>On some platforms, this form is even more efficient:y = d;if (a &gt; b) {   y = c;}provided that &lt;b&gt;y&lt;/b&gt; is a regular RAM-based variable. If not, e.g. it is a memory-mapped UART transmit data register, the double assignment could (and will) have disastrous side effects.Use with caution.</description>
		<content:encoded><![CDATA[<p>On some platforms, this form is even more efficient:y = d;if (a &gt; b) {   y = c;}provided that <b>y</b> is a regular RAM-based variable. If not, e.g. it is a memory-mapped UART transmit data register, the double assignment could (and will) have disastrous side effects.Use with caution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-125</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Thu, 19 Feb 2009 13:22:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-125</guid>
		<description>I&#039;m not surprised. While I have seen compilers generate the same code for both the ternary operator and the if/else construct, I have never seen a case where the compiler generates better code for the ternary operator. Conversely, I have seen cases where the compiler generates better code for the if/else construct than it does for the ternary operator.</description>
		<content:encoded><![CDATA[<p>I&#8217;m not surprised. While I have seen compilers generate the same code for both the ternary operator and the if/else construct, I have never seen a case where the compiler generates better code for the ternary operator. Conversely, I have seen cases where the compiler generates better code for the if/else construct than it does for the ternary operator.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Uhmmmm</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-124</link>
		<dc:creator>Uhmmmm</dc:creator>
		<pubDate>Wed, 18 Feb 2009 22:45:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-124</guid>
		<description>I just did a quick test with gcc 4.3.3, with a function that just does &quot;return (a &gt; b) ? c : d;&quot; and another one with the equivalent if/else.  It generates identical assembly for me on both amd64 and arm.</description>
		<content:encoded><![CDATA[<p>I just did a quick test with gcc 4.3.3, with a function that just does &quot;return (a &gt; b) ? c : d;&quot; and another one with the equivalent if/else.  It generates identical assembly for me on both amd64 and arm.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
