Barr Group TwitterBarr Group Vimeo

Rules:

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.

Integer Width Signed Type Unsigned Type
8 bits int8_t uint8_t
16 bits int16_t uint16_t
32 bits int32_t uint32_t
64 bits int64_t uint64_t

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

5.2.c. Use of the keyword char shall be restricted to the declaration of and operations concerning strings.

Example: See Appendix D.

Reasoning: 

The C90 standard purposefully allowed for implementation-defined widths for char, short, int, long, and long long types, which has resulted in code portability problems. The C99 standard did not resolve this but did introduce the type names shown in the table, which are defined in the stdint.h header file.

See also Portable Fixed-Width Integers in C:

        barrgroup.com/embedded-systems/how-to/c-fixed-width-integers-c99

In the absence of a C99-compatible compiler, it is acceptable to define the set of fixed-width types in the table above as typedefs built from underlying types. If this is necessary, be sure to use compile-time checking (e.g., static assertions).

Enforcement: 

At every build an automated tool shall flag any use of keywords short or long. 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

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.