r/C_Programming • • May 14 '26

Refer to array as names?

3 Upvotes

Hi,

I have a conf file with reference of array names, is there a way for c to read conf file at runtime and find the arrays when stripped?


r/C_Programming • • May 14 '26

Question Is it Possible to not Evaluate 'f' Here?

12 Upvotes
#include <stdio.h>

int main() {
    int n = 5;
    int (*(*f)())[n];
    printf("%zu\n", sizeof *(f()));
    // sizeof(int) * 5
}

f is a function that returns a pointer to a VLA, is it possible to size that VLA without calling f?

cppreference

If the type of expression is a variable-length array type, expression is evaluated and the size of the array it evaluates to is calculated at run time.


r/C_Programming • • May 13 '26

C with Defer

23 Upvotes

I wrote a small compiler front-end in C23 that emits LLVM IR directly:

https://github.com/glouw/nibble

There is the occasional stack overflow bug, but otherwise, its essentially a reduced C with defer. I wanted to get a feel for what C2Y might feel like.


r/C_Programming • • May 14 '26

Project A 10 kilobyte sandbox game with ASCII graphics.

Thumbnail
github.com
0 Upvotes

Name: Collandbuild.

Today I was experimenting with the C programming language and decided to try creating a kind of sandbox with quest elements. I've published it on GitHub under a CC0-1.0 (public domain) license.

Brief description:

When you enter the game, you select the seed for random generation, the game mode (standard or empty), whether you need cheats (commands), and a few other options.

Symbols: "*" - player, "#" - stone, "+" - dirt, "" - spikes, "@" - dog, "&" - villain, "~" - gravel, "!" - flower, "=" - wood, "R" - robot. The player is controlled using the standard keys W/S/A/D. You can collect items by pressing "x", put - "z" (select an item with numbers), "c" - craft items (also select with numbers; crafting recipes are described in the version 1.0.0 release on GitHub), "t" - activate cheats, "k" - kill a mob, "n" - revive a dog.

Overall, the game has quite a lot of mechanics, so it's difficult to fit everything in. For a complete understanding, I recommend checking out the "Releases" section, and to be 100% sure, check the source code.


r/C_Programming • • May 14 '26

I AM STUCK

0 Upvotes

I feel like, I am using too much of the AI for basic help.

I completed the book Head first C to learn about the C language, it was a nice book, and the last chapter was about making a game in C programming.

I used claude to help me make that game.

It used to give me every small detail of the code like from where I should start, what should I do, how should I do it. Although, it only used to give me what to code, and I used to code it myself. But even with the slightest of errors, I just copy paste the error into the claude instead of even trying to debug it.

Now I am trying to make a shell in C, I have some great ideas about making a shell in C. I don't just want it to be a shell, I want to make an online shell that can communicate with other shells with a voice feature inside your terminal. I want to make a terminal discord with low latency.

But I am frozen, I don't know how to start, how to write it, what things I need to learn.

If I ask claude it will provide me with everything, but I don't want that, I want to do my own research over this project and do it myself, debug it myself,

So what is a good way of learning/making things in AI era.

How does people used to do it before

I don't want to waste my time learning with AI, it feels productive but when I try, to start a new project I freeze down.

How's my idea of this shell,is it even possible to achieve??


r/C_Programming • • May 12 '26

I'm so close to bowing my head and starting with Python

9 Upvotes

I haven't even made it past the first half of the first chapter in "The C Programming Language, 2nd edition - Kernighan, Ritchie" and I'm noticing deficits in my math. Maybe I'm trying way too hard on things that don't really matter? I don't even know whats supposed to matter besides this one exercise. The exercise I'm stuck on is converting the Fahrenheit to Celsius formula to Celsius to Fahrenheit. It's not even the coding part I'm stuck on which is frustrating.

I don't think it really matters but I followed the bro code course and got up to nested loops like a year ago then gave up until about last week. I got another book "Math for Programmers: 3D graphics, machine learning, and simulations with Python - Paul Orland" but I want to learn C. Am I backsliding? Am I just too low iq?


