3 ms·
"Avoid #defines for constants like the plague. Use static constexpr instead." Pretty much every C textbook I can think of now is full of #defines for constant
by inglor_cz 2y ago
"Avoid #defines for constants like the plague. Use static constexpr instead."
Pretty much every C textbook I can think of now is full of #defines for constants. I, too, am (de)formed by this.
- niborgen 2y agoThanks for your comment. The blog post was written with C++ in mind, but I just learned that C now also has constexpr: https://en.cppreference.com/w/c/language/constexpr https://en.cppreference.com/w/c/language/constexpr. Time to update those books (:
- sltkr 2y agoBecause `constexpr` was not supported in C until very recently (C23). And `static const` by itself is not a good alternative, because constants can't be used in a lot of places, for example when declaring the size of an array.
- nj5rq 2y agoConstants can be used for array sizes since a lot of C standards ago, right?
- PhilipRoman 2y agoThey end up as a VLA which may or may not be what you want.