to get macro name from macro value

Discussion in 'C Programming' started by sounak, Nov 22, 2005.

  1. sounak

    sounak Guest

    How could we get a macro name from a macro value
    such that
    in a header file
    #define a 100
    #define b 200
    now the source file will be such that
    the user gives 100
    then the value is outputted as a
    the user gives 200
    then the value is outputted as b


    is it possible
    thanks in advance
    regards
    sounak
     
    sounak, Nov 22, 2005
    #1
    1. Advertising

  2. sounak

    Marc Boyer Guest

    Le 22-11-2005, sounak <> a écrit :
    > How could we get a macro name from a macro value
    > such that
    > in a header file
    > #define a 100
    > #define b 200
    > now the source file will be such that
    > the user gives 100
    > then the value is outputted as a
    > the user gives 200
    > then the value is outputted as b
    >
    > is it possible


    What did you really need ? What would be the
    result with:
    #define a 100
    #define b 200
    #define c ((b)-(a))
    #define d (a<<1)
    #define e 100

    Marc Boyer
     
    Marc Boyer, Nov 22, 2005
    #2
    1. Advertising

  3. sounak

    Richard Bos Guest

    Marc Boyer <> wrote:

    > Le 22-11-2005, sounak <> a écrit :
    > > How could we get a macro name from a macro value
    > > such that
    > > in a header file
    > > #define a 100
    > > #define b 200
    > > now the source file will be such that
    > > the user gives 100
    > > then the value is outputted as a
    > > the user gives 200
    > > then the value is outputted as b
    > >
    > > is it possible

    >
    > What did you really need ? What would be the
    > result with:
    > #define a 100
    > #define b 200
    > #define c ((b)-(a))
    > #define d (a<<1)
    > #define e 100


    ....and the user enters 350?

    Richard
     
    Richard Bos, Nov 22, 2005
    #3
  4. sounak

    sounak Guest

    i just wanted to get the macro name of the specified macro value
    just the opposite what macro does
    #define a 100
    we specify 100 we get output as a
    sounak
     
    sounak, Nov 22, 2005
    #4
  5. sounak

    Marc Boyer Guest

    Le 22-11-2005, sounak <> a écrit :
    > i just wanted to get the macro name of the specified macro value
    > just the opposite what macro does
    > #define a 100
    > we specify 100 we get output as a


    Please, answer my questions ? What if two macros
    have the same value ?

    Marc Boyer
     
    Marc Boyer, Nov 22, 2005
    #5
  6. sounak

    sounak Guest

    i need to get the macro name from the value
    like suppose
    #define a 100

    if now i give input to a function as 100 then a should be
    returned
    just the opposite of the defination we see in macro

    do we have to access the sysbol table
    if yes then how could we do that
    and is there any other way to get the macro name
    regards
    sounak
     
    sounak, Nov 22, 2005
    #6
  7. sounak

    Chris Dollin Guest

    sounak wrote:

    > i need to get the macro name from the value
    > like suppose
    > #define a 100
    >
    > if now i give input to a function as 100 then a should be
    > returned
    > just the opposite of the defination we see in macro


    C does not support this. If you want to run the mapping backwards,
    you have to do it yourself.

    > do we have to access the sysbol table


    When the program runs, there is no symbol table [1].

    If you want to do this - and /why/ do you want to do this? - you
    can create your own table:

    static struct { int value; char *name; } backwards =
    {
    { A, "A" },
    { B, "B" },
    // and so on, for all the macros you want to do it to
    { 0, 0 }
    };

    Now you can look up the value and find the corresponding string.

    [1] Not one that can be portably accessed by an ANSI C program.
    In any case, usually the #defines have gone before we have
    what's usually called a "symbol table".

    --
    Chris "another virtual machine" Dollin
    missing tests don't fail.
     
    Chris Dollin, Nov 22, 2005
    #7
  8. sounak

    sounak Guest

    if 2 macros have same value then 2 name will be shown
     
    sounak, Nov 22, 2005
    #8
  9. sounak

    sounak Guest

    tell me one thing
    while compiling the compiler replaces the values of the defined macros
    after then the table is gone ?


    now to get the values at run time
    we can do one thing
    parse the compiled file
    and find out #define statement
    and store the values in some structure as you told
    and then show the values
    Is it this thing you are saying?
    sounak
     
    sounak, Nov 22, 2005
    #9
  10. sounak

    pete Guest

    sounak wrote:
    >
    > tell me one thing
    > while compiling the compiler replaces the values of the defined macros
    > after then the table is gone ?
    >
    > now to get the values at run time
    > we can do one thing
    > parse the compiled file
    > and find out #define statement


    There are no macros in a translation unit.

    --
    pete
     
    pete, Nov 22, 2005
    #10
  11. sounak

    Chris Dollin Guest

    Chris Dollin wrote:

    > static struct { int value; char *name; } backwards =


    Oops. s/backwards/backwards[]/.

    --
    Chris "another virtual machine" Dollin
    missing tests don't fail.
     
    Chris Dollin, Nov 22, 2005
    #11
  12. sounak

    Jordan Abel Guest

    On 2005-11-22, Chris Dollin <> wrote:
    >
    > When the program runs, there is no symbol table [1].
    >
    > [1] Not one that can be portably accessed by an ANSI C program.
    > In any case, usually the #defines have gone before we have
    > what's usually called a "symbol table".


    Well, the preprocessor maintains its own construct that might reasonably
    be called a "symbol table", but that's not even available non-portably
    at execution time, let alone portably.
     
    Jordan Abel, Nov 22, 2005
    #12
  13. sounak

    Chris Dollin Guest

    Jordan Abel wrote:

    > On 2005-11-22, Chris Dollin <> wrote:
    >>
    >> When the program runs, there is no symbol table [1].
    >>
    >> [1] Not one that can be portably accessed by an ANSI C program.
    >> In any case, usually the #defines have gone before we have
    >> what's usually called a "symbol table".

    >
    > Well, the preprocessor maintains its own construct that might reasonably
    > be called a "symbol table",


    It might, but I wouldn't, only because I expect a "symbol table" to
    contain information about all the symbols in a translation unit,
    and the #defines table thingy only holds the #defines and nothing
    about eg the variables of the program. Hence the weasel.

    --
    Chris "pop11 goes the weasel" Dollin
    missing tests don't fail.
     
    Chris Dollin, Nov 22, 2005
    #13
  14. sounak

    mazsx Guest

    sounak wrote:
    > How could we get a macro name from a macro value
    > such that
    > in a header file
    > #define a 100
    > #define b 200
    > now the source file will be such that
    > the user gives 100
    > then the value is outputted as a
    > the user gives 200
    > then the value is outputted as b
    >
    >
    > is it possible
    > thanks in advance
    > sounak


    As the folks explained, it is not possible in a portable way.
    But I do think, that non-portable hooks exist.
    Consider the following code under a GNU/Linux system.
    (So this post might be off-topic. I would still like to refer to the
    enum constants and the creation of symbol table entries that
    my can be useful for the original questioner.)

    /* contains non-portable code */
    /* compilation (if called macroback.c)
    * gcc -O -rdynamic -ansi -pedantic -Wall macroback.c -ldl -o
    macroback
    */

    #include <stdio.h>
    #define concat(a,b) a##b
    #define my_define(name,value) char *concat(const_id_,value)=#name; \
    enum { name = value }


    /* values should be non-negative integer constants */

    my_define(A,12);
    my_define(B,11);

    const char **symbol_access(const char *name);

    int main()
    {
    char array[A];
    int i;
    printf("A=%d works\n",sizeof array);
    printf("B=%d works\n",B);
    while(scanf("%d",&i)==1) {
    char name[16];
    const char **found;
    sprintf(name,"const_id_%d",i);
    if((found=symbol_access(name))) printf("%s=%d\n",*found,i);
    else printf("Value %i not found\n",i);
    }
    return 0;
    }

    /* OS dependent part, should work under GNU linux */

    #include <dlfcn.h>
    const char **symbol_access(const char *symbol_name)
    {
    static void * handle;
    if(handle || (handle=dlopen(NULL,RTLD_LAZY)))
    return dlsym(handle,symbol_name);
    fprintf(stderr,"Could not access symbol table: %s\n",dlerror());
    return NULL;
    }

    mazsx
     
    mazsx, Nov 22, 2005
    #14
  15. "sounak" <> writes:
    > How could we get a macro name from a macro value
    > such that
    > in a header file
    > #define a 100
    > #define b 200
    > now the source file will be such that
    > the user gives 100
    > then the value is outputted as a
    > the user gives 200
    > then the value is outputted as b


    The first thing you should do is learn how to post proper followups
    (this is in reference to your other followups in this thread). Don't
    assume that everyone can see the article to which you're replying.
    You need to provide some context, as I've done here, so each article
    can be read on its own. Google makes it gratuitously difficult to do
    this, but there is a workaround (that's been posted here over 1000
    times).

    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.

    As for your question, the answer is that there's no way to do what
    you're asking with macros, because that's not what macros are for.
    You can build a table mapping strings to numbers, and look up and
    display which strings (if any) are mapped to a specified number; it
    just doesn't make any sense to use macro definitions for this purpose.

    What are you really trying to accomplish, and why do you think that
    macro definitions should be part of the solution?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Nov 22, 2005
    #15
  16. sounak

    Thad Smith Guest

    sounak wrote:
    >
    > i need to get the macro name from the value
    > like suppose
    > #define a 100
    >
    > if now i give input to a function as 100 then a should be
    > returned just the opposite of the defination we see in macro
    >
    > do we have to access the sysbol table
    > if yes then how could we do that
    > and is there any other way to get the macro name


    Normally, I would construct my own table, as Chris Dollin has
    suggested, for the symbols that I want to use.

    If you really want to automatically search from macros defined in C
    source and don't want to construct your own table manually, as
    suggested, you can write a parser to read the relevant C source,
    extract the macro names and replacement text, place them into a table
    containing both name and value, then compile that module and link into
    your other code.

    The relevant question is: why are you attempting to do this from
    existing macro definitions, as opposed to constructing your code to
    meet your project requirements?

    --
    Thad
     
    Thad Smith, Nov 22, 2005
    #16
  17. sounak

    Mike Wahler Guest

    "Keith Thompson" <> wrote in message
    news:...
    > "sounak" <> writes:
    >> How could we get a macro name from a macro value
    >> such that
    >> in a header file
    >> #define a 100
    >> #define b 200
    >> now the source file will be such that
    >> the user gives 100
    >> then the value is outputted as a
    >> the user gives 200
    >> then the value is outputted as b

    >
    > The first thing you should do is learn how to post proper followups
    > (this is in reference to your other followups in this thread). Don't
    > assume that everyone can see the article to which you're replying.
    > You need to provide some context, as I've done here, so each article
    > can be read on its own. Google makes it gratuitously difficult to do
    > this, but there is a workaround (that's been posted here over 1000
    > times).
    >
    > If you want to post a followup via groups.google.com, don't use
    > the broken "Reply" link at the bottom of the article. Click on
    > "show options" at the top of the article, then click on the
    > "Reply" at the bottom of the article headers.
    >
    > As for your question, the answer is that there's no way to do what
    > you're asking with macros, because that's not what macros are for.
    > You can build a table mapping strings to numbers, and look up and
    > display which strings (if any) are mapped to a specified number; it
    > just doesn't make any sense to use macro definitions for this purpose.
    >
    > What are you really trying to accomplish, and why do you think that
    > macro definitions should be part of the solution?


    I have done something like what OP is asking about when learning
    Windows programming, to watch Windows messages and have their
    macro names visible (e.g. 'WM_PAINT', 'BN_CLICKED', etc.).

    I created a little utility that scanned the appropriate headers
    and created a C source file containing tables which associated
    the macro names to their values, and #included that in my application.

    There are existing 'Spy' programs that can do this message watching
    (one comes with VC++), I did mine so that I could integrate the 'spying'
    with the application rather than switching between it and a separate 'spy'
    program, and admittedly also for fun.

    Later I added this table building to the application logic itself, making
    it 'dynamic' (searching the headers and adding table entries as needed
    during lookup).

    Doing things like this does have an inherent 'fragility', as headers can
    change (and/or change locations/availability) over time, so one must
    ensure the availability (and correct version) of the headers, and make
    sure the same header is used during compilation and execution. Then
    there are the issues of macros inside #if/#endif etc. and those inside
    comments. It would be quite a large chore to take all that into account,
    I only went as far with that as needed to meet my immediate needs
    (IOW "I cheated a bit" :) )

    Elsethread folks warned about e.g. macros defined in terms of others
    (that did happen a bit with my specific example), and different macros
    producing the same values. So of course context must be considered (e.g.
    in my example, many of the Windows control notification macros have the
    same values, so lookup of the correct macro needs more context information
    (such as a window class name).

    Anyway, my point is that while it's 'uncommon', I don't think what OP
    wants is completely unthinkable.

    But as you say, we need OP to state a specific problem in order to
    offer the most appropriate advice.

    -Mike
     
    Mike Wahler, Nov 22, 2005
    #17
  18. On 22 Nov 2005 01:13:35 -0800, in comp.lang.c , "sounak"
    <> wrote:

    >How could we get a macro name from a macro value


    you can't - macros are literally replaced in the text of your code
    with their value, before compilation. at runtime the macro no longer
    exists.

    >#define a 100

    ....
    >the user gives 100
    >then the value is outputted as a


    You'd have to have an auxilliary array or two

    char defnarray[1000] ;
    defnarray[100] = 'a';
    defnarray[200]='b';
    etc

    and lookup one into the other.

    You may be able to write a C programme to parse your header and
    automatically generate the array, so that you can compile it into
    your other C programme.


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Nov 22, 2005
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    861
    Roedy Green
    Jan 2, 2006
  2. ding feng
    Replies:
    2
    Views:
    2,852
    ding feng
    Jun 25, 2003
  3. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    590
    Jack Klein
    Sep 21, 2003
  4. Bobby Chamness
    Replies:
    2
    Views:
    2,415
    Joe Smith
    Apr 22, 2007
  5. John Reye
    Replies:
    14
    Views:
    798
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page