Thread: how can i use sleep in my code..

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    how can i use sleep in my code..

    what i need to know is how can i make the numberzz go like
    1 then rest for a sec
    2 then rest for a sec
    3 do the same as above
    4 same....

    now how can i do this in my code....

    #include <stdio.h>

    int main()

    {
    int count;

    for(count=10;count>0;count=count-1)
    printf("%i\n",count);

    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    First:
    > count=count-1

    Usually people use count-- It does the same thing.

    Also, how to sleep depends on the compiler.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If it's a windows compiler, then one option is:

    #include <windows.h>
    .
    .
    Sleep(1000); //Sleep 1 second

  4. #4
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    If you choose not to use <windows.h> and use Sleep(), I believe you can use <dos.h> and use Delay(). It works the same as sleep and is measure in miliseconds, so Sleep(3000) will sleep 3 seconds.

    Example of Sleep for a Simple Progress Looking Bar:

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    void main()
    {
    	int x;
    
    	printf("\n\t[.....]\b\b\b\b\b\b");
    	for (x=5; x>0; x--) 
    	{
    		Sleep(130);
    		printf("-\b");
    		Sleep(130);
    		printf("+\b");
    		Sleep(130);
    		printf("*");
    	}
    
    	printf ("\n\n\t");
    
    }

    enjoy.

  5. #5
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by swoopy
    If it's a windows compiler, then one option is:

    #include <windows.h>
    .
    .
    Sleep(1000); //Sleep 1 second
    ...and on POSIX systems (Linux, UNIX, etc), the argument for sleep() is in seconds:
    Code:
    sleep(1);  /* sleep for one second */
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM