# Copyright 2016 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. // comments // comments /* comments comments * / // */ #include <stdio.h> #include <hello, world.h> /* */int main() { printf("hello, world\n"); printf("#include <hello, world.h>\n"); return 0; } /*
// Copyright 2016 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. #include <stdio.h> int main() { int i, sum; i = 0; sum = 0; while(i <= 100) { sum = sum+i; i++; } printf("sum(1+..+100) = %d\n", sum); return 0; }
// Copyright 2016 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. #include <stdio.h> int main() { int t1, t2, next; t1 = t2 = 1; printf("%d %d ", t1, t2); next = t1 + t2; while(next <= 1000) { printf("%d ",next); t1 = t2; t2 = next; next = t1 + t2; } printf("\n"); return 0; }
// Copyright 2016 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. #include <stdio.h> int is_prime(int n) { int i; i = 2; while(i*i <= n) { if(n%i == 0) return 0; i++; } return 1; } int main() { int i; i = 2; while(i < 20) { if(is_prime(i)) { printf("%d\n", i); } i++; } return 0; }
// Copyright 2016 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. int gcd(int m, int n) { return n? gcd(n, m%n): m; } int main() { printf("gcd(27,15): %d\n", gcd(27,15)); return 0; }
// Copyright 2016 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. #include <stdio.h> #include <stdlib.h> int gcd(int m, int n) { return n? gcd(n, m%n): m; } int gcd_stk(int m, int n) { int *stk; int sp; int ret; stk = malloc(1024); sp = 0; // push call frame stk[sp+0] = 0; // return value stk[sp+1] = 0; // return flag stk[sp+2] = m; // push m stk[sp+3] = n; // push n sp = sp + 4; while(sp > 0) { m = stk[sp-2]; n = stk[sp-1]; if(stk[sp-3]) { stk[sp-4] = stk[sp]; // return gcd(n, m%n); sp = sp - 4; // pop call frame } else if(n == 0) { stk[sp-4] = m; // if(n == 0) return m; sp = sp - 4; // pop call frame } else { stk[sp-3] = 1; // parent call is in return path // push call frame stk[sp+0] = 0; // return value stk[sp+1] = 0; // return flag stk[sp+2] = n; // push n stk[sp+3] = m%n; // push m%n sp = sp + 4; } } ret = stk[sp]; free(stk); return ret; } int main() { printf("gcd(27,15): %d\n", gcd(27,15)); printf("gcd(12,18): %d\n", gcd(12,18)); printf("gcd(13,19): %d\n", gcd(13,19)); printf("gcd_stk(27,15): %d\n", gcd_stk(27,15)); printf("gcd_stk(12,18): %d\n", gcd_stk(12,18)); printf("gcd_stk(13,19): %d\n", gcd_stk(13,19)); return 0; }
// Copyright 2016 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. #include <stdio.h> #include <stdlib.h> int main() { char *name; int fd; char *p; int i; // embedded file name = "data/hello.c"; // open for readonly fd = open(name, 0); if(fd < 0) { printf("could not open(%s) file\n", name); return -1; } p = malloc(1024); i = read(fd, p, 1024-1); close(fd); if(i <= 0) { free(p); printf("read() returned %d\n", i); return -1; return -1; } p[i] = 0; printf("%s", p); free(p); return 0; }
main(a){printf(a="main(a){printf(a=%c%s%c,34,a,34);}",34,a,34);}