Now, I am learning C programming, I stumbled upon a countdown code on 'Codeproject.com' and decided to run it and analyse it to learn. However, the output Countdown repeats each number thousands of times before moving to the next. Please I need help understanding why this is. The code is shown below:
#include <stdio.h>
#include <time.h>
int main()
{
unsigned int x_hours = 0;
unsigned int x_minutes = 0;
unsigned int x_seconds = 0;
unsigned int x_milliseconds = 0;
unsigned int totaltime = 0, count_down_time_in_secs = 0, time_left=0;
clock_t x_startTime, x_countTime;
count_down_time_in_secs = 10; // 1 min is 60
x_startTime = clock();
time_left = count_down_time_in_secs-x_seconds; //update timer
while (time_left>0)
{
x_countTime = clock();
x_milliseconds = x_countTime-x_startTime;
x_seconds=(x_milliseconds/(CLOCKS_PER_SEC))-(x_hours*60);
x_minutes=(x_milliseconds/(CLOCKS_PER_SEC))/60;
x_hours=x_minutes/60;
time_left = count_down_time_in_secs-x_seconds;
printf("\nyou have %d seconds left", time_left, count_down_time_in_secs);
}
printf("\n\n\nTime's out\n\n\n");
return 0;
}
Just add a line to print out x_milliseconds inside your loop and it will become obvious what is happening. i.e. you are executing your loop thousands of times a second.