An interesting Macro-function define??? 
Author Message
 An interesting Macro-function define???

Hi,
  I want to define a Macro-function, which I pass a MACRO constant, if
the MACRO-constant has been defined, I wish it return its value,
otherwise, if the MACRO-constant is NOT defined, I wish it can return (-
 1). Thus I used to define a macro-function as follows, but as you know
it doesn't work, the compiler cannot compile the macro-define.
    Can anyone give me any help? Thanks in advance.

Jintao
Sep,22/2000

//------------------- beg of code -----------------------------

#define GetValue(macroName) \
#ifdef macroName            \
(macroName)                \
#else                      \
(-1)                      \
#endif

// a defined macro-constant
#define MACRO_NUM_DEFINED    1234

void test(void)
{
    int n;

    n = GetValue( MACRO_NUM_DEFINED );        // should be 1234
    n = GetValue( MACRO_NUM_NOT_DEFINED );    // should be (-1)

Quote:
}

//------------------- end of code -----------------------------
--



Thu, 13 Mar 2003 10:47:22 GMT  
 An interesting Macro-function define???

Quote:

> Hi,
>   I want to define a Macro-function, which I pass a MACRO constant, if
> the MACRO-constant has been defined, I wish it return its value,
> otherwise, if the MACRO-constant is NOT defined, I wish it can return (-
>  1). Thus I used to define a macro-function as follows, but as you know
> it doesn't work, the compiler cannot compile the macro-define.
>     Can anyone give me any help? Thanks in advance.

The problem is that macros are expanded once and the macro-expansion
from your code is not really valid C.

[Sample code deleted]

According to GCC, the test function is:
        void test(void)
        {
            int n;

            n = ifdef   1234    (  1234   ) else (-1) endif ;        
            n = ifdef   MACRO_NUM_NOT_DEFINED   ( MACRO_NUM_NOT_DEFINED  )
                        else (-1) endif ;    
        }

This is not valid C. What you want to do is (afaik) not doable in
C. Or, if it is, needs several more intermediate steps. You could
(possibly) do something that is fed through cpp multiple times to
acheive the effect you want.

//Ingvar
--

Visited my homepage? <http://www.algonet.se/~gruk> ?egapemoh ym detisiV
--



Wed, 19 Mar 2003 03:00:00 GMT  
 An interesting Macro-function define???

Quote:

> Hi,
>   I want to define a Macro-function, which I pass a MACRO constant, if
> the MACRO-constant has been defined, I wish it return its value,
> otherwise, if the MACRO-constant is NOT defined, I wish it can return (-
>  1). Thus I used to define a macro-function as follows, but as you know
> it doesn't work, the compiler cannot compile the macro-define.
>     Can anyone give me any help? Thanks in advance.
> Jintao
> Sep,22/2000
> //------------------- beg of code -----------------------------

Not a C comment in C89, which is the common variant. In the future, yes...

Quote:
> #define GetValue(macroName) \
> #ifdef macroName            \
> (macroName)                \
> #else                      \
> (-1)                      \
> #endif

Which of course doesn't work since you cannot end the line before
the #ifdef preprocessor directive with \. OTOH, you must if you
try this structure, so it's impossible this way.

The problem is impossible, anyway, since you probably want to evaluate
the being defined of macroname at the point where the macro is
expanded. This can only be done
- during the preprocessor phase (because that's when #ifdef is evaluated)
- afer the preprocessor phae (because that's when the macro is expanded).
Since there cannot be a point in time that is both during and after this
phase, it can't be done. I cannot invent an easy way to fake it, either.

Michiel Salters
--



Wed, 19 Mar 2003 03:00:00 GMT  
 An interesting Macro-function define???

Quote:

>   I want to define a Macro-function, which I pass a MACRO constant, if
> the MACRO-constant has been defined, I wish it return its value,
> otherwise, if the MACRO-constant is NOT defined, I wish it can return (-
>  1).

That's not possible. The C preprocessor is a 1-pass macro processor,
sort of. It is explicitly defined *not* to honor any #-directive in
the 'right hand side' text of a macro definition.  But the only place
you can test whether a given name is a #define'd macro or not is an
#ifdef or #if defined() line.

Sorry, but I think you'll have to go back to square 1.
--

Even if all the snow were burnt, ashes would remain.
--



Wed, 19 Mar 2003 03:00:00 GMT  
 An interesting Macro-function define???

Quote:
> Hi,
>   I want to define a Macro-function, which I pass a MACRO constant, if
> the MACRO-constant has been defined, I wish it return its value,
> otherwise, if the MACRO-constant is NOT defined, I wish it can return (-1)

Asked and answered in comp.lang.c.

--
- David.Thompson 1 now at worldnet.att.net
--



Wed, 19 Mar 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. An interesting Macro-function define???

2. An interesting Macro-function define???

3. #define functions as macros

4. class view dosen't shows member variable's and functions defined with macros

5. #define a macro to be another macro?

6. Preprocessor: defining a macro inside a #define directive

7. interesting macro

8. Interesting Problem Needs Interesting Solution. :)

9. #define macro question

10. #define macros

11. Detecting macro defined as empty (as opposed to zero or undefined)

 

 
Powered by phpBB® Forum Software