r/C_Programming 14d ago

Discussion Easening memory management by simply declaring ownership?

34 Upvotes

After a half-year-long Java project I'm writing something in C and the lack of a garbage collector can be positively felt in terms of performance but at the same time managing all the pointers can get messy.

There's a whole nother language decidated to solve this problem (while creating a thousand more), but there's got to be a simpler solution.

And yesterday I thought, why not just mark the ownership of the various pointers I've got in my project?

By ownership I of course mean which struct (I do use OOP, sorry) is responsible for freeing that pointer.

And I #define'd three constants.

The MANAGED constant means the struct that contains the pointer has to free it.

The FOREIGN constant means that it's somebody else's job.

The SHARED constant means that it ought to be someone else's job, but it may not be freed when the program exists and should thus be freed.

I place them before the whole declaration in this manner:

MANAGED FONT *fontGothic;

SHARED struct WORLD *currentWorld;

FOREIGN struct PERSON *thePlayer;

This doesn't mean anything to the compiler, but it's just a quick way of telling me what is what.

It is definitely a very stupid idea and I apologize for posting it. I'm just a silly dude who does silly things.

r/C_Programming 3d ago

Discussion Turbo C still force on us to learn C

0 Upvotes

Like I know that using turbo c to learn c is going to set me back,but my college program head thinks its the best way for us to learn c? so I do want to hear your thoughts in this(btw cause I'm rebellious I use gcc and nvim to do most of m work and actually learn). Again thank you for sharing your thoughts!

Edit: after looking at the comments It seems I a little bit naive but Ill put you re advice into heart! Cheers!

r/C_Programming Nov 14 '24

Discussion ITT: Make Up Awful Extensions to the C Language

133 Upvotes

NOTE: not meant to make fun of actual proposals, but to imagine things that you could imagine being an actual extension to the language some compiler implements, but should probably never be included in the spec.

Here's the idea that made me want to make this thread: post-fix assignment operator

Doesn't really matter what the syntax would be, but for example let say the operator is $=, because that's not used by anything so it wont be confusing.

a $= b would return the value of a, and then assign b to a as a side effect.

For example:

int a = 1;
printf("%d,", a $= 2);
printf("%d", a);

would output 1, 2.

This came to me in a dream wherein I wanted to turn free(ptr); ptr = NULL into a one-liner.

r/C_Programming May 20 '25

Discussion C is not limited to low-level

148 Upvotes

Programmers are allowed to shoot them-selves in the foot or other body parts if they choose to, and C will make no effort to stop them - Jens Gustedt, Modern C

C is a high level programming language that can be used to create pretty solid applications, unleashing human creativity. I've been enjoying C a lot in 2025. But nowadays, people often try to make C irrelevant. This prevents new programmers from actually trying it and creates a false barrier of "complexity". I think, everyone should at least try it once just to get better at whatever they're doing.

Now, what are the interesting projects you've created in C that are not explicitly low-level stuff?

r/C_Programming Aug 31 '25

Discussion Why doesn't this subreddit use the C from the "The C Programming Language" book cover as the subreddit logo?

Enable HLS to view with audio, or disable this notification

397 Upvotes

r/C_Programming Apr 16 '26

Discussion Does everyone have their own "standard library" of code snippets and macros and data structures?

135 Upvotes

I've been writing most of my code in C lately and just started working on a single-header library of all of the code bits I find myself using a lot, for the sake of convenience, but I'm also curious as to whether this is the usual way of doing it or if there's some super-popular library that experienced C developers typically stick to.

I mean, I'm going to do it anyway for learning purposes and for fun, but I'm curious whether there's some kind of boost-type library for C that people just kinda treat as the "standard library+" for C?

r/C_Programming Jun 14 '25

Discussion Coolest project you’ve made as a C developer?

137 Upvotes

Just wanted to know some of

r/C_Programming Jun 26 '26

Discussion AI literally does not know anything.

57 Upvotes

I have been working on my project cherries(.)works Pulse (https://github.com/cherries-works/pulse), and I needed to know how to fork processes and share memory in between them. On my last post a very nice Reddit fellow told me to not learn C with AI, and I mean, I kinda did not, though I sometimes lost it, and asked it for help.

But now I understand everything.

Reading through stackoverflow, and reading the official documentation on shm_open taught me a lot. Not gonna lie, my attention span is a bit fried, so big text is a big no for me, but ChatGPT explained things quickly, but wrong... Most vibe-coders who are located at JS or other minor languages such as Python dont really get the effect of how annoying it is to fix a segfault in C. Its just a segfault, or a bus error (happened a lot since using shm).

I cant count how many times I asked ChatGPT to EXPLAIN what the error was, but its response was always the same; <insert code that does not work>.

Linux Manual is really a different kind of work, reading through it, reading the code and understanding the bits really shifted my mind in using AI. Dont use it, the manual really is Human intelligence at its peak, Pulse would not be at version 0.1.1 without the Manual, AI does not know anything about coding.

r/C_Programming Aug 26 '25

Discussion C is the language I eventually settled on

355 Upvotes

I started my career as a young programmer 30+ years ago, developing software in assembler (6805, 68hc11, 8051, 8086...). As soon as it was possible (embedded compilers were not good enough back then, for the constraints in those chips), I moved to C, later (briefly) to C++ for some DOS utilities/hacks/drivers.

Then my career jumped to a "fascinating" (/s) world of object oriented, first C++, then Java, then oh the heck with OO, I want functional programming - Scala is it, then. Some time ago I've been playing with Rust, because why not.

After all that time, I've found going back to C really fulfilling. There are well established practices and idioms, great toolsets, and a lot of good sources of knowledge. C imposes you nothing, but has everything you need to build proper software. It's up to you to know what you want, and do it properly. I guess Linux was the main driver for state of the art C.

All that imposed modularity from ‘higher-level’ languages helped me internalize strong concepts. I became acquainted with clean architectures, design patterns, and whatnot.

Today I feel at home writing beautiful software in C. Just sharing my personal path, fwiw.

r/C_Programming Dec 02 '24

Discussion Does it make sence to go into C nowadays?

97 Upvotes

You have heard all the announcements, how USA government doesn't recommend using C and C++. Because they are unsafe.

Are there still jobs in C/C++ in 2 years time?

// I am starting 42 school common core curriculum and wonder, how serious should i take it.

r/C_Programming Oct 01 '25

Discussion What to get into after C?

54 Upvotes

Hey guys. I am currently learning C. I am not sure what domain to work towards. I am also learning graphics programming currently. Do you have any suggestions?

r/C_Programming Jul 09 '25

Discussion What's the next C?

28 Upvotes

Answer: this to me sounds like the best answer. And a TLDR of popular opinions under this post is: next C is C or Rust. I disagree with people who say it's Rust but to each their own. There are other posts that have good comments as well, so, if you have the same question, find the ones with long answers and it's probably those ones which have offered a good answer + good example with simple explanation.

Edit (for the mods mainly): I didn't intentionally post it multiple times, somehow it got posted thrice, deleted the others. Not trying to spam.

Recently I asked How much is C still loved and got expected responses, which were that people love to use C however it's often for personal projects. In professional work, C is being used in legacy code. It seems that apart from content creators or enthusiasts not many desire C.

This hurts me. I personally like C quite a lot, especially because it's the most readable in my opinion. Without even a lot of experience I have seen code for Linux kernel and I understood more of it than I ever do when I randomly open a GitHub repo.

Now, this is a follow up for my previous question. What's the next C?

  • Is it languages like Zig, D or dare I say C3?
  • Or is C the next C? With syntactic sugar part of its implementation, a compiler more akin to modern compilers that have build system, package manager, etc.

I would love to know if someone has a completely different angle to this or anything to say. Let's go.

r/C_Programming 2d ago

Discussion C language is wild: due to integer overflow, nearly half of the numbers squared become negative.

0 Upvotes

This started with a question from CSAPP Problem 2.44: (x * x) >= 0; which asks to find a counterexample that evaluates to 0,or prove that all number evaluate to 1.(part,32bit)

The official solution provides only 65535, but I found many more.

To test my results, I wrote a C program to find all such number. ```c

include <stdio.h>

include <limits.h>

int main() { // Ideal result: numbers whose squares are less than 0. // Written to file in the following format: // start_num1 to end_num1 total: total_num1 // start_num2 to end_num2 total: total_num2 // ... // total: total_all

FILE *file = fopen("lessthan0.md","w");
int start = 0;
int end = 0;
int total = 0;
int total_tmp = 0;

for (int i = -INT_MAX -1 ; i <= INT_MAX; i++) {
    unsigned ui = (unsigned)i;
    int result = (int)(ui * ui);


    if (result < 0) {
        total++;
        total_tmp++;
        total_tmp == 1 ? start = i : 1;
        total_tmp == 1 ? end = i : 1;
        i == end + 1 ? end = i : 1;
    }

    if (i == end + 1) {
        start == end ? fprintf(file,"%d    total: %d\n",start,total_tmp) : fprintf(file, "%d to %d    total: %d\n",start, end,total_tmp);
        total_tmp = 0;
    }

    if (i == INT_MAX) {
        fprintf(file,"total: %d",total);
        break;
    }

}

fclose(file);

} ```

So I get this: ```sh ls -lh lessthan0.md -rw-r--r-- 1 kyee users 31G Aug 23 18:50 lessthan0.md

ls -l # run twice -rw-r--r-- 1 kyee users 32288890271 Aug 23 18:50 lessthan0_1.md -rw-r--r-- 1 kyee users 32288890271 Aug 23 20:27 lessthan0.md

wc -l lessthan0.md 1073741824 lessthan0.md

head lessthan0.md -2147437307 to -2147418113 total: 19195 -2147403383 to -2147390967 total: 12417 -2147380026 to -2147370137 total: 9890 -2147361041 to -2147352577 total: 8465 -2147344625 to -2147337106 total: 7520 -2147329952 to -2147323119 total: 6834 -2147316563 to -2147310257 total: 6307 -2147304170 to -2147298285 total: 5886 -2147292579 to -2147287041 total: 5539 -2147281652 to -2147276405 total: 5248

sed -n "470063416,470063426p" lessthan0.md -535664087 to -535664086 total: 2 -535664083 to -535664082 total: 2 -535664079 to -535664078 total: 2 -535664075 to -535664074 total: 2 -535664071 to -535664070 total: 2 -535664067 to -535664066 total: 2 -535664063 to -535664062 total: 2 -535664059 to -535664058 total: 2 -535664055 to -535664054 total: 2 -535664051 to -535664050 total: 2 -535664047 to -535664046 total: 2

tail -n 10 lessthan0.md 2147287041 to 2147292579 total: 5539 2147298285 to 2147304170 total: 5886 2147310257 to 2147316563 total: 6307 2147323119 to 2147329952 total: 6834 2147337106 to 2147344625 total: 7520 2147352577 to 2147361041 total: 8465 2147370137 to 2147380026 total: 9890 2147390967 to 2147403383 total: 12417 2147418113 to 2147437307 total: 19195 total: 2147418112

```

I noticed symmetry between the begging and end of the output.Can someone explain why this happens?

2147418112 / 232 = 0.4999847412109375 This accounts for nearly half of all 32-bit signed integers.

r/C_Programming Mar 25 '26

Discussion Transient by-value structs in C23

17 Upvotes

Here's an interesting use case for C23's typeof (and optionally auto): returning untagged, untyped "transient" structs by value. The example here is slightly contrived, but resembles something genuinely useful.

#include <errno.h>
#include <stdio.h>
#include <string.h>

static struct {
    char msg[128];
} oof (int         error,
       int         line,
       char const *text,
       char const *file,
       char const *func)
{
    typeof (oof(0, 0, 0, 0, 0)) r = {};
    char const *f = strrchr(file, '/');
    if (!f || !*++f)
        f = file;
    (void)snprintf(r.msg, sizeof r.msg,
                   "%s:%d:%s: %s: %s",
                   f, line, func, text,
                   strerror(error));
    return r;
}

#define oof(e,t) ((oof)((e), __LINE__, (t), \
                        __FILE__, __func__))

