Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which C version is used in the Linux kernel?

Tags:

linux-kernel

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.

like image 705
PerrierCitror Avatar asked Dec 15 '13 22:12

PerrierCitror


People also ask

What version of C is the Linux kernel?

The Linux kernel's foundation is the ancient C89 standard of C.

Is the Linux kernel in C?

Linux kernel development started in 1991, and it is also written in C. The next year, it was released under the GNU license and was used as part of the GNU Operating System. The GNU operating system itself was started using C and Lisp programming languages, so many of its components are written in C.

Which language is used in Linux kernel?

As of now, the Linux kernel is written in the C programming language—essentially, the same language used to write kernels for Unix and Unix-like operating systems since the 1970s.

What is a kernel in C?

The kernel is a computer program at the core of a computer's operating system and generally has complete control over everything in the system. It is the portion of the operating system code that is always resident in memory and facilitates interactions between hardware and software components.


2 Answers

See the bottom of this answer for updates.

The Linux kernel coding style document doesn't say much about the use of C90 vs. C99.

It suggests the use of typedefs for "New types which are identical to standard C99 types, in certain exceptional circumstances" while discouraging typedefs in most cases. Note that this doesn't actually imply depending on the C99 standard, since such typedefs can be defined in pure C90.

Later in the discussion of typedefs, it says:

In certain structures which are visible to userspace, we cannot require C99 types and cannot use the u32 form above. Thus, we use __u32 and similar types in all structures which are shared with userspace.

The implication is that the kernel must work with user code written in C90.

The only other reference to C99 is:

Linux style for comments is the C89 "/* ... */" style.
Don't use C99-style "// ..." comments.

The top-level kernel documentation web page refers to the C99 standard as the "current version of the C programming language" (which was probably correct when it was written; the current official version is now C11).

Looking at the kernel sources, there are 1766 Makefiles in the directory tree (as of the last time I checked it out from git). Of these, only 3 refer to the -std=gnu99 gcc option, and those are for tools, not for the main kernel itself (and 2 more refer to -std=gnu89, which is currently the default). This implies that the vast majority of the Linux kernel sources are written to be compiled with options that cause it to (mostly) conform to the C89/C90 standard with some GNU-specific extensions. Some of these extensions are C99 features.

The coding conventions for the Linux kernel are largely controlled by Linus Torvalds. This message of his from April 2012 shows his personal attitude regarding (some) C99-specific features:

On Wed, Apr 11, 2012 at 9:28 PM, Oleg Nesterov <[email protected]> wrote: > > Agreed. But, > >        error: 'for' loop initial declaration used outside C99 mode > > we should change CFLAGS, I guess. BTW, personally I'd like very much > to use "for (type var; ...")" if this was allowed.  The sad part is that if we allow that, we also get that *other* insane C99 variable thing - mixing variables and code.  I *like* getting warnings for confused people who start introducing variables in the middle of blocks of code. That's not well-contained like the loop variable.  That said, most of the stuff in C99 are extensions that we used long before C99, so I guess we might as well just add the stupid flag. And discourage people from mixing declarations and code other ways (sparse etc).                           Linus 

Bandrami's answer is partially correct in pointing out that many of the feature added by C99 are in the library. For the most part, library features are irrelevant to the Linux kernel. The kernel does not run in a "hosted" environment, and it doesn't have access to most of the C standard library; for example, you can't use printf in the kernel (there's a similar printk function used for logging and debugging). But that's only part of the picture. Many of the features added by C99 are in the language proper (i.e., the part described by section 6 of the ISO C standard), and are at least potentially applicable to kernel source code.

UPDATE :

RudolfW points out this commit, which makes the -std=gnu89 configuration (C 89/90 with GNU extensions) explicit.

End result: we may be able to move up to a newer stdc model eventually, but right now the newer models have some annoying deficiencies, so the traditional "gnu89" model ends up being the preferred one.

This was in response to a change in gcc release 5, which changed the default standard option from -std=gnu90 to -std=gnu11 (skipping -std=gnu99). The top-level Makefile in the linux git repo still refers to -std=gnu89 as of Fri 2020-09-18. (-std=gnu89 and -std-gnu90 are equivalent.)

Marc.2377's answer cites a document that's probably more directly relevant. It explicitly says that "the kernel is typically compiled with gcc under -std=gnu89".

like image 141
Keith Thompson Avatar answered Oct 06 '22 00:10

Keith Thompson


As of now, 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=gnu89 [gcc-c-dialect-options]: the GNU dialect of ISO C90 (including some C99 features).

like image 24
Marc.2377 Avatar answered Oct 05 '22 23:10

Marc.2377