Tuesday, June 2, 2009

1 - Where do I start

1 - Where do I start
============================ Aliens C Tutorial ================================
-----------------Written by Alien <-> alien@ktv.koping.se----------------------
===============================================================================
First of all, this tutorial will be much more of a challange to write
then my previous tutorial (bash.tutor), in this tutorial I will try
to explain the C programming language and make it undestandeble.....
This tutorial will explain ANSI C,
(ANSI is American National Standards Institute).
C was first developed at Bell Labs by Brian W. Kernighan and Dennis M. Ritchie.
C is a programming language that needs to be compiled when written, this
is the oposit of shell scripting like bash.
C is ALOT more powerful then bash or any script language, with C you
have real controll over what your program will do.
C is developed for UNIX, and the examples in this tutorial is made to
work on UNIX, they should however also work in DOS, but I leav no
promices for that.
The code in this tutorial is written more or less only in K&R
(Kernighan and Ritchie) style C, this style is also called Bell Labs style.
Later I will explain the difference between the Bell Labs style and
student style.
I will not take up anything about graphical programming in this tutorial
for more then one reason, but the best is that it would take up to much
space and be confusing, so if you want to learn graphical programming in
C, read this tutorial to learn the actual language and then some other
tutorial about graphical programming, another reason is that there is
more then one way of doing graphical programming because there are
several sets of 'wigets' and libs that can do that.
I will try to start from the very beginning so here we go.

===============================================================================
1 - Where do I start ?
===============================================================================


You start here, with a simple "hello world" program.
Make a file called hello.c and in it do the following:
//----------------------------------------------------------
#include
int main ( void )
{
printf ( "hello world!\n" );
return 0;
}
//----------------------------------------------------------
And once you've saved and exited, your ready to compile it,
and that you do like this:
gcc -o hello hello.c
Now you can execute it:
./hello
And it will look like this:
alien:~$ gcc -o hello hello.c
alien:~$ ./hello
hello world!
alien:~$
-------------------------------------------------------------------------------
So, now what have you done, and what does that mean ?
You have done your first program in C, that will output "hello world!"
to the screen.
So, now 'exactly' what does the code mean ?
Let's start from the first line.
#include
This will include the header file stdio (standard input output), this file
holds the information on how, among others, the printf function works and
how it should be handeld by the compiler.
I will explain header files better later.
The next after that is:
int main ( void )
This makes a function called main, which is the core function of your code
that can call for other functions later.
main is always of type 'int' (integer), because it has to 'return' something to
the system, I will come to that very shortly.
And the ( void ) part means that we will not pass any arguments to the main
function.
You dont really have to know what all this means at this point, but I'm still
telling it so you will have an easyer time understanding it later.
The next line after that is the:

{
This opens the function, and expects an action.
The next line after that is:
printf ( "hello world!\n" );
This is the main action in our program.
Here we print 'hello world!' to the screen, the \n means new line.
printf is a function, and as any funtion it's arguments is written inside
the paranteses, and the line is ended/terminated with a semi-colo.
The next line is:
return 0;
Here we return 0 to the system, this means success, and the program know's
that it has finished successfully here.
0 means successful, and -1 means unsuccessful.
And the last line:
}
This closes your function.
-------------------------------------------------------------------------------
Note: return 0; can also be written as: return ( 0 );
-------------------------------------------------------------------------------
So what do you need to remember about this at this point ?
Well, the thing that you need to remember is the basic layout for a
program.

#include
int main ( void )
{
return 0;
}

So memorize that for the time beeing.
-------------------------------------------------------------------------------
So next up ?
Now it's time to explain integers and how to count in C, so here we go.
Make a file called count.c and in it do the following:
//----------------------------------------------------------
#include
int main ( void )
{
int i, x;
i = ( 50 );
x = ( i + 10 );
printf ( "x is now: %i \n", x );
return 0;
}
//----------------------------------------------------------
And once you've saved and exited, your ready to compile it,
and that you do like this:
gcc -o count count.c
And now you can execute it by doing:
./count
And it will look like this:
alien:~$ gcc -o count count.c
alien:~$ ./count
x is now: 60
alien:~$
-------------------------------------------------------------------------------
So, now what have we done this time, and what does that mean ?
The 3 first lines is (I hope) already well explained.
So the fist new line here is:
int i, x;
This tells the program that there is 2 variables (separated by a comma),
that are of type integer, all pointers and variables must be declared
before any real action in the code.
Exactly what a pointer is I will explain after the next example.
A variable is just a name that can hold data inside it ...
That's not the best explanation, but that's a good way of thinking about it.
It really works about the same a pointer, but just think of it as a
synonyme or a rename of a value.
These 2 integer variables can hold data that is integer numbers.
Which is shown in the next line:
i = ( 50 );
Here we make the integer variable i hold the value 50.
And the next line:
x = ( i + 10 );
Here we make the int x hold the value of i (50) plus 10, which
is 60.
The next line after that we print the resault out to the screen:
printf ( "x is now: %i \n", x );
The new things here is the %i which tells printf to replace that space
with an integer that's held in a variable that comes after the a comma
after the double quote, this will be shown better in later examples.
And the return 0; and the close bracket is already explained.
-------------------------------------------------------------------------------
Next up is almost the same as the previous example, but this time with
characters instead of integers.
So here we go with an example of character pointers.
Make a file called string.c and in it do the following:
//----------------------------------------------------------
#include
int main ( void )
{
char ptr[10];
sprintf ( ptr, "My String" );
printf ( "ptr contains: %s \n", ptr );
return 0;
}
//----------------------------------------------------------

