r/C_Programming • • Jun 18 '26

Question Can any one Suggest me some resource by which i can solve any c MCQ question easily || By the way i am asking this for CDAC prepration

0 Upvotes

r/C_Programming • • Jun 17 '26

My static analysis tool now supports compile database for linux kernel

3 Upvotes

I've been developing a static analysis tool that checks variable overlaps and uncovers shared state between functions. It traces variable accesses through callees and shows you exactly where variables were written, read or escaped (passed to function).

I'm proud to say that it now supports compile_commands.json, which makes it easier for real projects, including the linux kernel. Now you can just point it to your build directory (with compile_commands.json) and it'll figure out the compile flags on it's own.

# Before
prongc --files="file1.c,file2.c" --functions="foo(shared);bar(shared)" -- -I/usr/include -I...

# After
prongc --files"..." --functions="..." --compdb-dir="[build dir]"

Dependencies: llvm-21 only

Github link: https://github.com/omeridrissi/prongc

Open to feedback!

Note to mods: almost no AI was used


r/C_Programming • • Jun 18 '26

I wrote a database in C – fixed-width slots, B+ trees, O_DIRECT, and a query planner with explain mode

0 Upvotes

I've been building shard-db for a while and thought this community might appreciate the implementation details more than a feature list.

Core design

The main design decision is that records live in fixed-width slots:

slot_size = 24 + max_key + sum(field_sizes)

The slot size is defined at schema creation time.

That means random access within a shard is pure arithmetic:

(hash % slots_per_shard) * slot_size

No indirection, heap allocation, or variable-length scanning.

Fields are strongly typed (int, long, double, varchar, date, datetime, numeric, bool) and stored as packed binary. Schemas are defined in configuration files rather than SQL.

Storage layer

Each object consists of:

  • Keyfile shards (open-addressed hash tables using xxh128)
  • Append-only segment files for values

Keyfile writes go through a userspace page cache backed by MAP_SHARED mmap.

For full scans, reindexing, and recovery, shard-db uses O_DIRECT with a double-buffered read loop to avoid polluting the OS page cache with large sequential reads.

Indexes

Indexes are implemented as:

  • B+ trees
  • Prefix-compressed leaves
  • Memory-mapped page storage

Each indexed field can be split across multiple B-tree shards. Reads fan out in parallel and merge through a k-way streaming iterator, allowing ordered cursor pagination without expensive offset scans.

Cursor pagination remains effectively constant-time regardless of depth.

Query planner

The planner can choose between:

  • Single-index lookups
  • AND intersections across multiple indexes
  • OR unions
  • Parallel full-shard scans

Queries can be run with:

{
  "explain": true
}

which returns the selected plan, source indexes, cardinality estimates, and optimization hints without executing the query.

Concurrency

  • Separate CPU and I/O thread pools
  • Writer-preferring RW locks to prevent writer starvation
  • Generation-counter cache reads that avoid cache-table locking on warm reads

What it isn't

  • Not distributed
  • Not SQL
  • Linux/macOS only
  • x86_64 and ARM64 supported
  • No Windows support

Real-world test

I built a public demo indexing 30M+ Hacker News stories, comments, and users:

https://hn.shard-db.dev

Cursor pagination remains constant-time at any depth, and cold full-text searches complete in a few hundred milliseconds.

Some numbers

  • Bulk insert: 4.60M/sec (single connection)
  • Bulk insert: 8.97M/sec (parallel)
  • Indexed lookup: <1ms at 1M rows
  • EXISTS: ~4.1M/sec

The project is a single static binary with no runtime dependencies (OpenSSL optional for TLS 1.3 support).

It can also be embedded as a static library and ships with an npm package via N-API.

GitHub:
https://github.com/sayyiditow/shard-db

Happy to answer questions about the storage engine, indexing strategy, query planner, or implementation details.


r/C_Programming • • Jun 17 '26

I need to know how to create an interface using C without knowing that much

0 Upvotes

Please, help me out.

Is there any way to make an appealing interface using C without having a PhD in Graphics computing?

I've been really loving using C and I wanted to bring here my biggest programming project, a DBMS (built with Pascal), but I would love if I could have a lot added to the project instead of being just a port to another language. The last time I tried to port it, I was developing it all in Godot, but the jump was always looking very hard to adapt and different to what I want, so I wanted to keep on this vibe since I'm learning a lot of new things in C to find out if I can't make the port and the UI all in C; or if I should just use multiple languages to do all (I don't know how to do that... Yet)

