hello.
An error occurred in the code below. I have a question about it.
Code:
#include <stdio.h>
#define STR(ABC) #ABC
int main(void)
{
puts(STR(123));
puts(STR(12, 23, 34)); ---> error
putchar('\n');
return 0;
}
I am learning C language. In the book that I see, I explain the following.
#define STR(ABC) #ABC
eg)
STR(123) ---> "123"
STR(12, 23, 34) ---> "12, 23, 34"
I am learning C language with CentOS 7 and gcc 4.8.5. However, the following is an error.
================================================== ==================
puts(STR(12, 23, 34)); ---> error
test0140.c: In function ‘main’:
test0140.c:7:21: error: macro "STR" passed 3 arguments, but takes just 1
puts(STR(12, 23, 34));
^
test0140.c:7:7: error: ‘STR’ undeclared (first use in this function)
puts(STR(12, 23, 34));
^
test0140.c:7:7: note: each undeclared identifier is reported only once for each function it appears in
================================================== ==================
To fix the error, I changed the code as follows:
puts(STR(12));
or
#define STR(A, B, C) #A #B #C
puts(STR(12, 23, 34));
Is the question impossible to code originally shown in the book? Is the book simply wrong? (For others, compiling to Visual Studio on Windows will only print a warning.)
*** Contents of the book ***
#define STR(ABC) #ABC
eg)
STR(123) ---> "123"
STR(12, 23, 34) ---> "12, 23, 34"
This is the second question. Is it the use to distinguish "," in macro?
Last question. How do I include a "," in a string using a macro?
have a nice day!