December 9th, 2024

C has its limits. If you know where to look

The article highlights the evolution of C programming, focusing on data type portability challenges, the standardization through ANSI-C and stdint.h, and the importance of staying updated with programming developments.

Read original articleLink Icon
C has its limits. If you know where to look

The article discusses the evolution of the C programming language, particularly focusing on the challenges of data type portability across different systems. Initially, C lacked standardization, leading to inconsistencies in data type sizes, such as the "int" type, which could vary from 16 bits to 32 bits depending on the architecture. The author shares personal experiences with various compilers and how the introduction of ANSI-C and later the stdint.h header file improved the situation by providing standardized data types like uint32_t and int32_t. The article emphasizes the importance of using these standardized types and the limits defined in limits.h and stdint.h to ensure code compatibility across different systems. The author reflects on their learning journey and the realization that newer features in C, such as the defines for maximum and minimum values of standard integer types, can enhance coding practices. The piece concludes with a note on the significance of staying updated with programming language developments.

- C programming has evolved to address portability issues with data types.

- The introduction of ANSI-C and stdint.h has standardized data types across systems.

- Using defined limits helps ensure code compatibility on different architectures.

- Staying informed about updates in programming languages is crucial for effective coding.

- The author shares personal experiences to illustrate the learning process in programming.

Link Icon 4 comments
By @eqvinox - 4 months
This should be updated for ISO C23's _BitInt(N), which is not equivalent to intN_t (the type promotion rules differ!)

(It also supports "uncommon" values of N)

By @pajko - 4 months
Never use fixed-size integers unless you are communicating with hardware, implementing some protocol or manipulating binary files.

If some code shared between 8/16-bit and 32/64-bit architectures requires an a least 16-bit type, go for "int". If it requires an at least 32-bit type, go for "long".

Also fixed-size integers shall not be printed with standard directives like %d or %u, but the PRId32, PRIu32, PRIx32, etc... macros shall be used from inttypes.h.

By @gregw2 - 4 months
FYI, there was a semi-related discussion on architectures and bit widths on HN a couple months ago: https://news.ycombinator.com/item?id=41773559