Programming in C

(Barry) #1

342 Chapter 15 Working with Larger Programs


// Date type
typedef struct date Date;

// Functions that work with dates
Date dateUpdate (Date today);
int numberOfDays (Date d);
bool isLeapYear (Date d);

// Macro to set a date in a structure
#define setDate(s,mm,dd,yy) s = (Date) {mm, dd, yy}

// External variable reference
extern Date todaysDate;
The header file defines two enumerated data types,kMonthand kDay, and the date struc-
ture (and note the use of the enumerated data types); uses typedefto create a type
called Date; and declares functions that use this type, a macro to set a date to specific val-
ues (using compound literals), and an external variable called todaysDate, that will pre-
sumably be set to today’s date (and is defined in one of the source files).
As an example using this header file, the following is a rewritten version of the
dateUpdatefunction from Chapter 9.
#include "date.h"

// Function to calculate tomorrow's date

Date dateUpdate (Date today)
{
Date tomorrow;

if ( today.day != numberOfDays (today) )
setDate (tomorrow, today.month, today.day + 1, today.year);
else if ( today.month == December ) // end of year
setDate (tomorrow, January, 1, today.year + 1);
else // end of month
setDate (tomorrow, today.month + 1, 1, today.year);

return tomorrow;
}.

Other Utilities for Working with Larger
Programs
As briefly mentioned previously, the IDE can be a powerful tool for working with larger
programs. If you still want to work from the command line, there are tools you might
Free download pdf