3

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
  • 4
    You can calculate the area of a circle with area = PI*radius*radius with the first example. Your second example is a syntax error. This question is confusing. Jan 5, 2015 at 22:06
  • 2
    Where would printf in the macro come from? Usually it's a library function declared in the header stdio.h which... is included using the C preprocessor. See the issue?
    – user7043
    Jan 5, 2015 at 22:06
  • 3
    I believe that he is intending that the compiler executes the printf.
    – DeadMG
    Jan 5, 2015 at 22:07
  • 1
    @whatsisname I think 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.
    – user7043
    Jan 5, 2015 at 22:07
  • 2
    @delnan: If that's the idea, then I cannot imagine the horror of trying to debug software with significant usage of those. The preprocessor as-is is bad enough. Jan 5, 2015 at 22:10

1 Answer 1

5

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
  • 7
    It's certainly hard to give satisfactory syntax and semantics to compile-time metaprogramming, but then again the C preprocessor doesn't look too good in hindsight either. Working fully programmable macros are a Lisp thing, and while they didn't spring from McCarthy's mind fully grown, they existed in the sixties. Admittedly, interpreting Lisp at read-time is easier than interpreting (even a stripped-down dialect of) C.
    – user7043
    Jan 5, 2015 at 22:13
  • The C preprocessor is not great, but it was actually implementable.
    – DeadMG
    Jan 6, 2015 at 10:10
  • And I supplied evidence a similarly not-great macro processor might be implementable. I'm too young to make definite statements about the capabilities of 60's technology but so far you only asserted that "compiler technology was far too primitive".
    – user7043
    Jan 6, 2015 at 10:31
  • @delnan: Of course, you argue that Lisp implemented similar features, but there's little evidence that it was competitive with C at the time. "Some dude implemented it" != "It was reasonably implementable".
    – DeadMG
    Jan 6, 2015 at 16:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.