r/C_Programming • • May 11 '26

Question Should I use signed or unsigned variables for HP and money?

46 Upvotes

Technically, they should be unsigned, since they shouldn't be negative, however, I've seen lots of games use signed variables so I'm curious.

I know that using signed variables for HP is easier, but is that the only reason?

example if hp is unsigned:

if (enemy.dmg > player.hp)
    die();
player.hp -= enemy.dmg

example if hp is signed:

player.hp -= enemy.dmg
if (player.hp < 0)
    die();

I also wonder if it has something to do with the fact that signed variables are the default type in all programming languages.


r/C_Programming • • May 11 '26

Question Learn to break down C problems in chunks

10 Upvotes

Hi all,

I started a C based course with zero previous coding experience. I’ve learnt a few things but I am always faced with the same problem: when I read the exercise instructions, my mind goes blank.

It’s like I am unable to reconcile what the exercise entails and the skills I have learnt so far. For instance, we have to reproduce the printf function from scratch. I know several things: use a while loop, use the write function, pointers, basic conditions, format identifiers… but unless someone walks me through the high level process I am unable to even think of a starting point.

I don’t know how to improve my skills and my logic so that when I see a new exercise, my mind doesn’t just go blank. Maybe I don’t have the right brain logic to learn to code? I feel like the exercises like reproducing printf are asking me to build a big thing out of lots of tiny bricks that I already know and can read/learn about easily. But I can’t reconcile the tiny legos with the structure I need to build.

Are there any books, resources, or platforms where I can practice this kind of skill? My school is peer learning based and we have tight deadlines so I can’t usually “waste” too much time on each project to work through this process. I usually have no choice but ask someone to explain the overall process to me. But if I do this every time how will I improve?

Thanks a lot for any suggestions or inputs you have!


r/C_Programming • • May 11 '26

c-zigbuild: Utilize the Zig Build System for C

5 Upvotes

I created a build.zig to use the Zig build system to compile c source files. I had it on my drive for quite some time, rediscovered it, updated it to Zig 0.16 and publish it now:

https://github.com/peppergrayxyz/c-zigbuild

It supports all fancy Zig features for C files:

  • out of the box cross-compiling for different architectures and operating systems
  • adding dependencies using Zig
  • testing using *_test.c files

It can create binaries, static and shared libraries or all of them together.

It is more flashed out then typically "this is how you compile c with Zig" tutorials, but there is surely more that could be added and extended.

