RicardasSim icon

macro test

RicardasSim | PRO | 06/14/22 12:28:33 PM UTC (Edited) | 0 ⭐ | 804 👁️ | Never ⏰ | []
C |

1.77 KB

|

None

|

0 👍

/

0 👎

//linux, -Wall -Wextra -Wpedantic -Wshadow
 
#include <stdio.h>
#include <stdlib.h>
 
#define COLOR_RESET "\x1B[0m"
#define COLOR_RED "\x1B[31m"
#define COLOR_GREEN "\x1B[32m"
#define COLOR_YELLOW "\x1B[33m"
 
char g_info_msg[] = COLOR_GREEN "INFO: " COLOR_RESET;
char g_error_msg[] = COLOR_RED "ERROR: " COLOR_RESET;
char g_warning_msg[] = COLOR_YELLOW "WARNING: " COLOR_RESET;
 
#define PRINT_INFO_MESSAGE_1(...) printf( "%s", g_info_msg ); printf(__VA_ARGS__)
//#define PRINT_ERROR_MESSAGE_1(...) printf( "%s", g_error_msg ); printf(__VA_ARGS__)
//#define PRINT_WARNING_MESSAGE_1(...) printf( "%s", g_warning_msg ); printf(__VA_ARGS__)
 
#define PRINT_INFO_MESSAGE_2(...) { printf( "%s", g_info_msg ); printf(__VA_ARGS__); }
//#define PRINT_ERROR_MESSAGE_2(...) { printf( "%s", g_error_msg ); printf(__VA_ARGS__); }
//#define PRINT_WARNING_MESSAGE_2(...) { printf( "%s", g_warning_msg ); printf(__VA_ARGS__); }
 
int main()
{
 
    int test_var = 1;
    
    // note: some parts of macro expansion are not guarded by this ‘if’ clause
    if ( test_var )
        //warning: macro expands to multiple statements in expansion of macro ‘PRINT_INFO_MESSAGE_1’
        PRINT_INFO_MESSAGE_1("message 1\n");
    // error: ‘else’ without a previous ‘if’
    else    
        PRINT_INFO_MESSAGE_1("message 2\n");
    
    if ( test_var )
    {
        PRINT_INFO_MESSAGE_1("message 3\n");
    }
    else
    {
        PRINT_INFO_MESSAGE_1("message 4\n");
    }
    
    if ( test_var )
        PRINT_INFO_MESSAGE_2("message 5\n")
    else    
        PRINT_INFO_MESSAGE_2("message 6\n")
    
    if ( test_var )
    {
        PRINT_INFO_MESSAGE_2("message 7\n")
    }
    else
    {
        PRINT_INFO_MESSAGE_2("message 8\n")
    }
 
    return 0;
}

Comments