LEC20081031 - Introduction to Computer Programming October...

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

View Full Document Right Arrow Icon
Introduction to Computer Programming October 31, 2008 CSC180 Fall 2008, University of Toronto
Background image of page 1

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

View Full DocumentRight Arrow Icon
The # operator Macro parameters are not substituted within quoted strings: #define dprint1(expr) printf(‘‘expr = %g\n’’, expr) dprint1(0.5/2); /* print expr = 0.25 */ # converts a macro parameter into a string literal: #define dprint2(expr) printf(#expr ‘‘ = %g\n’’, expr) dprint2(0.5/2); /* print 0.5/2 = 0.25 */ Example: CSC180 Fall 2008, University of Toronto 1
Background image of page 2
#define tempfile(dir) #dir ‘‘/%s’’ char path[256]; sprintf(path, tempfile(/usr/tmp), ‘‘temp.txt’’); printf(‘‘%s\n’’, path); /* print /usr/tmp/temp.txt */ Example: #define str(s) #s #define xstr(s) str(s) #define MSG hello printf(‘‘%s\n’’, str(MSG)); /* print MSG */ printf(‘‘%s\n’’, xstr(MSG)); /* print hello */ Macro parameters following # in the replacement text are not expanded. xstr does not involve the # operator so its parameters can expand. CSC180 Fall 2008, University of Toronto 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
The ## operator ## concatenates actual arguments during macro expansion: #define paste(front, back) front ## back double paste(a, 2) = paste(0., 25); /* int a2 = 0.25; */ printf(‘‘%f’’, a2); Example: #include <stdio.h> #define lenof(x) (sizeof((x))/sizeof(*(x))) #define t(x, y, z) x ## y ## z CSC180 Fall 2008, University of Toronto 3
Background image of page 4
{ int i; int a[] = {t(1,2,3), t(,4,5), t(,,6), t(,,)}; for (i = 0; i < lenof(a); i++) printf(‘‘%d ‘‘, a[i]); /* print 123 45 6 */ printf(‘‘\n’’); return 0; } Example: #include <stdio.h>
Background image of page 5

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

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

Page1 / 19

LEC20081031 - Introduction to Computer Programming October...

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