[Courses] [C] Beginner's Lesson One: Getting Started

KWMelvin kwmelvin at intrex.net
Tue Oct 8 11:30:21 EST 2002


I have received some encouragement to start posting some beginning
C lessons, so I'd like to get started.  According to the list archives,
most everyone has a copy of _Practical C Programming_ by Steve Oualline,
published by O'Reilly and Associates.  I have the first edition, and
will be using it as a syllabus.  However, I will probably be bringing
in some related exercises from other textbooks as well.  If you have
the textbook, fine, if you don't, that is okay too--you'll just have
to ask more questions from those of us who do have it.

I'm going to make some assumptions, namely: we are using some flavor
of GNU/Linux; we have it installed and working on our computer; we
have a text editor of some sort (vi, vim, emacs, etc.); we have the 
GNU C compiler installed and working.

I use the console for the most part because I do not have X installed
yet.  If you're using X, you'll need to start an xterm from one of
your menus, or launch pads, usually by clicking one of the buttons
on your mouse and choosing from a menu.  I'm not familiar with Gnome
or KDE, so I can't help you with that (I used fvwm without a "desktop"
the last time I had X installed--on an old 486).

First of all, let's see if the GNU C compiler is installed.  
I'll be using the "$" as a prompt:

$ gcc --version  <enter>

You should always press the <enter> or <return> key after typing a
command at the shell prompt.  I'll not be putting that at the end
of commands from now on.

If you have the GNU C compiler installed, you should get a version
number back, something like this:

$ gcc --version

Another way to find out if the compiler is installed is to use the
"which" command:

$ which gcc

If you don't get something similar to the above, you'll need to
install the compiler so you can compile and run the C source code
from the exercises. Ask for help!  I run Debian GNU/Linux and can
try to help you if you run that distribution.  I don't know about
RedHat, Mandrake, SuSE, or Slackware, so maybe someone here can help

Secondly, after getting the GNU C compiler installed and going, we
need to start a text editor and write the traditional first C program.
I use "vi" as a text editor, and recently installed and tried the
"vim" text editor (I learned how to turn on syntax highlighting from
reading the list archives!).  I like vi because you can start using
it immediately, with only a handful of commands.  I have a .exrc file
in my home directory with some useful commands in it.  Let's create
a simple .exrc file with vi right now, as an introduction on how to
start it and use it for simple text editing.

vi basically has two modes: command and insertion.  You need to know
that it starts out in command mode.  In order to start typing, you'll
need to get into insertion mode, by pressing the "i" key.  Start vi:

$ vi .exrc

You'll now be in the full-screen editor.  The cursor will be blinking
at the top left hand corner of the screen, with a column of tildes
under it.  To start editing, press the "i" key.  You are now in
inserion mode.  Type the following lines:

set tabstop=2
set num

Now press the "Esc" key to return to command mode.  To save this file
and exit vi, you'll need to press the colon ":" key, the "w" key, and 
the "q" key.  To exit without saving, press the colon ":" key, the
"q" key, and the bang "!" key. If you saved this file, you'll now have
a .exrc (dot exrc--don't forget the dot!) file in your home directory.

I really only use a few commands while editing in vi.  The main ones
I use are: "x" to delete the character over the cursor; "dd" to 
delete the entire line that the cursor is on; "yy" to copy the line
the cursor is on; and "p" or "P" to paste the line below or above
the cursor.  It's amazing what you can do with just those. I'll show
a few more commands as we go along.  I've learned them a little at a
time, so they weren't too hard to grasp. 

The most useful of the two commands that we put in the .exrc file is
the "set num" command.  This shows line numbers during our edit. 
They will come in handy when we are trying to debug a program using 
the error messages generated by the GNU C compiler!

Okay, I guess we're ready to create our first C program. I highly
recommend that you create a directory to put all your exercises in
so that your home directory doesn't get too cluttered.  I have a
"src" directory for my examples.  You can call yours whatever you
want to.  Here's how to make it:

$ mkdir src

Now change to it:

$cd src

Now start your text editor and type in the following C program.

$ vi hello.c
(remember to press "i" to get into insertion mode)

-----8<---cut here--->8-----
#include <stdio.h>

int main(void)
	printf("hello, world\n");

  return 0;
-----8<---and here--->8-----

Check it over carefully to make sure you haven't left off any
punctuation, or misspelled anything.  You should not type in
the "cut here" lines at all ;-).  When you are satisfied that
everything is okay, enter command mode by pressing the "Esc"
key, then type :wq to save the file to disk and exit.

If you do an "ls" at the shell, you should see the file, hello.c

Now we need to compile the source code into an executable:

$ gcc -o hello hello.c

Run the executable with this command:

$ ./hello

If you've done everything correctly, you should be rewarded with
the following output:

hello, world

If you have followed this so far, then congratulations!  We are
ready to start our C programming adventure!  If you need help,
please ask. All mistakes in this lesson are mine.
Reply to: courses at linuxchix.org

Happy Programming!

More information about the Courses mailing list