I thought up a way to do proper templates in C89, probably K&C variety too if they support #include, #define, #undef, #error & defined() in some form or way. There's also a loop version that I derived this from (because I initially thought of the loop version then realised the template application after).

Here's a link to some code that chooses which template to use for emulating an integer, the templates themselves probably have a bug or 2 due to being converted from 1 of my existing implementations but my code became overall smaller and easier to manage for emulating integers so those bugs will be ironed out soon enough.

include/paw/pawcls.h * e74aef09e57a87462e7911aab4da325294472e4a * Lee Shallis / Dragonbuilder * GitLab

As for the loop version it can only do 1 scope for now and uses __COUNTER__ to define an index of which to use directly or multiply against another value to get the intended value for that loop, so something like this:

Code:
/* loop.h */
#define BEGAN __COUNTER__
#define N BEGAN
#define I (N - BEGAN)
#include "_loop.h"
#undef CMP
#undef INC
#undef I
#undef N
#undef BEGAN

/* _loop.h */
#if CMP
#include INC
#undef N
#define N __COUNTER__
#include "_loop.h"
#endif
It's a smidge more complicated for error checking but that's the core of it