January 8, 2025
written by Walter Bright
Standard C undergoes regular improvements, now at C23.
But there are baffling things that have not been fixed at all.
The Dlang community embedded a C compiler in the D programming language compiler so it could compile C.
This C compiler (aka ImportC) was built from scratch.
It provided the opportunity to use modern compiler technology to fix those shortcomings.
Why doesn’t Standard C fix them?
- Evaluating Constant Expressions
- Compile Time Unit Tests
- Forward Referencing of Declarations
- Importing Declarations
Evaluating constant-expression
Consider the following C code:
int sum(int a, int b) { return a + b; } enum E { A = 3, B = 4, C = sum(5, 6) };
When compiled with gcc:
gcc -c test.c test.c:3:20: error: enumerator value for C is not an integer constant enum E { A = 3, B, C = sum(5, 6) }; ^
In other words, while C can compute at compile time a simple expression
by constant folding,
it cannot execute a function at compile time. But ImportC can.
Everywhere a C constant-expression appears in the C grammar
the compiler should be able to execute functions at compile time, too,
as long as the functions do not do things like I/O, access mutable global
variables, make system calls, etc.
Compile Time Unit Testing
&#x