We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
time_t is defined in time.h as :
typedef unsigned int time_t;
In my opinion it should be defined as :
typedef long time_t;
Am I right ?
No.
The standard says that time_t is an arithmetic type. It also says that integer and floating types are collectively called arithmetic types. So time_t could just as legitimately be a floating type.
Yes, that is the traditional definition, even for 64-bit architectures. However, you shouldn't rely on it, since it isn't required. The whole point of having a named data type time_t is to allow implementations to change the actual data type used.
If you want to print it, you should probably think about
printf("Time: %lu\n",(long)t);
Better make that:
printf("Time: %lu\n",(unsigned long)t);
Or, if available, make use of the extensions to integer types and printf formats in Standard C99.
Slip of the finger. The typecast is correct, but it should be "%ld", and not "%lu". The time_t datatype is signed just because it should be possible to store a negative difference in it.
This is the big reason why the data type isn't just changed from long to unsigned long - a lot of programs would break then. In early 2038, we will see if your microcontrollers have suddenly become 64-bit or if a lot of hardware appliances will suddenly break :)
C99 isn't always available :(
Thank you for all replies ! As I understand function time_t time(time_t *) should return the number of seconds elapsed since january/1970. If this is true an int wouldn't be enough.
Depends on processor. You need a 32-bit data type, and you are talking about ARM, so you are in the clear.
It isn't number of seconds since january 1970 but number of seconds since 00:00 January 1st 1970, or since 1969.