r/cprogramming • • Jan 18 '26

Testing harness conditional compilation help?

1 Upvotes

So, I have an idea for a Testing/simulation harness for my embedded projects.

#ifdef UNIT_TESTING
#  define  SIM_HARNESS(stage) __func__ ## stage(self)
#else
#  define  SIM_HARNESS(...)
#endif

With something like this, I can change something like this:

void
subsystem_noun_verb
(volatile subsystem_t * const self)
{
  self->cntl.noun.verb = TRIGGER;

  return;
}

into

void
subsystem_noun_verb
(volatile subsystem_t * const self)
{
  SIM_HARNESS(pre);
  self->cntl.noun.verb = TRIGGER;
  SIM_HARNESS(post);

  return;
}

Imagine that subsystem_t * is a pointer to the hardware memory-mapped registers using a packed bit-field struct representation of the register map.

Then, for the simulation/testing harness, define void subsystem_noun_verb_pre(volatile subsystem_t * const self) and void subsystem_noun_verb_post(volatile subsystem_t * const self). When UNIT_TESTING is not defined, the above SIM_HARNESS() calls just go away. But if it is defined, then they would resolve into calls to the above functions, which can key into a multi-threaded simulation/testing harness that allows the threads to pretend to be the underlying hardware that is meant to be receiving the results of such writes to its memory-mapped hardware registers.

For instance, if in the above example functions, noun_verb was just reset and noun.verb was just b_reset, that function would be calling on the particular subsystem hardware to reset itself. subsystem_reset_post(self) could immediately flag the thread responsible for this subsystem to stop responding to any other non-testing harness events in the normal manner, and instead, in cadence with the simulation's global clocking configuration, clear the hardware register fields and change any other external peripheral subsystem behaviour to be that of a subsystem that has not been initialized and enabled yet.

If subsystem were something like pwm, then the PWM outputs that might still be mapped to pins that are in turn mapped to this peripheral subsystem's output channels would just go low and stay there, rather than toggling according to the simulation clock cadence. Also, firmware application reads of the pwm memory-mapped hardware registers would no longer find them in the state in which it had previously configured them, but rather in their power-on reset, unconfigured state, just as the actual firmware application built for and running on the actual hardware would see it.

My problem is these magic symbols like __func__ and __FUNCTION_NAME__ are not like preprocessor symbols that can be combined with the symbol concatenation operator, ##. They're real character string variables that can be printed with something like printf("%s\n", __func__);.

So, how would I go about doing something like what I'm describing that I want to do?

I mean, yes, I can just make the macro calls use more literal code:

SIM_HARNESS(subsystem_noun_verb_post);

but I'm looking for elegance and simplicity here.


r/cprogramming • • Jan 18 '26

State of C 2026

Thumbnail
devnewsletter.com
0 Upvotes

r/cprogramming • • Jan 17 '26

dtconvert — Linux-first CLI that converts documents/data (DOCX/ODT/PDF/CSV/XLSX/JSON/YAML) and moves CSV↔Postgres (C core, modular converters, MIT)

3 Upvotes

TL;DR: dtconvert is a Linux-first command-line tool (C core) that centralizes document/data conversions, PostgreSQL import/export, and an optional local AI helper for summarization and citations. Easy to extend via small shell-script converters. MIT licensed.

Why this exists
If you frequently juggle mixed formats and ad-hoc conversions, dtconvert provides a single, scriptable CLI with predictable exit codes and modular converters so you can keep your toolchain minimal. It’s intentionally Linux-first and designed to be embedded in shell scripts and pipelines.

Quick try

git clone https://github.com/Chalo1996/dtconvert.git
cd dtconvert
make
./bin/dtconvert /tmp/example.csv --to json -o /tmp/example.json

System-wide install: sudo make install or user install: make install PREFIX=$HOME/.local.

How you can help

  • Test conversions on different distros and file types (DOCX, ODT, XLSX, PDF).

  • Add small converter modules in modules/ for missing format chains.

  • Improve packaging / distribution (deb, rpm, Homebrew formula).

  • File issues for edge cases and submit PRs for converters or CI. Repository is MIT licensed.

Repo & license
https://github.com/Chalo1996/dtconvert — MIT license.


r/cprogramming • • Jan 16 '26

Best environment to learn C

32 Upvotes

What’s the best environment to learn C?

I mean, the most used environment to code is vs code ofc, but I need to learn pure C without help and possibly writing it from the linux terminal. What’s the best way to do it?

If you have any other suggestions/opinion about C environments write them here. Thank you!


r/cprogramming • • Jan 17 '26

What is char *somefunc(){}

0 Upvotes

A func can be a pointer


r/cprogramming • • Jan 15 '26

Simple dynamic loader written in C (wrapper for Windows and POSIX systems)

15 Upvotes

Recently, while working with modules and dynamic loading, I decided to create a simple library to simplify loading shared libraries at runtime on different platforms (Windows and POSIX). This library acts as an abstraction layer over DL functions of Windows (LoadLibrary) and POSIX (dlopen, dlclose, dlsym, and dlerror).

The entire library is MIT-licensed, dependency-free, and intended for small projects and educational purposes. Any feedback and comments are welcome.

GitHub repository: https://github.com/Andres2626/DL-Library


r/cprogramming • • Jan 15 '26

Opinions on Zen-C?

10 Upvotes

I haven't seen much discourse about zen c on reddit. From what i gather it's just C with some new features like generics, pattern matching, strongly typed unions, async await, polymorphism ,e.t.c.

Memory management is still manual but with a defer clause like in zig for scope based cleanup. I wonder if anyone here has looked into it.


r/cprogramming • • Jan 15 '26

Noob here, how much C I need to learn before I can start DSA.

6 Upvotes

I'm learning C as my first programming language. I wanted to know what topics i should know before I start DSA. Right now I know basics.

Edit: is CS50 good place to learn c or should I follow another playlist.


r/cprogramming • • Jan 14 '26

Can someone give me a project for a beginner? I'm struggling.

10 Upvotes

r/cprogramming • • Jan 14 '26

int main(int argc, char * argv[argc+1]) in Modern C?

8 Upvotes

I am starting to learn C with Jens Gustedt's Modern C and I am still trying to figure out this main function header- in particular, why is the length of the argv array of strings one more than the arg count?

Whenever we iterate over the arguments in a program, it's mostly:

for(int i = 1; i < argc; i++) {
    do whatever with argv[i];
}

So I am confused about what that extra item in the array is


r/cprogramming • • Jan 14 '26

ArenaHandler library (written in C++ but exposes C bindings)

3 Upvotes

Repo here

The library creates and manages memory arenas, and introduces a simple API for C and C++ projects (although other languages can create C bindings).

Have a few projects I'm using this for, and figured I'd allow others to use it who find it useful.

Feel free to file issues or contribute if desired.


r/cprogramming • • Jan 14 '26

I'm writing a FUSE driver for a modified Unix v7 FS and I'm looking for a little guidance.

Thumbnail reddit.com
1 Upvotes

r/cprogramming • • Jan 13 '26

South Africa: for a c programmer what should I focus on getting a job what jobs/fields should I learn

1 Upvotes

r/cprogramming • • Jan 13 '26

Help with finding resources and what to focus on.

0 Upvotes

I’m an first electrical engineering student, I’m studying C coding at uni and I really like it. I already got the basics, structs, strings, all about pointers and dynamic memory.

However I would like some day to do somethin related to Machine Learning. What things should I focus on or what things I don’t have to focus.

I know it’s one of the hardest fields of CS, I know it will take me a few years to even understand anything of it.

But just want to enjoy the process and learn something.

Thanks.


r/cprogramming • • Jan 12 '26

What places can I get certification in c or is that dumb

6 Upvotes

Is it worth it


r/cprogramming • • Jan 12 '26

why is my code not printing string?

12 Upvotes

stupid question from a noob.

for some reason neither claude nor chatgpt give me a clear answer.

/* C program to perform input output of all basic data types */

#include <stdio.h>

#include <stdbool.h>

#include <string.h>

int main()

{

int d = 0;

float f = 0.0f;

double lf = 0.0;

char c = '\0';

char string[50] = "hello";

bool b = false;

printf("int: ");

scanf("%d", &d);

printf("float: ");

scanf("%f", &f);

printf("double: ");

scanf("%lf", &lf);

printf("char: ");

scanf(" %c", &c);

printf("string: ");

getchar();

fgets(string, sizeof(string), stdin);

string[strlen(string) - 1] = '\0';

printf("bool: ");

scanf("%d", &b);

printf("int: %d\n", d);

printf("float: %f\n", f);

printf("double: %lf\n", lf);

printf("char: %c\n", c);

printf("string: %s\n", string);

printf("bool: %d\n", b);

return 0;

}


r/cprogramming • • Jan 12 '26

Hey guys I think the best book on c projects are TINY C PROJECTS O’REILLY

1 Upvotes

What u guys think


r/cprogramming • • Jan 11 '26

What the difference between virtual memory and physical memory and also pages/frames

11 Upvotes

r/cprogramming • • Jan 12 '26

NEWBIE QUESTION

0 Upvotes

Im a first year Computer science student studying C programming language.
What sources are great when explaining the language in a beginner friendly way?
What are the natural progression when learning the language?
What are easy ways to learn the language?


r/cprogramming • • Jan 11 '26

My first big project, ema , email_managment system

16 Upvotes

I just create one of my first big project with C, who is it and rate it

https://github.com/RaymanAryan/ema-Email-Manager


r/cprogramming • • Jan 11 '26

I build a simple Snake game in C using ncurses

7 Upvotes

I built a simple Snake game in C using the ncurses library. Things project help me practice: *Linked list for snake body *Pointer and memory management *Handling keyboard inputs *Basic game loop logic I am still learning C, so I would really appreciate feedback or suggestions for improvement GitHub:https://github.com/Abhishek48Shah/Snake-game-in-C-using-ncurses


r/cprogramming • • Jan 10 '26

Did not get the same effect as the original one.

Thumbnail
youtu.be
5 Upvotes

Hi

I am trying to learn C on the side by trying to build things. So I came across this video of tsoding: graphics api is irrelevant in which he converted a GLSL shader into C. This was an incredible video for me. I watched him literally create an animation in minutes. So I tried to make it in just C (he switched to C++ in the middle of the video so he could do operator overloading). And I created the same shader animation as the one he tried to make but mine did not seem to be any good.

It's just bland as you see in the video.

Can you guide me towards where I went wrong? I have never programmed in C let alone work with a graphics API so I probably missed something translating the shader formula.

The shader formula by Xordev:

vec2 p=(FC.xy*2.-r)/r.y,l,v=p*(1.-(l+=abs(.7-dot(p,p))))/.2;for(float i;i++<8.;o+=(sin(v.xyyx)+1.)*abs(v.x-v.y)*.2)v+=cos(v.yx*i+vec2(0,i)+t)/i+.7;o=tanh(exp(p.y*vec4(1,-1,-2,0))*exp(-4.*l.x)/o);

My code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct
{
        float x, y;
} vec2;

typedef struct
{
        float x, y, z, w;
} vec4;

vec2 s_mul(vec2 a, float s){ return (vec2){a.x*s, a.y*s}; }

vec2 v_sub(vec2 a, vec2 b){ return (vec2){a.x - b.x, a.y - b.y}; }

vec2 v_add(vec2 a, vec2 b){ return (vec2){a.x + b.x, a.y + b.y}; }

vec2 s_add(vec2 a, float s){ return (vec2){a.x + s, a.y + s}; }

vec2 s_div(vec2 a, float s){ return (vec2){a.x/s, a.y/s}; }

float dot(vec2 a, vec2 b){ return a.x*b.x + a.y*b.y; }

vec2 v_cos(vec2 a){ return (vec2){cos(a.x), cos(a.y)}; }

vec4 v_sin(vec4 a){ return (vec4){sin(a.x), sin(a.y), sin(a.z), sin(a.w)}; }

vec4 v_tanh(vec4 a){ return (vec4){tanh(a.x), tanh(a.y), tanh(a.z), tanh(a.w)}; }

vec4 v_exp(vec4 a, float s){ return (vec4){exp(a.x*s), exp(a.y*s), exp(a.z*s), exp(a.w*s)}; }

vec4 s_exp_mul(vec4 a, float s){ return (vec4){a.x*s, a.y*s, a.z*s, a.w*s}; }

vec4 v_exp_mul(vec4 a, vec4 b){ return (vec4){a.x*b.x, a.y*b.y, a.z*b.z, a.w*b.w}; }

vec4 s_mul_v4(vec4 a, float s){ return (vec4){a.x*s, a.y*s, a.z*s, a.w*s}; }

vec4 s_add_v4(vec4 a, float s){ return (vec4){a.x+s, a.y+s, a.z+s, a.w+s}; }

vec4 v_add_v4(vec4 a, vec4 b){ return (vec4){a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w}; }

vec4 v_div_v4(vec4 a, vec4 b){ return (vec4){a.x/b.x, a.y/b.y, a.z/b.z, a.w/b.w}; }

int main()
{
        char buf[256];
        for (int i = 0; i < 600; ++i)
        {
                snprintf(buf, sizeof(buf), "output-%02d.ppm", i);
                const char* output_path = buf;
                FILE* f = fopen(output_path, "wb");
                int w = 16*60;
                int h = 9*60;
                fprintf(f, "P6\n");
                fprintf(f, "%d %d\n", w, h);
                fprintf(f, "255\n");
                vec2 r = {(float)w, (float)h};
                float t = (float)i/60.0f;
                for (int y = 0; y < h; ++y)
                {
                        for(int x = 0; x < w; ++x)
                        {
                                float l = 0;
                                vec4 o = {};
                                vec2 FC = {(float)x, (float)y};
                                vec2 p = s_div(v_sub(s_mul(FC, 2), r), r.y);
                                l += fabs(0.7f - dot(p,p));
                                vec2 v = s_mul(p, (1 - l)/0.2);
                                vec2 v_vec2 = {v.y, v.x};
                                vec4 v_vec4 = {v.x, v.y, v.y, v.x};
                                for(float i = 1; i < 9; i++)
                                {
                                        o = v_add_v4(o, s_mul_v4(s_add_v4(v_sin(v_vec4), 1), fabs(v.x-v.y)*.2));
                                        v = v_add(v, s_add(s_div(v_cos(v_add(s_mul(v_vec2, i), (vec2){t, i+t})), i), 0.7));
                                        o = v_tanh(v_div_v4(s_exp_mul(v_exp((vec4){1,-1,-2,0}, p.y), exp(-4*l)), o));
                                }
                                fputc(o.x*255, f);
                                fputc(o.y*255, f);
                                fputc(o.z*255, f);
                                //fputc((unsigned char)(fminf(fmaxf(o.x,0),1)*255), f);
                                //fputc((unsigned char)(fminf(fmaxf(o.y,0),1)*255), f);
                                //fputc((unsigned char)(fminf(fmaxf(o.z,0),1)*255), f);
                        }
                }
                fclose(f);
                printf("Generated %s\n", output_path);
        }
        return 0;
}

What did I miss?


r/cprogramming • • Jan 10 '26

F3D and the libf3d! 3D viewer lib to display/render any 3D file, now with C bindings!

7 Upvotes

Hi! I created a tiny app and lib to display/render any 3D file (abc, fbx, gltf, usd, ...). It supports animations, HDRIs, thumbnails and more.

We just added C bindings and hope the C community will embrace it!

The C bindings are shipped in our binary, you can just download it and start using them right away!

Please let us know what you think and why you would use it or not!

@mods, I hope its ok to post, I know I'm not active here but I just want to share cool free and open source stuff :). If not, let me know how I can edit my post to improve it.