Wednesday, December 26, 2012

C11, yo

I think that the free software community should throw its collective enthusiasm behind the features in the new C language standard -- ISO/IEC 8988:2011.  We should encourage, collectively, that the new features of C11 be used whenever appropriate.

The new C11 standard for the C language has some features I think are exciting.
  • Threads and mutexes have been brought into the standard library
  • A set of atomics and thread-local storage primitives are defined
  • UTF-16 and UTF-32 to multibyte conversion functions are added
  • Static compile-time assertions allow the use of sizeof() and friends
  • The safer C library functions: fopen_s, strcpy_s, etc.
  • Anonymous structs and unions
  • Type-generic macros
This is on top of C99 features that haven't been used much
  • Variable length arrays, for example
Many of these features have been in gcc for a while; but, the de facto practice has been to avoid extensions for the sake of portability.  There's been a tendency to hold fast to C89 or even pre-K&R C.

For quite a while now, the basic GNU guidance on choosing languages has been to go with C or Guile first, and choose other languages only for special cases.  But the basic GNU and GNOME practice in recent years has been to go with pretty much anything other than C or Guile for new code.  People avoid C because it is too much manual labor, and people avoid Guile for other reasons.

If GNU wants its coders to stick with C in non-kernel space, as a community, we've got to stop discouraging the use of new features that improve the life of a C coder and the readability of C code.

The argument against using new features of C99 and now C11 is that it harms portability.  However, making a virtue of requiring C89 discourages the use of these time-saving constructs, which drives coders away from C, which harms (a different type of) portability.

Variable-length arrays, alone, would save a tremendous amount of time and energy:

int func(int n)
{
  double arr[n];
}

In this case, the array 'arr' is allocated and freed automatically.

All the major C compilers have committed to implementing these features: gcc, clang, and the non-free offerings.  In the next few months, all the major compilers will be up to speed.  Soon the only reason not to use C11 will be because you want to target older or minority compilers.

Let's make our lives just a little bit easier, and use the new features.