Error expanding macro

I tried to understand the macro extension and found out that the second printf was throwing an error. I expect the second print statement to generate the same result as the first. I know there are functions for string concatenation. It’s hard for me to understand why the first print statement works and the second doesn't.

#define CAT(str1, str2) str1 str2

void main()
{
    char *string_1 = "s1", *string_2 = "s2";
    printf(CAT("s1", "s2"));
    printf(CAT(string_1, string_2));
}
+4
source share
2 answers

Try manual preprocessing:

CATshould take 2 input variables and print them one by one with a space between them. So ... if we pre-process your code, it will become:

void main()
{
    char *string_1 = "s1", *string_2 = "s2";
    printf("s1" "s2");
    printf(string_1 string_2);
}

While "s1" "s2"automatically merging with the "s1s2"compiler, string_1 string_2 .

+1

, "s1" "s2", . , , string_1 string_2 .

, strcat, .

+8

Source: https://habr.com/ru/post/1614143/


All Articles