single-quoting a macro argument

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

    single-quoting a macro argument

    Is it possible to write a macro that single-quotes its argument?

    #define SOME_MACRO(x)

    such that SOME_MACRO(foo) expands to 'foo'

    Thanks,
    Marius

  • SM Ryan

    #2
    Re: single-quoting a macro argument

    "Marius Lazer" <marius.lazer@g mail.com> wrote:
    # Is it possible to write a macro that single-quotes its argument?
    #
    # #define SOME_MACRO(x)
    #
    # such that SOME_MACRO(foo) expands to 'foo'

    For a single character you can do
    #define LITERAL(x) (*#x)
    and perhaps the compiler can optimise away the memory reference.

    For multiple characters, you might be able to get away with some like
    #define LITERAL(x) (*((int*)#x "\0\0\0\0\0\0\0 \0"))
    but that's making assumptions about how the compiler stores strings.

    Unless there's particular reason you need to make a constant,
    you can do something like
    int literalise(char *s) {
    int i,v; unsigned char *u = (unsigned char*)s;
    for (i=0,v=0; u[i] && i<sizeof v; i++)
    v = (v<<CHAR_BIT) | u[i]
    return v;
    }
    literalise("foo ");
    which also avoids implementation dependent interpretation of
    multi-character literals, endianess, and memory alignment
    questions.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Title does not dictate behaviour.

    Comment

    • Marius Lazer

      #3
      Re: single-quoting a macro argument

      I meant literally expansion to 'foo', not to it's first character.
      There are no value semantics here.

      #define SOME_MACRO(x) #x

      SOME_MACRO(foo) expands to "foo" but I need 'foo'

      I tried something like this which is close but not exactly what I need:

      #define QUOTE '
      #define SOME_MACRO(x) QUOTE x QUOTE

      SOME_MACRO(foo) expands to ' foo ' (with spaces instead of 'foo').

      Thanks,
      Marius

      Comment

      • Ben Pfaff

        #4
        Re: single-quoting a macro argument

        "Marius Lazer" <marius.lazer@g mail.com> writes:
        [color=blue]
        > I meant literally expansion to 'foo', not to it's first character.
        > There are no value semantics here.
        >
        > #define SOME_MACRO(x) #x
        >
        > SOME_MACRO(foo) expands to "foo" but I need 'foo'[/color]

        I don't think you can get what you want from the C preprocessor.
        --
        "I don't have C&V for that handy, but I've got Dan Pop."
        --E. Gibbons

        Comment

        • Joe Smith

          #5
          Re: single-quoting a macro argument


          "Ben Pfaff" <blp@cs.stanfor d.edu> wrote in message[color=blue]
          > "Marius Lazer" <marius.lazer@g mail.com> writes:[/color]
          [color=blue][color=green]
          >> I meant literally expansion to 'foo', not to it's first character.
          >> There are no value semantics here.
          >>
          >> #define SOME_MACRO(x) #x
          >>
          >> SOME_MACRO(foo) expands to "foo" but I need 'foo'[/color]
          >
          > I don't think you can get what you want from the C preprocessor.[/color]

          If you could torture the preprocessor into doing this, I would claim that it
          acted under duress. This is processing. joe


          Comment

          • Kenneth Brody

            #6
            Re: single-quoting a macro argument

            Marius Lazer wrote:
            [...][color=blue]
            > #define QUOTE '
            > #define SOME_MACRO(x) QUOTE x QUOTE
            >[/color]
            [...]

            Is MSVC broken, or is this a valid error?

            ===== usenet.c
            #define QUOTE '
            =====

            Attempting to pre-process this one-line file gives me:

            usenet.c(1) : error C2001: newline in constant

            ===== usenet.c
            #define QUOTE \'
            =====

            This gives:

            usenet.c(1) : error C2295: escaped ''' : is illegal in macro definition

            --
            +-------------------------+--------------------+-----------------------------+
            | Kenneth J. Brody | www.hvcomputer.com | |
            | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
            +-------------------------+--------------------+-----------------------------+
            Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

            Comment

            • David Resnick

              #7
              Re: single-quoting a macro argument


              Marius Lazer wrote:[color=blue]
              > I meant literally expansion to 'foo', not to it's first character.
              > There are no value semantics here.
              >
              > #define SOME_MACRO(x) #x
              >
              > SOME_MACRO(foo) expands to "foo" but I need 'foo'
              >
              > I tried something like this which is close but not exactly what I need:
              >
              > #define QUOTE '
              > #define SOME_MACRO(x) QUOTE x QUOTE
              >
              > SOME_MACRO(foo) expands to ' foo ' (with spaces instead of 'foo').
              >
              > Thanks,
              > Marius[/color]

              why not

              #define SOME_MACRO(x) 'x'

              Seems to me to work, unless I don't understand what you want. What I
              see:

              temp(1267)$ cat foo.c
              #define SOME_MACRO(x) 'x'

              int main(void)
              {
              SOME_MACRO(a);

              return 0;
              }

              temp(1268)$ gcc -o foo -E foo.c
              temp(1269)$ cat foo
              # 1 "foo.c"
              # 1 "<built-in>"
              # 1 "<command line>"
              # 1 "foo.c"


              int main(void)
              {
              'x';

              return 0;
              }




              -David

              Comment

              • Kenneth Brody

                #8
                Re: single-quoting a macro argument

                David Resnick wrote:
                [...][color=blue]
                > why not
                >
                > #define SOME_MACRO(x) 'x'
                >
                > Seems to me to work, unless I don't understand what you want. What I
                > see:
                >
                > temp(1267)$ cat foo.c
                > #define SOME_MACRO(x) 'x'[/color]
                [...][color=blue]
                > SOME_MACRO(a);[/color]
                [...][color=blue]
                > 'x';[/color]

                Didn't you notice a problem with the macro expansion here?

                [...]

                --
                +-------------------------+--------------------+-----------------------------+
                | Kenneth J. Brody | www.hvcomputer.com | |
                | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
                +-------------------------+--------------------+-----------------------------+
                Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

                Comment

                • David Resnick

                  #9
                  Re: single-quoting a macro argument


                  David Resnick wrote:[color=blue]
                  > Marius Lazer wrote:[color=green]
                  > > I meant literally expansion to 'foo', not to it's first character.
                  > > There are no value semantics here.
                  > >
                  > > #define SOME_MACRO(x) #x
                  > >
                  > > SOME_MACRO(foo) expands to "foo" but I need 'foo'
                  > >
                  > > I tried something like this which is close but not exactly what I need:
                  > >
                  > > #define QUOTE '
                  > > #define SOME_MACRO(x) QUOTE x QUOTE
                  > >
                  > > SOME_MACRO(foo) expands to ' foo ' (with spaces instead of 'foo').
                  > >
                  > > Thanks,
                  > > Marius[/color]
                  >
                  > why not
                  >
                  > #define SOME_MACRO(x) 'x'
                  >
                  > Seems to me to work, unless I don't understand what you want. What I
                  > see:
                  >
                  > temp(1267)$ cat foo.c
                  > #define SOME_MACRO(x) 'x'
                  >
                  > int main(void)
                  > {
                  > SOME_MACRO(a);
                  >
                  > return 0;
                  > }
                  >
                  > temp(1268)$ gcc -o foo -E foo.c
                  > temp(1269)$ cat foo
                  > # 1 "foo.c"
                  > # 1 "<built-in>"
                  > # 1 "<command line>"
                  > # 1 "foo.c"
                  >
                  >
                  > int main(void)
                  > {
                  > 'x';
                  >
                  > return 0;
                  > }
                  >
                  >
                  >
                  >
                  > -David[/color]

                  OK, I'm a moron. I looked at that and thought it worked.

                  -David

                  Comment

                  • David Resnick

                    #10
                    Re: single-quoting a macro argument


                    Kenneth Brody wrote:[color=blue]
                    > David Resnick wrote:
                    > [...][color=green]
                    > > why not
                    > >
                    > > #define SOME_MACRO(x) 'x'
                    > >
                    > > Seems to me to work, unless I don't understand what you want. What I
                    > > see:
                    > >
                    > > temp(1267)$ cat foo.c
                    > > #define SOME_MACRO(x) 'x'[/color]
                    > [...][color=green]
                    > > SOME_MACRO(a);[/color]
                    > [...][color=green]
                    > > 'x';[/color]
                    >
                    > Didn't you notice a problem with the macro expansion here?
                    >
                    > [...]
                    >[/color]

                    Post in haste, repent at leisure.

                    -David

                    Comment

                    • Robert Gamble

                      #11
                      Re: single-quoting a macro argument

                      Marius Lazer wrote:[color=blue]
                      > Is it possible to write a macro that single-quotes its argument?
                      >
                      > #define SOME_MACRO(x)
                      >
                      > such that SOME_MACRO(foo) expands to 'foo'[/color]

                      I *think* this is Standard, someone will correct me if its not:

                      #define QUOTE(x) '
                      #define foo2(x) x
                      #define foo(x) QUOTE(x)foo2(x) QUOTE(x)

                      foo(test)

                      Is this just for academic purposes or is there actually a reason you
                      can't say 'test'?

                      Robert Gamble

                      Comment

                      • Andrew Poelstra

                        #12
                        Re: single-quoting a macro argument

                        On 2006-05-24, Robert Gamble <rgamble99@gmai l.com> wrote:[color=blue]
                        > Marius Lazer wrote:[color=green]
                        >> Is it possible to write a macro that single-quotes its argument?
                        >>
                        >> #define SOME_MACRO(x)
                        >>
                        >> such that SOME_MACRO(foo) expands to 'foo'[/color]
                        >
                        > I *think* this is Standard, someone will correct me if its not:
                        >
                        > #define QUOTE(x) '
                        > #define foo2(x) x
                        > #define foo(x) QUOTE(x)foo2(x) QUOTE(x)
                        >
                        > foo(test)
                        >
                        > Is this just for academic purposes or is there actually a reason you
                        > can't say 'test'?
                        >[/color]

                        Also, what use is it to have a string in single quotes? My compiler
                        invariably gives me warnings if I try to do such a thing.

                        --
                        Andrew Poelstra < http://www.wpsoftware.net/blog >
                        To email me, use "apoelstra" at the above address.
                        It's just like stealing teeth from a baby.

                        Comment

                        • Keith Thompson

                          #13
                          Re: single-quoting a macro argument

                          "Robert Gamble" <rgamble99@gmai l.com> writes:[color=blue]
                          > Marius Lazer wrote:[color=green]
                          >> Is it possible to write a macro that single-quotes its argument?
                          >>
                          >> #define SOME_MACRO(x)
                          >>
                          >> such that SOME_MACRO(foo) expands to 'foo'[/color]
                          >
                          > I *think* this is Standard, someone will correct me if its not:
                          >
                          > #define QUOTE(x) '
                          > #define foo2(x) x
                          > #define foo(x) QUOTE(x)foo2(x) QUOTE(x)
                          >
                          > foo(test)[/color]

                          Interesting. That seems to work on at least one compiler, but I
                          believe it invokes undefined behavior. The source file is decomposed
                          into preprocessing tokens in translation phase 3; preprocessing
                          directives and macros are handled in phase 4. C99 6.4 defines
                          preprocessing tokens:

                          The categories of preprocessing tokens are: header names,
                          identifiers, preprocessing numbers, character constants, string
                          literals, punctuators, and single non-white-space characters that
                          do not lexically match the other preprocessing token
                          categories. If a ' or a " character matches the last category, the
                          behavior is undefined.

                          --
                          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.

                          Comment

                          • Ben Pfaff

                            #14
                            Re: single-quoting a macro argument

                            "Robert Gamble" <rgamble99@gmai l.com> writes:
                            [color=blue]
                            > I *think* this is Standard, someone will correct me if its not:
                            >
                            > #define QUOTE(x) '[/color]

                            No, it's not, on the basis of that line alone. The replacement
                            list in a macro definition must be a sequence of preprocessing
                            tokens. A lone single-quote is not a valid preprocessing token.
                            In fact C99 section 6.4 explicitly disallows the possibility:

                            preprocessing-token:
                            header-name
                            identifier
                            pp-number
                            character-constant
                            string-literal
                            punctuator
                            each non-white-space character that cannot be one of the above

                            ....

                            The categories of preprocessing tokens are: header names,
                            identifiers, preprocessing numbers, character constants,
                            string literals, punctuators, and single non-white-space
                            characters that do not lexically match the other
                            preprocessing token categories.58) If a ' or a " character
                            matches the last category, the behavior is undefined.
                            --
                            "...Almost makes you wonder why Heisenberg didn't include postinc/dec operators
                            in the uncertainty principle. Which of course makes the above equivalent to
                            Schrodinger's pointer..."
                            --Anthony McDonald

                            Comment

                            • Marius Lazer

                              #15
                              Re: single-quoting a macro argument

                              [color=blue]
                              > SOME_MACRO(a);[/color]

                              Yeah, but the line above should have expanded to 'a', not 'x'. I tried
                              this first...

                              Marius

                              Comment

                              Working...