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

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.

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.