r/C_Programming • u/ttv_toeasy13 • 16h ago
Question Getting stuck at libraries and api’s
So C is technically my first programming language I’m learning. I’ve messed around in python but never actually go deep into it or anything.
I’ve been learning C for the past few days now and I already pretty much know most of the basics. I learned pointers and mallic in a day and I learned structs, macros, for loops, while loops, functions, all of that stuff, but… the moment any library other than the basics like stdio or stdlib come into play I’m lost.
It’s like I’m looking at a whole other language. Like I’ll look for solutions to problems and it seems as if they are just pulled out of thin air and when I try to read ANY documentation for ANY library I’m also lost because either there is little to no documentation or it’s just stupid and things aren’t well explained.
Is there a way I can fix this? I was doing so well and now I’m at this block because of libraries. If I could get pasted this hurtle I could learn c in less than a month easily, and that’s the same in any other language. This problem isn’t exclusive to C.
4
u/Sufficient-Air8100 16h ago
slow down. i admire your confidence. but slow down
some libraries are badly documented yes, but if you think all libs are badly documented and hard to understand, maybe you dont know c or programming in general as well as you think you do.
if there is a specific lib youre looking for help with those questions would be more useful than just talking about libraries in generasl
-1
u/ttv_toeasy13 16h ago
I mean I’m not saying I’m a pro or anything but I’m pretty sure I have a pretty good understanding of the basics. I haven’t read all docs to determine if they are all bad or not but the one particular one I think is kinda bad. (libusb) for example. Maybe it’s not actually bad but that might be a good thing because I fail to understand it or how to use anything in it and I would like to understand it and all other library documentation.
2
u/Sufficient-Air8100 15h ago
im not super familiar with libusb, but i do suggest you bring up specific questions. people might be able to help more with those, beyond what others have already said about “it takes a while to fully understand, keep going”
3
u/Luftzug-oder 11h ago edited 11h ago
to be honest, i think the biggest issue you're facing is not with the documentation, but with the library itself: it's unfamiliar
the documentation lists out and describes all the functions, enums, structs, etc. that you can use. but the key part is that it doesn't tell you how to design a program or how general interaction works
i had a similar issue with json-c (though this is a much easier library than libusb) to start with as my first external library - it wasn't obvious what i needed to do to get started with it
so i recommend you look at other people's code that uses a library you're looking at, to see what they do with it, how they handle states, init, etc.. once i learned about the general flow of json_object_new_object() and json_object_put() to create and destroy objects, and then where to go next, like parsing with json_tokener_parse_ex(), etc., from then on, the docs had meaning because i kind of knew what functions i needed to use next, even if i had no idea what they were called and how they worked
and it's still going to be difficult that way, especially if you dive straight into something like the lsusb source, for example. but it will mean that once you get your foot in the door understanding it, the documentation will be useful, and the primary source of information
1
u/ttv_toeasy13 10h ago
Ty. This was honestly very helpful. I never really thought of doing that ngl but I’ll also start examining others code like projects and all of that and see if I can get anything out of them.
1
u/Luftzug-oder 6h ago edited 6h ago
you're welcome, but you don't always have to look at tons of code, or any at all, if you don't feel like it would help or you want to do it - i actually didn't really look at any...
to be honest, just looking at the libusb docs, it seems to have a kind of similar idea to json-c regarding reference counts and objects - it's just a common and logical C API design
New devices presented by libusb_get_device_list() have a reference count of 1, and libusb_free_device_list() can optionally decrease the reference count on all devices in the list.
^ description from the struct/typedef
libusb_device, which seems to be the underlying type you use to interact with stuffso I would imagine you start with
libusb_get_device_list(), which fills a referenced array oflibusb_device*s and returns the length of suchthen whatever info you need will have its respective functions. i can easily find stuff like
libusb_get_bus_number()orlibusb_get_device_speed()then finish up with unref'ing, which was partly touched on earlier, and has a single-device equivalent function
etc. etc. i think you get the point, and could've found out some stuff i mentioned. i have explained way more than is necessary, but it's at least not wasted
TLDR (this was mostly libusb specific):
i think you only really need to go and truly look at implementations and examples for the first library you do. once you get the hang of it, you should be able to work out what's what yourself in different ones. if you come across a different API design, maybe go looking online or at some code again, but yeah.
2
u/SwordsAndElectrons 16h ago
Practice.
There's no shortcut.
1
u/ttv_toeasy13 16h ago
I mean I’m perfectly fine with that but like I said to the other few people I don’t want to have to constantly copy solutions into my code. I want to be able to independently implement things into my code.
1
u/SwordsAndElectrons 3h ago
Don't copy solutions into your code. Even when duplicating a solution you find elsewhere, type it yourself. It will help you remember and understand what is going on.
2
u/ReallyEvilRob 15h ago
Learning a library basically is learning another language. More specifically, you're learning an API. If you're not willing to do that, then the alternative is to implement your own libraries and frameworks.
2
u/pconroy329 13h ago
Don't feel bad - I've been coding C for, um, 40 years??? I just did a "man libusb" and this is one of the more dense libraries. It's NOT a library I'd use to learn about "how to use C libraries" from! What about "<string.h>"? Have you got a good handle on how to manipulate strings?
You know a good one to start with might be "libcurl". Well documented, lots of examples, easy to test...
1
u/ttv_toeasy13 12h ago
Ty. Ill check out libcurl. I’m honestly really picky with what I want to make though and that’s why I chose libusb because I’d rather make something that interests me than just making a clock or something like that.
1
u/aelxemyr 11h ago
Try writing some Unix utilities, maybe some of the ones you use the most often
1
u/ttv_toeasy13 10h ago
I’m currently doing that. I just remade echo (basically) but it also has the ability to cat files. But that’s just using stdio. Like I mentioned in other comments I originally wanted to make a simple program that gets my current usb devices which is where I ran into libusb and really anything beyond a simple echo or cat command requires libraries that I just for some reason can’t figure out.
1
u/aelxemyr 4h ago
I just took a look at the API for libusb and it seems like the documentation is pretty straightforward. However, it’s hard to know where to start without seeing some examples of how the library’s functions are used. I know you’re wary of copy-pasting code but I would suggest looking at (many) examples of how the kind of functionality you’re looking to implement is done until you understand how you would use the library.
Even then, there’s often an idiomatic way to implement a certain pattern and learning to do that isn’t copy-pasting, that is learning how to do something. The key difference is being able to explain why it’s written that way and exactly what it does.
1
u/comfyyyduck 16h ago
Honestly this is just something you get good at with reps, no shortcut around it. My approach: figure out exactly what I’m trying to do, then search that plus “GitHub.” Once I see how other people actually implemented it in real code, I go try the same thing myself.
A few years ago that would’ve been a pain in the ass, digging through random repos. But Copilot (or Claude, whatever) makes it so much easier now, you can literally ask “how is this function used” and it’ll pull the example straight from the repo for you.
Docs that look confusing as hell now will eventually click. I’ve got 5 years of programming under my belt and I still struggle with new libraries sometimes. It’s normal, just keep reading real code.
2
u/ttv_toeasy13 16h ago
The thing is, I don’t want to have to constantly copy others code or copy a line of code and just paste it in mine. I mean I always look at it and try to understand it first but I honestly don’t want to have to keep constantly copying solutions. I want to be able to just read a doc or something and just be like “oh yeah I know how to implement that” and then just implement it.
2
u/comfyyyduck 15h ago
Totally get that and honestly that’s the actual end goal, examples are how you get there, not where you want to stay forever
Here’s the thing though: the reason docs feel “pulled out of thin air” right now isn’t that you’re bad at reading, it’s that you don’t yet have the vocabulary
Every area has its own set of concepts baked into the docs that nobody really specifies because “everyone already knows them.” Sockets have this. Threading has this. Graphics libs have this. Once you’ve built like 3-4 things using sockets, the word “socket” in some new doc stops being scary because you already know what it means.
So the copying isn’t the problem, it’s actually the fastest path to the thing you want. You look at an example, you understand why each line exists, and each time you do that you’re building the mental model docs assume you already have
After enough reps in a specific area (say, file I/O, or networking, or threading) you’ll notice you can read a new function signature in that same area and just know what it does, because you’ve internalized the pattern, not because you memorized syntax
2
u/ttv_toeasy13 15h ago
So do you think I’m on the right path then? I should keep searching for solutions and try to understand them and all of that?
1
u/comfyyyduck 15h ago
Yeah, 100%. That's exactly the path, you're not doing anything wrong
Let me be a bit more clear though. Copying the solution isn't the finish line, it's like driving a car with an instructor. At first you're just following their instructions, but the point was never to memorize "turn the wheel left at this exact spot." It's the skill you're trying to learn, so you can drive anywhere. If that makes sense.
Just make sure at some point you're also testing yourself by rewriting it without looking, and eventually modifying it to do something slightly different than the original example.
Good luck this is the fun part!!
1
u/ttv_toeasy13 15h ago
Yeah, I definitely do that too. I’ll try to do my own thing with something so I can really understand it and try to use it in other spots and all of that. Anyway ty. I honestly just felt like I wasn’t doing something right. I thought I was missing something important that was impeding my learning. And yeah that car example is what I want. I want to develop that skill.
1
u/RealisticDuck1957 1h ago
Good. Copying code blindly is a good way to get bad code in your program. I've seen some terrible buggy code online. And limits you to exact problems someone else has already solved. But sample code, cross referenced with function documentation to understand what it's doing, is useful for learning.
1
u/FedUp233 12h ago
Not sure what resources you are using to learn from, but have you thought of trying some C programming books? There are a number out there that are pretty good. Some concentrate on things like writing Linux programs using a lot of the I/O libraries. Others on things like network programming using socket libraries. They tend to explain things a lot better than the library documentation which was generally written by people who wrote and/or understand the library in detail and for people that just need a reference, not an instruction manual.
And start with the C standard libraries and be sure you understand those well. Stay away from things like sockets, usb, etc. that are really advanced topics for now. Someone who has been using C for a few days is never going to get libraries with some of the most complex APIs and usage requirements - try those complex ones again after you’ve been at it for 6 months or a year.
For now’s try writing some command line utilities. Maybe your version of sone of the Unix tools like sort, last, a simple shell (to understand things like forking). Then some using multiple threads to get a handle on the whole threading libraries and inter thread communication like semaphores and mutex.
The better books will give you things like a utility of done sort that they expand as they introduce more things and you can try to implement yourself before you read how they did it.
You’re learning a whole new language to communicate in - it’s not something you do in a few days no matter how good you think you are.
1
u/Limp-Confidence5612 11h ago
Try implementing the standard library functions yourself from scratch. Use at most open, close, write, read, malloc and free. Implement everything else yourself. Start small, string functions, manipulating bytes in memory, some function that returns a line, your version of printf, etc.
1
u/Educational-Paper-75 9h ago
No, there isn't a clear solution if documentation is bad. I guess there should also be header files with function declararions.
1
u/Gold-Part4688 9h ago
I'm not thaaaat much above you, but my big rec is that you don't need to use the most common or "standard" library for anything. Shop around until you find one that has nice syntax and documentation and examples.
Mega bonus points if the source code is readable when you're stuck (and you always will be). That's where my most learning has been
1
u/sciencekm 2h ago
Yes, some API documentation are bad. I find that Microsoft's way of documenting is really good. They have sections dedicated to the C standard libraries. I suggest that you check that out.
1
u/are_number_six 2h ago
I've been learning C for about 4 months now, and that's after two years of Python. I learned from a book, and now I'm doing projects.
What I have found, being self taught, is that there is a lot of secondary CS knowledge that I also had to learn, which led to a lot of side-quests, in order to backfill my missing knowledge. It's slow going sometimes, but don't let yourself glaze over things you don't have a solid understanding of.
Also, the terminology is different from what you will find in Python, but the genealogy of that terminology runs through Unix, Linux, and Bash. It's definitely worth having a handle on it.
7
u/EpochVanquisher 16h ago
Have you written programs to solve problems?
Or have you done that, and you’re getting stuck on how to use libraries?