Conditional compilation and the preprocessor

This is a discussion on Conditional compilation and the preprocessor within the C Programming forums, part of the General Programming Boards category; This questions is regarding conditional #define's. I was curious if I could use the pre-processor to figure out the value ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    47

    Conditional compilation and the preprocessor

    This questions is regarding conditional #define's. I was curious if I could use the pre-processor to figure out the value of a #define, or if in fact a #define is defined or undefined.

    example"

    Code:
    #ifdef ON
    #define size 10
    #else
    #define size 5
    #endif
    Can I find out if ON is defined in the above example and/or what the value of size is? Are there pre-processor directives for this? All i have used the pre-processor for is to do the passovers and remove all of this so I can analyze the actual code being used. This is alot different of an idea.

    Also, I am not talking about C code or runtime stuff, just basically a gcc directive I could use to say ON is defined, or size is 10 without any execution.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    You want to tell if ON is defined? So, what do you think "#ifdef ON" means?

    If you want to test the value of a preprocessor symbol, just do it:

    Code:
    #if size == 10
    ....
    #elif size == 5
    ....
    #endif
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    The variables that are tested may not even be defined in any of the source code modules... They could be defined externally - in some programming whatchamacallit "envornment" bloated Microsoft Project/Visual-Outhouse or something.

    For example, for debugging you might have some conditional compiler switches surrounding diagnostic printfs all over the place. So depending on whether you chose to define some variable "DEBUG", the #ifdefs ensure that a diagnosing version is generated.

    Yeah, that's the way I do it, dagnabit.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21