3 [[wp>C_(programming_language)|C]]
5 ===== How to print the current time =====
7 Using [[http://www.cplusplus.com/reference/clibrary/ctime/strftime/|strftime]].
16 char charTime[100] = {0};
18 strftime(charTime,sizeof(charTime)-1,"%c",localtime(&currtime));
19 printf("time: %s\n", charTime);
20 /* "time: Sat Apr 17 11:43:47 2010" */
24 ===== How to install colorgcc =====
27 $ sudo apt-get install colorgcc
29 $ sudo ln -s /usr/bin/colorgcc gcc
30 $ sudo ln -s /usr/bin/colorgcc g++
33 ===== Tail Recursion Without Return =====
35 In a function with a [[wp>Tail_recursion|Tail Recursion]] ...
45 ... we can delete de [[wp>Tail_recursion]] return(s) ...
47 <file c mdc_changed.c>
55 ... 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.
60 int ackermann(int m, int n)
63 if(!n) return ackermann(m - 1, 1);
64 return ackermann(m - 1, ackermann(m, n - 1));
68 This time we have two tail recursive calls, so we need an else.
70 <file c ackermann_changed.c>
71 int ackermann(int m, int n)
74 if(!n) ackermann(m - 1, 1);
75 else ackermann(m - 1, ackermann(m, n - 1));
79 ===== Minimal Factorial Program =====
82 main(n){return !n?:n*main(n-1);}
95 $ ./f . . . .||echo $?
101 * main return type defaults to int
102 * main first parameter defaults to int and is the number of command line arguments
103 * "!n" is the same as "n == 0"
104 * "x ? : y" is the same as "x ? x : y"
105 * $? is the return value of the program
107 ===== Minimal Fibonacci Program =====
109 Same idea of the factorial.
112 main(n){return n<2?:main(n-1)+main(n-2);}
125 $ ./f . . . .||echo $?
127 $ ./f . . . . .||echo $?