Monday, April 18, 2011

Starting to program in C: Chapter Three

Variables

Well, now we introduce the variables. To understand the concept, I'll put a very simple example: imagine we have boxes that we use to store objects. But these cases are rare forms that only allow us to keep a certain type of object. There are shoe boxes, bottle crates, boxes for mosquitoes, etc ... As well, these boxes are variables, and store data: numbers, words, etc ... Are called variables because you can change the object that they have inside, but whenever the same type.

For example, the following code:
_________________________________________________________________________________________

#include
#include

int main(int argc, char *argv[])
{
int age_jhon = 5;
int age_mike = 12;

printf("Age of John: %d\n", age_jhon);
printf("Age of Mike: %d\n", age_mike);

getch();
return 0;
}
_________________________________________________________________________________________

int age_jhon = 5;
_________________________________________________________________________________________

We define a variable of type int with the name age_jhon, integer, and assign a value of 5.
_________________________________________________________________________________________

int age_mike = 12;
_________________________________________________________________________________________

Here another variable of type integer (int) age_mike named, and assigned the value 12.
_________________________________________________________________________________________

printf("Age of John: %d\n", age_john);
_________________________________________________________________________________________

As we know from the class II, printf() prints on the screen. But here you will see that it prints the value of the variable age_john. You can try changing the value of the variables (you change to 5 other number) and see how it changes what is written on the screen. You see there is a sentence between two double quotes ("Age of John: %d\n"), then a comma, and a variable. In fact, there may be several variables. For example:
_________________________________________________________________________________________

#include
#include

int main(int argc, char *argv[])
{
int age_john = 5;
int age_mike = 12;

printf("Age of John and Mike: %d y %d\n", age_john, age_mike);

getch();
return 0;
}
_________________________________________________________________________________________

Here you'll see that both ages appear in the same printf(). Place all the variables we want to show on the screen separated by a comma after the phrase. Then in the phrase put % followed by a character type (d for int for now, that is, %d). Each % is in order with the list of variables. In any case, don't worry so much.
_________________________________________________________________________________________

getch();
_________________________________________________________________________________________
Wait for a keystroke.

Another example:
_________________________________________________________________________________________

#include
#include

int main(int argc, char *argv[])
{
float theoretical_note = 5.0;
float practical_note = 2.0;

printf("Theoretical examination mark: %f\n", nota_teorico);
printf("Practical examination mark: %f\n", nota_practico);

getch();
return 0;
}
_________________________________________________________________________________________

Now we have declared float variables, ie real numbers or decimals (comma):
_________________________________________________________________________________________

float theoretical_note = 5.0;
float practical_note = 2.0;
_________________________________________________________________________________________

And as you will see, in printf() no longer use %d, but %f, because the variables are "float" and not "int".

Another example, this time with strings:
_________________________________________________________________________________________

#include
#include

int main(int argc, char *argv[])
{
char* student_name = "Fernando Alonso";
char* teacher_name = "Melendi";

printf("Name of the teacher: %s\n", teacher_name);
printf("Name of the bad student: %s\n", student_name);

getch();
return 0;
}
_________________________________________________________________________________________

Now we use the data type char* and as result we have changed %f with %s in printf().

It's important to note that variable names can't have spaces or start with a number. Can only be composed of letters, numbers and the underscore character (_). If we stick to these rules, the compiler will give an error indicating the line of error. You can try it.

And as an exercise, I let it make me a small program that, watched these examples, print:

(Student name) scored a note (Note theory) in the review of the day (Date of theory)

Of course replacing parentheses content by the corresponding variables. Hint: the date is as the name.

That I leave you to the next chapter.

Wednesday, April 13, 2011

Starting to program in C: Chapter Two

Hello again. In this new tutorial we'll build our first program. A "Hello World", the basic program with which every coder starts.

The following applies every time we create a new program.
NOTE: don't use copy-paste the code. Copy that by hand, you'll learn more.

Our first program 
Linux:

Simply open the text editor that we installed in the previous chapter. We write:
Don't forget to leave a blank line at the end. If not, the compiler'll ruling.
Create a directory named for example hello world. Save the file there as hello.c. You'll see that by doing this automatically appears colored text.

Now we go to a console, drag the directory hello world over the console and release. We'll see how it shows the full path of the folder in the console. Press the Start / Home, add a cd followed by a space and press enter. Now if you type ls you should see the file hello.c.

You type gcc-o hello hello.c and press enter. You shouldn't give any message. Then you type ./hello and see how it appears on the console a Hello World.

Windows:
Open Dev-C + + and press File -> New -> Project. A window like this will appear:

We accept. It'll ask you where you want to save the project. I advise you to think a new folder for each project, a project can have multiple files.

After saving the project, the appearance of Dev-C + + will be something like the following:

Now modify the text (code) main.c tab to make it like this:

 Don't forget to leave an empty line at end.
Now click on Compile and Run
After the compilation window go away (with a bar that is filled), you should see a Windows console with the message Hello World.

Understanding (a little) our first program

The Linux and Windows version of our program is almost identical except for the part of system. So I will use the version of Linux and we see what each thing:

Lines beginning with pound (#) are preprocessor directives. In this case, simply indicate which files are to be included in our project: stdio.hy stdlib.h. These (and many others) files allow us to use functions that are already made, so that you do not need to reprogram us, such as printf (), which the more astute will have guessed who writes things on the screen.

This is the famous main() function of C. This feature appears in all the programs in C, because it indicates where the code begins. You'll be filled with it.

These keys tell us where to start and finish a certain structure of C, in this case the function main ().

printf() is one of the most used functions of C. Used to write something on the screen. Specifically what is going between the two double quotes: "Hello World \ n ". \ n is the newline character.

The system() function simply executes a command on the console. This obviously depends on the operating system, and therefore not the same in Linux / Windows. In this case, wait pressing Enter (any key in the Windows version).

End a function and returns to that you called, returning a value or not. In our case, we end main(), which means we have finished the program and return to the operating system, with a value of 0 indicating that the program has completed successfully.

Look also in C, instructions are provided, at the end, except when they have {}.

Well. That's it for today. In the next tutorial we'll begin to see variables a very important part of C.

I have to remember that I'm not the author of the tutorial. It's is extracted and translated from another site (DaXHordes.org, in Spanish).