Thread: Quoting the contents of a #define

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    3

    Question Quoting the contents of a #define

    I want to print the contents of a VC Preprocessor Definition (essentially a #define set by the IDE - such as X=ABC).

    When I do the following:

    printf("%s", X);

    I get the compiler error:

    error C2065: 'ABC': undeclared identifier

    This makes sense to me... but the net effect that I want to acheive is

    printf("%s", "X"); /* Where the preprocessor define X resolves to value ABC */

    You might think why am I being so dense - by not simply hard code "ABC" since I'm setting this value statically within the VC IDE... The effect would be the same...

    Well - I need to compile this code on mulitple O/S'es and want the value of X to be set by my build script...

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use the "stringize" operator.
    Code:
    #define debug_long(x) \
        printf(#x " = %li\n", x)
    It turns an argument into a string representing the argument. Passing the above macro var would result in
    Code:
    printf("var" " = %li\n", var);
    or
    Code:
    printf("var = %li\n", var);
    [edit] See the section "Quoting the Macro Arguments" in this wikipedia entry: http://en.wikipedia.org/wiki/C_preprocessor [/edit]
    Last edited by dwks; 01-10-2007 at 02:24 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    3

    Question

    What you've offered will produce the following:

    "X" (the name of the #define) not "ABC" (the value of the #define)...

    I want the value contained within X to be stringized

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's exactly what happens, unless I'm misunderstanding you.
    Code:
    #define def(X) puts(#X)
    
    def(ABC);
    /* is the same as */
    puts("ABC");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    3

    Lightbulb Answer found... sorry to have trouble you...

    QUESTION
    How do I convert a constant passed to a #define macro into a text string?

    ANSWER
    Pass the parameter to a stringizing macro as shown below:

    #define NUMBER B85800 // this can be defined in the command line

    #define VAL(str) #str
    #define TOSTRING(str) VAL(str)

    unsigned char part1[10] = TOSTRING (NUMBER); // generates "B85800"
    unsigned char part2[10] = VAL(NUMBER); // generates "NUMBER"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing flags from a DLL?
    By RobotGymnast in forum C++ Programming
    Replies: 17
    Last Post: 10-27-2008, 01:34 PM
  2. Help me with function call
    By NeMewSys in forum C++ Programming
    Replies: 16
    Last Post: 05-22-2008, 01:53 PM
  3. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  4. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  5. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM