Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to return error with time_t function in C

Tags:

c

time-t

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?

like image 840
Guid Avatar asked Feb 19 '23 21:02

Guid


1 Answers

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.

like image 70
hmjd Avatar answered Feb 21 '23 10:02

hmjd