K
Kohn Emil Dan
..and lateron
And how about this one? This is of course an unusual recursion, but I
think it's funny. Notice that here you are likely to be hit by a limit
imposed by the OS regarding the number of processes. Did I pass the
interview now? ;-))
Emil
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
/* Make sure you define PROG_NAME to the name of your executable.
Using argv[0] is not portable */
#define PROG_NAME "print_1_to_n.exe"
#define N 100
int main(int argc, char *argv[])
{
/* Allocate 1 digit for every 3 bits, hopefully is enough */
char buffer[sizeof(PROG_NAME) +
(sizeof(int)*CHAR_BIT + 2)/3 +
1];
int n;
switch (argc) {
case 1:
n = 0;
break;
case 2:
/* Yeah I know, atoi() is bad, real programs should
should use strtol() and check for errors*/
n = atoi(argv[1]);
if (n <= 0) {
fprintf(stderr,"Wrong arguments\n");
exit(1);
}
break;
default:
fprintf(stderr,"Wrong arguments\n");
exit(1);
}
n++;
printf("%d\n",n);
if (n>=N)
return 0;
sprintf(buffer,"%s %d",PROG_NAME,n);
system(buffer);
return 0;
}