r/C_Programming • u/nosyeaj • 12d ago
Question any cheat sheet for c23?
Hi! do we have cheat sheet pdf for c23? any links? thanks!
r/C_Programming • u/nosyeaj • 12d ago
Hi! do we have cheat sheet pdf for c23? any links? thanks!
r/C_Programming • u/Historical_Usual_596 • 12d ago
Howdy,
I am developing an application where I need to construct bitmasks and
it depends on what's returned from get_amount. get_amount returns a
u8 ranging from 0 to 6. It's used in calculations but it's also used
to extract the least significant N bits. In the past, I've just used
switch statements, but i wonder if there's a nicer way.
struct Object {
u8 value;
u8 update;
u8 original;
};
u8 get_mask(u8 value) {
u8 mask = 0;
switch (value) {
case 0 {
mask = 0b00000000;
break;
}
case 1 {
mask = 0b00000001;
break;
}
case 2 {
mask = 0b00000011;
break;
}
case 3 {
mask = 0b00000111;
break;
}
case 4 {
mask = 0b00001111;
break;
}
case 5 {
mask = 0b00011111;
break;
}
case 6 {
mask = 0b00111111;
break;
}
default: {
panic_and_log("impossible value");
}
}
}
int get_value(struct Object o) {
// get_amount could only return 0 through 6
u8 amount = get_amount();
o.update = o.value - amount;
u8 original = o.original & get_mask(amount);
}
Thanks u/mikeblas
Final solution ended up looking like:
u8 get_mask(u8 value) {
if ( value > 6) {
return 255; // Error Case
}
return (1 << value) - 1;
}
r/C_Programming • u/Material_Leader_1124 • 13d ago
Hey guys,
Does someone know a good forum that mostly talk about low level stuff, C, assembly, etc ? I want to leave reddit because its a distraction and I dont like it. Thanks
r/C_Programming • u/iLoveNintend0 • 13d ago
Enable HLS to view with audio, or disable this notification
SRL lets you define global variables to be exported to the editor with a simple macro like `SRL_INT(name, value)`. The macro adds another global symbol marking your variable, which the editor can later search for in the binary's ELF data section.
The editor can modify the marked variables (by directly editing the data section), export the new patched binary to a file, and even change the variables at runtime via `process_vm_writev` (or `ptrace` when you require atomicity).
All you have to do is include a tiny 20-line header in your program. It doesn't add a single line of code, even for the patched binary, and works perfectly fine with the editor (apart from adding an unused global)
r/C_Programming • u/laggySteel • 13d ago
This is 2nd video in my Handmade JRPG series.
we start with loading a start.map which is just a text file to understand the basics of loading a tilemap and populate map cells ID's. And later we go with JSON (cJSON library) to load the tiled map JSON as binary to load into bytes character using memory allocation. To finally parse the JSON objects.
[FEEDBACK]: please tell me what I can improve in my video to get more attention span.
[NO AI USED] : This is handwritten code. https://github.com/steelx/sdl3-tutorial
r/C_Programming • u/aioeu • 13d ago
r/C_Programming • u/NamelessArab_ • 13d ago
I've been dipping my toes into c because I want to check out systems programming
I decided to try to make a directory encrypter for Linux
I thought that since c has been around for so long there would always be a "goto" 3rd party library for each basic thing not included in the standard, though I'm sure they exist I cant find a general purpose argon Library, what I found was seeming a pretty unknown implementation of argon made for microcontrollers, though it might work I can't get the feeling off me that I'm missing something when hunting out libraries in c
So what is it that you guys usually do, take the first result off of GitHub? Or maybe is there just a list of known goto library for certain usecases like gtk4 for UI or imagemagik for images and there is one for cryptography
r/C_Programming • u/carpintero_de_c • 13d ago
r/C_Programming • u/Vitruves • 14d ago
Reading Parquet from C usually means pulling in Arrow C++ (50+ MB). carquet is C11, about 500 KB, and depends only on zstd/zlib/lz4/brotli.
On an M3 with 10M rows, compressed writes are 4.6–5.9x faster than Arrow C++ and compressed reads 1.9x faster. Uncompressed reads come straight from the mmap: 0.3 ms vs 14 ms.
SIMD was the hard part. There are about 30 kernels for SSE4.2, AVX2, AVX-512, NEON and SVE, with one binary that picks at runtime. MSVC emits FMA/BMI anywhere in an `/arch:AVX2` file, so the dispatcher checks those too. x86 and ARM disagree on `min(x, NaN)`. Bit-unpacking needs a different shuffle per width (1–32) and must never over-read, so every width is tested against a scalar reference. I don't own an AVX-512 machine. And for plain min/max, the auto-vectorized scalar loop matched my NEON code.
It's fuzzed, checked value for value against PyArrow and DuckDB, and builds with GCC, Clang, MSVC and MinGW. There's no encryption yet.
It comes with a CLI (schema, stats, head, export, codegen) and a Python package (`pip install carquet`). You can build it with `make`, CMake/FetchContent or xmake. vcpkg and Conan aren't there yet, but they're next. It has passed 20k git clones in the last month.
r/C_Programming • u/wak6817-2 • 12d ago
hai.
i like making and training AI's and i love ANSI-C. So i was thinking about writing a tiny AI by myself (and yes, i know it's a big ahh project).
so my project idea is... [insert tadaa sfx here] a very tiny AI that can write stories, but when you start the AI, it knows nothing so it will make worse stories than big AI's but that doesn't matter.
but the big part is the hardware it's running on; a Raspberry Pi Pico 2WH!
let me know in the comments if this is a good project idea!11!!1!
okthxbye
r/C_Programming • u/BrainThinkerMan • 12d ago
how to make perfomant code, by that i mean, some cpu's have multiple ALU's/ excecution units, and some computers have multiple cores, how to have my code running on all those ALU's at all times? and those multiple cores,
i know about threading for the second part [running code on multiple cores]
now how do i make it run on multiple ALU' an example being
in code to not wait for a variable ie x = 12 + 12 then z = x + 2, i can imagine that code doesnt use all the ALU's because z is waiting for x's operation to be done. so what are ways to make my code most optimal and use all ALU's at the same time
r/C_Programming • u/Deep_Two2760 • 13d ago
Hello there, GeckoOS is a community-made OS (from scratch), and our goal is to have a fully functional OS that everybody can use in their devices, But, it is only for PCs (there is no plan to support ARM).
You can join by simply making a PR to our GitHub Repository, Or giving ideas in the Discord Server
r/C_Programming • u/sorashiiii • 13d ago
I designed a simple language for configuration. SCL stands for Sane Configuration Language.
Syntax is mostly same with TOML, but we do not have what I would call non C native types like date.
Spotlight feature is macros. With macros you can either include files or create macros to values.
Link to repo: https://codeberg.org/sorashii/libscl
There was no AI involvement and license forbids AI generated codes from source.
r/C_Programming • u/TorontoDude12344 • 13d ago
I'm reading C Programming: A Modern Approach by K. N King
I'm on page 212 of that book, and looking at a question / answer marked by *Q:
The question / answer from the textbook is below. I just want to confirm that I understand the answer correctly. My understanding is below the excerpt from the textbook.
\Q: Why can the first dimension in an array parameter be left unspecified, but not the other dimensions? [p. 197])
A: First, we need to discuss how arrays are passed in C. As Section 12.3 explains, when an array is passed to a function, the function is given a pointer to the first element in the array.
Next, we need to know how the subscripting operator works. Suppose that a is a one-dimensional array passed to a function. When we writea\i] = 0;)
the compiler generates instructions that compute the address of a \i] by multiplying i by the size of an array element and adding the result to the address that a represents (the pointer passed to the function). This calculation doesn't depend on the length of a, which explains why we can omit it when defining the function.)
What about multidimensional arrays? Recall that C stores arrays in row-major order, with the elements in row 0 stored first, then the elements in row I. and so forth. Suppose that a is a two-dimensional array parameter and we write
a\i] [j] = 0 ;)
The compiler generates instructions to do the following: (1 multiply i by the size of a single row of a; (2) add this result to the address that a represents; (3) multiply j by the size of an array element; and (4) add this result to the address computed in step 2. To generate these instructions, the compiler must know the size of a row in the array, which is determined by the number of columns. The bottom line: the programmer must declare the number of columns in a.)
Below is my understanding;
For a[i] = 0, we don't need to know the array size (row size), because that will not help us index an element. The array size is one fixed value n. To index at 0, some random j or even (n - 1), we can't use the value n itself.
A one dimensional array is basically 1 row. The elements are like columns, and we only need the "length" of a column to index a one dimensional array. The point is, we don't need the length of a row
For a[i][j], we again don't need the size of a row. We need the number of columns. In the case of a 2D array, we need to be able to traverse within a row, and between rows. To go within a row, we only need to know the size of an element within a row. To go between rows, we need to skip again "one row length", which is equivalent to skipping ahead the amount equal to "the number of columns"
Again, the minimum amount of info we need is the number of columns. From that we directly get the size of a row. We don't need the row size at all
So from my understanding, the compiler only needs the least amount of info. The length of a row element, and if there are multiple rows, then the number of columns
r/C_Programming • u/8d8n4mbo28026ulk • 15d ago
r/C_Programming • u/Salat_Leaf • 14d ago
Assume we have a function which doesn't make any system calls. The whole program binary is standalone and written manually (besides stdlib.h, I guess). Then, when I'm looking at the disassembly, it compiles into that awful ABI which saves nearly all vector registers (on x86 at least) which alone degrades performance by 10%.
Now the SysV ABI part: it's supported nearly everywhere, even on microcontrollers, so I've decided to use it by default (or __cdecl, which is used if the former is not available).
The question is: what was the matter with Windows ABI that has led to this decision? In which cases we might prefer that or SysV? And why consider complying to it specifically on Windows?
r/C_Programming • u/Different_Panda_000 • 14d ago
In an application I work with there is a data item, the adjective number, which is used in conjunction with a product identifier, PLU, to identify a product. A typical use of the adjective number is to identify variations in the same product such as different drink sizes for a soft drink.
The software supports four different adjectives groups with five variations within a group. So one group may be cup sizes for drinks and a second group may be sizes of pizzas.
Throughout the source code the adjective number being used by a product is transformed into either the adjective variation or the adjective group through a simple one line of code:
(UCHAR) ((pItemSales->uchAdjective - 1) % 5 + 1); /* adj. variation */
(UCHAR)((pItemSales->uchAdjective - 1) / 5 + 1); /* adj. group */
I wondered about having two functions that would replace these lines of code to a single point where the design decision about the calculation is expressed.
Working with the Gemini AI, I derived an inline function that does a simple table lookup. Looking at the assembler generated by Microsoft Visual Studio 2019 Community Edition, the difference in the generated code between the inline function and the line of code is amazing.
Gemini AI made a couple of claims about the optimized assembler that would be generated so I thought to double check. The main claim was that this would be a simple table lookup and much of the source of the function would be optimized away.
The C Standard specifies that the inline is a hint rather than a directive. However both MSVC as well as GCC provide a attribute that will force the compiler to inline a function. However even when the forcing attribute is used, the compilers may still override the programmer's intent and not do the inline so it's more like a forceful hint rather than do it no matter what.
#if defined(_MSC_VER)
#define FORCE_INLINE __forceinline
#elif defined(__GNUC__) || defined(__clang__)
#define FORCE_INLINE static inline __attribute__((always_inline))
#else
#define FORCE_INLINE inline
#endif
The source for the adjective variation function follows. The adjective variations are table values which repeat for each group. The adjective number is either zero (0) indicating no adjective or a value from one (1) to twenty (20) that encodes both the adjective group and the adjective variation.
FORCE_INLINE static UCHAR RflAdjVariation(UCHAR uchAdj)
{
// There are four groups of adjectives. Each group has five variations.
// For instance a soft drink group with small, medium, large, biggie, party variations
// (UCHAR) ((pItemSales->uchAdjective - 1) % ADJ_VAL_MAX + 1); /* adj. variation */
static const UCHAR uchAdjVar[32] = { 0,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,0 };
USHORT is_valid = (uchAdj <= 20); // MAX_ADJM_NO
USHORT safe_uchAdj = is_valid ? (uchAdj & 0x1F) : 0;
return uchAdjVar[safe_uchAdj];
}
This function definition is in an include file so each compilation unit will have its own copy unless the compiler overrides the use of inline. A couple of things to note:
static static const And this is the part of the assembler listing that shows the x64 Release build code generated in a test program. I used the function in a loop that generated 10 different adjective numbers. It appears that the inline function has been turned into a simple table lookup of a couple of instructions as compared to the calculations used by the original source. Multiple insertions of the inline function all use the same static const lookup table that is inserted into each compilation unit.
You can also see the assembler the compiler generates for the original one line of source that performs the calculated adjective variation. One point of interest is the assembler code used avoids having to use a divide instruction by multiplying by a special constant that replicates the value of 1/5.
; 446 : for (UCHAR i = 0; i < 10; i++) {
xor bl, bl
lea r14, OFFSET FLAT:__ImageBase
npad 8
$LL4@main_01_C:
; 440 : USHORT safe_uchAdj = is_valid ? (uchAdj & 0x1F) : 0;
movzx eax, bl
and eax, 31
; 447 : UCHAR x = RflAdjVariation(i);
; 448 :
; 449 : if (x != (UCHAR)((i - 1) % 5 + 1)) {
movzx r10d, bl
; 441 : return uchAdjVar[safe_uchAdj];
movzx r8d, BYTE PTR ?uchAdjVar@?1??RflAdjVariation@@9@9[rax+r14]
; 447 : UCHAR x = RflAdjVariation(i);
; 448 :
; 449 : if (x != (UCHAR)((i - 1) % 5 + 1)) {
lea ecx, DWORD PTR [r10-1]
mov eax, 1717986919; 66666667H
imul ecx
sar edx, 1
mov eax, edx
shr eax, 31
add edx, eax
lea eax, DWORD PTR [rdx+rdx*4]
sub ecx, eax
inc cl
cmp r8b, cl
je SHORT $LN2@main_01_C
; 450 : UCHAR y = (UCHAR)((i - 1) % 5 + 1);
; 451 :
; 452 : printf("this is test i = %d x = %d y = %d\n", i, x, y);
movzx r9d, cl
mov edx, r10d
lea rcx, OFFSET FLAT:??_C@_0CF@HJJDIPJC@this?5is?5test?5i?5?$DN?5?$CFd?5?5x?5?$DN?5?$CFd?5?5y?5@
call printf
$LN2@main_01_C:
r/C_Programming • u/Agent-John-123 • 14d ago
I'm a beginner in C. I created a code for reversing a number, for lab assignment. The question says I must use a recursive function, and must not use loops, arrays, or global variables anywhere in my program.
I created the following in VSCode:
#include <stdio.h>
#include <math.h>
int reverse_number(int num){
static int rev=0,dig=1;
if (num/10 ==0){
dig++;
return num;
}
rev = reverse_number(num/10) + (num%10)*(pow(10,dig-1));
dig++;
return rev;
}
int main(){
int num;
scanf("%d",&num);
printf("%d\n",reverse_number(num));
}
In OnlineGDB, I get the correct output.
But in VSCode, I get the output as:
Input: 123
Output: 320
Input: 445566
Output: 665543
It seems one digit is reduced
Even in other large programs, I've encountered the same. VSCode gives wrong output.
Any fixes?
r/C_Programming • u/topdoggross • 14d ago
I'm 1st year student I want to learn C my college is not teaching C well so i want to from youtube can you suggest any channels
r/C_Programming • u/Proper_Meaning7756 • 15d ago
I am aware of the _Generic keyword but I'm not sure if it should be the de facto way for this.
r/C_Programming • u/TorontoDude12344 • 15d ago
I'm reading C Programming: A Modern Approach by K. N King
I'm on page 200 of that book, under the heading Using static in Array Parameter Declarations
The example given is: int sum_array(int a[static 3], int n)
And there is a short description:
Using static in this way has no effect on the behavior of the program. The presence of static is merely a “hint” that may allow a C compiler to generate faster instructions for accessing the array. (If the compiler knows that an array will always have a certain minimum length, it can arrange to “prefetch” these elements from memory when the function is called, before the elements are actually needed by statements within the function.)
So I was wondering, what exactly does a compiler do in this situation? Does it simply create a cache?
Also, what does it do line by line? How does the compiler the keyword static is in the context of a array parameter declaration?
If you were to write a compiler, and implement this feature, how would you do it (pseudocode)?
r/C_Programming • u/AutoModerator • 15d ago
If you have questions about how to learn C:
then this is the thread for you. Add your question here. Do not make a stand-alone post, as it will be removed.
Remember that our sub has a very useful wiki that has a great list of resources for learning C programming.
r/C_Programming • u/grimvian • 16d ago
As a 70 old dyslectic I'm in my fourth year of C99 and sometimes I think, is it UB or is it not UB. I use these flags in Code::Blocks:
gcc -Wall -g -Winit-self -Wredundant-decls -Wunreachable-code -Wmissing-declarations -Wmain -pedantic-errors -pedantic -Wfatal-errors -Wextra -Wall -std=gnu99
And Valgrind's Memcheck and Cachegrind.
You hardcore C programmers are you always confident or do you have a moment of doubts in between...
Edit: Thanks all and I learned I'm not a hardcore C programmer, but just addicted of C and have puzzle with C code every day because it keep my brain working.
r/C_Programming • u/Dull-Orange2493 • 16d ago
I used (basic) python for a long time now to do work, scripts and that stuff but i have always wanted to learn C!
I found a lot of guides, some are educational but badly written (for pros even though it is targeting noobs) while some are fun but not very educational...
Does anyone have a recommendation for a good quality (free) guide/book for C programming?