Programming in C - First Program
Assuming the program you wrote compiled correctly, you are now ready to go on. If not, you should go back and look for mistakes in your code. Typical problems include:
Forgetting the ' ; ' at the end of a line.
Not putting the () after main, or forgetting the word void.
Other simple typographical errors.
#include <stdlib.h>
#include <stdio.h>
void main()
printf ("Hello World\n");
Let's begin with the first line:
#include <stdlib.h>
This command tells the compiler that this is not a "standalone" program, but that it must be associated with the program 'stdlib.h' before it will run properly. 'stdlib.h' is the STANDARD LIBRARY HEADER file that contains many of the command functions, including the 'exit(0)' function which is used in this file. There are many header files and libraries used in C, C++, and Java, which are covered in the ANSI/ISO standards. You must become familiar with the various standard header files that are used by the C language. A good reference for these is the "C/C++ Programmer's Reference by Herbert Schildt (Osborne Press)".
For now, though, it is sufficient to know that they exist, and can be attached as part of your program by using the command '#include'. Note also, that the stdlib.h is surrounded by greater and less than signs <>, commonly called 'gator' symbols in the programming world. I dunno why they are called gators, but it's easer to say than 'greater than'. Forgetting to use them is a common syntax problem.
Similarly, the next line:
#include <stdio.h>
is a call to use the Standard Input / Output header, which tells the computer that you want to use the standard output - the monitor. Other outputs could be a printer, a plotter, a network card, a communications port, etc. The Standard Input, by the way, is the keyboard. Other inputs could be a mouse, a network card, a com port, etc. In this case we will be using the 'printf' function to print out to a DOS shell type window on the monitor.
#include <stdlib.h>
#include <stdio.h>
void main()
printf ("Hello World\n");
The next command is the 'void main()' command. This tells the computer that this is the main portion of the program, and that it returns a void ( zero, zilch, nada, nothing ). You see, all programs return something. For instance, a small program might take two inputs ( A and B ), add them ( A+B=C ), then it has to RETURN the "C" to another program. In this program, all we are doing is printing something, so we really don't need to pass (return) anything on to any other programs. We say that the return is void, and at the end of the program we 'exit ( 0 ). Note that after 'main' we put open and close parenthesis (). Forgetting these is a common syntax error.
After the 'void main()' command we have the main portion of the program. This portion of the program MUST be surrounded by "curley braces" or { } symbols. Everything within the curley braces is assumed by the computer to be the portion of the program within the parenthesis of the main statement..... or to be the main portion of the program ( hope I didn't lose you there. )
#include <stdlib.h>
#include <stdio.h>
void main()
printf ("Hello World\n");
So now we are inside the curley braces, and into the meat of the program. This program has 2 simple commands:
printf ("Hello World\n");
exit(0);
Note that everything within the quotation marks " " is what is printed. The \n is printed too, but has a special meaning. The \ symbol is called the 'escape' symbol, and sort of gets you out of normal mode. In this case, it escapes you out of normal print mode, and performs the 'n' function instead of printing the letter n. The n function is 'next line' or 'new line'. So \n means "stop printing normal characters and do a carriage return."
Finally, we come to the 'exit(0)' command. It says "let's end the program, get out, and don't return anything to any other programs." The exit(0) and printf commands are functions found within the stdlib.h header file.
One final note on this little program:
EVERY LINE ENDS IN A SEMICOLON !!! " ; "
The semicolon tell the program that it is the end of the program line. In some programming languages, various programming lines are determined by a line number at the beginning of each line. C does not use line numbers because small programs ( like this one ) are rag-tag-tattered together to form one big one, and who knows what line number a certain line will have in the "big picture".
Time for your first practical exercise -
Write a NEW program that will print out your name and address, as if it were a mailing label. Call the program "address.c" We may actually make use of this simple program later.
DO NOT go on to page 4 until you have at least attempted at writing the source code of the address.c program.
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: