Preprocessing directives

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

    Preprocessing directives

    Please, consider the following code:

    const int a = 1;
    #if a == 1
    #define VAR 200
    #else
    #define VAR 100
    #endif

    int main() {
    cout << VAR << endl;
    return 0;
    }

    Under some compiler it prints 200. So a preprocessing directive can check
    constant values. Is it a standard C++ feature?

    But how can be possible? Syntax analysis, following the standard, is done
    after all preprocessing directive are executed, so how can an #if
    be aware about a "const int" construct?

    Regards.

  • Rolf Magnus

    #2
    Re: Preprocessing directives

    Ron wrote:
    [color=blue]
    > Please, consider the following code:
    >[/color]

    #include <iostream>
    using std::cout;
    using std::endl;
    [color=blue]
    > const int a = 1;
    > #if a == 1
    > #define VAR 200
    > #else
    > #define VAR 100
    > #endif
    >
    > int main() {
    > cout << VAR << endl;
    > return 0;
    > }
    >
    > Under some compiler it prints 200. So a preprocessing directive can
    > check constant values. Is it a standard C++ feature?[/color]

    I doubt that.
    [color=blue]
    > But how can be possible? Syntax analysis, following the standard, is
    > done after all preprocessing directive are executed, so how can an #if
    > be aware about a "const int" construct?[/color]

    It can't. That's why I doubt it. But I'm not a language guru.


    Comment

    • Dan Cernat

      #3
      Re: Preprocessing directives

      Rolf Magnus <ramagnus@t-online.de> wrote in message news:<boqh0e$38 5$07$4@news.t-online.com>...[color=blue]
      > Ron wrote:
      >[color=green]
      > > Please, consider the following code:
      > >[/color]
      >
      > #include <iostream>
      > using std::cout;
      > using std::endl;
      >[color=green]
      > > const int a = 1;
      > > #if a == 1
      > > #define VAR 200
      > > #else
      > > #define VAR 100
      > > #endif
      > >
      > > int main() {
      > > cout << VAR << endl;
      > > return 0;
      > > }
      > >
      > > Under some compiler it prints 200. So a preprocessing directive can
      > > check constant values. Is it a standard C++ feature?[/color]
      >
      > I doubt that.
      >[/color]

      FYI

      VC 7.1 prints 100

      /dan

      Comment

      • Jack Klein

        #4
        Re: Preprocessing directives

        On Tue, 11 Nov 2003 11:12:31 GMT, Ron <no@spam.com> wrote in
        comp.lang.c++:
        [color=blue]
        > Please, consider the following code:
        >
        > const int a = 1;
        > #if a == 1
        > #define VAR 200
        > #else
        > #define VAR 100
        > #endif
        >
        > int main() {
        > cout << VAR << endl;
        > return 0;
        > }
        >
        > Under some compiler it prints 200. So a preprocessing directive can check
        > constant values. Is it a standard C++ feature?[/color]

        Any compiler that outputs 200 is broken. It is not a standard
        feature, it is a non-conforming "feature".
        [color=blue]
        > But how can be possible? Syntax analysis, following the standard, is done
        > after all preprocessing directive are executed, so how can an #if
        > be aware about a "const int" construct?
        >
        > Regards.[/color]

        There are almost an infinite number of ways one could write a compiler
        to create this defect. If you want to know which one occurred in this
        particular compiler, you need to either examine the source code if
        available, or ask the people who wrote it.

        --
        Jack Klein
        Home: http://JK-Technology.Com
        FAQs for
        comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
        comp.lang.c++ http://www.parashift.com/c++-faq-lite/
        alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

        Comment

        Working...