RicardasSim

macro test

Jun 14th, 2022 (edited)
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. //linux, -Wall -Wextra -Wpedantic -Wshadow
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define COLOR_RESET "\x1B[0m"
  7. #define COLOR_RED "\x1B[31m"
  8. #define COLOR_GREEN "\x1B[32m"
  9. #define COLOR_YELLOW "\x1B[33m"
  10.  
  11. char g_info_msg[] = COLOR_GREEN "INFO: " COLOR_RESET;
  12. char g_error_msg[] = COLOR_RED "ERROR: " COLOR_RESET;
  13. char g_warning_msg[] = COLOR_YELLOW "WARNING: " COLOR_RESET;
  14.  
  15. #define PRINT_INFO_MESSAGE_1(...) printf( "%s", g_info_msg ); printf(__VA_ARGS__)
  16. //#define PRINT_ERROR_MESSAGE_1(...) printf( "%s", g_error_msg ); printf(__VA_ARGS__)
  17. //#define PRINT_WARNING_MESSAGE_1(...) printf( "%s", g_warning_msg ); printf(__VA_ARGS__)
  18.  
  19. #define PRINT_INFO_MESSAGE_2(...) { printf( "%s", g_info_msg ); printf(__VA_ARGS__); }
  20. //#define PRINT_ERROR_MESSAGE_2(...) { printf( "%s", g_error_msg ); printf(__VA_ARGS__); }
  21. //#define PRINT_WARNING_MESSAGE_2(...) { printf( "%s", g_warning_msg ); printf(__VA_ARGS__); }
  22.  
  23. int main()
  24. {
  25.  
  26.     int test_var = 1;
  27.    
  28.     // note: some parts of macro expansion are not guarded by this ‘if’ clause
  29.     if ( test_var )
  30.         //warning: macro expands to multiple statements in expansion of macro ‘PRINT_INFO_MESSAGE_1’
  31.         PRINT_INFO_MESSAGE_1("message 1\n");
  32.     // error: ‘else’ without a previous ‘if’
  33.     else    
  34.         PRINT_INFO_MESSAGE_1("message 2\n");
  35.    
  36.     if ( test_var )
  37.     {
  38.         PRINT_INFO_MESSAGE_1("message 3\n");
  39.     }
  40.     else
  41.     {
  42.         PRINT_INFO_MESSAGE_1("message 4\n");
  43.     }
  44.    
  45.     if ( test_var )
  46.         PRINT_INFO_MESSAGE_2("message 5\n")
  47.     else    
  48.         PRINT_INFO_MESSAGE_2("message 6\n")
  49.    
  50.     if ( test_var )
  51.     {
  52.         PRINT_INFO_MESSAGE_2("message 7\n")
  53.     }
  54.     else
  55.     {
  56.         PRINT_INFO_MESSAGE_2("message 8\n")
  57.     }
  58.  
  59.     return 0;
  60. }
Add Comment
Please, Sign In to add comment