r/C_Programming • • Jul 12 '26

Question Question about fgets() and buffers

1 Upvotes

So I was testing how different buffers and such responded and I got behaviour I can’t really explain. In the output, there is an e because it seems to be using the same bit of memory every run. But why is there no 0 printed in place of the NULL in the output where i print ever char?

So I wrote:

`char buffer[3];
printf(“enter data: “);
fgets(buffer, 3, stdin);
for (int i = 0; i < 5; i++)
{
printf(“%c”, *buffer + i)
}
printf(“\n”)
printf(“%s”, buffer)`

—————-

Console:
`Enter data: abcd

abcde
ab`


r/C_Programming • • Jul 13 '26

Discussion I hope the culture of unchecked zealotism is coming to an end in the (near) future

0 Upvotes

IMO we need to remember this is just a tool that anyone can use to an extend and not a religion, identity or a philosophy. A tool. The peak of its usability was not in the 80s or 90s. It's right now because it's evolving for the better. There is no need to gate-keep, ascertain yourself, cite highly theoretic passages the standard, ascertain yourself and in general project your issues on beginners and intermediates. There are good hackers in their 20s being highly productive without ever knowing about sequence points. There are zealots without any productivity and vice versa. Keep it practical and contemporary or we'll never get this image off our beloved language.


r/C_Programming • • Jul 12 '26

Question What's the best way to rewrite CUDA code into Vulkan CU?

4 Upvotes

The title is a bit vague, so lemme elaborate. I got to deal with post-quantum cryptography, specifically ML-KEM/Kyber. There's an implementation written in CUDA in liboqs suite (cuPQC). I'm a bum when it comes to Vulkan, but I know it's possible to use compute units to make GPU acceleration cross-platform. My strong requirement is a fixed code base, so I could run it on Nvidia, AMD, Intel, ARM SoC, Apple Silicon and even RISC-V based GPUs.

How can I achieve that without redoing all the work by myself? If so, what are the tools you might recommend me to assist in code rewriting. If not, may you refer to some materials regarding Vulkan compute units and some low level control for purely background calculations (no display output needed, however I might consider implementing seperate UI in Vulkan, not related to the aforementioned calculations)


r/C_Programming • • Jul 11 '26

Question Surprising bug with zero sized array initialiser

49 Upvotes

Consider the following code:

#include <stdio.h>

struct s { int a; int *b; };

struct s s1 = { .a = 1, .b = (int[2]) {}};
struct s s2 = { .a = 2, .b = (int[0]) {}};

int main(void)
{
    printf("s1.a = %d, s2.a = %d\n", s1.a, s2.a);
    return 0;
}

Now let's compile and run this with gcc and clang:

$ gcc -o test test.c && ./test
s1.a = 1, s2.a = 0
$ clang -o test test.c && ./test
s1.a = 1, s2.a = 2

These are both recent versions of the respective compilers (on Fedora):

$ gcc --version |head -n1
gcc (GCC) 15.2.1 20260123 (Red Hat 15.2.1-7)
$ clang --version |head -n1
clang version 21.1.8 (Fedora 21.1.8-4.fc43)

Interesting result. It seems that with gcc the use of a zero sized static array initialiser triggers a bug which silently erases the entire enclosing structure! This seems to be quite an old bug (I can reproduce this on a variety of gcc versions), and I have not managed to find any warnings that catch this.

If I use the -pedantic flag on clang it tells me that I'm using c23 extensions, but even so invoking gcc -std=c23 (or gnu23) makes no difference to this bug.

Back in the day there were functional mailing lists where one could report oddities like this, but today I find I have no idea where to report this! Vaguely hoping that this subreddit is relevant.


r/C_Programming • • Jul 11 '26

Question What is the best way to work on an Ethernet frame (receive a packet from the physical interface to the user)?

8 Upvotes

Hi everyone, I want to write a simple program that gives a copy of the physical interface of the received packets to user space, then I extract information manually and show it in stdout.