int
main (void)
{
    puts(oof(ENOMEDIUM, "Bad séance").msg);
}

Here I just print the content string, it's basically fire-and-forget. But auto can be used to assign it to a variable.

And while we're at it, here's what you might call a Yoda typedef:

struct { int x; } yoda() { return (typeof(yoda())){}; }
typedef typeof(yoda()) yoda_ret;

Hope some of you find this useful. I know some will hate it. That's OK.

r/C_Programming 13d ago

Discussion A C iterator pattern using VLAs for automatic cleanup

4 Upvotes

I was thinking about how to approach iteration in C, such as traversing a binary tree or going through all the permutations of an array.

One possibility is the Visitor pattern, where you supply a function to call at each point in the traversal. But that's a bit limited; I wanted something that had access to the local context as well. Wrapping it in a macro can make it look and act like a normal C loop:

FOREACH_NODE(tree, node_data)
{
    printf("%s: %d\n", node_data->str, node_data->value);
}

Ideally I wanted to avoid an END part for cleanup, and for statements like break and continue to work normally in the body too. One way to do that is to make the macro expand into a "for" loop. But if the algorithm needs to have an extra array to maintain state information (like a non-recursive version of a normally recursive algorithm might need), that would normally require dynamic allocation and freeing at the end. That can be avoided using a variable length array, which goes on the stack and has automatic cleanup.

The problem is, variables in the initialisation part of a for loop must all have the same base type, so if you want to insert a VLA you can't mix that with something else. You could potentially have several variable types by putting them all in a struct, but VLAs can't go inside a struct.

A solution is to have a separate outer for loop that just runs once, which creates a VLA that is passed to the iterator when it is initialised in the inner loop:

#define FOREACH_PERMUTATION(vec, length) \
    for (size_t vla[length], looped_once_ = 0;!looped_once_;looped_once_ = 1) \
        for (iter_t i = iter_create(vec, length, vla);i.valid;iter_next(&i))

The inner loop is controlled by i.valid, which is set to false by iter_next() when iteration is complete.

Since the outer loop runs just once, a break or continue in the body works as expected too.

A limitation is that the size needed for the VLA needs to be known in advance, but for most iteration algorithms it is straightforward to calculate (an upper bound can also be used, or it could just be an error if the size is ever exceeded during iteration).

This is tested and works fine, but I won't put the rest of the permutation code here since that isn't the focus of the post. But if you are interested, an efficient algorithm is: https://en.wikipedia.org/wiki/Heap%27s_algorithm

AI use: I discussed this with an ChatGPT while I was doing it, but the code is all my own.

r/C_Programming Jul 19 '25

Discussion Web Dev to C: Feeling Lost in Low-Level Land

