learning name

P

Parahat Melayev

Let's say I have such definition in one of my headers

#define one 1

and later in some of my program functions there is such definition

void enter_the_matrix(unsigned int key)
{
if(key == one) // <--- **** ATTENTION: Question is here. ***
printf("welcome neo!\n");
}

Now, my question is; is there some function or macro which will help me
to retreive name of a defined value as string in standart C?

I mean when I say:

printf("%s\n", give_me_your_name(1));

it will give me "one" which is defined in header file.

Thanks
 
T

Tomás

printf("%s\n", give_me_your_name(1));

Why not do the following:

const char* const IntegralValueNames[] = {
"zero",
"one",
"two",
"three",
"four",
"five",
"six"
};


printf("%s\n", IntegralValueNames[1] );


-Tomas
 
P

Parahat Melayev

Laziness...

There are too many header files and definitions so it is really boring
to find the name of catched value.
 
C

CBFalconer

Parahat said:
Laziness...

There are too many header files and definitions so it is really
boring to find the name of catched value.

Another incomprehensible post from an ignorant google poster. My
sig. below is an attempt to replace the ignorance with knowledge.
Read it, and the referenced URLs.

--
"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." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
I

Ico

Parahat Melayev said:
Laziness...

What is ?
There are too many header files and definitions so it is really boring
to find the name of catched value.

No idea what you are talking about. Please quote the messages you are
replying to. If you insist on using the broken google interface, reply
by clicking 'show options' first, and then choose the 'Reply' link.
 
T

Thad Smith

Parahat said:
Let's say I have such definition in one of my headers

#define one 1

and later in some of my program functions there is such definition

void enter_the_matrix(unsigned int key)
{
if(key == one) // <--- **** ATTENTION: Question is here. ***
printf("welcome neo!\n");
}

Now, my question is; is there some function or macro which will help me
to retreive name of a defined value as string in standart C?

I mean when I say:

printf("%s\n", give_me_your_name(1));

it will give me "one" which is defined in header file.

You could do that be writing a program to scan your header files,
collect relevant defines, and generate a lookup table which can be
queried at run time.
 
K

Keith Thompson

Parahat Melayev said:
Laziness...

There are too many header files and definitions so it is really boring
to find the name of catched value.

Read <http://cfaj.freeshell.org/google/>. If you're capable of using
Google, you're capable of using it properly. If you refuse to take
the time to use it properly, don't expect anyone here to take the time
to figure out what you're talking about or answer your questions.

Laziness can be a virtue; most human progress is made by people trying
to figure out easier ways to do things. If you're too lazy to use
some complex method, and that leads you to find or invent an easier
one, that's great. But if you're too lazy to take the time to make
yourself understood to people you're asking for help, you'll won't get
much sympathy.
 
K

Keith Thompson

Parahat Melayev said:
Let's say I have such definition in one of my headers

#define one 1

and later in some of my program functions there is such definition

void enter_the_matrix(unsigned int key)
{
if(key == one) // <--- **** ATTENTION: Question is here. ***
printf("welcome neo!\n");
}

Now, my question is; is there some function or macro which will help me
to retreive name of a defined value as string in standart C?

I mean when I say:

printf("%s\n", give_me_your_name(1));

it will give me "one" which is defined in header file.

No. Macro definitions are not visible in a running program.

If you want to store the information so that your program can read it
at run time, you'll have to write the code to do it. It will take
some work.
 
W

Walter Roberson

Parahat Melayev said:
Now, my question is; is there some function or macro which will help me
to retreive name of a defined value as string in standart C?
I mean when I say:
printf("%s\n", give_me_your_name(1));
it will give me "one" which is defined in header file.

1) What is the type of the parameter passed to give_me_your_name() ?

2) If I have

#define One 1
#deine LowIndex 1

then which name should give_me_your_name(1) return?

3) If I have

#define One 1
#define LowIndex One

then which name should give_me_your_name(1) return?
 
S

SM Ryan

# Another incomprehensible post from an ignorant google poster. My

Off topic whining noted. Your opinion of Google has nothing
to do with ANSI C.
 
D

Denis Kasak

SM said:
Off topic whining noted. Your opinion of Google has nothing
to do with ANSI C.

No, it doesn't, but quoting and attributing text correctly does. Not
directly with ANSI C, per se, but definitely with this newsgroup and the
Usenet as a whole. Also, CBFalconer wasn't expressing his opinion about
Google. He merely tried to educate the OP of the proper way to post,
something in which he succeeded, judging by the OP's most recent
followup. Thus, Usenet has become a happier place once again.

Please provide full context when replying to messages, including text
attributions. You haven't done so in your reply to CBFalconer so I have
corrected it here as an example. Please do the same in your future posts.
 
C

CBFalconer

Denis said:
No, it doesn't, but quoting and attributing text correctly does. Not
directly with ANSI C, per se, but definitely with this newsgroup and the
Usenet as a whole. Also, CBFalconer wasn't expressing his opinion about
Google. He merely tried to educate the OP of the proper way to post,
something in which he succeeded, judging by the OP's most recent
followup. Thus, Usenet has become a happier place once again.

Please provide full context when replying to messages, including text
attributions. You haven't done so in your reply to CBFalconer so I have
corrected it here as an example. Please do the same in your future posts.

I have no idea what Ryan wrote, apart from your quote. He has long
been PLONKed here for refusal to follow normal usenet practices,
such as quote characters. He insists on attempting to foul the
system for everybody.

--
"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." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
N

Neil

Parahat said:
Let's say I have such definition in one of my headers

#define one 1

and later in some of my program functions there is such definition

void enter_the_matrix(unsigned int key)
{
if(key == one) // <--- **** ATTENTION: Question is here. ***
printf("welcome neo!\n");
}

Now, my question is; is there some function or macro which will help me
to retreive name of a defined value as string in standart C?

I mean when I say:

printf("%s\n", give_me_your_name(1));

it will give me "one" which is defined in header file.

Thanks

Simply No. You need to read up on what the pre-compiler does.

The compiler does not see "one" it sees "1".
 
D

Dave Thompson

Let's say I have such definition in one of my headers

#define one 1

0) For 'int' values, generally better to use a (dummy) enum, which is
more nicely scoped.
Now, my question is; is there some function or macro which will help me
to retreive name of a defined value as string in standart C?
No.

1) Use Ada and an enumerated type. Ada compilers are required to
provide a standardized (and fairly nice) binding to C. Among other
things it handles converting between C-style strings and Ada-style
(fixed or doped array of char) which may be useful here.

2) If your definitions are all of simple form like your example, write
a tool which scans your applicable header and/or source files and
generates a function which accepts value and returns (pointer to)
string or even an array indexed by value pointing to or containing
string. With good discipline in your source this can be a one-liner in
awk or perl. With make and some other build environments also you can
have your tool automatically (re)executed when (possibly) needed.

3) Force all your definitions to be of simple (enough) form in your
own language from which you automatically derive both C declarations
and/or #define's, and also a the mapping array or function.

4) <*ON*TOPIC> Use the C preprocessor to do #3 by either:

/* mylits.h */
#ifndef ONELIT
#define ONELIT(id,val) enum { id = val };
#endif
ONELIT(one,1)
ONELIT(two,2)

const char * get_mylit (int val) ;

/* mylits.c */
#include "mylits.h" /* for prototype */
const char * get_mylit (int val)
{ switch (val) {
#undef ONELIT
#define ONELIT(id,val) case val : return # id ;
#include "mylits.h" /* for each
default: return "INVALID MYLIT"; /* or perhaps abort() */
}
}

or (if the list is small enough):
/* mylits.h */
#define MYLITS ONELIT(one,1) ONELIT(two,2) /* etc. */
#ifndef ONELIT
#define ONELIT(id,val) enum { id = val };
MYLITS
#endif

/* mylits.c */
#include "mylits.h"
const char * get_mylit (int val)
{ switch (val) {
#undef ONELIT
#define ONELIT(id,val) case val : return # id ;
MYLITS
default: return "INVALID MYLIT"; } }


Aside: I thought I recalled a FAQ answer along the lines of #2-4, but
only found 2.24. At the usual places and http://c-faq.com/ .

- David.Thompson1 at worldnet.att.net
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top