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.
12
u/ScallionSmooth5925 Oct 14 '26
Because we have 40 years of legacy code you can't just replace core features in a breaking way and if you keep the old system then what's the point of the new why would this be better?
2
u/flatfinger Oct 15 '26
C was designed in an era where there a compiler wouldn't always have enough memory available to hold all of the constants that a program might need. Having a preprocessor whose job was simply to perform macro substitutions for all the constants used by a program, and then feeding the output from that into the main compiler, reduced the amount of stuff that would need to be kept in memory during any stage of processing. As it is, the preprocessor is just barely good enough to discourage the development of something better.
6
u/ffd9k Oct 14 '26
Preprocessing is supposed to be simple, consisting only of simple text replacement and conditional inclusion, without complex things like loops, recursion etc., so the preprocessor is intentionally limited.
Having two layers of complex language features working on top of each other would make it very hard to figure out what is actually going on, see C++ templates.
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
4
u/soundman32 Oct 14 '26
When I started using C (late 1980s) you were limited to a few kilobytes of RAM. Building code was at least 3 separate programs, pre-processor, one-pass compiler, and linker. These days those 3 steps are combined, but you still have that backward compatibility.
3
u/AKostur Oct 14 '26
Code generators exist. Simple text replacement already exists in the preprocessor. Complicated text replacement: C's not the best/easiest tool to reach for.
2
u/Local-Difference-997 Oct 14 '26
Why?????
2
u/WittyStick Oct 14 '26 edited Oct 14 '26
(f)lex and yacc/bison work similarly:
The
.land.yfiles are then transformed to plain .c files:The rules differ slightly between lex and yacc, but are similar and share the syntax of semantic actions - code between
{and}is emitted as plain C code for the matching rule. There are also various other syntactic forms to emit structures, unions etc before the start of the rules, and configure the lexer/parser options.Internally, flex and bison use the
m4macro processing language for their templates (as does autoconf).m4 is simple enough to use as a text-replacement pre-preprocessor, which is why nobody has bothered to write one which uses C.
Eg, to replace
foowith "hello", you write an .m4 file with:And run
m4 main.m4 > main.c.It's not even necessary to have the
defineline in the m4 file. You can just have a plain c file:And pass
-Dfoo=\"hello\"to replace allfoowith"hello".1
u/mjmvideos Oct 14 '26
Where does #define and #include get processed?
1
u/GhostVlvin Oct 14 '26
Why not just
?
1
u/mikeblas Oct 14 '26
Lots of people extend the language by writing their own translators that eat whatever extensions they want and translate that into standard C. I'm not sure how this particular construct is valuable, but maybe you have some others planned, or some wide-spread use for this one. Why not just write the preprocessor you want?
1
u/NeuroSynchroBonding Oct 14 '26
I think you'd like Lisp
1
u/Still_Explorer Oct 16 '26
The only way to think about having programs that dynamically change themselves, would make sense on an environment of scripting/interpreted language.
Not in a sense that "this is the most important way to do the job" but in a sense that since the interpreted environment has already the entire infrastructure ready to work as such, doing fancy and bold AST manipulations comes for free.
Then the next part obviously is to take the generated-modified AST and have it compiled in a static form with LLVM to become native executable as such.
The catch here however is that having dynamic languages be compiled to native, is only a thing happened recently (say about last 15+ years or something), and despite many options available still is not an *industry standard* practice, but rather like some workaround.
On the other hand talking about C/C++, those would become mission-critical compilers and they would only need to stabilize-secure-evolve only upon their first initial proof of concept. Impossible to get those to have ground breaking innovations.
However it won't be odd at all that if there's a variant of C that goes through the interpreted language route first - and to be compiled at the late stage next, then possible that will allow for very advanced meta programming by allowing you to gain access on program elements during runtime.
One such as this: https://root.cern/cling/
-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.