Advertisement
metalx1000

GCC C code for Basic Time Delay with time.h

Oct 24th, 2017
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2017 Kris Occhipint.
  3.  * http://filmsbykris.com
  4.  *
  5.  * This program is free software: you can redistribute it and/or modify  
  6.  * it under the terms of the GNU General Public License as published by  
  7.  * the Free Software Foundation, version 3.
  8.  *
  9.  * This program is distributed in the hope that it will be useful, but
  10.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12.  * General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17.  
  18. #include<stdio.h>
  19. #include <time.h>
  20.  
  21. int delay(int seconds){
  22.     //Convert seconds to milli seconds
  23.     int milli = 1000 * seconds;
  24.  
  25.     // Get starttime
  26.     clock_t start = clock();
  27.  
  28.     // loop untill time is reached
  29.     while (clock() < start + milli);
  30. }
  31.  
  32. int loop(){
  33.   for (int i=1;i<=10;i++){
  34.     fflush(stdout);
  35.     printf("\rI'm counting: %d", i);
  36.     delay(1000);
  37.   }
  38.   //end with new line
  39.   printf("\n");
  40.   return 0;
  41. }
  42.  
  43. int main(){
  44.   printf("I'm going to count...\n");
  45.   loop();
  46.   return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement