The long
type is an integer type.
The problem is that you're trying to store a result which you want to be a float
(or a double
) in a long
.
There are two problems here:
1- Your operands are never cast into float
/double
(implicit cast by the compiler), so all the intermediate operation returns an int
or a long
.
2- Your result is a long
: even if your operands are float
/double
, your result will be cast to long
.
What you have to do: use double
variables.
double divider = 1000000.0;
double elapsed = ((t2.tv_sec - t1.tv_sec) * 1000000.0) + (t2.tv_usec - t1.tv_usec);
elapsed = (elapsed/divider);
printf("Time in seconds: %5.5f seconds\n", elapsed);