Is this valid?

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

    #1

    Is this valid?

    Hi i recently came across some code that looked like this which I've never
    ever seen before! I appreicate it may be 'c' but is it valid in c++??


    //main.c

    int main()
    {
    int a;
    int b;
    //Get values for a & b;

    if(a==1)
    {
    #include "DoA.c"
    }
    else
    {
    #include "DoB.c"
    }

    // More stuff


    return 0;
    }


    //DoA.c
    b =4;


    //DoB.c
    b=6;



    Is this valid? Or just obscure?


  • Matt Wharton

    #2
    Re: Is this valid?

    "Michael" <slick_mick_00@ hotmail.com> wrote in message
    news:clp7u1$dje $1@sparta.btint ernet.com...[color=blue]
    > Hi i recently came across some code that looked like this which I've never
    > ever seen before! I appreicate it may be 'c' but is it valid in c++??
    >
    >
    > //main.c
    >
    > int main()
    > {
    > int a;
    > int b;
    > //Get values for a & b;
    >
    > if(a==1)
    > {
    > #include "DoA.c"
    > }
    > else
    > {
    > #include "DoB.c"
    > }
    >
    > // More stuff
    >
    >
    > return 0;
    > }
    >
    >
    > //DoA.c
    > b =4;
    >
    >
    > //DoB.c
    > b=6;
    >
    >
    >
    > Is this valid? Or just obscure?
    >
    >[/color]

    Yes, it's valid. Yes, it's obscure (and entirely unnecessary in the example
    you posted).

    -Matt


    Comment

    • Ioannis Vranos

      #3
      Re: Is this valid?

      Michael wrote:
      [color=blue]
      > Hi i recently came across some code that looked like this which I've never
      > ever seen before! I appreicate it may be 'c' but is it valid in c++??
      >
      >
      > //main.c
      >
      > int main()
      > {
      > int a;
      > int b;
      > //Get values for a & b;
      >
      > if(a==1)
      > {
      > #include "DoA.c"
      > }
      > else
      > {
      > #include "DoB.c"
      > }
      >
      > // More stuff
      >
      >
      > return 0;
      > }
      >
      >
      > //DoA.c
      > b =4;
      >
      >
      > //DoB.c
      > b=6;
      >
      >
      >
      > Is this valid? Or just obscure?[/color]


      It is valid.



      --
      Ioannis Vranos


      Comment

      • Michael

        #4
        Re: Is this valid?

        in that case, does #include mean copy contents of file here ( with the
        absence of pragma's)?

        "Matt Wharton" <noSpam@noSpam. com> wrote in message
        news:1098916940 .753896@cswreg. cos.agilent.com ...[color=blue]
        > "Michael" <slick_mick_00@ hotmail.com> wrote in message
        > news:clp7u1$dje $1@sparta.btint ernet.com...[color=green]
        > > Hi i recently came across some code that looked like this which I've[/color][/color]
        never[color=blue][color=green]
        > > ever seen before! I appreicate it may be 'c' but is it valid in c++??
        > >
        > >
        > > //main.c
        > >
        > > int main()
        > > {
        > > int a;
        > > int b;
        > > //Get values for a & b;
        > >
        > > if(a==1)
        > > {
        > > #include "DoA.c"
        > > }
        > > else
        > > {
        > > #include "DoB.c"
        > > }
        > >
        > > // More stuff
        > >
        > >
        > > return 0;
        > > }
        > >
        > >
        > > //DoA.c
        > > b =4;
        > >
        > >
        > > //DoB.c
        > > b=6;
        > >
        > >
        > >
        > > Is this valid? Or just obscure?
        > >
        > >[/color]
        >
        > Yes, it's valid. Yes, it's obscure (and entirely unnecessary in the[/color]
        example[color=blue]
        > you posted).
        >
        > -Matt
        >
        >[/color]


        Comment

        • Jonathan Turkanis

          #5
          Re: Is this valid?


          "Michael" <slick_mick_00@ hotmail.com> wrote in message
          news:clpffo$k93 $1@titan.btinte rnet.com...[color=blue]
          > in that case, does #include mean copy contents of file here ( with the
          > absence of pragma's)?[/color]

          It means exactly the same thing as it does when used at the beginning of a file.
          The preprocessor knows nothing about control structures such as if ( ) { }else
          { }, and so has no idea that headers are being #include'd in unusual places.

          The ability to #include headers in the middle of a block of code is very useful
          for preprocess metaprogramming (see
          http://boost-consulting.com/tmpbook/preprocessor.html)

          Jonathan


          Comment

          • Ioannis Vranos

            #6
            Re: Is this valid?

            Michael wrote:
            [color=blue]
            > in that case, does #include mean copy contents of file here ( with the
            > absence of pragma's)?[/color]


            #includes are processed by the preprocessor before the compilation. In
            the final source code that gets compiled, the #includes have been
            replaced by the text that they include.



            --
            Ioannis Vranos


            Comment

            Working...