Thread: Questions regarding usage of #undef and #ifdef

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    93

    Questions regarding usage of #undef and #ifdef

    Lets say I've got a three module source (File1.c, File2.c and File3.c) and I use #undef at a global level in one of the modules like shown in Example A. Will this redefinition effect the other modules?

    Example A:
    Code:
    /* File2.c */
    
    typedef signed char MYBYTE;
    
    #undef LOBYTE
    #undef HIBYTE
    
    #define LOBYTE(w)  ((MYBYTE)(int)(w))
    #define HIBYTE(w)  ((MYBYTE)(((int)(w) >> 8) & 0xFF))
    Scenerio two. Lets say I do a #ifdef as shown below in case WM_SIZE under WinMain(). Will these redefinitions be confined to just case WM_SIZE?

    Code:
    case WM_SIZE:
        {
            #ifdef LOBYTE
            #ifdef HIBYTE
    
            #undef LOBYTE
            #undef HIBYTE
            #define LOBYTE(w)  ((signed char)((signed) w & 0x00FF)
            #define HIBYTE(w)  ((signed char)(((signed) w & 0xFF00) >> 8))
    
            /* .... */  // rest of code
    
            #endif
            #endif
        }
        break;
    BTW, These are just made up scenerios.

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    That's a no to both. 1) Files are compiled separately so you'd have to do it in each one, 2) The preprocessor doesn't give a hoot about your C-scopes, because it has no idea what anything is unless the line starts with a # or is a macro name.
    Last edited by adeyblue; 01-21-2011 at 08:45 PM.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    93
    In Example A i could see that none of the other modules would be effected by the redefinition of LOBYTE and HIBYTE since like you mentioned there all compiled separately. Now, in scenerio two I would expect the redefinition of LOBYTE and HIBYTE not to effect anything else within the module except for case WM_SIZE; correct?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TAZIN View Post
    In Example A i could see that none of the other modules would be effected by the redefinition of LOBYTE and HIBYTE since like you mentioned there all compiled separately. Now, in scenerio two I would expect the redefinition of LOBYTE and HIBYTE not to effect anything else within the module except for case WM_SIZE; correct?
    It's still no to both. #define is just search-and-replace. The pre-processor doesn't even know what a brace is, let alone anything about scope.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TAZIN View Post
    In Example A i could see that none of the other modules would be effected by the redefinition of LOBYTE and HIBYTE since like you mentioned there all compiled separately. Now, in scenerio two I would expect the redefinition of LOBYTE and HIBYTE not to effect anything else within the module except for case WM_SIZE; correct?
    AdeyBlue has it right... The C preprocessor doesn't know about the delineated scopes in a program, and it doesn't care... at any point after you redefine those macros the preprocessor will see them as changed... even hundreds of function calls later...

    It operates a lot like HTML does... it sees a B and until it sees /B it will bold face everything in site... when the preprocessor sees a definition it will hold that definition until you change it.

    The preprocessor is not part of C, per se... it's a separate thing with it's own set of rules.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    93
    So, in scenerio two the redefinition of LOBYTE and HIBYTE will effect the whole module.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    93
    Now, lets say that you wanted to go back to the precompiled header version of LOBYTE and HIBYTE in scenerio two. What's the best way to do that? Would you have to do what's shown below?

    Code:
    typedef unsigned char BYTE;
    
    #undef LOBYTE
    #undef HIBYTE
    
    #define LOBYTE(w)           ((BYTE)(w))
    #define HIBYTE(w)           ((BYTE)(((WORD)(w) >> 8) & 0xFF))

  8. #8
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    No, you'd call your signed version something else so you wouldn't have to do these dumb hacks in the first place. That way the things you know about the preprocessor and the things you don't can't conspire to use one version when you want the other one.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    93
    Thanks guys for your input.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TAZIN View Post
    So, in scenerio two the redefinition of LOBYTE and HIBYTE will effect the whole module.
    Everything below your definition in the file will be affected.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #ifdef error
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2010, 07:03 PM
  2. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  3. A Couple of Questions
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 12-03-2005, 06:47 PM