Monday 17 February 2014

C Programing - Introductory to Linux/Unix terminal, file editing and using a C compiler!

Let's see if we can figure out how to write a C program that produces a box made with "X" characters and with a hollowed centre. The size will be defined and you will be able to change it.
This example will be done using a linux command prompt, but should work the exact same with your Mac's terminal.

To do this I have connected to my Raspberry Pi which is a small computer running a Linux distribution known as Raspbian. I've connected to it using a Windows program called Tera Term. You don't really need to know much about any of those things for today, but I will be hopefully discussing them later on in other posts. If you know how to open a terminal window on your Mac or Linux computer, you should be good to go. If you're running Windows, my method won't work, but the code will work if you're using a different compiler for C in Windows.

At this point I'm going to assume you have your terminal window open, but don't know how to use it, so I'll walk step-by-step through what to do.
When you first open your terminal you should be in your home directory.

Let's make a directory called "cStuff" so you have somewhere for your projects. type in: mkdir cStuff - you must press the return/enter key after each command. You can name it whatever you want, but you should learn about naming conventions and remember that you need to type the name to access the directory, so don't get carried away.

You can now move into that directory, type: cd cStuff.
We can now make a new folder for the square we are going to make today. Go ahead and type in: mkdir square. now move into that directory by typing: cd square
You should now be in the directory we will be using to make the square.

In the window above you can see that my prompt has the current directory listed in the prompt, like this:
jarret@raspberrypi:~/cStuff/square $

If you're interested in the full path you can type in "pwd" - present working directory to see where you are.

To create the file we will use to do the work in, we can use the "touch" command. This command will create a file with the name you specify. We could also just open a text editor, and save our work with whatever file name we want, but if we do it this way, the editor will know what type of file we are working on, and will use a profile to colour code some of our C statements for us.

typing in "touch square.c" will create the file square.c. Go ahead and do that. With the ".c" file extension, our editor will know we are programming in C!
To see the file we can type in "ls" to see the contents of the directory.  If you do this now, the only file you should see is square.c

let's get down to business. To do the actual C programming we're going to use an editor called nano. It should already be installed on your system, as it is a very popular command line editor.

To tell nano you want to open square.c you can simply type in "nano square.c". When it opens up, you should not see anything in the file, as it is empty, but you will see at the top of the screen that you're working on File: square.


You can see the colour coding it uses in the above picture for different keywords, this will happen while you're writing your code. The software I wrote is very basic, and should be very short for programming drills like the one in this example.

You can use any text editor, even notepad to do this work. I recommend looking into gedit, or notepad++ if you will be writing software often. Using nano at the command line is good practice and I recommend programmers play with it a little to get the feel. You can also use it for configuring and editing system files and viewing logs.

Here is the cut & paste version of my code if you want to bypass the exercise and play more with the command prompt and the method we are using:

#include <stdio.h>

#define boxSize 9

int main ()
{
        unsigned char counter, i;

        for (counter =  0; counter < boxSize; counter++){
                if ((counter < 2) || (counter >= boxSize - 2)){
                        for (i = 0; i < boxSize; i++){
                                printf ("X");
                        }
                        printf ("\n");
                }
                else {
                        printf("XX");
                        for (i = 0; i < boxSize - 4; i++){
                                printf(" ");
                        }
                        printf("XX\n");
                }
        }
}


I do recommend you attempt to solve this on your own, or try different exercises such as making diamonds or pyramids....

You can also change the value assigned to boxSize in the second line of the program to change the size of the square on the output.

Create your program or paste this code into the file.
At the bottom of the screen while you're in nano, you will see various options you can select.
^X Exit is what we will use to save and exit. "^X" simply means press ctrl+X to quit. Do that when you are finished.


press "y" to save your changes. It will then confirm the file name, you can press enter to save, and ignore the other options it presents for today.

We now need to compile our program. This is done at the command line using gcc. This is a program that will convert our square.c file into a program that the computer can execute. This program should already be on your computer, You can check by typing: "gcc -v" which will also tell you the version. If it doesn't work, try typing: "sudo apt-get install gcc" It will ask you for a password since you're changing the files on your computer. You can use this command even if you have gcc already installed to ensure you have the latest version.

The easiest way to get it to output a file is to use the command like this:

gcc square.c -o square

The first part is telling the prompt what program we want to use (gcc), followed by what file we want it to use (square.c) and -o is telling it we want to output to a file, and (square) is the name we want the program to use for the output.

If everything goes well, it won't say anything, you will just have a command prompt. If it does find any problems while compiling the code, it won't generate the file, and it will give you some clue as to the problem. If this is the case, open up the file again: nano square.c

Make sure you have all the brackets correct and that you have no typos, then save and try the compiler again.

If you use the "ls" command now, you should see the directory contains both "square.c" and "square".

To run your program, use the command:
./square

Depending on how things are set up, it may require you to use the password associated with your username.

If you're happy with everything, you can type in: "exit" to end your terminal session. It's considered bad practice to click the x in the corner to close this window (There are reasons, but another day).

I would like to see if anyone can write a shorter program to do the same thing as the one I wrote. I intentionally kept it short and sweet. Please give me feed back on this post and let me know if something needs to be cleared up, or if I'm wrong about anything. It was really meant as a C crash course for Linux/Unix users.

We will look at the code later too!