Part 1.
We begin with program discussed in class. It contains two
preprocessor directives #define
used for two different purposess: the first one defines a
symbolic constants (cf.Bronson Sect 3.5) and the other allows the
programmer to
switch between two versions of a program with minimal effort.
It is common, although not required, to begin names of symbolic
constants with underscore.
In the present edition of the following program, the red part
is ignored by the compiler.
To switch to the other version, where the green part is
ignored but the red one is not, comment out the #definition of _DEBUG:
/* #define _DEBUG */
.
Using conditional compilation is convenient when you are debugging a program that has an interactive input (scanf) and you don't want to type in the values in the terminal window every time. (Bronson p.452 discusses a different application.)
/*******************************
M2120-Fall'05-Lab 4 - Part 1
Author: Sergey Sadov
Date: Thur/Fri Sept.29/30 2005
Lecture Sept 28 program: counting, scanf, #define
********************************/
#include<stdio.h>
#define _STEP 3
#define _DEBUG
/* This example shows how it is possible to "switch" from
one version of the code to a very different one
without re-typing. Enough to comment/uncomment the line #define _DEBUG
above.
*/
int main()
{
int n; /* counter, or accumulator */
int step; /* counting step, by which the counter is incremented every time */
#ifdef _DEBUG /*if _DEBUG is #define'd, the green part is compiled,
otherwise it is skipped as if it was commented out */
printf("Debug version\n");
step=_STEP;
printf("Using fixed step=%d\n",step);
#else /* if _DEBUG is #define'd, the red part is ignored,
otherwise it is compiled */
printf("Release version\n");
printf("Please enter the value of step: ");
scanf("%d", &step); /* Attention: ampersand before variable's name! */
printf("Using user-defined step=%d\n", step);
#endif /* End of switch depending on _DEBUG. The rest is compiled
unconditionally.*/
n=0; /* initialization of the counter */
printf("Before counting started: n=%d\n", n);
n+= step; /* incrementing n by step */
printf("New value of n=%d\n", n);
/*Once again*/:
n+= step; /* incrementing n by step */
printf("New value of n=%d\n", n);
printf("Done\n");
return(0);
}
Part 2:
The following program is our first encounter with loops.
For simplicity, the requisites of conditional compilation
are removed from the code.
/*******************************
M2120-Fall'05-Lab 4 - Part 2
Author: Sergey Sadov
Date: Thur/Fri Sept.29/30 2005
For-loop, speed test, integer overflow
********************************/
#include<stdio.h>
#define _STEP 10
/* See comments on www.math.mun.ca/~m2120/f2005/Lab4/lab4.html */
int main()
{
int n; /* counter, or accumulator */
int step; /* counting step, by which the counter is incremented every time */
int i; /* loop counter for the `for' loop */
printf("Debug version\n");
step=_STEP;
printf("Using fixed step=%d\n",step);
n=0; /* initialization of the counter */
printf("Before counting started: n=%d\n", n);
for(i=1; i<=100; i++)
/*the next two lines will be automatically repeated 100 times */
{
n+= step; /* incrementing n by step */
printf("New value of n=%d\n", n);
}
printf("Done\n");
return(0);
}
One four-loop in this program replaces what otherwise would be
100 copies of the two lines between the braces {}.