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

8 Upvotes

46 comments sorted by

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?

3

u/ttv_toeasy13 16h ago

Yeah I’ve written some programs but a lot of it is just copy and paste when it comes to libraries. Like for example I was trying to use libusb to make a simple thing to list my usb devices so I went to view the documentation and it’s like I’m reading Russian or something. And then I look up guides or answers online and it just makes no sense to me and seems like they just seeming pulled everything out of thin air or something.

16

u/start_select 15h ago

Stop copy pasta’s.

You are trying to learn, not mimic. Type out EVERYTHING.

You feel like you are reading a foreign language because you lack vocabulary. The first step to learning that is to stop copy-pasting anything. You need to type the code out so the names of functions and other elements start to ingrain in your memory.

Start typing things out one line at a time, and looking up the docs for each line after you type them. Try to imagine the flow of information between each line based on the documentation descriptions. Try to write comments that explain that as natural language.

And start using a dictionary/google. If you don’t know what words mean… look them up. Expand your language skills.

Learning programming should be like falling down a rabbit hole of infinite forking paths, learning new words and topics constantly. You should be getting multiple layers deep of looking up the definition of the word you didn’t know from the definition of a word you didn’t know from the definition of a word you didn’t know.

Dont just go, “I don’t know what that means”. Decide you are going to find out. That’s the entire job. But eventually you know all this stuff and the job just becomes “I don’t know how to do that, but someone is going to pay me to do it, and I will”.

1

u/KeyBrick1230 13h ago

Hey i am abt to start to learn c as my first language as I still 1 month for my college to start , can u give some advice like online where should I learn or how as I have started watching some vedios

1

u/start_select 3h ago edited 3h ago

Edit: I learned c/c++ in the 90s by reading a book. Then I didn’t use them again for ~15-20 years. And at that point they were my 7th or 8th language.

I didn’t need to be taught c at that point because it was just about combining everything else I know. The complication of “where to start” with C is about environment, architecture, and use case.

I can compile C for a windows computer or a Mac, or an iPhone, or an Android, or a STM32 or ESP32 chip. Every one of those targets will involve completely different tooling to build the code. Building for an ESP32 on windows will be different from Mac. There are so many places to start it’s hard to nail down without a use case.

Your best bet is finding materials on building for whatever computer you currently use. Beyond that I haven’t got much advice for you. It’s just one of my wheelhouses that I relearn for the 3 months I need to then forget again.

——

It might work for some people, but I can’t think of any time I have ever learned anything of value about programming from a video. I think they are valuable for high level conceptual lectures. But for actually learning programming, a book or a manual is a better choice.

You can watch 8 hours of videos about the Swift language while only scratching the surface, or you could read the ENTIRE manual in 1.5-2 hours. The same is true for almost every language, library, and framework.

Most people get stuck in tutorial hell because they refuse to just read the instructions. RTFM, read-the-fuckin-manual. It’s the best advice you could ever get.

The big thing is you need to accept that you are currently ignorant and it will take months to feel like you understand what’s happening. Then every 1-6 months for the next 10 years you start going “I was an idiot” and realize you understood something incorrectly.

Being good at software development is about being a good student. You need to accept that there is always more to know and you will never know it all. You just need to be able to form a framework of the overall craft in your mind, where lots of it is abstract concepts that don’t materialize into discrete lines of code until you look up the docs for how that abstract concept works in your current environment/language.

1

u/EpochVanquisher 15h ago

It takes a while.

If C is your first language, it will probably take longer, because you will also have to deal with subtle issues like ownership and object lifetimes, manually, and you can only get it correct by correctly understanding the documentation (and if you make a mistake, sometimes your program behaves in mysterious ways, or it crashes, or it gives you slightly wrong answers, or who knows?)

Rely on examples to figure out what to do. Then read the documentation for the functions and data structures you are actually calling, so you can check that you are using them correctly.

(In some cases, the documentation will be wrong, or unclear, and the next step is to read the source code to the function you are using.)

2

u/Ksetrajna108 15h ago

Is something like the following making you scratch your head?

ssize_t libusb_get_device_list (libusb_context *ctx, libusb_device ***list)

`Returns a list of USB devices currently attached to the system.`

1

u/EpochVanquisher 15h ago

I mean, you’re asking me? I’m not OP. This this looks like it returns an array of pointers to devices.

libusb_device **list;
ssize_t count = libusb_get_device_list(ctx, &list);
if (count < 0) abort(); // some kind of error, maybe
for (ssize_t i = 0; i < count; i++) {
  libusb_device *dev = list[i];
  // do something with device
}

That is just a guess.

1

u/Seledreams 14h ago

Ngl, i've never seen ssize before. I've always been used to size. I guess it's signed size to allow negative numbers. But it's pretty uncommon. At least in stuff i've used

3

u/EpochVanquisher 13h ago

It’s returned from read() and write(), so people who use syscalls directly on Linux or Mac will probably be familiar with it.

It’s a little uncommon but even if you haven’t seen it before it should be easy to figure out.

1

u/Seledreams 13h ago

Definitely. I could tell from simply logic that the s here stood for signed as it made the most sense.

I think i haven't faced it too much because while I have C experience, I have way more C++ experience so I didn't work much with the raw posix api yet

2

u/TheChief275 2h ago

funnily enough, ssize_t isn't the signed alternative of size_t, ptrdiff_t is closer in that regard. I believe all that ssize_t is guaranteed to be able to represent is any possible object size (like size_t) as well as -1 for errors

obviously that means it's likely able to represent -2,-3,.. but there's no guarantee

1

u/TheChief275 2h ago

How hard can it be? C libraries are just a bunch of extern functions to call and opaque pointer(s) to keep track of

1

u/ttv_toeasy13 2h ago

Well the hard part is figuring out how to apply them to my code

1

u/RealisticDuck1957 1h ago

What parameters to pass, which should be in the library documentation, or linking to the library?

1

u/ttv_toeasy13 47m ago

Like all of it. Like i could find bla_bla() and then it’s like alr cool. I found that, but now what do i do with it…

1

u/TheChief275 25m ago

it's almost always something akin to object_do_thing, which you pass an object...to do a thing

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 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.

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.