<?xml version="1.0"?>
<?xml-stylesheet type="text/xml" href="presenter.xsl"?>
<slideshow>
	<title>C for C++ Programmers</title>

	<slide>
		<title>C for C++ Programmers</title>
		<body>
			<p>By Karl Voelker</p>
		</body>
	</slide>

	<slide>
		<title>Agenda</title>
		<body>
			<ul>
				<li class='cur'>Why know C?</li>
				<li class='after'>Syntax</li>
				<li class='after'>Memory management</li>
				<li class='after'>Strings</li>
				<li class='after'>Input and Output</li>
			</ul>
		</body>
	</slide>

	<slide>
		<title>Why know C?</title>
		<body>
			<ul>
				<li>C is not fun.</li>
				<li>C is not easy.</li>
				<li>C is not pretty.</li>
			</ul>
			<p><em>But...</em></p>
			<ul>
				<li>C is <em>everywhere</em>.</li>
				<li>C explains the quirks of C++.</li>
			</ul>
		</body>
	</slide>

	<slide>
		<title>Agenda</title>
		<body>
			<ul>
				<li class='before'>Why know C?</li>
				<li class='cur'>Syntax</li>
				<li class='after'>Memory management</li>
				<li class='after'>Strings</li>
				<li class='after'>Input and Output</li>
			</ul>
		</body>
	</slide>

	<slide>
		<title>Classes</title>
		<body>
			<p>C doesn't have classes.</p>
			<p>Lots of libraries for C pretend they have classes, 
				without the polymorphism.</p>
		</body>
	</slide>

	<slide>
		<title>Pointers and References</title>
		<body>
			<p>C does not have references.</p>
			<p>Example:</p>
			<table>
				<tr><td>C++</td>
				<td><tt>int a = 5;<br/>int &amp; b = a;</tt></td></tr>
				<tr><td>C (or C++)</td>
				<td><tt>int a = 5;<br/>int * b = &amp;a;</tt></td></tr>
			</table>
		</body>
	</slide>

	<slide>
		<title><tt>struct</tt></title>
		<body>
			<p>This struct type is valid in C and C++:</p>
			<code>struct foo {
	int i;
	double d;
};</code>
			<p>How do we refer to this type?</p>
			<table>
				<tr><td>C++</td><td><tt>foo</tt></td></tr>
				<tr><td>C</td><td><tt>struct foo</tt></td></tr>
			</table>
		</body>
	</slide>

	<slide>
		<title><tt>struct</tt> continued</title>
		<body>
			<p>For example:</p>
			<table>
				<tr><td>C++</td>
				<td><tt>foo f;<br/>f.i = 3;<br/>f.d = 4.0;</tt></td></tr>
				<tr><td>C</td>
				<td><tt>struct foo f;<br/>f.i = 3;<br/>f.d = 4.0</tt></td></tr>
			</table>
		</body>
	</slide>

	<slide>
		<title><tt>struct</tt> with <tt>typedef</tt></title>
		<body>
			<p>This works in C and C++, but is more common in C:</p>
			<code>typedef struct {
	int i;
	double d;
} foo;</code>
			<p><tt>typedef</tt> makes the anonymous <tt>struct</tt> have the 
				typename <tt>foo</tt>.</p>
		</body>
	</slide>

	<slide>
		<title>Agenda</title>
		<body>
			<ul>
				<li class='before'>Why know C?</li>
				<li class='before'>Syntax</li>
				<li class='cur'>Memory management</li>
				<li class='after'>Strings</li>
				<li class='after'>Input and Output</li>
			</ul>
		</body>
	</slide>

	<slide>
		<title>Memory Management</title>
		<body>
			<p>Memory management isn't much different in C:</p>
			<table>
			<tr><td>C++</td>
			<td><tt>foo * f = new foo;<br/>
					delete foo;</tt></td></tr>
			<tr><td>C</td>
			<td><tt>foo * f = malloc(sizeof(foo));<br/>
					free(f);</tt></td></tr>
			</table>
			<p>Note: in C, the allocated memory is not 
				initialized.</p>
		</body>
	</slide>

	<slide>
		<title>Allocating Arrays</title>
		<body>
			<p>In C, allocating arrays is often necessary.</p>
			<table>
			<tr><td>C++</td>
			<td><tt>foo * f = new foo[64];<br/>
					delete[] foo;</tt></td></tr>
			<tr><td>C</td>
			<td><tt>foo * f = <br/>
					malloc(sizeof(foo) * 64);<br/>
					free(f);</tt></td></tr>
			</table>
			<p>Note: in C, the array is not initialized.</p>
		</body>
	</slide>

	<slide>
		<title>Agenda</title>
		<body>
			<ul>
				<li class='before'>Why know C?</li>
				<li class='before'>Syntax</li>
				<li class='before'>Memory management</li>
				<li class='cur'>Strings</li>
				<li class='after'>Input and Output</li>
			</ul>
		</body>
	</slide>

	<slide><title>Strings</title><body>
		<ul>
		<li>C doesn't have a <tt>string</tt> class, so we use 
			character arrays.</li>
		<li>We'll still call them "strings."</li>
		<li>Literal strings are made with <tt>""</tt>.</li>
		<li>Strings always end with \0.
			<ul>
				<li>Literal strings end with \0 implicitly.</li>
			</ul>
		</li>
		</ul>
	</body></slide>

	<slide><title>String Functions</title><body>
		<p>These are from <tt>string.h</tt>.</p>
		<table>
		<tr><td><tt>strlen</tt></td>
			<td>Find length</td></tr>
		<tr><td><tt>strcmp</tt></td>
			<td>Compare</td></tr>
		<tr><td><tt>strcpy</tt></td>
			<td>Copy</td></tr>
		<tr><td><tt>strcat</tt></td>
			<td>Concatenate</td></tr>
		<tr><td><tt>strchr</tt></td>
			<td>Find character</td></tr>
		<tr><td><tt>strstr</tt></td>
			<td>Find substring</td></tr>
		<tr><td><tt>strtok</tt></td>
			<td>Tokenize</td></tr>
		<tr><td><tt>atoi</tt></td>
			<td>Convert to <tt>int</tt></td></tr>
		</table>
	</body></slide>

	<slide><title><tt>strlen</tt> and <tt>strcmp</tt></title><body>
		<pre>char * alice = "alice";
