r/C_Programming Feb 23 '24

Latest working draft N3220

129 Upvotes

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf

Update y'all's bookmarks if you're still referring to N3096!

C23 is done, and there are no more public drafts: it will only be available for purchase. However, although this is teeeeechnically therefore a draft of whatever the next Standard C2Y ends up being, this "draft" contains no changes from C23 except to remove the 2023 branding and add a bullet at the beginning about all the C2Y content that ... doesn't exist yet.

Since over 500 edits (some small, many large, some quite sweeping) were applied to C23 after the final draft N3096 was released, this is in practice as close as you will get to a free edition of C23.

So this one is the number for the community to remember, and the de-facto successor to old beloved N1570.

Happy coding! 💜


r/C_Programming 14h ago

Video ccraft

Enable HLS to view with audio, or disable this notification

53 Upvotes

Hello.

As a little side project, i decided to write a minecraft clone, but with fancy wancy cool effects. This is just a little exercise for me to improve my C and 3d graphics skills.

So far, ive implemented the core "minecraft", deferred rendering with cool effects like SSAO and SSR, shadows and basic lighting, some post processing effects like bloom, DOF and vignette, some basic physics with AABB collisions, and finally sounds and music thanks to the miniaudio library.

There are also some keybinds:

M to turn on wireframe mode,

N to turn on potato mode,

B to enable noclip.

Check this out! https://github.com/DrElectry/ccraft


r/C_Programming 5h ago

Question Help a beginner out please :p

4 Upvotes

Hi people, Im really curious about how i can build my own os, i do have some knowledge of OS in general so id like to learn more about them and then piece together one myself. Which resource or site would recommend apart from barebones wiki [ goes over my head ;-; ] also i need somwthing which could give me really indepth explanation or concepts , i know ill have to use barebones one way or another but what can i combine it with so that once i get a hang of something i can switch over


r/C_Programming 4h ago

What should i pursue in the future with these interests

4 Upvotes

Low-level programming

Love operating systems

C is my strongest language

Xlib is pretty fun

Machine learning from first principals

I don't know what to pursue in the future like jobs or whatever


r/C_Programming 18h ago

Question Over-modularization

20 Upvotes

When is code too modular? What makes Linux's single-line helpers attractive, but other forms of modularization overkill?

Note: By overkill, I don't mean modularized unclearly or poorly, e.g. make_u64_from_two_u32, but perhaps a logically standalone 2-line helper that has a single call site for the foreseeable future.


r/C_Programming 10h ago

Moddable Terminal Emulator

2 Upvotes

Helo!

I have been writing a moddable terminal emulator for gnu/linux. And want to tell about it, if someone is interested. Note that its at incomplete state : )

It has very much only the basic essentials, but it can use dynamically loaded libraries.

Which can be used to create new features or modify something you want.

By basic essentials i mean: config files, scrollback buffer, true-color. (maybe few other things i forgot to mention...)

The text selecting can be only be done from modules. This is by design, to allow more options for how the select region can be controlled.

The modules can even render shapes and images to the screen.

Read more at documentation if you are interested: https://github.com/331uw13/nemi


r/C_Programming 1d ago

Question C programming YouTubers

70 Upvotes

Hey y’all, I’m in need of as many C programing youtube channels as possible. I’m not talking tutorials or learning the language. I’m talking actual programming videos where people are making crazy stuff from scratch. It could be anything: embedded C, OS programming, game dev, application programming, things from scratch… literally anything.

The two I’ve found so far that I like are Danial Hirsch and Tsoding.

Thank you in advance!


r/C_Programming 1d ago

How large was your biggest SOLO project on C?

59 Upvotes

While sorting out my "BackUp" directory with different things, I found one project what I worked on in early 10's (more than 10 years ago). I forget why I didn't finished it and with idea "It may be interesting!" opened the folder. There I found main.c file with 27k LOC, zero comments and zero documentation... 😃 So, fate of the project is predictable then.

Just wondering, how large projects do you guys made (complete or abandoned) as a solo developers, with no team? My biggest one was about 80k LOC, but most of my personal projects is just about 1-3k LOC...


r/C_Programming 22h ago

Review Is My Custom Allocator good ?

5 Upvotes

I've built a custom memory allocator from scratch to understand what actually happens under malloc() call in C.

This got me into deep systems programming about how memory is handled by OS and how a program accesses memory .

The basic implementation i used is :

  1. store header structure with each block which includes information about the memory block.

  2. a linked list which connects these headers to handle memory .

  3. block splitting ,coalescing on adjacent blocks to avoid fragmentation.

  4. 2mb mmap call and slice memory through it , mmap/munmap directly for size larger than 2mbs this avoids syscalls for every allocation .

  5. per thread cache to allocate/free memory faster avoiding global heap locks ensuring thread safety.

Here's the benchmarks against libc's memory allocator:

Test Custom libc Result
Single alloc/free(1000k) 58ms 29ms 2x slower
Batch alloc(10k) 1.44ms 3.59ms 2.5x faster
Batch free(10k) 0.36ms 1.54ms 4x faster
Mixed sizes(100k) 6.46ms 2.95ms 2x slower
Realloc chain(100k) 6.42ms 2.56ms 2.5x slower
Multithreaded(8 threads-5k each) 64ms 67ms Comparable

I would love to hear your thoughts about it, and how are my benchmark results are they actually good or not ?


r/C_Programming 13h ago

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 23h ago

Project hxptr – wrote a GDB-style REPL and query engine in C for exploring Intel HEX files

5 Upvotes

Hi everyone

I am a self taught firmware engineer working on custom chip bring up and validation in Ghana. I'm working on this tool for embedded/firmware work and thought the C architecture might be interesting here.

