The following training material is available for students with limited knowlege of programming.
The traditional first program for C programmers is "Hello World".
// @(#) $Header: $
#include <stdio.h>
main()
{
printf("Hello, World\n");
}
This program should be written into a file and compiled. Let's call the program "hello" and therefore this file should be named "hello.c". You can write the program into hello.c with the editor of your choice.
Now you should compile it. I will assume you are on a Linux or other Unix system. If you are stuck with a MS Windows system you can get much of the needed capability with Cygwin.
To compile your program type
make helloat the shell prompt.
The "make" program responds with something like:
cc hello.c -o hello
This means that "make" has invoked the C compiler, "cc" on your source file "hello.c" and output the binary executable "hello". If there were no error message that resulted, the compile was successful and we should now have the "hello" program.
To run our new program, "hello", type
./helloat the shell prompt. It responds with
Hello, WorldWe run it with "./hello" rather than simply "hello" because the program is in the current directory and that directory may not be on our PATH.
You could add your current directory to your PATH variable. If you did you could then run "hello" without the leading "./". I recommend against this because it creates a security hole, making an easy path for Trojan Horses.
So far, so good. But how did it work?
Review the program we wrote. Line five performs the action.
printf("Hello, World\n")
"printf" is a standard C library function. The name
stands for "print formatted".
The backslash-n, "\n", at the end of our quoted
string gets turned into a newline.
That is why you didn't see it in the output.
But if you had left it out it might have messed up
the shell prompt after you ran "./hello".
Wrapped around the call to "printf" is "main(){}". This tells the C compiler what is the top level code of our program. Every program needs exactly one "main" definition.
Above that there is the cryptic line "#include <stdio.h>". This declares to the compiler that we will be using one or more functions from the Standard I/O library. That is where "printf" resides. If we left out this line the program might still compile but we might get a warning that the function "printf" has no prototype. This means that the compiler cannot check that we called printf correctly. Good programmers always remember to #include for each of their libraries.
What about that first line? Actually, it is a comment. All lines that start with "//" are comments and are ignored by the compler. I had us include that line because it is a flag to our revision control system (CVS, RCS, or Subversion) to record the revision of the file there. You should always maintain the habit of including that line near the top of your source files.
Granted the "hello" program doesn't really do anything useful. But it showed us several important things.
But what if we want to do something useful? For that we will probably need variables and functions and other capabilities which will be introduced below.
In algebra we were introduced to variables. These were numbers which could be any value, possibly all at once. In C we have something we call a variable, but it is somewhat different than what is in algebra.
You can think of a variable as a temporary storage place. For instance if I wanted to count to ten a variable would be a convenient place to keep that number. Lets write a program that does this.
// @(#) $Header: $
#include <stdio.h>
int main()
{
int count=0;
for(count=0; count<=10; count++)
{
printf("count is %d\n", count);
}
exit(0);
}
You should type the program above into the program source file "count.c". You can then compile it with "make count". Finally run it with "./count".
Just inside the definition of "main()" we have declared "int count=0;". This declares the variable "count" to be of type "int" and to have the initial value of 0. Type "int" is much like integer, but since it is stored in a computer word it is limited in size. On our Linux computer an int is 32 bits and is limited to the range from -2147483648 to 2147483647. On the FRC an int is 16 bits and is limited to the range from -32768 to 32767.
This shows a pitfall to debugging our programs under Linux when we will run them under FRC. If a variable declared as "int" reaches the value 33000, for instance, the program may work under Linux but fail on the FRC simply because that number cannot be represented on the FRC. We will develop programming methods to work around this problem.
The line "for(count=0; count<=10; count++)" says a series of things.
The code enclosed in the "{}" after the "for()" statement is the body of the loop controlled by "for". That code is the "printf". In this case it is executed 11 times, each time with a different value of "count".
We have called "printf" with two parameters this time. The first parameter is the format, "count is %d\n". The %d tells printf to expect a second parameter of type "int". So each time thru the loop the value of "count" is inserted into the printf output where the %d is written.
In this case the "main()" function has been declared as type int. This is the current standard for C programs. Unfortunately the compiler used for the FRC is out of date and will not accept that declaration. But this program is for Linux, and its compiler is current.
Finally, we have a call to "exit(0)" at the end of the program. This tells the compiler to return a 0 to the system when the program exits. A zero exit status is the standard way to indicate the program was successful. This is useful for most programming, but is not useful on the FRC since the FRC never exits.
I have placed a blank line between the declaration and the first executable statement of the program. This is not a requirement, but is there for readability. The blank line before the "exit" call is for the same reason.
if () { } else { } while () { } for () { } switch () { case xxx: break; }