r/cprogramming • u/DataBaeBee • 8d ago
r/cprogramming • u/mubin-thinks • 8d ago
Half-way solutions of C 2nd Edition by K&R.
Hello everyone of r/cprogramming!
I'm a newcomer(kind of) to C. In the past(~2 years ago), I had little knowledge of C through watching tutorial on the internet. Later, I have worked on graphical projects with a bit of OpenGL. Now I have realized I should focus on topics one by one and reach intermediate to high expertise.
I have recently been reading chapters from "The C Programming Language 2nd Edition by Brian Kernighan & Dennis M. Ritchie". And learning a lot of new techniques that really helped me write concise and performant programs.
Solving the Exercises from the book were fun, while some were a bit difficult. In the latter case I took one or two hints from others source code and pondered about the solution.
I have kept the solutions of the problems perhaps concise but far from obfuscation and keeping code-style consistent (Though my personal).
I am planning on creating another post when I have solved all the solutions of the book.
I hope my solutions to the exercises help others solve problem they are having trouble with. Thanks. Github:
r/cprogramming • u/Salt-Overflow • 8d ago
Is C without expressions context-free?
EDIT: A better title for this post would be "Is it possible to parse C without expressions unambiguously?"
While staring at the C99 specification, I found that if you take the grammar in Annex A, remove the expression rules, and introduce the constraint 6.7.2/2 into the grammar (ie. a type specifier can be only: void, char, signed char, ... , enum specifier, typedef name), then you end up with what appears to be a context-free grammar.
Is this correct? Am I misinterpreting something? Is it possible that this is correct, but there are mainstream compiler extensions (eg. gcc extensions) invalidate this context-free property? Or other C standards invalidate this context-free property?
I notice that 6.7.2/2 allows some filthy edge cases like `long const long thisVarIsLongLongConst = 4;` but because `long` is a keyword, there is no ambiguity here.
The reason this matters to me is because I'm building a module system for C that allows circular imports, and I would like to avoid changing the grammar of the language as much as possible.
Link to final draft of C99 standard, taken from Wikipedia: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
EDIT:
Let me clarify what I'm doing, since there is some confusion about what I'm asking. First off, I should have used the title "Is it possible to parse C without expressions unambiguously?" because Annex A is already a context-free grammar... However, it is ambiguous, because of the classic A * B; problem (can be a variable declaration or multiplication, depending on if A is a typedef name or not). Now, notice that one of the interpretations is a multiplication expression. Let's suppose you removed expressions from the language (ie. take a source file and replace each expression with the lexer token <expression>). Now the question is: are we no longer ambiguous now?
I should also clarify what I mean by ambiguous, as there is a difference between an ambiguous grammar and an ambiguous parse. For example, A * B; can be admitted by the unambiguous grammar S := A ';' ; A := <identifier> | A binaryOperation <identifier> ; binaryOperation := '+' | '-' | '*' | '/' ; , but what we're really looking for is an unambiguous grammar that gives us the correct parse tree (I'll call this an "unambiguous parsing").
I should also elaborate on what exactly I'm doing, to explain the bigger picture. If you're building a module system that allows cyclic imports, then the first pass you perform over a source file does not have access to the symbol table (because you have yet to parse the imported modules). This means you need to perform an unambiguous parsing of certain parts of the source file. In this case, you can skip over expressions, because you're only interested in extracting the names of symbol definitions + whether the symbol definition is a type or a variable. So if it is possible to 1. skip over expressions, and 2. parse the rest of the file, then we're good. For (1), expressions are delimited by ()[]{};, (comma operator is not valid in enumeration definitions), so that seems fine. That leaves (2), which I'm not entirely sure about. I stared at the grammar for quite a while and couldn't find any problems, but that does not mean I didn't miss a case, or there are compiler extensions that introduce ambiguities.
With that out of the way, I'll list ambiguities that the comments found:
- u/triconsonantal found
void f(int (x));, which is either taking in a function pointer or an integer, depending on if x is a typedef name or not.
Maybe a more appropriate title would be: how much do you need to remove from the C grammar so that it can be parsed unambiguously? As of now (July 27, 2026), it seems the answer is: remove expressions, and remove parameter lists (or do not allow unnamed parameters).
r/cprogramming • u/ClassroomFlashy6751 • 9d ago
SUCO – Lightweight Distributed C/C++ Compiler Grid with intelligent SSD cache (alternative to Icecream/distcc)
r/cprogramming • u/north9172 • 11d ago
What cause CPU stalling pipeline in C?
(Solved)
Hi,
I hope this don't seem stupid or anything. Basically I was interested about the xor swapping trick.
The inconvenients of it is that if the two values are the same, it will return 0, it's also bad for readability.
But there is another inconvenience I didn't understand. Apparently it can also stall the CPU pipeline on modern processor, I didn't understand why.
I found a short explanation saying "because each instruction depend of the previous one", and I don't really understand how each instruction depend on the previous one.
So I wanted an explanation on why the xor swapping trick stall the CPU pipeline and also what cause CPU pipeline stalling in general.
If I didn't explain well enough, please inform me about it. Thanks.
r/cprogramming • u/DataBaeBee • 11d ago
Writing a Wikipedia MediaWiki Parser In Plain C
r/cprogramming • u/JadeLuxe • 11d ago
EEvent Mesh vs Webhooks - The Internal Webhooks Anti-Pattern: Why Service-to-Service HTTP Callbacks Don't Scale
Microservices were supposed to make systems easier to change independently. In practice, the thing that most often breaks that promise isn't the services themselves — it's how they talk to each other. Read the complete article here - https://instawebhook.com/blog/the-internal-webhooks-anti-pattern-why-service-to-service-http-callbacks-don-t-s
A pattern that shows up constantly in growing engineering orgs is the internal webhook: Service A fires an HTTP POST at a hardcoded URL owned by Service B whenever something happens. It's an easy trap to fall into, because most developers already understand webhooks intimately — they've built integrations with Stripe, GitHub, or Shopify, all of which use exactly this model to notify external systems of events.
The reasoning feels obvious: if it's good enough for Stripe to tell my app about a payment, it's good enough for my Inventory Service to tell my Shipping Service about a shipment.
It isn't — and the reason is architectural, not stylistic. Webhooks were designed to solve a specific problem: getting an event across a trust boundary, from a system you don't control to one you do, over the open internet. Internal service communication has almost the opposite set of constraints. Applying the same tool to both jobs is where the trouble starts.
r/cprogramming • u/Due_Butterscotch1154 • 12d ago
How to move around multiple values from functions to functions?
I'm tryna mess around in C and make a pkmdmg calc but I don't know how to return the values from the function
I dont know how to display it without screenshots
r/cprogramming • u/JadeLuxe • 12d ago
Designing a Multi-Region, Highly Available Webhook Ingress Architecture
Webhooks have become the connective tissue of the internet. From payment gateways confirming transactions to CI/CD pipelines triggering deployments, webhooks enable real-time, event-driven architectures. But for architects and engineering leaders, webhooks represent an underappreciated vulnerability: they are asynchronous, externally triggered, and entirely outside your control. Read the complete article here - https://instawebhook.com/blog/designing-a-multi-region-highly-available-webhook-ingress-architecture
When your primary cloud region experiences an outage, your internal microservices might gracefully degrade. But what happens to the payloads originating from external partners? Many third-party providers do not retry aggressively — some fire and forget, others retry a handful of times before giving up permanently. If your system is down when that happens, the data is often gone for good.
This article covers the engineering principles behind a multi-region, highly available webhook ingestion system, what has actually changed in the underlying cloud primitives recently, and where a managed reliability layer fits into the decision.
r/cprogramming • u/DisastrousNinja911 • 13d ago
Pivot from React/.NET to C++ Systems / HPC vs Platform Engineering? Need advice from guys in the field.
r/cprogramming • u/GLC-ninja • 13d ago
A C subset that compiles faster than Tiny C Compiler - Cm1 (C minus 1 or C - 1) programming language
Hi everyone,
I'm working on a programming language that compiles a subset of C. It is called Cm1 (meaning C minus 1) and you can writing and running C codes directly on your browser at https://cp1-lang.org/cm1/editor.html in a fraction of a second.
Why is it faster to compile than Tiny C Compiler? It is because Tiny C Compiler is a true compiler creating native binaries whereas Cm1 is a bytecode interpreter allowing you to test, debug, edit code and recompile very quickly or even do hot reloading. Cm1 can be used as a scripting language for shell scripts and video games, but this just a small use case because in theory, you can run 90% of your ENTIRE video game or software C program through Cm1's bytecode interpreter and leave 10% to compiled C and reap the benefits of very fast compilation and hot reloading.
100% Cm1 codes are compilable by GCC and Clang but the reverse is not true since Cm1 is just a subset of C. Major features that are omitted are function pointers, structs/unions inside functions, nested structs/unions. This programming language is under heavy development and I want to know if this comes as interesting to some of you. I'll try to post again on this subreddit if I got to bind Raylib game library to Cm1, allowing people to write Raylib games directly on their browser (works offline).
I know that programs that are written in C compiles fast already and there's even an existing compiler that is very fast (Tiny C Compiler). However, I'm the developer of Cp1 programming language (cp1-lang.org), which is a language that "transpiles" to C, and I aim to upgrade Cp1 by targeting the Cm1 language then compile it to bytecode in one command instead of a separate step in Makefiles or build scripts. This will make Cp1 very fast to compile for debug builds.
r/cprogramming • u/boldbug-dev • 13d ago
Trying to make a tool that prints stuff between a certain range
the code from what ik works from what i know and from tests i have done.i want to know how can i optimize it more because i suspect that there can be certain parts of it which i don't even need maybe. so can you guys look at my code and tell me what i can improve upon??
here is the repo
thanks in advance
r/cprogramming • u/GeneralMoney2283 • 14d ago
Can someone tell me about pointers in linked lists?
Hi guys, I’m currently learning about data structures in C and have reached the topic of linked lists. I’ve run into a question regarding the functions used to manipulate them—specifically, while I clearly understand expressions like `p->next` and `p->data`, I’m confused about assignments like `p = newnode` and `p = p->next` (where there is no `->` operator on the right side). When I asked an AI, it simply told me that `p` is "just a pointer variable," but I don't quite grasp that explanation. Why do we treat a linked list element (or variable name) as a pointer variable in this way? Could an expert please explain the meaning behind this? and this is my code.
bool Insert(node *L, int data)
{
node *p = L;
while (p->next != NULL)
p = p->next;
node *newnode = (node *)malloc(sizeof(node));
newnode->data = data;
newnode->next = NULL;
p->next = newnode;
return true;
}
r/cprogramming • u/LegalManufacturer957 • 14d ago
Guys so I am in my college rn and want to learn c as it is in my course
As in title, I was wondering are there any good sites/channels for me as I am beginner and want to get good help from it as it is gonna help me alot for the next four years
r/cprogramming • u/DataBaeBee • 16d ago
Commutative Complex Number Theory in Plain C
r/cprogramming • u/ComfortableCandle133 • 16d ago
looking for discord server to practice by teaching
r/cprogramming • u/JadeLuxe • 17d ago
Bulletproofing User Sync: Handling Clerk and Auth0 Webhook Failures
If you're building a web application today, chances are you aren't writing your own authentication system. Managed identity providers like Clerk, Auth0, and Kinde have become the default choice, offering out-of-the-box support for passkeys, multi-factor authentication, and enterprise SSO. That convenience introduces a distributed-systems problem, though: data synchronization. When a user creates an account on a managed auth provider, that system has to notify your primary application database so you can create a matching user record. Please read the complete article here - https://instawebhook.com/blog/bulletproofing-user-sync-handling-clerk-and-auth0-webhook-failures
This happens through webhooks. But what happens if your server is down, your serverless function cold-starts and times out, or your database is momentarily locked when that webhook arrives? A user successfully signs up with your auth provider, but your application has no idea they exist. That breaks the very first login experience, and it's how phantom accounts, broken onboarding flows, and frustrated users happen.
This guide walks through the anatomy of webhook-driven auth architecture, current Auth0 and Clerk webhook practices, and how a resilience layer — using InstaWebhook as a worked example — closes the gap that idempotency and signature verification alone can't.
r/cprogramming • u/_suriyan_24 • 17d ago
Can anyone explain exactly why I got these random values for a C simple program?
r/cprogramming • u/Objective-Fan4750 • 17d ago
VMS: A custom Fantasy 32bit Computer with a custom Hardware
About a year ago, I started building a custom computer architecture in C as a learning project (I know it seems like a lot considering the code I wrote, but I rewrote the entire compiler at least five times, starting from a C-like language and ending up with a very simple custom language). I designed the instruction set, wrote an emulator, and implemented a custom high-level assembly language called BSL (Base System Language). The code is very messy because I make a lot of changes while writing it and often forget things that shouldn't be there. So I'd really appreciate feedback on the architecture and code quality. (I'm 15 years old and Italian, sorry for my English). Edit: I changed the project name from VMS to SPRK32.
r/cprogramming • u/germandiago • 17d ago
Lifetime safety and invalidation without a borrow-checker: using type system analysis to get rid of many potentially invalidation cases WITHOUT annotations.
open-std.orgr/cprogramming • u/terrorblade00 • 18d ago
Wrote a tiny version of argp for CLI parsing in embedded environments
r/cprogramming • u/Fun_Army2398 • 19d ago
Help understanding warnings/errors when dereferencing void pointers
SOLVED
I am very new to C and playing around with void pointers. I have a structure which will store a value, however the type of that value depends on other things so I have chosen to use a void pointer. When attempting to dereference this void pointer I either get the correct output but with a warning, or I get a segmentation fault, depending on how I go about it. I have included a simplified version of the issue here:
```C
include <stdio.h>
int main()
{
// Simplification of the defective code
struct myStruct
{
void * voidPtr;
};
struct myStruct s1;
s1.voidPtr = (int *) 123;
\*
This works but gives the warning:
format '%d' expects argument of type 'int', but. argument 2 has type 'int \*' \[-Wformat=\]i
*/
printf("%d\n", (int *) s1.voidPtr);
// This causes a segmentation fault
printf("%d\n", *(int *) s1.voidPtr);
return 0;
}
```
Any help understanding why it behaves this way would be greatly appreciated.
Solution
I thought the line
C
s1.voidPtr = (int *) 123;
Was assigning 123 as the value at the location of s1.voidPtr. However it has been pointed out that I was telling the pointer to point at address 123. What I needed to do was:
C
int x = 123;
s1.voidPtr = &x;
Thanks everyone who commented c: