News for hackers. Stuff that matters.
Re:Printing 1 to 1000 without loop or conditionals by juiceandjuice on Monday January 03, 2011 @01:47AM (#2062398)
Actually the one that uses function pointers is by far the best, simplest way, and it doesn't segfault. Since x/1000 will always be floor(x/1000), once it reaches 1000 it calls nothing and exits cleanly.

This one (not mine):

    #include <stdio.h>

    void nothing(int);
    void next(int);
    void (*dispatch[2])(int) = {next, nothing};

    void nothing(int x) { }
    void next(int x)
    {
        printf("%i\n", x);
        dispatch[x/1000](x+1);
    }

    int main()
    {
        next(1);
        return 0;
    }

There are no comments on this comment yet.