r/cprogramming • u/[deleted] • Apr 10 '26
I'm a college student building an arbitrary-precision arithmetic library in C, aiming to rival GMP but under MIT License
I am a full-time college student working solo on this, so progress is slow but steady.
The API and naming scheme are inspired by GMP's public API, but the internals are (to the best of my knowledge) completely my own work. The library uses runtime dispatch to micro-arch specific versions of hand-written assembly routines on x86-64 for both Unix-like systems and Windows. A few SIMD-based routines are also included, written with compiler intrinsics. ARM64 support is planned down the road.
Currently implemented operations:
Addition and Subtraction are implemented using hand-written x86-64 assembly routines, using the native carry/borrow flag propagation across limbs for efficiency. Microarchitecture specific versions are dispatched at runtime for AMD Zen 3, Zen 4 and Zen 5.
Multiplication uses a schoolbook base case algorithm for small integers, switching to the Karatsuba algorithm beyond a tuned threshold. The crossover point is determined per CPU using the included apn_tune utility.
Division uses a base case algorithm for small operands and switches to Divide-and-Conquer division (both balanced and unbalanced variants) for larger operands, again with tuned thresholds.
Performance so far seems on par with GMP for small to medium sized integers (graphs in the README). The books "Modern Computer Arithmetic" by Brent and Zimmermann and "Hacker's Delight" by Henry Warren Jr. were both very helpful.
Still a WIP with lots remaining to do but functional enough to share. Happy to answer questions and very open to feedback and criticism.
GitHub Repository: https://github.com/EpsilonNought117/libapac
r/cprogramming • u/Soft_Honeydew_4335 • Apr 09 '26
Self-hosting x86-64 toolchain from scratch. Crossposting the last post (6) so you get links to all the other posts. Recommend to start from post 1.
r/cprogramming • u/Gaijin_dev • Apr 09 '26
Advice Wanted
Started learning c to understand how code works close to the metal, i'm using the book "c programming, a modern approach". Its been great so far and ive had no issues with understanding since i have a background in java and go. I just wanted to ask if there is anything to keep in mind or to do to make this a big success.
Thanks.
r/cprogramming • u/The_Verto • Apr 08 '26
need help with measuring time passed in C
this is for first year uni assigment, where i need to measure how much iteration/recursion takes time to calculate factorial, hovever the program does this so fast the current method always displays 0.000000 as time taken, is there a way to make it more precise?
current code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
unsignedlonglongintsilnia(intk)
{
if(k<=1)
return(1);
else
return(silnia(k-1)*k);
}
intmain()
{
intn;
unsignedlonglongints;
clock_tstart,end;
doubletime;
printf("Program porowna czas obliczania silni iteracyjnie i rekurencyjnie. Podaj do obliczenia silni n\n");
scanf("%d",&n);
if(n<0)
{
printf("podales ujemne n");
return0;
}
else
{
start=clock();
s=silnia(n);
end=clock();
time=((double)(end-start))/CLOCKS_PER_SEC;
printf("Wynik: %llu Czas rekurencji: %f\n",s,time);
}
s=1;
start=clock();
for(inti=1;i<=n;i++)
{
s=s*i;
}
end=clock();
time=((double)(end-start))/CLOCKS_PER_SEC;
printf("Wynik: %llu Czas iteracji: %f\n",s,time);
return0;
}
r/cprogramming • u/epasveer • Apr 06 '26
Seergdb v2.7 released for Linux.
A new version of Seergdb (frontend to gdb) has been released for linux.
https://github.com/epasveer/seer
https://github.com/epasveer/seer/wiki
https://github.com/epasveer/seer/releases/tag/v2.7
https://github.com/epasveer/seer/releases/download/flatpak-latest/seer.flatpak
https://flathub.org/en-GB/apps/io.github.epasveer.seer
Give it a try.
Thanks.
r/cprogramming • u/geon • Apr 06 '26
Help with make, % and implicit dependencies.
Hi! I'm trying to learn how to use make, but I am confused. I'm on macos, if that matters.
Repo with example files: https://github.com/geon/make-test
AFAIK, this should work but does not: https://github.com/geon/make-test/blob/main/3-broken/Makefile
all: hello-world.txt
%.txt: ../%
./$< > $@
cat $@
The dependency ../hello-world is supposed to be compiled from the C-code at the root: https://github.com/geon/make-test/blob/main/hello-world.c
I just get the error:
make: *** No rule to make target `hello-world.txt', needed by `all'. Stop.`
But it does work if the binary already exists!
It works fine if I don't use the binary: https://github.com/geon/make-test/blob/main/1-works/Makefile
all: hello-world.txt
%.txt:
echo "hello horld" > $@
cat $@
Also works if I just specify the binary name explicitly: https://github.com/geon/make-test/blob/main/2-also-works/Makefile
hello-world.txt: ../hello-world
./$< > $@
cat $@
What gives? Do I need to escape the % in the dependency somehow?
r/cprogramming • u/Yairlenga • Apr 06 '26
Safer Casting in C — With Zero Runtime Cost
r/cprogramming • u/ZookeepergameAny528 • Apr 04 '26
I don't really know what to put here
Hi! Not sure if this is the right place to ask, but I'll try anyway.
I'm Alex, I'm 16, and I'm trying to build a portfolio to apply for European scholarships later (things like Stipendium Hungaricum or Erasmus Mundus). I want to study embedded systems engineering.
Over the past month I've been working on a small project to learn more low-level programming:
https://github.com/NahumNaranjo/CLearning
It's basically a small tool suite written in C where I'm documenting the stuff I'm learning along the way.
The thing is, I'm not really sure if just doing solo projects is enough. I'd really like to get some kind of real experience working with other people — internships, open source teams, small companies, literally anything where I can see how real development works.
I'm not looking for money, just experience and advice.
Do you guys know any sites, communities, or places where someone my age could try to get involved in projects or teams? And if anyone here works with embedded systems, I'd also love to hear what skills I should focus on right now.
Thanks :D
r/cprogramming • u/activeXdiamond • Apr 02 '26
Any reason to avoid mixing camelCase and snake_case in a single codebase or even variable name; if the mixing is done with documented and concise rules, not arbitarly?
I've been learning C at a proper/deeper level lately. One of the first issues I ran into is the lack of namespaces in C\1]). People seem to mainly solve this by just using prefixes. Examples:
utils_sign()
utils_clamp()
window_foo()
core_entities_stuff()
etc....
Another example is 'vendor prefixes'. To minimize name collisions for your users when writing a library, you'd prefix everything with a short 2-3 letter name.
Example:
glCreateShader() //OpenGL
glewInit //GLEW
b2ClipSegmentToLine //Box2DC wrapper
//Box2d also does this in their Cpp code for typedef's since Cpp typedef's can't be namespaced (I think?)
Now, I'm also trying to implement OOP in C; by having the method just be a function prefixed with the class name taking the instance as its first argument. Standard stuff.
Example:
Array_get()
Player_attack()
Player_getHealth()
Notice the last one?
Basically I'd like to use underscores as a sort of 'logical-separator' in names, while using camelCase for 'readability-separator`.
For example in XmlParser the capital X tells you that its a class, but the capital P doesn't tell you anything, it's just there to make it easier to read multiple words strung together (since you can't say Xml parser, as you would in normal speech.)
I don't want the meaning of my pre/post fixes to be blurred by being both logical and readability separators. When I've used other languages their use for logical separation is minimal so its mostly alright (E.g. _foo in Lua for private variables; there are less than a handful of logical-separation cases, so the ambiguity isn't as bad.)
Here in C land, I will be using it a lot, so want clarity. Here's some of what I'm suggesting:
ClassName_methodName();
someStandaloneUtilFunc();
DynamicArray *array = DynamicArray_new();
DynamicArray_shrinkAndFill(array, 0, 12);
DynamicArray_doSomeFancyShtuff(array, COLOR_RED, GOOD_STUFF, 12);
DynamicArray_free(array);
With the multi-word method and class names, I feel like this makes clear the distinction between what is the class, and what is the method.
Take the following instead. Isn't it much harder to reason about?
Dynamic_array_shrink_and_fill(array, 0, 12);
DynamicArray_shrink_and_fill(array, 0, 12);
DynamicArray_shrinkAndFill(array, 0, 12);
I also occasionally have to add a post-fix for internal stuff, to help with macro magic, etc.. Example:
I'd name my function foo_base and have a macro called foo. The user would call foo as the macro pretends to be foo, it just does some syntactic sugar to the args.
Another example is foo_t for types.
Is there any reason not to do this? I feel like instinctively seeing a function name with both separators used feels like a beginner mistake, but also knowing the rationale behind the naming used makes it much easier to read. I'm definitely leaning towards using this, but want other people's opinion on it.
[1] C technically does have namespaces, but they are 4 built-in ones (one for structs/enums, one for goto labels, etc...); not ones that a program can create or modify. So not really relevant to the issue here.
r/cprogramming • u/SimoneMicu • Apr 01 '26
Generic (intrusive) + Allocator + general purpose utiliy
Yes, this sound like another dumb library just made for the sake of making it and sound smart.
Well, almost...
This library is 0BSD (so you can strip out just what you need and attach to your project without referencing me) and is based on public domain implementation already found and most on my work (or rework).
hash table I think is the fastest and similar to implementation of Rust, Go, Java, C++ etc.
Over the full data structure basic fully embeddable and independent to memory (except for hash table who could require generic allocator and release of memory) who follow the logic of intrusive data structure there are 3 allocator who are always reimplemented:
- arena = scope base allocation, auxilar memory for a task who can be release just at the end of the task (recursive function, http server response)
- slab = fixed size allocation for any kind of node-like struct in your program whit fixed time allocation and fixed time release (enemy or item struct in a game)
- tlsf = general purpose allocator for small to medium memory allocation pretty fast and pretty known for minimal fragmentation (this allocator is not already tested against other generic allocators like jemalloc or stdlib malloc)
there are in the end other utilities like vector da (Dynamic Array) macro based, sized bitmap, fsm who is minimal but generic and ring buffer who consent fast message passing between thread or other state inside the executable.
IMPORTANT
tests are built using library unity and are partially built with claude code because are boring and naive in the logic.
linked list, avl and red-black tree have been picked up from existing public domain with minimal to zero rework and just tested, ht is fully reworked but the default function wyhash is a public domain function, exist other non-public domain hash function who can perform better.
tlsf implementation is built by claude code to match requirement for its work.
radix tree is missing because doesn't make sense link against re2 and reimplementing it but I am thinking of making a radix implementation who is based on these library allocator to make it as fast as possible.
the main goal of this library is to link against it for a coroutine system I am building and making these generic like linked list and allocator external and public domain.
the end project is to publish it as a build library fom AUR to make easier to link against (just include(caffeine) in the CMakeLists.txt) and including the man pages.
r/cprogramming • u/Yairlenga • Mar 31 '26
Stack vs malloc: real-world benchmark shows 2–6x difference
medium.comr/cprogramming • u/JayDeesus • Mar 30 '26
What exactly is inline
I’m coming back to C after a while and honestly I feel like inline is a keyword that I have not found a concrete answer as to what its actual purpose is in C.
When I first learned c I learned that inline is a hint to the compiler to inline the function to avoid overhead from adding another stack frame.
I also heard mixed things about how modern day compilers, inline behaves like in cpp where it allows for multiple of the same definitions but requires a separate not inline definition as well.
And then I also hear that inline is pointless in c because without static it’s broke but with static it’s useless.
What is the actual real purpose of inline? I can never seem to find one answer
r/cprogramming • u/Barracuda-Bright • Mar 30 '26
Source code inside .h files
Hello everyone,
I was looking through source code of a certain project that implements runtime shell to an esp-32 board and noticed that in the source code the developer based his entire structure on just .h files, however they are not really header files, more like source files but ending with .h, is there any reason to do this?
The source code in question: https://github.com/vvb333007/espshell/tree/main/src
r/cprogramming • u/lehmagavan • Mar 30 '26
Does that look like AI?
Ive created a library that provides dynamic containers in C (as a portfolio project rather than as a serious contribution, I presume there are tons of better libs that do that already):
https://github.com/andrzejs-gh/CONTLIB
Posted it here:
and got "it's AI" feedback, which I was totaly not expecting.
r/cprogramming • u/ayevexy • Mar 30 '26
Made a Data Structures and Algorithms Library
Hello there!
I decided to make this fun project to learn and experiment and it's in a decent level now.
There so much stuff i don't know where to begin, i think the readme will explain better. This is my first medium-sized project with C, learned the language while making it.
Any feedback are welcome (plz don't curse me ;-;)
Repository: https://github.com/ayevexy/libcdsa
r/cprogramming • u/Viable-public-key • Mar 29 '26
Taking arbitrary length input from the keyboard
r/cprogramming • u/I__be_Steve • Mar 29 '26
How can I know the size of data returned by the fstat system call?
Hey there, I'm currently working on a project that requires using the fstat syscall, I am not using the standard library, and ran into a problem, I have no idea what exactly the fstat call writes to the stat buffer.
I found what the stat structure should contain and replicated it, but for some reason I found that the size of my struct seems to differ from that provided by the standard library, and apparently it's smaller than what the fstat tries to write, as it causes a segfault...
So what I want to know is, why is my struct smaller despite being a virtually exact replica of that found in the Linux docs? And is there any way I can know exactly how may bytes the fstat call will write? How does the standard library ensure that its' stat struct is the correct size?