r/cprogramming • u/DaCurse0 • Feb 01 '26
Pain points of C
I recently wrote a Discord bot in C and implemented the gateway connection and API requests myself (no wrapper library), using libcurl for WebSocket/HTTP and epoll for async I/O.
That was fun to write overall, but the biggest pain points were building and parsing JSON (I used cJSON) and passing data between callbacks (having to create intermediate structs to pass multiple values and accept opaque pointers everywhere).
Is C just not made for this? Will this always be a weak point when writing C, or are there ways to make this less painful? I can provide specific examples if I haven’t made myself clear.
8
u/dkopgerpgdolfg Feb 01 '26
Painful in comparison to what?
This doesn't say anything about how it was painful.
7
u/EpochVanquisher Feb 01 '26
Most people would write a Discord bot in another language, like Python, Go, C#, or Java.
C is going to be more painful to use. That’s just the nature of C. The language itself is over 50 years old and we are a lot better at designing languages today than we were 50 years ago.
People still use C because it has advantages. But being easy to use is not one of them.
There is very little benefit to using epoll directly. It’s a pain to use correctly and not portable. Most people will use a library that uses epoll, like libuv or something, or use a language that includes an async runtime that uses epoll (like Go).
1
u/Still-Cover-9301 Feb 01 '26
I’ve just written an acme client in C (I’m writing a new webserver) which feels similarly complex to your use case and I had a similarly bad time with json UNTIL I started to do things with nested functions. Nested functions allow you to express the resource management in a very granular way with nice boundaries that helped me explore the control flow and then collapse it into some abstractions that worked.
Nested functions are a gcc thing right now (but they are safe in gcc 15, the stack thing is fixed) but closures are spaced for the next revision of C so if they get approved we’ll have a really good way to do something similar to nested funcs.
When I posted about it on Reddit there was quite a lot of resistance based, it seemed, on the fact that it would make C slow. But that’s silly. It’s just another choice. You don’t have to use it and I don’t use it all the time, just for these web things where there is a lot of too and fro.
I have to say working this way made me very happy. It was a great experience and I didn’t find myself wanting C++ or Zig or anything like that.
1
u/GrandBIRDLizard Feb 01 '26
Could write your own JSON parser, I happen to like cJSON(or any single file library for that matter) but that's just me.
2
u/[deleted] Feb 01 '26
> having to create intermediate structs to pass multiple values and accept opaque pointers everywhere
Either that, or perhaps use an array or list of tagged union objects?
1
u/chocolatedolphin7 Feb 01 '26
The error handling can be greatly simplified with some refactoring, that seems to be the bulk of the verbosity in this case.
1
u/zhivago Feb 01 '26
C is a pretty primitive Ianguage by design.
For things like json parsers consider using a visitor pattern.
1
u/phdppp Feb 01 '26
The first C program I ever wrote was a web crawler, and I remember how painful it was to parse HTML with libtidy. These days I wouldn’t do that kind of thing in C, but at some point you learn, regardless of the language, how to properly break things into functions and reuse them.
1
u/konacurrents Feb 01 '26
Let's be serious. C doesn't support dynamic strings except in very clunky ways. This is because memory management is always visible and not hidden behind dynamic memory management. It's not an "old" vs "new" language issue, it's a dynamic memory issue. So when loaded on a constrained embedded ESP32 device, there are less memory gotchas (since no garbage collector).
It turns out one of the best languages for JSON - is javascript, especially as the "JS" stands for JavaScript. https://www.json.org/json-en.html
A user can easily create a JSON formatted string/object as follows:
And then access those fields by name (eg.
stringJSON.username). And a string is turned into JSON by their JSON parser, etc.A C version is a lot harder (char *, not C++ String). The above example would require creating a buffer (hopefully enough memory). Then sprintf, strcat, strncat, etc. Adding onto that string with more fields is hard. (I just crashed my ESP32 since the buffer wasn't big enough).
So for JSON processing, straight C is sadly not the best approach.
1
u/runningOverA Feb 02 '26
You need a variant + vector + hashmap library to easily use and parse data from a JSON library. Both the JSON library and your code should use the same hashmap library.
Other languages have these built in therefore these are given.
In C, you need to ensure it separately.
JSON is a mixture of variant, vector and hashmap.
0
u/flyingron Feb 01 '26
Eh? Json parsing is about the easiest encoding method out there. There are hundreds of C example code out there found easily by googling and writing your own isn't particluarly difficult if you got a NIH bent.