20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The #def<strong>in</strong>e Statement<br />

305<br />

And if the program is written to use MAXIMUM_DATAVALUES <strong>in</strong> all cases where the size of<br />

the array was used, the preced<strong>in</strong>g def<strong>in</strong>ition could be the only statement <strong>in</strong> the program<br />

that would have to be changed.<br />

Program Portability<br />

Another nice use of the def<strong>in</strong>e is that it helps to make programs more portable from one<br />

computer system to another. At times, it might be necessary to use constant values that<br />

are related to the particular computer on which the program is runn<strong>in</strong>g.This might have<br />

to do with the use of a particular computer memory address, a filename, or the number<br />

of bits conta<strong>in</strong>ed <strong>in</strong> a computer word, for example.You will recall that your rotate<br />

function from Program 12.4 used the knowledge that an <strong>in</strong>t conta<strong>in</strong>ed 32 bits on the<br />

mach<strong>in</strong>e on which the program was executed.<br />

If you want to execute this program on a different mach<strong>in</strong>e, on which an <strong>in</strong>t conta<strong>in</strong>ed<br />

64 bits, the rotate function would not work correctly. 4 Study the follow<strong>in</strong>g<br />

code. In situations <strong>in</strong> which the program must be written to make use of mach<strong>in</strong>edependent<br />

values, it makes sense to isolate such dependencies from the program as much<br />

as possible.The #def<strong>in</strong>e statement can help significantly <strong>in</strong> this respect.The new version<br />

of the rotate function would be easier to port to another mach<strong>in</strong>e, even though it is a<br />

rather simple case <strong>in</strong> po<strong>in</strong>t. Here’s the new function:<br />

#<strong>in</strong>clude <br />

#def<strong>in</strong>e kIntSize 32 // *** mach<strong>in</strong>e dependent !!! ***<br />

// Function to rotate an unsigned <strong>in</strong>t left or right<br />

unsigned <strong>in</strong>t rotate (unsigned <strong>in</strong>t value, <strong>in</strong>t n)<br />

{<br />

unsigned <strong>in</strong>t result, bits;<br />

/* scale down the shift count to a def<strong>in</strong>ed range */<br />

if ( n > 0 )<br />

n = n % kIntSize;<br />

else<br />

n = -(-n % kIntSize);<br />

if ( n == 0 )<br />

result = value;<br />

4. Of course, you can write the rotate function so that it determ<strong>in</strong>es the number of bits <strong>in</strong> an<br />

<strong>in</strong>t by itself and, therefore, is completely mach<strong>in</strong>e <strong>in</strong>dependent. Refer back to exercises 3 and 4 at<br />

the end of Chapter 12,“Operations on Bits.”

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!