bhylu.com

Advertisements

C Function Prototypes and inline

Declare an operation before main and place a private static inline helper where its contract is visible.

How it works

A prototype lets the compiler check a call before seeing the function body. Headers share declarations across translation units, while definitions normally belong in one source file. C inline rules differ from C++ rules; a small static inline definition in a header is a practical way to give each translation unit its own private helper. inline is not a promise that machine-code calls disappear. Optimisation choices remain with the compiler, so correctness must not depend on whether a call is inlined.

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 headers-inline.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.

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

Example

C17 · headers-inline.c

#include <stdio.h>

static void report_count(int count);

static inline int clamp_depth(int value) {
    return value < 0 ? 0 : (value > 100 ? 100 : value);
}

static void report_count(int count) {
    printf("Records: %d\n", count);
}

int main(void) {
    printf("Clamped: %d\n", clamp_depth(120));
    report_count(3);
    return 0;
}

Expected result

Clamped: 100
Records: 3

The prototype ends with a semicolon and matches the later definition. The inline helper returns a bounded display value without modifying input storage. Clamping is appropriate only when the domain accepts it; validation may instead need to reject an impossible reading. The separate module lesson provides an actual multi-file example with a guarded header and one external definition.

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