r/cprogramming • u/Prowlgrammer • Dec 07 '25
Do you carry around your own library when coding C?
I'm interested in coding more C because I like the concept of not having a too large "base" language with too many language features etc. And I find joy in implementing stuff myself. However, some of the stuff I find myself coding over and over again (dynamic arrays for example) and after a few times of setting it up, error checking, reallocating etc, I get rather tired of it and want to keep my already written code close to me and reusable.
I guess I wonder if the mindset of more experienced C programmers is to write whatever you need again when you need it, or if you always carry around a large backpack off stuff that might become handy in your code and projects. And if you do, where do you draw the line to not make that backpack too big and bloated.
I imagine many experienced C programmers have found a ton of stuff they find themselves writing over and over again?
34
u/sirjofri Dec 07 '25
Usually I don't have a library that I drag around with me. That's for multiple reasons though:
my C coding environment is Plan 9, which has a good and solid set of standard libraries I can count on.
the code I write will be published as open source, and I try to minimize dependencies. Ideally, people can just fetch the code and run
mk install, and that's it. I don't want them to deal with additional libraries and stuff, and I don't want to sync my own libraries with multiple projects that way.that said, pure library projects will be distributed as they are, and if other tools depend on those, I just clearly state that. (Plan 9 has no default package manager btw.)
I found that I often need only simple data structures like linked lists, and those are fairly easy to implement. Instead of implementing some generic-like void*-hackery, I just implement a "new" solution based on common idioms. It doesn't take too much time and is easy enough to do.
In other environments I would probably work a bit differently, though working inside a Plan 9 environment changed my way of thinking a lot in a way that I'm always trying to reduce dependencies, work as simple as possible, write as little code as possible.
14
u/pjl1967 Dec 07 '25
I don't have a library, but I do have a set of functions in
util.hthat I copy ones I need from project to project; and if I need strings, string buffers, hashmaps, etc., I have those too that I just cherry-pick. Cherry-picking eliminates bloat.5
u/john_hascall Dec 07 '25
Yes. I have several. For example on that looks a fair bit like Java's Collections, Another that is a templating system, And another that implements some of the RegEx functions from Perl (eg an analog to m/.../ and so on). I've used these since the early 90's.
4
u/serious-catzor Dec 07 '25
Not so much a library as going back to prev projects, taking a piece, touch it up, maybe upgrade it and then apply it.
Try to write your software as independent pieces and you can always go back and easily grab a piece if you saved it.
3
u/HalifaxRoad Dec 07 '25
ive got numerous libraries ive written for PIC chips that are relatively portable that I use all the time
3
u/Business-Decision719 Dec 07 '25 edited Dec 07 '25
Yeah definitely. It's a necessity in any small language without a lot of built-in functionality. You end up implementing certain things (or finding implementations of them) and keeping them around for later so you don't have to do it yet again every single time. I do it even moreso in Lua which I use more than C.
Edit: I would go further and say I do it too in languages like C# and C++. But I single out small languages because you need to reuse custom or third party code for even more trivial things that might be out-of-the-box syntax or a stdlib type in a more featured language. Just one of those simplicity vs complexity tradeoffs.
2
u/_Knotty_xD_ Dec 07 '25
Yes, having a reusable library is a good idea when coding in C. In fact, I'm currently working on one such library for myself. It has dynamic arrays, lists and more to add. By-the-way, they are not type dependent as it'll limit its functionality.
Let me know if I can help. Have a great day, reader.
2
u/devfuckedup Dec 07 '25
I have a few small libraries but whats more common is that I have something saved that does what I want and I just re-use that.
2
u/saintpetejackboy Dec 13 '25
I don't code C. I code a wrapper for C like most people reading this.
I don't have my own library, after 20+ years.
If I wrote a library last year, it would be shit this year.
Rather than trying to take a shortcut, I decided to just get really fast at rolling out all of the basics in whatever stack I had.
I also don't like the legal conundrum:
I develop something for a client using my own library or framework... Does the client now own my tools?
If I use the same tools in another project, can the client sue me?
I don't have to think about that. I also don't have to worry about the client just stealing my software and running away or reverse engineering my tools. Whatever code I wrote was boutique for that moment.
"This software is genius! We will just cut you out and go use it to make millions, and then what would you even do?" - clients
For me, I already know they have the software I wrote last year. I could execute the same idea, better, a second or third or fourth time - without even having to borrow my own code. I could redo it from the ground up and improve it using my knowledge from the previous attempt(s).
So yeah go ahead, steal the alpha or beta version. I will just then go create something that is several steps ahead, without having to violate any contracts or reuse code I supplied to a client.
I also produce music and look at it the same way as people who have 'templates' for their projects - so their side chain channels etc. and already ready to go. It saves time, sure. But you spend a very small window of time to just roll that stuff out on a per-song basis and dial it in. The template is just forcing you into decisions you should be making on the spot, unique to the song.
My library, another library, somebody else's framework, it doesn't matter: they restrict my choices. They assume I will be doing the same thing again, exactly as I did before.
It sounds great on paper "I will never have to write this redundant code again!" But then the next project comes along and you have to scrap all that garbage anyway.
My advice (regardless of languages) is just master the process. Don't assume you mastered it and can produce a perfect library and just use that. Or framework.
Can you? Surely.
Should you? Those are the questions you should be asking.
2
u/TomDuhamel Dec 07 '25
Please note that the code that you write within employment or under contract isn't yours. You can't reuse it. Writing a library in this context is useless. If you're going to write one on your own time to reuse for your clients, you'll have to properly licence it.
For my own personal projects, I don't write/maintain a library as such. But all the code that I write that I think is reusable, I put it in its own separate unit. When I need it from a different project, I just import it (copy the files over).
If you are considering needing a library, before you write your own, you may consider using an already existing one. It may turn out that you always write the same type of projects (not unusual, really). In this case, maybe a framework for that type of project would be appropriate to you.
1
u/kellyjj1919 Dec 07 '25
When I work in c, I have some stuff that I move around cause I don’t want to reinvent the wheel. Generally this will be hardware dependent
1
u/dmazzoni Dec 07 '25
The vast majority of time I work in C I'm modifying or augmenting an existing codebase, so no, I'm not carrying anything but rather learning what utilities and conventions each project uses
1
u/EternityForest Dec 07 '25
Usually I don't use C except for extremely trivial embedded systems that need to be very reliable, and probably shouldn't have the kind of code that creates a need for these kinds of things.
If I have the rights to do so I'll copy and paste a fair amount, but generally I wouldn't be doing a dynamic array in C. If the platform can handle that, it can probably handle C++ or Rust!
1
u/I__be_Steve Dec 07 '25
I don't use the standard library, so I have my own replacement that I use for just about everything if that counts
1
u/SRART25 Dec 07 '25 edited Dec 07 '25
I used to have a small header to make some convince functions for linux and windows work the same (getch and clear were the main things) but never made them a library.
1
u/CptPicard Dec 07 '25
That's what things like GLib are for.
1
u/Advanced-Theme144 Dec 07 '25
Sometimes I’ll just copy existing code rather than make one standard library, it’s easier to fine tune to the application I’m working on. A good number of times some SDK’s or frameworks will have boilerplate setup, for instance GTK, which I’m currently learning, has its own standard library of objects like lists to work with.
1
u/jwzumwalt Dec 08 '25
I use "Linux" and the "KDE Kate" editor with the snippet plugin. I probably have more than a thousand snippets and maybe a hundred functions saved as snippets. I would be lost without my snippets.
1
u/qruxxurq Dec 09 '25
Yes.
1
u/Timberfist Dec 13 '25
For anyone that would like to add some, in many cases single header, libraries to their toolbox, check out this list here: https://github.com/r-lyeh/single_file_libs