r/cprogramming • u/C_Sorcerer • Oct 20 '26
How to get better at well structured/conventional code in C
Hello all. I used to write C a lot but honestly over the past few years I haven’t used much if any since I was using C++ a lot, mainly for computer graphics. I ended up going very deep in C++ and feel as though I can confidently write a well structured program, as I have a game engine and a raytracer.
However, I’ve been trying to get back into C as I am working on a Gameboy Emulator in C and trying to get my low level systems portfolio up since I really enjoy that as well and have forsaken it in lieu of graphics. I also realized it’s prob not gonna be easy to get a job in graphics until I get a PhD, and yet embedded is in a much better spot, so with it being something I like as well, I find it more beneficial right now while looking for an embedded job to write programs in C for my portfolio and just to learn (and to have fun).
Anyways, I am running into some issues I didn’t have before and I think it is because I am trying too hard to write things like I did in C++. Using the same naming conventions, file organization structures, splitting of functions and modules to create deeper abstractions, etc.
And yet even throughout all of that, I seem to be ending up with much messier and harder to manage code in my Gameboy Emulator that in all honesty, I have no idea what I’m doing. I’ve gotten decently far on it, wirh the entire instruction set being implemented and interrupts (somewhat) working, but it is becoming much harder to manage than it seemed wirh C++.
Is there any advice or resources (I prefer more comprehensive books but articles and YouTube videos are good too) that you can give me on just getting more comfortable with writing good, well structured C programs?
8
u/pjl1967 Oct 20 '26
Program Organization (with Examples in C)
2
u/thebatmanandrobin Oct 22 '26
As other's have mentioned, check out some other projects that you're familiar with that are written in C (like any of the *nix kernels, etc).
Beyond that, as you've mentioned yourself: don't write C like C++.
That's not meant to be callous or anything, but more specifically point out that as you're seeing, you can't "think in C++" when trying to organize your programs in C - especially with things such as "header only" type of libraries (easy what to foot-gun yourself in C).
Beyond that, another option would be to share your project with the group and ask for critiques on the structure and layout specifically (you'll get some critiques on the code itself I'm sure, but if you emphasize the structure part, you'll likely get more in that realm).
Structure of code can be pretty subjective sometimes - I've had projects that I've worked on that had guidelines with how the code *should* be structured that insisted on 1 folder with the 100+ .c/.h files all in there ... fucking impossible to maintain in my head, but the same person who made these guidelines also had "that desktop" (you know the one .. 5000 links on their Windows desktop, sticky notes all over their monitor, papers under their chair, coffee stains on their shirt, a pinboard that looked like a crime investigation .... yeah, fuck that guy).
2
u/sergio-gdr Oct 21 '26
I don’t think there is much information around like you would find for a modern language. What I like to do is look at well established projects and see how they structure and organize things. It could be low-level (modern) libraries such as libwayland or pipewire, or maybe take a look at NetBSD, sqlite, things like that.
1
u/One_Kangaroo_9323 Oct 22 '26
Read doom and quake case studies
1
u/[deleted] Oct 22 '26
[removed] — view removed comment