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=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-642] -->
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=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=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=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=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" [643-1568] -->
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=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" [1569-1806] -->
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" [1807-2068] -->
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=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" [2069-] -->