And once you've saved and exited, your ready to compile it,
and that you do like this:
gcc -o string string.c
And now you can execute it by doing:
./string
And it will look like this:
alien:~$ gcc -o string string.c
alien:~$ ./string
ptr contains: My String
alien:~$
-------------------------------------------------------------------------------
So, now what have we done now, and what does that mean ?
As before the first 3 lines you should know now.
And then there is the next new line that looks like this:
char ptr[10];
This declares a pointer that will hold data of type char (characters),
and the [10], means that it will hold the room for max 10 characters.
The next new line in the code is this:
sprintf ( ptr, "My String" );
sprintf means string print file, and the pointer is before the static text,
because this time we are pointing the pointer to a string and not displaying
it like to the screen like we did with the integers in the last example.
In short, sprintf will point a point 'to' the string.
(pointers will be explaind under this example).
But that we do here instead:
printf ( "ptr contains: %s \n", ptr );
This works the same as with the integers but we have to use %s to tell
printf that it's suppose to write it out as characters.
And the 2 last lines you should know by now.
-------------------------------------------------------------------------------
So exactly what is a pointer ?
I will try to be breaf about this, and you dont really need to remember this,
I'm putting it here for the sake of the knowlidge and that you should atleast
have read it at some time .... so now is a time as good as any.
Here we have a chunk of memory in the computer:
1 2 3 4 5 6 7 8 9 10 11 12
-------------------------------------------------
| | | | | | | | | | | | |
-------------------------------------------------
This is a chunk of memory that has 12 bytes.
And if we do:
char pointer[6];
It will resault in this:
1 2 3 4 5 6 7 8 9 10 11 12
-------------------------------------------------
| | | | | | | | | | | | |
-------------------------------------------------
^ ^
|_____________________|
|
|------ pointer
So you see, that's why it's called a pointer, because it points to a
chunk of memory, and if we write something to it, by doing say:
sprintf ( pointer, "String" );
The memory will look like this:
1 2 3 4 5 6 7 8 9 10 11 12
-------------------------------------------------
| S | t | r | i | n | g | | | | | | |
-------------------------------------------------
^ ^
|_______________________|
|
|------ pointer
This is why you need to declare a pointer, so it can point to a free space
in the memory, of the size of your choice, where it can store it's data.
I hope that this explains how a pointer works.
-------------------------------------------------------------------------------
Here is an example with both characters and integers.
Make a file called intstring.c and in it do the following:
//----------------------------------------------------------
#include
int main ( void )
{
int i, j, x;
char ptr[10];
sprintf ( ptr, "My String" );
i = ( 3 );
j = ( 7 );
x = ( i + j );
printf ( "ptr contains: %s \nAnd x contains: %i \n", ptr, x );
return 0;
}
//----------------------------------------------------------
And once you've saved and exited, your ready to compile it etc.
And it will look like this:
alien:~$ gcc -o intstring intstring.c
alien:~$ ./intstring
ptr contains: My String
And x contains: 10
alien:~$
-------------------------------------------------------------------------------
This time you should know what we have done.
But just in case, I'll explain it anyway.
First we inlude the standard input/output header file.
Then we create a main function with nothing to pass to it, (the void).
Then we open the function.
After that we declare i, j and x as integers.
And then declare ptr as a char pointer that may hold 10 bytes.
After that we print "My String" to the memory location of the ptr pointer.
Next we say that i should be a 3, j a 7 and x should hold i + j,
which means 3 + 7.
And then we print it out to the screen.
If you look at the line where we print it out to the screen:
printf ( "ptr contains: %s \nAnd x contains: %i \n", ptr, x );
Now if we were to print out the integer first and the string after that,
the line would have looked like this:
printf ( "x contains: %i \nAnd ptr contains: %s \n", x, ptr );
Notice that you also have to switch places for the pointers after the
double quote.
After that we have the return 0; which tells the system that it's come to
this point successfully and that it will quit here.
And then we close the main function.
-------------------------------------------------------------------------------
Note: printf with pointers works like this:
printf ("char 1 %s - char 2 %s - char 3 %s etc.", char1, char2, char 3);
-------------------------------------------------------------------------------
Now it's time to start to comment the code.
To make memory notes so you know what you have done for later.
This is something all coders does.
Let's take the last code, intstring.c and comment it.
//----------------------------------------------------------
#include
int main ( void )
{
/* declare variables and pointers */
int i, j, x;
char ptr[10];
/* print the string to the memory location
* that the pointer ptr points to.
*/
sprintf ( ptr, "My String" );
/* point the integers to something */
i = ( 3 );
j = ( 7 );
x = ( i + j );
/* print it out to the screen */
printf ( "ptr contains: %s \nAnd x contains: %i \n", ptr, x );
/* return success and exit. */
return 0;
}
//----------------------------------------------------------
Now normally you dont comment *everything* in a code like this,
but you may wanna comment things in your codes so that you can remember
what you have done and to make it easyer to search, and if not for that,
to make it readeble to others, if you are in a coding team.
The /* starts a comment and */ ends a comment in C, and if you have several
lines you do like this:
/* here is
* a comment
* that's
* severa lines
*/
You may also come across comments that are made with 2 slashes like this: //
That is really a C++ comment and will comment out the rest of the line
from that point.
It can be used like this:
printf ( "Anything here\n"); // print something to the screen
But a real C comment should look like this:
printf ( "Anything here\n"); /* print something to the screen */
Note that they both work in C and C++, but it's standard to use the /**/ in C
and the // in C++.
-------------------------------------------------------------------------------
That was the very start to begin to learn C, this following section
will be a little bit harder, but still basics.

No comments:

Post a Comment