Detecting Constant Expressions in C: A Macro-Based Approach

2025-05-13

This article explores various methods for creating a C macro that checks if an expression is a compile-time constant. The author investigates approaches using C23's `constexpr` and `typeof`, GNU extensions like `__builtin_constant_p`, C11's `static_assert`, and clever tricks with `sizeof` and compound literals. Each method has its strengths and weaknesses; some require newer C standards, rely on compiler extensions, or might alter the expression's type or generate warnings. The article concludes by summarizing the pros and cons and expressing a desire for more elegant solutions.

Development compile-time constant