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

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.

1

u/grass_toucher_3200 Oct 16 '26

Wouldn't development of something better simply be a script that separates the two parts, creates a file like

#include <convenient code manipulation functions and shit.h>
main() {
  code = [paste lower part stringified here]
  [paste upper part here]
  printf(code)
}

and compiles and runs it?

1

u/flatfinger Oct 16 '26

Compiling and running C code was faster than compiling and running FORTRAN or COBOL, but it wasn't the fastest thing in the world. Judging from the 1974 C Language Reference, it seems likely that the process of building a C file would start by having what I'd guess would have been a small executable look at the beginning of a source file and then depending upon what appeared there either run the preprocessor and then feed its output to the compiler, or would simply run the compiler on the source file. The fact that there was a short-cut path to run the compiler directly suggests that each extra step in the build would have taken a non-trivial amount of time.

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

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.

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/WittyStick Oct 14 '26 edited Oct 14 '26

(f)lex and yacc/bison work similarly:

%{
// Header C code emitted verbatim
%}

%%
// rules go here
%%

// Footer C code emitted verbatim

The .l and .y files are then transformed to plain .c files:

flex -o lexer.c lexer.l
bison -o parser.c parser.y

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 m4 macro 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 foo with "hello", you write an .m4 file with:

define(foo, "hello")dnl

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

And run m4 main.m4 > main.c.

It's not even necessary to have the define line in the m4 file. You can just have a plain c file:

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

And pass -Dfoo=\"hello\" to replace all foo with "hello".

m4 -Dfoo=\"hello\" main.m4
#include <stdio.h>
void main() {
    defer(printf(" world\n"));
    printf("hello");
}

1

u/mjmvideos Oct 14 '26

Where does #define and #include get processed?

1

u/GhostVlvin Oct 14 '26

Why not just

#define foo "hello"
int main() {
  defer(printf(" World\n");
  printf(foo);
}

?

1

u/Ecstatic_Student8854 Oct 14 '26

No such thing as defer in C

1

u/ScallionSmooth5925 Oct 14 '26

It's a planed feture for C29. See ISO/IEC AWI TS 25755 or N3688 working draft 

1

u/NeuroSynchroBonding Oct 14 '26

I think it will be added in C29.

1

u/GhostVlvin Oct 15 '26

There is some gcc macro magic that gives us defer-like behaviour

0

u/ffd9k Oct 14 '26

Not yet.

And clang already supports it of course.

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.

2

u/etaithespeedcuber Oct 14 '26

None of these are really the same as literally altering the text before anything gets parsed though

1

u/ralphpotato Oct 14 '26

That’s on purpose though. The C preprocess is a hammer when usually you want something more restrictive. I haven’t done much macro writing myself, but people really love zig comptime and some projects have benefited greatly from good rust proc macros.

It is very hard to statically analyze anything a C preprocessor macro does because of how blunt it is. There are few rules- you can straight up redefine reserved words and inside a macro is basically a typeless, mysterious space.

-2

u/Amr_Rahmy Oct 14 '26

Personally I never thought lack of macro would really impact startup time much. Always saw it as a waste of development time.

I see it as something people did with the illusion or assumption that they will gain meaningful speed ups but to me it’s just silly. You can start the application, do all you need to do and then enter your main loop or code block.

Maybe in the 50-70s with severe constraints it made more sense but not in 2026.

1

u/ralphpotato Oct 14 '26

I’m not sure how this pertains to the concept of compile time code evaluation.

1

u/ScallionSmooth5925 Oct 14 '26

What you whant is constexpr not a macro.