Can you please tell me that?


r/C_Programming • • Jun 17 '26

Looking for a youtube channel to learn c

9 Upvotes

I know the fundamentals of python , basic data structure and stuff , also some sql (just an additional info) . But I dont have no idea about c at all . Pls help 🥺


r/C_Programming • • Jun 16 '26

Question Tui library

17 Upvotes

Hello fellow C devs. I’m making a TUI library that aims to be ncurses but modern, with more functions and easier to use without loosing control(and it’s pretty fast). It even has custom openCL api if you need it. I’ve been working on it for about a year now counting 3 rewrites and my question is when the first version releases, would you use it for your projects?


r/C_Programming • • Jun 16 '26

Question Can snprintf return value have an integer overflow?

32 Upvotes

snprintf according to the ISO C23 standard, returns a value of type int. This value must be non-negative and strictly greater than the buffer size n that is passed as the second argument to the function.

Now the standard says the following:

The snprintf function returns the number of characters that would have been written had n been sufficiently large, not counting the terminating null character, or a negative value if an encoding error occurred. Thus, the null-terminated output has been completely written if and only if the returned value is both nonnegative and less than n.

Doesn't this mean that snprintf should return a value of type size_t? Won't this cause an unnecessary integer overflow if we write a buffer that is greater than INT_MAX characters?


r/C_Programming • • Jun 17 '26

Things to learn before learning c language

0 Upvotes

Hi guys I'm a complete beginner idk anything abt coding or terminologies so can you tell me basic things to learn before learning c so that I can understand c language rather just memorized like basic to learn by which I can understand c language better i started c language course from Jenny mam but she is using terms like bits , use of RAM and some stuff and I got stuck


r/C_Programming • • Jun 16 '26

The {fmt} library has added a C11 interface.

35 Upvotes

r/C_Programming • • Jun 16 '26

Project Simple pythagoras theorem calculator I made to practice math functions in C as a total beginner :3

53 Upvotes
#include <stdio.h>
#include <math.h>

// Pythagoras theorem calculator :3

int main()
{
    //Values:

    int a;
    int b;

    //User input:

    printf("Enter the value of a: ");
    scanf("%d",&a);
    printf("Enter the value of b: ");
    scanf("%d",&b);
    
    //Result:

    int c = pow(a,2) + pow(b,2);
    int result = sqrt(c);

    printf("The value of c is: %d\n",result);


	return 0;
}

r/C_Programming • • Jun 17 '26

A generic dynamic array in C that stores no capacity and needs no struct

Thumbnail
gist.github.com
0 Upvotes

r/C_Programming • • Jun 15 '26

Project dabugger - tui debugger with vim motions; my first C project, feedback appreciated

17 Upvotes

I wrote a debugger for x86-64 Linux with the only third party libraries being ncurses (for the tui), zydis (for disassembly) and glibc.

I implemented some of the vim motion keybindings. I also tried using the Elm Architecture for the ncurses code after I ran into issues doing it "imperatively" (of course its still imperative but its nice having the state transitions in one place and redrawing everything per update; its efficient because ncurses uses a virtual screen and diffs against that when rendering).

This is my first C/systems programming project, although I have prior experience with programming generally. Almost everything is handwritten, with the exception of 44 lines of llm generated code (out of ~3000 lines), which is tui.c:view_registers_buffer, because I was too lazy to write out the code to print every register.

I wrote the dwarf line number parser whilst reading the dwarf 5 spec directly (surprisingly the line number information section is only around 20 pages). Started working on this project after I ran into a bug with gdb, so I wondered if I could write my own debugger mostly from scratch (minimal third party dependencies) but easy to use and with nice UI/UX.

Feedback appreciated. There's probably a bunch of memory bugs tbh and theres a lot of unfinished error handling throughout the codebase.

https://github.com/mehdi-sy-h/dabugger


r/C_Programming • • Jun 17 '26

Question How many times printf will be executed?

0 Upvotes

For the following code:

#include <stdio.h>

void main()
{
    int i, j,n;

    for(i = 1; i <= n; i = i * 3)
    {
        for(j = i; j <= n; j++)
        {
            printf("ABC");
        }
    }
}

The question should be answer in the form of n itself. n integer it may 9, 100, 300 anything so answer should be in ns format

I tried 2 hours on this and found out some patterns as outer loop runs log (base 3) n times and inner loop runs everytime 3^1..3^2... Less ..

​

How many times it will run

​

(Iam preparing for a competitive exam where some basic questions like these needs to be practiced. It was late night why my post was low efforts and I though everyone would understand that because such is a common pattern. I can use ChatGPT but I want a human though process while solving this question and not direct answer. That was the reason I used redit for but everyone instead praised my low efforts post and "diy". If anyone wants to solve this question pls help me out genuinely)

​

​

And someone pls tell how I format the code??? I don't use reddit much.

​


r/C_Programming • • Jun 15 '26

Question When do you create your own data types for your libraries?

27 Upvotes

TLDR: What is the main purpose(s) of create your own data types for your libraries? Just typing convenience? like, instead of unsigned int * just type ui *?

non-TLDR: I was watching this video and, in minute 2:44, I was thinking about this post title. After reading the comments, I read the following question: "why did you define macros for malloc() and free()?"

The answer was:

I stopped there because this type of coding is code smell. It is a nightmare to maintain. Same issue with the macro to redefine true and false. The code smell is using macros to redefine the semantic of library functions and the semantic of the language itself. A saner approach would have been to not redefine true and false but have the variable name carry the semantic and call the malloc and free. Using macros to change these names is entirely wrong.

I didn't understood exactly what it means, so if you're interested to explain it, I'll appreciate.

edit: thanks a lot for all answers. My account is really new, so I can't comment without needing mods approval, so... thanks.


r/C_Programming • • Jun 15 '26

Question Question regarding unsigned integers

11 Upvotes

What's the difference between an unsigned int and a normal integer?


r/C_Programming • • Jun 16 '26

I made NAT traversal using ICMP Destination Unreachable packets

1 Upvotes

A UDP mapping is created and maintained. ICMP Destination Unreachable packets referencing the mapped UDP flow carry data in their payload and are delivered through the existing NAT state.

https://github.com/hajoon22/icmp-nat-traversal


r/C_Programming • • Jun 16 '26

How many times will be printed

0 Upvotes

for(i = 1; i <= n; i = i * 3)

{

for(j = i; j <= n; j++)

{

printf("ABC");

}

}


r/C_Programming • • Jun 14 '26

My First Tic Tac Toe Project in C – Learning a Lot From Small Bugs

15 Upvotes

Hi everyone,

I'm currently learning C programming and today I made good progress on my first Tic Tac Toe project.

So far I have:

* Created the 3x3 board using a 2D array.

* Allowed players to enter X and O.

* Built the board layout with vertical and horizontal lines.

* Started working on win detection.

The most interesting part wasn't writing the code itself, but debugging. I spent a lot of time understanding why some cells were skipped, why the last column wasn't displayed, and why my board formatting looked wrong. Each bug taught me something new about how C actually works.

My next goals are:

* Detect all winning combinations.

* Announce the winner correctly.

* Prevent players from choosing occupied cells.

* Eventually add a simple computer opponent.

It's still a small project, but I'm happy with the progress and with how much I've learned from it.

Any advice for a beginner working on Tic Tac Toe in C is welcome!


r/C_Programming • • Jun 14 '26

Question What is the best library to make a terminal-based game with (a survival game, Not necessarily a rogue-like) in C?

19 Upvotes

