How to put a delay in execution in a C program?

Discussion in 'Programming' started by ionicsachin, Sep 11, 2009.

Thread Status:
Not open for further replies.
  1. ionicsachin

    ionicsachin New Member

    Joined:
    Apr 16, 2005
    Messages:
    592
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Hyderabad
    Hiii,
    I wanted to know how to put a delay of milliseconds or seconds at any step in a C program. On googling i came across sleep(), but its not a standard C funtion. Any suggestions?
     
  2. lucifer_is_back

    lucifer_is_back New Member

    Joined:
    Oct 17, 2008
    Messages:
    124
    Likes Received:
    0
    Trophy Points:
    0
    AFAIK there is no such standard function for putting delay
    u have to be platform specific in this case
     
  3. clmlbx

    clmlbx Active Member

    Joined:
    Aug 26, 2006
    Messages:
    2,110
    Likes Received:
    11
    Trophy Points:
    38
    Location:
    Indore
    #include "stdio.h"
    #include "time.h"
    int main(void){
    struct timespec t;
    t.tv_sec = 0;
    t.tv_nsec = 500000000;
    while(1){
    printf("Hello world!\n");
    nanosleep(&t,NULL);
    }
    return 0;
    }


    NOT THAT GOOD IN C .. This I have copied from somewhere else
     
  4. xmentrio

    xmentrio New Member

    Joined:
    Aug 21, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Mumbai
    There is also delay(int msec) function in dos.h.
    Try using it. It works.
    -----------------------------------------
    Posted again:
    -----------------------------------------
    There is also delay(int msec) function in dos.h.
    Try using it. It works.
     
    Last edited: Sep 11, 2009
  5. pr.itdude

    pr.itdude tHe nEw gEEk......ITian

    Joined:
    Oct 25, 2008
    Messages:
    357
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    New Delhi
    ^^ +1
    just include <dos.h>

    and u can use this function "delay()" anywhere in any standard C program.
     
  6. lucifer_is_back

    lucifer_is_back New Member

    Joined:
    Oct 17, 2008
    Messages:
    124
    Likes Received:
    0
    Trophy Points:
    0
    :shock::-o
     
  7. ashik.thomas

    ashik.thomas New Member

    Joined:
    Aug 26, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hai,
    You can use delay(int n) defined in dos.h
     
  8. vamsi360

    vamsi360 Always confused

    Joined:
    May 10, 2008
    Messages:
    348
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Garden City
    are you looking in Debugginh case?

    using Visual C++ you have more ease with debugging. Or use Geany and Mingw in Windows or Geany and gcc in Linux.
     
  9. anuvrat_parashar

    anuvrat_parashar paralinux

    Joined:
    Jul 16, 2008
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Noida
    I think this one will only work for Turbo C/C++ compiler.
     
  10. ruturaj3

    ruturaj3 New Member

    Joined:
    Feb 11, 2007
    Messages:
    226
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Mumbai
    yes, delay() function works well, u can used it in drawing animation or displaying msg, like calculating result and then after delay display d result.
    Instead of directly printing output.
     
Thread Status:
Not open for further replies.

Share This Page