Original question from Quora:

How do computers understand C?

My Answer:

Computers don’t. C is a magic trick…

The entire purpose of C is to trick humans into feeding computers instructions in a way that people will use.

Under the hood, computers only understand machine code instructions. That is to say, the processor reads some memory, it has some binary encoded numbers that correspond to instructions that the processor can use.

People generally can’t understand machine language at all because it’s like looking at 1283a820fk2f0d0293fa938210b0383820748302ab and making sense of it.

To make sense of that kind of binary encoding we have Assembly language which is a fairly/somewhat direct mapping of processor instructions into something that looks more like…

mov ax, 3
sub bx, 12
int 13h

Which still doesn’t make much sense until you understand the processor registers, interrupts, and built in functions.

Also, every type of processor has a different kind of Assembly instruction set it uses. That means if you write Assembly for a intel processor on your laptop, it won’t work in the ARM processor in your iPhone. They are completely different instruction sets.

For a long time people would write programs in Assembly because it often only needed to work for one kind of machine. Even until the early 2000’s it wasn’t unheard of to have a program written in Assembly, even if that was pretty impractical.

And that brings us to C.

C is a language that sits say one level higher than Assembly. It is still very low level, and in fact you can write Assembly inline in your C code if you want. But, C looks a lot more like a modern programming language than a list of processor instructions does.

However, C and Assembly both end up being compiled/assembled into machine code.

The biggest difference between the two (aside form aesthetics), is your C compiler is effectively going to generate a lot of Assembly code under the hood for you, whereas straight Assembly gives you pretty much total control down to very fine details of what the processor is doing.

So, a good way to think of this is….

Computers understand machine code, Assembly translates to machine code, and C sort of translates to Assembly.

Computer <- Machine Code <- Assembly <- C

Now, it’s a bit more complicated than that because a modern C compiler will probably go straight from C to machine code instead of doing much of any translation to assembly as a middle step.

Oh and I’ll give you a bonus tidbit of information while you are here.

In case you are curious, languages like Ruby, Python, PHP, Lua, and others sit on top of C and are often being run inside of C programs. So, for many things C is still a critical part of the modern programming environment even if most programmers don’t interact with C directly.


P.S. Have you subscribed to Code Career Genius yet?