r/C_Programming • u/grass_toucher_3200 • 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.cIt happens before anything else and only affects one file.
View all comments
-1
u/ralphpotato Oct 14 '26
I’m sure this topic has a lot of depth, but languages have solved this in some different ways:
C++ has constexpr
Zig has comptime
Rust has macro_rules
It’s obviously desirable to have macro behavior with the same syntax as the actual language. Other people probably know the reasons and history better but I view the C preprocessor as a choice before other ideas came along and also as a consequence of computers having more limited power to compile. Modern C++ or rust compilers would definitely have not been able to exist 20-30 years ago.