r/C_Programming • • 2h ago

ISO C Chaining: Look ma, no this!

Thumbnail goo.gle
8 Upvotes

Now we have a way of doing fluent interfaces in C89 without macros or preprocessing tricks, allowing code like this:

ChHash *user = ch_new_Hash(3)
    ->put("name", ch_new_String("Ada"))
    ->put("role", ch_new_Hash(2)
        ->put("title", ch_new_String("Engineer"))
        ->put("level", ch_new_Number(5)))
    ->put("active", ch_new_Boolean(1));

puts(user->generic.to_chars());
/* {"name":"Ada","role":{"title":"Engineer","level":5},"active":true} */
ch_release(user);

It even automatically cleans up anonymous/temporary elements. The repo includes an example Sqlite interface and http API server.


r/C_Programming • • 1h ago

Question Library API Naming Conventions

3 Upvotes

Hi Everyone,

I'm working on a small 2D game library/runner. At the moment it's for personal use but I may release it some day, which is why I'm creating this post!

At the moment, the function names in the library are named like this (the name of the library is Imp):

imp_texture_create(...)
imp_texture_destroy(...)
imp_camera_set_position(...)
imp_camera_set_rotation(...)

I quite like this naming convention, mainly because if I wanted to see what functions were available for a given resource or operation, I could type imp_camera_ and then my editor would show a list of things that I can do with, or to, a camera. I also used GameMaker a lot when I was younger, and this is the way their API names functions!

Recently though, I've been using a lot of Raylib and quite like the way it names its functions - they are, to me at least, a lot more readable when looking through code. This has me thinking whether or not a better naming convention would be:

imp_create_texture(...)
imp_destroy_texture(...)
imp_set_camera_position(...)
imp_set_camera_rotation(...)

The downside being that you sort of have to guess what operations are available for a given resource as you can no longer type imp_texture_ to have the editor show a list.

I understand that this is a really small issue and probably not really worth fussing about, but I was curious what preferences more experienced people here have and - more importantly - why you have them!


r/C_Programming • • 2h ago

Question Signal handlers, how to have them act differently (or the process) based on when the process receives the signal

1 Upvotes

Hello, everyone. I'm supposed to be doing a project for school (hence why I can't share the code), but I'll try to describe my problem to the best of my abilities.

My main parent process forks several children, who will execute 2 other programs, depending on the role assigned to them. Once the parent process receives a SIGALRM, it sends a SIGTERM to all its kids, and they, already having changed their signal handlers for SIGTERM, have to do whatever is specified in their signal handler.

Let's consider child A executes program A, and child B executes program B. These 2 processes have to communicate with each other through a series of msgsnd/msgrcv (we can only use System V IPCs in this project, so the msgops don't restart once interruped by a signal). A single process of type B will communicate with several processes of type A (but not with all at the same time: once its communication is finished with one, it goes on to the other). The last msgop A does, is a msgsnd and B does a msgrcv. I'd like to also add that, between the msgrcv and msgsnd on A's part, A is also supposed to go to sleep. A's almost entire program is in a while (it never terminates unless it receives the signal from its parent), whereas B terminates successfully once it's finished communicating with all the necessary A processes.

Whether B receives the SIGTERM signal or not, it has to modify sth in a shared memory section, but what it changes, depends on whether it has finished communicating with all the processes or not.

Ideally, I would like for B to terminate successfully, even if it receives SIGTERM, after having done all it had to do, and I thought of blocking SIGTERM with sigprocmask. That said, couldn't it still receive the signal between the mast msgrcv operation and sigprocmask? Is there any way whatsoever to make a set of instructions atomic?

I considered only having a sig_atomic_t volatile variable in the signal handler, which would increment if the the sign handler is executed, but then, not only would I have to continuously check in case it changed its value, but (and this could be me being wrong, please correct me if that's the case), B could still receive SIGTERM right after the condition of the variable being 0 is checked, and before the following instruction is executed (like a nanosleep - which I'd like to avoid if SIGTERM has been received - OR before a msgrcv, leaving B to wait for a message that will never come, because A could have received the same signal and will not be sending a message).

I thought of having the signal handler check a variable and act differently based on its value, but, again, maybe B receives the signal right before said variable's value changes. I also considered having an if where the conditions would be the variable==0 && msgrv(...)!=-1, but the variable can change after its value has been verified.

Disclaimer: we haven't studied sig_atomic_t volatile variables, so my knowledge might be lacking, although I did try to read up on them and research them. Also, the parent won't be deleting the IPCs until all kids have terminated.

Am I supposed to just hope for the best here and just proceed with sigprocmask? Is there another way I'm not seeing? Am I misunderstanding something? Any help offered is invaluable, so thank you in advance. I'm happy to answer any clarifying questions if I was unclear at all in the post.


r/C_Programming • • 23m ago

阅读《用C语言编写一个Shell》(lsh)让我深入思考:老一辈UNIX开发者是如何从零开始设计Shell的?

0 Upvotes

嘿,大家好,

我目前正在阅读斯蒂芬·布伦南的经典教程《用C语言编写一个Shell》(`lsh`),进展到一半。到目前为止,这本书读起来很有趣,但感觉有点空洞——它展示了如何将 `fork()`、 `exec()`和 `wait()` 结合在一起,却让我渴望看到真正的官方规格和更深入的设计文档。

这让我想到两件事:

首先,像肯·汤普逊或斯蒂芬·伯恩这样的老前辈是如何在一张白纸上构建早期UNIX shell的?我想要阅读真正的基础论文、POSIX文档或历史设计分析,而不仅仅是玩具教程。

其次——将经典的 `Read -> Parse -> Execute` 循环与现代AI代理相结合——这种方式与代理如何解析上下文和分发工具非常相似。

寻找关于历史规格、论文或最小化shell源代码库的推荐。请在下面留言!


r/C_Programming • • 6h ago

Question 2. Write a program to print integer number and input from user

0 Upvotes

#include<stdio.h>

void main()

{ int a; // a is variable and this data type is integer
printf("Enter your value : "); // printf is use for print value
scanf("%d",&a); //scanf  is use input from user
printf("Your Value is : %d", a);

}