macros

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

    #1

    macros

    Hi

    Can anyone explain why the output of the following code:

    #define A 1
    #define B 2
    A.B

    after preprocessing only, is:

    1 . 2

    Why does the preprocessor put spaces between each character, when the
    macro invocation has none?

  • =?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=

    #2
    Re: macros

    On Fri, 05 Oct 2007 00:04:18 +0200, Silent Witness wrote:
    Hi
    >
    Can anyone explain why the output of the following code:
    >
    #define A 1
    #define B 2
    A.B
    >
    after preprocessing only, is:
    >
    1 . 2
    >
    Why does the preprocessor put spaces between each character, when the
    macro invocation has none?
    Probably to make sure that it won't be interpreted as a single floating-
    point number after preprocessing. It is an integer constant, followed by
    a period, followed by another integer constant, and the compiler has to
    make sure it is interpreted as that. It may do so using whitespace, and
    in your case does. Anyway, standard C does not require preprocessor
    output to be available at all except when stringizing:

    #define A 1
    #define B 2
    #define STR(x) STR_(x)
    #define STR_(x) #x

    #include <stdio.h>
    int main(void) {
    puts(STR(A.B));
    return 0;
    }

    If your implementation prints "1 . 2\n" in this case too, then it is a
    bug. However, I would expect it will print "1.2\n" here.

    Comment

    • Richard Tobin

      #3
      Re: macros

      In article <slrnfgaor2.h7v .nospam@nospam. com>,
      Silent Witness <nospam@invalid .invalidwrote:
      >Can anyone explain why the output of the following code:
      >
      >#define A 1
      >#define B 2
      >A.B
      >
      >after preprocessing only, is:
      >
      >1 . 2
      >
      >Why does the preprocessor put spaces between each character, when the
      >macro invocation has none?
      C preprocessing is not defined as a textual transformation, but as a
      transformation between sequences of tokens. There's no need for a C
      compiler to be able to output the result of preprocessing as text - it
      can just produce some internal representation suitable for the next
      stage of compilation.

      Of course, it's traditional for the preprocessor to be usable by
      itself, and to ensure that the textual representation corresponds
      to the correct sequence of tokens it's putting spaces between them.

      If you want to combine two tokens into one, you need to use "token
      pasting" (Google for it).

      -- Richard
      --
      "Considerat ion shall be given to the need for as many as 32 characters
      in some alphabets" - X3.4, 1963.

      Comment

      • CBFalconer

        #4
        Re: macros

        Silent Witness wrote:
        >
        Can anyone explain why the output of the following code:
        >
        #define A 1
        #define B 2
        A.B
        >
        after preprocessing only, is:
        >
        1 . 2
        >
        Why does the preprocessor put spaces between each character, when
        the macro invocation has none?
        Because A is a token, B is a token, and '.' is a token. What if:

        #define A sizeof
        #define B stdin

        do you want A B translated as "sizeofstdi n" or "sizeof stdin"?
        Injection of spaces preserves the 'tokenness' of the individual
        items.

        --
        Chuck F (cbfalconer at maineline dot net)
        Available for consulting/temporary embedded and systems.
        <http://cbfalconer.home .att.net>



        --
        Posted via a free Usenet account from http://www.teranews.com

        Comment

        • Kenneth Brody

          #5
          Re: macros

          CBFalconer wrote:
          >
          Silent Witness wrote:

          Can anyone explain why the output of the following code:

          #define A 1
          #define B 2
          A.B

          after preprocessing only, is:

          1 . 2

          Why does the preprocessor put spaces between each character, when
          the macro invocation has none?
          >
          Because A is a token, B is a token, and '.' is a token. What if:
          [...]

          Didn't we have this exact question posted a few weeks ago? I
          remember, because my compiler's preprocessor would generate "1.B"
          as its output, and we had a whole discussion here as to whether
          that was legal or not.

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

          Comment

          • pete

            #6
            Re: macros

            Kenneth Brody wrote:
            >
            CBFalconer wrote:

            Silent Witness wrote:
            >
            Can anyone explain why the output of the following code:
            >
            #define A 1
            #define B 2
            A.B
            >
            after preprocessing only, is:
            >
            1 . 2
            >
            Why does the preprocessor put spaces between each character, when
            the macro invocation has none?
            Because A is a token, B is a token, and '.' is a token. What if:
            [...]
            >
            Didn't we have this exact question posted a few weeks ago?
            Yes, we did.

            --
            pete

            Comment

            • Martin Wells

              #7
              Re: macros

              Chuck:
              Because A is a token, B is a token, and '.' is a token. What if:
              >
              #define A sizeof
              #define B stdin
              >
              do you want A B translated as "sizeofstdi n" or "sizeof stdin"?
              Injection of spaces preserves the 'tokenness' of the individual
              items.

              I don't see your point. If you write "A B", then obviously you already
              have a space and it should be "sizeof stdin". As for "A.B", well
              that's a different kettle of fish altogether.

              I myself wuda thought that you'd be left with "sizeof.std in" as
              opposed to "sizeof . stdin"... but then I don't know the intracies at
              play here.

              Martin

              Comment

              • CBFalconer

                #8
                Re: macros

                Martin Wells wrote:
                Chuck:
                >
                >Because A is a token, B is a token, and '.' is a token. What if:
                >>
                >#define A sizeof
                >#define B stdin
                >>
                >do you want A B translated as "sizeofstdi n" or "sizeof stdin"?
                >Injection of spaces preserves the 'tokenness' of the individual
                >items.
                >
                I don't see your point. If you write "A B", then obviously you
                already have a space and it should be "sizeof stdin". As for "A.B",
                well that's a different kettle of fish altogether.
                >
                I myself wuda thought that you'd be left with "sizeof.std in" as
                opposed to "sizeof . stdin"... but then I don't know the intracies
                at play here.
                No you don't "have a space". Spaces, in C, are to be ignored,
                EXCEPT that they serve to delimit the edges of tokens. The C
                preprocessor handles only text, so it has to inject spaces to keep
                tokens separate. It absorbs the token A, looks it up, and spits
                out the translation, followed by a delimiting space, then absorbs
                the token B, and repeats the process.

                --
                Chuck F (cbfalconer at maineline dot net)
                Available for consulting/temporary embedded and systems.
                <http://cbfalconer.home .att.net>



                --
                Posted via a free Usenet account from http://www.teranews.com

                Comment

                • Keith Thompson

                  #9
                  Re: macros

                  CBFalconer <cbfalconer@yah oo.comwrites:
                  Martin Wells wrote:
                  >Chuck:
                  >>
                  >>Because A is a token, B is a token, and '.' is a token. What if:
                  >>>
                  >>#define A sizeof
                  >>#define B stdin
                  >>>
                  >>do you want A B translated as "sizeofstdi n" or "sizeof stdin"?
                  >>Injection of spaces preserves the 'tokenness' of the individual
                  >>items.
                  >>
                  >I don't see your point. If you write "A B", then obviously you
                  >already have a space and it should be "sizeof stdin". As for "A.B",
                  >well that's a different kettle of fish altogether.
                  >>
                  >I myself wuda thought that you'd be left with "sizeof.std in" as
                  >opposed to "sizeof . stdin"... but then I don't know the intracies
                  >at play here.
                  >
                  No you don't "have a space". Spaces, in C, are to be ignored,
                  EXCEPT that they serve to delimit the edges of tokens. The C
                  preprocessor handles only text, so it has to inject spaces to keep
                  tokens separate. It absorbs the token A, looks it up, and spits
                  out the translation, followed by a delimiting space, then absorbs
                  the token B, and repeats the process.
                  The output of the preprocessor is a sequence of tokens. It isn't
                  necessarily text.

                  A typical *implementation * emits text that looks very much like C
                  code; such an implementation will sometimes need to emit spaces to
                  separate adjacent tokens. But that's just an internal implementation
                  detail.

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