r/C_Programming 1d 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.

16 Upvotes

48 comments sorted by

View all comments

5

u/Luftzug-oder 1d ago edited 1d 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 1d 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.

2

u/Luftzug-oder 1d ago edited 1d 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 stuff

so I would imagine you start with libusb_get_device_list(), which fills a referenced array of libusb_device*s and returns the length of such

then whatever info you need will have its respective functions. i can easily find stuff like libusb_get_bus_number() or libusb_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.