Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

A while back I saw a comment on a Stack Overflow question that surprised me:

No one can write an operating system in C. It is not possible. What one can do is write an operating system in a C-based/C-like language that adds/defines a considerable amount of non-standard capabilities on top of C core.

Is it useful to define C so narrowly? People say Linux, or a GPU driver, or an embedded system was "written in C", not "written in C-with-some-extensions-to-accommodate-the-hardware."

Is it incorrect to do so?

share|improve this question
People on SO just like to be really technical for no good reason, 90% people will just say that linux is written in C – aaronman Aug 27 '13 at 22:23
It is possible to write an OS using "only C" if you use some non-standard compiler features like naked functions, and inline assembly for a few things like hlt. – Daniel Aug 27 '13 at 22:29
People wrote OS's in assembly before C came along (and even after). You can write an OS in anything as long as it emits machine code and you can convince the bios that your OS is actually bootable. Supporting hardware is a bit of a different problem ;) – Matt D Aug 27 '13 at 23:47
I believe it is possible to write an operating system with a series of 8 donkeys that kick you in the head based on what sounds you make from your perhaps-abused mouth. – Erik Reppen Aug 31 '13 at 3:02
@ErikReppen I see no reason to drag Windows ME into this. – GrandmasterB Aug 31 '13 at 3:25

5 Answers 5

If you allow for the possibility of using device drivers (having a C interface) to communicate with the hardware, there's no reason you couldn't write an OS in ANSI C. Anything you might need (like naked pointer) that's not present in ANSI C could be provided by the device driver.

I think the writer of that comment is being pedantic to make a point. He's saying that you can't capture every possible thing that you might need to do in an operating system in the ANSI C language. I'm not sure I agree; ANSI C is a Turing-complete language, and has many low-level constructs including bit twiddling.

In any case, you quoted him out of context. Look at one of the additional comments further down in the thread:'re making it sound more drastic than it is. A small amount of assembly language (inline or in separate files) and a good linker are really all you need on most architectures. Other non-standard stuff that kernels use (like fiddling with output sections, packing, alignment, branch-prediction hints, crazy pointer arithmetic, undefined union behaviour) is for optimisation.

share|improve this answer
My intention isn't to snipe at a particular commenter, but to suss out the boundaries of when one can say "X was written in C" and when you really need to say "X was written in a C-like language." – Crashworks Aug 27 '13 at 22:56
It's English. You were expecting a hard and fast rule? – Robert Harvey Aug 27 '13 at 23:03
Wouldn't you need to be able set specific registers and call IRQs to write drivers? I haven't done device drivers for modern PCI bus stuff. – Martin Beckett Aug 28 '13 at 1:43
+1 for mentioning the drivers and layers. – Apoorv Khurasia Aug 31 '13 at 2:48
@Leushenko: Your comment is both wrong and largely irrelevant. Turing-completeness is a minor point in my answer, but yes, you need a real programming language to do this (HTML is not going to get it done). Yours is the only downvote, BTW. – Robert Harvey Mar 20 at 13:53

The question is quite broad and as it stands the answer is yes. In fact, the answer is yes for most programming languages but there is a catch.

You need to translate your OS code into something that the machine understands. And it is the machine and its native language and your capability to translate from "your" language to the "native" language that would determine if you can write an operating system in your language or not.

General Theory

Let's say a machine has a lever. And only two things are possible -when the lever is pushed down the machine shall pour coffee and when pulled up the machine shall stop pouring coffee. Now imagine my native language translator is a human who can read C code printed on a paper and decide whether to push the lever up or down. So, for example upon reading the following line our human "compiler-linker" will push the lever down.

printf("pour coffee"); //Support more similar syntax in later versions!

And on reading the following line our human "compiler-linker" will pull the lever up.

printf("stop pouring coffee"); //Support more similar syntax in later versions!

We have successfully written an operating system for this coffee machine entirely in C. For that matter, we can train our compiler to translate Java, C#, KSH, Perl, English, Slavik, Sanskrit etc… and "operate" the machine. Hence the term, Operating System.

Of course most modern computers are capable of doing something more complex than this. And hence the complexity of their instruction set goes beyond just a simple lever state. These instruction sets will ultimately just put some transistors/magnets/LEDs etc. in some state and we humans will interpret that as a result.

The problem hence becomes one of manipulating this machine to achieve the states we want. And if your language (e.g., C) can be translated into that native instruction set and if you can use this bridge to make the machine do what you want then you shall have a successful operating system.

Concrete Example

I am running an x86_64 system right now. This system does not know what C is. It does not know what OS X, Windows, or BSD are. It is ultimately running some x86_64 assembly code. That code was not written by humans (for most part). It was probably written by humans in C or C++ and translated into the assembly language. If I copy this compiled code on to my power PC machine it will not work. I need the original C/C++/whatever_language codebase and I need to translate that code to the power PC language. If I am trying something in C that is not translatable to power PC language then that feature will not work on the new operating system.

What about devices?

The more devices you plug into the machine (e.g., a monitor, a mouse, a printer, an audio output etc.) the more code you will have to write for the bridges. That is what the drivers do. You can write a driver in C if you want. All you need is a way to translate C into the "instruction set" of that device. Usually, it is easier to just embed some code/circuit into the device that reads standard electrical signals and trick the core computer into generating those electrical signals using the standard instruction set and device buses (e.g., USB).

What about bootstrapping?

Now of course, the operating system these days "ships" with the machine. And what people do is just install the code [compiled into the instruction set language of the machine] to some place where the machine can find it on its own. The trick is just to bootstrap the machine to find that code when booting it up. A JVM bootstrap example is below:

public final class Hello {
    public static void main(String[] args_) {
        System.out.println("Hello, World!");

0000: cafe babe 0000 002e 001a 0a00 0600 0c09  ................

What about… ?

Please see Tanenbaum or ask another question.

share|improve this answer
Good answer, surprised that it doesn't have more up votes :-) Coffee machine example put a smile on my face. – CodeART Aug 31 '13 at 20:19
Your code example cout << "pour coffee" is C++, not C! – Basile Starynkevitch Oct 15 at 5:15
@BasileStarynkevitch Fixed and thank you. – Apoorv Khurasia Oct 17 at 2:16

Is it incorrect to do so?

It is not "incorrect" to define C that way ... in a particular context.

My intention isn't to snipe at a particular commenter, but to suss out the boundaries of when one can say "X was written in C" and when you really need to say "X was written in a C-like language."

One can say what one likes! There are no IT terminology laws that say what words mean.

The guy is making a (somewhat pedantic) point by equating "C" with the language that is strictly defined by the ANSI C standards. But that's OK. We know what he means.

And the flips-side is that the rest of us can use "C" to mean practical implementations of the C language ... if we want to.

share|improve this answer

I think that whoever wrote that comment doesn't know what he's talking about because the reason behind C's creation was to write UNIX (ONE of the best operating systems of all time) I'm not saying it's the best language to write an operating system with because lots of things have changed since then (and believe me when I say a lot ) and there is now many languages that are better than C when it comes to writing an operating system , but it's far from impossible to write an OS with C.

share|improve this answer

From what I have learned over time in development, C is a middle-level programming language that could be used to write an O.S. (I am currently writing one myself; not giving any details). Just make sure that your boot-loader can be read (that may require something low-level than C; try an Assembler dialect, such as NASM), and can load your entire System. If so, your good to go.

Here's a possibly helpful hyperlink:

share|improve this answer

protected by gnat Apr 2 '14 at 18:13

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.