Why isn’t (almost) pure C used instead of the C preprocessor?
Sure I understand it would be a little more verbose. For example:
#define PI 3.14159
If we had regular C as preprocessor language, the above could be rewritten to something like:
#define PI {printf("3.14159");}
The idea is to not substitute {printf(...)}
for PI
but to run the code from the macro definition and splice the output of that code in the place of PI
.
Yes, it’s more verbose, but for more complex stuff I think it would help alot. Also, going with this design, programmers wouldn’t have to learn another language (namely the C preprocessor) in order to write macros.
So, are there any other reason except the verbosity that the C preprocessor is not just pure C (with perhaps small modifications, like the addition of the define
keyword)?
11
What you suggest is possible with more modern languages. C was invented at a time when compiler technology was far too primitive to allow this kind of programmable compilation pipeline. Furthermore, most programmers knew of no use for such a mechanism.
Even today it’s difficult to construct a compiler that can permit such programming. It’s easy to specify the behaviour in such simple cases but far more difficult to consider what it should do in the general case.
4