user2219847
user2219847

Reputation:

execute program for every 1min

I wanted to execute simple c program that call function every 1 minute.Please help me in coding.

#include<stdio.h>
main()
{
    printf("hello");
    fun1();
    printf("welcome");
    delay(1000);

}
void  fun1()
{
    printf("fun1 is called");

    delay(10000);
}

void delay(int k)
{

    for(i=0;i<k;i++)
    {}
}

output i wanted in format:

hello
welcome

Each 10 time after 10 statement it should print fun1 is calledthen it should continue printing hello welcome anoter 10 times

Upvotes: 1

Views: 865

Answers (1)

mariano
mariano

Reputation: 1367

You can use the sleep function. It will delay execution for a given amount of time. Here and here you have some more resources and examples.

Took the example from Mr. Anon in the first SO link I posted (specific to Windows):

#include <windows.h>
#include <stdio.h>

int main() {
    printf( "starting to sleep...\n" );
    Sleep( 3000 );   // sleep three seconds
    printf( "sleep ended\n" );
}

Indication is to watch out for the uppercase S.

Good luck!

Upvotes: 3

Related Questions