<?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>Codec &#187; c</title>
	<atom:link href="http://www.trembl.org/codec/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.trembl.org/codec</link>
	<description>A Personal Polylogic Code/Decode &#039;Zettelkasten&#039;</description>
	<lastBuildDate>Thu, 02 Feb 2012 03:38:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>printf()ing string in C++</title>
		<link>http://www.trembl.org/codec/640/</link>
		<comments>http://www.trembl.org/codec/640/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 09:11:47 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[print]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=640</guid>
		<description><![CDATA[Silly, little C++ Beginner&#8217;s mistake. How do you print strings in C++? string test = &#34;tssst&#34;; printf&#40;test&#41;; Of course not, that&#8217;s C++!!! That&#8217;s how you do it: printf&#40;test.c_str&#40;&#41;&#41;; And if you want to stop the compiler throwing warnings: printf&#40;&#34;%s&#34;, test.c_str&#40;&#41;&#41;; So close, yet so far out.]]></description>
			<content:encoded><![CDATA[<p>Silly, little C++ Beginner&#8217;s mistake.</p>
<p>How do you print strings in C++?</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">string test <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;tssst&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>test<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Of course not, that&#8217;s C++!!!</p>
<p>That&#8217;s how you do it:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>test.<span style="color: #202020;">c_str</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And if you want to stop the compiler throwing warnings:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span> test.<span style="color: #202020;">c_str</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So close, yet so far out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/640/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Inline Functions in C</title>
		<link>http://www.trembl.org/codec/632/</link>
		<comments>http://www.trembl.org/codec/632/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 07:36:08 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[inline]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=632</guid>
		<description><![CDATA[The keyword inline, when applied to functions, tells the compiler to optimize calls to the functions. Usually used for short functions and for the sake of clarity. inline int max&#40;int a, int b&#41; &#123; return a + b; &#125; &#160; int main&#40;void&#41; &#123; int x = 2; int y = 6; printf&#40;&#34;result: %d&#34;, add&#40;x,y&#41;&#41;; &#125; [...]]]></description>
			<content:encoded><![CDATA[<p>The keyword <strong>inline</strong>, when applied to functions, tells the compiler to optimize calls to the functions. Usually used for short functions and for the sake of clarity.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">inline</span> <span style="color: #993333;">int</span> max<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> a <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> x <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> y <span style="color: #339933;">=</span> <span style="color: #0000dd;">6</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;result: %d&quot;</span><span style="color: #339933;">,</span> add<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ok, the example is stupid, but i hope it illustrates the idea</span></pre></div></div>

<p>Looks exactly the same as calling a function, but actually the function is <strong>not</strong> called, no function-calling overheads are created. Instead the inline function is <strong>expanded</strong> in place, where it is called. Obviously this only makes sense with rather short functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/632/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Padding with Zeros (or other characters)</title>
		<link>http://www.trembl.org/codec/452/</link>
		<comments>http://www.trembl.org/codec/452/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 04:36:12 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[format]]></category>
		<category><![CDATA[NSLog]]></category>
		<category><![CDATA[NSString]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[sprintf]]></category>
		<category><![CDATA[zeros]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=452</guid>
		<description><![CDATA[int x = 11; &#160; &#91;NSString stringWithFormat:@&#34;%03i&#34;, x&#93;; // @&#34;011&#34; &#160; &#91;NSString stringWithFormat:@&#34;%+5i&#34;, x&#93;; // @&#34;+++11&#34; &#160; &#91;NSString stringWithFormat:@&#34;%+05i&#34;, x&#93;; // @&#34;+0011&#34; And not really like that. 381. I&#8217;ll still have to learn a lot of C.]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> x <span style="color: #002200;">=</span> <span style="color: #2400d9;">11</span>;
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%03i&quot;</span>, x<span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">// @&quot;011&quot;</span>
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%+5i&quot;</span>, x<span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">// @&quot;+++11&quot;</span>
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%+05i&quot;</span>, x<span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">// @&quot;+0011&quot;</span></pre></div></div>

<p>And not really like that. <a href="http://www.trembl.org/codec/381/">381</a>. I&#8217;ll still have to learn a lot of C.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/452/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting current Time with Microseconds</title>
		<link>http://www.trembl.org/codec/420/</link>
		<comments>http://www.trembl.org/codec/420/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 08:22:09 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[micro]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[seconds]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[usec]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=420</guid>
		<description><![CDATA[#import &#38;lt;sys/time.h&#38;gt; &#160; struct&#60;/span&#62; timeval tv; gettimeofday&#40;&#38;tv, NULL&#41;; int sec = tv.tv_sec; int usec = tv.tv_usec; The system&#8217;s notion of the current Greenwich time and the current time zone is obtained with the gettimeofday() call, and set with the ettimeofday() call. The time is expressed in seconds and microseconds since midnight (0 hour), January 1, [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &amp;lt;sys/time.h&amp;gt;</span>
&nbsp;
struct&lt;<span style="color: #002200;">/</span>span&gt; timeval tv;
gettimeofday<span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>tv, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">int</span> sec <span style="color: #002200;">=</span> tv.tv_sec;
<span style="color: #a61390;">int</span> usec <span style="color: #002200;">=</span> tv.tv_usec;</pre></div></div>

<blockquote><p>The system&#8217;s notion of the current Greenwich time and the current time zone is obtained with the gettimeofday() call, and set with the ettimeofday() call.  The time is expressed in seconds and microseconds since midnight (0 hour), January 1, 1970.  The resolution of the system clock is hardware dependent, and the time may be updated continuously or in &#8220;ticks.&#8221;  If tp is NULL and tzp is non-NULL, gettimeofday() will opulate the timezone struct in tzp.  If tp is non-NULL and tzp is NULL, then only the timeval struct in tp is populated. If both tp and tzp are NULL, nothing is returned.</p></blockquote>
<p>Check out the man page for more info.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/420/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Swimming Nearly drowning in C++</title>
		<link>http://www.trembl.org/codec/313/</link>
		<comments>http://www.trembl.org/codec/313/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 09:18:19 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[introdcution]]></category>
		<category><![CDATA[primer]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=313</guid>
		<description><![CDATA[OK, I&#8217;ve decided to do it. To leave the elegance of Objective-C and have a closer look at C++. After all, isn&#8217;t everyone doing oF these days? Here a recapitulation of usesful C and some quick notes on C++ peculiarities &#8211; sorry &#8211; features: (That might be a good place to start.) Initialisation int a [...]]]></description>
			<content:encoded><![CDATA[<p>OK, I&#8217;ve decided to do it. To leave the elegance of Objective-C and have a closer look at C++. After all, isn&#8217;t everyone doing <a href="http://www.openframeworks.cc">oF</a> these days?</p>
<p>Here a recapitulation of usesful C and some quick notes on C++ peculiarities &#8211;  sorry &#8211; features: (<a href="http://www.cplusplus.com/doc/tutorial/">That</a> might be a good place to start.)</p>
<p><strong>Initialisation</strong><br />
int a = 1; 		// standard c way<br />
int b(1);			// same</p>
<p><strong>Defined constant</strong>s<br />
#define PI 3.1415927		// faster than var</p>
<p><strong>Declared constants</strong><br />
const int c = 100		// immutable</p>
<p>Assignment<br />
= can also be used as rvalue<br />
a = 1 + (b = 2); </p>
<p>same as:<br />
b = 2;<br />
a = 1 + b;</p>
<p><strong>Comma operator</strong><br />
a = (b=3, b+2);		// a = 5</p>
<blockquote><p>When the set of expressions has to be evaluated for a value, only the rightmost expression is considered</p></blockquote>
<p><strong>Type casting</strong><br />
int i, j;<br />
float f = 1.234;<br />
i = (int) f;<br />
j = int (f);</p>
<p><strong>sizeof()</strong><br />
One parameter, either a type or a variable and returns the size in bytes.</p>
<p>a = sizeof (char);</p>
<p>Will become important with arrays.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/313/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Converting float to string %4.2f</title>
		<link>http://www.trembl.org/codec/305/</link>
		<comments>http://www.trembl.org/codec/305/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 10:26:34 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[char]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[precision]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[printf]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=305</guid>
		<description><![CDATA[printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416); floats: 3.14 +3e+000 3.141600E+000]]></description>
			<content:encoded><![CDATA[<p><code>printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);<br />
</code><br />
floats: 3.14 +3e+000 3.141600E+000</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/305/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Typedef, Struct, Enum</title>
		<link>http://www.trembl.org/codec/107/</link>
		<comments>http://www.trembl.org/codec/107/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 07:05:15 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[enum]]></category>
		<category><![CDATA[struct]]></category>
		<category><![CDATA[typedef]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=107</guid>
		<description><![CDATA[struct a collection of types, composite data type, composition, &#8220;user-definded data structure&#8221; struct Test { char* name; int nr; float f; }; Name of type is &#8220;struct Test&#8221;, and not &#8220;Test&#8221; alone. struct Test myNewTest Semicolon after closing parethesis is essentional. Access composed types with dot-syntax: myNewTest.name = "hjkjnk"; myNewTest.nr = 32; myNewTest.f = 2.2424; [...]]]></description>
			<content:encoded><![CDATA[<p><strong>struct</strong><br />
a collection of types, composite data type, composition, <em>&#8220;user-definded data structure&#8221;</em><br />
<code>struct Test {<br />
    char* name;<br />
    int nr;<br />
    float f;<br />
};</code></p>
<p>Name of type is &#8220;struct Test&#8221;, and not &#8220;Test&#8221; alone.<br />
<code>struct Test myNewTest</code></p>
<p>Semicolon after closing parethesis is essentional.</p>
<p>Access composed types with dot-syntax:<br />
<code>myNewTest.name = "hjkjnk";<br />
myNewTest.nr = 32;<br />
myNewTest.f = 2.2424</code>;</p>
<p><strong>typedef</strong><br />
a shorthand, an alias for a type, for ease of readability.</p>
<p><code>typedef int MyType</code></p>
<p>&#8220;MyType n;&#8221; would be equal to &#8220;int n;&#8221;</p>
<p><strong>enum</strong><br />
&#8220;introduces a symbolic name for an integer constant&#8221;</p>
<p>enum direction_enum {LEFT, RIGHT};<br />
/* LEFT -> 0, RIGHT -> 1; */<br />
enum direction_enum x = LEFT;</p>
<p><strong>typedef enum<br />
</strong><br />
<code>typedef enum {<br />
    LEFT;<br />
    RIGHT;<br />
} direction;<br />
</code><br />
<em>declatation:</em><br />
-(void)myFunction:(direction)LEFTorRIGHT;</p>
<p><em>call:</em><br />
-(void)myFunction:LEFT;</p>
<p><strong>typedef struct</strong></p>
<p>typedef struct objectInfo_ {<br />
    CGPoint origin;<br />
    NSString* name;<br />
    NSColor* color;<br />
} objectInfo</p>
<p>Shortcut. Instead of writing &#8216;struct objectInfo_&#8217;, write &#8216;objectInfo&#8217;. The keyword &#8216;objectInfo_&#8217; seems to be optional.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/107/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

