Programming in C - Try It Out: An Example C Program
(Page 3 of 9 )
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 as hello.c. You can use whatever name you like instead of hello, 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 is cc.
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 your cc command. 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 .exe file 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 .exe file 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
Editing Your First Program You could try altering the same program to display something else on the screen. For example, you might want to try editing the program to read like this:
/* Program 1.2 Your Second C Program */
#include <stdio.h>
void main() {
printf("If at first you don\'t succeed, try, try, try again!");
}
The \' sequence in the middle of the text to be displayed is called an escape sequence. Here it’s a special way of including a single quote in the text. Because single quotes are used to indicate where a character constant begins and ends, you need a special way to indicate when you mean a single quote and not the start of a character constant. You’ll learn more about escapes sequences very soon. You can try recompiling the program, relinking it, and running it again once you’ve altered the source. With a following wind, and a bit of luck, you’ve now edited your first program. You’ve written a program using the editor, edited it, and then compiled, linked, and executed it.
This article is excerpted from Beginning C by Ivor Horton (Apress, 2004; ISBN 1590592530). Check it out at your favorite bookstore today. Buy this book now. |
Next: Dealing with Errors >>
More Code Examples Articles
More By Apress Publishing