fork(1) download
  1. // Literally equal to the #define MIN_(A, B) ((A) < (B) ? (A) : (B))
  2. #define MIN_(A, B) (\
  3.   (A) < (B) ? \
  4.   (A) : (B)\
  5.   )
  6.  
  7. // Using PRINT_ERROR_("%s they be stealin my %s!", "oh noes", "bucket")
  8. // will print "oh noes they be stealin my bucket" in to the standart error stream
  9. #define PRINT_ERROR_(...) fprintf(stderr, __VA_ARGS__)
  10.  
  11. #define HELLO_ "say hello to my little"
  12. #define SPACE_ " "
  13. #define WORLD_ "friend"
  14.  
  15. // Will be ptr. to the compile-time constant C str. "say hello to my little friend"
  16. static auto const HELLO_WORLD_ = HELLO_""SPACE_""WORLD_;
  17.  
  18. #define NUMBER_FROM_DIGITS_(D1, D2, D3) D1 ## D2 ## D3
  19.  
  20. static const auto MY_NUMBER_ = NUMBER_FROM_DIGITS_(1, 2, 3); // will be 123
  21.  
  22. #define MAKE_STR_(STR1, STR2, STR3) #STR1""#STR2""#STR3
  23.  
  24. // Will be ptr. to the compile-time constant C str. "paint_it_black"
  25. static auto const MY_STR_ = MAKE_STR_(paint, _it_, black);
  26.  
  27. #define M_S_(arg) #arg // stringify
  28. #define MAKE_STR__(arg) M_S_(arg)
  29.  
  30. // Will be ptr. to the compile-time constant C str. "+2,147,483,647" (for 32 bit) NOT "INT_MAX"!
  31. static auto const MY_STR__ = MAKE_STR__(INT_MAX);
  32.  
  33. int f() throw() {return 1;}
  34.  
  35. // Only expanded if appears with parentheses
  36. #define f() (f() + 1) // self-reference is NOT considered a macro call (passed unchanged)
  37.  
  38. static const auto VAL_1_ = f(); // will be 2
  39.  
  40. static auto const VAL_2_ = f; // will ptr. to the function
  41.  
  42. #define SUM(A, B) ((A) + (B))
  43. #define SUM(A, B) ((A) + (B)) // NO compile error here [the rule doesn't applied to macros]
  44.  
  45. #define SUM(A, B) ((A)+(B)) // compile warning here (allowed redefinition)
  46.  
  47. #define SUM(A, B) ( (A) + (B) )
  48. #define SUM(A, B) ( (A ) + ( B) ) // NO compile warning here (if spacers, their amount is ignored)
  49. #define SUM(A, B) ( (A ) /*what if*/ + /*add some comments here*/ ( B) ) // NO redefinition here: comments count as spacers
  50.  
  51. #define SUM(A, B) ((A) + (B))
  52.  
  53. // Will be 1 + 3 + 2 = 6
  54. static const auto VAL_ = SUM(1
  55. #undef SUM
  56. #define SUM + 3
  57. SUM, // new definition is used for argument pre-expansion
  58. 2); // original definition is used for argument replacement
  59.  
  60. #include <iostream>
  61.  
  62. int main() {
  63. std::cout << MIN_(1 + 2, 3 + 4) << std::endl; // prints 3
  64. PRINT_ERROR_("%s they be stealin my %s!", "oh noes", "bucket");
  65. std::cout << HELLO_WORLD_ << std::endl;
  66. std::cout << MY_NUMBER_ + 1 << std::endl; // prints 124
  67. std::cout << MY_STR_ << std::endl;
  68. std::cout << MY_STR__ << std::endl;
  69. std::cout << VAL_1_ << std::endl;
  70. std::cout << VAL_2_ + 10 << std::endl;
  71. std::cout << VAL_ << std::endl; // prints 6
  72.  
  73. return 0;
  74. }
Success #stdin #stdout #stderr 0s 3412KB
stdin
Standard input is empty
stdout
3
say hello to my little friend
124
paint_it_black
INT_MAX
2
1
6
stderr
oh noes they be stealin my bucket!