CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Programming] C programming series (I)


[Reply] [View by Thread] [Help]
[Back To Article Discussion Forum]

Posted by Author alfer On 2007-04-29 10:01:46




View and vote on the article here: C programming series (I)


C programming series (I)

Category
Programming
Summary
One of the most widely used programming languages is C. Almost any competent

programmer knows C or one of its derivates (C++, Java...). Let's include you among those

programmers.
Body
In this set of tutorials we'll try to teach you, progressively, to code

basic programs that actually work, to use the compiler and other tools to

obtain your program from your code, and, of course, to understand the code you

are writing.



To follow this set of tutorials, you only need a text editor, a good C

compiler (the program that translates what you write in instructions the

computer can understand), and time to study!. We'll try to put the rest.



For the C compiler, we strongly recommend the GNU C compiler (gcc). It is

the best C compiler available by far, and also it's free! Even more, if you

have installed GNU/Linux in your computer, you already have it!! What? You say

you have RedHat Linux but not GNU/Linux? Wrong! All "Linux" are GNU/Linux:

Linux is exclusively the kernel of the Operating System. The rest of the

things you use to work (tools like the compiler) are GNU. For more

information about this important point, go the <a

href="http://www.gnu.org]GNU site[/url] and have a look


If you don't use GNU/Linux or a different *nix variant, check the web for

free C compilers (DJGPP, for example).



C is NOT a good choice for a first programming language. In fact it

is recommended that you already know some of the basic concepts about

programming: variables, loops, data structures... But don't worry: we'll try

to provide an explanation of almost everything.



Furthermore, when you learn a bit about C, probably you'll want to buy a good

book on the subject, to learn more advanced concepts and for reference. If you

accept my recommendation, buy "The C programming language", by B. Kerninghan

and D. Ritchie (also know as the "K&R book"). It is not only written by the

original designer of the language: it is also one of the best written books in

the computers field. But, again, it is NOT a good book to learn programming: it

is excellent to learn C, you already need to know your stuff.



So enough preamble!, let's start coding!!. Here is our first C program (copy

to your editor the lines between the dashed lines, not including these):





Program 1.1: Hello World.


------------8<-Cut here------------


#include &lt;stdio.h&gt;


main()


{


printf("Hello World!

");


}


------------>8-Cut here------------



And that's it! Simple, right?

Before explaining each of the lines, we are to compile the program. The

steps to follow are:

1. Copy the text of the program (the code) to a text editor.


2. Save the files as "hello.c". It will be saved in your local directory.


3. Exit your editor and, in the command line, type: "gcc -o hello

hello.c".


4. "Nothing" should happen. Now check your directory to find a new file

named "hello". Do a "ls -l hello".


5. Change the permissions of the file to make it executable. Do a "chmod +x

hello".


6. Run the program. Type "./hello".



If everything goes smooth, you should see a:


Hello World!


on your screen. If you have found a problem at any step, stop the process

and go to the troubleshoot section of this tutorial. It covers each step and

some possible problems you can find.

Now, let's have a look at each part of our program.



#include &lt;stdio.h&gt;

This line tells the compiler to "add" a toolbox to our program. This way we

can use other code people has written before. In our case, we include

stdio.h, standard input/ouptup library header. Don't

worry too much about this at the moment, just include this line at the

beginning of your programs.



main()

The main function. Every C program starts in the main function, and ends at

the end of the main function.

{

Beginning of the function main (in this example). Each function has a

beginning and a end, marked by '{' and '}', respectively.

printf("Hello World!

");


The actual code. This is the only line in our program that actually

does something. In this case, it prints to your screen a message

(the text included between double quotes).

}

End of the main function and, therefore, end of the program.

Of course, this explanation doesn't clarify all your doubt, but it's helpful

as a simple overview of each part of the code. In following tutorials we'll

cover much deeper each part and its functionality. Now, read the

troubleshoot section and start with the homework! (exercises section).



Troubleshoot.

NOTE: You should read this section even if you haven't had any

problem with your program. It will show you some common mistakes and how to

recognize them.

Program 1.1: Hello World.



1. Copy the text of the program (the code) to a text editor.


For GNU/Linux, you can use, among others, the following editors: jed, pico

emacs, vi. If you don't know which one to use, choose jed or pico (if you have

them!). The only one that for sure is on your computer is vi. Basic vi

instructions:

Exit vi without saving the file: [Esc] :q! (and then press [Enter])


Exit vi saving the file: [Esc] :wq! (and then press [Enter]


Enter insert mode: type 'i', and then write your text



2. Save the files as "hello.c". It will be saved in your local directory.


Check you have permissions you write in that directory. Check your editor

doesn't add a ".txt" extension to your saved file (ie. "hello.c.txt").



3. Exit your editor and, in the command line, type: "gcc -o hello

hello.c".


If you get something like: "gcc: Command not found", it means you don't have

the GNU C compiler installed (or it's not in your path. Ask your local

guru or post a detailed message in the <a

href="http://www.cyberarmy.com/wwwboard/programming]Cyberarmy Programming Board[/url].

Make sure you don't mix the order or the arguments. If you type something

like: "gcc -o hello.c hello", you WILL lose your file, and will have to

re-type it again.

4. "Nothing" should happen. Now check your directory to find a new file

named "hello". Do a "ls -l hello".


This is the most likely point in which you will find errors. If "something"

appears when you try to compile, it will possibly be an error. Review

carefully your code and correct any typo you find. Here are some

classical errors you can get:

hello.c:1: undefined or invalid # directive


hello.c:1: &lt;something&gt;: No such file or directory


hello.c:5: syntax error before '&lt;something&gt;'



Exercises:



What is the use of "

"?


Write a program that produces the following output:


First line


Second line


Third line



Write the previous program using only one printf function. Write it using several.


This article was imported from zZine. (original author: alfer)


There are no replies to this post yet.



Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.02491 seconds


About Us | Privacy Policy | Mission Statement | Help