<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JB Enterprises – Johan Bezem &#187; C programming bizarre</title>
	<atom:link href="http://www.bezem.de/tag/c-programming-bizarre/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bezem.de</link>
	<description>Development &#38; Project Management</description>
	<lastBuildDate>Tue, 24 Jan 2012 18:22:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Bizarre switch statement in C</title>
		<link>http://www.bezem.de/2009/11/bizarre-switch-statement-in-c/</link>
		<comments>http://www.bezem.de/2009/11/bizarre-switch-statement-in-c/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 05:25:47 +0000</pubDate>
		<dc:creator>Johan Bezem</dc:creator>
				<category><![CDATA[General development]]></category>
		<category><![CDATA[C programming bizarre]]></category>
		<category><![CDATA[static syntax checking]]></category>

		<guid isPermaLink="false">http://www.bezem.de/?p=256</guid>
		<description><![CDATA[An example of what can be legally done in C illustrated in form of a rather bizarre switch-statement.]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been in my &#8220;Bizarre C&#8221; box for many years. Things you may want or need to know about, but never would want to duplicate.</p>
<p>Currently, I am researching for a new publication, maybe a PDF document, maybe a course, on the nooks an crannies of the C programming language. And I&#8217;m finding some abstruse samples, I can tell you.</p>
<p>But one example I found most bizarre at the time (around 1990), and which still is among my favorites to flabbergast experienced C programmers, I found in the best C programming reference I&#8217;ve known to date: &#8220;<a href="http://www.amzn.com/013089592X">C &#8211; A Reference Manual</a>&#8221; by Samuel P. Harbison and Guy L. Steele (see also the <a href="http://www.careferencemanual.com/">book&#8217;s website</a>).</p>
<p>Imagine a calculation depending on certain numbers being prime or not. If the routine gets a prime parameter it executes one routine, if the parameter is not prime, another. Like this:<br />
<code>if (is_prime(a))<br />
Â·Â·process_prime(a);<br />
else<br />
Â·Â·process_nonprime(a);</code><br />
The <code>if</code>-statement is just that, a statement, a composite statement.<br />
A <code>switch</code>-statement is composed of a <code>switch</code> keyword, the value on which to operate, and a, possibly composite, statement. It could be our <code>if</code>-statement, if we so choose. However, without any <code>case</code>/<code>default</code>-labels, the <code>switch</code>-statement would just jump over the statement so specified, effectively doing nothing. To remedy that, we put the <code>default</code>-label before the <code>if</code>-statement:<br />
<code>switch (a)<br />
Â·Â·default:<br />
Â·Â·Â·Â·if (is_prime(a))<br />
Â·Â·Â·Â·Â·Â·process_prime(a);<br />
Â·Â·Â·Â·else<br />
Â·Â·Â·Â·Â·Â·process_nonprime(a);</code><br />
This code already looks strange, but it is functionally equivalent to the <code>if</code>-statement by itself.<br />
If we now imagine the function <code>is_prime(a)</code> to be very expensive, it would make sense to take a shortcut around that function wherever possible. And if 99% of the values the variable <code>a</code> can have lie between 2 and 10 inclusive, it definitely would make sense to circumvent the <code>is_prime()</code> function, since we know the primeness of those values without calculation:<br />
<code>switch (a)<br />
Â·Â·default:<br />
Â·Â·Â·Â·if (is_prime(a))<br />
Â·Â·case 2: case 3: case 5: case 7:<br />
Â·Â·Â·Â·Â·Â·process_prime(a);<br />
Â·Â·Â·Â·else<br />
Â·Â·case 4: case 6: case 8: case 9: case 10:<br />
Â·Â·Â·Â·Â·Â·process_nonprime(a);</code><br />
To understand why this is correct C, we need to realize that <code>case</code>-labels belonging to a <code>switch</code>-statement can be positioned anywhere within the boundaries of the composite statement belonging to the <code>switch</code>. In our case, this means until the semicolon after the <code>process_nonprime(a)</code> statement.<br />
It works as follows:</p>
<ul>
<li>If <code>a</code> is 3 upon execution of the <code>switch</code>, the label <code>case 3:</code> is where execution continues after determining the value of <code>a</code> to be three, jumping into the middle of the <code>if</code>-statement and calling the function <code>process_prime</code> with a parameter 3. After execution of that function, the <code>if</code>-statement is terminated, terminating the enclosing <code>switch</code>-statement at the same time.</li>
<li>If <code>a</code> is 8 upon execution of the <code>switch</code>, the label <code>case 8:</code> is where execution continues after determining the value of <code>a</code> to be eight, jumping into the middle of the <code>if</code>-statement and calling the function <code>process_nonprime</code> with a parameter 8. After execution of that function, the <code>if</code>-statement is terminated, terminating the enclosing <code>switch</code>-statement at the same time.</li>
<li>If <code>a</code> is 29 upon execution of the <code>switch</code>, the label <code>default:</code> is where execution continues after determining that no known value is to be processed, executing the <code>if</code>-statement from its beginning. Depending on the value of <code>a</code> (29 in this case) the <code>if</code>-condition determines which of the two alternative functions to call, just like a normal <code>if</code>-statement.</li>
</ul>
<p>I can only agree with Sam Harbison and Guy Steele: &#8220;This is frankly the most bizarre switch statement we have ever seen that still has pretenses to being purposeful.&#8221;<br />
Would you want the nuclear reactor around the corner being coded like this?<br />
Happy coding!</p>
<p>Johan</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bezem.de/2009/11/bizarre-switch-statement-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

