Search code examples
ctime.h

How to get the current time in milliseconds in C Programming


Possible Duplicate:
How to measure time in milliseconds using ANSI C?
How can I get the Windows system time with millisecond resolution?

We want to calculate the time which a player have taken to finish the game. But with time.h we could only calculate in seconds. but that is not exact. Is it possible to get the time in milliseconds? and what is the %? to printf?


Solution

  • quick answer

    #include<stdio.h>   
    #include<time.h>   
    
    int main()   
    {   
        clock_t t1, t2;  
        t1 = clock();   
        int i;
        for(i = 0; i < 1000000; i++)   
        {   
            int x = 90;  
        }   
    
        t2 = clock();   
    
        float diff = ((float)(t2 - t1) / 1000000.0F ) * 1000;   
        printf("%f",diff);   
    
        return 0;   
    }