How to Code Delay Timer in C

There are many functional programming languages that are currently used by developers and engineers; C language is one of them. C is a very popular programming language due to its flexibility and functionality that it allows. This programming language is used to build variety of applications which we use everyday. When you start coding in C language there are certain situations where you want your C code to pause for some time or introduce a delay. You can introduce a delay timer in C using number of different ways but the following step by step guide will show you very simple techniques to accomplish this.

Instructions

  • 1

    The first technique uses a sleep(int t_in_ms) technique to introduce a timer. Sleep(int t_in_ms) is built in API in C and it is defined in time.h file.

    To include time.h in your code, write following line in your C file where you are including libraries.

    #include

    Now use sleep (int t_in_ms) function in your code where timer is desired.

    For example sleep(1000) will introduce a delay of one second.

    Example code:

    #include

    #include

    int void main()

    {

    int a;

    for(a=0;a<9;a++){

    printf(ā€œ%a\nā€, a);

    sleep(1000);

    }

    Return 0;

    }

  • 2

    The other simple technique is to use a bigger for loop for a time delay. It is not a very efficient way to introduce a delay timer. But it is easy to understand and easy to use.

    For example to introduce a simple delay, add following line of code where desires:

    for(int k=0; k<=2000; k++);

  • 3

    If you are new to programming in C then you might want to try these steps slowly. Be sure to not skip any part otherwise you might have problems. Go online and find some websites or forums that deal with issues in C programming language. There is a large selection of sites that are completely dedicated to programming C language that you can use for some help with adding a delay timer. Post your own questions on a forum for some expert help in the subject.

  • 4

    You can also read a wide variety of books that are available regarding programming in C language. There are many decent books that will show you different tips and tricks to adding a delay timer in C programming language. Take some time and research or study the C programming language so that it becomes easy for you to understand the coding process and the various steps that are involved.

Leave a Reply

Your email address will not be published. Required fields are marked *


× six = 30