I'm currently writing a C function that return a time_t value but I need to manage error cases (because this function uses I/O functions) too. Is it correct to use (time_t) -1 as an error indicator?
Using (time_t)-1
is already used by time()
function to report a failure so does not seem an unreasonable choice:
Current calendar time encoded as time_t object on success, (time_t)(-1) on error. If the argument is not NULL, the return value is equal to the value stored in the object pointed to by the argument.
However, if it is necessary for the caller to differentiate between a time related failure or an IO related failure (or specific IO failures) you may consider adding a status type argument to your function that can be used to return additional information about the failure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With