All posts by Michael Gao

Array

We have learned the basic data type and we can use that to define any variable, the defined variable will then carry and manage a copy of data of that given type. For example,

int ijk;

 That will define a variable ‘ijk’ of type ‘int’, we can assign any integer to ‘ijk’ any time, ‘ijk’ will then carry that integer from that point on.

Sometime we need to work on more than one instance of data, and they all have the same type. For example, let’s say we design a game that can support up to 50 players, we need to record the score for each player. We could define 50 variables like the following,

int score1, score2, … score50;

That may work,  but has at least two obvious issues, we have to separately operate on each different variable, and it becomes not manageable if we need to support 5000 players.

C language has a data structure defined to handle that,

array: a data structure that can store a fixed number of elements of the same type

 An array can be defined like the following,

<type> array-name [ number-of-elements ];

For example,

int scores[5000];

This defines an array called ‘scores’ that has 5000 elements of integers. Each element of the array can be accessed by its index, starting from zero, for example,

scores[0] is the very first element, and scores[4999] is the last element.

With array, we can easily display all the scores using a loop,

It is very important to note that,

  • array index starts from 0, not 1
  • you can never write beyond the array length, for example, the following will create a serious error and cause the program to crash,

scores[5000] = 123;

An array can be defined initialized or uninitialized, for example, the above scores array is defined uninitialized, we did not give an initial value to each element. We can also give array initial values at definition,

int xyz[5] = {1, 2, 3, 4, 5};

This will define an array of 5 integers, with xyz[0] of value 1, xyz[1] of value 2 and so on.

Array index has to be an integer of value greater than or equal to zero, thus xyz[-1] is incorrect, since there is not element of array that has an index value of -1.

That’s all for Array, with one exception, we haven’t talked about how to pass array around, as a function parameter or return value, which we will learn later on. For now, it is good enough for us to just use array directly …

Function

We briefly introduced function as a building block to C programming, let’s take a closer look at it, as we will use it for almost all the programs we write.

We already know that program is a collection of statements, so why do we need function at all? Or, maybe we need just the main function itself, why do we ever need to write our own function?

We need function for code reusing,

– function is a good way of reusing common code, so we do not need to write the same thing over and over

– function gives us the power to use someone else’s code as well, even if the code is from someone we do not know

– function allows multiple developers to work together, each developer can work on one part of the program

Let’s look at a simple example, the following program prints the last digit of a few numbers,

Basically we do a modular division of 10, then print the value, and we are doing the same thing 4 times. How about we write a function to print the last digit of any number, then just call that function?

The above program can be rewritten the following way,

As introduced before, function takes the following format,

<return_value_type> <function_name> ( parameters) { instructions; }

The printLastDigit function we wrote above takes only one parameter, the ‘number’ we want to print the last digit of. Each time when we call that function, we pass in a different number for the function to print.

In above example, we only print the last digit, once we pass in the number, that is all we need that function to do. We do not expect anything returned from it, that is why the return type is ‘void’.

Let’s change the requirement a little bit, after the last digit is printed, we also return it, so the caller can do something with it. Let’s write the program that prints the last digit of a few numbers, then we sum up all the last digits, and print the last digit of the sum value.

We can modify the above program to the following,

The ‘getAndPrintLastDigit’ function returns the last digit after the printing. At line #11 we initialize the sum as zero, then we add each return value to it, after line #16, we have the sum of all the last digits of the 4 numbers we have above.

At line #18, we print the last digit of sum itself, and ignore the return value, since we do not have any further use of it.

Note a few things in above program,

– when calling the function, we can pass in either a constant value, for example line #13 through #16, or pass in a variable as we do in line #18.

– when the function is called, the parameter will have the value we pass in. For example, the ‘number’ parameter variable will have the value of 3456 when line #15 is calling the function. The ‘number’ parameter variable will have the value of sum when line #18 is calling the function.

