

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This document, from the 15-410 course at carnegie mellon university, fall 2013, provides examples and explanations of macro programming in c, including define, precedence, and parentheses. It includes examples of common mistakes and how to avoid them.
Typology: Slides
1 / 25
This page cannot be seen from the preview
Don't miss anything!
Dave Eckhardt Dave Eckhardt Todd Mowry Todd Mowry L08a_define
“Don't make me stop the car...”
What's wrong with this picture? What's wrong with this picture?
From: Dave Eckhardt Subject: keyboard buffer size Keyboard buffers must handle somebody placing a cat on the keyboard (the 101-key keyboard).
Photo credit: Ivan Jager, 2006-10-
int scanbuf[64]; int getchar(void) { ... if (++bufindex == 256) bufindex = 0; ... }
#define KSB_SIZE 256 int scanbuf[KSB_SIZE]; int getchar(void) { ... if (++bufindex == KSB_SIZE) bufindex = 0; ... }
How to fix How to fix halfhalf of the problemof the problem % grep 64 *.c Wrong Wrong twotwo waysways (^) Won't find 63 (^) Will find random unrelated 64's
#define CENTER_X 40 #define CENTER_Y 12 ... set_cursor_pos(CENTER_Y, CENTER_X); ...
p->regsave[-18] |= 0x402; ... Don't make me stop the car...... Don't make me stop the car......
#define INC_TWICE(x) ++x; ++x // Note: only one ';'
#define INC_TWICE(x) ++x; ++x INC_TWICE(ac); ⇒ ++ac; ++ac;
#define INC_TWICE(x) ++x; ++x INC_TWICE(ac); ⇒ ++ac; ++ac; if (ac) INC_TWICE(ac);
#define INC_TWICE(x) ++x; ++x What to do? What to do? (^) See “multi-line macro”
#define TWICE(x) 2x TWICE(3) / 2*3 ⇒ 6 */