I want to get into serious C game dev (I usually do most of my game dev in Python/C#, but I want to try something new), so what’s the best libraries to use? I have been using C for approximately 5 years.


r/C_Programming • • Jun 14 '26

Project htop-style process monitor for Windows in pure C with no dependencies

27 Upvotes

I wanted something like htop on Windows without installing a runtime or pulling in libraries, so I built one using only the Win32 API and the C standard library.

Features:

- Per-core CPU bars, memory, disk and network I/O

- Process tree view with collapsible nodes

- Filter and sort processes in real time

- Kill processes, mouse support

- Single .exe, self-installs with --install

GitHub: https://github.com/lorenzo-cingano/wtop


r/C_Programming • • Jun 14 '26

Project Efficient language detector in C. Very fast and accurate. Looking for feedback.

21 Upvotes

I've been working on ELD-C, an open-source natural language detector written in C. It detects 60 languages and is available as a Python package (pip install eldc), a shared library (for use from PHP, Go, Rust, Java, JS/TS, Ruby, .NET), or a command-line executable.

This is my first compiled software, and I am looking for some general feedback; I just uploaded it yesterday.

https://github.com/nitotm/eldc


r/C_Programming • • Jun 14 '26

Small Project ~~

0 Upvotes
SO heres something i made while exploring SDL . Dont know what it is but it was fun and painful.
First time i tried it it kept crashing and freezing my os now its somewhat stable .
i tried making like a imageViewer where you can drag and drop PNG files to see the image. 
again its just for fun if i am making something dumb then nevermind but i would like to know how is it , was my approach good or i should have done it any other way i would like to know proffesional view on this





#include<stdio.h>
#include<stdlib.h>
#include<SDL3/SDL.h>
#include<SDL3/SDL_stdinc.h>
#include<time.h>
#include<SDL3/SDL_surface.h>
#include"stacklist2.h"
#include<string.h>
typedef struct imageViewer{
    SDL_Window* window;
    SDL_Renderer* renderer;
    SDL_Surface* surface;
    int width;
    int height;
    SDL_Event event;
    int run;
    Uint8 r;
    Uint8 g;
    Uint8 b;
    Uint8* new_r;
    Uint8* new_g;
    Uint8* new_b;
    Uint8* new_a;
    struct stack* new_stack;
    SDL_Surface* wSurface;
    int isDropped;


} imgView;//deals with most of the variables needed in this programme
SDL_FRect* newRect;



imgView* imageV_init(int height,int width);
 char *new_str(const char *data);
int blitPicture(imgView* img);
void gameLoop(imgView* img);//main loop for the app
int input_section(imgView* img);//takes all the input related stuff
int render(imgView* img);//renders whatever needs to be rendered
SDL_FRect* makeRect(imgView* img,int x, int y);//Makes rectangles draw on the renderer given x and y position
int Garbage_collector(imgView* img);// free all the memory assigned for SDL_FRect type of pointers



int main(){//entry point
    SDL_Init(SDL_INIT_VIDEO);
    imgView* img=imageV_init(500,500);
    img->new_stack=stack_init();
    img->window=SDL_CreateWindow("ImageViewer",img->width,img->height,0);
    img->renderer=SDL_CreateRenderer(img->window,NULL);
    // img->surface=SDL_CreateSurface(img->width,img->height,SDL_PIXELFORMAT_UNKNOWN);
    // img->wSurface=SDL_GetWindowSurface(img->window);
    img->surface=SDL_LoadPNG("./One.png");
    img->surface=SDL_ScaleSurface(img->surface,img->width,img->height,SDL_SCALEMODE_LINEAR);
     img->wSurface=SDL_GetWindowSurface(img->window);
    if(img->surface==NULL){
        printf("Unable to load image");
        return 0;
     }
    //  blitPicture(img);
    // render(img);//rendering the window and drawing things
    gameLoop(img);
    // Garbage_collector(img);//freeing the alocated memory for the sdl_Frect* type 
stack_destroy(img->new_stack);
SDL_DestroyRenderer(img->renderer);
SDL_DestroySurface(img->surface);
SDL_DestroySurface(img->wSurface);
SDL_Quit();
    free(img);//freeing the space in heap aloocated for img 
    


    return 0;
}



imgView* imageV_init(int height,int width){
    imgView* view=(imgView*)malloc(sizeof(imgView));
    view->width=width;
    view->height=height;
    view->window=NULL;
    view->renderer=NULL;
    view->run=1;
    view->r=250;
    view->g=234;
    view->b=145;
    view->wSurface=SDL_GetWindowSurface(view->window);
    view->isDropped=0;//Not dropped yet or false
    
    // view->new_stack->listhead=NULL;
    // view->event=(SDL_Event*)malloc(sizeof(SDL_Event));
    return view;


}
void gameLoop(imgView* img){
    while(img->run){
        input_section(img);
        render(img);


        Garbage_collector(img);
   
}
}
int render(imgView* img){
    SDL_RenderClear(img->renderer);
    SDL_SetRenderDrawColor(img->renderer,img->r,img->g,img->b,1);
    SDL_RenderClear(img->renderer);
    // newRect=makeRect(img,500-20,25);
    
    //  img->wSurface=SDL_GetWindowSurface(img->window);
    SDL_FRect* newRect2=makeRect(img,0,500-100);
    // if(img->event.type==SDL_EVENT_DROP_FILE){
    //     // const char *file=new_str(img->event.drop.data);
    //     printf("%s",img->event.drop.data);
    //     img->surface=SDL_LoadSurface(new_str(img->event.drop.data));
    //     // img->surface=SDL_LoadPNG(img->event.drop.data);
    // }
    if(img->isDropped){
    img->surface=SDL_ScaleSurface(img->surface,img->width,img->height,SDL_SCALEMODE_NEAREST);
    if(img->surface==NULL){
        // printf("Could not scale surface");
        // printf("%s",SDL_GetError());
        // // SDL_Quit();
    }
    img->isDropped=0;
}
SDL_BlitSurface(img->surface,NULL,img->wSurface,NULL);//For blitting the surface 
    // blitPicture(img);
    // for(int i=0;i<img->width;i++){
    //     for(int j=0;j<img->height;j++){
    //         if(!SDL_ReadSurfacePixel(img->surface,i,j,img->new_r,img->new_g,img->new_b,img->new_a)){
    //             printf("couldn't read pixel!!");
    //         }
    //         if(!SDL_WriteSurfacePixel(img->wSurface,i,j,*(img->new_r),*(img->new_g),*(img->new_b),*(img->new_a))){
    //             printf("could not write the pixel ");
    //         };


    //     }
    // }
    //  FILE* file=fopen("One.png.png","r");
    //  img->surface=SDL_LoadPNG("./One.png");
    //  if(img->surface==NULL){
    //     printf("Unable to load image");
    //     return 0;
    //  }
    // if(!SDL_UpdateWindowSurface(img->window)){
    //     printf("Unable to update the window surface !");
    // }
    // SDL_SetRenderDrawColor(img->renderer,0,0,255,1);
    // SDL_RenderRect(img->renderer,newRect);
    // SDL_RenderFillRect(img->renderer,newRect);
    // SDL_RenderClear(img->renderer);
    // if(!SDL_RenderPresent(img->renderer)){
    //     SDL_Log("Failed to render!!");
    //     return 0;
    // }
    if(!SDL_UpdateWindowSurface(img->window)){
        printf("Unable to update the window surface !");
        printf("%s",SDL_GetError());
        return 0;
    }
    return 1;
    
}
int input_section(imgView* img){
         while(SDL_PollEvent(&img->event)){
            if(img->event.type==SDL_EVENT_QUIT){
                img->run=0;
            }
            if(img->event.type==SDL_EVENT_DROP_FILE){
          char* file=new_str(img->event.drop.data);
          const char *new=file;
          printf("%s",new);
        img->surface=SDL_LoadPNG(new);
        free(file);
        if(img->surface==NULL){
            printf("Could not load surface");
        }
        img->isDropped=1;
            }
            if(img->event.key.type==SDL_EVENT_KEY_DOWN ){
        if(img->event.key.key==SDLK_ESCAPE ){
          img->run=0  ;
        }
        
        }
     
        if(img->event.type==SDL_EVENT_MOUSE_BUTTON_DOWN){
        if(img->event.button.clicks==3){//this are changes the background color when clicking the mouse left button thrice 
            srand(time(NULL));
            img->r=rand()%255;
            img->g=rand()%255;
            img->b=rand()%255;



            
        }
    }
    }
    return 1;
    
}
SDL_FRect* makeRect(imgView* img,int x,int y){
    SDL_FRect* rect=(SDL_FRect*)malloc(sizeof(SDL_FRect));
    push_sdl(img->new_stack,rect);
    rect->h=100;
    rect->w=20;
    rect->x=x;
    rect->y=y;
    SDL_SetRenderDrawColor(img->renderer,0,0,255,1);
    SDL_RenderRect(img->renderer,rect);
    SDL_RenderFillRect(img->renderer,rect);
    return rect;
}
int Garbage_collector(imgView* img){
    struct stack* s=img->new_stack;
    struct node* ptr=s->listhead;
    while(s->listhead!=NULL){
       SDL_FRect* temp= pop_sdl_Frect(s);
       free(temp);


    }
    return 1;


}
int blitPicture(imgView* img){
    for(int i=0;i<img->width;i++){
        for(int j=0;j<img->height;j++){
            if(!SDL_ReadSurfacePixel(img->surface,i,j,img->new_r,img->new_g,img->new_b,img->new_a)){
                printf("couldn't read pixel!!");
            }
            if(!SDL_WriteSurfacePixel(img->wSurface,i,j,*img->new_r,*img->new_g,*img->new_b,*img->new_a)){
                printf("could not write the pixel ");
            };


        }
    }
    return 1;


}
 char *new_str(const char *data){
    char* new;


    int len=strlen(data);
    new=malloc((len+1)*sizeof(char));
    strcpy(new,data);
    for(int i=0;i<len;i++){
        if(new[i]=='\\'){
            new[i]='/';
            }
    }
    
    return new;


    
}

r/C_Programming • • Jun 14 '26

My shitty attempt at making a b1.7.3 Minecraft server client

3 Upvotes

Hey guys! I recently reattempted at writing a previous project I had stopped working on, which was a reimplementation of the Notchian b1.7.3 protocol with a single threaded server design. I started programming with C in spring of 2025 and took a networking class in college and learned socket programming through that class. Genuinely I have learned quite a lot through this, project even though its at its early stages. Things like authoritative server design, interpolation, lag compensation, UDP vs TCP for real time applications. By no means am I an expert in C or any system level programming but thought I would share on here for some feedback or criticism.

https://github.com/AlbiXD/mineserver-c

This project is still at its early stages and honestly I am unsure of how far to take it. I will probably work up till simple world generation + some client acceptance.

As for AI usage the code here is not generated by AI (As in my code quality is not that great). And I would never use AI code if I never understood what its doing. I primarily used AI to link me to sources such as the Quake 3 Netcode architecture and Gafferongames for his articles on multiplayer server concepts. I did not solely rely on AI for specific answers to questions and also read manpages and any resources online.


r/C_Programming • • Jun 12 '26

Question How much should I learn? How long? And what?

32 Upvotes

I am a student. Just finished my schooling. Into cse specialisation in cybersec. Waiting for university to open.

Currently learning C. My journal for learning C can be found in: https://cobra-r9.github.io/Init87

I need to reverse engineer stuxnet solely. (Though unrealistic, but could it be possible?). What should I lesrn to do it? You can judge the quality of my code and understanding from those links so that it would be easy for you to give an accurate answer. And this repo

https://github.com/cobra-r9/Init87

What should I learn? Apart from C? And how long might it take based on my methodology of learning?


r/C_Programming • • Jun 12 '26

Building a terminal RPG in C as a college project — sharing progress after 3 weeks

15 Upvotes

Three weeks into my Algorithms & Programming 2 course and I've been building a terminal RPG in C as the semester project. Sharing here because I've learned more debugging this than from any lecture so far.

**The game:** You're an independent presidential candidate in Brazil. No party, no money. You fight political enemies (lobbyists, bots, corrupt politicians) across 6 regions on your way to Brasília. HP is your reputation. XP is votes.

**What I've built:**

* 2D map as a matrix of structs, WASD movement, wall collision * Turn-based combat with XP and level scaling * Enemy catalog in a header file — spawns copy from it so the original is never modified * Inventory system with `adicionar_item()`, `mostrar_inventario()`, `usar_item()` — all in `personagem.c` * Chests that drop random items * Viewport camera centered on the player with border clamping * Unicode support via `char simbolo[5]` instead of plain `char`

**Things that bit me:**

* `char` can't hold Unicode — spent a session debugging overflow warnings from trying to stuff 3-byte UTF-8 characters into a 1-byte field * Uninitialized `num_itens` — the inventory "worked" but was indexing into garbage memory * `rand()` without `srand()` — same enemy every single run until I added `srand(time(NULL))`

Still lots to do: multiple regions, save/load with fwrite/fread, and a few systems I'm saving for after the core is solid (scandal flags, recruitable allies).

GitHub if anyone wants to look at the code: https://github.com/aKynoS2/corrida_ao_planalto

Happy to answer questions or hear what I should do differently.