fork(1) download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. #define PRINT1(...) printf(__VA_ARGS__)
  7. #define PRINT2(X, ...) printf("Message "#X": ", ##__VA_ARGS__)
  8.  
  9. #define PRINT3(...) printf(##__VA_ARGS__)
  10. #define PRINT4(X, ...) printf("Message "#X": " ##__VA_ARGS__)
  11.  
  12. int main()
  13. {
  14. //__VA_ARGS__
  15. double a = 40;
  16. double b = sqrt(a);
  17. PRINT1("Hello~ \n"); //Hello~
  18. PRINT1("a = %f, b = %.3f \n", a, b); //a = 40.000000, b = 6.325
  19. PRINT2(0); //Message 0:
  20. PRINT2(1, "a = %.2f, b = %.4f\n", a, b); //why??? Expect: Message 1: a = 40.00, b = 6.3246; Result: Message 1:
  21. printf("\n\n\n");
  22.  
  23. //##__VA_ARGS__
  24. //PRINT3("Hello~ \n"); //Compile error, why???
  25. //PRINT3("a = %f, b = %.3f \n", a, b); //Compile error, why???
  26. PRINT4(0); //Message 0:
  27. //PRINT4(1, "a = %.2f, b = %.4f\n", a, b); //Compile error, why???
  28.  
  29. return 0;
  30.  
  31. }
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
Hello~ 
a = 40.000000, b = 6.325 
Message 0: Message 1: 


Message 0: