3.2 PROGRAM PARTS
C programs are basically simple text programs that follow a general set of rules (syntax). Figure 3.1 shows the classic beginners program that will add two numbers and print the result. The first line in this example is a comment. Comments are between '/*' and '*/' can stretch over many lines. Comments can also be the end of a line if they follow '//'. The 'main()' program declaration indicates where the program starts. The left and right curly brackets '{' and '}' are used to group together a set of program statements, in this case the program. Notice that the program statements are indented to indicate how they are grouped. This is a very valuable structuring technique that makes the programs much easier to read.
Figure 3.1 - A Program to Add Two Numbers (and results)
The program begins with the definition of three variables, 'x', 'y' and 'z'. All three are defined to be 'int' integers and the value of 'y' is set to '2'. The statement is terminated with a semicolon to separate it from the next statement. The next line assigns a value of '3' to 'x'. The following line adds the values of 'x' and 'y' and assigns the value to 'z'. The last statement in the program, 'printf', prints the values with a format statement. The first string in the command is a format string. In the string a '%d' indicates an integer, and '\n' indicates an end of line. The remaining characters in the format string are printed as shown. The format string is followed by the variables in the format string, in the same sequence. The result of the program shown that when it is run it prints '3 + 2 = 5'.
Some of the general rules that are worth noting are listed below.
The data types for C are listed below with typical data sizes. Sometimes these sizes may be larger or smaller. Their behavior of 'char', 'short', 'int' and 'long' can be modified when preceded with 'unsigned'. For example an integer 'x' defined with 'int x;' could have a value from -32768 to 32767, but if defined with 'unsigned int x;' it can have a value from 0 to 65535.
Beginners will often write a program in a single 'main' function. As the program becomes more complex it is useful to break the program into smaller functions. This makes it easier to write and read. Figure 3.2 contains an example program that uses subroutines to perform the same function as the program in Figure 3.1. As before the 'main()' program function is the starting point for program execution. The subroutine 'add' is defined after 'main', so a function prototype is required before. A prototype just indicates the values that are passed, and the function return type. In this example the values 3 and 2 are passed to the 'add' function. In the add function these values are then put into 'a' and 'b'. The values are then added and assigned to 'c'. The value of 'c' is then returned to the main function where it is assigned to 'z'.
Figure 3.2 - Program to add numbers with a function:
Every variable has a scope. This determines which functions are able to use that variable. If a variable is global, then it may be used by any function. These can be modified by the addition of static, extern and auto. If a variable is defined in a function, then it will be local to that function, and is not used by any other function. If the variable needs to be initialized every time the subroutine is called, this is an auto type. static variables can be used for a variable that must keep the value it had the last time the function was called. Using extern will allow the variable types from other parts of the program to be used in a function.
Figure 3.3 - Program example using global variables:
Other variable types of variables are union, enum and struct. Some basic control flow statements are while(), do-while(), for(), switch(), and if(). A couple of example programs are given below which demonstrate all the 'C' flow statements.
Figure 3.4 - Program example with a for loop:
Figure 3.5 - Examples of Other Loops
Figure 3.6 - An If-Else Example
Figure 3.7 - A Switch-Case Example
#include <filename.h> will insert the file named filename.h into the program. The *.h extension is used to indicate a header file which contains `C' code to define functions and constants. This almost always includes "stdio.h". As we saw before, a function must be defined (as with the `add' function). We did not define printf() before we used it, this is normally done by using #include <stdio.h> at the top of your programs. "stdio.h" contains a line which says `int printf();'. If we needed to use a math function like y = sin(x) we would have to also use #include <math.h>, or else the compiler would not know what type of value that sin() is supposed to return.
#define CONSTANT TEXT will do a direct replacement of CONSTANT in the program with TEXT, before compilation. #undef CONSTANT will undefine the CONSTANT.
Figure 3.8 - A More Complex Example
#fictive, #finder, #if, #else and #else can be used to conditionally include parts of a program. This is used for including and eliminating debugging lines in a program. Statements such as #define, #include, #fictive, #finder, #if, #else, /* and */ are all handled by the Preprocessor, before the compiler touches the program. Matrices are defined as shown in the example. In `C' there are no limits to the matrix size, or dimensions. Arrays may be any data type. Strings are stored as arrays of characters.
Figure 3.9 - Printing ASCII Values
Pointers are a very unique feature of `C'. First recall that each variable uses a real location in memory. The computer remembers where the location of that variable is, this memory of location is called a pointer. This pointer is always hidden from the programmer, and uses it only in the background. In `C', the pointer to a variable may be used. We may use some of the operations of `C' to get the variable that the pointer, points to. This allows us to deal with variables in a very powerful way.