backup de julho
[h2N7SspZmY.git] / data / cache / 9 / 9b19acc1b0687c846be54ddaec71f1e5.xhtml
blobc0daa276bc9271eec71db121c142f909dd44d306
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=main: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-634] -->
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=main: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=main: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=main: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=main: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" [635-1560] -->
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=main: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" [1561-1798] -->
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" [1799-2060] -->
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=main: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" [2061-2361] -->
207 <h2><a name="how_to_pass_a_matrix_as_parameter_to_a_funcion" id="how_to_pass_a_matrix_as_parameter_to_a_funcion">How to pass a matrix as parameter to a funcion</a></h2>
208 <div class="level2">
209 <pre class="code c"><span class="kw4">int</span> sum2<span class="br0">&#40;</span><span class="kw4">int</span> n<span class="sy0">,</span> <span class="kw4">int</span> m<span class="sy0">,</span> <span class="kw4">int</span> matrix<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#91;</span>m<span class="br0">&#93;</span><span class="br0">&#41;</span>
210 <span class="br0">&#123;</span>
211 <span class="kw4">int</span> i<span class="sy0">,</span> j<span class="sy0">,</span> sum<span class="sy0">;</span>
212 <span class="kw1">for</span><span class="br0">&#40;</span>i <span class="sy0">=</span> sum <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0">&lt;</span> n<span class="sy0">;</span> i<span class="sy0">++</span><span class="br0">&#41;</span>
213 <span class="kw1">for</span><span class="br0">&#40;</span>j <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> j <span class="sy0">&lt;</span> m<span class="sy0">;</span> j<span class="sy0">++</span><span class="br0">&#41;</span>
214 sum <span class="sy0">+=</span> matrix<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#91;</span>j<span class="br0">&#93;</span><span class="sy0">;</span>
215 <span class="kw1">return</span> sum<span class="sy0">;</span>
216 <span class="br0">&#125;</span>
217 &nbsp;
218 <span class="kw4">int</span> sum3<span class="br0">&#40;</span><span class="kw4">int</span> n<span class="sy0">,</span> <span class="kw4">int</span> m<span class="sy0">,</span> <span class="kw4">int</span> p<span class="sy0">,</span> <span class="kw4">int</span> matrix<span class="br0">&#91;</span>n<span class="br0">&#93;</span><span class="br0">&#91;</span>m<span class="br0">&#93;</span><span class="br0">&#91;</span>p<span class="br0">&#93;</span><span class="br0">&#41;</span>
219 <span class="br0">&#123;</span>
220 <span class="co1">// ...</span>
221 <span class="br0">&#125;</span></pre>
222 <div class="plugin_uparrow">
223 <a href="#" title="Back to top">
224 <img src="lib/plugins/uparrow/images/tango-small.png" alt="Back to top"/>
225 </a>
226 </div>
228 </div>
229 <!-- SECTION "How to pass a matrix as parameter to a funcion" [2362-2665] -->
230 <h2><a name="designated_initializers" id="designated_initializers">Designated Initializers</a></h2>
231 <div class="level2">
232 <pre class="code c"><span class="coMULTI">/* [index] = value */</span>
233 <span class="kw4">int</span> a<span class="br0">&#91;</span>6<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="br0">&#123;</span> <span class="br0">&#91;</span>4<span class="br0">&#93;</span> <span class="sy0">=</span> 29<span class="sy0">,</span> <span class="br0">&#91;</span>2<span class="br0">&#93;</span> <span class="sy0">=</span> 15 <span class="br0">&#125;</span><span class="sy0">;</span> <span class="co1">// { 0, 0, 15, 0, 29, 0 };</span>
234 &nbsp;
235 <span class="kw4">int</span> a<span class="br0">&#91;</span>6<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="br0">&#123;</span> <span class="br0">&#91;</span>1<span class="br0">&#93;</span> <span class="sy0">=</span> v1<span class="sy0">,</span> v2<span class="sy0">,</span> <span class="br0">&#91;</span>4<span class="br0">&#93;</span> <span class="sy0">=</span> v4 <span class="br0">&#125;</span><span class="sy0">;</span> <span class="co1">// { 0, v1, v2, 0, v4, 0 };</span>
236 &nbsp;
237 <span class="kw4">int</span> a<span class="br0">&#91;</span>6<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="br0">&#123;</span> <span class="br0">&#91;</span>1 ... 4<span class="br0">&#93;</span> <span class="sy0">=</span> 1 <span class="br0">&#125;</span><span class="sy0">;</span> <span class="co1">// { 0, 1, 1, 1, 1, 0 }</span>
238 &nbsp;
239 <span class="coMULTI">/* also works with chars */</span>
240 <span class="kw4">int</span> isWhitespace<span class="br0">&#91;</span><span class="nu0">256</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="br0">&#123;</span> <span class="br0">&#91;</span><span class="st0">' '</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="nu0">1</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="st0">'<span class="es1">\t</span>'</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="nu0">1</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="st0">'<span class="es1">\n</span>'</span><span class="br0">&#93;</span> <span class="sy0">=</span> 1 <span class="br0">&#125;</span><span class="sy0">;</span></pre>
241 <pre class="code c"><span class="kw4">struct</span> point <span class="br0">&#123;</span> <span class="kw4">int</span> x<span class="sy0">,</span> y<span class="sy0">;</span> <span class="br0">&#125;</span><span class="sy0">;</span>
242 &nbsp;
243 <span class="coMULTI">/* .fieldname = value */</span>
244 <span class="kw4">struct</span> point p <span class="sy0">=</span> <span class="br0">&#123;</span> .<span class="me1">y</span> <span class="sy0">=</span> yvalue<span class="sy0">,</span> .<span class="me1">x</span> <span class="sy0">=</span> xvalue <span class="br0">&#125;</span><span class="sy0">;</span> <span class="co1">// p = { xvalue, yvalue };</span>
245 &nbsp;
246 <span class="coMULTI">/* [index].fieldname = value */</span>
247 <span class="kw4">struct</span> point ptarray<span class="br0">&#91;</span>10<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="br0">&#123;</span> <span class="br0">&#91;</span>2<span class="br0">&#93;</span>.<span class="me1">y</span> <span class="sy0">=</span> yv2<span class="sy0">,</span> <span class="br0">&#91;</span>2<span class="br0">&#93;</span>.<span class="me1">x</span> <span class="sy0">=</span> xv2<span class="sy0">,</span> <span class="br0">&#91;</span>0<span class="br0">&#93;</span>.<span class="me1">x</span> <span class="sy0">=</span> xv0 <span class="br0">&#125;</span><span class="sy0">;</span></pre>
248 <div class="plugin_uparrow">
249 <a href="#" title="Back to top">
250 <img src="lib/plugins/uparrow/images/tango-small.png" alt="Back to top"/>
251 </a>
252 </div>
254 </div>
255 <!-- SECTION "Designated Initializers" [2666-3281] -->
256 <h2><a name="how_to_find_the_standard_header_files" id="how_to_find_the_standard_header_files">How to find the standard header files</a></h2>
257 <div class="level2">
261 /usr/include/ <br/>
263 /usr/include/c++/ <br/>
265 </p>
266 <div class="plugin_uparrow">
267 <a href="#" title="Back to top">
268 <img src="lib/plugins/uparrow/images/tango-small.png" alt="Back to top"/>
269 </a>
270 </div>
272 </div>
273 <!-- SECTION "How to find the standard header files" [3282-] -->