Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. /*
  2. * We are gonna learn how to use define with sharps(#).
  3. *
  4. * There are two examples below. First, We are gonna study the single sharp in define
  5. * TEST(x) is mapped to printf(#x "=%d\n", x). #x is expressed as letter.
  6. * so, the result of this function is x = 0~. as you know, serializing strings such
  7. * as "123" "456" is joined as one string "123456".
  8. *
  9. * Second, double sharps is the meaning of concaternating strings.
  10. * JOIN(x) is defined with printf("lim%d\n", (int)1##x). if I just put x 2,
  11. * inteager 2 casted the letter "2". and it becomes (int)12.
  12. * So, the result of this function is "lim12"
  13. */
  14.  
  15.  
  16.  
  17. #include <stdio.h>
  18.  
  19. #define TEST(x) printf(#x "=%d\n", x)
  20. #define JOIN(x) printf("lim%d\n", (int)1##x)
  21.  
  22.  
  23. int main(int argc, char** argv)
  24. {
  25. int i = 0;
  26. for (i = 0; i < 5; i++) {
  27. TEST(i);
  28. }
  29. JOIN(2); // <-- print "12"
  30. return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement