maio
[h2N7SspZmY.git] / data / cache / 4 / 48875ca639901baf08f76e1eeba5ff87.xhtml
blobea8202c4636a75af023ec1e1db6dd38eb03457bc
4 <h1><a name="c" id="c">C</a></h1>
5 <div class="level1">
7 <p>
9 <a href="http://en.wikipedia.org/wiki/C_(programming_language)" class="interwiki iw_wp" title="http://en.wikipedia.org/wiki/C_(programming_language)">C</a>
10 </p>
11 <div class="plugin_uparrow">
12 <a href="#" title="Back to top">
13 <img src="lib/plugins/uparrow/images/tango-small.png" alt="Back to top"/>
14 </a>
15 </div>
17 </div>
18 <!-- SECTION "C" [1-52] -->
19 <h2><a name="how_to_print_the_current_time" id="how_to_print_the_current_time">How to print the current time</a></h2>
20 <div class="level2">
22 <p>
24 Using <a href="http://www.cplusplus.com/reference/clibrary/ctime/strftime/" class="urlextern" title="http://www.cplusplus.com/reference/clibrary/ctime/strftime/" rel="nofollow">strftime</a>.
25 </p>
26 <dl class="file">
27 <dt><a href="/~kssilveira/site/doku.php?do=export_code&amp;id=c&amp;codeblock=0" title="Download Snippet" class="mediafile mf_c">show_time.c</a></dt>
28 <dd><pre class="code file c"><span class="co2">#include &lt;stdio.h&gt;</span>
29 <span class="co2">#include &lt;time.h&gt;</span>
30 &nbsp;
31 <span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>
32 <span class="br0">&#123;</span>
33 time_t currtime<span class="sy0">;</span>
34 <span class="kw4">char</span> charTime<span class="br0">&#91;</span>100<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="br0">&#123;</span>0<span class="br0">&#125;</span><span class="sy0">;</span>
35 time<span class="br0">&#40;</span><span class="sy0">&amp;</span>currtime<span class="br0">&#41;</span><span class="sy0">;</span>
36 strftime<span class="br0">&#40;</span>charTime<span class="sy0">,</span><span class="kw4">sizeof</span><span class="br0">&#40;</span>charTime<span class="br0">&#41;</span><span class="sy0">-</span><span class="nu0">1</span><span class="sy0">,</span><span class="st0">&quot;%c&quot;</span><span class="sy0">,</span>localtime<span class="br0">&#40;</span><span class="sy0">&amp;</span>currtime<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
37 <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&quot;time: %s<span class="es1">\n</span>&quot;</span><span class="sy0">,</span> charTime<span class="br0">&#41;</span><span class="sy0">;</span>
38 <span class="coMULTI">/* &quot;time: Sat Apr 17 11:43:47 2010&quot; */</span>
39 <span class="br0">&#125;</span></pre>
40 </dd></dl>
41 <div class="plugin_uparrow">
42 <a href="#" title="Back to top">
43 <img src="lib/plugins/uparrow/images/tango-small.png" alt="Back to top"/>
44 </a>
45 </div>
47 </div>
48 <!-- SECTION "How to print the current time" [53-462] -->
49 <h2><a name="how_to_install_colorgcc" id="how_to_install_colorgcc">How to install colorgcc</a></h2>
50 <div class="level2">
51 <pre class="code bash">$ <span class="kw2">sudo</span> <span class="kw2">apt-get</span> <span class="kw2">install</span> colorgcc
52 $ <span class="kw3">cd</span> <span class="sy0">/</span>usr<span class="sy0">/</span>local<span class="sy0">/</span>bin
53 $ <span class="kw2">sudo</span> <span class="kw2">ln</span> <span class="re5">-s</span> <span class="sy0">/</span>usr<span class="sy0">/</span>bin<span class="sy0">/</span>colorgcc <span class="kw2">gcc</span>
54 $ <span class="kw2">sudo</span> <span class="kw2">ln</span> <span class="re5">-s</span> <span class="sy0">/</span>usr<span class="sy0">/</span>bin<span class="sy0">/</span>colorgcc <span class="kw2">g++</span></pre>
55 <div class="plugin_uparrow">
56 <a href="#" title="Back to top">
57 <img src="lib/plugins/uparrow/images/tango-small.png" alt="Back to top"/>
58 </a>
59 </div>
61 </div>
62 <!-- SECTION "How to install colorgcc" [463-642] -->
63 <h2><a name="tail_recursion_without_return" id="tail_recursion_without_return">Tail Recursion Without Return</a></h2>
64 <div class="level2">
66 <p>
68 In a function with a <a href="http://en.wikipedia.org/wiki/Tail_recursion" class="interwiki iw_wp" title="http://en.wikipedia.org/wiki/Tail_recursion">Tail Recursion</a>
69 </p>
70 <dl class="file">
71 <dt><a href="/~kssilveira/site/doku.php?do=export_code&amp;id=c&amp;codeblock=2" title="Download Snippet" class="mediafile mf_c">mdc.c</a></dt>
72 <dd><pre class="code file c"><span class="kw4">int</span> mdc<span class="br0">&#40;</span><span class="kw4">int</span> x<span class="sy0">,</span> <span class="kw4">int</span> y<span class="br0">&#41;</span>
73 <span class="br0">&#123;</span>
74 <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>y<span class="br0">&#41;</span> <span class="kw1">return</span> x<span class="sy0">;</span>
75 <span class="kw1">return</span> mdc<span class="br0">&#40;</span>y<span class="sy0">,</span> x <span class="sy0">%</span> y<span class="br0">&#41;</span><span class="sy0">;</span>
76 <span class="br0">&#125;</span></pre>
77 </dd></dl>
79 <p>
80 … we can delete de <a href="http://en.wikipedia.org/wiki/Tail_recursion" class="interwiki iw_wp" title="http://en.wikipedia.org/wiki/Tail_recursion">Tail_recursion</a> return(s) …
81 </p>
82 <dl class="file">
83 <dt><a href="/~kssilveira/site/doku.php?do=export_code&amp;id=c&amp;codeblock=3" title="Download Snippet" class="mediafile mf_c">mdc_changed.c</a></dt>
84 <dd><pre class="code file c"><span class="kw4">int</span> mdc<span class="br0">&#40;</span><span class="kw4">int</span> x<span class="sy0">,</span> <span class="kw4">int</span> y<span class="br0">&#41;</span>
85 <span class="br0">&#123;</span>
86 <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>y<span class="br0">&#41;</span> <span class="kw1">return</span> x<span class="sy0">;</span>
87 mdc<span class="br0">&#40;</span>y<span class="sy0">,</span> x <span class="sy0">%</span> y<span class="br0">&#41;</span><span class="sy0">;</span>
88 <span class="br0">&#125;</span></pre>
89 </dd></dl>
91 <p>
92 … This works because the return value goes to eax register and, as the other function calls don&#039;t change its value, they return the same as the last call done.
93 </p>
95 <p>
96 Another example:
97 </p>
98 <dl class="file">
99 <dt><a href="/~kssilveira/site/doku.php?do=export_code&amp;id=c&amp;codeblock=4" title="Download Snippet" class="mediafile mf_c">ackermann.c</a></dt>
100 <dd><pre class="code file c"><span class="kw4">int</span> ackermann<span class="br0">&#40;</span><span class="kw4">int</span> m<span class="sy0">,</span> <span class="kw4">int</span> n<span class="br0">&#41;</span>
101 <span class="br0">&#123;</span>
102 <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>m<span class="br0">&#41;</span> <span class="kw1">return</span> n <span class="sy0">+</span> <span class="nu0">1</span><span class="sy0">;</span>
103 <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>n<span class="br0">&#41;</span> <span class="kw1">return</span> ackermann<span class="br0">&#40;</span>m <span class="sy0">-</span> 1<span class="sy0">,</span> 1<span class="br0">&#41;</span><span class="sy0">;</span>
104 <span class="kw1">return</span> ackermann<span class="br0">&#40;</span>m <span class="sy0">-</span> 1<span class="sy0">,</span> ackermann<span class="br0">&#40;</span>m<span class="sy0">,</span> n <span class="sy0">-</span> 1<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
105 <span class="br0">&#125;</span></pre>
106 </dd></dl>
109 This time we have two tail recursive calls, so we need an else.
110 </p>
111 <dl class="file">
112 <dt><a href="/~kssilveira/site/doku.php?do=export_code&amp;id=c&amp;codeblock=5" title="Download Snippet" class="mediafile mf_c">ackermann_changed.c</a></dt>
113 <dd><pre class="code file c"><span class="kw4">int</span> ackermann<span class="br0">&#40;</span><span class="kw4">int</span> m<span class="sy0">,</span> <span class="kw4">int</span> n<span class="br0">&#41;</span>
114 <span class="br0">&#123;</span>
115 <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>m<span class="br0">&#41;</span> <span class="kw1">return</span> n <span class="sy0">+</span> <span class="nu0">1</span><span class="sy0">;</span>
116 <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>n<span class="br0">&#41;</span> ackermann<span class="br0">&#40;</span>m <span class="sy0">-</span> 1<span class="sy0">,</span> 1<span class="br0">&#41;</span><span class="sy0">;</span>
117 <span class="kw1">else</span> ackermann<span class="br0">&#40;</span>m <span class="sy0">-</span> 1<span class="sy0">,</span> ackermann<span class="br0">&#40;</span>m<span class="sy0">,</span> n <span class="sy0">-</span> 1<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
118 <span class="br0">&#125;</span></pre>
119 </dd></dl>
120 <div class="plugin_uparrow">
121 <a href="#" title="Back to top">
122 <img src="lib/plugins/uparrow/images/tango-small.png" alt="Back to top"/>
123 </a>
124 </div>
126 </div>
127 <!-- SECTION "Tail Recursion Without Return" [643-1568] -->
128 <h2><a name="minimal_factorial_program" id="minimal_factorial_program">Minimal Factorial Program</a></h2>
129 <div class="level2">
130 <dl class="file">
131 <dt><a href="/~kssilveira/site/doku.php?do=export_code&amp;id=c&amp;codeblock=6" title="Download Snippet" class="mediafile mf_c">f.c</a></dt>
132 <dd><pre class="code file c">main<span class="br0">&#40;</span>n<span class="br0">&#41;</span><span class="br0">&#123;</span><span class="kw1">return</span> <span class="sy0">!</span>n<span class="sy0">?:</span>n<span class="sy0">*</span>main<span class="br0">&#40;</span>n<span class="sy0">-</span>1<span class="br0">&#41;</span><span class="sy0">;</span><span class="br0">&#125;</span></pre>
133 </dd></dl>
134 <pre class="code bash">$ <span class="kw2">gcc</span> <span class="re5">-o</span> f f.c
135 $ .<span class="sy0">/</span>f<span class="sy0">||</span><span class="kw3">echo</span> <span class="re4">$?</span>
137 $ .<span class="sy0">/</span>f .<span class="sy0">||</span><span class="kw3">echo</span> <span class="re4">$?</span>
139 $ .<span class="sy0">/</span>f . .<span class="sy0">||</span><span class="kw3">echo</span> <span class="re4">$?</span>
141 $ .<span class="sy0">/</span>f . . .<span class="sy0">||</span><span class="kw3">echo</span> <span class="re4">$?</span>
143 $ .<span class="sy0">/</span>f . . . .<span class="sy0">||</span><span class="kw3">echo</span> <span class="re4">$?</span>
144 <span class="nu0">120</span></pre>
145 <div class="plugin_uparrow">
146 <a href="#" title="Back to top">
147 <img src="lib/plugins/uparrow/images/tango-small.png" alt="Back to top"/>
148 </a>
149 </div>
151 </div>
152 <!-- SECTION "Minimal Factorial Program" [1569-1806] -->
153 <h3><a name="explanation" id="explanation">Explanation</a></h3>
154 <div class="level3">
155 <ul>
156 <li class="level1"><div class="li"> main return type defaults to int</div>
157 </li>
158 <li class="level1"><div class="li"> main first parameter defaults to int and is the number of command line arguments</div>
159 </li>
160 <li class="level1"><div class="li"> ”!n” is the same as “n == 0</div>
161 </li>
162 <li class="level1"><div class="li"> “x ? : y” is the same as “x ? x : y”</div>
163 </li>
164 <li class="level1"><div class="li"> $? is the return value of the program</div>
165 </li>
166 </ul>
167 <div class="plugin_uparrow">
168 <a href="#" title="Back to top">
169 <img src="lib/plugins/uparrow/images/tango-small.png" alt="Back to top"/>
170 </a>
171 </div>
173 </div>
174 <!-- SECTION "Explanation" [1807-2068] -->
175 <h2><a name="minimal_fibonacci_program" id="minimal_fibonacci_program">Minimal Fibonacci Program</a></h2>
176 <div class="level2">
180 Same idea of the factorial.
181 </p>
182 <dl class="file">
183 <dt><a href="/~kssilveira/site/doku.php?do=export_code&amp;id=c&amp;codeblock=8" title="Download Snippet" class="mediafile mf_c">f.c</a></dt>
184 <dd><pre class="code file c">main<span class="br0">&#40;</span>n<span class="br0">&#41;</span><span class="br0">&#123;</span><span class="kw1">return</span> n<span class="sy0">&lt;</span>2<span class="sy0">?:</span>main<span class="br0">&#40;</span>n<span class="sy0">-</span>1<span class="br0">&#41;</span><span class="sy0">+</span>main<span class="br0">&#40;</span>n<span class="sy0">-</span>2<span class="br0">&#41;</span><span class="sy0">;</span><span class="br0">&#125;</span></pre>
185 </dd></dl>
186 <pre class="code bash">$ <span class="kw2">gcc</span> <span class="re5">-o</span> f f.c
187 $ .<span class="sy0">/</span>f<span class="sy0">||</span><span class="kw3">echo</span> <span class="re4">$?</span>
189 $ .<span class="sy0">/</span>f .<span class="sy0">||</span><span class="kw3">echo</span> <span class="re4">$?</span>
191 $ .<span class="sy0">/</span>f . .<span class="sy0">||</span><span class="kw3">echo</span> <span class="re4">$?</span>
193 $ .<span class="sy0">/</span>f . . .<span class="sy0">||</span><span class="kw3">echo</span> <span class="re4">$?</span>
195 $ .<span class="sy0">/</span>f . . . .<span class="sy0">||</span><span class="kw3">echo</span> <span class="re4">$?</span>
197 $ .<span class="sy0">/</span>f . . . . .<span class="sy0">||</span><span class="kw3">echo</span> <span class="re4">$?</span>
198 <span class="nu0">13</span></pre>
199 <div class="plugin_uparrow">
200 <a href="#" title="Back to top">
201 <img src="lib/plugins/uparrow/images/tango-small.png" alt="Back to top"/>
202 </a>
203 </div>
205 </div>
206 <!-- SECTION "Minimal Fibonacci Program" [2069-] -->