bhylu.com

Advertisements

C Portability

Separate required language guarantees from byte widths, operating-system services and compiler extensions.

How it works

Portable code makes its assumptions visible. C17 is the teaching baseline here, not a claim about the newest standard. C23 adds facilities and changes details that require a suitable compiler and library; do not enable a newer mode and assume every target supports the same features. Some C11 facilities are optional in C17 implementations, including threads and variable-length arrays. This course uses fixed arrays and conditional capability notes instead of making those facilities prerequisites for every learner.

Run it locally

Use a hosted C17 compiler in a separate lab folder. The command assumes a driver named cc; substitute gcc, clang or zig cc as appropriate. C does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed for these C programs.

cc -std=c17 -Wall -Wextra -Wpedantic -Werror portability.c -o lesson

On Windows, use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Math-library linking differs by toolchain; GCC on many Unix systems needs -lm after the source.

Environment boundary: Hosted C17 on an eight-bit-byte target. The final output is a target property, not a universal C guarantee.

Download the complete C source. File-writing labs must run in an empty private directory, away from existing reports.

Example

C17 · portability.c

#include <stdio.h>
#include <limits.h>

int main(void) {
    printf("Hosted implementation: %d\n", __STDC_HOSTED__ == 1);
    printf("C17 or later mode: %d\n", __STDC_VERSION__ >= 201710L);
    printf("Eight-bit byte target: %d\n", CHAR_BIT == 8);
    return 0;
}

Expected result

Hosted implementation: 1
C17 or later mode: 1
Eight-bit byte target: 1

The first two checks describe the selected review mode. The final result is an explicitly documented property of the review target, not a universal guarantee. The binary-format lab checks it before using an octet-based representation. Windows paths, terminal encodings and POSIX networking are outside the portable core language; use platform adapters when an application needs them.

For language and library rules, consult the WG14 C working group and its public C11 committee draft, which describes the core facilities used here; C17 incorporates corrections. Check your compiler and runtime documentation for implementation details.

Advertisements