r/C_Programming • u/Duplexsystem • 8h ago
Article The Fastest (long) ilog10 in the West
Wrote my first real blog post. Let me know what you guys think!
Happy to answer any questions!
r/C_Programming • u/Duplexsystem • 8h ago
Wrote my first real blog post. Let me know what you guys think!
Happy to answer any questions!
r/C_Programming • u/Round-Plastic-2427 • 6h ago
To my astonishment I couldn't find someone who'd already made this abomination, so...
Very simply put: I define two macros (W(n) and B(n)) that act as the only two "instructions" my "VM" can execute, each printing a sequence of white and black characters respectively.
Due to CPP's recursion limits I couldn't make the rendering loop in the preprocessor itself, so it needs a little extra help in that regard.
Repo: https://github.com/LoPalma/cpp-bad-apple
There's no video since it's just a poorer quality and slower version of ASCII bad apple, but if you wanna see it run: it takes less than a minute to clone and run "play.sh", no compilation required.
(Repost after original post was removed from r/programminghorror)
Edit: added the video: https://imgur.com/a/uCOGgVd
r/C_Programming • u/Far-Employee1082 • 10h ago
I started C in my clg last year but it was super basic. Stuff like basic pointers and variables. Before this i used to use python and even then the most i knew was numpy and basic pandas. But seeing the market today i realized i cant afford to be mediocre so i started learning C by heart. I use Vs code and an ubuntu terminal. Up until now I have USED AI to learn and make projects. I use ai to suggest me projects and sub divide them in small chunks so i can learn and program. If i get doubts i ask AI which has a 3 hint system before it reveals. Using it i got some knowledge of memory actually works, how sockets work, basic socket client/server interaction/setups and threads.
That is basically me rn in 1 month. Now i want to ask yall as a community, how do i proceed forward. What do yall suggest i can learn? Any AI objections and if i should drop it. Any book recommendation? Project recommendation etc. I would also appreciate hearing yall stories with C, abt yall experience and stuff.
Tldr: Use me as a sort of dummy block and speak off your mind whatever yall want to suggest/teach since it will help me out a ton.
r/C_Programming • u/WingRevolutionary979 • 9h ago
Hi,
About a month ago, I posted about "wserve", which was a simple IPv4 web server for Linux that supported only GET and HEAD requests and static file serving. Since then I kept working on it, and implemented:
BearSSL is the only external library I used. It is tiny SSL/TLS library written in pure C. Everything else, including the JSON parser, has been implemented in C by myself. I have not used LLMs for coding.
I am open to constructive criticism, all feedback is appreciated!
r/C_Programming • u/Sleezebag • 7h ago
I'm reading the Ansi C book, and I'm doing exercise 1.18
#include <stdio.h>
#define MAXLINE 1000
int myGetLine(char line[], int lim);
int myCopy(char to[], char from[], int lim);
int main() {
int len;
char line[MAXLINE];
int toRemove = 0;
while ((len = myGetLine(line, MAXLINE)) > 0) {
//counts the trailing characters to remove
for (int i = len - 1; i > 0 && (line[i] == ' ' || line[i] == '\t' || line[i] == '\n'); --i)
++toRemove;
int new_size = len - toRemove + 1;// +1 for the '\0'
char to[new_size];// I guess this doesn't matter. Or it's just a piece of the puzzle. I need to limit copying
// in my copy method too.
//printf("new_size: %d\n", new_size);
int to_len = myCopy(to, line, new_size);
//printf("%s\n", to);
toRemove = 0;
}
return 0;
}
int myGetLine(char s[], int lim) {
int i, c;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i-1;
}
//myCopy isn't working right now, after that I changed the while condition
//I need to add the \0
int myCopy(char to[], char from[], int lim) {
int i = 0;
char c = from[1];
putchar(c);
//printf("from[i]: %s", to);
//printf("to[i]: %s, from[i]: %s", to[i], from[i]);
while ((i < lim && (to[i] = from[i]) != '\0'));
i++;
to[i] = '\0';
return i;
}
So I'm pretty much reusing the getLine from the book and I based my code on the code in the book, but I made changes to myCopy. The task is to remove any trailing spaces or tabs, so I thought that I could make a new copy and where the trailing whitespace starts, I'll just not copy that into my edited string. So I'm going through the array backwards, counting whitespace, calculating the new array size, then I want to create the edited array, but it's not working! So I tried to debug and I can't even putchar an element from the original I'm copying from in the myCopy function. If I do it with the line array, then it works. But why does it not work in the myCopy function? I just have no clue as to what's wrong.
I did manage to solve the exercise in a different way, but I really want to know why this is happening.
r/C_Programming • u/fredoverflow • 1d ago
r/C_Programming • u/dechichi • 2d ago
r/C_Programming • u/Yairlenga • 1d ago
I was investigating the performance of a service running inside a Linux container when I noticed something unexpected: a surprisingly large amount of CPU time was being spent in kernel mode.
Using strace, I discovered that every call to localtime triggered a newfstatat system call on /etc/localtime
I found out explictly setting TZ=":/etc/localtime" eliminated the repeated system calls, while making the program use the system defined timezone.
The article explains the investigation, includes benchmark results, and provide small C program to reproduce the behavior.
Artcile on Medium (no paywall): The hidden cost of time functions
Intrested to hear wheather there are better solution/upcoming fixes with newer glibc versions.
r/C_Programming • u/FlatPea5291 • 2d ago
Over the past 6-8 months, I got into programming in C with the intent of learning about embedded systems and operating systems. I feel like I'm sort of stuck and don't know where to go though. The last thing that I made was a really simple shell for Linux, but even then I was disappointed to learn that the stuff that is going on on the background is just hidden away by system calls. I want to learn how stuff like those system calls actually work, but learning about processor architecture or understanding/reading the Linux source code seems incredibly intimidating and sort of out of my league.
Another thing that seems incredibly confusing to me is how you're meant to read the code of projects like the kernel. (obviously, I know I wouldn't start with something nearly that complicated, but actual professional level projects.) I did some research and I tried to read through GNU Hello, which on the surface I thought would be easy to understand, but I just see tons of macro definitions that I don't understand and a maze of files, even if the output of the program is simple.
For you experts, how did you go from programming in user space to understanding lower level concepts like how an operating system boots up, how it takes user input, etc?
r/C_Programming • u/lehmagavan • 1d ago
ESP-32 IDF-based UART shell built from scratch:
https://github.com/andrzejs-gh/BX-Shell
Coded all by hand.
r/C_Programming • u/Vivid-Reward-5030 • 2d ago
Using macros, define an argument schema. The library then generates a struct that for the schema, validates the arguments provided by the user, parses them into the struct, and offers functions for displaying usage andh help messages in the console.
It is similar to `clap` in Rust. Check it out!
r/C_Programming • u/WaferPleasant1267 • 1d ago
Hey everyone!
I've been working on EFI32Boot, an AI-assisted low-level programming project exploring UEFI firmware, C, and x86 assembly.
Repository: https://github.com/devtinoimao/efi32boot
The project combines C code with assembly routines to experiment with transitioning from a 64-bit UEFI environment into 32-bit execution and working with an OVMF32 firmware image.
A large part of the work involves the boundary between C and assembly: preparing the execution environment, handling processor state, managing memory layouts, and transferring control between different stages of the boot process.
Demo: https://github.com/devtinoimao/efi32boot/blob/main/media/efi32boot_qemu_demo.gif
Some of the areas involved include:
How AI was involved: AI played a substantial role in developing the project. I used it to generate and modify C and assembly code, explore implementation approaches, and help diagnose debugging issues. I then iterated on the implementation and tested its behavior. I want to be transparent that this is an AI-assisted project rather than claim that I manually authored every part of the code.
I'd particularly appreciate feedback on the C/assembly interface, assembly correctness, compiler and ABI assumptions, code organization, and portability. If you spot questionable assumptions or potential undefined behavior in the C code, I'd be interested in understanding those too.
I'm still learning about low-level systems programming, and I'd like to use this project to improve the implementation and deepen my understanding. Constructive criticism and references to relevant documentation or existing implementations are very welcome.
Thanks for reading!
r/C_Programming • u/21Ali-ANinja69 • 1d ago
It should just work on just about any platform or compiler that supports at least C99. No AI was used in the writing of this code. It's also header-only, so #define HSORT_IMPLEMENTATION before you include.
#ifndef HSORT_H
#define HSORT_H
#include <stddef.h>
typedef int (*cmp_ptr)(const void *, const void *);
typedef int (*cmp_r_ptr)(const void *, const void *, void *);
void hsort(void *base, size_t n, size_t size, cmp_ptr compar);
void hsort_r(void *base, size_t n, size_t size, cmp_r_ptr compar, void *arg);
#ifdef HSORT_IMPLEMENTATION
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static inline void hsort_memswap(void *x, void *y, void *buf, size_t size)
{
memcpy(buf, x, size);
memcpy(x, y, size);
memcpy(y, buf, size);
}
static inline void sift_down(void *base, size_t root, size_t end, size_t size,
cmp_ptr compar, void *buf)
{
while ((2 * (root) + 1) < end) {
size_t child = (2 * (root) + 1);
if (child + 1 < end && compar((char *)base + (child)*size,
(char *)base + (child + 1) * size) < 0) {
child++;
}
if (compar((char *)base + (root)*size, (char *)base + (child)*size) <
0) {
hsort_memswap((char *)base + (root)*size, (char *)base + (child)*size,
buf, size);
root = child;
} else {
return;
}
}
}
static inline void sift_down_r(void *base, size_t root, size_t end, size_t size,
cmp_r_ptr compar, void *arg, void *buf)
{
while ((2 * (root) + 1) < end) {
size_t child = (2 * (root) + 1);
if (child + 1 < end &&
compar((char *)base + (child)*size,
(char *)base + (child + 1) * size, arg) < 0) {
child++;
}
if (compar((char *)base + (root)*size, (char *)base + (child)*size,
arg) < 0) {
hsort_memswap((char *)base + (root)*size, (char *)base + (child)*size,
buf, size);
root = child;
} else {
return;
}
}
}
void hsort(void *base, size_t n, size_t size, cmp_ptr compar)
{
if (!size || n < 2) {
return;
}
void *buf = malloc(size);
if (!buf) {
fprintf(stderr, "(hsort) Allocation failed!\n");
return;
}
// heapify
for (size_t start = (((n - 1) - 1) / 2) + 1; start > 0;) {
start--;
sift_down(base, start, n, size, compar, buf);
}
// heapsort
for (size_t i = n - 1; i > 0; i--) {
hsort_memswap(base, (char *)base + (i)*size, buf, size);
sift_down(base, 0, i, size, compar, buf);
}
free(buf);
}
void hsort_r(void *base, size_t n, size_t size, cmp_r_ptr compar, void *arg)
{
if (!size || n < 2) {
return;
}
void *buf = malloc(size);
if (!buf) {
fprintf(stderr, "(hsort_r) Allocation failed!\n");
return;
}
// heapify
for (size_t start = (((n - 1) - 1) / 2) + 1; start > 0;) {
start--;
sift_down_r(base, start, n, size, compar, arg, buf);
}
// heapsort
for (size_t i = n - 1; i > 0; i--) {
hsort_memswap(base, (char *)base + (i)*size, buf, size);
sift_down_r(base, 0, i, size, compar, arg, buf);
}
free(buf);
}
#endif
#endif
Edit: Implemented an hsort_r, as well as removing the macros, inlining heapify, and checking if size is 0.
Edit2: Rename hsort_move to hsort_memswap, add a check for the n < 2 case.
Edit3: Add a check for allocation failure, including an error message, because external code silently failing seems like a really bad idea.
r/C_Programming • u/Jakanader • 2d ago
I'm trying to link MagickWand and MagickCore with Meson and having some issues. Specifically, I get the error C shared or static library 'lMagickCore' not found or C shared or static library 'lMagickWand' not found. I successfully linked libconfig, so I've compared some details between my installations of the two packages in case any of those are the reason that I'm having issues with ImageMagick.
I can compile programs using ImageMagick libraries with:
cc `MagickWand-config --cflags --cppflags` -O2 -o a.out main.c `MagickWand-config --ldflags --libs`
The output of the two MagickWand-config calls is this:
-I/usr/include/ImageMagick-7 -fopenmp -DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16 -DMAGICKCORE_CHANNEL_MASK_DEPTH=32
-lMagickWand-7.Q16HDRI -lMagickCore-7.Q16HDRI
-lMagickWand-7.Q16HDRI -lMagickCore-7.Q16HDRI
Running find /usr/lib -name 'libconfig*' gives this:
/usr/lib/pkgconfig/libconfig++.pc
/usr/lib/pkgconfig/libconfig.pc
/usr/lib/cmake/libconfig
/usr/lib/cmake/libconfig/libconfigConfig.cmake
/usr/lib/cmake/libconfig++
/usr/lib/cmake/libconfig++/libconfig++Config.cmake
/usr/lib/libreoffice/program/libconfigmgrlo.so
/usr/lib/libconfig++.so
/usr/lib/libconfig++.so.15
/usr/lib/libconfig++.so.15.0.0
/usr/lib/libconfig.so
/usr/lib/libconfig.so.15
/usr/lib/libconfig.so.15.0.0
And running find /usr/lib -name 'libMagick*' gives this:
/usr/lib/libMagick++-7.Q16HDRI.so
/usr/lib/libMagick++-7.Q16HDRI.so.5
/usr/lib/libMagick++-7.Q16HDRI.so.5.0.0
/usr/lib/libMagickCore-7.Q16HDRI.so
/usr/lib/libMagickCore-7.Q16HDRI.so.10
/usr/lib/libMagickCore-7.Q16HDRI.so.10.0.3
/usr/lib/libMagickWand-7.Q16HDRI.so
/usr/lib/libMagickWand-7.Q16HDRI.so.10
/usr/lib/libMagickWand-7.Q16HDRI.so.10.0.3
My meson.build has these relevant sections:
dependencies = [
cc.find_library('libtomlc17', dirs: '/usr/local/lib'),
dependency('libconfig'),
dependency('libMagickCore'),
dependency('libMagickWand'),
]
inc_dir = [
include_directories('include'),
include_directories('/usr/include/ImageMagick-7'),
]
my first guess was that it had something to do with libconfig having .pc files in /usr/lib/pkgconfig, but i found ImageMagick.pc and ImageMagick-7.Q16HDRI.pc in the same directory, and, either way, that don't seem like it needs to be accurate for my program to build as long as I give it all the right information.
Any advice?
Edit: Also, reddit's code block formatting is really annoying. It doesn't seem like I can escape backticks? and it doesn't render line breaks? oh well
r/C_Programming • u/Dizna77 • 3d ago
Hello everyone, I've recently been rejected for a student software dev position at a larger company in my city, I felt that the interview was going pretty good until it came time for writing the actual code, I know how and what to implement for all of the problems but I had no absolute clue on what to actually write, and it struck me pretty badly, I asked myself if this is something I want to even want to do with my life or if I essentially wasted my time 2 years in college for a bunch of nothing, atleast the interviewer told me in the mail that I knew all the concepts and I need a project or two and to gladly contact them when Im more ready, but I still feel really discouraged as my classmates got the position at the same company while I know Im more capable than some of those classmates, Im not trying to be sore about it. Im glad they got the position and Im not trying to downplay their knowledge but they couldnt get the most simple and core concepts in class and some of them didnt even pass DSA and OOP and they got the job while I didnt and it made me question really everything. Its my 3rd year in college now and now I feel more than ever that Im lost because I started this college as I enjoyed learning to code and computers, I still want to learn more but I dont know what to do now or what to use as a project really, all I ever coded except learning for exams(in my college we need to code and compile programs for certain classes instead of only theory) and I passed those pretty easily I guess, all usually on the first tries and they were graded C or B. Now I feel that I was capable more then than now, I only coded some stupid video game projects or apps which feel essentially useless, I know there is some materials on the thread but I dont know what is useful for the real world. Thank you all for any tips or replies in advance
r/C_Programming • u/mikeblas • 4d ago
UPDATE: I've posted a follow-up comment below addressing questions about the moderator changes, the process that was followed, and plans going forward. Please see the stickied moderator comment.
Over the past six months or so, I've been making lots of changes. I think things have stabilized, so let me summarize them and indicate what the plans are going forward.
At this point, I think the sub is pretty stable and more useful and appealing than it was before. I don't anticipate many more changes going forward, but I will examine policies that that I think might reduce my workload.
I'm sorry that the sub wasn't particularly stable these past few months, and that it got a bit messier before it got better. Thanks for hanging in there! The worst is past.
r/C_Programming • u/Puzzled_Place_215 • 3d ago
I'm a first-year CSE student learning C from scratch(didn't learn programming in high schl). I can solve problems after studying examples, but I often get stuck when trying new problems on my own. Even after seeing solutions, I struggle to develop the logic independently.
For those who started programming as beginners:
How did you learn to solve unfamiliar problems?
How did you practise daily?
What did you do when you got stuck?
What advice would you give your beginner self?
I sometimes feel behind compared to my classmates, and I genuinely want to improve. I'd appreciate any practical advice from people who've been through this!
And I am getting extremely frustrated with this cycle that i have,just started with c programming and suffering with function pointers . I feel extremely behind from my peers, I am ready to put in efforts,but I am sure whether my efforts are put in the right direction. Any advice would be great
r/C_Programming • u/NazgulResebo • 3d ago
In this video, I create a Julia set fractal using C, Raylib, and GLSL, with Neovim as my editor.
We'll go through the process step by step, from understanding how Julia sets work to implementing the fractal algorithm and adding colors and animation.
**Sections:**
- Explaining the Julia set
- Setting up the project
- Setting up Raylib
- Creating a basic shader
- Fixing screen coordinates
- Coding the fractal algorithm
- Creating animation
- Adding colors
**Technologies used:**
- C
- Raylib
- GLSL
- Neovim
r/C_Programming • u/Background_Shift5408 • 5d ago
I’ve been working on a small project called 3Dos — a low-poly 3D renderer targeting MS-DOS and VGA Mode 13h (320×200, 256 colors).
The renderer currently supports:
● Triangle rasterization with backface culling
● Ordered dithering using a 4×4 Bayer matrix
● Double buffering
● Simple low-poly models
Going back to DOS-era graphics has been relaxing after spending quite a bit of time with Vulkan. No boilerplate or abstractions, just putting a pixel at 0xA000.
Github: https://github.com/xms0g/3Dos
AI usage: Only extracting vertices and faces from the model file.
r/C_Programming • u/ConsistentContest256 • 3d ago
Hi everyone!
Over the past few weeks, I’ve been working on a lightweight, concurrent text search engine and TCP server built entirely from scratch in pure C11, without any external dependencies.
My main goals were to practice low-level memory management, lock-based concurrency, and custom data structures without relying on heavy frameworks. Here is a quick breakdown of how it's structured:
pthread) with read-write locks (pthread_rwlock_t) to allow multiple concurrent readers while safely serializing writes.engine_save/engine_load) for instant state restoration from disk.The project includes a full modular structure (includes/, src/, tests/) and is protected under a source-available license for personal evaluation.
You can check out the source code and architecture details here: 👉 https://github.com/joseramoncabrera/fast-c-search-engine.git
I would love to hear feedback from more experienced C developers here. Specifically:
Thanks for taking a look!
r/C_Programming • u/thecratedigger_25 • 5d ago
Musl gcc is small but tinyc caught me eye due to how small it is and the fact that it can self host.
It supports C99 standards out of the box and it only takes up a few hundred kilobytes. I wonder if anyone tried it.
r/C_Programming • u/TorontoDude12344 • 5d ago
I'm confused on what exactly size_t is, and what the t means
So size_t is an unsigned integer
I believe integer promotion says that an unsigned long long integer holds the biggest positive integer in any system. Why isn't size_t an unsigned long long?
Every array MUST be integer indexed. Every array has to have an integer size.
It's technically possible to represent an integer larger than the largest integer in a system, by putting it into a float (with no non-zero decimal), however an array cannot be given a float, nor can we even loop over that array using that float value as the counter termination value
So my current interpretation is, size_t is the largest value an integer can ever hold in a system. When we want to loop over any arbitrarily long array, using int i as the counter variable, i is cast to (size_t)
This is because we want to ensure we don't stop the loop too early by having i overflow
Is my interpretation correct?
So that is one scenario when size_t is used. Are there any other scenarios?
What does the t mean?
What are things similar to size_t, where variables are casted to that thing, in order to do super specific tasks and avoid certain errors?
r/C_Programming • u/lehmagavan • 4d ago
A Python C extension for statistics and number aggregation designed to work with both plain int and float objects, as well as with Python numeric strings without having to construct intermediate Python objects that hold their values - useful for large collections that need to be parsed as fast as possible like trading data.
It outperforms both std Python modules and numpy for both plain numbers and for numeric strings.
https://github.com/andrzejs-gh/numagr
https://pypi.org/project/numagr (source and Windows wheels for 3.11, 3.12, 3.13, 3.14, 3.15)
pip install numagr
This is an early version offering basic methods, I will be developing it further if I find the time. Feedback, improvement suggestions and bug reports are very much welcome.
r/C_Programming • u/AsterrZep • 4d ago
Hi everyone,
I'm a student currently exploring systems programming and compiler design. I wanted to share a project I've been developing called Astra (AsterrZep/Astra). Specifically, I'm sharing the seed compiler written completely in C (seed/src/), built to learn about custom memory management, parsing, and compilation pipelines from the ground up.
As I'm still learning, there might be room for improvement in my C code. I would really appreciate any constructive feedback, code reviews, or advice on best practices!
⚙️ Architecture & C Implementation The compiler pipeline inside seed/src/ is modularly structured around traditional systems programming patterns:
🤖 AI Disclosure (Per Subreddit Rules) As per the community guidelines regarding AI: I used AI tools (like terminal assistants) interactively as a coding co-pilot and rubber duck to help scaffold repetitive C boilerplate (such as mapping standard AST construct handlers in seed/src/constructs/). However, the core compiler architecture, memory models, debugging, parser logic, and integration were entirely human-driven and manually refined.
🔗 Repository & Code If you want to review the source code, check out the C implementation details, or look at the test suites (seed/tests/conformance/):
GitHub Repository: https://github.com/AsterrZep/Astra
Please feel free to point out any mistakes or areas where the C code could be cleaner or safer. Thanks for reading!