C Reference ยท Concepts and Tools
Writing portable C code
How to write C that builds and behaves the same on different compilers, operating systems and CPUs: fixed-width types, endianness, feature macros and platform checks.
Portable C compiles and gives the same results on different compilers, operating systems and processors. The standard guarantees a lot, but it also leaves many details to the implementation: the size of int, whether plain char is signed, the byte order of integers, the meaning of long. Code that quietly depends on those details works on your machine and breaks on someone else's.
The strategy is to rely on what the standard promises, make the unavoidable assumptions explicit (with static_assert and fixed-width types), and isolate platform-specific code behind a small interface.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Sources of non-portability
| Issue | What to do |
|---|---|
Size of int, long, pointers |
Use <stdint.h> types (int32_t, uint64_t) when the exact width matters, size_t for sizes, ptrdiff_t for differences, uintptr_t to hold a pointer as an integer. long is 32-bit on 64-bit Windows and 64-bit on 64-bit Linux. See Fixed-width types. |
Plain char signedness |
Use unsigned char for bytes and signed char for tiny integers; cast to unsigned char before calling <ctype.h> functions. |
| Byte order | Never write a struct or an integer to a file or socket with fwrite/send. Serialize field by field with shifts, as above. See Endianness. |
| Struct layout and padding | Padding differs per ABI. Do not depend on sizeof of a struct in a file format. See Padding. |
| Shifts and signed overflow | Right-shifting negative values is implementation-defined; signed overflow and shifts by too large a count are undefined. Use unsigned arithmetic for bit work. |
| Evaluation order | Order of function arguments and most subexpressions is unspecified. See Sequence points. |
| Floating-point | Results of float expressions can differ with FMA, x87 extended precision and optimization flags. Compare with tolerances. |
| Text I/O | Line endings (\n versus \r\n) differ; open binary files with "rb"/"wb". File paths use / or \. |
| Locale and encoding | Text encoding of char strings depends on the system; use UTF-8 explicitly where possible. |
printf formats |
Use %zu for size_t, %td for ptrdiff_t, and the PRI* macros of <inttypes.h> for fixed-width types. |
Feature test and platform macros
#if defined(_WIN32) /* Windows (32- and 64-bit) */ #elif defined(__linux__) /* Linux */ #elif defined(__APPLE__) /* macOS and iOS */ #elif defined(__unix__) /* other Unix systems */ #endif #if defined(__GNUC__) || defined(__clang__) /* GCC-compatible compilers: attributes, builtins */ #elif defined(_MSC_VER) /* Microsoft compiler */ #endif
__STDC_VERSION__reports the language standard;__has_includeand__has_c_attributetest features directly. See Predefined macros and __has_include.- POSIX feature macros such as
_POSIX_C_SOURCE 200809Land_GNU_SOURCEmust be defined before the first include; strict-std=c23hides functions likestrdupotherwise. __STDC_NO_VLA__,__STDC_NO_THREADS__,__STDC_NO_ATOMICS__mark optional C11 parts a compiler may omit.CHAR_BITis 8 on all mainstream platforms, but not guaranteed; some DSPs use 16 or 32.
Usage notes
- Isolate platform code. Put system-specific calls (threads, sockets, files, time) in one module behind a small header, with one
.cfile per platform. - Wrap compiler extensions (
__attribute__,__declspec) in macros that expand to nothing elsewhere, or use standard attributes in C23. - Test on more than one compiler and architecture. Build with GCC, Clang and, if relevant, MSVC; run with sanitizers; if possible try a big-endian or 32-bit target through an emulator.
- Turn on warnings (
-Wall -Wextra -Wconversion -Wpedantic) and treat portability warnings as bugs. See Compiler flags. - Strict aliasing: casting a byte buffer to
uint32_t *can be undefined and misaligned; usememcpyas in the first example. See Strict aliasing. - Prefer the standard library where it suffices; reach for POSIX or Win32 only when needed, and then through your wrapper.
- Do not assume ASCII for character arithmetic beyond the digits (
'0'..'9'are guaranteed consecutive; letters are not).