By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

to get macro name from macro value

P: n/a
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

Nov 23 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
Le 22-11-2005, sounak <so*****@gmail.com> 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
Nov 23 '05 #2

P: n/a
Marc Boyer <Ma********@enseeiht.yahoo.fr.invalid> wrote:
Le 22-11-2005, sounak <so*****@gmail.com> 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
Nov 23 '05 #3

P: n/a
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

Nov 23 '05 #4

P: n/a
Le 22-11-2005, sounak <so*****@gmail.com> 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
Nov 23 '05 #5

P: n/a
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

Nov 23 '05 #6

P: n/a
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.
Nov 23 '05 #7

P: n/a
if 2 macros have same value then 2 name will be shown

Nov 23 '05 #8

P: n/a
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

Nov 23 '05 #9

P: n/a
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
Nov 23 '05 #10

P: n/a
Chris Dollin wrote:
static struct { int value; char *name; } backwards =


Oops. s/backwards/backwards[]/.

--
Chris "another virtual machine" Dollin
missing tests don't fail.
Nov 23 '05 #11

P: n/a
On 2005-11-22, Chris Dollin <ke**@hpl.hp.com> 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.
Nov 23 '05 #12

P: n/a
Jordan Abel wrote:
On 2005-11-22, Chris Dollin <ke**@hpl.hp.com> 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.
Nov 23 '05 #13

P: n/a
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

Nov 23 '05 #14

P: n/a
"sounak" <so*****@gmail.com> 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) ks***@mib.org <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.
Nov 23 '05 #15

P: n/a
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
Nov 23 '05 #16

P: n/a

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"sounak" <so*****@gmail.com> 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
Nov 23 '05 #17

P: n/a
On 22 Nov 2005 01:13:35 -0800, in comp.lang.c , "sounak"
<so*****@gmail.com> 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 =----
Nov 23 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.