Sunday, 25 August 2013

Enter the Dragon - Running your first program

Before we run our first program lets make sure you have Linux installed on your PC.
If not the check our installation guide.
  1. Installing Ubuntu with Windows.
  2. Installing Ubuntu on a Virtual Machine.
*If you install Linux on a Virtual Machine you won't have to restart your PC to switch between Windows and Linux

Since you will be using Linux for the first time, I made sure there were a lot of screenshots to guide you.

For first time you  run a C program on Linux (Ubuntu) you will have to:

  1. Create a directory and the (program) file.
  2. Locate it on the Terminal.
  3. Run the C program on the Terminal. 

Create a directory and the (program) file.

Step 1:

Open the Home directory using the file explorer.

Step 2:

Create a new directory, name it MyPrograms. To create a directory simply Right Click and select New Directory.

Step 3:

Create a new file(Empty Document) in MyPrograms directory. To create a file, RightClick, followed by New Document and select Empty Document. Name it FirstProgram.c

*Make sure you don't leave a blank space in the directory name or file name.

Step 4:

Now open the file and copy the code below.


int main(){
 printf("Congratulations,\nYou successfully ran this piece of code !!!\n");

 return 1;

Congratulations, you created your first C program.

Locate it on the Terminal

Now that the first part of creating the program is done, lets locate it on the Terminal.
A terminal is like the command prompt in Windows.

Step 1:

You can either open the Terminal by searching for it in the search utility, or simply use Ctrl+Shift+T to open the terminal.

This is how the Terminal in Ubuntu looks like.
Better than the rusty Command prompt in Windows right?
The text that appears on the left side is called the shell prompt. It displays your user name and the current directory you are on.

In the pic below ryan is the username and ~ is the present directory.  ~ is an alias for your home directory.

Step 2:

To locate the file on the Terminal open the properties of the file you just created (FirstProgram.c).
Copy the Location.

Step 3:

What u do next will make you feel like a pro programmer.
Use the pwd command to display the directory you are currently using. PWD stands for Present Working Directory

The ls command will display the contents of the directory. LS is an abbreviation for Listing.

You can see the directory you created. MyPrograms.
If you cannot see your directory, it means you are in the wrong directory and you need to enter the following command:
cd <The file location you copied>

If you were able to see the directory then enter the following command:

cd MyPrograms

*Notice the current directory has changed from ~ to MyPrograms.

Step 5:

All that's left to do is locate your file. Use the ls command to list the contents of MyDirectory. It will
display your file FirstProgram.c

Good job, you located your program in the Terminal.

Run the C Program on the Terminal

Just a few steps until you run the program. I know it wasn't easy, but don't give up. You only need to learn this once, it wont be as difficult the next time.

Step 1:

This is the command you need to enter to run your program.

gcc <Your Program Name>

You will notice that no output is printed, instead executing the ls command will show a new file called a.out is created. 
Don't worry, your program is converted into a binary code, a format that your computer understands. If this file gets created you can say your code is compiled.

Step 2:

Now that your code is compiled its time to run it. Simply execute this command


Congratulations, you didn't give up and successfully ran the program. A great achievement indeed.

More Information:

If you don't like the name a.out run the gcc command with the -o option. This will make the command 
gcc <Your Program Name> -o <output file name>th>

And use the ./ prefix  with the output file name to run it.

No comments:

Post a Comment