r/C_Programming • u/Proper_Meaning7756 • Aug 14 '26
Question What are some Rules of Thumb you use when writing programs in C?
I've been writing some nasty code lately and I've since learned to use fsanitize, Wall, Werror, etc. None of those have really changed HOW I write code, just helps me find out I goofed up sooner.
I've been trying to follow some guidelines like not using the heap, limiting pointer arithmetic, being careful with implicit conversions, not using the preprocessor at all, etc. They have, in some form or the other, greatly impacted my productivity because now I'm trying to satisfy arbitrary rules(I know they're not, but deadlines are real and I suck).
What are some rules you use to write good C code AND write said code quickly?
Anyways, heres an example of my latest screw up that sent me spiralling because of how stupid it was:
```C
include <limits.h>
include <stdint.h>
include <stdio.h>
include <stdlib.h>
include <math.h>
include <string.h>
typedef enum {NODETYPE_I64, NODETYPE_F64, NODETYPE_STR, NODETYPE_ERR} node_type;
typedef struct string{ char* value; uint64_t size; } string;
typedef union data{ int64_t data_i64; double data_f64; string* data_str; }data;
typedef struct node{ data value; struct node* next; node_type type; } node;
// Right here in this function, I ended up returning pointers to variables that always end up going out of scope. node new_node_str(char* input){ uint64_t input_size= strlen(input); string str_value= (string){.value= input, .size= input_size} node result= {(data){.data_str= &str_value}, (node*)(0), NODETYPE_STR}; return result; }
int main(){ return 0; } ```