These lines of code:
long duration;
duration = (long)(500*g_sampleTimeInSeconds) / 60;
printf("Memory will fill up after %d minutes\n", duration);
are producing a negative number when g_sampleTimeInSeconds is greater than 65. When g_sampleTimeInSeconds = 65 the printed number is 541 but when it is 66 the printed number is -542 rather then the correct number of 550. This is also the case when g_sampleTimeInSeconds is replaced with hardcoded values.
This code is compiled with the XC8 compiler to run on Microchip PICs
UPDATE:
g_sampleTimeInSeconds is an 16 bit int
Is this a 16-bit compiler? My guess is that this is a integer conversion issue. where you are exceeding the size of a 16 bit integer, which is being treated as a two's complement number.
So, in your example, you are casting the results of the calculation to a long, after the calculation takes place. Assuming that g_sampleTimeInSeconds is a signed 16-bit int, then
500*66 = 33000, which is larger than the largest positive value of 32767 allowed for a signed 16 bit number. This causes an overflow, and the intermediate value is calculated as 33000 - 65536 = -32536. -32536/60 = -542.xxx.
So, you need to either cast the values within the expression, or change the data type of g_sampleTimeInSeconds to a long.