// Literally equal to the #define MIN_(A, B) ((A) < (B) ? (A) : (B))
#define MIN_(A, B) (\
(A) < (B) ? \
(A) : (B)\
)
// Using PRINT_ERROR_("%s they be stealin my %s!", "oh noes", "bucket")
// will print "oh noes they be stealin my bucket" in to the standart error stream
#define PRINT_ERROR_(...) fprintf(stderr, __VA_ARGS__)
#define HELLO_ "say hello to my little"
#define SPACE_ " "
#define WORLD_ "friend"
// Will be ptr. to the compile-time constant C str. "say hello to my little friend"
static auto const HELLO_WORLD_ = HELLO_""SPACE_""WORLD_;
#define NUMBER_FROM_DIGITS_(D1, D2, D3) D1 ## D2 ## D3
static const auto MY_NUMBER_ = NUMBER_FROM_DIGITS_(1, 2, 3); // will be 123
#define MAKE_STR_(STR1, STR2, STR3) #STR1""#STR2""#STR3
// Will be ptr. to the compile-time constant C str. "paint_it_black"
static auto const MY_STR_ = MAKE_STR_(paint, _it_, black);
#define M_S_(arg) #arg // stringify
#define MAKE_STR__(arg) M_S_(arg)
// Will be ptr. to the compile-time constant C str. "+2,147,483,647" (for 32 bit) NOT "INT_MAX"!
static auto const MY_STR__ = MAKE_STR__(INT_MAX);
int f() throw() {return 1;}
// Only expanded if appears with parentheses
#define f() (f() + 1) // self-reference is NOT considered a macro call (passed unchanged)
static const auto VAL_1_ = f(); // will be 2
static auto const VAL_2_ = f; // will ptr. to the function
#define SUM(A, B) ((A) + (B))
#define SUM(A, B) ((A) + (B)) // NO compile error here [the rule doesn't applied to macros]
#define SUM(A, B) ((A)+(B)) // compile warning here (allowed redefinition)
#define SUM(A, B) ( (A) + (B) )
#define SUM(A, B) ( (A ) + ( B) ) // NO compile warning here (if spacers, their amount is ignored)
#define SUM(A, B) ( (A ) /*what if*/ + /*add some comments here*/ ( B) ) // NO redefinition here: comments count as spacers
#define SUM(A, B) ((A) + (B))
// Will be 1 + 3 + 2 = 6
static const auto VAL_ = SUM(1
#undef SUM
#define SUM + 3
SUM, // new definition is used for argument pre-expansion
2); // original definition is used for argument replacement
#include <iostream>
int main() {
std::cout << MIN_(1 + 2, 3 + 4) << std::endl; // prints 3
PRINT_ERROR_("%s they be stealin my %s!", "oh noes", "bucket");
std::cout << HELLO_WORLD_ << std::endl;
std::cout << MY_NUMBER_ + 1 << std::endl; // prints 124
std::cout << MY_STR_ << std::endl;
std::cout << MY_STR__ << std::endl;
std::cout << VAL_1_ << std::endl;
std::cout << VAL_2_ + 10 << std::endl;
std::cout << VAL_ << std::endl; // prints 6
return 0;
}