115_pdfsam_cs2022 - #define sum_sq(a, b) a*a + b*b int...

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

View Full Document Right Arrow Icon
Macros with arguments You can #define macros with parameters Looks like a function call, but it isn’t Like functions, macros make code easier to read But don’t have the overhead associated with function calls Compare: #define sum_sq(a,b) a*a + b*b int main() { double num1 = 3.0, ssq; ssq = sum_sq(num1, 5.0); return 0; } double sum_sq(double a, double b){ return a*a + b*b; } int main() { double num1 = 3.0, ssq; ssq = sum_sq(num1, 5.0); return 0; } Macro Function
Background image of page 1

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

View Full DocumentRight Arrow Icon
Macros with arguments Preprocessor literally does a search-and- replace operation before the code is compiled #include <stdio.h> #define sum_sq(a, b) a*a + b*b int main() { double num1 = 3.0, ssq; ssq = sum_sq(num1, 5.0); return 0; } /* . .Contents of stdio.h. . */ int main() { double num1 = 3.0, ssq; ssq = num1*num1 + 5.0*5.0; return 0; } Before Processing After Processing
Background image of page 2
Macro pitfalls “Search-and-replace” can be problematic It’s easy to write macros that have unintended consequences #include <stdio.h>
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

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

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

Unformatted text preview: #define sum_sq(a, b) a*a + b*b int main() { double num1 = 3.0, ssq; ssq = sum_sq(num+3, 5.0); return 0; } /* . .Contents of stdio.h. . */ Before Processing After Processing Macro pitfalls Search-and-replace can be problematic Its easy to write macros that have unintended consequences #include &lt;stdio.h&gt; #define sum_sq(a,b) (a)*(a) + (b)*(b) int main() { double num1 = 3.0, ssq; ssq = sum_sq(num+3, 5.0); return 0; } ssq = (num+3)*(num+3)+(5.0)*(5.0); Before Processing After Processing Macro pitfalls Search-and-replace can be problematic Its easy to write macros that have unintended consequences #include &lt;stdio.h&gt; #define sum_sq(a,b) (a)*(a) + (b)*(b) int main() { double num1 = 3.0, ssq; ssq = sum_sq( rand() , 5.0); return 0; } Before Processing After Processing More preprocessor features The preprocessor has many other features check K&amp;R for details...
View Full Document

This note was uploaded on 01/10/2010 for the course CS 2022 taught by Professor Crandall during the Spring '08 term at Cornell University (Engineering School).

Page1 / 6

115_pdfsam_cs2022 - #define sum_sq(a, b) a*a + b*b int...

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

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