Which C version is used in the Linux kernel?

Viewed 26254

Does the Linux kernel use only the old C90 syntax or has it been optimized with C99 / C11 features?

I was wondering if the newest versions of C are used when possible.

5 Answers

Stumbled over this while checking why the kernel isn't requiring C99 after 22+ years. There are parts of C99 used here and there in the kernel, but guarded by #ifdefs.

In any case, the following discussion (Sept. 2021) between Linus and some GCC people gives some relevant context, although it's mostly about standard-mandated header-includes (like <stdint.h>) and not so much about C language-features.

I find it a pity that the thread petered out, particularly the C99-<stdint.h> case with fixed-width integer types would be really helpful IMO.

More generally, now that the kernel also supports being built by Clang, it'd be great to move to a standard (by which I mean ISO C) baseline, rather than effectively a GNU-based one. And a more than 20 year old standard should really be enough IMO, even for the kernel...

It's surprisingly difficult to find an official declaration (or even recommendation) on which version of the C standard is allowed in kernel code. The closest thing that can be found (which doesn't really settle the question) is that the minimum compiler requirement for the kernel is gcc 3.2.

However, note that designated initializers and "long long" (both being strictly C99) are widely used throughout the kernel code and kernel modules. Thus the kernel would not compile as pure C89/C90. You absolutely require a C99 compiler (or, at a very minimum, a C89 compiler with those things as "non-standard extensions"). So the idea that the kernel is written in C89 is definitely incorrect. (There might also be other C99 features there, but I haven't checked.)

On the other hand, the coding style requirements for kernel code are very strict (this being programmatically checked), and AFAIK some C99 features are disallowed (such as declaring variables in the middle of the code, or as a for-loop parameter). I get the impression that there's some kind of implicit mostly-unstated principle of "code in C89 by default, but you are allowed to use these few C99 features, but nothing else".

Related