Thread: use of self-referential macro

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    48

    use of self-referential macro

    Hi

    I'm reading <The C Preprocessor - GUN version> and cannot understand this piece :
    One common, useful use of self-reference is to create a macro which expands to itself. If
    you write
    #define EPERM EPERM
    then the macro EPERM expands to EPERM. Effectively, it is left alone by the preprocessor
    whenever it’s used in running text. You can tell that it’s a macro with ‘#ifdef’. You might
    do this if you want to define numeric constants with an enum, but have ‘#ifdef’ be true for
    each constant.
    Can anybody make an example to illustrate this use?

    Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Sometimes you define constants by using an enumeration type, like this:

    Code:
    enum {
       FOO = 1
    };
    However, enumerations aren't available at preprocessing time, so you can't test their existance with an #ifdef:

    Code:
    #include <stdio.h>
    
    enum {
       FOO = 1
    };
    
    int main()
    {
    #ifdef FOO
       puts("FOO is defined");
    #endif
    }
    That's where a self-referencing macro comes handy: You define a macro to evaluate to that same enum so that you can effectively test whether it exists or not:

    Code:
    #include <stdio.h>
    
    enum {
       FOO = 1
    };
    
    #define FOO FOO
    
    int main()
    {
    #ifdef FOO
       puts("FOO is defined");
    #endif
    }

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What does that have to do with an enum and how is that different than just using:
    Code:
    #define FOO
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    You can't do this for example:

    Code:
    #define FOO
    
    int main()
    {
       int x = FOO;
    }
    glibc for instance has a tendency to define macros that use the same names as enums with error codes (errno) and locale items for nl_langinfo, among others.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Well that's completely different than your previous example.
    This would have the same effect as your other example:
    Code:
    #include <stdio.h>
    
    enum {
       FOO = 1
    };
    
    #define FOO
    
    int main()
    {
    #ifdef FOO
       puts("FOO is defined");
    #endif
    }
    as would this:

    Code:
    #include <stdio.h>
    
    enum {
       BLAH = 1
    };
    
    #define FOO
    
    int main()
    {
    #ifdef FOO
       puts("FOO is defined");
    #endif
    }
    So I don't see an example of where the code only does what you want if you define a macro with the same value as its name...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Quote Originally Posted by cpjust View Post
    This would have the same effect as your other example
    Well, yes. The point is that a library designer may utilize the above trick so that a user can verify the existance of an enumeration constant (As in the glibc case). Defining the macro to something completely different, while possible, is of little use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. need help with multi line macro usage
    By Cdigitproc1 in forum C Programming
    Replies: 9
    Last Post: 04-29-2005, 09:50 AM
  5. about Makefile and Macro
    By tom_mk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 01:07 PM