Thread: #define STR(ABC) #ABC ---> Question on how to run

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    3

    #define STR(ABC) #ABC ---> Question on how to run

    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!
    Last edited by Seokhyun Jeong; 04-10-2019 at 05:05 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,526
    You can do this.
    puts(STR((12, 23, 34)));

    But you end up with ( ) in the output as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    3
    Thank you for answer. To represent it as a string, I need to create one string.
    puts(STR("12, 23, 34")); ---> I also confirmed this.

    Thank you for answer.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    3
    There was an answer from the publisher yesterday.


    Please modify it as follows.
    It seems to be the code that was in the past compiler.

    STR (12, 33, 44) ---> STR (12 33 44)

    Thank you for informing us.



    So I decided to understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about a #define
    By Boxknife in forum C Programming
    Replies: 2
    Last Post: 07-04-2008, 09:15 AM
  2. #define question
    By haus in forum C Programming
    Replies: 1
    Last Post: 03-08-2003, 05:26 AM
  3. #define question
    By lakai02 in forum C Programming
    Replies: 2
    Last Post: 12-26-2002, 10:25 PM
  4. #define question
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-30-2001, 09:23 AM
  5. #define question
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-29-2001, 12:45 PM

Tags for this Thread