Sunday, January 29, 2012

Adding Timer in C code

Hello everyone! Hope you are passing great time with coding!!
Today I am here with how to add or insert a timer in your C code to show the time how much your code taking time to execute. Sometimes it becomes important to see time your code taking. Okay let’s do it…
We will discuss with a very simple problem. User input two integers and a code to add them !!!
-----------------------------------------------------------------------------------------------------------------
Inserting timer:

#include <stdio.h>
#include <time.h>  //inserting time.h library function.

int main ( )
{
    int a, b, add;  //a & b two integers and add is their addition.
scanf ( “ %d %d ”, &a, &b ); // taking input.

clock_t start=clock(); // clock starting.
add = a + b; //adding a & b.

printf(“\nAddition of %d & %d is =  %d, a, b, add );
                      //printing result.
printf("\nTime = %.2f secondes\n",(double)(clock()-start)/CLOCKS_PER_SEC); // printing time.
    return 0 ;
-----------------------------------------------------------------------------------------------------------------
Inserting current time:
#include <stdio.h>
#include <time.h>

int main(void)
{
    time_t timer = time(NULL);
    printf("current time is %s", ctime(&timer));
    return 0;
}
-----------------------------------------------------------------------------------------------------------------
Reference:  

No comments:

Post a Comment

Thanks - Jajabor, 2014