C's Limited Macro Capabilities

Brian Moquin bpm at psuecl.bitnet
Sun Dec 3 14:58:39 AEST 1989


A student in my C class asked me an interesting question:  can you have
conditional compilation directives embedded within '#define' macros?
The answer is no.  The student pointed out that this then
severely limits the macro capabilities of C.  He said that in assembler
(MASM, I'm sure), he can write macros which contain arguments that
determine how the macros get expanded.  I had trouble coming up with
a good analogous example in C, but here's one to think about:
        #define cast(flag,x)    #if flag=='I' \
                                        ((int)(x)) \
                                #elif flag=='F' \
                                        ((float)(x)) \
                                #endif
This is not legal C, but if it were I think it would enhance the power
of macro expansions significantly.  Thoughts?



More information about the Comp.lang.c mailing list