bunch of changes
This commit is contained in:
33
examples/fib.c
Normal file
33
examples/fib.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int n = 35;
|
||||
|
||||
int fib(int x) {
|
||||
if (x < 2) {
|
||||
return x;
|
||||
}
|
||||
return fib(x - 1) + fib(x - 2);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc > 1) {
|
||||
char*p;
|
||||
|
||||
errno = 0;
|
||||
long conv = strtol(argv[1], &p, 10);
|
||||
|
||||
// Check for errors: e.g., the string does not represent an integer
|
||||
// or the integer is larger than int
|
||||
if (errno != 0 || *p != '\0' || conv > INT_MAX || conv < INT_MIN) {
|
||||
printf("invalid argument: %s\n", p);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
n = conv;
|
||||
}
|
||||
|
||||
printf("%d\n", fib(n));
|
||||
}
|
||||
Reference in New Issue
Block a user