Click here to Skip to main content
Click here to Skip to main content

A count down timer in c.

By , 10 Jun 2010
 
set the variable count_down_time_in_secs in seconds for desired count down time.
The Code is self explanatory.
 
1 minute = 60 secs
5 minutes = 60x5 secs
30 minutes =60x30 secs
1 hour = 60x60 secs
 

#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 minute is 60, 1 hour is 3600

 
    x_startTime=clock();  // start clock
    time_left=count_down_time_in_secs-x_seconds;   // update timer

	while (time_left>0) 
	{
		x_countTime=clock(); // update timer difference
		x_milliseconds=x_countTime-x_startTime;
		x_seconds=(x_milliseconds/(CLOCKS_PER_SEC))-(x_minutes*60);
		x_minutes=(x_milliseconds/(CLOCKS_PER_SEC))/60;
		x_hours=x_minutes/60;
 

	 
 
		time_left=count_down_time_in_secs-x_seconds; // subtract to get difference 

 
		printf( "\nYou have %d seconds left ( %d ) count down timer by TopCoder 
 
",time_left,count_down_time_in_secs);
	}
 

	printf( "\n\n\nTime's out\n\n\n");
 
return 0;
}
.
.
.
 
TopCoder

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Clark Kent SuperCoder
Sweden Sweden
Member
About me:
I attended programming School and I have a degree in three programming languages.
C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
I am a professional, I've gotten paid to teach coding. I am roughly 20 years old and i have been a teacher's assistant in programming ,
i have held a lecture in Visual basic programming. I have also coached students in C++, Java and Visual basic.

In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.
 
I've written about a dozen small simple applications and games.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 1 This is useless. No cmd line params,...memberSledgeHammer0118 May '10 - 16:04 
Reason for my vote of 1
This is useless. No cmd line params, no threading, no nothing. Just a while loop and hardcoding. Completely useless.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 10 Jun 2010
Article Copyright 2010 by Clark Kent SuperCoder
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid