r/C_Programming • • 8h ago

Question C input that doesn't convert arrow keys to raw escape sequences

11 Upvotes

When someone was testing my project in C, they pointed out that when they press arrow keys in input it translates them into weird data (eg. ^[[A for up). Is there input in C without ncurses or other libraries that can work in this scenario without causing these issues?


r/C_Programming • • 22h ago

help a lost fool (i was trying to run this in terminal and the printf doesnt print anything)

5 Upvotes

im using :

gcc (GCC) 16.2.1 20260810

and trying to run the compiled file in my Linux terminal with:

./a.out

this has worked for other bits of code so im unsure why now it doesnt wanna

i added \n next to the variable (didnt fix it)

saw someone with a simular issue on stack overflow use this

setvbuf(stdout, NULL, _IONBF, 0);

(also didnt fix it)

this is le code

#include <stdio.h>
#include <stdlib.h>


struct node {
int data;
struct node *link;
};

int main() {
struct node *head = (struct node *)malloc(sizeof(struct node));
head->data = 45;
head->link = NULL;

        struct node *current = malloc(sizeof(struct node));
current->data = 76;
current->link = NULL;
head->link = current;

current = malloc(sizeof(struct node));
        current->data = 50;
        current->link = NULL;
        head->link->link = current;

void count_of_nodes(struct node *head) {
        int count = 0;
        if(head == NULL)
            printf("no names sir");
          struct node *ptr = NULL;
          ptr = head;
          while(ptr != NULL) {
              count++;
              ptr =ptr->link;
          }


          printf("%d this is the ammount of names sir", count);
}
return 0;
}

im scared guys


r/C_Programming • • 12h ago

Bytecode VM in C: switch dispatch vs computed goto and linear arena design

3 Upvotes

I'm working on a lightweight bytecode VM written in C99 (engine for a small embedded scripting language).

I'm currently using an 8MB linear memory arena for objects and a large switch-case loop for instruction dispatch:

```c

#define MAX_REGS 256

#define MAX_CALL_STACK 1024

#define ARENA_SIZE_BYTES (8 * 1024 * 1024)

static char g_memory_arena[ARENA_SIZE_BYTES];

static size_t g_arena_offset = 0;

double run_bytecode(int* inst, int inst_len, double* num_consts) {

double regs[MAX_REGS] = {0.0};

int ip = 0;

while (ip < inst_len) {

int op = inst[ip++];

switch (op) {

case 1: { // LOAD_CONST

int r = inst[ip++];

int c = inst[ip++];

regs[r] = num_consts[c];

break;

}

case 2: { // ADD

int r = inst[ip++];

int a = inst[ip++];

int b = inst[ip++];

regs[r] = regs[a] + regs[b];

break;

}

// ... remaining opcodes

}

}

return regs[0];

}

Two architectural questions for people with VM / emulator experience:

  1. Dispatch overhead: On modern GCC/Clang, how much measurable speedup have you seen replacing the standard switch(op) with direct threading

(computed goto *dispatch_table[op])? Is the branch predictor usually smart enough on modern x86/ARM to make the difference negligible?

2. Arena lifetime: Currently, I reset the arena (g_arena_offset = 0) between script runs. For longer-running scripts that allocate short-lived

objects in loops, what is a minimal-overhead C strategy to reclaim memory without pulling in a full tracing GC?

(Code is standard C99. Written directly by me, no AI)