Programming in C - Creating Your First Program
(Page 2 of 8 )
It’s time to create your first program. Let’s step through the processes of creating a simple C program, from entering the program itself to executing the program. Don’t worry if what you type doesn’t mean anything to you at this stage—I’ll explain everything as we go along.

Figure 1-1. Creating and executing a program
.........................................................................................
Try It Out: An Example C Program
Run your editor and type in the following program exactly as it’s written. Be careful to input the punctuation exactly as you see here. The brackets used on the fourth and last lines are braces—the curly ones {}, not the square ones[]or the round ones()—it really does matter. Also, make sure you put the slashes the right way (/), as later you’ll be using the backslash (\) as well. Don’t forget the semicolon (;).
Run your editor and type in the following program exactly as it’s written. Be careful to input the punctuation exactly as you see here. The brackets used on the fourth and last lines are braces—the curly ones {}, not the square ones [] or the round ones ()—it really does matter. Also, make sure you put the slashes the right way (/), as later you’ll be using the backslash (\) as well. Don’t forget the semicolon (;).
/* Program 1.1 Your Very First C Program - Displaying Hello World */
#include <stdio.h>
void main()
{
printf("Hello world");
}
When you’ve entered the preceding source code, save the program ashello.c. You can use whatever name you like instead ofhello, but the extension must be.c. This extension name is the common convention when you write C programs. The extension identifies the contents of the file as C source code. Most compilers will expect the source file to have the extension.c, and if it doesn’t, the compiler may refuse to process it.
Now you’re ready to compile your program. Exactly how you do this will depend upon which compiler you’re using. If your C compiler allows you to work in an IDE, then you should easily be able to find your way to a menu where you can select the Compile option. In UNIX, the command you would use iscc.
Next, you’ll link all the pieces necessary to create an executable program. This will add in code from the standard libraries that your program needs. Once again, the precise way to do this will depend upon which compiler system you’re using. If you’re using UNIX, then append any modules that you need to include at the end of yourcccommand. In an IDE, you’ll find the Link option on one of the menus. Remember that many IDEs offer a Build option that will compile and link your program automatically. If you aren’t working in an IDE, you should again consult your documentation to find out what the command is to run the linker.
Finally, you can execute your program. Remember that you can do this in several ways. There is the usual method of double-clicking the.exefile from Windows Explorer if you’re using Windows. You could also run your program from the command line. To do so, just start a command-line session, change the current directory to the one that contains the.exefile for your program, and then enter the program name to run it. Within an IDE, you’ll probably also have the option of running your program directly. Alternatively, there may be a Run menu that will compile, link, and execute the program in one go.
NOTE If you’re working in an IDE, you may need to use the Window menu to change to an Output window, where you can see the results of your program execution.
If everything worked without producing any error messages, you’ve done it! This is your first program, and you should see the following message on the screen:
---------------------------------------------------------------------- Hello world
----------------------------------------------------------------------
........................................................................................
Next: Editing Your First Program >>
More C++ Articles
More By Apress Publishing
|
This article is excerpted from the book Beginning C, third edition, written by Ivor Horton (Apress, 2004; ISBN: 1590592530). Check it out today at your favorite bookstore. Buy this book now.
|
|