1 #include <stdio.h> 2 #include <stdlib.h>
3
4
#define MONDAY 1
5
#define TUESDAY 2
6
#define WEDNESDAY 3
7
#define THURSDAY 4
8
#define FRIDAY 5
9
#define SATURDAY 6
10
#define SUNDAY 7 11 #define NO_DAY 8
12
13
int main(int argc, char *argv[])
14
{
15
int day = MONDAY;
16
for(day = MONDAY;day <= NO_DAY;day++)
17
{
18
switch(day)
19
{
20
case MONDAY:
21
printf("It's Monday, off to work\n");
22
break;
23
case TUESDAY:
24
printf("It's Tuesday, long week ahead\n");
25
break;
26
case WEDNESDAY:
27
printf("It's Wednesday, halfway there\n");
28
break;
29
case THURSDAY:
30
printf("It's Thursday, one more day to go\n");
31
break;
32
case FRIDAY:
33
printf("Thank Crunchie, it's Friday\n");
34
break;
35
case SATURDAY: /*This is an example of a drop through*/
36
case SUNDAY:
37
printf("It's the weekend, let's live it up\n");
38
break;
39
default:
40
printf("Hey! This day cannot exist\n");
41
}
42
}
43
system("PAUSE");
44
return 0;
45
}
Purpose of the program
This
program prints out each day of the
week with a little comment beside it. It demonstrates the usage of the switch
statement. It also demonstrates a drop through in line 35 which cause the same
message for Saturday and Sunday to be printed out.
Break down of the code
Line 1 - This line starts with
a '#' hash sign which indicates that it is a preprocessor directive. The
#include directive instructs the preprocessor to copy the contents of the
following file into this source code file at this point. The filename is
surrounded with '<>' brackets this instructs the preprocessor to look in the
system 'include' directory first for this file. So this line copies the contents
of the file 'stdio.h' into the source code file at this point before compiling
starts. The file 'stdio.h' contains the declaration of many basic input output functions
that are used in C programs.
Line 2 - This
line works in the same manner as line 1. The difference is that the preprocessor
copies the contents of the file 'stdlib.h' in to the source code at this point
before compiling starts. The file 'stdlib.h' contains the declaration of many
basic functions used by C programs.
Line 3 - Is
blank.
Line 4 - This
line also starts with a '#' hash sign so is again a preprocessor directive. This
one tells the preprocessor that everywhere it finds 'MONDAY' in the source code
replace it with the value 1.
Line 5 - Like
line 4 this one tells the preprocessor to replace all occurrences of 'TUESDAY' with
the value 2.
Line 6 - Like
line 4 this one tells the preprocessor to replace all occurrences of 'WEDNESDAY' with
the value 3.
Line 7 - Like
line 4 this one tells the preprocessor to replace all occurrences of 'THURSDAY' with
the value 4.
Line 8 - Like
line 4 this one tells the preprocessor to replace all occurrences of 'FRIDAY' with
the value 5.
Line 9 - Like
line 4 this one tells the preprocessor to replace all occurrences of 'SATURDAY' with
the value 6.
Line 10 - Like
line 4 this one tells the preprocessor to replace all occurrences of 'SUNDAY' with
the value 7.
Line 11 - Like
line 4 this one tells the preprocessor to replace all occurrences of 'NO_DAY' with
the value 8. This value is used in the for loop in line 16 as the value the loop
should terminate on.
Line 12 - Is
blank.
Line 13 - This
line contains the start of our definition of the main function. As you can see
main returns an int value. This value indicates to the operating system whether
or not the program has terminated normally. The main function also takes two
parameters these contain any command line options that are sent to you program
when it starts. E.g. if you start notepad from the command line you would write
notepad if you want to start notepad with a file to edit you would write notepad
readme.txt. readme.txt would be a command line option. This is not a subject
that will be considered further in the book so you may wish to find out more
yourself.
Line 14 - This
line contains the opening brace of the main function. There must be a closing
brace to match every opening brace and vice versa. The closing brace is on line
13.
Line 15 - This
line declares a variable of type integer and names it day. Then it sets the
value of day to be equal to MONDAY which will be replaced by the preprocessor
with the value 1.
Line 16 - In
this line we set up a for loop. The first argument is our new variable day. It
may seem superfluous to again assign the value MONDAY to the variable, but
getting in a habit of doing so helps avoid problems with uninitilised variables.
The second argument checks that the value of the variable is lower than NO_DAY
which is our terminating value. The third argument increments the value of the
variable, therefore making it equal to TUESDAY, WEDNESDAY, etc.
Line 17 - This
line contains the opening brace of the for loop block. There must be a closing
brace to match every opening brace and vice versa. The closing brace is on line
42.
Line 18 - This
line contains the switch statement. The value within the curved brackets is the
value which will be checked in the case part of the switch statement. In this
example we will be checking the value of the variable day. This of course will
be different each time the loop executes.
Line 19 - This
line contains the opening brace of the switch statement block. There must be a closing
brace to match every opening brace and vice versa. The closing brace is on line
41.
Line 20 - This
line contains the first case statement. If the value of the variable in the
switch statement is equal to the value specified in the case statement in the
case MONDAY ( or 1) then the statement(s) following the colon ':' will be
executed. If they are unequal then the next case statement will be checked.
Line 21 - This
line uses the function printf() which is found in the stdio.h header file. This
function is then instructed to print "It's Monday, off to work\n" on to the
screen. Since there is a new line marker '\n' after this any further
output will appear on the same line directly below this.
Line 22 - This
line contains the break statement. This is vital to remember, it you omit this
then the order of execution drops through to the next case statement. If we
omitted this then the program would print out the message in line 21, followed
by the message in line 24.
Line 23 - This
line contains a case statement. If the value of the variable in the switch
statement is equal to the value specified in the case statement in the case
TUESDAY ( or 2) then the statement(s) following the colon ':' will be executed.
If they are unequal then the next case statement will be checked.
Line 24 - This
line uses the function printf() which is found in the stdio.h header file. This
function is then instructed to print "It's Tuesday, long week ahead\n" on
to the screen. Since there is a new line marker '\n' after this any
further output will appear on the same line directly below this.
Line 25 - This
line contains the break statement. This is vital to remember, it you omit this
then the order of execution drops through to the next case statement.
Line 26 - This
line contains a case statement. If the value of the variable in the switch
statement is equal to the value specified in the case statement in the case
WEDNESDAY ( or 3) then the statement(s) following the colon ':' will be
executed. If they are unequal then the next case statement will be checked.
Line 27 - This
line uses the function printf() which is found in the stdio.h header file. This
function is then instructed to print "It's Wednesday, half way there\n" on
to the screen. Since there is a new line marker '\n' after this any
further output will appear on the same line directly below this.
Line 28 - This
line contains the break statement. This is vital to remember, it you omit this
then the order of execution drops through to the next case statement.
Line 29 - This
line contains a case statement. If the value of the variable in the switch
statement is equal to the value specified in the case statement in the case
THURSDAY ( or 4) then the statement(s) following the colon ':' will be executed.
If they are unequal then the next case statement will be checked.
Line 30 - This
line uses the function printf() which is found in the stdio.h header file. This
function is then instructed to print "It's Thursday, one more day to go\n"
on to the screen. Since there is a new line marker '\n' after this any
further output will appear on the same line directly below this.
Line 31 - This
line contains the break statement. This is vital to remember, it you omit this
then the order of execution drops through to the next case statement.
Line 32 - This
line contains a case statement. If the value of the variable in the switch
statement is equal to the value specified in the case statement in the case
FRIDAY ( or 5) then the statement(s) following the colon ':' will be executed.
If they are unequal then the next case statement will be checked.
Line 33 - This
line uses the function printf() which is found in the stdio.h header file. This
function is then instructed to print "Thank Crunchie, it's Friday\n" on to
the screen. Since there is a new line marker '\n' after this any further
output will appear on the same line directly below this.
Line 34 - This
line contains the break statement. This is vital to remember, it you omit this
then the order of execution drops through to the next case statement.
Line 35 - This
line contains a case statement. If the value of the variable in the switch
statement is equal to the value specified in the case statement in the case
SATURDAY ( or 6) then the statement(s) following the colon ':' will be executed.
If they are unequal then the next case statement will be checked. However since
there are no statements following the colon execute will drop through to the
next case statement. This causes the same value to be printed out for SATURDAY
and SUNDAY.
Line 36 - This
line contains a case statement. If the value of the variable in the switch
statement is equal to the value specified in the case statement in the case
SUNDAY ( or 7) then the statement(s) following the colon ':' will be executed.
If they are unequal then the next case statement will be checked, which will be
the default statement.
Line 37 - This
line uses the function printf() which is found in the stdio.h header file. This
function is then instructed to print "It's the weekend, let's live it up\n"
on to the screen. Since there is a new line marker '\n' after this any
further output will appear on the same line directly below this.
Line 38 - This
line contains the break statement. This is vital to remember, it you omit this
then the order of execution drops through to the next case statement.
Line 36 - This
line contains the default case statement. If the value of the variable in the
switch statement is not equal to any of the above case statements then the
statement(s) following the colon ':' will be executed. The default usually
exists to catch error values such as this example, values which you don't care
about or a set of values that require the same processing no matter what they
are.
Line 37 - This
line uses the function printf() which is found in the stdio.h header file. This
function is then instructed to print "Hey! This day cannot exist\n" on to
the screen. Since there is a new line marker '\n' after this any further
output will appear on the same line directly below this.
Line 41 - This
line contains the closing brace of the switch statement block.
Line 42 - This
line contains the closing brace of the for loop block.
Line 43 - The
function system("PAUSE") is used to pause the program until a key is pressed.
The reason for this is that under windows the console would open, the text would
be output to it then it would close. You would see little more than a flash on
the screen as this happened. Try commenting out the line to see this happen.
Line 44 - The
return statement is used at the end of functions to return an item of data. This
item of data should match the return type declared at the start of the function.
We are in the main function and as you can see in line 6 this should be an
integer. The return statement marks the end of the function any code after this
will not be executed your compiler will usually warn you of this when you
compile the program. The value 0 is a value that tells the
operating system that the program has terminated normally.
Line 45 - This
line contains the closing brace of the main function.