This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

RVCT :time_t

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 ?

Parents
  • 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);

Reply
  • 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);

Children
  • 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 :(