# C:无需使用循环语句或递归的循环[英] C: Looping without using looping statements or recursion

## 推荐答案

```#include <signal.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

volatile sig_atomic_t counter;
volatile sig_atomic_t stop;

void alarm_handler(int signal)
{
printf("%d\n", counter++);
if ( counter > stop )
{
exit(0);
}
}

int main(int argc, char **argv)
{
struct itimerval v;
v.it_value.tv_sec = 0;
v.it_value.tv_usec = 5000;
v.it_interval.tv_sec = 0;
v.it_interval.tv_usec = 5000;
int pipefds[2];
char b;

stop = 10;
counter = 1;

pipe(pipefds);

signal(SIGALRM, alarm_handler);

setitimer(ITIMER_REAL, &v, NULL);

read(pipefds[0], &b, 1);
}
```

## 其他推荐答案

```#include <stdlib.h>

int callback(const void *a, const void *b) {
static int n = 1;

if (n <= N)
printf("%d\n", n++);

return 0;
}

int main(int argc, char *argv) {
char *buf;
/* get N value here */

buf = malloc(N);  // could be less than N, but N is definitely sufficient
qsort(buf, N, 1, callback);
}
```

## 其他推荐答案

n不是固定的，因此您无法解开循环.据我所知，C没有迭代器.

(例如，n仅限于1000，但很容易适应)

```int f(int N) {
if (N >= 900) f100(100);
if (N >= 800) f100(100);
if (N >= 700) f100(100);
...

f100(n % 100);
}

int f100(int N) {
if (N >= 90) f10(10);
if (N >= 80) f10(10);
if (N >= 70) f10(10);
...

f(n % 10);
}

int f10(int N) {
if (N >= 9) func();
if (N >= 8) func();
if (N >= 7) func();
...
}
```