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.
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.
As of Mon 2022-09-19, the Linux kernel is typically compiled with gcc -std=gnu11
, which supports the GNU dialect of the 2011 edition of the ISO C standard.
Reference: https://www.kernel.org/doc/html/latest/process/programming-language.html
The kernel is written in the C programming language. More precisely, the kernel is typically compiled with gcc under
-std=gnu11
: the GNU dialect of ISO C11. clang is also supported, see docs on "Building Linux with Clang/LLVM".
Thanks to marc-2377 for posting a reference to that document in his answer.
Linus Torvalds has previously expressed a dislike for some features that were introduced in C99, such as mixing declarations and statements. At the time, the kernel was compiled with gcc -std=gnu89
. I haven't investigated whether his attitude has changed since then. But that's a matter of coding style, not language standards.
I've deleted the bulk of my original answer since it's no longer relevant.
As of today (2022-09-19), the document at https://www.kernel.org/doc/html/latest/process/programming-language.html says:
The kernel is written in the C programming language [c-language]. More precisely, the kernel is typically compiled with gcc [gcc] under
-std=gnu11
[gcc-c-dialect-options]: the GNU dialect of ISO C11.
(It also says Clang is supported as well.)
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 #ifdef
s.
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".
There isn't really an answer because your question makes faulty assumptions. The C language versions assume the existence of a platform, but OS kernels like Linux are the platform (or at least a large part of it), so they don't have a "version" in that sense.
In terms of the parser's definition of the language, Linux is written in whatever a the concurrent gcc/icc/etc. will support, which as of now is C99. But like I said, the differences between C90 and C99 are based on the kernel and the library so they don't really apply to the kernel to begin with. (The only exception I can think of is anonymous functions, which the kernel doesn't use.)
Most of the day-to-day things you know about C are actually from the library, which depends on the kernel. So when you're programming a kernel, you're actually dealing with a much different set-up than when you are writing a normal C program.