<?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/category/tutorial/c-programming/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.bstpierre.org</link>
	<description>Software Development, version 3.0</description>
	<lastBuildDate>Fri, 03 Feb 2012 02:59:01 +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>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).]]></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>
]]></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 [...]]]></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>
]]></content:encoded>
			<wfw:commentRss>http://blog.bstpierre.org/getting-started-in-c/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

