'Build Your Own Lisp' Solutions: 3.3

Q: Declare a function that outputs 'Hello World!' n number of times. Call this from main.

A:

#include <stdio.h>
#include <stdlib.h>

void hello_n_times(int n)
{
        while (n--)
                printf("Hello world!\n");
}

int main(int argc, char *argv[])
{
        int n;

        /* not part of the exercise, but we 
           may as well not hardcode the number */
        if (argv[1] && (n = strtol(argv[1], NULL, 10)))
                hello_n_times(n);
        else
                printf("Needs Positive Integer!\n");

        return 0;
}
> ./hello_n_times 3
Hello world!
Hello world!
Hello world!