mina86 
02112013 07:15 AM 
If you use (unsigned) (long (long)) integers, then you can just use % operator like you did in your original code (except for the bug millgates pointed).
From man pages:  The remainder() function computes the remainder of dividing x by y. The return value is xn*y, where n is the value x / y, rounded to the nearest integer. If the absolute value of xn*y is 0.5, n is chosen to be even.
 The fmod() function computes the floatingpoint remainder of dividing x by y. The return value is x  n * y, where n is the quotient of x / y, rounded toward zero to an integer.
So the difference is in a way values are rounded.
Code:
$ cat a.c
#include <fenv.h>
#include <math.h>
#include <stdio.h>
int main(void) {
double a = 2.0, b = 3.0, c = a / b, d;
printf("%g / %g = %g\n\n", a, b, c);
fesetround(FE_TOWARDZERO); d = nearbyint(c);
printf("%g / %g = %g (rounded towards zero)\n", a, b, d);
printf("%g  %g * %g = %g\n", a, d, b, a  d * b);
printf("fmod(%g, %g) = %g\n\n", a, b, fmod(a, b));
fesetround(FE_TONEAREST); d = nearbyint(c);
printf("%g / %g = %g (rounded towards nearest)\n", a, b, d);
printf("%g  %g * %g = %g\n", a, d, b, a  d * b);
printf("remainder(%g, %g) = %g\n", a, b, remainder(a, b));
return 0;
}
$ gcc o a a.c lm && ./a
2 / 3 = 0.666667
2 / 3 = 0 (rounded towards zero)
2  0 * 3 = 2
fmod(2, 3) = 2
2 / 3 = 1 (rounded towards nearest)
2  1 * 3 = 1
remainder(2, 3) = 1
$
