Barr Group FacebookBarr Group TwitterBarr Group LinkedInBarr Group Vimeo

Rules

The following C coding rules relate to the declaration and use of fixed-width integers:

Rule 5.2.a.) Whenever the width, in bits or bytes, of an integer value matters in the program, one of the fixed width data types shall be used in place of char, short, int, long, or long long. The signed and unsigned fixed-width integer types shall be as shown in the table below. (Note: If program execution speed is also a consideration, note that the [C99] standard defines a set of “fastest” fixed-width types. For example, uint_fast8_t is the fastest integer type that can hold at least 8 bits of unsigned data.)

Integer WidthSigned TypeUnsigned Type
8 bitsint8_tuint8_t
16 bitsint16_tuint16_t
32 bitsint32_tuint32_t
64 bitsint64_tuint64_t

Rule 5.2.b.) The keywords short and long shall not be used.

Rule 5.2.c.) Use of the keyword char shall be restricted to the declaration of and operations concerning (null-terminated) strings.

Reasoning

The ISO C programming language standard allows implementation-defined widths for char, short, int, long, and long long types, which leads to portability problems. A 1999 update to the language standard did not resolve this original issue, but did introduce the uniform type names shown in the table above, which are defined in the header file <stdint.h>.

Exceptions

In the absence of a C99-compliant compiler, it is acceptable to define the set of fixed width types in the table above as typedefs based on char, short, int, long, and long long. If this is done, use compile-time checks (such as static assertions) to have the compiler flag incorrect type definitions. It is acceptable to use the native types when C Standard Library functions are used—just be careful.

Enforcement

At every build an automated tool shall scan for and flag the use of the keywords short and long, which are not to be used. Compliance with the other rules shall be checked during code reviews.

Comments:

For embedded software developers, many of the most significant improvements to the C programming language made in the 1999 update to the ISO language standard are in the new <stdint.h> header file. Learn the typedef names for these fixed-width integer data types, to make hardware interfacing in C easier.

See the article Portable Fixed-Width Integers in C for more information about this important topic.

I agree that using these standard fixed-width types by including <stdint.h> is a helpful step to create more portable code. Unfortunately, that header file is only available with C99-compliant compilers.

When using other compilers, you can (and probably should) create your own stdint.h header file similar for your environment--rather than replying on non-standard type names from your silicon vendor's compiler. This makes your code less ready for the future.

Note that this coding standard contains a rule that prohibits defining your own header file that shares a file name with a standard header file.

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.