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).

No comments:

Post a Comment