Barr Group TwitterBarr Group Vimeo

Rules:

5.1.a. The names of all new data types, including structures, unions, and enumerations, shall consist only of lowercase characters and internal underscores and end with ‘_t’.

5.1.b. All new structures, unions, and enumerations shall be named via a typedef.

5.1.c. The name of all public data types shall be prefixed with their module name and an underscore.

Example:

typedef struct
{
    uint16_t	count;
    uint16_t	max_count;
    uint16_t	_unused;
    uint16_t	control;

} timer_reg_t;

Reasoning: Data type names and variable names are often appropriately similar. For example, a set of timer control registers in a peripheral calls out to be named ‘timer_reg’. To distinguish the structure definition that defines the register layout, it is valuable to create a new type with a distinct name, such as ‘timer_reg_t’. If necessary this same type could then be used to create a shadow copy of the timer registers, say called ‘timer_reg_shadow’.

Enforcement: An automated tool shall scan new or modified source code prior to each build to ensure that the keywords struct, union, and enum are used only within typedef statements or in anonymous declarations. Code reviews shall be used to enforce the naming rules for new types.

Comments:

User-defined typedef names ending with _t should neither begin with int nor with uint.

FYI: The C POSIX standard reserves all names ending in _t:

http://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html

POSIX requires the names ending `_t` be avoided (section B.2.12) http://pubs.opengroup.org/onlinepubs/9699919799/ as it creates a lot of its own type names using this convention which may cause conflicts GCC reserves names ending in `_t` by adding them to the reserved namespace.

Usually safe to use though if using rigorous name prefixes, e.g. `modulename_typename_t`

What’s happening and how it’s done. Get in the know.

Sign Up for Our Newsletter

Receive free how-to articles, industry news, and the latest info on Barr Group webinars and training courses via email. 

To prevent automated spam submissions leave this field empty.