43_pdfsam_cs2022_lectures - #define #define creates macros...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: #define #define creates macros Syntax #define macro_name macro_value The preprocessor replaces macro_name with macro_value everywhere it appears in the program Typically used for defining constants #define CENT_TO_INCHES 2.54 #define SIZE 10 int main() { double inches[SIZE], centimeters[SIZE]; int j; /* read in inches ... */ for(j=0; j<SIZE; j++) centimeters[j] = CENT_TO_INCHES * inches[j]; return 0; } #define Advantages of macros Makes it easier to change constants Makes code easier to read No overhead associated with a variable A macro is not the same as a variable The preprocessor literally does a search-and- replace in your source code, before it is seen by the rest of the compiler Another use for #define Debugging #define DEBUG // Comment off for actual version int main() { ... #ifdef DEBUG printf(Value of i at this point is %d.\n,i); #endif ......
View Full Document

This note was uploaded on 01/10/2010 for the course CS 2022 at Cornell University (Engineering School).

Page1 / 6

43_pdfsam_cs2022_lectures - #define #define creates macros...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online