1.3 PROGRAM PARTS

 

• /* is the start of a comment.

 

• */ is the end of comment.

 

• The main program is treated like a function, and thus it has the name main().

 

• lower/UPPER case is crucial, and can never be ignored.

 

• Statements are separated by semi-colons ‘;’

 

• Statements consist of one operation, or a set of statements between curly brackets {, }

 

• There are no line numbers.

 

 

• lines may be of any length.

 

• A very common function in ‘C’ is printf(). This function will do a formatted print. The format is the first thing which appears between the brackets. In this case the format says print an integer %d followed by a space then a ‘+’ then another space, another integer, another space, ‘=’, and another space, another integer, then a line feed ‘\n’. All variables that follow the format statement are those to be printed. x, y, and z are the three integers to be printed, in their respective orders.

 

• Major Data Types for variables and functions are (for IBM PC):

int (2 byte integer),

short (1 byte integer),

long (4 byte integer),

char (1 byte integer),

float (4 byte IEEE floating point standard),

double (8 byte IEEE floating point standard).

 

•int, short, long, char can be modified by the addition of unsigned, and register. An unsigned integer will not use 1 bit for number sign. A register variable will use a data register in the microprocessor, if possible, and it will speed things up (this is only available for integers).

 

 

• A function consists of a sub-routine or program, which has been assigned a name. This function is capable of accepting an argument list, and returning a single value. The function must be defined before it is called from within the program. (e.g. sin() and read()).

 

 

 

• 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.

 

 

 

• Other variable types of variables are union, enum, struct, etc.

 

• 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.

 

 

 

 

 

 

 

• #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.

 

 

• #ifdef, #ifndef, #if, #else and #else can be used to conditionally include parts of a program. This is use for including and eliminating debugging lines in a program.

 

• #define, #include, #ifdef, #ifndef, #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.

 

• i++ is the same as i = i + 1.

 

 

 

• 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.