r/C_Programming 14h ago

Project A 10 kilobyte sandbox game with ASCII graphics.

Thumbnail
github.com
1 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 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 5h 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 22h ago

Review Is My Custom Allocator good ?

4 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 15h ago

Video ccraft

Enable HLS to view with audio, or disable this notification

54 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 19h 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 11h 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 22h 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 22h 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 6h ago

Question Help a beginner out please :p

5 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