Linux kernel has a coding style guide:

https://www.kernel.org/doc/Documentation/process/coding-style.rst

Nicer Formatted Version

Regarding your example, I personally prefer the first style. With the second style you will quickly violate this Linux kernel style rule (kernel style has 8-character indentation):

if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

Writing code from top to bottom (as opposed to horizontally) is sometimes referred as duffing. I can suggest you this excellent reading on the subject:

Reading Code From Top to Bottom

Answer from ouah on Stack Overflow
🌐
Linux Kernel
kernel.org › doc › html › v5.8 › process › coding-style.html
Linux kernel coding style — The Linux Kernel documentation
/* * This is the preferred style for multi-line * comments in the Linux kernel source code. * Please use it consistently. * * Description: A column of asterisks on the left side, * with beginning and ending almost-blank lines.
🌐
Reddit
reddit.com › r/c_programming › linux kernel coding style
r/C_Programming on Reddit: Linux kernel coding style
September 21, 2020 -

I would like to know your opinions on the coding guidelines found in the Linux kernel page.

What I really like:

  • "... align the switch and its subordinate case labels in the same column."

  • "It’s a mistake to use typedef for structures and pointers." (I do prefer repeating struct everywhere).

However I'm curious to what you think about:

  • Maximum size of 80 columns.

  • Using braces when single statements are enough:

if (condition)
        do_this();
else
        do_that();

I'm trying to stick to one coding style and be consistent with it.

What do you think about the linux kernel coding style? Jun 2, 2026
r/C_Programming
last mo.
Linux Kernel Aug 9, 2025
r/linuxquestions
11mo ago
Interesting example of Linux Kernel Source Code Nov 15, 2020
r/linuxquestions
5y ago
the linux "kernel" Feb 27, 2025
r/linux4noobs
last yr.
More results at reddit.com
Discussions

Linux kernel coding style
You should instead look into historical repositories like https://archive.org/details/git-history-of-linux, which go further back; however, before Bitkeeper the authorship of each change was not tracked in detail (that historical repository IIRC does not have the detailed Bitkeeper history, ... More on news.ycombinator.com
🌐 news.ycombinator.com
101
124
November 30, 2014
Linux kernel coding style

I always use braces for single-line if, while and for.

In my opinion, it reads more consistently, and gives me a solid visual indication of where the block ends.

Moreover, I'm often doing things like adding ad-hoc printf (or rather printk in kernel-space) for debugging purposes. Leaving the brackets in leaves me less prone to errors like this:

if (foo)
    bar();
    printf("%s\n", "bar called");
More on reddit.com
🌐 r/C_Programming
54
41
September 21, 2020
Linux kernel coding style - surprisingly fun to read!
Copying some of the best bits: There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3. Heretic people all over the world have claimed that this inconsistency is ... well ... inconsistent, but all right-thinking people know that (a) K&R are right and (b) K&R are right. Unlike Modula-2 and Pascal programmers, C programmers do not use cute names like ThisVariableIsATemporaryCounter. To call a global function foo is a shooting offense. Encoding the type of a function into the name (so-called Hungarian notation) is brain damaged - the compiler knows the types anyway and can check those, and it only confuses the programmer. No wonder MicroSoft makes buggy programs. If you are afraid to mix up your local variable names, you have another problem, which is called the function-growth-hormone-imbalance syndrome. However, if you have a complex function, and you suspect that a less-than-gifted first-year high-school student might not even understand what the function is all about, you should adhere to the maximum limits all the more closely. NEVER try to explain HOW your code works in a comment: it’s much better to write the code so that the working is obvious, and it’s a waste of time to explain badly written code. But remember: indent is not a fix for bad programming. There appears to be a common misperception that gcc has a magic “make me faster” speedup option called inline. More on reddit.com
🌐 r/programming
54
90
September 7, 2017
How can I configure the C language formatter to follow the Linux kernel coding style?
I believe there is an option hidden in settings that allows control of that. I’m not sure if it is universal or if it can be separated based on what you are doing (loops vs functions calls) More on reddit.com
🌐 r/vscode
1
0
March 29, 2022
🌐
GitHub
gist.github.com › davidzchen › 9187984
Sample C code using the Linux kernel coding style · GitHub
What a load of nonsense, unless cleanup needs to be done, you return directly, because it makes the intent clear instead of having to go down to the bottom of the function and read the code below a WORTHLESS label that immediately hits a return statement. This is specifically adressed in the real Linux kernel coding style guide.
🌐
Rptu
hpc.rz.rptu.de › documentation › coding_style.pdf pdf
Linux Kernel Coding Style Linus Torvalds
This is a short document describing the preferred coding style for the linux kernel.
🌐
Hacker News
news.ycombinator.com › item
Linux kernel coding style | Hacker News
November 30, 2014 - You should instead look into historical repositories like https://archive.org/details/git-history-of-linux, which go further back; however, before Bitkeeper the authorship of each change was not tracked in detail (that historical repository IIRC does not have the detailed Bitkeeper history, ...
🌐
Litux
litux.nl › mirror › kerneldevelopment › 0672327201 › ch20lev1sec2.html
Linux Coding Style
The Linux Kernel, like any large software project, has a defined coding style that stipulates the formatting, style, and layout of your code. This is done not because the Linux kernel style is superior (although it might be) or because your style is illegible (although it might well be), but ...
Find elsewhere
🌐
Linux Journal
linuxjournal.com › article › 5780
Proper Linux Kernel Coding Style | Linux Journal
If you find that your code is indented too deeply, with more than three levels of indentation that cause the code to shift to the right of the screen too far, then you should fix this. ... The original authors of UNIX placed their braces with the opening brace last on the line, and the closing brace first on the line: ... Therefore, the Linux kernel uses this style.
🌐
Linux Kernel Newbies
kernelnewbies.org › New_Kernel_Hacking_HOWTO › Kernel_Programming_Style_Guidelines
Linux kernel coding style
December 30, 2017 - (defun linux-c-mode () "C mode with adjusted defaults for use with the Linux kernel." (interactive) (c-mode) (c-set-style "K&R") (setq tab-width 8) (setq indent-tabs-mode t) (setq c-basic-offset 8))
🌐
ACM Digital Library
dl.acm.org › doi › fullHtml › 10.5555 › 513581.513588
Kernel korner: proper Linux kernel coding style | Linux Journal
Kernel korner: proper Linux kernel coding style · Software and its engineering · Software organization and properties · Contextual software domains · Operating systems · The pre-defined base kernel greatly affects the performance of multiple kernel learning (MKL), but selecting the pre-defined base kernel still has no theoretical guidance.
🌐
Kworkflow
kworkflow.org › tutorials › codestyle.html
Check if your code follows kernel code style — kw documentation
The Linux kernel has a very clear and well-defined code style automated by a tool named checkpatch.pl. You can use this script to check a file or a patch, and it has a large number of options.
🌐
LWN.net
lwn.net › Articles › 694755
Coding-style exceptionalism [LWN.net]
July 20, 2016 - Once a little attention was focused on this code, other complaints arose, with Ingo Molnar complaining about the excessive use of parentheses, the unusually long field name partial_block_buffer_length, and, responding to what is clearly a sore spot for some, requesting that the "customary" style be used for multi-line comments. ... The preferred style for long (multi-line) comments is: /* * This is the preferred style for multi-line * comments in the Linux kernel source code.
🌐
Linux Kernel
kernel.org › doc › html › v6.6 › process › 4.Coding.html
4. Getting the code right — The Linux Kernel documentation
A code base as large as the kernel requires some uniformity of code to make it possible for developers to quickly understand any part of it. So there is no longer room for strangely-formatted code. Occasionally, the kernel's coding style will run into conflict with an employer's mandated style.
🌐
Wikipedia
en.wikipedia.org › wiki › Linux_kernel
Linux kernel - Wikipedia
2 weeks ago - Linux is written in a special C ... using inline sections of code written in the assembly language (in GCC's "AT&T-style" syntax) of the target architecture. In September 2021, the GCC version requirement for compiling and building the Linux kernel increased from GCC 4.9 to ...
🌐
Google
android.googlesource.com › kernel › msm › + › android-wear-5.0.2_r0.1 › Documentation › CodingStyle
Documentation/CodingStyle - kernel/msm - Git at Google
Sign in · android/kernel/msm/android-wear-5.0.2_r0.1/./Documentation/CodingStyle · blob: e00b8f0dde52e520d5bcc378aa0bbf4f506a6b23 [file] · Powered by Gitiles| Privacy| Termstxt json
🌐
ResearchGate
researchgate.net › publication › 242104761_Kernel_korner_proper_Linux_kernel_coding_style
(PDF) Kernel korner: proper Linux kernel coding style
January 1, 2002 - indentation that cause the code to shift to the right of the screen too far, then you should fix this. ... The original authors of UNIX placed their braces with the opening brace last on the line, and the closing ... Therefore, the Linux kernel uses this style.