Take the 2-minute tour ×

I'm currently using a explicit cast to unsigned long long and using %llu to print it, but since size_t has the %z specifier, why doesn't clock_t have one?

There isn't even a macro for it. Maybe I can assume that on an x64 system (OS and CPU) size_t is 8 bytes in length (and even in this case, they have provided %z), but what about clock_t?

share|improve this question

5 Answers 5

up vote 5 down vote accepted

Although unsigned long long is currently the largest int possible, a larger one could come out in the future, so it is best to typecast to the largest unsigned integer type possible:

#include <stdint.h>

printf("%ju", (uintmax_t)(clock_t)1);

uintmax_t is guaranteed to have the size of the largest possible integer size on the machine.

uintmax_t and its printf specifier %ju were introduced in c99 and gcc for example implements them.

As @Bastien Léonard mentions for POSIX, ANSI C also says that:

The range and precision of times representable in clock_t and time_t are implementation-defined.

meaning it is not necessarily an integer.

However, since clock, which returns a clock_t type usually has error much greater than 1 clock, and since in theory clock should return a representation of the number of ticks (which should be an integer value) it is reasonable to suppose that that the rounding error won't be noticeable when using a clock_t that comes from a clock at least.

As a bonus, this solves once and for all the question of how to reliably printf integer typedefs (which is unfortunately not the necessarily the case for clock_t).


share|improve this answer
%ju? this prints exactly ju –  Victor Aug 3 '13 at 8:37
@Victor: Are you compiling with gcc -std=c99 (or telling your compiler to use c99)? What is your compiler version? I have just tested it and the following works for me under gcc --version equals gcc (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3: printf( "printf uintmax_t = %ju\n", (uintmax_t)1 );. –  Ciro Santilli Aug 3 '13 at 9:40
I am using Visual Studio 2010 :) –  Victor Aug 3 '13 at 13:30
It seems that c99 is not supported by VS2010. Also read that MS has no plans for implementing it on their compilers (correct me if wrong). I'd stick with C++ for Windows programming. –  Ciro Santilli Aug 3 '13 at 13:47

As far as I know, the way you're doing is the best. Except that clock_t may be a real type:

time_t and clock_t shall be integer or real-floating types.


share|improve this answer
Very well stated, I've forgotten about it possibly being a floating point type. –  Spidey Jul 5 '09 at 3:29
Is there a way to get the floating point type with the largest precision possible and its specifier like can be done for integers with uintmax_t and %ju? That would be the optimal way to go. –  Ciro Santilli Jun 19 '13 at 10:22
@cirosantilli: it seems no –  Ciro Santilli Jun 19 '13 at 12:06

It's probably because clock ticks is not a very well-defined unit. You can convert it to seconds and print it as a double:

time_in_seconds = (double)time_in_clock_ticks / (double)CLOCKS_PER_SEC;
printf("%g seconds", seconds);

The CLOCKS_PER_SEC macro expands to an expression representing the number of clock ticks in a second.

share|improve this answer

The C standard has to accomodate a wide variety of architectures, which makes it impossible to make any further guarantees aside from the fact that the internal clock type is arithmetic.

In most cases, you're interested in time intervals, so I'd convert the difference in clock ticks to milliseconds. An unsigned long is large enough to represent an interval of nearly 50 days even if its 32bit, so it should be large enough for most cases:

clock_t start;
clock_t end;
unsigned long millis = (end - start) * 1000 / CLOCKS_PER_SEC;
share|improve this answer
That's exactly why I can't imagine a macro for clock_t printing format isn't specified in any headers. –  Spidey Jul 5 '09 at 3:30
@Spidey: but what should the output format be if you can't make any guesses on the representation? Remember, it's not specified if clock_t will be an integer or floating point value; if you want to do anything useful, you have to relate it to CLOCKS_PER_SEC, and that's beyond the domain of printf() –  Christoph Jul 5 '09 at 3:39
@Christoph: since clock_t is not defined, CLOCKS_PER_SEC, afaik, is not either, and it is of the same type as clock_t. Since clock_t doesn't usually make it to production, I don't care what type it is and I won't rely on it to make any parsing. But I see the difference in that size_t is some integer type, and clock_t is not (not always), so we're trapped with explicit casting. –  Spidey Jul 6 '09 at 18:08

//One of the way is by using gettimeofday function, one can find difference using this function.

unsigned long  diff(struct timeval second, struct timeval first)


struct timeval  lapsed;

struct timezone tzp;

unsigned long t;

if (first.tv_usec > second.tv_usec) {

second.tv_usec += 1000000;



lapsed.tv_usec = second.tv_usec - first.tv_usec;

lapsed.tv_sec = second.tv_sec - first.tv_sec;

t = lapsed.tv_sec*1000000 + lapsed.tv_usec;

printf("%lu,%lu - %lu,%lu = 

return t;

share|improve this answer

Your Answer


