Compiling In C And Japanese

An example of compiling from someone who has never compiled before. Also, a bit of Japanese.

Rick Nilon
6 min readAug 25, 2018

I’ve recently begun to dive deeper into computer science subjects and have been learning a bit of C in my spare time. Because my programming background is in JavaScript and Ruby, I’ve come to realize how different these languages are from C; C is a completely different animal in terms of how you write your programs. One of the biggest differences I’ve discovered so far is how you run your program. With Ruby and JavaScript, you can just enter into a REPL like IRB for quick code execution. But with C, you need to compile your code before being able to run your program. So I am going to go through the basics of what compiling is and the difference between how languages like JavaScript and Ruby are run in comparison to a language like C, and also the basic C I’ve learned so far.

Interpreted Languages

A lot of modern languages, like JavaScript, Ruby, Python to name a few, are interpreted languages. This means that you can just run the program right away, without an extra step called compiling. Compiling is, essentially, an extra step you need to do to get your program to finally run and be understood by the computer. More on this in a minute.

Interpreted languages are read, and then translated to machine code by a program called an interpreter at run time. Machine code is the language that a computer can actually read to do the things you want it to do. Different programs can be interpreted differently, but they all share the same trait of being able to be run right away. For example, Ruby and Python are translated into an intermediate representation, which is a representation of the program between the code you’ve written and a target language, and then immediately executed. JavaScript is kind of a mixture of compiled and interpreted as it depends on where you run it (for example, it is interpreted in the browser).

Compiled Languages

In order to be run, a compiled language needs to be compiled first by a compiler. A compiler is a program that translates your code into the machine code your computer can understand, and compiling is this process of translation. So both interpreted languages and compiled languages are translated to machine code at some point. But compiled languages, unlike interpreted languages, can not be run right away. You need to run the compiled version of your code. Some of the most common compiled languages are Java, C, and C++.

I’ll give you an example of how this works.

I first create a directory called random-C-project in MyProjects directory. Then I create a file with touch called my-proj.c.

Making things, touching things.

In my text editor (I’m using Vim here), I create a very simple C program, which outputs something stupid in Japanese (日本語!). There are actually a couple of weird ‘mistakes’ that I’ve added so you can see how the compiler handles these. The 5th line is missing a semicolon, and the 9th line doesn’t have the variable that we add to the string in our printf function.

わかりません。

I’ve also created a Makefile, which is a file that automates tasks for you, such as our subject today of compiling.

Letting other programs do things for me.

The first line in the Makefile tells the compiler to report all of the warnings it comes across when compiling. A warning in C is different than an error, although you can treat them similarly when writing your code because they point out mistakes you may have made. Essentially, you can’t compile your program if there are errors, but you can compile and run your program even if there are warnings. You program might just crash or do something very strange at run time.

The third line is just an automated task for removing the compiled version of our project Note the absence of the file format .c ending, because we are only removing the compiled version, not the program we’ve written. (When writing this, accidentally had it remove the actual c program with the .c ending, so after I ran make clean I had to rewrite my entire program from scratch…).

Lets see what happens when we try to compile this.

Breaking things.

Uh oh. We have one error with the missing semicolon and one warning saying that we have nothing to add to the printf function from line 9 (or more correctly, the computer was expecting more conversions than we gave it to convert from variables). As a result of the error, our program did not compile and we have nothing to run. Let’s fix our error, but not the warning, and add a semi-colon on line 5 so we can run our program with a warning and see the output.

After fixing this error and compiling, I now get two warnings about the variable. One is the original warning about more conversions than data arguments. The compiler is also telling me that I’m not using this variable that I’ve created.

The compiler warning me about doing something stupid.

Let’s run this broken program.

XW??? わかりません…..

Woah, it added something weird in for me! XW??? WTF?!?(Apparently this is random data from my computers memory that the compiler automatically adds in the absence of actual passed in data). That’s not what we want. Let’s now add our variable to the printf function so it outputs what I want it to say.

成功!(Seikō) => Success!

Ah! That’s better!

Much different and more verbose than JavaScript to get essentially the same product.

Summary

There are more steps in writing a compiled language vs an interpreted language. This extra step can also point out a few errors or mistakes in your code before running, which in turn forces you to edit your program and adds more time before you can run your program. This extra step requires you to think more about the code you write before you write it, which is an important skill that I personally didn’t develop while writing in Ruby and JavaScript, both of which just allow you to write and run. Oh, and if you are wondering what my program says, it is this.

私はC言語を理解していません

I don’t understand the C language.

私はルビーがより好きです

I like Ruby better.

Warning: There may be some grammatical mistakes in my Japanese (I am a novice Japanese student).

Oh, another side note. Ruby is actually written in C.

Third side note. This is what the executable file looks like in vim. This is the code the computer understands.

--

--

Rick Nilon

Full Stack Developer, Programmer, Musician, Blogger!