char * bob = "bob";
char * eve = "eve";

strlen(alice) == 5;
strlen(bob) == 3;

strcmp(alice, bob) == -1;
strcmp(bob, bob) == 0;
strcmp(eve, bob) == 1;</pre>
	</body></slide>

	<slide><title><tt>strcpy</tt> and <tt>strcat</tt></title><body>
		<pre>char * alice = "alice";

char * clone = malloc(sizeof(char) * 
	(strlen(alice) + 1));
strcpy(clone, alice);

char * twins = malloc(sizeof(char) * 
	(2 * strlen(alice) + 1));
twins[0] = '\0';
strcat(twins, alice);
strcat(twins, alice);</pre>
	</body></slide>

	<slide><title><tt>strchr</tt> and <tt>strstr</tt></title><body>
		<pre>char * alice = "alice";
char * bob = "bob";
char * lice = "lice";

strchr(alice, 'o') == NULL;
strchr(alice, 'c') == alice + 3;

strstr(alice, bob) == NULL;
strstr(alice, lice) == alice + 1;</pre>
	</body></slide>

	<slide><title><tt>strtok</tt></title><body>
		<pre>char * tokens = "foo@bar";

char * first = strtok(tokens, "@");
first == tokens;
strcmp(first, "foo") == 0;

char * second = strtok(NULL, "@");
second == tokens + 4;
strcmp(second, "bar") == 0;

strtok(NULL, "@") == NULL;</pre>
	</body></slide>

	<slide><title><tt>atoi</tt></title><body>
		<pre>char * alice = "alice";
char * zero = "0";
char * hacker_alice = "41ic3";
char * five = "5";
char * six = "   6";
char * seven = "7 + 3";

