r/C_Programming • • 4h ago

Best Way to Read 'Raw Data'

13 Upvotes

I'm writing a C program for the pi pico and realized I never really figured out how to properly cast characters to raw bytes then to other things, or if there was really a way to do that.

  1. Does char mean "signed char" by default in any C program, or is that up to interpretation by the compiler?
  2. Would using a void pointer to dereference then recast to *uint8_t work?
  3. When bytes are read through getc(), are the bytes modified before they are stored? As in, how are the bytes that are read interpreted to translate to characters, and how are they stored? My initial thought was that the bytes were interpreted "as-is", meaning that they were copied directly to the memory in the character and interpreted as signed characters. But it seems like they're being reinterpreted in some way as they are cast.
  4. If something about the process is too implementation specific, is reading as "raw data" just a flat out bad idea or not supported at all? Will the behavior be undefined on other systems? As long as I can get it to work on the pico it's fine, it doesn't have to be portable. But for future reference, should I not try to think of things as "raw data" unless the implementation specifically defines how it stores the data?
  5. Less important, but how are ascii codes mapped to signed characters? (every chart I've seen has never listed a negative number, only 0-255) (( and if signed is the standard, then why does every asii chart list 0-255... ))

// this was the void pointer way I was going to try, would this work?

tnext =  *((uint8_t*)((void*)&next));  // Cast to uint8_t

(Not sure if this is a repeat but I couldn't find exactly what I was looking for. Sorry if this is a dupe)

Conclusion: Seems like when you read getc() you just need to indicate "unsigned char" so that the byte is read unmodified and can be cast directly to a "uint8_t"? I think the reason I hadn't done that originally was because I thought "but getc returns char, not unsigned char", but I dont think that matters. getc should read into either


r/C_Programming • • 11h ago

Question A beginner in C ... Is building a shell a good beginner project ..if so what are the resources that i can follow to create it...

7 Upvotes

r/C_Programming • • 13h ago

Discussion Revealed vs. Opaque structs for Libraries

5 Upvotes

Hi all, I'm developing a win32 windowing library for mostly myself but also want it to be easy and intuitive to use and follow good C standards.

In my library have the main object window_t that is used for mostly everything. However, I'm wondering between a couple different design decisions for it:

// public .h
typedef struct window_t window_t;

window_t* createWindow(...); << createWindow heap allocates a window_t

void destroyWindow(window_t* window);

2.

// public .h

typedef struct window_t { implementation... ) window_t;

void createWindow(window_t* out, ...);

// no destroyWindow

the difference I'm looking is that for #1, the user mantains control of the heap allocated memory through createWindow() and must free it after they're done using it, but for #2 the user can create a stack allocated window and it doesn't need to be freed, however it removes the opaque struct safeness in #1.

Are there any pros/cons for either method I haven't thought of? My instinct tells me that #2 seems better as the user controls the memory allocation, they just need to be careful how they use the window_t.

Thoughts?


r/C_Programming • • 13h ago

Learning C weekly megapost for 2026-11-25

5 Upvotes

If you have questions about how to learn C:

  • which books are best?
  • which videos are best?
  • which classes are best?
  • which websites are best?
  • is there a "roadmap"?
  • what projects can I do?

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 • • 20h ago

cc_task: write C coroutines with async/await and closure

Thumbnail
github.com
4 Upvotes

Just like js and C#, but this is C, with closure. Also very lightweight, small enough to fit in a CPU cache line.

#include "cc_task.h"
#include <stdio.h>

int main(void)
{
    int c = 10; // task_b captures c and uses it as task state
    cc_task_t *task_b = cc_task_new(CC_TASK(t)
    {
        CC_BEGIN(t);
        printf("B: c = %d\n", ++$(c));
        CC_YIELD(t);
        printf("B: c = %d\n", ++$(c));
        CC_END(t, c);
    });

    cc_task_t *task_a = cc_task_new(CC_TASK(t)
    {
        CC_BEGIN(t);
        printf("A: await B ...\n");

        cc_uintptr code;
        CC_AWAIT(t, task_b, code);
        printf("A: B end, code = %llu\n", (unsigned long long)code);
        CC_END(t);
    });

    c = 0; // modifying c does not change the copy captured by task_b
    cc_task_t *t = task_a;
    cc_tr_e ret;
    do
    {
        ret = cc_task_resume(&t);
    }
    while (ret != cc_tr_end);

    return 0;
}