Is this valid?

Discussion in 'C++' started by Michael, Oct 27, 2004.

  1. Michael

    Michael Guest

    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?
     
    Michael, Oct 27, 2004
    #1
    1. Advertising

  2. Michael wrote:

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



    It is valid.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Oct 27, 2004
    #2
    1. Advertising

  3. Michael

    Matt Wharton Guest

    "Michael" <> wrote in message
    news:clp7u1$dje$...
    > 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?
    >
    >


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

    -Matt
     
    Matt Wharton, Oct 27, 2004
    #3
  4. Michael

    Michael Guest

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

    "Matt Wharton" <> wrote in message
    news:...
    > "Michael" <> wrote in message
    > news:clp7u1$dje$...
    > > 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?
    > >
    > >

    >
    > Yes, it's valid. Yes, it's obscure (and entirely unnecessary in the

    example
    > you posted).
    >
    > -Matt
    >
    >
     
    Michael, Oct 28, 2004
    #4
  5. "Michael" <> wrote in message
    news:clpffo$k93$...
    > in that case, does #include mean copy contents of file here ( with the
    > absence of pragma's)?


    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
     
    Jonathan Turkanis, Oct 28, 2004
    #5
  6. Michael wrote:

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



    #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

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Oct 28, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aschel Kritsos

    Valid file types

    Aschel Kritsos, Nov 11, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    598
  2. Ajit
    Replies:
    1
    Views:
    3,614
    Victor Garcia Aprea [MVP]
    Apr 24, 2004
  3. CT

    vbc is not valid command

    CT, Oct 30, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    2,844
  4. Pial
    Replies:
    6
    Views:
    52,415
    Nassos
    Apr 28, 2005
  5. MM
    Replies:
    3
    Views:
    450
    Kai Harrekilde-Petersen
    Jun 19, 2004
Loading...

Share This Page