Thread: macro (#define) question

  1. #1
    Darrok
    Guest

    macro (#define) question

    ive been trying to do this for a while, but i havent had any luck

    how (if you can) do you have other preprosessor commands within a #define? and is there a way to have a #define within a #define?

    like can i do something like this (using code that doesnt work)

    #define BLAH
    #ifdef Blah2
    1
    #else
    2
    #endif

    and then could i have something like this?

    #define BLAH
    #define Blah2 0
    #ifdef Blah2
    1
    #else
    2
    #endif
    #undef Blah2

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I'm not sure what you're asking, but you can do this -

    #ifdef Blah2
    #define BLAH 1
    #else
    #define BLAH 2
    #endif
    zen

  3. #3
    Darrok
    Guest
    no, i mean can i do something like this

    sorry, it removed the spaces the last time

    #define macro(x)
    _#if x==1
    __dostuff();
    _#elif x==2
    __dootherstuff();
    _#else
    __domorestuff();
    _#endif
    _#define Blah 2
    _do_stuff_that_uses_Blah();

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I think I understand what you're asking and I think the answers no. Macros are just used for text substitution by the pre-processor at compile time and you'd be asking it to evaluate runtime variables. To get your second example to work you'd have to write the macro like a C function rather than a series of #if/#else.
    zen

  5. #5
    Darrok
    Guest
    ok, then how about this?

    #define macro(x)
    _#if 1==1
    __dostuff();
    _#elif 2==2
    __dootherstuff();
    _#else
    __domorestuff();
    _#endif
    _#define Blah 2
    _do_stuff_that_uses_Blah();

    the reason why i used the first example was because i was able use simlar code in an assembler

    and it wouldnt be anything done at run time, it'd still be able to be completed by the preprocessor, if you look, it's comparing x, a constant declared by the macro, to a value, which you can do if you declare the constant using a #define. it would have this value at preprossessor time

    i wasnt planning on doing it like that anyway, although it does make things a lot easier. i was planning on using an #ifdef and have it result in a different substitution depending upon whether or not something was defined

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Do you mean something like this -

    Code:
    #include <iostream> 
    using namespace std;
    
    #define x 2 
    
    #if x==1
    #define macro() cout << "1"
    #elif x==2 
    #define macro() cout << "2" 
    #else
    #define macro() cout << "X"
    #endif 
    
    
    int main() 
    { 
    	macro();
    	return 0; 
    }
    zen

  7. #7
    Darrok
    Guest
    yeah, that would work in most cases

    but I still need to know if it's possible to use have a define inside of a macro

    what i want to do is something like this

    #define macro(text)
    _#define Blah array
    _text
    _#undefine Blah
    _#define Blah array2
    _text

    that way i can do something like this

    int x;
    macro({
    for(x=1; x<Blah[0]; x++) Blah[x] *=2;
    somefunc(Blah[0]);
    }) //yes, this does work, ive tried it already

    without the macro I'd have to type it twice or put an extra load on the processor to loop it and change a pointer

    what im actually doing is trying to optimize the code for an rpg battle system that im writing, and i found that i was writing most code twice

    also, what do i do to specify a 2nd argument for a macro? would the above code still work with a 2nd argument if i had a , in there somewhere for some reason?

  8. #8
    Darrok
    Guest
    actually, i probably dont need the {}s

  9. #9
    Darrok
    Guest
    Originally posted by zen
    Do you mean something like this -

    Code:
    #include <iostream> 
    using namespace std;
    
    #define x 2 
    
    #if x==1
    #define macro() cout << "1"
    #elif x==2 
    #define macro() cout << "2" 
    #else
    #define macro() cout << "X"
    #endif 
    
    
    int main() 
    { 
    	macro();
    	return 0; 
    }
    actually, it doesnt work for what i want, i just tried it

    what i want is to be able to do something like that, but then if i do
    #undef x
    #define x 1
    it'll change which one is used

    i know that what i want is possible with more advanced macro processors.

    if it's not possible, i'll probably end up writing my own preprocessor to run before the C++ preprocessor

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    no, i mean can i do something like this

    sorry, it removed the spaces the last time

    #define macro(x)
    _#if x==1
    __dostuff();
    _#elif x==2
    __dootherstuff();
    _#else
    __domorestuff();
    _#endif
    _#define Blah 2
    _do_stuff_that_uses_Blah();
    Yes. But not exactly the way you think:
    Code:
    #define myMacro(x) \
        if((x)==1) \
            doStuff( ); \
        else \
        if((x)==2) \
            doOtherStuff( );
    That will do it. You can use \ in macros to tell the macro that it spans multiple lines. Remember though, that you'll have to make sure you call it correctly. You could modify that macro to work like this:

    #define myMacro(x) ((x)==1?doStuff( ):doOtherStuff( ))

    Or perhaps:

    #define myMacro(x) ((x)==1?doStuff( )x)==2?doOtherStuff( ):someDefaultElseResult( ))

    This way, the macro is easy to call:

    if( myMacro(x) ) .... and the return value will be the return value of either function, based on whatever x evaluates to in your macro.

    So to answer your question, "Yes, you can."

    Quzah.
    Last edited by quzah; 12-18-2001 at 08:14 PM.
    Hope is the first step on the road to disappointment.

  11. #11
    Darrok
    Guest
    but would that still be handled by the preprocessor? if not, i dont think that would even work because x isnt a memory address, it's a defined constant

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. Macros are handled by the preprocessor. All the preprocessor does is replace every instance of 'myMacro' with the code as defined. It will work. Trust me on this one. Those macros are valid C/C++ code.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Darrok
    Guest
    no, i meant is the x==1 being handled by the preprocessor?

    also, do you know anything about having #define's in a macro?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let me clarify how macros work:

    #deifne myMacro(x) (someFunction( ) == (x) ) ? "true" : "false"

    If you wrote this macro, as above, and you called it like so:

    cout << myMacro( myVariable );

    Then the preprocessor would expand the code to actually be:

    cout << (someFunction( ) == (myVariable)) ? "true" : "false";

    All it does is basicly a text replacement for your definition to the body of the macro.

    [edit] Regarding macros in macros:

    #define MYMACRO 20
    #define SOMEMACRO "hello there"
    #define aMacro(x) (x)==4? MYMACRO : MYMACRO * MYMACRO
    #define bMacro(y,z) aMacro(y) == MYMACRO ? "invalid" : aMacro(z) > MYMACRO ? "valid" : SOMEMACRO

    Consider the above.

    cout << bMacro( 15, 30 );

    Expands to:

    cout << aMacro( 15 ) == MYMACRO ? "invalid" : aMacro( 30 ) > MYMACRO ? "valid" : SOMEMACRO ;

    Expands to:

    cout << aMacro( 15 ) == 20 ? "invalid" : aMacro( 30 ) > 20 ? "valid" : "hello there" ;

    Expands to:

    cout << (15 == 4 ? 20 : 400) == 20 ? "invalid" : (30 == 4 ? 20 : 400 ) > 20 ? "valid" "hello there";

    Or something like that. As you can see, you can compact, from the reader's point of view, some fairly complicated code into much simpler end results with macros.

    Some people think macro usage is bad, but in reality they can be useful when used sparingly or when appropriate.

    Quzah.
    Last edited by quzah; 12-18-2001 at 08:32 PM.
    Hope is the first step on the road to disappointment.

  15. #15
    Darrok
    Guest
    ah, ok, got it

    that's still not quite what i wanted though, i cant have it output the name of a variable or something with that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why?!?
    By p3rry in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 12:52 PM
  2. Would someone solve my problem?
    By Lonners in forum C Programming
    Replies: 9
    Last Post: 01-19-2008, 06:58 PM
  3. Bor to DevC++ prog convert prob
    By kryptkat in forum Windows Programming
    Replies: 16
    Last Post: 09-18-2007, 05:11 AM
  4. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 09:44 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM