Monday, July 19, 2010

C Programming Basic

Let's move on. Before we go ahead analyzing the program we wrote in the previous chapter, we'll spend a few minutes discussing about computer programming in general, and then about C.

Programming

First of all, why write a computer program? There are many reasons. However, most of them is written in order to take the advantage of computer in processing a large amount of data. For example, we have over 40.000 student in our university. Each study about 9 subjects per semester and we have to store, organize and do some calculation with 3 x 9 x 40.000 = 1.080.000 numbers. We can't deal with that by hand, can we?

Second, about C programming language. Nowadays, there are numerous of programming languages, but C is the most popular one. It was developed by Dennis Ritchie at AT&T Bell Labs in 1970s. The kernel - the core - of Unix and Linux operating system is written in C. Its syntax inspired many modern languages such as Java, C# and C++.

C lacks many functionalities that modern programming language requires. For example: It is too close to the hardware (C is know as the most low level of high-level programming languages); there is not any garbage collection so we have to do all the memory managing task by hand. Further more, it is not object oriented, so for complicated projects, people tend to use object oriented flavor of C such as Objective-C or C++.

C Programming

In general, a programming language comprise at least three components, the syntax, the compiler or interpreter, and the core library. Let's all of those things working together in our example in previous chapter.

The C syntax

As I said above, C's syntax inspired many modern programming languages. It is very clean and efficient. Obviously, each statement is finished by a semicolon (;); and chunk of statements can be grouped together inside curvely bracket ({}). Of course, learning a programming language is all about the syntax, so we will talk about it many times later.

The C compiler

Some programming languages have compiler, some others have interpreter, and some even have both compiler and interpreter. What these things do is to translate the source code into binary, or execute it. With C, there are both compiler and interpreter, but most of the time we will use compiler because it is the C way. There are also various C compiler, such as GNU C Compiler (gcc - which we used), Borland C, Microsoft C Compiler and many more. We use gcc in this course because it is free and open source, it is also cross platform and it is the most popular one. Remember our command?

gcc -Wall -Werror -W -o hello hello.c

gcc invoke the compiler. -Wall, -W, and -Werror is short for Warn all and Warn Error, which tell the compiler provide more feed back if there was something wrong with our program. -o hello let the compiler know that it should provide the binary file named hello instead of its default hello.out. Finally, hello.c is the file it should compile.

C Standard Library

The first line in our program:

#include <stdio.h>

tells the compiler that we will be using a standard C library named stdio.h. Naturally, C programming language comes with many standard libraries, which is collections of pre-written functions we can use in our program later. In this example, we include this library so that later we can use the function printf(), whose job is to print out a given string.

What's next?

Up to this point, you haven't had have many thing to learn yet. So your exercise will be as easy as skim the gcc man page and make the program display "Hell, no!". Until next time.

Source code: hello.c

No comments:

Post a Comment