Saturday, May 28, 2011

Chapter[1]: The First Program

Welcome to the world of programming!

Lets start with the simplest of programs in C, the hello world program. Its actually a kind of a tradition to start off with when learning a new programming language. You won't be an exception either.

I will use codeblocks to run all the codes in this book. You are free to use any other program you are comfortable with. Usually the basic steps are quite similar.

So how do you install Codeblocks? You can also download the software as instructed in Chapter[0]. I am assuming you are on Windows PC and have the codeblocks installer file. Double click the installer and follow screenshots below.

Fig 1.1

Fig 1.2

Fig 1.3

Fig. 1.4
Fig. 1.5
Click 'No' here (Fig 1.5).
Fig 1.6

'Finish' finishes the installation. To start codeblocks, click on the Start menu, choose Programs and find an entry named 'Codeblocks'. If you are in Ubuntu, you will find it in Applications > Programming menu.

Fig 1.7
If you don't want to see the 'Tip of the day' windows every time you start codeblocks, remove the check from the check-box labelled 'Show tips at startup'.

As the first step of beginning to code, you should create a file where you will save your code. To create a new file, click on the File menu > New File > Empty file. You should reach the following screen (Fig 1.8).

Fig 1.8
We are almost done. To keep your programs organized, create a folder on your hard-drive. Save all your codes in this folder during the practice. To save the empty file you just created, click on File > Save. From Save as type choose C/C++ as shown below.

Fig 1.9
So you just saved your file as hello.c. Check if hello.c is the name of the selected tab in the editor window. Save all your C programs with a .c extension.

Fig 1.10
Now we are ready to write some code. Type in the code below on the editor. Save the file when finished.

Fig 1.11
Don't let this scare you! I will explain everything shortly. For now, just carry out all the steps mentioned here. From the Build menu, click on Compile Current File.

Fig 1.12
If you have written the code from the book as-is, you should see the message 0 errors, 0 warnings. This  means that you have used the syntax of the language right. The message should appear as shown below.

Fig 1.13
You may now run the program you have written. From the Build menu, click on Run (Fig 1.14). This will execute your code and produce an output similar to Fig 1.15.

Fig 1.14

Fig 1.15
Notice the Hello World you have printed on the screen. That is exactly what your code does. The next line says Process returned 0 (0x0). We shall ignore this message for now. Execution time 0.031s means that your program took 0.031 seconds to run. Finally, the message Press any key to continue actually explains itself. Pressing any key will close the window and take you back to the editor.

If you have successfully run the program to print Hellow World on the screen, I must say you have done a very nice job indeed. Take your pride!

In case you haven't been able to run it successfully, try again, restarting the whole sequence of steps if necessary. If you are still stuck, take help from someone experienced. You have to get it right by now, otherwise you will gain nothing by moving forward with this book.

Let us now understand what we have actually written. The #include<stdio.h> in the first line will be explained shortly. This is followed by an empty line just for the sake of clarity. Empty lines serve no purpose in the code other than providing some comfort to our eyes. The third line is int main().  This is called the main function of the program, named such for the fact that all C programs begin execution by starting from the main function. A program may have only one main function. The code for the main function is enclosed within curly braces ( {} ), and a return 0; is placed right before the closing brace ( } ). This will be explained in a later chapter where I will discuss functions in C. For now, just use the following code skeleton to build your program on. 

int main() 
{
    your code goes here
    return 0;
}

The next line of our code says printf("Hello World"); In C, this is called a statement. printf() is a function that we will use whenever we need to print some text on the screen. We need to put the text within double quotes ( "" ) inside the printf() function. The text in our code is Hello World. The printf() function uses the file stdio.h to carry out the steps in writing the text on the screen. Files like stdio.h are called header files in C, and have the extension .h. The functions for performing standard input/output operations are defined in the stdio.h header file. We really do not have to know how these functions work internally. But to use these functions we have to write #include<stdio.h> in the beginning of our code as we did in our hello world program. There are many other useful header files available in C, each providing a different set of functions. We will use a few of them in our programs when necessary.

Statements in C, such as printf("Hello World");, are terminated with a  semicolon ( ; ) . C executes each statement sequentially, from top to bottom. return 0; is also a statement, hence it has a semicolon at the end. A common mistake by beginners is to forget to put a semicolon after each statement, and thereby earn a compile error. Try to compile your code by removing a semicolon from one of the statements. Prepare for a compile error.

A very important part of writing code is indentation. Notice carefully the code I have written here. The lines #include<stdio.h>, int main(), { and } all start at the leftmost column of the editor, along the same vertical line. However, printf() and return 0; are actually aligned four spaces to the right of the lines above. This method of keeping a fixed amount of space for codes within the curly braces ( {}, or blocks ) is called indentation. Like an empty line, keeping these spaces are not necessary for the execution of the code. In fact, they are ignored by the C compiler. Nevertheless, maintaining proper indentation of your code is a must. Good habits are hard to grow, but you should grow the habit of always indenting your code. A code without indentation can upset your colleague, irate your boss or even slim your chances in an interview! I hope you will realize the significance of indenting your code. The standard practice of indentation is to keep four spaces to the left. You can set the Tab key of your keyboard to put four spaces for indentation in the editor. To do this, from the Settings menu, click on Editor. In Tab Options, put a check on Tab indents and set TAB size in spaces to 4 (Fig 1.16).

Fig 1.16
Now, here comes the first exercise for you! Write a program that prints I love my country, Bangladesh! on the screen.

You must write the complete code for the program anew, compile it, and finally run it. Remember to save your file before compiling.

No comments:

Post a Comment