Page 97 - Programming Microcontrollers in C
P. 97
82 Chapter 2 Advanced C Topics
types month_days as an array of 26 integers. This array is a
two-dimensional array of two rows of 13 columns each. The values
assigned are shown. The extra 0 entry at the beginning of each array
is to allow the conventional month designations 1 through 12 to be
used as indices and not have to worry about the fact that arrays in C
start with a 0 index.
The introduction of the program is normal. The function returns
an int and expects to receive three int arguments; one for the
month, one for the day of the month, and one for the year. Note that
the year must be the full year, like 2013, rather than merely 13. The
first executable statement is the logic statement:
leap = year%4==0 && year%100!=0 || year%400==0;
Leap years are usually every four years. However, a small discrepancy
still exists in the length of the year with the “once each four years”
correction. To further correct the error, the calendar makers have de
cided that years divisible by 100 will not be a leap year unless the year
is divisible by 400. The above statement is a logical statement that
determines first if the year is divisible by 4. If it is divisible by 4, it is
then checked to determine if it is divisible by 100. The result of this
much of the analysis will be TRUE for any year divisible by 4, and not
divisible by 100. If this portion of the calculation is TRUE, leap will
be assigned a value TRUE, or 1, and the evaluation will terminate. If
the result of the first portion of the calculation is FALSE, it will be
necessary to evaluate the last term to determine if the whole statement
is TRUE or FALSE. The variable leap will be assigned the result of
year%400==0
in this case.
leap is assigned a value of 1 or 0 according to the result of the
logic evaluation. This value can be used as an index into the two di
mensional array to determine if the number of month days in a leap
year or a nonleap year will be used in the calculation of the Julian date.
Pointers and Multidimensional Arrays
Perhaps one of the most widely misunderstood and therefore mys
terious aspects of pointers and C has to do with multidimensional
arrays. These problems are really not difficult. A multidimensional