– Each time the function is called, the caller must supply the parameter, but can choose to ignore the return value. For example, at line #18, we call the function but do not collect the return value.

If you wonder what the ‘static’ keyword does in above example, it is a way of saying that the function we wrote can only be called from within the same file. So if our program has more than one files, the static function can not be called from any other files. Why we add that limit? we will learn more on that later, but it is enough to know for now that it is a good practice to use ‘static’ keyword for any local function we write.

We now have a new weapon to simplify our code and get more done …

Operators

We’ve learned that programming is essentially either processing data or moving them around, so we ‘operate’ on data. To do flow control, we need to construct conditions, thus we do logic operations too. In C programming, we use operators do all that.

An operator is a symbol that operates on value or variable, or on condition.  For example, we already know that ‘+’ is a operator to perform addition.

C language provides a wide range of operators to support different operation needs. Let’s learn a few of the basic ones here.

Arithmetic Operators

These are operators we use for math.
+ addition
subtraction
* multiplication
/ division
% modular division

Modular division gives us the remainder after the division, for example,

9 % 2 = 1
13 % 10 = 3

The ‘-‘ can also be used on a value or variable to negate it, for example, if x = 5, then -x equals to -5.

‘++’ and ‘–‘  are called increment and decrement operators, they can be applied to a variable. The ‘++’ is equivalent to +1, and ‘–‘ to -1. So if x = 1; then after ++x or x++, x becomes 2. You may wonder what is the difference between ‘x++’ and ‘++x’ then?  We will learn it later on, but that would be something fun for a research.

When we do math, it usually takes the following form,

x = y + z;

Sometime though, we want to operate on the same variable, like the following,

/* add 10 to current value of x and assign the value back to x
x = x + 10;

For that, we can use a simpler form,

x += 10;

Relational Operators

These are operators we use for conditions.

== equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
!= not equal to

They operate on two variables or a variable and a value, we then use the result for decision making, or flow control as we just learned. For example,

if ( a != b) { /* do something here if a is not equal to b */ }

Logical Operators

These are operators for, well, logic, as the title indicates. We use them to combine condition expressions.

&& Logical AND
|| Logical OR
! Logical NOT

For example,

if ( (a > 3) && (a < 10) ) {
        /* do something here only if
             a is greater than 3, and
             a is less than 10
        */
}
if (  ! (a == 3)  ) {
        /* do something here only if NOT a == 3,
              so this is a different way of saying (a != 3)
        */
}

Note that it is always a good habit to use parenthesis around the conditions that we do logic operations on, to make the logic obvious.

There are other operators, which we will learn later, for now these should be sufficient for us to do some interesting math.

Next, let’s look at how we write our code to make that interesting math more manageable …

Flow Control

We’ve learned that the instructions are executed one after the other from the beginning to the end within a function. Quite often though, we actually need more than that. For example, we might need to do certain things only under certain conditions, or we need to do certain things more than one time before continuing with something else. We basically need to control the flow of our program, or in other words, making decisions based on conditions.

C language provides a few different flow control or decision making methods. Before explaining them, let’s first introduce the ‘block’ and ‘condition’.

block: a group of instructions that are enclosed in a pair of curly braces. Instructions in a block are executed together, variable definition within a block is also allowed, which we’ll learn later.

Any number of instructions can be grouped together  by having ‘{‘ and ‘}’ around them, anywhere. But normally it only makes sense to group instructions together for given conditions. For example, if this condition is true, then do these block of instructions.

A block looks like the following,

condition: a test expression that is evaluated to either true or false. For example, ‘3 > 1′ is a true condition, and ‘2 == 3′ is false one (note the double ‘=’ sign, since a single ‘=’ is used for value assignment). It is not very useful to have constant condition like that though, usually the test expression has variable in it, so we test whether the variable value meets the condition or not at our program run time. For example, ‘x > 2′ will be true only if the value of variable x is greater than 2.

 With that, here comes the flow control methods …

