Barr Group FacebookBarr Group TwitterBarr Group LinkedInBarr Group Vimeo

Rules

The following C coding rules relate to the naming of data types:

Rule 5.1.a.) The names of all new data types, including structs, unions, and enums, shall consist only of lowercase characters and internal underscores and end with _t.

Rule 5.1.b.) All new structs, unions, and enums shall be named via a typedef.

Example

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

Reasoning

Type names and variable names are often appropriately similar. For example, a set of timer control registers in a peripheral (as in the example above) calls out to be named timer. 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_t. If necessary, this same type could then also be used to create a shadow copy of the timer registers, say called timer_shadow.

Exceptions

It is not necessary to use typedef with anonymous structures and unions.

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).

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 today!

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.