Related Tags

c

# What is CLOCKS_PER_SEC in C?

Samia Ishaque

## Overview

CLOCKS_PER_SEC is a macro in C language and is defined in the <time.h> header file. It is an expression of type, ﻿as shown below﻿:

 clock_t clock(void)


CLOCKS_PER_SEC defines the number of clock ticks per second for a particular machine. The number of seconds elapsed since ﻿the launch of a program can be calculated with the following formula:

$seconds = \frac{clock ticks}{CLOCKS\_PER\_SEC}$

## Example

CLOCKS_PER_SEC is always used in combination with the clock() function. Below is an example of how you can use the macro CLOCKS_PER_SEC to calculate ﻿the seconds ﻿that have passed since a program was launched:

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

int main(){
clock_t start, end;
int i;

start = clock();

printf("Clock ticks at starting time: %ld\n", start);
for(i=0; i< 1000000000; i++){
}
end = clock();
printf("Clock ticks at end time: %ld\n", end);

printf("CLOCKS_PER_SEC: %ld\n", CLOCKS_PER_SEC);
printf("The duration in seconds since the program was launched: %ld\n", (end-start)/CLOCKS_PER_SEC);

return 0;
}

RELATED TAGS

c

CONTRIBUTOR

Samia Ishaque