4 <h1><a name=
"c" id=
"c">C
</a></h1>
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>
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"/>
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>
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>.
27 <dt><a href=
"/~kssilveira/site/doku.php?do=export_code&id=main:c&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
<stdio.h
></span>
29 <span class=
"co2">#include
<time.h
></span>
31 <span class=
"kw4">int
</span> main
<span class=
"br0">(</span><span class=
"br0">)</span>
32 <span class=
"br0">{</span>
33 time_t currtime
<span class=
"sy0">;
</span>
34 <span class=
"kw4">char
</span> charTime
<span class=
"br0">[</span>100<span class=
"br0">]</span> <span class=
"sy0">=
</span> <span class=
"br0">{</span>0<span class=
"br0">}</span><span class=
"sy0">;
</span>
35 time
<span class=
"br0">(</span><span class=
"sy0">&</span>currtime
<span class=
"br0">)</span><span class=
"sy0">;
</span>
36 strftime
<span class=
"br0">(</span>charTime
<span class=
"sy0">,
</span><span class=
"kw4">sizeof
</span><span class=
"br0">(</span>charTime
<span class=
"br0">)</span><span class=
"sy0">-
</span><span class=
"nu0">1</span><span class=
"sy0">,
</span><span class=
"st0">"%c
"</span><span class=
"sy0">,
</span>localtime
<span class=
"br0">(</span><span class=
"sy0">&</span>currtime
<span class=
"br0">)</span><span class=
"br0">)</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">(</span><span class=
"st0">"time: %s
<span class=
"es1">\n
</span>"</span><span class=
"sy0">,
</span> charTime
<span class=
"br0">)</span><span class=
"sy0">;
</span>
38 <span class=
"coMULTI">/*
"time: Sat Apr
17 11:
43:
47 2010" */
</span>
39 <span class=
"br0">}</span></pre>
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"/>
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>
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"/>
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>
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> …
71 <dt><a href=
"/~kssilveira/site/doku.php?do=export_code&id=main:c&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">(</span><span class=
"kw4">int
</span> x
<span class=
"sy0">,
</span> <span class=
"kw4">int
</span> y
<span class=
"br0">)</span>
73 <span class=
"br0">{</span>
74 <span class=
"kw1">if
</span><span class=
"br0">(</span><span class=
"sy0">!
</span>y
<span class=
"br0">)</span> <span class=
"kw1">return
</span> x
<span class=
"sy0">;
</span>
75 <span class=
"kw1">return
</span> mdc
<span class=
"br0">(</span>y
<span class=
"sy0">,
</span> x
<span class=
"sy0">%
</span> y
<span class=
"br0">)</span><span class=
"sy0">;
</span>
76 <span class=
"br0">}</span></pre>
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) …
83 <dt><a href=
"/~kssilveira/site/doku.php?do=export_code&id=main:c&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">(</span><span class=
"kw4">int
</span> x
<span class=
"sy0">,
</span> <span class=
"kw4">int
</span> y
<span class=
"br0">)</span>
85 <span class=
"br0">{</span>
86 <span class=
"kw1">if
</span><span class=
"br0">(</span><span class=
"sy0">!
</span>y
<span class=
"br0">)</span> <span class=
"kw1">return
</span> x
<span class=
"sy0">;
</span>
87 mdc
<span class=
"br0">(</span>y
<span class=
"sy0">,
</span> x
<span class=
"sy0">%
</span> y
<span class=
"br0">)</span><span class=
"sy0">;
</span>
88 <span class=
"br0">}</span></pre>
92 … This works because the return value goes to eax register and, as the other function calls don
't change its value, they return the same as the last call done.
99 <dt><a href=
"/~kssilveira/site/doku.php?do=export_code&id=main:c&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">(</span><span class=
"kw4">int
</span> m
<span class=
"sy0">,
</span> <span class=
"kw4">int
</span> n
<span class=
"br0">)</span>
101 <span class=
"br0">{</span>
102 <span class=
"kw1">if
</span><span class=
"br0">(</span><span class=
"sy0">!
</span>m
<span class=
"br0">)</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">(</span><span class=
"sy0">!
</span>n
<span class=
"br0">)</span> <span class=
"kw1">return
</span> ackermann
<span class=
"br0">(</span>m
<span class=
"sy0">-
</span> 1<span class=
"sy0">,
</span> 1<span class=
"br0">)</span><span class=
"sy0">;
</span>
104 <span class=
"kw1">return
</span> ackermann
<span class=
"br0">(</span>m
<span class=
"sy0">-
</span> 1<span class=
"sy0">,
</span> ackermann
<span class=
"br0">(</span>m
<span class=
"sy0">,
</span> n
<span class=
"sy0">-
</span> 1<span class=
"br0">)</span><span class=
"br0">)</span><span class=
"sy0">;
</span>
105 <span class=
"br0">}</span></pre>
109 This time we have two tail recursive calls, so we need an else.
112 <dt><a href=
"/~kssilveira/site/doku.php?do=export_code&id=main:c&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">(</span><span class=
"kw4">int
</span> m
<span class=
"sy0">,
</span> <span class=
"kw4">int
</span> n
<span class=
"br0">)</span>
114 <span class=
"br0">{</span>
115 <span class=
"kw1">if
</span><span class=
"br0">(</span><span class=
"sy0">!
</span>m
<span class=
"br0">)</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">(</span><span class=
"sy0">!
</span>n
<span class=
"br0">)</span> ackermann
<span class=
"br0">(</span>m
<span class=
"sy0">-
</span> 1<span class=
"sy0">,
</span> 1<span class=
"br0">)</span><span class=
"sy0">;
</span>
117 <span class=
"kw1">else
</span> ackermann
<span class=
"br0">(</span>m
<span class=
"sy0">-
</span> 1<span class=
"sy0">,
</span> ackermann
<span class=
"br0">(</span>m
<span class=
"sy0">,
</span> n
<span class=
"sy0">-
</span> 1<span class=
"br0">)</span><span class=
"br0">)</span><span class=
"sy0">;
</span>
118 <span class=
"br0">}</span></pre>
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"/>
127 <!-- SECTION "Tail Recursion Without Return" [635-1560] -->
128 <h2><a name=
"minimal_factorial_program" id=
"minimal_factorial_program">Minimal Factorial Program
</a></h2>
131 <dt><a href=
"/~kssilveira/site/doku.php?do=export_code&id=main:c&codeblock=6" title=
"Download Snippet" class=
"mediafile mf_c">f.c
</a></dt>
132 <dd><pre class=
"code file c">main
<span class=
"br0">(</span>n
<span class=
"br0">)</span><span class=
"br0">{</span><span class=
"kw1">return
</span> <span class=
"sy0">!
</span>n
<span class=
"sy0">?:
</span>n
<span class=
"sy0">*
</span>main
<span class=
"br0">(</span>n
<span class=
"sy0">-
</span>1<span class=
"br0">)</span><span class=
"sy0">;
</span><span class=
"br0">}</span></pre>
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"/>
152 <!-- SECTION "Minimal Factorial Program" [1561-1798] -->
153 <h3><a name=
"explanation" id=
"explanation">Explanation
</a></h3>
156 <li class=
"level1"><div class=
"li"> main return type defaults to int
</div>
158 <li class=
"level1"><div class=
"li"> main first parameter defaults to int and is the number of command line arguments
</div>
160 <li class=
"level1"><div class=
"li"> ”!n” is the same as “n ==
0”
</div>
162 <li class=
"level1"><div class=
"li"> “x ? : y” is the same as “x ? x : y”
</div>
164 <li class=
"level1"><div class=
"li"> $? is the return value of the program
</div>
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"/>
174 <!-- SECTION "Explanation" [1799-2060] -->
175 <h2><a name=
"minimal_fibonacci_program" id=
"minimal_fibonacci_program">Minimal Fibonacci Program
</a></h2>
180 Same idea of the factorial.
183 <dt><a href=
"/~kssilveira/site/doku.php?do=export_code&id=main:c&codeblock=8" title=
"Download Snippet" class=
"mediafile mf_c">f.c
</a></dt>
184 <dd><pre class=
"code file c">main
<span class=
"br0">(</span>n
<span class=
"br0">)</span><span class=
"br0">{</span><span class=
"kw1">return
</span> n
<span class=
"sy0"><</span>2<span class=
"sy0">?:
</span>main
<span class=
"br0">(</span>n
<span class=
"sy0">-
</span>1<span class=
"br0">)</span><span class=
"sy0">+
</span>main
<span class=
"br0">(</span>n
<span class=
"sy0">-
</span>2<span class=
"br0">)</span><span class=
"sy0">;
</span><span class=
"br0">}</span></pre>
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"/>
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>
209 <pre class=
"code c"><span class=
"kw4">int
</span> sum2
<span class=
"br0">(</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">[</span>n
<span class=
"br0">]</span><span class=
"br0">[</span>m
<span class=
"br0">]</span><span class=
"br0">)</span>
210 <span class=
"br0">{</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">(</span>i
<span class=
"sy0">=
</span> sum
<span class=
"sy0">=
</span> <span class=
"nu0">0</span><span class=
"sy0">;
</span> i
<span class=
"sy0"><</span> n
<span class=
"sy0">;
</span> i
<span class=
"sy0">++
</span><span class=
"br0">)</span>
213 <span class=
"kw1">for
</span><span class=
"br0">(</span>j
<span class=
"sy0">=
</span> <span class=
"nu0">0</span><span class=
"sy0">;
</span> j
<span class=
"sy0"><</span> m
<span class=
"sy0">;
</span> j
<span class=
"sy0">++
</span><span class=
"br0">)</span>
214 sum
<span class=
"sy0">+=
</span> matrix
<span class=
"br0">[</span>i
<span class=
"br0">]</span><span class=
"br0">[</span>j
<span class=
"br0">]</span><span class=
"sy0">;
</span>
215 <span class=
"kw1">return
</span> sum
<span class=
"sy0">;
</span>
216 <span class=
"br0">}</span>
218 <span class=
"kw4">int
</span> sum3
<span class=
"br0">(</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">[</span>n
<span class=
"br0">]</span><span class=
"br0">[</span>m
<span class=
"br0">]</span><span class=
"br0">[</span>p
<span class=
"br0">]</span><span class=
"br0">)</span>
219 <span class=
"br0">{</span>
220 <span class=
"co1">// ...
</span>
221 <span class=
"br0">}</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"/>
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>
232 <pre class=
"code c"><span class=
"coMULTI">/* [index] = value */
</span>
233 <span class=
"kw4">int
</span> a
<span class=
"br0">[</span>6<span class=
"br0">]</span> <span class=
"sy0">=
</span> <span class=
"br0">{</span> <span class=
"br0">[</span>4<span class=
"br0">]</span> <span class=
"sy0">=
</span> 29<span class=
"sy0">,
</span> <span class=
"br0">[</span>2<span class=
"br0">]</span> <span class=
"sy0">=
</span> 15 <span class=
"br0">}</span><span class=
"sy0">;
</span> <span class=
"co1">// {
0,
0,
15,
0,
29,
0 };
</span>
235 <span class=
"kw4">int
</span> a
<span class=
"br0">[</span>6<span class=
"br0">]</span> <span class=
"sy0">=
</span> <span class=
"br0">{</span> <span class=
"br0">[</span>1<span class=
"br0">]</span> <span class=
"sy0">=
</span> v1
<span class=
"sy0">,
</span> v2
<span class=
"sy0">,
</span> <span class=
"br0">[</span>4<span class=
"br0">]</span> <span class=
"sy0">=
</span> v4
<span class=
"br0">}</span><span class=
"sy0">;
</span> <span class=
"co1">// {
0, v1, v2,
0, v4,
0 };
</span>
237 <span class=
"kw4">int
</span> a
<span class=
"br0">[</span>6<span class=
"br0">]</span> <span class=
"sy0">=
</span> <span class=
"br0">{</span> <span class=
"br0">[</span>1 ...
4<span class=
"br0">]</span> <span class=
"sy0">=
</span> 1 <span class=
"br0">}</span><span class=
"sy0">;
</span> <span class=
"co1">// {
0,
1,
1,
1,
1,
0 }
</span>
239 <span class=
"coMULTI">/* also works with chars */
</span>
240 <span class=
"kw4">int
</span> isWhitespace
<span class=
"br0">[</span><span class=
"nu0">256</span><span class=
"br0">]</span> <span class=
"sy0">=
</span> <span class=
"br0">{</span> <span class=
"br0">[</span><span class=
"st0">' '
</span><span class=
"br0">]</span> <span class=
"sy0">=
</span> <span class=
"nu0">1</span><span class=
"sy0">,
</span> <span class=
"br0">[</span><span class=
"st0">'
<span class=
"es1">\t
</span>'
</span><span class=
"br0">]</span> <span class=
"sy0">=
</span> <span class=
"nu0">1</span><span class=
"sy0">,
</span> <span class=
"br0">[</span><span class=
"st0">'
<span class=
"es1">\n
</span>'
</span><span class=
"br0">]</span> <span class=
"sy0">=
</span> 1 <span class=
"br0">}</span><span class=
"sy0">;
</span></pre>
241 <pre class=
"code c"><span class=
"kw4">struct
</span> point
<span class=
"br0">{</span> <span class=
"kw4">int
</span> x
<span class=
"sy0">,
</span> y
<span class=
"sy0">;
</span> <span class=
"br0">}</span><span class=
"sy0">;
</span>
243 <span class=
"coMULTI">/* .fieldname = value */
</span>
244 <span class=
"kw4">struct
</span> point p
<span class=
"sy0">=
</span> <span class=
"br0">{</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">}</span><span class=
"sy0">;
</span> <span class=
"co1">// p = { xvalue, yvalue };
</span>
246 <span class=
"coMULTI">/* [index].fieldname = value */
</span>
247 <span class=
"kw4">struct
</span> point ptarray
<span class=
"br0">[</span>10<span class=
"br0">]</span> <span class=
"sy0">=
</span> <span class=
"br0">{</span> <span class=
"br0">[</span>2<span class=
"br0">]</span>.
<span class=
"me1">y
</span> <span class=
"sy0">=
</span> yv2
<span class=
"sy0">,
</span> <span class=
"br0">[</span>2<span class=
"br0">]</span>.
<span class=
"me1">x
</span> <span class=
"sy0">=
</span> xv2
<span class=
"sy0">,
</span> <span class=
"br0">[</span>0<span class=
"br0">]</span>.
<span class=
"me1">x
</span> <span class=
"sy0">=
</span> xv0
<span class=
"br0">}</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"/>
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>
263 /usr/include/c++/
<br/>
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"/>
273 <!-- SECTION "How to find the standard header files" [3282-] -->