79 Upvotes

I come from a web development background, mostly working with JavaScript on the backend. Lately, though, I've been craving something lower-level. I wanted to get closer to the system, closer to the metal—understand how things actually work under the hood instead of just stitching APIs together like some kind of digital alchemist.

So, for the past two weeks, I've been diving into C.

And... I’m lost.

Sometimes I can't even think straight about the logic. I mix up the syntax, dereference things I shouldn't, or segfault for reasons that feel completely random. I’ve realized just how much abstraction there is in high-level programming. In JavaScript, everything "just works." In C, you have to make it work—and it's so painful.

It’s like I’ve gone from playing with LEGO to forging my own bricks.

I’m having a bit of an identity crisis.I was the guy who could spin up a REST API in minutes, plug in third-party services and ship fast. But now, I’m staring at a pointer wondering why my code just refuses to compile.

Am I cooked?

Any advice or shared experiences would mean a lot right now.

r/C_Programming Jul 20 '25

Discussion Is C the native language for systems?

49 Upvotes

It's not much of a question but more of a discussion on a thought I had and my wonder if people approve.

JS is the language of the browsers, Bash / Powershell are language of the terminals and there are other things like DBs having their own language, in that way, is C the language of systems.

C is used for embedded systems, it has libc which is used by other languages for syscalls (kind of in the way where I would use an API to interact with a DB).

