49

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.

Acorn
  • 24,970
  • 5
  • 40
  • 69

5 Answers5

65

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 2
    See also [this commit](http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=51b97e354ba9fce1890cf38ecc754aa49677fc89) - they are now explicitly using C90 with GNU extensions because an immediate transition to C99/C11 might not yield fully reliable results. – RudolfW Feb 02 '15 at 22:37
  • This answer does not seem up to date. See Marc's answer https://stackoverflow.com/a/58313785/9305398 – Acorn Sep 19 '20 at 22:02
  • @Acorn How is it not up to date? (I edited the last paragraph after you posted your comment but before I saw it, but I believe it was already current and correct. Marc's answer does add a link to a document I hadn't seen before. – Keith Thompson Sep 19 '20 at 23:54
  • @KeithThompson The answer starts linking a document that is not really relevant (the style guide), then continues trying to infer an answer from Makefiles, commits and emails. That information is a good analysis, but if something is documented, one should refer to that instead of implementation details or trying to infer things from other sources of information. – Acorn Sep 20 '20 at 00:47
  • @Acorn I'd say it's incomplete, not out of date. – Keith Thompson Sep 20 '20 at 00:54
  • 1
    @KeithThompson It is out of date because that document seems to be new, so this answer was OK before. – Acorn Sep 20 '20 at 00:56
  • @Acorn I just added a citation to Marc's answer. – Keith Thompson Sep 20 '20 at 00:59
  • In case anyone is interested, I added an [answer](https://stackoverflow.com/a/70981967/2965879) below that references a relatively recent discussion of LKML that's broadly on this topic. – Axel Feb 04 '22 at 05:51
13

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.)

Marc.2377
  • 7,807
  • 7
  • 51
  • 95
1

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...

Axel
  • 636
  • 5
  • 10
0

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".

Warp
  • 363
  • 2
  • 9
-11

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.

Bandrami
  • 4,242
  • 1
  • 13
  • 12
  • 6
    That's incorrect. There are a number of *language* changes between C90 and C99, such as variable length arrays, `long long`, mixed declarations and statements, `_Bool`, and so forth. And I'm not sure what you mean by "anonymous functions"; there's no such feature in C90, C99, or C11. – Keith Thompson Dec 16 '13 at 00:03
  • @Keith can you then give an answer to my question ? –  Dec 16 '13 at 01:12
  • @Keith, the kernel has to do its own typing because all of those "language" features are implemented in the library, not the compiler (check the typedefs in the source tree some time). And, yes, the anonymous functions I was thinking of were a gcc extension; oops. – Bandrami Dec 16 '13 at 05:37
  • 3
    None of the features I mentioned are implemented in the library. None of them are typedefs. – Keith Thompson Dec 16 '13 at 05:40
  • 6
    This is wrong and misleading. There are loads of differences between C90 and C99 which have nothing to do with the standard library. Also, the standard doesn't depend on a "platform". It describes syntax and abstract semantics. It's not true either that Linux is written in C99, because it relies on quite a few non-standard GNU extensions. –  Dec 18 '13 at 17:55