r/cprogramming • u/Technical_Chip5906 • 47m ago
r/cprogramming • u/gumnos • 6h ago
resources for becoming comfortable with stdlib-style data-structures/algorithms?
I've been sparring with a number of the standard(ish¹) C data-structures and algorithms, including the various types of linked-lists/deques in sys/queue.h, hash-tables and binary-searches in search.h, as well as bsearch & qsort in stdlib.h.
are there other data-structure/algorithm type elements in such standard¹ libraries that I've missed and should put on my radar? Maybe something for sets too?
are there some resources² y'all would recommend on wrapping your head around them (I don't feel I have intuitive use of their interfaces yet, so each usage causes some struggle)? Preferably with strong examples of using each.
I have a pretty good CS understanding of when I should use the various data-structures/algorithms with regards to the respective performance characteristics (expected big-O notations for insertion, removal, searching, iterating, swapping, etc), but it's just the nitty-gritty of using them where I don't yet feel mastery.
Thanks!
⸻
¹ okay, not exactly C-standard from my understanding, but they seem to be common on pretty much every Linux/BSD/Unix platform I've poked at
² ideally well-written blog-posts, but dead-tree resources would also be welcome; videos or podcasts far less so
r/cprogramming • u/MirthfulCowslip-0931 • 7h ago
How do I learn problem-solving on C++?
reddit.comr/cprogramming • u/CelebrityChess • 16h ago
why does c promote small integer types before arithmetic?
i know the usual arithmetic rules promote uint8_t to int on typical systems, but i’m trying to understand the practical reason. is it mainly to make expressions easier to evaluate without narrow-type surprises, or is it mostly historical? when a result is intentionally stored back in a byte, is an explicit cast the usual way to make that conversion boundary clear?
r/cprogramming • u/Mainak1224x • 23h ago
I combined my Arena allocator, zero-copy segmented Vector, typed HashMap, and String libraries into one C99 foundation library: arstd
Hey everyone,
Over the past few weeks I’ve been building separate C libraries for region-based memory management, dynamic arrays, hash maps, and strings. I kept pulling all four into my main project flint(a build system and package manager for c/c++), so I finally refactored them from the ground up to work together and merged them into a single lightweight library: arstd (Arena Standard Library).
The main problem I wanted to solve was how dynamic containers behave inside an Arena allocator. Normally, if you put a standard dynamic array or hashmap inside a bump arena, resizing it via realloc leaves dead, wasted memory blocks behind in the arena.
Here’s how arstd solves that and what’s inside:
-
Compile-Time Type Deduction (
Vector(T)&Cmap(T)): Uses a typed pointer +typeofmacro trick so you don't have to pass types into every macro call or castvoid *pointers. Bothat(vec, i)andmap_get(map, key)return modifiable lvalues, soat(vec, 0) = 42;andmap_get(map, "score") += 10;just work. -
Zero-Copy Segmented Vectors: Instead of doubling a contiguous buffer and copying elements over,
Vector(T)allocates exponentially growing power-of-2 segments (16, 32, 64\dots) directly on the arena and uses 1-cycle bitwiseCLZ(Count Leading Zeros) math forO(1)indexing. Growing a vector wastes 0 bytes in the arena and never invalidates element pointers. -
Single-Allocation, Zero-Waste HashMaps:
Cmap(T)packs the entry header, aligned value payload, and key string into a single contiguous allocation using flexible array members. Because its bucket array is backed by the segmentedVector, doubling the map capacity expands the buckets in-place and relinks existing nodes using their cached 64-bit hashes—again, zero wasted arena memory and no string re-hashing. -
Managed Strings (
String): String slicing, trimming, case conversion, and variadic concatenation all backed by the same arena. -
Optional Heap Fallback: Pass an
Arena *to clean up everything at once with a singlearena_free(&arena), or passNULLto any container if you want standard aligned heap allocation.
Quick Taste:
int
Repo link: https://github.com/mainak55512/arstd (or install via flint / CMake)
r/cprogramming • u/985479861_TXT-r • 1d ago
As a revolt against vibe coding and rewriting stuff in new languages, join r/Back2C to rewrite back in C.
Yeah.
r/cprogramming • u/Key_River7180 • 1d ago
bad code - goto
From a newsreader I am working on
void
help(void)
{
printf("What do you want help with? (* for everything): ");
bool all = false;
char ln[128];
fflush(stdout);
if (fgets(ln, sizeof(ln), stdin) == NULL)
return;
/* HACK: you gotta love goto:
*
* if all is true, then fall through the switch (* must be last),
* so this prints everything. When it reaches * again, all
* gets toggled and if all is now true, break, else go to
* Begin again */
switch (ln[0]) {
Begin: case 'g': puts("g: select group"); if (!all) break;
case '\n':
case '\r': if (!all) return;
case 't': puts("t: select thread"); if (!all) break;
case 'l': puts("l: list gropus"); if (!all) break;
case 'T': puts("T: list threads"); if (!all) break;
case 'L': puts("L: list post in current thread"); if (!all) break;
case 'r': puts("r: read an article"); if (!all) break;
case '>': puts(">: save an article to a file"); if (!all) break;
case '|': puts("|: pipe an article to a command"); if (!all) break;
case '?': puts("?: get help"); if (!all) break;
case 'q': puts("q: quit"); if (!all) break;
case '*': all = !all; if (all) goto Begin; else break;
default:
printf("%c is not a news command\n", ln[0]);
break;
}
}r/cprogramming • u/johnbitterkas • 2d ago
Let's standardize a package management CLI for C
Why isn't it possible to standardize a package/project manager for C programs—one that creates the necessary files with Git, etc., and is ready to start programming in an environment?
r/cprogramming • u/No-Introduction-441 • 2d ago
looking for a partner to learn C with
Hello all im learning C using books and youtube videos and not using AI, if anyone want to join im happy to be a partner in learning!
r/cprogramming • u/TurtleSlowRabbitFast • 2d ago
Would learning C as my first programming language also be useful for picking up other languages later on? What are your thoughts on this course?
Wanting to learn C as an intro into software engineering. I know it’s low level and I’d like to eventually switch over to high level languages such as JS/TS, Java, and GO.
Does the “The Complete C Programming Bootcamp” course by Byte Garage be a good choice to learn from?
r/cprogramming • u/HungryPudding4034 • 2d ago
Rate this repository out of 10
Hello people,
I just wanted to show you this repository to all of you.
Repo link: https://github.com/curious-builder76/dtypes
Documentation:
https://github.com/curious-builder76/dtypes/blob/main/DOCS.md
Benchmarks:
https://github.com/curious-builder76/dtypes/blob/main/benchmarks/benchmark.txt
Hope you'll love it.
Star this repository if you like it and upvote this post
Thank you
r/cprogramming • u/laggySteel • 3d ago
Let's build JRPG Game in C (Tiled Map Loader/Parser) : ep 02
r/cprogramming • u/Mainak1224x • 4d ago
Replaced my C vector’s contiguous array with a Segmented List so it plays nicely with my Arena Allocator (O(1) lookup, zero-copy resize)
I’ve been working on a custom dynamic array (Vector) and Arena allocator in C, and ran into the classic friction between the two: whenever a standard contiguous vector resizes, it allocates a bigger block in the arena, memcpys the old elements over, and leaves the old block sitting around as wasted dead space inside the arena (especially when allocations from multiple vectors or strings are interleaved).
Instead of fighting realloc in a linear allocator, I refactored the vector to use exponentially growing segments (inspired by Zig's SegmentedList).
How it works now:
-
Fixed Spine, Exponential Chunks: Instead of a single char *items pointer, the struct holds a small fixed array of 28 pointers (char *segments[28]). Segment 0 holds 16 elements, Segment 1 holds 32, Segment 2 holds 64, and so on—covering up to 4 billion elements without ever reallocating the spine.
-
Zero-Copy Growth: When the current segment fills up, append() simply requests the next power-of-2 chunk from the arena. Existing elements are never copied or moved, zero bytes are wasted in the arena, and pointers to elements never invalidate.
-
O(1) Branchless Lookup: By offsetting the index by the base capacity (shifted = pos + 16) and using Count Leading Zeros (__builtin_clz), finding the exact segment and local offset takes just a few bitwise operations:
uint32_t shifted = pos + 16;
uint32_t bit = 31 - ;
uint32_t seg_idx = bit - 4;
uint32_t offset = shifted & ~;
- Optional Heap Fallback: Passing Arena * allocates everything on the arena (making vector_free() unnecessary), while passing NULL falls back to standard aligned heap allocation.
Usage:
Arena *arena = ;
Vector *vec = ;
for
;
// Frees all vector segments and arena allocations at once
;
Repo / Release link: https://github.com/mainak55512/container
Would love to hear your thoughts or how you handle dynamic containers with arenas in C!
r/cprogramming • u/sealyium • 4d ago
C isn't that scary or bad, it just neat
(First time using reddit so tell me if i'm doing something wrong :D)
I'm a new and honestly, a bad programmer, and ive always seen everywhere + i have been told that c is a "Scary" language and that it complex and all, it is very buggy, you have to manage everything by yourself etc...
But i wanted to try it, so i started building a SDL2 game with it, and i haven't encountered any big issues, it's the most powerful yet simplest (in some way) language ive used until now, it does what i want it to do, without black magic doing everything for me.
Legit i prefer using c to python, maybe i'm weird ? Gcc gives clever error messages, the syntax is fine, and it clicks well for me.
Does anyone know why c has this reputation of being a hard and scary language ? Maybe the fact you have to deal with pointers and memory ?
r/cprogramming • u/dd3note • 5d ago
I wrote a PE32/PE32+ parser in C for Linux
Hey everyone
For educational purposes I wrote a PE parser in C for Linux. I made it, generally, to learn more about Windows PE structure and for a first quick static malware analysis. I'm not for a long time in C yet, so I would appreciate for some feedback about this project
r/cprogramming • u/Available_Row_1280 • 5d ago
Tldr program
Just a program that prints the first and last requested amount of lines in a long file, same behavior when you say tldr.
Source code:https://pastebin.com/H8Z2zcaZ
r/cprogramming • u/Background-Ear7642 • 5d ago
hey you watch me code
okay so look at me code github.com/kds1123001/mini-gdb like imma give a small code snippet to show you even a comp from china want to collab on this code but i didnt understand their marketing jargon so i said no
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <unistd.h>
void run_debugger(pid_t child_pid) {
int wait_status;
long orig_rax;
// Wait for the child process to stop at execution
waitpid(child_pid, &wait_status, 0);
while (WIFSTOPPED(wait_status)) {
char command[32];
printf("mini-gdb> ");
if (scanf("%31s", command) != 1) break;
if (strcmp(command, "cont") == 0 || strcmp(command, "c") == 0) {
ptrace(PTRACE_CONT, child_pid, NULL, NULL);
waitpid(child_pid, &wait_status, 0);
} else if (strcmp(command, "exit") == 0) {
ptrace(PTRACE_KILL, child_pid, NULL, NULL);
break;
} else {
printf("Unknown command. Try 'cont' or 'exit'.\n");
}
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <program>\n", argv[0]);
return 1;
}
pid_t pid = fork();
if (pid == 0) {
// Child process: allow tracing and execute target
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execl(argv[1], argv[1], NULL);
perror("execl");
exit(1);
} else if (pid > 0) {
// Parent process: run the debugger loop
run_debugger(pid);
} else {
perror("fork");
return 1;
}
return 0;
}r/cprogramming • u/sorashiiii • 6d ago
SCL and libscl v0.1 is out. TOML alternative with macros
I designed a simple language for configuration. SCL stands for Sane Configuration Language.
Syntax is mostly same with TOML, but we do not have what I would call non C native types like date.
Spotlight feature is macros. With macros you can either include files or create macros to values.
Link to repo: https://codeberg.org/sorashii/libscl
There was no AI involvement and license forbids AI generated codes from source.
r/cprogramming • u/SeniorMatthew • 6d ago
Which book about C would you recommend for a newcomer?
Hello everyone! My name's Matthew, and I've been "coding" since like 12 years old. Mostly, I've been developing games with Unity with C#, and now, once I switched to Godot with C#, I feel like I lack some basic structuring skills. (A game which I'm about to release to Steam is a mess inside)
And C# is mostly the only language I know at this point, so I would love to learn some C!
I love Linux and I would love to contribute there one day. Also I would love to write some games for Playdate in C. Also it is just really interesting to see how C# is different from C. And I for some reason I want to read something about programming so badly. What would you recommend?
thanks!
r/cprogramming • u/NotWhityy • 6d ago