If you add pre- and postconditions using the function assert you can make sure that parameters and function results have reasonable values:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
//grid problem
int fact(int n)
{
assert(n >= 0);
int i, f = 1;
if (n == 0) {
return 1;
}
for (i = 1; i <= n; i++) {
f *= i;
}
assert(f >= 1);
return f;
}
int uniquePaths(int A, int B)
{
assert(A >= 1);
assert(B >= 1);
int q = fact(A - 1) * fact(B - 1);
assert(q > 0);
int m = fact(A + B - 2) / q;
assert(m >= 1);
return m;
}
int main(int argc, char const *argv[])
{
int a, b;
//aXb grid
int n = scanf("%d%d", &a, &b);
if (n == 2) {
printf("%d\n", uniquePaths(a, b));
} else {
fprintf(stderr, "invalid input\n");
exit(1);
}
return 0;
}
On my machine, running the program above with intput 10 10
, for instance, will result in
t: t.c:16: int fact(int): Assertion `f >= 1' failed.
Aborted
(I don't know why you get a floating point exception however.)