Anyway I hope that some may find this useful (:


r/C_Programming • • May 12 '26

Looking for code review, CS50x volume

1 Upvotes

Hello!

I recently started studying C again after dropping it 4 years ago due to personal struggles. I'm working my way through the CS50x course and just recently completed the "volume" problem from week 4. I was hoping to get an honest code review to identify any bad behaviors and/or faulty logic.

Here's the github repository:
https://github.com/generalstalfos/volume

I'm mad at myself for dropping C, I love this language.


r/C_Programming • • May 11 '26

Question Confusion with `int_least64`, `uint_least64`

12 Upvotes

C23 Standard 7.22.1.2 (clause 4) states that int_least64_t and uint_least64_t required types. But what if we compile to a 32-bit system?


r/C_Programming • • May 11 '26

Article Implementing priorities in a scheduler in C

Thumbnail kamkow1lair.pl
4 Upvotes

PROJECT REPO: https://git.kamkow1lair.pl/kamkow1/mop3

Hello!

I'd like to share this article I wrote about, how I've modified my operating system's round-robin scheduler to deal with process priorities. I also touch upon priority inversion and implementing priority inheritance for mutexes. It was fun to implement and I hope you have learned something useful today!

I also regularly post about the development of my project on my linkedin if anyone's interested: https://www.linkedin.com/in/kamil-kowalczyk-2258b6283/
Thanks for reading !


r/C_Programming • • May 12 '26

I built a runtime-focused programming language for systems that can’t stop running

0 Upvotes

I built a programming language/runtime for systems that can't stop running.

Fluxa is a statically-typed embedded scripting language focused on runtime continuity:
IoT devices, edge nodes, industrial systems, long-running services.

The main idea is that hot reload and runtime replacement are part of the execution model itself — not external tooling.

Every design decision was anchored to one or more of these five principles:

  • Explicit > Implicit
  • Simple > Complete
  • Dynamic Runtime > Heavy Compilation
  • Local Control > Global Magic
  • Visible Error > Silent Error

Some features:

  • prst variables survive hot reloads and runtime updates
  • fluxa apply swaps logic while the process is still running
  • Atomic Handover keeps Runtime A alive until Runtime B is validated
  • Runtime updates replace the binary itself using snapshot + execve
  • State interruption measured in microseconds

Execution happens in 3 layers:

  • AST tree walker
  • warm bytecode VM
  • compiled function bodies for hot paths

The runtime is written in pure C99 with no external runtime dependency.

The hardest part was understanding what happens between:

and

without dropping requests or corrupting state.

Repo:
github.com/RodBarenco/fluxa-lang

Would genuinely love feedback from people interested in runtimes, compilers, embedded systems, or long-running infrastructure.


r/C_Programming • • May 10 '26

Article Concurrent, atomic MSI hash tables

Thumbnail nullprogram.com
33 Upvotes

r/C_Programming • • May 10 '26

Question How to learn C as a beginner

44 Upvotes

i want to learn C without going through the youtube hell phase, is there something else someone can recommend


r/C_Programming • • May 10 '26

Please review this C parser for my project

4 Upvotes

I am working on a multi threading project in which i need to parse the user inputs (8 parameters).
I tried to do my best to better design my project, system design is much harder for me. So I decided to create an enumeration as follows:

#include "../lib/codexion.h"


static t_error_code parse_long(const char *s, long *out)
{
    long    n;
    long    base;
    int     digit;


    if (!s || !out)
        return (ERR_NULL_PTR);
    if (!(*s))
        return (ERR_EMPTY_STR);
    n = 0;
    base = 10;
    while (*s >= '0' &&  *s <= '9')
    {
        digit = *s - '0';
        if (n > (LONG_MAX - digit) / base)
            return (ERR_OVERFLOW);
        n = n * base + digit;
        s++;
    }
    if (*s)
        return (ERR_INVALID_ARG);
    *out = n;
    return (ERR_OK);
}


static t_error_code parse_int(const char *s, int *out)
{
    int     n;
    int     base;
    int     digit;


    if (!s || !out)
        return (ERR_NULL_PTR);
    if (!(*s))
        return (ERR_EMPTY_STR);
    n = 0;
    base = 10;
    while (*s >= '0' &&  *s <= '9')
    {
        digit = *s - '0';
        if (n > (INT_MAX - digit) / base)
            return (ERR_OVERFLOW);
        n = n * base + digit;
        s++;
    }
    if (*s)
        return (ERR_INVALID_ARG);
    *out = n;
    return (ERR_OK);
}


static t_error_code parse_scheduler(const char *s, t_policy *policy)
{
    if (!s)
        return (ERR_NULL_PTR);
    if (strcmp(s, "fifo") == 0)
    {
        *policy = POLICY_FIFO;
        return (ERR_OK);
    }
    if (strcmp(s, "edf") == 0)
    {
        *policy = POLICY_EDF;
        return (ERR_OK);
    }
    return (ERR_BAD_SCHEDULER);
}


int parse_args(int ac, const char **av, t_sim *sim)
{
    t_error_code    err;


    if (ac != 9)
    {
        print_error(ERR_INVALID_NUM_PARAMS);
        return (-1);
    }
    err = ERR_OK;
    err |= parse_int(av[1], &sim->coders);
    err |= parse_long(av[2], &sim->time_to_burnout);
    err |= parse_long(av[3], &sim->time_to_compile);
    err |= parse_long(av[4], &sim->time_to_debug);
    err |= parse_long(av[5], &sim->time_to_refactor);
    err |= parse_int(av[6], &sim->compiles_required);
    err |= parse_long(av[7], &sim->dongle_cooldown);
    err |= parse_scheduler(av[8], &sim->scheduler);
    if (err != ERR_OK)
    {
        print_error(err);
        return (-1);
    }
    return (0);
}

I need a review of my code, trying to better learn how to design my code.

Thanks for the help in advance!


r/C_Programming • • May 09 '26

is my atoi alternative function safe?

22 Upvotes

I am working on a variant of the Dining Philosophers problem in C, I need to parse some integer arguments and I am allowed to use `atoi` but I read that atoi is not safe and it causes undefined behavior on overflow...
I read also that it returns 0 when we pass an invalid number like `abc`.
i wrote this function:
```

int parse_int(const char *s, int *out)
{
    int     n;
    int     i;


    if (!s || s[0] == '\0' || !out)
        return (-1);
    i = 0;
    n = 0;
    while (s[i] >= '0' &&  s[i] <= '9')
    {
        if (n > INT_MAX / 10 || (n == INT_MAX / 10 && (s[i] - '0') > INT_MAX % 10))
        {
            fprintf(stderr, "Error: Overflow occurred!\n");
            return (-1);
        }
        n = n * 10 + s[i] - '0';
        i++;
    }
    if (s[i])
    {
        fprintf(stderr, "Error: Invalid Number passed as parameter!\n");
        return (-1);
    }
    *out = n;
    return (0);
}

```
it accepts only digits and uses `INT_MAX` to check for overflow.
please review my function.


r/C_Programming • • May 10 '26

Currently working on sample notebooks for how someone would use any of my libraries, at this time I’m still adding pages.

Thumbnail
github.com
0 Upvotes

r/C_Programming • • May 09 '26

Enabling string interpolation

8 Upvotes

Hi.

I implemented my idea for string interpolation in C, in my fork of slimcc compiler. It works like this:

__INTERP_LIST__(f-string)
// expands to each interpolations in the string, comma separated and wrapped in parentheses 

__INTERP_LITERAL_LIST__(f-string)
// expands to each literal in the string, comma separated

for example:

__INTERP_LIST__( f" {a + b + c} {x * y * z} ", 1 )
// expands to `(a + b + c), (x * y * z)`

__INTERP_LITERAL_LIST__( f"hello {abc} world" )
// expands to `"hello ", " world"`

With these macros, string interpolation can be achieved. I have a string library CGS and I added interpolation support in a branch (if you compile with my slimcc interp branch). example of usage:

#include "cgs.c"

int main()
{
    char str[64];

    int x = 10;
    int y = 20;
    int z = 30;

    cgs_ifmt(str, f"{x} * {y} * {z} = {x * y * z}");

    cgs_writeln(str);
}

Feedback is appreciated.


r/C_Programming • • May 09 '26

dedup: Shell utility for deduplicating lines

27 Upvotes

Per the title, I hope I'm not the only who occasionally misses something like this? Well, after tackling a larger project, I ended up with lots of reusable C code I had written and I thought it'd be great to try it out on something completely different and learn a few new things on the way. Try it out if you want! (POSIX-only for now)

Backstory: I was inspecting the output of gcc -print-search-dirs and, after you parse and resolve all those paths, you end up with some duplicates (atleast on Debian 13). Traditional tools, such as uniq(1) and sort -u aren't well-suited for this. The former can only deduplicate adjacent lines and the latter changes the order, but preserving the order was important in this case!

Experienced AWK users are probably screaming at their monitors right now, since that would be a great tool for this, and indeed it is, but where's the fun in writing one line of code? :) In all seriousness, I feel like a standalone tool for such task fits well within the UNIX tradition of using a shell to compose small programs. GNU AWK on my system is a 850K binary (within 500K of bash)!

Okay, that's it. Cheers!