1. if … else

if ( conditions ) blockA else blockB

if ‘conditions’ are true, then blockA will be executed, otherwise instructions in blockB will be executed, for example,

The else part is optional, for example,

if … else can be concatenated and cascaded, to however many levels needed, for example,

As shown above, the position of the ‘{‘ or ‘}’ does not really matter, but it is important to make sure they always come as a pair.

2. switch … case

switch ( variable or expression ) {case ( constant_1 ) : instructions; break; case ( constant_2 ) : instructions; break; default : instructions; break; }

Depending on the value of ‘variable or expression’, only the instructions under the matching ‘case’ are executed.

The ‘break‘ instruction ends the entire switch block. Without that, instructions from the following ‘case’ will be executed, till it hits a ‘break‘ statement.

For example,

 3. goto

goto label;

Any time when there is a ‘goto label’ statement, program will unconditionally go to the instruction pointed to by ‘label’. For example,

Note that in above code, the instruction between the ‘goto’ statement and label ‘next_st’ will never get executed. Thus ‘goto’ is usually paired with ‘if‘ or other conditional statement.

Please keep in mind that we should always try to minimize of the use of ‘goto’ statement, as it breaks the normal flow of the program and makes it hard to follow and understand the program.

4. while … do

This comes in two forms,

while ( conditions block

do block while ( conditions );

The first form executes the block while conditions are true, the next block first executes the block once, then continue to execute block while conditions are true. For example,

or,

Exercise: are the output from above two different  from each other? what if we change line #1 to ‘x = 5’?

5. for loop

for ( initial instructions; conditions; update instructions ) block

The initial instructions (separated by ,) are executed once and once only at the beginning of loop.

Then the conditions are tested, if false, the loop is terminated, otherwise the the block of instructions are executed.

Then the update instructions are executed.

The above repeats until the loop terminates, for example,

 

Note that ‘ii++’ is the same as ‘ii = ii + 1′.

It is ok to have all fields of for loop empty, in which case it becomes an infinite loop. In real programming though, you probably want some condition to terminate the loop. For example,

 6. break … continue

Within while or for loop, ‘break‘ statement will immediately terminate the loop, ‘continue‘ statement will immediately skip the rest of the instructions in the loop and jumps to the beginning of the loop. For example,

Exercise: how many times the “I am here” is printed above?

We learned all the basic flow controls above, there’s one last statement, ‘return‘.

return: immediately returns from and terminates function.

The return statement can be anywhere in the function, after that instruction, the entire function terminates and the program flow goes back to the caller of the function. If we return from the ‘main’ function, the entire program terminates.

So we ‘return’ here …

Data Types and Variable

When programming, we always work with data, either processing them by doing math, or moving them around. Most of the time programming job requires that we do both.

For example, if we write a pong game, we first need to ‘draw’ a ball, imagine that a ball is made of lots of small dots (we usually can them pixels), we’ll need some math to determine where each dot needs to be drawn at. Then we want to move the ball around,  we’ll probably just move the pixel data around.

So what is data?

In the simplest form, let’s think data as ‘number’.

Even for just number, there are different ‘types’ of them. For example, integer, fractional, there is also the difference between positive and negative.

The data needs to be stored somewhere, thus the size of the data matters. If we know the number is going to be small, we do not want to reserve large storage space for it. On the other hand, if the number can grow to be really big, we need to make sure we have enough space for it. For example, in the pong game above, we probably only need a small number to record the level the player is at, but would need a big number to record player’s current score.

To manage all different types of data, C language defines data types, some of the basic types are,

  • char: character, range from -128 to 127
  • short: short integer, range from -32768 to 32767
  • int: integer, range from -32768 to 32767, or -2,147,483,648 to 2,147,483,647 (we call this platform dependent, which we’ll learn later, for now it is safe to assume int has a range of the latter)
  • long: long integer, range from -2,147,483,648 to 2,147,483,647

Each of the above integer types also has a corresponding ‘unsigned’ type, thus,

  • unsigned char: from 0 to 256
  • unsigned short: from 0 to 65536
  • unsigned int: from 0 to  4, 294,967,296
  • unsigned long: from 0 to  4, 294,967,296

For fractional (or floating point) number,

  • float: 6 decimal places
  • double: 16 decimal places
  • long double:19 decimal places

I’ll leave the actual range of these types for your research, at this point it is enough to know that these are good for fractional math, such as 3.14 * 0.96.

There is also a special type called ‘void’, which we’ll learn more about it later on.

  • void: special type to indicate no data available.

As mentioned above, data needs to be stored somewhere, we call that storage ‘variable’,

  • variable: the name to the storage place that holds the data

To define a ‘variable’, we use the following syntax,

<type> <variable name>

Once the variable is defined, we can then store data there, please keep in mind that the data stored must match the variable type.

Here’s a simple data type example,

A few things to note,

  1. variable can only be accessed after they are defined.
  2. variable can be used to store data, and storing data must be on the right hand. In other words, the following would be incorrect,
    234567 = iy;
  3. variable only holds one copy of data, old data gets lost when new data is stored. For example, add the following in line #12, and see what happens, ix = 999;
  4. printf can print out formatted data, “%d” and “%f” above are format specifier. Research yourself to see what more printf can do.
  5. note line #4 and line #8, they are there to help us and others to understand what the program does. They’ll be ignored by the compiler. It is always a good habit to write comments when programming. C language comments can be wrapped around by /* and */.

So much for the data types and variables, let’s move on …

Introduction

As we’ve learned, when programming, we basically provide a collection of instructions for computer to follow and execute. The programming language is designed to make those instructions easier to understand and remember, for us programmers, and provide flexibility to do many different things, also in different ways. Most importantly, programming language is designed to be consistent, once the program is written, there will never be ambiguity in how the program will behave. This is unlike the language we speak or write in, the same combination of words may mean totally different thing, depending on who says them when, and sometime in what tone.

C language is no exception, it has strict grammar that we must follow. Let’s look at the very first hello program we wrote,

C program instructions is organized in block called ‘function’,

function: is an execution block, the instructions in the function will be executed from start to end.

Function takes the following format,

<return_value_type> <function_name> ( parameters) { instructions; }

The <return_value> is something this function returns once it completes all its instructions, we’ll learn more on this later.

On line #3 above,  we can see that the return value is ‘int’, and the function name is ‘main’. Also this function takes two parameters, enclosed in a parenthesis ‘()’. we’ll explain what parameter means later.

Function then starts with a ‘{‘, with instructions in the middle, and ends with a ‘}’. At the end of each instruction, there must be a ‘;’.

White space or tab will be ignored, and can be used to format the code so it looks neat and easier to read. There are cases where space and tab are significant, we’ll learn all that later.

Within a function, we can call other functions, for example, we call the ‘printf’ function in line #4.

When programming we write our own functions, and also call to functions that are written by someone else, from a library. For the latter, we’ll need to include the library ‘header’.  In line #1, we include the standard I/O library header, since we use ‘printf’, a function from that library.

For the function we write, we can give it any name we like, as long as the name is allowed by C language grammar, for example, function name has to start with an alphabetical letter or _, and can not have space in between etc. There’s a few exceptions,

  • some name is reserved by C language, for example, you can not name a function as ‘int’
  • the same name can only be used once in one file

And there is a requirement for the very first function, it must be named as ‘main’, since this will be the main entry point to the program. No matter how big our program grows to, it always starts from this ‘main’ function.

Here’s another simple program, this time we write a new function to print out more info,

In above example, the ‘hey’ function takes no parameters, thus it has an empty ‘()’, and it has no return value either, thus the ‘void’ return type.

So ‘main’ function calls our ‘hey’ function twice, one after the other other, and in between we print a separator line.

Next, let’s look at more language basics …

 

The Tools and Libraries

Before we start learning C language, let’s first take a look at the tools needed. We already know that C language is a ‘Compile to Run’ language, after we write our program, or rather, source code for the program, we need a ‘compiler’ to compile and turn the source to runnable program. On Linux, we’ll be using gcc, GNU Compiler Collection. gcc does more than code compiling, but it is good enough for now to just remember that gcc is the magic tool to turn our C source code to real program.

For example, if we already write our program source in a file called “hello.c”, we can use gcc to to ‘compile’ it,

gcc hello.c -o hey

This will create a runnable program called ‘hey’. If ‘hey’ has problems, we can edit the ‘hello.c’ file then run “gcc hello.c -o hey” to generate a newer version of ‘hey’ program.

Sounds straight forward?

There’s more.

Invoking gcc manually each time like the above is ok, if our program is simple and only has one or a few source files. Imagine a complex program has thousands of different source files, we’d need some other tools to manage the way ‘gcc’ is called. One of the most popular tools is called ‘make’. We’ll not learn ‘make’ here, but this is something to keep in mind. Once our program grows beyond a few files, we’ll need tools like ‘make’ to manage how ‘gcc’ is called. For even more complex program, there are tools built on top of ‘make’ to further simplify the ‘compile’ process, such as automake and cmake.

Other than ‘gcc’, there’s also the library part.

Programming language is designed so that we can program to give working instructions to CPU. However, it will be a problem if every programmer needs to write everything from scratch. For example, in order to create a file on disk, we need to be able to access the disk itself, find the available directory, open a file, write to it, and save, then close. Each programmer probably would write the same thing over and over to handle the disk access and file open/write/save/close part. Ideally the code can be shared so once it is written by one programmer, the other programmers can just reuse it.

One of the mechanism for reusable code is called ‘Library’. Think ‘library’ is a collection of shared code, that can be used by any programmers.

‘gcc’ plus ‘C library’, this will give us the base to C programming. To put it all together, when we program, we write our program following C language grammar, and using shared code from library.

Ok, enough talking, let’s see how to use ‘gcc’ and how to use code from library, open the editor, I am impressed and pleased to see that Frank actually learned to use emacs already! use your favourite editor to create a file named “hello.c” with the following lines,

Now, type the following,

gcc hello.c -o hey

If you see something printed on your terminal, something went wrong, double check to make sure there’s no typo. If nothing happens, that’s good. Remember that “no news is good news”? that means our compiling succeeded. You now should see a file named ‘hey’ is generated. ‘ls -l hey’ even shows that file is runnable!

Let’s run it,

./hey

You see ‘hello’ printed on terminal, it works! We just compiled our first C program!

We used the ‘gcc’ tool, but where is the library?

In this simple example, we only used the standard input/output library, for ‘printf’ support, that’s what the “include <stdio.h>” is for.  By including that, ‘gcc’ automatically finds shared code from that standard library. There is a lot more to learn on how to work with libraries, once we learned the language. For now, it is good enough to know that including <stdio.h> is sufficient to bring in the standard shared code.

Now, we learned all the programming basics, we finally are ready to learn the programming language itself …

The Language

It was exciting to see our first ‘hello’ program running, that wasn’t very hard either, programming seems quite easy after all.

Maybe we can write a game with that technique?

The answer is yes and no. Yes, we can write a simple game with what we learned so far. No, it can’t be a fancy game if we use the programming language that basically does what we can do in a terminal.

We already learned that in order for computer to understand our program, we need to use certain language. Computer can understand many different type of languages, we need to pick one for ourselves.

So what options do we have?

There are many programming langues available, it is nearly impossible to master all of them. The good news is that most programming languages are conceptually the same, if you master one, it will be easy to learn the next one.

We already learned that CPU understands certain instructions, various programming languages are designed to supply CPU those ‘instructions’, as it would otherwise be difficult to directly work with CPU instructions. In order to ease the talking to CPU, there are two different approaches,

  • Write to Run Language

With this type of programming languages, we can write the program to a file, then give the file executable permission, the file becomes a runnable program!

This is what we did with ‘hello’ program.

There are many different types of languages that work like this, the one ‘hello’ program is written with is called ‘shell script’. Shell script can do complicated things, but basically it is just a way of grouping commands that we can type on a terminal. It is a good choice when we want to automate things.

Some of the languages in this group can do fancier stuff, like drawing on screen, plot curves etc. ‘Python’ or ‘Perl’ are both good and powerful ones.

  • Compile to Run Language

With this type of programming languages, the files we write are called source files. But source files themselves can not run. We have to use some special tools to ‘compile’ them and generate different files as runnable program. C, C++, Java are popular ones in this group.

What is the difference between the two? why the “compile to run” one even exists? why we add the extra ‘compile’ step?

There can be many differences, depending on what languages we compare between. The primary difference is performance, the ‘write to run’ ones rely on certain interpreter to understand the instructions in the program and run them. The interpreter itself is a program, which runs on CPU. So basically the interpreter is a middle man between the program and the CPU. The ‘compile to run’ ones though, removes the middle man. The ‘compile’ step will turn the program into instructions that can be understood and run directly by CPU.

Ok, the above is an overly simplified description. Java, for example, even after been compiled, it does not directly run on CPU, instead it runs on some virtual machine. We’ll learn all that when we get to learn Java programming. Some languages may not fall into any of the above groups. But it is good for now to remember these two, and their primary differences.

Programming languages are designed to handle different type of tasks. For example, when trying to automate some commands that we have to type on terminal over and over, we may choose shell script.  If we need to directly work with hardware, we may use C language.

Programming languages are also designed to be ‘portable’. For example, if we need to write program that can run on more than one operating system (recall that our program has to run in certain OS?), we might choose a language that can ‘program once, run everywhere’. Java would be a good fit.

So what we’ll be learning here? we’ll start with the very basic, yet powerful one, C language

Run the Program

Let’s do some forward thinking here, if we already cretaed our program, how do we run it?

Although we haven’t even learned how to write the program, let’s get us familiar with how we will run it in Linux environment. We know program is a file or a group of files, we also know that obviously not every file is runnable. For example, you can not take a document file and run it, although you can open it by running some other program.

In Linux, to make a file runnable, we give that file a special permission, an executable permission. By running the following command,

chmod a+x file-name

There’s a lot to explain here, but for now let’s just understand that by running the above command, we change the file mode to make it executable, chmod stands for ‘change mode’.

Running the above command to any file will make the file itself executable, but does not turn an arbitrary file to a program, obviously.

It is important though to remember that, if a valid program does not have the executable permission, or in other words not in executable mode, the program is not runnable!

How can we tell if a file is runnable or not?

We can list it in long format, by running ‘ls -l file-name’, and look for ‘x’ in the first column,

-rwxrwxr-x 1 frank frank 0 May 30 22:29 my-program

If we see ‘x’ there, the file is runnable, and likely you can run it. Again, there’s a lot more information encoded there, ‘r’ stands for ‘readable’, and ‘w’ stands for ‘writable’. Why there are more than one ‘x’ and ‘rw’? These have something to do with file permission management, which is something we’ll learn later.

For now, just remember, we must ‘chmod’ a file to add the ‘x’ permission to make it runnable, that’s what the ‘chmod a+x file-name’ for.

If a file is runnable, we can simply type the file name with its path to run it. For example, if the file is at /home/frank/my-programs/hello, the following will run it,

/home/frank/my-programs/hello

Or if we are already at /home/frank, the following will run it,

my-programs/hello

Or if we are already at /home/frank/my-programs, the following will run it,

hello

I lied.

The last one will not work, we have to type file name with its path, if we are already at where the file sits, we still need to type the path, which is current directory. Remember ‘..’ stands for parent directory, ‘.’ stands for current directory.

So, if we are already at /home/frank/my-programs, the following will run it,

./hello

Why? when running something without the path, Linux will try to search that something from a given set of directories, not including current directory. Too complicated? no worries, it is enough for now to remember that we always need to have some path to the file, in order to run it.

Let’s write a simple ‘hello’ program, and play with the executable permission.

Open the file editor, and add the following to the file with name as ‘hello’,

# this is my first program
echo "hello, I am here"
pwd

Save the file to your program directory, for example, the ‘my-programs’ directory we created in last lesson.

Now we have a file named ‘hello’ under /home/frank/my-programs, let’s run it,

./hello

Oops, we have the following error,

bash: ./hello: Permission denied

Let’s give it executable permission,

chmod a+x hello

Run it again,  wow, our first program now works!

hello, I am here
/home/frank/my-programs

Congratulations, we just wrote and run our very first program!

Finally, we are ready to learn programming …

Create New File

Writing program creates new file, we call file that has our program ‘source file’, because it is the ‘source’ of the program.

Before that though, we first need to create a directory to hold all our source files. By having a directory, it will help us find them later easier. So where do we create this directory?

Linux operating system can have multiple users, each user can have his own ‘home’ directory. A ‘home’ directory is where user can create new file. When a different user is using the computer, he’ll get to his own home directory. This way everyone can have his own stuff in a private area, so think ‘home’ directory as your private school locker.

Where is my home directory?

Open a terminal (CTRL + ALT + T), type ‘cd‘ without anything else. Now you are at your home directory.

How do we make sure we indeed are at home, in other words, how do we know where exactly we are? If we are in the middle of a huge tree,  we can easily get lost otherwise.

We’ll learn another useful command here,

  • command: pwd

‘pwd’, Print current Working Directory, tells us where we are in the directory tree.

So type ‘cd’ then ‘pwd’ tells us our home directory is ‘/home/frank’. Remember this is called directory path, the path to our home directory? Let’s explain what this path means,

The first ‘/’ denotes the root directory, this is the root of the tree, and every path starts here. To experiment, the following command will always bring us to the root of directory,

cd  /

The ‘home’ is a subdirectory under root, this is the directory that holds each user’s home directory.

The next ‘/’ indicates there’s more within current directory. Or, this is the separator of multiple sub-directories in any path.

The ‘frank’ is a subdirectory, and this is user Frank’s home directory.

We are home, let’s create a directory called ‘my-programs’, note that I used ‘-‘ instead of space between words. Space is allowed in directory name, but usually using space in directory or file name in Linux/Unix system is a very bad idea. Why? if you try “cd /home/frank/my program/fun games”, you won’t reach the target “fun games” directory, instead it will complain that ‘my’ directory is unable to be found, since anything after the space will not be treated as part of the ‘cd’ command. There’s way to work-around this issue, but we’d rather not to get to this trouble.

In order to create the directory, let’s learn another command,

  • command: mkdir

‘mkdir’, MaKe DIRectory, makes a directory.

Make sure we are at home directory (simply type ‘cd’), then type ‘mkdir my-programs‘, now we have our directory for the great programs we are going to write!

Let’s move on to create our program file …

For our program, we not only need to create it, we need to edit it, probably many times. We need a file editor, what editor shall we use?

Word? Power Point?

Those are all editors, but not the right ones for programming. There are actually many options, emacs can be a good one, however it may take you some time to learn it, although you’ll love it once you learn it. Let’s start with a much easier one, ‘gedit’,

On your terminal, while you are at ‘/home/frank/my-programs’ directory, type ‘gedit hello‘, you’ll see the editor shows up and now you can type whatever. Once you are done editing, click ‘save’, you’ll see ‘hello’ file created in ‘my-programs’ directory.

Great, you just created your first program file under your own directory. Well, this file is not yet a program, but we now are much closer …