Module 1 - Programming Basics
The example code we'll be using is in the file module1.tar. You'll need to be at a computer running UNIX with a C compiler in order to access and run the programs in this file.
Once you download module1.tar, go to the directory where you saved it and uncompress it with the following command:
> tar -xvf module1.tar
This will place a subdirectory called module1 in the current directory, and in it you'll find the following files and folders:
README |
hello.c
|
productf.c |
sumf.c |
SCANF |
|
producti.c |
sumi.c |
The README file describes what each of these C programs does. SCANF is a subdirectory that contains some programs that read information in via user input or from a file.
Let's start the most basic of our examples, hello.c. This program simply prints out the sentence Hello Class on the screen. Here's what that program looks like:
#include <stdio.h>
main () {
printf("Hello Class\n");
}
Notice the following:
- The first line isn't part of the program; it tells the computer to include functions for handling input and output (stdio is short for standard input/output).
We'll talk more about includes, or library files and functions, later.
- The program starts with the word main, followed by opening and closing parentheses ( & ). We sometimes refer to these as "open paren" and "close paren" for short.
All C programs start with the word main. It is a reserved word in the C programming language that tells the computer that a set of instructions is coming up for the computer to perform.
The parentheses allow you to pass information (arguments) to the program that can be used during program execution. We aren't passing any initial data to this program, but the opening and closing parentheses must still be there.
We'll explain the usefullness of passing initial data to a program more fully in a later module.
- Immediately following the word main and the open and close parens is a left curly bracket {.
All of the instructions that the computer will perform must be included within this curly bracket and a corresponding right curly bracket } at the end of the program. We sometimes refer to these as "open curly" and "close curly", respectively.
- There is only 1 instruction for the computer to perform in this case. It is:
printf("Hello Class\n");
This syntax for a valid C instruction is also critical; it is made up of the following:
- A function name (Either one reserved by C - in this case printf - or defined in one of it's libraries, or a function written by you - more on these later),
- A set of opening and closing parentheses ( and ) that contain any data (or arguments) passed to the function, and
- A semicolon ";" to terminate the instruction (But not the program).
This semi-colon ; is a critical (And often overlooked) portion of the instruction, and you will forget this at least once in your C programming career.
Keeping in mind how important this bit of syntax is, and how easy it is to overlook, can save you hours of costly debugging time in the future, so be aware of it early, and hunt for it early and often we you need to debug a program.
- This function, printf, prints formatted data to the screen. It's defined in the include file <stdio.h> that the hello.c program requested at the beginning.
- As mentioned above, a right curly bracket } (That corresponds to the opening left curly bracket {) must be placed at the end of the program to indicate to the computer that processing of instructions terminates here.
This is also a critical syntactic feature, and one that is also easy to miss or forget, and often is.
- Immediately following the right curly bracket are comments by Dr. Impelluso, the program author, suggesting different things you can try to augment program and increase your understanding of how C programming works.
Comments are an invaluable part of C programming; they are often used to more fully explain the operation of complex code, or the usage of more arcane functions (Even if the comments are only for your own benefit, when you look back on the code after being away from it for awhile). They are there to be read by programmers, and are ignored by the computer.
Comments must be preceeded by the two characters /* (Forward-slash asterisk) can be many lines long, and must be proceed by */ (Asterisk forward-slash).
So this simple example program is a set of 1 instruction, which prints the words Hello Class to the screen, followed by a blank line (The \n portion of the argument passed to the printf command), and then terminates.
How useful is this program? Not very (Obviously). But it is presented here simply to show how a C program is constructed, and the rigid set of basic syntactic rules that must be followed in order for a program to be successfully compiled and executed.
What does it mean for a program to be compiled? Very briefly, the instructions you write in C are not actually run in that form by the computer; they must first be reduced to a set of machine code instructions that the computer can understand. This process of generating machine code from the C program is called program compilation, and is performed by an application called a C compiler. There are different C compilers for different machines; we will use a public version called the GNU C Compiler for this class.
Dr. Impelluso will explain more about the process of compilation in class.
Meanwhile, take a look at the comments at the end of hello.c, and take a look at - and then compile and run - the other program examples within the module before they are presented in class.
Next, go to the Apply section of this module to test yourself on how well you've understood the information we just covered.
Continue to the Apply section->