Assorted Topics
#include
The #include statements are used to include other files into this file. It is as if you had
typed all the lines in the included file into the this file. Usually these files are used
to hold global variable definitions and function prototypes.
#define
This is a kind of string substitution. It is often used in the same way as const variables.
The form is
#define MAXLEN 50
Anywhere in the program that the string MAXLEN appears in the program, it is replaced
by the string 50. The only places it doesn't do this substitution is in quoted strings
or comments.
parameters to main
The main() function takes two arguments. The first is an integer. It is always
called argc. It contains the number of arguments that were on the command line.
This includes the name of the program. The second argument is an array of character
pointers. This is always called argv. Each array element points to a character
array which contains one of the arguments on the command line. The first or 0 element
contains the name of the program. The last element of the array or the argc element
the value NULL. This is the marker of an empty pointer. It is not the same thing
as the NUL character used in strings.
switch
The switch statement is a shorthand for a nested if-then-else structure. The form looks like this.
switch(expr) {
case value : statement;
break;
default: statement;
break;
}
The expression part must resolve to an integer. Usually this is actually an integer or
a character. Each of the cases is a single value for the expression. If the actual
value of the expression matches the case, the statements are executed. The break marks
the end of the statements attached to the case. If you want to check the return
value of a function against several values, a switch is a good construct.
switch(foo(x)) {
case 1 : cout << "value is 1" << endl;
break;
case 2 : cout << "value is 2" << endl;
break;
default: cout << "some other value" << endl;
break;
} // switch
If you leave off a break statement, the control flows into the next case.