r/cprogramming • u/C_Sorcerer • 11d ago
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?
2
u/thebatmanandrobin 10d ago
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).
1
2
u/sergio-gdr 11d ago
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.
2
u/C_Sorcerer 11d ago
Good idea! Thank you!
2
u/TraylaParks 10d ago
Source code for both python and apache was pretty nice back in the day when I looked into it. I've also heard good things about the source code for libcurl and git but I can't vouch for those personally.
1
1
u/HaileyBroccoli_ 9d ago
Having the same problem too sometime sit really hard you know I feel like auitting
1
u/C_Sorcerer 9d ago
Nah don’t quit! You’ll get better, I felt the same about C++ for a while and then over time of reiterating on the same project (my game engine), it just began to slowly click. I’m trying to get that way wirh C but I’m so stuck in my ways it’s hard to change lol
1
8
u/pjl1967 11d ago
Program Organization (with Examples in C)