How to Program in
C,continued...
How to Turn C code into a Working Program
One of the great character flaws -- or is it strengths? --
of most hackers is a burning desire to make something work RIGHT
NOW, DARN IT! Are you ready to become a C programmer? How about
becoming one NOW! The first thing you need is a C compiler. While
in your shell account, give the command "cc". If you
get the message "command not found," try the command
"gcc" If these don't work, try "whereis cc"
, "whereis gcc", "which cc" (in Linux), "locate
cc" or "locate gcc". If none of those work, complain
to tech support at your ISP (or read your Linux manual). Don't
email us, because we can't help you with this problem! If you
have a free shell account, and it doesn't offer a C compiler,
maybe you should consider paying for a good shell account.
If these commands tell you where the C compiler is, try either
changing to that directory or including a path statement to that
directory in your login script.
So, are you ready to
write your first C program?
At the prompt in your shell account, type "pico hello.c".
The command "pico" brings up a super easy editing program.
All the commands are listed at the bottom of the screen. Even
I could learn how to use pico in a few minutes without help.
********************************************************
Newbie note: Don't worry if you make mistakes with pico. There
is nothing you can do to seriously hurt your computer unless
you are root. How do you know if you are root? If you have to
ask this question -- you aren't:)
********************************************************
If you can't find pico, or if you are one of the rare people
who hasn't learned yet to program in C, yet who knows how to
use a more advanced editor, try "man vi" or "man
emacs" to learn how to use a more powerful, but harder to
understand, editor.
At the prompt in your editor, type in these lines exactly
the way they are here.
#include<stdio.h>
void main()
{
printf( "Hello, hackers!\n" );
}
Next, save this program with the command "control-X".
Now give the command "ls". This will reveal that
you now have a file named "hello.c". The "c"
at the end of this file name identifies this as a file containing
C commands. Congratulations, you are already halfway to making
your own C program.
However, at this point, if you type in the command "hello"
or even "hello.c", just like you would to run a shell
script (program), nothing will happen. That is because this file
is still just "source code," a listing of commands
that your computer doesn't understand. This is different from
shell programs which only have commands that your computer already
understands without having to compile them first. Shell programs
are called "interpreted" languages, meaning your computer
can automatically interpret the shell commands you give it. By
contrast, C is a language that must be compiled before you computer
understands what you are asking it to do.
So our next step must be to compile hello.c. Give the command:
cc hello.c
Or, if this doesn't work, give the command "gcc hello.c".
Throughout the rest of this chapter we will assume "cc"
is the correct command, so if you need to give the command "gcc",
please replace cc with gcc in everything below.
********************************
Wizard tip: Your system may offer a choice of C compilers. On
some systems "cc" will run a compiler written by the
company that also wrote the operating system for your computer,
while "gcc" will run the GNU C compiler. Every C programmer
I know says the GNU compiler is best.
*******************************
What this does is
1) start your C compiler running with the "cc" command
2) with the 'hello.c" part of the command you tell the compiler
where to find the source code you just wrote.
3) the compiled program is, in most cases, automatically stored
as a.out. (If it wasn't stored as a.out in your case, you will
get the solution to your problem in a few more paragraphs.)
Now -- the big event. Let's run your first program. Simply
give the command "a.out". Your computer should say
back to you, "Hello, hackers!"
Congratulations! You are now a C programmer.
More on C -- what to do if your program
didn't run!!! --->>