C ABI

The experimental C ABI represents a parsed PUQ quantity as an opaque handle. Evaluate an expression, convert it to target units, format the result, and release every owned handle.

Basic examples

Evaluate and print a quantity. Link these C examples against snt-c:

#include <snt/c/puq.h>
#include <stdio.h>

int main(void) {
    snt_puq_quantity* energy = NULL;
    snt_puq_error error = {0};
    char text[128];
    int failed = snt_puq_quantity_eval("35*eV", &energy, &error) ||
                 snt_puq_quantity_format(energy, text, sizeof(text), &error);
    if (failed) fprintf(stderr, "%s\n", error.message);
    else puts(text);
    snt_puq_quantity_free(energy);
    return failed;
}

Convert a quantity and release both owned handles:

#include <snt/c/puq.h>
#include <stdio.h>

int main(void) {
    snt_puq_quantity* length = NULL;
    snt_puq_quantity* converted = NULL;
    snt_puq_error error = {0};
    char text[128];
    int failed = snt_puq_quantity_eval("2.5*m", &length, &error) ||
                 snt_puq_quantity_convert(length, "cm", &converted, &error) ||
                 snt_puq_quantity_format(converted, text, sizeof(text), &error);
    if (failed) fprintf(stderr, "%s\n", error.message);
    else puts(text);  /* 250*cm */
    snt_puq_quantity_free(converted);
    snt_puq_quantity_free(length);
    return failed;
}

Common workflows

Task

Call

Result

Parse an expression

snt_puq_quantity_eval

Owned snt_puq_quantity*

Convert a quantity

snt_puq_quantity_convert

New owned snt_puq_quantity*

Read a value as text

snt_puq_quantity_format

Caller-owned buffer

Release a handle

snt_puq_quantity_free

No result; accepts NULL

Report an error

snt_puq_error

code and borrowed message

The normal chain is evaluate, convert, format, then free both quantity handles. Every created quantity is independently owned, including the converted one.

See the C binding guide for build and ownership rules and C ABI: PUQ for the function reference.

Current limitations and differences

Compared with the canonical C++ implementation:

  • The ABI exposes evaluation, conversion, and formatting through opaque handles. It has no typed arithmetic, array access, correlation, Monte Carlo, unit-listing, or custom unit-system registration entry points.

  • PUEL input can carry uncertainties, which conversion retains. Returned values are accessed as formatted text rather than native Quantity records.

  • Callers allocate output buffers and free each handle explicitly. C++ RAII and exceptions are replaced by return codes and snt_puq_error.

Binding reference

This is a lookup index, not a sequence of steps. Use Common workflows above to choose a task, then find the exact C name below. Calls that return an int use zero for success; output handles and buffers are passed by pointer. See the linked C API reference for complete argument details.

  • snt_puq_quantity_eval(expression, &quantity, &error) evaluates a PUEL expression and returns an owned quantity.

  • snt_puq_quantity_convert(quantity, units, &converted, &error) creates a converted quantity; the source handle remains owned by the caller.

  • snt_puq_quantity_format(quantity, buffer, capacity, &error) writes the textual form into a caller-owned buffer.

  • snt_puq_quantity_free(quantity) releases a quantity and accepts NULL.

  • snt_puq_error carries code and a library-owned message.