# character in macros

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Venky

    # character in macros

    Hello,

    I have situation to put # character in macros.

    Eg:

    I have to include file1 ,file2, and file3 in a file.
    #ifndef file1
    #include<file1. h>
    #endif

    #ifndef file2
    #include<file2. h>
    #endif

    #ifndef file3
    #include<file3. h>
    #endif

    I want to add a macro to do the above task

    #define INCLUDE(x) #ifndef #x \
    #include<#x.h\
    #endif

    and then i want to use INCLUDE(file1)
    INCLUDE(file2) ...etc.,

    But as you all know, macro thinks that all "#" are for stringizing
    characters. But i want to tell that macro that this # is special
    character and don't treat it as stringizing character.

    Any help ?
    Thanks,
    Venkatesh.

  • Spade

    #2
    Re: # character in macros

    On Jul 4, 11:44 am, Venky <venkatesh.dugg ir...@gmail.com wrote:
    Hello,
    >
    I have situation to put # character in macros.
    >
    Eg:
    >
    I have to include file1 ,file2, and file3 in a file.
    #ifndef file1
    #include<file1. h>
    #endif
    >
    #ifndef file2
    #include<file2. h>
    #endif
    >
    #ifndef file3
    #include<file3. h>
    #endif
    >
    I want to add a macro to do the above task
    >
    #define INCLUDE(x) #ifndef #x \
    #include<#x.h\
    #endif
    >
    and then i want to use INCLUDE(file1)
    INCLUDE(file2) ...etc.,
    >
    But as you all know, macro thinks that all "#" are for stringizing
    characters. But i want to tell that macro that this # is special
    character and don't treat it as stringizing character.
    >
    Any help ?

    AFAIK, its not possible to have macro expand to another preprocessor
    directive.

    Comment

    • Flash Gordon

      #3
      Re: # character in macros

      Spade wrote, On 04/07/07 08:53:
      On Jul 4, 11:44 am, Venky <venkatesh.dugg ir...@gmail.com wrote:
      >Hello,
      >>
      >I have situation to put # character in macros.
      >>
      >Eg:
      >>
      >I have to include file1 ,file2, and file3 in a file.
      >#ifndef file1
      >#include<file1 .h>
      >#endif
      <snip>
      >I want to add a macro to do the above task
      >>
      >#define INCLUDE(x) #ifndef #x \
      > #include<#x.h\
      > #endif
      >>
      >and then i want to use INCLUDE(file1)
      > INCLUDE(file2) ...etc.,
      >>
      >But as you all know, macro thinks that all "#" are for stringizing
      >characters. But i want to tell that macro that this # is special
      >character and don't treat it as stringizing character.
      >>
      >Any help ?
      >
      >
      AFAIK, its not possible to have macro expand to another preprocessor
      directive.
      You are correct, it is not possible. The correct was to handle the
      problem the OP is showing is to put the include guards in the include
      files themselves.
      --
      Flash Gordon

      Comment

      • Laurent Deniau

        #4
        Re: # character in macros

        Venky wrote:
        Hello,
        >
        I have situation to put # character in macros.
        >
        Eg:
        >
        I have to include file1 ,file2, and file3 in a file.
        #ifndef file1
        #include<file1. h>
        #endif
        >
        #ifndef file2
        #include<file2. h>
        #endif
        >
        #ifndef file3
        #include<file3. h>
        #endif
        >
        I want to add a macro to do the above task
        >
        #define INCLUDE(x) #ifndef #x \
        #include<#x.h\
        #endif
        >
        and then i want to use INCLUDE(file1)
        INCLUDE(file2) ...etc.,
        >
        But as you all know, macro thinks that all "#" are for stringizing
        characters. But i want to tell that macro that this # is special
        character and don't treat it as stringizing character.
        >
        Any help ?
        Unless you run the preprocessor twice, this is not possible.

        a+, ld.

        Comment

        • Cedric Roux

          #5
          Re: # character in macros

          Venky wrote:
          Hello,
          >
          I have situation to put # character in macros.
          >
          Eg:
          >
          I have to include file1 ,file2, and file3 in a file.
          #ifndef file1
          #include<file1. h>
          #endif
          >
          #ifndef file2
          #include<file2. h>
          #endif
          >
          #ifndef file3
          #include<file3. h>
          #endif
          >
          I want to add a macro to do the above task
          >
          #define INCLUDE(x) #ifndef #x \
          #include<#x.h\
          #endif
          >
          and then i want to use INCLUDE(file1)
          INCLUDE(file2) ...etc.,
          >
          But as you all know, macro thinks that all "#" are for stringizing
          characters. But i want to tell that macro that this # is special
          character and don't treat it as stringizing character.
          >
          Any help ?
          Thanks,
          Venkatesh.
          Note also that "backslash followed by newline" is deleted by
          the compiler, so you cannot define a macro that will expand on
          several lines. There is no way to tell the preprocessor that it
          should insert a newline at this specific point of the macro
          replacement.

          Cedric.

          Comment

          • SM Ryan

            #6
            Re: # character in macros

            # #define INCLUDE(x) #ifndef #x \
            # #include<#x.h\
            # #endif
            #
            # and then i want to use INCLUDE(file1)
            # INCLUDE(file2) ...etc.,

            cpp barely qualifies as a macro processor. What you are trying
            to do is not guarenteed to work, and rarely does. You either
            live with the severe limitations of cpp or use a real macro
            processor, such as m4 and many other available.

            --
            SM Ryan http://www.rawbw.com/~wyrmwif/
            One of the drawbacks of being a martyr is that you have to die.

            Comment

            • Keith Thompson

              #7
              Re: # character in macros

              SM Ryan <wyrmwif@tang o-sierra-oscar-foxtrot-tango.fake.orgw rites:
              # #define INCLUDE(x) #ifndef #x \
              # #include<#x.h\
              # #endif
              #
              # and then i want to use INCLUDE(file1)
              # INCLUDE(file2) ...etc.,
              >
              cpp barely qualifies as a macro processor. What you are trying
              to do is not guarenteed to work, and rarely does.
              In fact, it's guaranteed not to work. If it does work, then your C
              preprocessor is broken.

              (Have you decided to start snipping attributions as well as using an
              obnoxious quoting character?)

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              Working...