bhylu.com

Advertisements

C Comments

Document a measurement rule without leaving an unexplained number in the calculation.

How it works

Comments help a maintainer understand why a rule exists. They do not run and cannot make an invalid program safe. A line comment ends at the next newline; a block comment runs from its opening marker to its closing marker and does not nest. Prefer a named constant for a repeated rule, then use a short comment to describe the assumption behind it. If the rule changes, update the code, its comment and the test together. A stale explanation can be more confusing than no explanation.

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 comments-diagnostics.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 · comments-diagnostics.c

#include <stdio.h>

int main(void) {
    /* One control reading is required for each plot. */
    const int control_readings = 1;
    int plots = 3;
    int field_readings = 9;
    int total = field_readings + plots * control_readings;
    printf("Readings including controls: %d\n", total);
    return 0;
}

Expected result

Readings including controls: 12

The comment records a domain rule, while the variable name makes its use visible in the expression. Multiplication calculates the controls needed before addition includes the field readings. This avoids explaining every token in a comment. Compiler warnings and explicit tests remain necessary because a comment cannot check that a requirement was implemented correctly.

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