Hi Alejandro,
> I've added the _Countof to ISO C2y and GCC 16. Clang 21 has also added
> it. There's a <stdcountof.h> compiler header with a countof macro too.
> <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3550.pdf#subsubsection.0.6.5.4.5>
> <https://thephd.dev/the-big-array-size-survey-for-c>
> <https://thephd.dev/the-big-array-size-survey-for-c-results>
> <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117025>
> <https://github.com/llvm/llvm-project/issues/102836>
Thanks for working on this. It's a nice addition, to not have to write
#define SIZEOF(a) (sizeof(a)/sizeof(a[0]))
in many places.
> I see that gnulib uses the usual sizeof division often, so I was
> wondering if we should add the countof macro to gnulib with a fallback
> implementation when the system doesn't support it, and also use it
> within gnulib.
I think we should do it, once the next version of ISO C is finalized.
Why not already now? Because
* The existing code
#define SIZEOF(a) (sizeof(a)/sizeof(a[0]))
will continue to work without problems. Thus, there is no hurry.
* The standards authors may still change their minds.
- We have seen ISO C changes even in the final stages of ISO C 23
(remember the #b support?).
- The need for an extra header <stdcountof.h> is questionable,
because no existing code uses '_Countof' [1] and we have seen
how <stdbool.h> was introduced and later made redundant.
- How about the synchronization with ISO C++? I was disappointed to see
that code that uses 'unreachable' as defined by ISO C is not valid C++
[2].
Bruno
[1] https://codesearch.debian.net/search?q=%5Cb_Countof%5Cb&literal=0
[2] https://lists.gnu.org/archive/html/bug-gnulib/2025-05/msg00263.html