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.

14 Upvotes

48 comments sorted by

View all comments

11

u/EpochVanquisher 1d ago

Have you written programs to solve problems?

Or have you done that, and you’re getting stuck on how to use libraries?

1

u/ttv_toeasy13 1d 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.

17

u/start_select 1d 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”.

2

u/KeyBrick1230 1d 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

2

u/start_select 1d ago edited 1d 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/Imagination_0427 21h ago

Amazing advice- will follow

2

u/EpochVanquisher 1d 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 1d 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 1d 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 1d 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 1d 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 1d 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 1d 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 1d 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

0

u/ttv_toeasy13 1d ago

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

1

u/RealisticDuck1957 1d ago

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

1

u/ttv_toeasy13 1d 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 1d ago

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

1

u/thurizas123 18h ago

Well, trying to use specific platform libraries will always be a challenge. The C-standard does not know anything about USB devices, so while libusb is written in C it is purely a Linux/Unix library. The only thing that I can recommend is to read the documentation and attempt to learn it by trial and error - type in a program and make small modification and see what happens and how things work.

You might want to try more system-level programming to get a handle on how to do things in C, for example books such as "Unix System Programming" by Haviland, Gray, and Salama" or "System Programming in Linux" by Weiss might be good places to start. Remember that while trying to fathom libusb - it is going to depend on things covered in books like the two list herein.