Programming in C - Baby Steps
If you haven't done it already, you MUST have a compiler to go any further. I recommend you download OPEN WATCOM now if you don't already have one.
Assuming you've already downloaded the compiler, or have one available, I suggest you create a special directory or folder to put all your C files in. You can name this folder anything you want to, and name your files however you choose. For the sake of argument, we'll call our C folder "CRUD". Hey, it begins with 'C', and describes most of the files we'll be writing. Don't expect many of them to be useful. They are just "demo" files to teach you how the language works, so that you can use it to fix problems. Please note - if you don't know how to create a folder or directory, this isn't the course for you. You first need to take a basic course in how to use a computer. Learning how to program one is more difficult than learning to use one, and learning to program in an object oriented, compiled language is a challenge for advanced users.
To survive in a C world, you have to understand certain things about a COMPILED language. First and formost, you can't just type it and run it like you may have done with basic. The program just doesn't run by itself. After you write the program, there is an order of events which must take place:
- You Write the Source Code
I'm sort of obligated at this point to tell you that I told a little white lie. You COULD feasably write the original source code in any text editor, like notepad or word, and simply save it as a "C" file such as " file1.c ". However, you would still need the compiler program to perform the next two steps. This is worthwhile knowing if you get stuck in an airplane, equipped with a laptop that doesn't have a compiler on it. You can still write the code while in flight, but you can't test it until you get somewhere with a compiler. In Assembly language, this same function was performed using an Editor and an Assembler.
- You Compile the Source Code into Object Code
Object code is sort of a combination of the source code you wrote, along with "header files" (we'll discuss them in a bit) written by someone else. In a Linux box, they are called .o files. In a Win-doze ( no typo there ) box they are called .obj files.
Object files are created by Compiling, Building, or Making them, depending on the program you are using. Check the User's Manual if you have questions. Object files are NOT complete and can not be run as is. In order to make the object code run, you have to LINK it.
- You Link the Object Code with other Codes to make the EXE Code.
When you LINK Object Code, you connect your object code to the ANSI Standard C Library, and other libraries to create an executable file - the actual 1's and 0's that your computer speaks.
In other words, for every program, no matter how simple or complex, there is a 4 step process that must happen:
- Write the program (source code)
- Compile the source code into object Code
- Link the object codes into executable programs
- Run and Test the Program to see if what you wrote will work.
Having been totally confused now, open up your Compiler program and begin to write a new file. For the sake of stupidity, we'll call the file file1.c
In formal tradition, we'll use the customary "Hello World" program. Here's the source code:
#include <stdlib.h>
#include <stdio.h>
void main()
printf ("Hello World\n");
For now, don't worry about how it works, or what the commands mean. I'll explain all that in lesson 3. Simply type it into your compiler program, then compile it, link it, and run it. That's the main purpose of lesson 2 - to teach you the process of creating an EXE file from source code. If it works, GREAT! If it doesn't try-try again. You could - theoretically pludge on through, but without checking to make sure your code works, have you really learned anything?
If you followed my directions and downloaded the OPEN WATCOM compiler, then the following instructions will help you through the compilation process. If you are lone gunning it through some other compiler program, I wish you luck, and you can skip this section and go on to page 3.
Using the OPEN WATCOM compiler:
After installing the program, feel free to look around at all the neat stuff that comes with it. Lots of documentation, helps, tools, and references to confuse you. When you're done with that, this is how to write, compile and link our program:
1) Open the TEXT EDITOR. [Start] {Programs] [Open Watcom C_C++][Text Editor]
2) Click [File] [New] to create a new source code file.
3) Type in the program above.
( Yes, you could cheat by cutting and pasting, but you tend to learn and retain more if you hard type it yourself.)
4) Click [File] [Save As] to SAVE the source code. When it asks for a name, call it hi.c (no reference to the fruit punch).
5) Now comes the tricky part. Hopefully you have SOME experience with command prompts, but if not, these instructions should carry you through.
Click [File] [System], and a command prompt (sometimes called a terminal window) screen will magically appear. You should find yourself in the C:\> directory.
6) Type into the command teminal window wcc hi.c [ENTER]
Your results should look something like this:

What happened here, is it took your source code, and using the Watcom C Compiler ( wcc ) it compiled your 103 byte hi.c source code file into a 358 byte hi.obj object file. It got bigger because it included bits and pieces from a couple of other programs ( stdlib.h and stdio.h ), and compiled, or assembled them together into one single file.
Now type in wlink [ENTER]Your results should look something like this:

Note that you are no longer at a "C:> prompt, but at a WLINK> prompt.
This indicates that you are running teh Watcom Linker program. One by one, type in the following commands, followed by the [ENTER] key.
WLINK> system dos [ENTER]
WLINK> option map [ENTER]
WLINK> name hi [ENTER]
WLINK> file hi.obj [ENTER]
Finally, type the TWO KEY combination
WLINK> [CTRL] Z
To exit the WLINK program and go back to the C prompt.

The outcome should have been:
"searching for libraries
creating map file
creating a dos executable"
If you now request a listing of all the files in the directory - dir [ENTER] , you will not that 2 new files have been generated. One is a 9,170 byte map file, and of course, our desired 8,702 byte hi.exe executable file.
Assuming everything went well, you can now hold your breath, and type in hi [ENTER] and see your newly made executable file in all it's glory. This is the final of the 4 steps - run and test. If anything in this series of steps didn't work out as planned, you must go back to the previous step and fix the problem. Usually, it's a problem with the source code, but it could be that your fingers got twisted as you were trying to type in the object name or something in the linking process.
Anyway - that is the basic process for writing, compiling, linking, and running an executable file. If you think it was a lot of work for the results - you are right. Let me say this again, just to be sure I've been noted: C is HARD.
Now on to page 3.
On The Following Indicator...
(
GREEN
will indicate your current location)
|
|
1
|
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|
10
|
[
ElectronicsTheory.Com
]
This Course was written by Ray Dall © All Rights Reserved.
This page and all it's content Copyright, Trademarks, Intellectual Properties
and other legal issues 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Ray Dall.
All Rights Reserved.
And for what it's worth... this page was last updated HexDate 01-11--7D1
VISITORS: