티스토리 뷰

[원글 - 2014년 이글루스]


stackoverflow 눈팅하다가 괜찮아 보여서 링크~


http://stackoverflow.com/questions/9104568/macro-vs-function-in-c


Example. 1

#define square(a) a * a

square(5)     // --> 5 * 5 --> 25
square(1 + 2) // --> 1 + 2 * 1 + 2 --> 1 + 2 + 2 --> 5
square(x++)   // --> x++ * x++ --> increments x twice

Example. 2

#define swap(x, y) t = x; x = y; y = t;

if (x < y) swap(x, y); // --> if (x < y) t = x; x = y; y = t; --> if (x < y) { t = x; } x = y; y = t;

Example. 3

#define print(x, y)  printf(x y)  /* accidentally forgot comma (콤마를 깜빡하면...) */
print("foo %s", "bar");           /* prints "foo %sbar" */

Example. 4

#define min(a, b) (a < b ? a : b)

min(x++, y) // --> (x++ < y ? x++ : y)
min(x & 0xFF, 42) // --> (x & 0xFF < 42 ? x & 0xFF : 42) --> (x & (0xFF < 42) ? x & 0xFF : 42)


댓글
최근에 올라온 글
Total
Today
Yesterday
최근에 달린 댓글
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31