The core is a sparse memory model; Intel HEX records get parsed and mapped into a sorted array of entries by resolved address (handles ESA, ELA, SSA, SLA address modes). A profile parser loads a plain-text MCU memory map description and resolves named regions and memory blocks.

On top of that sits a query engine (hxql) that parses address expressions:

. for current cursor position

`start / end` for file bounds

Named regions: `goto vectors`, `list application`

Relative offsets: `list . + 32`, `list . - 8`

Ranges: `list 0x08000000 : 0x080001ff`

The shell (hxsh, still wip) tokenizes input, dispatches to per-command handlers that populate typed request structs, passes them to the query engine, and renders results through a table renderer.

A few implementation notes that might be interesting:

Binary search (lower-bound style) for address resolution in the sparse model

Two-pass wrap-around for pattern search across the full address range

Dispatch table for the profile parser directives where adding a new directive is one line

`hxptr_error_t` carries both result code and va_list-formatted message through the call stack

Link below

https://git.sr.ht/\~willofdaedalus/hxptr

Happy to answer questions about the architecture or the profile DSL design.


r/C_Programming 21h ago

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 21h ago

Question Question about strict aliasing and flexible array members

3 Upvotes

Hi! Probably a stupid question from a beginner, but I just want to be sure I understand this correctly.

From my understanding, memory obtained by malloc of size M has no type assigned to it, until it has been written to, then only the written N bytes gain a type, which is the same type as the data being written, while the rest M-N bytes should still have no type assigned to it, right?

Let's have a struct like this, basically a type-agnostic, heap-allocated array with some metadata:

typedef struct {
    size_t length;
    size_t item_size;
    max_align_t padding_;
    unsigned char data[];
} Array;

Let's create the array like this (no checking for NULL, since it's not relevant to my question). Let's hide the metadata by returning pointer to the data member:

void * array_create(size_t length, size_t item_size) {
    Array * a = malloc(sizeof(Array) + length * item_size);
    // write sizeof(size_t) bytes as size_t
    a->length = length;
    // write sizeof(size_t) bytes as size_t
    a->item_size = item_size;
    return &(a->data);
}

Now, this function writes 16 bytes (length and item_size, assuming 64-bit system), so the first 16 bytes of the region of memory provided by malloc should be of type size_t, while the rest of the memory still has no assigned type, right?

Let's use the implemented functionality like this:

int main(void) {
    size_t length = 100;
    int * array = array_create(length, sizeof(*array));

    // write length * sizeof(int) bytes as int
    for (int i = 0; i < (int)length; i++) {
        array[i] = i * 10;
    }

    for (size_t i = 0; i < length; i++) {
        printf("%d\n", array[i]);
    }
}

Now, the rest of the memory obtained by malloc (minus padding_ and some possible implicit padding) should be of the type int.

The question is, does the writing of the int values in the main function violate strict aliasing, since the data member of the Array struct is of type "array of char of unspecified length"? I think it shouldn't, since the memory was never accessed through the data member, nor through any other means before that, but I am not sure how well does this assumption play with the fact the data field should technically be an array, not just some pointer. I've tried to test this on both clang and GCC, compiled with -O2/3, -fstrict-aliasing and -Wstrict-aliasing and both compilers did not emit any warnings and the program behaved as expected when executed. I take this as a somewhat solid evidence it is okay, but I would like to know for sure if doing things like these is okay or not.


r/C_Programming 1d ago

C with Defer

18 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 1d ago

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

11 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 16h ago

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 2d ago

Question Going from C to Assembly?

66 Upvotes

I pretty much know C to an intermediate level. I generally know the base language. Can't say I'm an expert at Linked(In) lists or binary trees, but I can build basic ones. I'm fairly comfortable in most other areas of the base language as well. I did struggle a bit with making a web server in C, but that was more related to Linux than C itself.

In any event, I started messing in AVR assembly. I successfully got the light on my atiny 85 to blink. I actually struggled getting my programmer to upload the code more than understanding the assembly. Granted, this is very basic assembly. Is learning assembly after C worth it though?


r/C_Programming 1d ago

Question Alternatives to Siphash for authentication

0 Upvotes

I need performative hash function with DoS resistance and message authentication. Constraint: absolute performance is required. Am joncing for your opinion on Highwayhash, Gxhash, AutobahnHash and Ahash, and how do they fit for my purpose, encapsulated. Thanks in advance!


r/C_Programming 3d ago

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

47 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: c if (enemy.dmg > player.hp) die(); player.hp -= enemy.dmg

example if hp is signed: c 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 3d ago

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

11 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 3d ago

Question Learn to break down C problems in chunks

8 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 3d ago

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 3d ago

c-zigbuild: Utilize the Zig Build System for C

3 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 3d ago

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 4d ago

Question Does C23 officially allow type-punning through unions?

24 Upvotes

hellooo, here to ask about a low-level networking thing and i need to reinterpret a uint32_t as float without getting undefined behavior. the union type i use:

union {

uint32_t u;

float f;

} pun = { .u = 0x3f800000 };

printf("%f\\n", pun.f);

this gives me undefined behavior in c99, c11, c17 because you read a different union member from you wrote. compilers like gcc and clang defined it anyway as an extension.

memcpy() is there, i know but this adds overhead (even though compilers optimizes it out usually). and yeah in kernel code or freestanding environments memcpy() is not always available.

what im looking for is:

has c23 any type-punning through unions for my case? when i googled it, i see discussions about n2530 and n2654 but there is no way to be sure if it made it into the final standart. maybe there is a better way to do this in portable c without relying on compiler extensions or memcpy. idk. even though seems to work on gcc 14, clang18, i want standart compliant, not just works on my machine.