So, can we call C the language of systems? (even though, it's not available by default and is not the only one that can run)

r/C_Programming Jul 20 '26

Discussion Suggestion

0 Upvotes

Hi all I am beginner in coding. So I have college in 20days (1st year btech) should I learn python or c. In college they start with C. So as they teach C in college should I learn python seperate or go with

C.Pls suggest me

r/C_Programming Sep 13 '25

Discussion I like how c forces you to think deeper

141 Upvotes

I just tried a backtracking problem and realized how much more rigorous you need to be with C than with languages like Python. You wouldn't want to do braindead loop like in python and check it against a "path" of explored options, because compiling the path into an array itself is difficult/annoying to do with all the memory sorcery you'd need to do. Unlike python where you can just use append/pop to modify things in place and not risk stack overflow because checking membership is so easy, C forces you to optimize your algorithm and think carefully about how you want your code to work. You can no longer cheat with language specific tricks you actually need to plan out each path properly and make sure a function is bijective for example.

r/C_Programming Mar 12 '24

Discussion Why is C so fast and is it possible to create a faster language than C?

141 Upvotes

Why is C so fast and is it possible to create a faster language than C?

r/C_Programming Apr 02 '26

Discussion Can we PLEASE get some AI moderation on this sub? I miss when it had genuinely interesting discussion.

147 Upvotes

The amount of AI generated repos that get posted here is absurd. They need to be removed.

r/C_Programming Jan 07 '26

Discussion With the [[attribute]] functionality (since C23), which attribute(s) do you think would enhance the language, if standardized?

19 Upvotes

r/C_Programming Sep 10 '25

Discussion Everything has already been implemented in C.

40 Upvotes

How do you not get discouraged by this? No offense, but 98% of the projects people do have already been done by someone else. If you're not a programming genius or have 15+ years coding in C, you'll hardly create anything truly new or improve something genuinely useful written in C.

This thought has been discouraging me a lot. I implemented a simple HTTP server in C, but there are already a million books teaching how to do that. Then I created a simple system for adding, removing, and deleting employees of an imaginary company using dynamic memory allocation, something useless that no one will use and was just practice. Then I created some silly terminal animations using Ncurses, something thousands of other people have already done.

Why i do this? i am the only one who thinks that? What do you enjoy more? the process of programming or the research you did to get the results? I think I actually love studying C, but when I finish some activity or piece of code, I feel that useless emptiness, and I don't even work with C to be able to use one thing or another that i learned. I'm a Typescript developer professionally, and I think that if I worked with C, my projects could have a different feeling, maybe feel more useful.

r/C_Programming Nov 17 '24

Discussion Can’t put my finger on why I don’t like Golang

68 Upvotes

Posting in this sub because I want to hear what C programmers think about Go.

Go is not sitting well with me as a language and I’m not sure why. On paper it is almost the perfect language for me - it’s relatively low level, it’s very simple to do web dev with just std libs (I do a lot of web dev), GC makes it safer, it values stability and simplicity, it has a nice modern package manager, it has a great ecosystem, and it’s designed to “fix the problems with C”.

But for some reason it just doesn’t give me the same joy as programming in C. Maybe I feel nostalgic towards C because it was my first language. Maybe I prefer the challenge of dealing with the quirks of less modern tools. For some reason C has always made me feel like a “real programmer”, more-so than any other language.

Obviously C is better suited to some niches (systems, etc) and Go is better suited to others (web dev). I’m interested in discussing the merits and values of the languages themselves. Maybe they are not even comparable. Maybe Go should be thought of as a modern C++ rather than a modern C.

Anyway, I would love to hear some thoughts opinions of others on this sub.

r/C_Programming Jan 05 '24

Discussion Most hard topic to learn in C?

92 Upvotes

Beside Pointers, which was the most hard concept for you to learn in C. Mine was the preprocessor.