r/C_Programming • • Oct 14 '26

Why not preprocess in C itself?

For example, at the beginning of the code, have something like this:

replace_token("foo", "\"hello\"");
use_defer();

#define PREPROCESSOR_END

#include <stdio.h>
void main() {
  defer(printf(" world\n"));
  printf(foo);
}

Then before compiling, a script simply runs the upper part, with the lower as a global string. The difficulty of course is in implementing the functions. But this way you don't have to wait for a C feature to be added in 2030. Which now that I think of it isn't that far off. Time flies. Perhaps there are better uses of it than programming, like dancing at party and doing the sex.

Edit: I mean like this:

preprocess ./main.c -o new_main.c; gcc new_main.c

It happens before anything else and only affects one file.

0 Upvotes

29 comments sorted by

View all comments

12

u/etaithespeedcuber Oct 14 '26

Because C is a very very old language and this kind of thing wasn't really thought of until the last 2 decades. Many modern languages have it (or something similar). You can achieve some of the functionality you're looking for through your build system and making it compile and run a build.c file before anything else is done

3

u/ActuallyFullOfShit Oct 14 '26

Well, not really. Same language macros were a thing in LISP back in the 60s or something. And what is a C preprocessor directive if not just a shitty version of a macro.