_t
means "type". It's a common convention to use it with typedef
s.
I've been using it for years, for instance, in professional code bases, like this: notice my usage of timer_t
and timer_error_t
:
typedef struct timer_s
{
// When the timer was started
uint32_t start_ticks;
// How long until the timer expires
uint32_t timeout_ticks;
// Whether or not the timer has previously expired without being restarted
bool expired;
} timer_t;
typedef enum timer_error_e
{
// No error
TIMER_ERROR_NONE = 0,
// You passed in a NULL pointer
TIMER_ERROR_NULL_POINTER,
// The timeout value invalid (ex: too large)
TIMER_ERROR_INVALID_TIMEOUT,
} timer_error_t;
However, I recently learned from @Jonathan Leffler's answer that POSIX reserves the _t
suffix for its own types, and I ran into the problem where timer_t
is already defined by POSIX, preventing the above from compiling!
G++ compile-time error:
/usr/include/x86_64-linux-gnu/bits/types/timer_t.h:7:19: note: previous declaration as ‘typedef void* timer_t’
7 | typedef __timer_t timer_t;
| ^~~~~~~
So, I'm switching to this new convention to avoid using the _t
suffix which is reserved by POSIX. I've made the following changes, thereby removing my usage of _t
:
timer_s --> Timer_s
timer_t --> Timer
timer_error_e --> Timer_error_e
timer_error_t --> Timer_error
Here's the final result:
Recommended naming convention for structs and enums to avoid redefining _t
types which are reserved by POSIX
typedef struct Timer_s
{
// When the timer was started
uint32_t start_ticks;
// How long until the timer expires
uint32_t timeout_ticks;
// Whether or not the timer has previously expired without being restarted
bool expired;
} Timer;
typedef enum Timer_error_e
{
// No error
TIMER_ERROR_NONE = 0,
// You passed in a NULL pointer
TIMER_ERROR_NULL_POINTER,
// The timeout value invalid (ex: too large)
TIMER_ERROR_INVALID_TIMEOUT,
} Timer_error;
This new convention avoids the risk of having overlapping usages of _t
.
int_t
defined? If it's always defined asint
, it's not useful; it's much clearer to useint
directly. If it's not always defined asint
(say, if it could belong int
orshort int
), then it's a poorly chosen and confusing name. – Isidoro