atoi(alice) == 0;
atoi(zero) == 0;
atoi(hacker_alice) == 41;
atoi(five) == 5;
atoi(six) == 6;
atoi(seven) == 7;</pre>
	</body></slide>

	<slide>
		<title>Agenda</title>
		<body>
			<ul>
				<li class='before'>Why know C?</li>
				<li class='before'>Syntax</li>
				<li class='before'>Memory management</li>
				<li class='before'>Strings</li>
				<li class='cur'>Input and Output</li>
			</ul>
		</body>
	</slide>

	<slide><title>Input and Output</title><body>
		<p>C does not have <tt>cin</tt> or <tt>cout</tt>.</p>
		<p>We use <tt>scanf</tt> and <tt>printf</tt>, which are 
			functions.</p>
	</body></slide>

	<slide><title><tt>printf</tt></title><body>
		<p>The "format string" tells <tt>printf</tt> how to 
			print its other arguments.</p>
		<pre>printf("Hello, World!\n");

char * name = "Alice";
printf("Hello, %s!\n", name);

int hour = 7;
printf("It is %d o'clock.\n", hour);

printf("Hello, %s, it is %d o'clock.\n", 
	name, hour);</pre>
	</body></slide>

	<slide><title>Format Strings</title><body>
		<table>
		<tr><td><tt>%s
		</tt></td><td>String
		</td></tr>

		<tr><td><tt>%d
		</tt></td><td>Signed decimal integer
		</td></tr>

		<tr><td><tt>%Xd
		</tt></td><td>Signed decimal integer<br/>
			Minimum field width X<br/>
			Padded with spaces
		</td></tr>

		<tr><td><tt>%0Xd
		</tt></td><td>Signed decimal integer<br/>
			Minimum field width X<br/>
			Padded with zeroes
		</td></tr>

		<tr><td><tt>%%
		</tt></td><td>Percent sign
		</td></tr>

		</table>
	</body></slide>

	<slide><title>More Integer Format Strings</title><body>
		<table>
		<tr><td><tt>%u
		</tt></td><td>Unsigned decimal integer
		</td></tr>

		<tr><td><tt>%o
		</tt></td><td>Octal integer
		</td></tr>

		<tr><td><tt>%x
		</tt></td><td>Hexadecimal integer<br/>
			Lowercase letters
		</td></tr>

		<tr><td><tt>%X
		</tt></td><td>Hexadecimal integer<br/>
			Uppercase letters
		</td></tr>
		</table>
	</body></slide>

	<slide><title>Floating-Point Format Strings</title><body>
		<table>
		<tr><td><tt>%X.Yf
		</tt></td><td>Floating-point<br/>
			Minimum field width X<br/>
			Y decimal places
		</td></tr>

		<tr><td><tt>%X.Ye
		</tt></td><td>Scientific notation<br/>
			Lowercase <tt>e</tt>
		</td></tr>

		<tr><td><tt>%X.YE
		</tt></td><td>Scientific notation<br/>
			Uppercase <tt>E</tt>
		</td></tr>
		
		</table>
	</body></slide>

	<slide><title>Functions like <tt>printf</tt></title><body>
		<dl>
		<dt><tt>fprintf(FILE*, const char*, ...)</tt></dt>
		<dd>Print to a file</dd>
		<dt><tt>sprintf(char*, const char*, ...)</tt></dt>
		<dd>Print to a buffer</dd>
		<dt><tt>snprintf(char*, size_t, const char*, ...)</tt></dt>
		<dd>Print safely to a buffer 
			(extra parameter is buffer size)</dd>
		</dl>
	</body></slide>

	<slide><title>Input</title><body>
		<p>The <tt>scanf</tt> family of functions work 
			like <tt>printf</tt>, but for input:</p>
		<pre>int i, j;
scanf("%d%d", &amp;i, &amp;j);</pre>
	</body></slide>

	<slide><title>Files</title><body>
		<dl>
		<dt><tt>fopen</tt></dt>
		<dd>Open a file</dd>
		<dt><tt>fclose</tt></dt>
		<dd>Close a file</dd>
		<dt><tt>feof</tt></dt>
		<dd>Test for end-of-file</dd>
		<dt><tt>ferror</tt></dt>
		<dd>Test for errors</dd>
		</dl>
	</body></slide>

	<slide><title>Learning More</title><body>
		<p>On UNIX-like systems, manual pages should exist 
			for all standard C functions.</p>
		<p>Also, try 
		<a href='http://cppreference.com/'>http://cppreference.com/
		</a>, great for C and C++.</p>
	</body></slide>

	<slide><title>The End</title><body>
		<p>Questions?</p>
	</body></slide>

</slideshow>
