<?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>The Daily Build &#187; c-programming</title>
	<atom:link href="http://blog.bstpierre.org/tag/c-programming/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.bstpierre.org</link>
	<description>Software Development, version 3.0</description>
	<lastBuildDate>Wed, 26 May 2010 16:08:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Data vs Code</title>
		<link>http://blog.bstpierre.org/data-vs-code</link>
		<comments>http://blog.bstpierre.org/data-vs-code#comments</comments>
		<pubDate>Fri, 05 Feb 2010 16:08:05 +0000</pubDate>
		<dc:creator>Brian St. Pierre</dc:creator>
				<category><![CDATA[engineering]]></category>
		<category><![CDATA[c-programming]]></category>
		<category><![CDATA[software-engineering]]></category>

		<guid isPermaLink="false">http://blog.bstpierre.org/?p=285</guid>
		<description><![CDATA[I&#8217;ll take an array over a giant switch-case statement any day.

The array definition will be more compact and easier to see all at once.
Defining actions in an array enforces uniformity.
You can put checks in the code to automatically verify that the array definition is complete. (I.e. verify it contains a definition for every item it [...]


Related posts:<ol><li><a href='http://blog.bstpierre.org/who-else-wants-better-short-term-memory' rel='bookmark' title='Permanent Link: Who Else Wants Better Short Term Memory?'>Who Else Wants Better Short Term Memory?</a> <small> In &#8220;Talent is Overrated&#8221;, Geoff...</small></li><li><a href='http://blog.bstpierre.org/if-the-comments-are-ugly-the-code-is-ugly' rel='bookmark' title='Permanent Link: If the comments are ugly, the code is ugly'>If the comments are ugly, the code is ugly</a> <small> If the comments are ugly,...</small></li><li><a href='http://blog.bstpierre.org/insist-on-tests' rel='bookmark' title='Permanent Link: Insist on Automatic Tests'>Insist on Automatic Tests</a> <small> At some point your team...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll take an array over a giant switch-case statement any day.</p>
<ol>
<li>The array definition will be more compact and easier to see all at once.</li>
<li>Defining actions in an array enforces uniformity.</li>
<li>You can put checks in the code to automatically verify that the array definition is complete. (I.e. verify it contains a definition for every item it should have.) Yes, some tools can do this for certain types of switch statements. Using an array-based check is more portable and more foolproof.</li>
</ol>
<p>If you have a switch statement of mostly cut and paste cases, you can probably convert it to an array very easily, and then rewrite the switch statement to look up the value in the array and do whatever thing is supposed to be done, either via function pointers or by using an associated value from the array.</p>
<p>I did this on a horrible switch statement once. Dozens of cases, 80% were nearly identical but the few that weren&#8217;t were awful to untangle. Once I pulled the case bodies into separate functions, put function pointers into a table, and replaced the switch body with a lookup loop it was much cleaner. The code for the odd cases was eventually pushed out (it was a symptom of bad design). The whole exercise enabled another round of changes that allowed the functions for the case bodies to be collapsed back into the table &#8212; we ended up removing an entire unnecessary layer of indirection and made the design much easier to grok.</p>


<p>Related posts:<ol><li><a href='http://blog.bstpierre.org/who-else-wants-better-short-term-memory' rel='bookmark' title='Permanent Link: Who Else Wants Better Short Term Memory?'>Who Else Wants Better Short Term Memory?</a> <small> In &#8220;Talent is Overrated&#8221;, Geoff...</small></li><li><a href='http://blog.bstpierre.org/if-the-comments-are-ugly-the-code-is-ugly' rel='bookmark' title='Permanent Link: If the comments are ugly, the code is ugly'>If the comments are ugly, the code is ugly</a> <small> If the comments are ugly,...</small></li><li><a href='http://blog.bstpierre.org/insist-on-tests' rel='bookmark' title='Permanent Link: Insist on Automatic Tests'>Insist on Automatic Tests</a> <small> At some point your team...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.bstpierre.org/data-vs-code/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simpler C Development Environment</title>
		<link>http://blog.bstpierre.org/simpler-c-development-environment</link>
		<comments>http://blog.bstpierre.org/simpler-c-development-environment#comments</comments>
		<pubDate>Mon, 19 Jan 2009 17:14:35 +0000</pubDate>
		<dc:creator>Brian St. Pierre</dc:creator>
				<category><![CDATA[c-programming]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.bstpierre.org/?p=88</guid>
		<description><![CDATA[If you're on Windows and you had problems installing Cygwin, there is another package that includes GCC called "MinGW" (minimalist GNU for Windows).


Related posts:<ol><li><a href='http://blog.bstpierre.org/configure-ssh-username' rel='bookmark' title='Permanent Link: How to Tell SSH Who You Are'>How to Tell SSH Who You Are</a> <small>Do you log in to several...</small></li><li><a href='http://blog.bstpierre.org/ssh-forward-multiple-protocols-to-multiple-machines' rel='bookmark' title='Permanent Link: Use SSH to Forward Multiple Protocols to Multiple Machines'>Use SSH to Forward Multiple Protocols to Multiple Machines</a> <small>Let's say you have a half-dozen...</small></li><li><a href='http://blog.bstpierre.org/using-pythons-ctypes-to-make-system-calls' rel='bookmark' title='Permanent Link: Using Python&#8217;s ctypes to Call Into C Libraries'>Using Python&#8217;s ctypes to Call Into C Libraries</a> <small>The ctypes module makes loading and...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re on Windows and you had problems installing Cygwin, there is another package that includes GCC called &#8220;MinGW&#8221; (minimalist GNU for Windows). Download and run this file:</p>
<p>http://internap.dl.sourceforge.net/sourceforge/mingw/MinGW-5.1.4.exe</p>
<p>Go through the installer and keep clicking the defaults. It will then<br />
download and install several other packages. When the install finishes,<br />
do Start Menu -> Run&#8230; -> &#8220;cmd&#8221;, then:</p>
<p>C:\> md tutorial<br />
C:\> cd tutorial<br />
C:\tutorial> set path=C:\MinGW\bin;%path%<br />
C:\tutorial> gcc hello.c</p>
<p>And follow the rest of the directions <a href="/getting-started-in-c">from the first tutorial post</a>, making the appropriate path substitutions and using \ instead of /.</p>
<p>If you have problems downloading through the automatic installer, try following the <a href="http://www.mingw.org/wiki/Getting_Started#toc2">MinGW &#8220;Manual Download&#8221; directions</a>. (Just for the &#8220;minimum requirements&#8221;.)</p>


<p>Related posts:<ol><li><a href='http://blog.bstpierre.org/configure-ssh-username' rel='bookmark' title='Permanent Link: How to Tell SSH Who You Are'>How to Tell SSH Who You Are</a> <small>Do you log in to several...</small></li><li><a href='http://blog.bstpierre.org/ssh-forward-multiple-protocols-to-multiple-machines' rel='bookmark' title='Permanent Link: Use SSH to Forward Multiple Protocols to Multiple Machines'>Use SSH to Forward Multiple Protocols to Multiple Machines</a> <small>Let's say you have a half-dozen...</small></li><li><a href='http://blog.bstpierre.org/using-pythons-ctypes-to-make-system-calls' rel='bookmark' title='Permanent Link: Using Python&#8217;s ctypes to Call Into C Libraries'>Using Python&#8217;s ctypes to Call Into C Libraries</a> <small>The ctypes module makes loading and...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.bstpierre.org/simpler-c-development-environment/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Started in C</title>
		<link>http://blog.bstpierre.org/getting-started-in-c</link>
		<comments>http://blog.bstpierre.org/getting-started-in-c#comments</comments>
		<pubDate>Fri, 09 Jan 2009 19:14:16 +0000</pubDate>
		<dc:creator>Brian St. Pierre</dc:creator>
				<category><![CDATA[c-programming]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.bstpierre.org/?p=74</guid>
		<description><![CDATA[Some may argue that C is an outdated language to learn in 2009. I disagree. Learning C requires some degree of understanding how computers really work. And if you can understand pointers, you&#8217;re ten steps ahead of the game.
This is the first in a series of articles designed to help the reader learn the C [...]


Related posts:<ol><li><a href='http://blog.bstpierre.org/using-pythons-ctypes-to-make-system-calls' rel='bookmark' title='Permanent Link: Using Python&#8217;s ctypes to Call Into C Libraries'>Using Python&#8217;s ctypes to Call Into C Libraries</a> <small>The ctypes module makes loading and...</small></li><li><a href='http://blog.bstpierre.org/ssh-forward-multiple-protocols-to-multiple-machines' rel='bookmark' title='Permanent Link: Use SSH to Forward Multiple Protocols to Multiple Machines'>Use SSH to Forward Multiple Protocols to Multiple Machines</a> <small>Let's say you have a half-dozen...</small></li><li><a href='http://blog.bstpierre.org/zsh-prompt' rel='bookmark' title='Permanent Link: Set Your zsh Prompt'>Set Your zsh Prompt</a> <small>Since the beginning of time, all...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Some may argue that C is an outdated language to learn in 2009. I disagree. Learning C requires some degree of understanding how computers really work. And if you can <a href="http://http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html">understand pointers</a>, you&#8217;re ten steps ahead of the game.</p>
<p>This is the first in a series of articles designed to help the reader learn the C language and understand how to use it to solve practical problems. The tutorial will be based on gcc in a Cygwin or Linux environment.</p>
<p>If you are already running Linux or have Cygwin and GCC installed, you&#8217;re halfway done with the first lesson.</p>
<h2>Lesson 1</h2>
<h3>Part 1, Get GCC</h3>
<p>If you&#8217;re using linux, open a terminal window (bash prompt). Type &#8220;gcc &#8211;version&#8221;. If you get back something like:</p>
<pre>$ gcc --version
gcc (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.</pre>
<p>Then you&#8217;re all set. If you don&#8217;t have gcc installed, use your distribution&#8217;s package installation tool to install it. (For example, Debian-based systems will use <tt>sudo apt-get install gcc</tt>.)</p>
<p>If you&#8217;re on Windows, install <a href="http://cygwin.com/setup.exe">Cygwin</a>. When the selection box pops up to choose packages, expand the &#8220;Devel&#8221; category and select gcc:</p>
<p><img class="aligncenter size-medium wp-image-75" title="Select GCC from the list of packages." src="http://blog.bstpierre.org/wp-content/uploads/2009/01/cygwin-gcc-select-300x211.png" alt="Select GCC from the list of packages." width="300" height="211" /></p>
<p>There&#8217;s a great <a href="http://http://matrixprogramming.com/Tools/InstallCygwin.html">step-by-step tutorial</a> available if you want more detail.</p>
<h3>Part 2, &#8220;Hello World&#8221;</h3>
<p>Now you&#8217;re ready to write your first program. If you&#8217;re on Windows, I strongly recommend using a decent text editor instead of Windows Notepad or WordPad. If you have no editor on your system, try &lt;a href=&#8221;http://www.brixoft.net/dload.asp?file=srcedit.zip&#8221;&gt;Source Edit&lt;/a&gt;. I haven&#8217;t used it much, but it seems to work well enough. If you&#8217;re on Linux, you have a choice of whatever editors are installed on your system: vi, emacs, pico, etc.</p>
<p>Now, create a directory to store your source files. On Windows, place this directory under your cygwin installation directory. I&#8217;m going to assume you&#8217;re using c:\cygwin\tutorials\, so adjust accordingly if you choose a different path.</p>
<p>Finally, we can write a program to print the string &#8220;Hello World&#8221; to the output when you run it. Open your text editor and <em>type in</em> the following program. (Do not cut and paste. The act of retyping the code presented in this tutorial will help you learn better.)</p>
<pre>#include &lt;stdio.h&gt;

int main(int argc, char** argv)
{
    printf("Hello World\n");
    return 0;
}</pre>
<p>Now, get to a bash prompt. (On cygwin, this is the cygwin icon that should be on your desktop from part 1 above. On linux, this is a terminal window or whatever your system happens to call it.) Change to the directory where your files are stored.</p>
<pre>bash$ cd /tutorial</pre>
<p>On cygwin, this will put you in the c:\cygwin\tutorial directory. Now, compile the file.</p>
<pre>bash$ gcc hello.c</pre>
<p>GCC does not give output for a successful compile, it just produces the executable. If you do a directory listing (&#8220;ls&#8221; at the bash prompt) you will see a file called a.out (linux) or a.exe (cygwin). For historical reasons, this is what the compiler produces by default. You can run this and see the output. Type &#8220;./a.exe&#8221; or (./a.out) at the bash prompt and you should get the greeting.</p>
<p>We can change the output file by telling the compiler what we want it to name the file.</p>
<pre>bash$ gcc -o hello hello.c</pre>
<p>The -o flag (think of &#8220;output&#8221;) tells the compiler to produce a file called hello.exe (cygwin) or hello (linux). Try this, and run &#8220;./hello.exe&#8221;.</p>
<h3>Part 3, Breakdown</h3>
<p>Here&#8217;s a rundown of what the pieces of this program mean.</p>
<p><tt>#include &lt;stdio.h&gt;</tt></p>
<p>This tells the compiler to pull the text of the file stdio.h into the program. This is called a &#8220;header&#8221; file, because it is typically included at the top (head) of your program. This header file contains the standard input/output definitions and declarations, including the declaration of the function &#8220;printf&#8221; which is used below.</p>
<p><tt>int main(int argc, char** argv)</tt></p>
<p>This is the start of a function definition. &#8220;main&#8221; is the name of the function that the operating system will call when you run the program. Main is always declared this way: it accepts two &#8220;arguments&#8221; called argc and argv, and it returns an integer (int). We&#8217;ll come back to the argc and argv in another lesson.</p>
<p><tt>{</tt></p>
<p>A left brace is used to indicate the start of a block. The line that describes the type and arguments of the function is followed by a block that contains the programming statements that actually do something useful.</p>
<p><tt>printf("Hello World\n");</tt></p>
<p>This calls the printf function, which sends text to the output device (in this case, your terminal window). A function call is built by following the function name with parentheses and including arguments inside the parentheses. The printf function is passed an argument: the string &#8220;Hello World\n&#8221;.</p>
<p>In C, strings are surrounded by quotation marks. The sequence \n means &#8220;newline&#8221;. strong&gt;Exercise: modify the program to remove the \n, compile it, and see what happens.</p>
<p><tt>return 0;</tt></p>
<p>This ends the function, returning control to the function that called it (in this case, back to the operating system). It gives the value 0 to the calling function. When we return 0 to the operating system, this signifies a normal exit. Returning 1 or other values generally signifies that an error occurred.</p>
<p><tt>}</tt></p>
<p>A right brace is used to mark the end of the function.</p>
<p><strong>Exercise</strong>: Experiment with this. Change the string. Add another call to printf to display a different greeting.</p>
<p>Next lesson: the debugger.</p>


<p>Related posts:<ol><li><a href='http://blog.bstpierre.org/using-pythons-ctypes-to-make-system-calls' rel='bookmark' title='Permanent Link: Using Python&#8217;s ctypes to Call Into C Libraries'>Using Python&#8217;s ctypes to Call Into C Libraries</a> <small>The ctypes module makes loading and...</small></li><li><a href='http://blog.bstpierre.org/ssh-forward-multiple-protocols-to-multiple-machines' rel='bookmark' title='Permanent Link: Use SSH to Forward Multiple Protocols to Multiple Machines'>Use SSH to Forward Multiple Protocols to Multiple Machines</a> <small>Let's say you have a half-dozen...</small></li><li><a href='http://blog.bstpierre.org/zsh-prompt' rel='bookmark' title='Permanent Link: Set Your zsh Prompt'>Set Your zsh Prompt</a> <small>Since the beginning of time, all...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.bstpierre.org/getting-started-in-c/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
