Writing Portable C Code: Preprocessor Directives, Data Types, and GNU Autotools
Preamble
Writing a portable C program turns out to be more of a nightmare than imagined. C is notorious for being platform dependent, yet most of the software that runs across wildly different systems is written in this language. The secret lies in understanding what varies between platforms — data type widths, byte ordering, memory alignment, available system calls and library functions — and using the right tools to handle those differences at build time. In this post, we'll walk through the C compilation pipeline, explore how preprocessor directives enable conditional compilation, examine data type portability pitfalls like endianness and struct padding, and introduce GNU Autotools as a build system for portable projects. This is the third in a three-part series. The first post covers UNIX terminal devices, and the second explains how the shell executes programs.
