Code:
#include <time.h>
#include <stdio.h>
#include <windows.h>
int main(void)
{
clock_t ticks1, ticks2;
Sleep(265);
ticks1 = clock();
ticks2 = ticks1;
while((ticks2/CLOCKS_PER_SEC-ticks1/CLOCKS_PER_SEC)<1)
ticks2=clock();
printf("Took %ld ticks to wait one second. ticks2 = %ld, ticks1 = %ld\n",ticks2 - ticks1, ticks2, ticks1);
printf("This value should be the same as CLOCKS_PER_SEC which is %ld.\n",CLOCKS_PER_SEC);
return 0;
}
That code is weird, notice the windows Sleep function I put there. When I sleep for 265 miliseconds, ticks1 will be 265, so will ticks2 (lines 10 and 11). This is my output:
Code:
Took 735 ticks to wait one second. ticks2 = 1000, ticks1 = 265
This value should be the same as CLOCKS_PER_SEC which is 1000.
Apparently, the loop breaks when ticks2 is 1000, and it took 735 ticks to wait one second, which is not true because CLOCKS_PER_SEC is 1000 in my computer.
Now, if I do it the way I put it, it works. And if I remove the Sleep() function, it also works. But If the program had some time running, then it doesn't work.