Skip to content

Using Code

Building

The code that we write in programming languages are designed to be easy to read for humans. However, computers are unable to understand this, instead they only understand what is written in binary. So, we need to convert our code to binary. To do this, we use a compiler. An example of a compiler for the C programming language is gcc. To compile code using this, we write:

Text Only
% cat fun.c
#include <stdio.h>

int main() {
        printf("Wow, coding is fun!\n");
        return 0;
}

% gcc fun.c

% ls
a.out fun.c

Running

To run the binary, we simply call it in the command line:

Text Only
% ./a.out
Wow, coding is fun!

Permissions

Now, this may not have ran for you. This reason for this is permissions. Remember early when we ran ls -l, that there was at the beginning of a line something like -rw-------? This is telling us several things. This first character will be either a - for a file, or a d for directories - so the above string relates to a file. Following the first character are nine characters; three characters for three groups of people:

  1. The owner of the file
  2. Users in the group of the file
  3. Everyone else.

The three groups of characters for each group of people tells us if they can read (r) the file, write (w) to the file, and execute (x) the file. So in the case above, only the owner of the file can read and write to it contents.

In order to run code, we need to be allowed to execute it. To change our permissions we can use the chmod command. To let anybody execute the file a.out, we can run:

Text Only
% chmod a+x a.out
% ls -l a.out
-rwx--x--x 1 Osiris  staff  197  2 Mar  2018 a.out
Extra Information

If you'd like to learn more about the options for the chmod command, you can learn a lot more through its manual using man.

As a sidenote, the command to change the ownership of a file is chown, and again, if you'd like to know more about it, you can use its detailed man page.