Thread: #Preprocessor commands

  1. #1
    Registered User nurulsiddik's Avatar
    Join Date
    Feb 2002
    Posts
    8

    Question #Preprocessor commands

    Hello,
    Can any one let me know when this will work.

    #if 0
    printf("hello");
    #endif

    I am not able to understand this line as i think will never get compiled because the condition fails as zero. Can any one help me and throw some light on the above code. When will "hello" will be printed..
    Thank you,

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    You are correct, never.

    the #if...#endif is a preprocessor directive. This means that during the compile process, if the #if is true then the code following will be compiled, if false it will not.

    It's used to compile special code in special places, or to not compile code that has already been compiled. An example of the first type:

    #if DEBUG
    printf("we are debugging the code\n");
    debval = 1;
    #else
    debval = 0;
    #endif

    If the preprocessor value DEBUG is set to TRUE, the printf and debval = 1 will be compiled otherwise debval = 0 will be.

    I've used this when I was compiling graphics code for Microsoft and Borland compilers in the same code. They use different functions to do the same tasks like cursor positioning.

    The second type can be seen in standard .H files. You'll notice in most of these files code that starts with something like:

    #ifndef VALUE
    #define VALUE
    . . .
    #endif

    This way if a header file is loaded more than once (which is often the case) the first time it's loaded VALUE is not defined so the code is compiled, the second time VALUE is define, so the code is skipped.

    Walt

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    This preprocessor statement is used to "comment out" code

    #if 0 // What this code says is: If 0>0
    as opposed to:
    #if 1 // This code will always compile, as it says: if 1>0

Popular pages Recent additions subscribe to a feed