给我一些有趣的C语言代码
时间: 2023-05-24 08:06:57 浏览: 71
1. Hello World program with a twist:
```
#include <stdio.h>
int main() {
char str[] = {72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33, 10};
for (int i=0; i<sizeof(str); i++) {
printf("%c", str[i]);
}
return 0;
}
```
2. Fibonacci sequence using recursion:
```
#include <stdio.h>
int fibonacci(int n) {
if (n<=1) {
return n;
}
return fibonacci(n-1) + fibonacci(n-2);
}
int main() {
int n = 10;
for (int i=0; i<n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
```
3. Bouncing ball animation using ASCII characters:
```
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define WIDTH 50
int main() {
int x=0;
int dir=1;
char ball[] = "\n|*>\n";
while (1) {
system("clear");
char row[WIDTH+1];
memset(row, 32, sizeof(row));
row[WIDTH] = '\0';
row[x] = ball[0];
row[x+1] = ball[1];
row[x+2] = ball[2];
row[x+3] = ball[3];
printf("%s", row);
x += dir;
if (x>=WIDTH-3 || x<=0) {
dir *= -1;
}
usleep(100000);
}
return 0;
}
```