C Reference function clock()

Usage of clock():

clock_t clock ( void );



Return Value:

The number of clock ticks elapsed since the program start. If the function fails then it will return a value of -1. The clock_t type is defined in ctime.h.


The function returns the number of clock ticks elapsed since the start of the program.

The macro constant expression CLOCKS_PER_SEC specifies the relation between a clock tick and a second (clock ticks per second).

Note:The initial moment of reference used by clock as the beginning of the program on different platforms.

Source code example of clock():

	#include <stdio.h>
	#include <time.h>

	void sec_wait ( int sec ) {
                clock_t wait_till_end;
                wait_till_end = clock () + sec * CLOCKS_PER_SEC ;
                while (clock() < wait_till_end) {}

        int main () {
                int i;
                printf ("Starting countdown...\n");
                for (i=10; i>0; i--) {
                         printf ("%d\n",i);
                         sec_wait (1);
                         if ( i == 3 )
                                printf ("Engine started...\n");
               printf ("and lift off....\n");
               return 0;

Output example of the program above:

	Starting countdown...
	Engine started...
	And lift off....

This entry was posted in C Reference time.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 2 responses to “C Reference function clock()”

Why not let us know what you think by adding your own comment!

  1. George H on May 16th, 2011:

    The clock() code example has stuff to the right of the ‘while’ (underlined below):

    while (clock() < wait_till_end) {} } int main


    I like your site.
    — George

  2. admin on May 17th, 2011:

    @ George H: Nice catch, yes it was a formatting error. Changed the source code example and now the website is displaying the right format. Thx!