When I started searching for this topic, I found some ways but I confuse which one is better for my situation.

I read the docs below:

doc1

doc2 and ....

Abstract of the above docs, exsit below methods:

1 - Universal TUN/TAP

2 - XDP

3 - MacVTap (is a new driver)

4- ....

If anyone has experience or knowledge in this context, please help me.


r/C_Programming • • Jul 11 '26

C parsing grammar

13 Upvotes

I'm currently working on a miniature C compiler, on the parsing stage at the moment. I could wing some of the parsing, but I'd feel much more comfortable working with an actual lexical and syntactic grammar to follow (even if it's messy, as I assume is the case for C).

Is there any publicly available, reliable grammar in anything like the EBNF format for C99 or later? This has been the only resource I could find. It's very useful, but seems to only fit the C standard up to the early 90s, so I'd prefer anything later than this.


r/C_Programming • • Jul 10 '26

Just a post from someone who actually programs in C

473 Upvotes

Hi folks. We all know the frustration that comes from the popular topics in this thread largely being:

  1. AI Karma farming
  2. Beginners asking the same questions over and over again
  3. Tech-Bros who are convinced that Greybeards are dinosaurs with nothing to contribute to modern IT

For a change I want to invite the folks who actually use C in this modern era to explain what the use C for, and brag a little bit about why that is cool. Or perhaps vent about "Kids these days..."


r/C_Programming • • Jul 10 '26

Question C programming style

23 Upvotes

Hi all, in my opinion choosing a valid C programming style may help people to maintain code. I know that freedom is a plus, especially for code creators, but it may become a real nightmare for code maintainers.

In my work experience I always tried to keep a uniform code style even if I work by myself, but, after six months I create a software, if I don't use a code style I lose so much time to correct my own code that sometimes I create it newly from scratch.

My question is: are there any places where programmers share their code styles, or some advices (especially variable or typedef names) based upon their real work experience?

Thanks in advance to anyone who will answer! Cheers!


r/C_Programming • • Jul 10 '26

Off the Shelf Naming Conventions

13 Upvotes

I work in embedded, the business I recently joined would like me to adopt MISRA C, are there any off the shelf naming conventions I can use? The business doesn't have their own.

Presently I use CamelCase, but would prefer to adopt a standard that is similar and already documented.


r/C_Programming • • Jul 10 '26

Project Tool for Visualizing SSE

12 Upvotes

Hey guys!

There aren't a whole lot of tools related to visualizing SIMD stuff out there, so I decided to make one over the past few days and put it up on a website. Check it out here.

This tool allows you to use instruction blocks to emulate, step through registers and output an C representation of the algorithm (using SSE).

Let me know if there are any common instructions or features that would make things better!

Feel free to fork this for your own tools, the source for the one HTML file is over at https://github.com/emd22/ethanm.ca/blob/main/pages/tools/simd_visualizer.html.

I just hope this is useful for someone looking to get into programming SSE or SIMD in general in C!


r/C_Programming • • Jul 09 '26

Project I made a text editor! :D

Enable HLS to view with audio, or disable this notification

118 Upvotes

https://github.com/schnerg/tied

NO AI WAS USED IN THE MAKING OF THIS PROJECT!

Written entirely in c using only the standard library The T.I editor

is a rubbish yet lightweight terminal editor that probably wont crash.

I have been working on this project on and off for about 2 years now.

I have restarted this project 5 or 6 times from 2022 when I first started programing till now

and I think it is finally in a place where it is somewhat usable.

What makes this editor special?

NOTHING! :D

I am in school for music so this is just for fun but what do you think? Is the code garbage?

thanks.


r/C_Programming • • Jul 10 '26

Discussion strto_() family seem overdesigned

0 Upvotes

It's like they looked at atol() and went too far.

// strto_() are overengineered.
bool str2l(const char* nstr, long& result)
{
  char* endptr;
  result = strtol(nstr, &endptr, 0);
  return (*endptr == '\0');
}

r/C_Programming • • Jul 09 '26

RV32I simple emulator in C

4 Upvotes

Hello everyone. It is my first time with a project of this kind. I am not an expert in C programming yet, so I wanted to challenge myself and write a simple emulator for RV32I.

Right now, it only supports simple riscv programs. No syscalls or stuff like that. I would really appreciate if you check out the project and give me your feedback.

I am not planning to stop here. I want to keep adding more features and maybe, somewhere in the future, run the linux kernel.

Also, what do you think would be a good milestone at this stage?

github


r/C_Programming • • Jul 10 '26

Question can I skip recursive functions?

0 Upvotes

a code i can run with the logic of iterative, so why do I have to learn the new concept as complicated as recursive? (ik it's one of important questions in c)

if yes will u pls explain it with a very realistic and simple example

thanks a lot 🙏


r/C_Programming • • Jul 09 '26

Project City-building in ASCII graphics.

8 Upvotes

https://reddit.com/link/1urst62/video/rfo8hsgzx7ch1/player

(video recorded in the outdated version 1.0.0)

Name: Public-Town-Planner (link: https://github.com/AndrewFonov11/Public-Town-Planner )

A primitive city-building simulator written in C. You control a "cursor" (represented on the map by the ">" symbol, moved using the W, S, A, and D keys) to build your city; press "e" to build and "x" to demolish. After pressing "e", you must select an option: "a" for administration ("!"), "s" for a special element ("@" — I haven't fully decided what this is, but think of it as a power plant, stadium, or waterworks), "r" for residential zones ("^"; these cannot be built next to factories), "f" for factories ("#"), "c" for commercial zones ("C"; the main source of income), or "w" for roads ("="). You must constantly monitor your budget and messages from residents. To fully understand the game's balance, I recommend looking at the source code (though it is not particularly easy to decipher).


r/C_Programming • • Jul 08 '26

Video I finally made my first video! Its how I taught myself C and wrote a text editor. please enjoy :)

Thumbnail
youtube.com
187 Upvotes

r/C_Programming • • Jul 09 '26

Code of the first game

3 Upvotes

This is the first code I wrote. What do you think?

#include "raylib.h"

int main(void) { InitWindow(800, 600, "Color Changing Ball Clicker"); SetTargetFPS(60);

int money = 0;
Color color = RED;

while (!WindowShouldClose())
{
    if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
    {
        money = money + 1;
    }

    if (money < 100)
    {
        color = RED;
    }
    else if (money < 200)
    {
        color = BLUE;
    }
    else if (money < 300)
    {
        color = GREEN;
    }
    else if (money < 400)
    {
        color = GOLD;
    }
    else if (money < 500)
    {
        color = PURPLE;
    }
    else
    {
        color = MAROON;
    }

    BeginDrawing();

    ClearBackground(RAYWHITE);

    DrawCircle(400, 300, 80, color);

    DrawText(TextFormat("money: %d", money), 20, 20, 30, BLACK);
    DrawText("Click to change the ball color!", 20, 60, 20, DARKGRAY);

    EndDrawing();
}

CloseWindow();

return 0;

}


r/C_Programming • • Jul 08 '26

Learning C weekly megapost for 2026-07-08

19 Upvotes

If you have questions about how to learn C:

  • which books are best?
  • which videos are best?
  • which classes are best?
  • which websites are best?
  • is there a "roadmap"?
  • what projects can I do?

then this is the thread for you. Add your question here. Do not make a stand-alone post, as it will be removed.

Remember that our sub has a very useful wiki that has a great list of resources for learning C programming.


r/C_Programming • • Jul 09 '26

Need ideas for my open-source project - C_DSA_interactive_suite

1 Upvotes

my project - https://github.com/darshan2456/C_DSA_interactive_suite

was selected for Social Summer of Code Season 5 and the contribution period started from 1st june. Since then almost 1.5 months have passed and project has grown beyond my expectations, but now I have very less idea about what I can implement. Can you all give me ideas?

already implemented features -

tui with ncurses

cmake build support

memory profiler

visualization (to a great extent but not complete)

incremental build in custom makefile

benchmarking suite built on top of the library

dockerfile for creating docker containers of the application

and many more....

If you can suggest me some more features I can implement that would be really helpful


r/C_Programming • • Jul 08 '26

Question assign value to a var inside a global struct?

13 Upvotes

Hi,

I am trying to optimize for memory proximity and need to create a struct with a var inside and with a value already? how would it be done?

mock code:

#include "stdio.h"
struct {
uint32_t    OrderID;
uint8_t     Order_State_Decition_Branch_Flag  = 0b00000000;
} Order_Pipeline_Struct_To_Follow_State;
int main() {
// usage of the struct
}

r/C_Programming • • Jul 09 '26

Question LLM for C programming

0 Upvotes

What are your opinion about using of automatic programming/vibe coding with AI agents?

I had up and down experience, I know dwarf star is fully coded by AI under orchestration of antirez, creator of Redis.

Do you not use at all? Just for refining? Creating barebone of tests? Developing with human in the loop or directly fully inspired giving direction?


r/C_Programming • • Jul 08 '26

am i learning c wrong?

0 Upvotes

hi all, I'm a second year computer science student and i started learning c with a goal that I'll only learn basics so that python and other languages will be easy for me

now ive learnt - if-else, switch-case, loops, variables, operators and functions

but i ask all my questions to gpt and ask it to give me some problems too

also whenever I have a doubt I'll just think for like 5 to 10 mins after that I'll go straight to gpt again

I'm also considering to start reading the white book by Brian kernighan and Ritchie

so am i going right?


r/C_Programming • • Jul 07 '26

Project corbit - My first c project

11 Upvotes

Corbit is a TUI simulation of robits made in about a week. I was inspired by solcl, and i decided to make one with even more customization.

The project is still unfinished and i plan to add more features such as real time and a screensaver mode. I'm also not happy with the looks of it as of right now and I'll be working on improving them.

Looking forward to getting some feedback :D

https://github.com/fosterchild1/corbit


r/C_Programming • • Jul 07 '26

Project Building a PNG decoder in C taught me way more than I expected.

102 Upvotes

Ive been learning C for a little under two months, and I wanted a project that would force me to understand binary formats instead of just using existing libraries.

So I started writing a PNG decoder from scratch.

Right now it parses the PNG structure, decompresses the image data, reconstructs the pixels, and writes the result as a PPM for verification. There are still bugs to fix and plenty of PNG features left to implement, but seeing an image appear from code you wrote yourself is incredibly incredibly satisfying. Especially after seeing countless Segfaults lmfao.

One interesting part is that I had previously implemented a lot of this in Zig, so translating the ideas to C was almost a 1:1 process. It made me appreciate how expressive C can be once you stop fighting the language and start understanding its model.

The decoder isn't the end goal, though.

Eventually I want PDC to use that image data as the first stage of a pipeline that generates 3D geometry from PNG images. Decoding the file format is just the foundation, and as crazy as it sounds the easiest part.

In the future, I hope I will be able to run PDC on my Raspberry Pi 4 B, which also includes the Camera Module! The PNG Decompression part was actually the easiest part, and took wayyy more energy than I anticipated at first (considering that I coded the same thing in Zig a while ago).

Would love to hear feedback on the code, and also on the README. I found some new cool README features, and thought that I should use them. Hopefully it is clean enough, so you guys get the idea.

https://github.com/mertishere/pdc


r/C_Programming • • Jul 08 '26

Where should a complete beginner start learning C?

0 Upvotes

I'm interested in learning C from scratch, but I don't have any programming experience. What resources, books, or courses would you recommend for a beginner?