Tuesday, November 6, 2007

Programming Language C-Tutorial1

Introduction


C is an extremely powerful programming language. C is a language written by programmers for programmers, it became the most popular programming language ever in 1978. UNIX, its tools, and C were developed almost simultaneously, C grew up with UNIX but has since then left its home and is available on just about every computer system around. The development of C is not over, a researcher Bjarne Stroustrup Of Bell Laboratories began experimenting with an object orientated flavour of C that he called C++ (c plus plus). C++ extended C, in Bjarne's words "making a better C". This tutorial focuses on C, not C++. The X3J11 committee have adopted some of Bjarne's proposals into the ANSI C standard and since then an ANSI C++ standard has been created.

C++, however, didn't take over from C. C++ isn't for everyone and when learning C it's advised that you stick to the basics, learn to program in C, and when you're ready, move onto object-orientated C++.

C is a compiled language. For those of you that know Perl or PHP, you know that you write the source code and the Perl or PHP interpreter compiles the script on the fly (or interprets it). With C you must compile your source code into "machine language" before you can run the program. You write the source code, compile that source code into machine language that the computer can understand, and then run your program. Here is what you need to do:

Install a Compiler, there are many out there.
Learn the Basics of C.
Compile your source code into machine code and run the program.

Let's go on and learn more about compilers.

Compilers


When you have written your C program in source code, you must then compile it into machine code.

As it says on whatis:

"A compiler is a special program that processes statements written in a particular programming language and turns them into machine language or "code" that a computer's processor uses. Typically, a programmer writes language statements in a language such as Pascal or C one line at a time using an editor. The file that is created contains what are called the source statements. The programmer then runs the appropriate language compiler, specifying the name of the file that contains the source statements."

So basically, a compiler converts source code into executable code that can be processed by your computer. Source code enables the programmer to write a program at a higher level and logically spell out what they want the program to do. The compiler then converts these logical steps into a language the processor can understand.

Which Compiler?


There are many compilers to choose from, and it depends what operating system you're using, but here are a couple links to compilers that are quite popular.

Borland's Site - Borland are currently giving away Borland C++ 5 for free.
DJGPP - DJGPP is a free compiler that has its own graphics library. It's a pretty big download and can be tricky to set up, but it's worth it.
BloodShed - Another free compiler, a 6MB download, very easy to set up and it runs in windows.


Remember, C++ compilers can also compile pure C source code, so you may aswell get a c++ compiler, as if you ever move onto C++ you wont have to download a separate compiler.

Ok that clears compilers up, let's take a look at a simple C program. It'll have to be simple as i'm very new to C, so i can only cover the very basics.

Take a look at the simple program below:

#include

int main()
{
/*My first program*/
printf("Hello World!n");
return 0;
}


If you're completely new to C that'll probably look confusing, so let's go through it.

#include

This line does what it says it does, it includes the header file "stdio.h" - stdio means "standard input/output. This "header" file contains ready-to-go functions for your program. Without this file you would have to write your own functions to input and output data, but why should you when others have done it for you? You can simply include their functions that are stored in the header file into your program and use these functions. If you know perl or PHP this may seem weird (it did for me) but C's functions aren't "built-in" as they are in Perl and PHP, you have to include the appropriate header files containing the functions you wish to use in your program.

/*My first program*/

This line is a comment, anything between /* and */ is a comment and will not be compiled. Comments are used to, of all things, comment your source code. If you get into C and start writing programs that interest others, sooner or later others will be reading your source code. To help them follow it more easily comment it, tell tham what's happening at each step, commenting = good. A comment can span multiple lines, just contain it between the comment symbols.

int main()

We have included our header file and have the functions contained within it available to us, now we need to make the program do something. But before we do anything we must tell the program where to start and where to finish.

int main()
{
/*program instructions*/
}


This defines where the program starts and where it ends, we contain the program instructions between the curly braces. All C programs must contain this if nothing else.

printf("Hello World!n");

This is the printf() function. It is contained within the "stdio.h" file we included earlier. Our little program simply prints "Hello World!" to the screen. This is what prinf() does. We don't need to cover functions yet, just take note of this, we give printf() the string "Hello World!" and printf() outputs it (prints it to the screen). printf() has more features, we'll see one of these in the next chapter. The n character just means a new line, so after we print "Hello World!" take the pointer down to the next line.

return 0;

This tells the main() function to return, in this case it just means end it. This isn't necessary, it depends which compiler you're using, so it's a good idea to include it just incase.


Save this little program to a file called "hello_world.c". You need a compiler (see last chapter). Then compile it and watch what it does, congratulations you just compiled your first C program.

This is the extreme basics of C, but we all have to start somewhere, the hardest part if you're completely new to programming is getting started, hopefully this chapter got you started and explained a few things to you.

Variables


Defining variables in C is pretty simple. Again, if you know Perl or PHP this may seem a little strange, alot of C might, but it's worth learning this powerful language.

When we define a variable in C we must define what type it is, aswell as its name.

int MyNumber;

The above define a variable named "MyNumber" that can handle integer values. Meaning this variable can only contain number values, not fractional numbers, whole numbers. There are a few types of variables:

int MyNumber; /*int variable.*/
char MyLetter; /*char, can contain letters.*/
float MyFraction; /*float, can contain fraction numbers.*/
double MyExponential; /*can contain exponential values*/


We must know what kind of data the variable will contain, variable names are case-sensitive, We cannot have two variables with the same name, we cannot have two functions with the same name. We can also define two variables of the same type in one go:

int FirstNumber, SecondNumber;

We could define as many variables as we wanted. When we make a variable we are giving a name to our data. Any data you store in that variable is stored in a location in memory associated with that variable. So when we use the variable in later functions or operations, you can access it by a name, it makes it easier for us humans to solve problems this way.

We can also define a variable and give it a value all in one go:

int FirstNumber = 5;
More on printf()


Let's have a look deeper into printf(), let's use a variable with it.

#include

int main()
{
int Number = 4;
printf("The number is %d", Number);
}


We have used an extra parameter with printf(). If we want to use a variable with printf() we must first enter the text to be printed, follow it by a comma, and then the variable name.

"The number is %d"

What's that %d? That's where the value stored in the variable will be printed out. The second parameter is the variable name, and the %d is the place in the printf() string where we want that value to appear. If we did:

printf("The number %d is", Number);

The program would read "The number 4 is", get it? As there are many types of variable, we must use the correct sign to output a variable in printf(), there are different ones depending on the type of data contained in the variable.

%d /*integer input*/
%c /*chatacter input*/
%s /*string input*/
%f /*fractional input*/


So if the variable contained a character:

#include

int main()
{
char Letter = 'S';
printf("The letter is %c", Letter);
}


We would use %c to show that the data stored in "Letter" is the type "char". We can use many variables in a printf() function.

#include

int main()
{
int Number = 3, Number2 = 5, Sum = Number + Number2;
printf("The sum of %d and %d is %d", Number, Number2, Sum);
}


Will print "The sum of 3 and 5 is 8". Just keep the %d signs in order with the variable parameters on the right, as simple as that.

No comments: