Empty Main

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

    #1

    Empty Main

    Could anyone tell me how to display "Hello World" in C++ without
    writing anything inside main?i.e, main should not contain even a
    single statement (no object creation or no cout or anything)

  • Ivan Vecerina

    #2
    Re: Empty Main

    "Raj" <raj.reshmi@gma il.com> wrote in message
    news:1140593268 .885746.174340@ f14g2000cwb.goo glegroups.com.. .
    : Could anyone tell me how to display "Hello World" in C++ without
    : writing anything inside main?i.e, main should not contain even a
    : single statement (no object creation or no cout or anything)

    Have you studied the construction/destruction of global
    objects yet ?


    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



    Comment

    • Harry

      #3
      Re: Empty Main

      Try the following:

      #include <iostream>
      using namespace std;

      class t
      {
      public:
      t(){ std::cout<<"Hel lo world!"<<std::e ndl; }
      };

      t _t;

      int main()
      {

      }

      This should work.

      Comment

      • Fred Zwarts

        #4
        Re: Empty Main

        "Harry" <hapordigi@gmai l.com> wrote in message news:1140599038 .819812.7910@g1 4g2000cwa.googl egroups.com...[color=blue]
        > Try the following:
        >
        > #include <iostream>
        > using namespace std;
        >
        > class t
        > {
        > public:
        > t(){ std::cout<<"Hel lo world!"<<std::e ndl; }
        > };
        >
        > t _t;
        >
        > int main()
        > {
        >
        > }
        >
        > This should work.[/color]

        If have seen that this works on some platforms.
        On other platforms, however, it failed because cout is also a static object
        and the constructor of _t was called when cout had not yet been constructed.
        In such cases I use printf.

        Fred.Zwarts.

        Comment

        • Ivan Vecerina

          #5
          Re: Empty Main

          "Fred Zwarts" <F.Zwarts@KVI.n l> wrote in message
          news:dthkko$p74 $1@info.service .rug.nl...
          :"Harry" <hapordigi@gmai l.com> wrote in message
          news:1140599038 .819812.7910@g1 4g2000cwa.googl egroups.com...
          :> Try the following:
          :>
          :> #include <iostream>
          :> using namespace std;
          :>
          :> class t
          :> {
          :> public:
          :> t(){ std::cout<<"Hel lo world!"<<std::e ndl; }
          :> };
          :>
          :> t _t;
          :>
          :> int main()
          :> {
          :>
          :> }
          :>
          :> This should work.
          :
          :If have seen that this works on some platforms.
          :On other platforms, however, it failed because cout is also a static
          object
          :and the constructor of _t was called when cout had not yet been
          constructed.
          :In such cases I use printf.

          The C++ standard requires the above to work, and most of today's
          implementations will be compliant with respect to this (there is
          a common/published trick for the standard library to ensure the
          timely initialization of std::cout).

          Ivan
          --
          http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
          Brainbench MVP for C++ <> http://www.brainbench.com


          Comment

          • Raj

            #6
            Re: Empty Main

            Thanks a lot all of you..
            I tried it and its working....
            Thank you

            Comment

            • Csaba

              #7
              Re: Empty Main

              "Raj" <raj.reshmi@gma il.com> wrote in news:1140593268 .885746.174340
              @f14g2000cwb.go oglegroups.com:
              [color=blue]
              > Could anyone tell me how to display "Hello World" in C++ without
              > writing anything inside main?i.e, main should not contain even a
              > single statement (no object creation or no cout or anything)
              >[/color]

              ---- begin hello.cxx ----
              char *p = "Hellow, world";
              ---- end hello.cpp ----

              Hey look, no main() at all !

              Compile with one of:

              g++ -E hello.cxx
              g++ -S hello.cxx

              ( alas, g++ -c hello.cxx -o - doesn't seem to work )-:


              Of course this prints some extraneous stuff, which can safely be ingnored
              :-)

              --
              Life is complex, with real and imaginary parts.

              Comment

              • GB

                #8
                Re: Empty Main

                Harry wrote:[color=blue]
                > Try the following:
                >
                > #include <iostream>
                > using namespace std;
                >
                > class t
                > {
                > public:
                > t(){ std::cout<<"Hel lo world!"<<std::e ndl; }
                > };
                >
                > t _t;
                >
                > int main()
                > {
                >
                > }
                >
                > This should work.
                >[/color]

                A simpler approach that doesn't require defining a class is this:

                #include <iostream>

                std::ostream& s = std::cout << "Hello World\n";

                int main()
                {
                }

                Comment

                • roberts.noah@gmail.com

                  #9
                  Re: Empty Main


                  Raj wrote:[color=blue]
                  > Thanks a lot all of you..
                  > I tried it and its working....
                  > Thank you[/color]

                  Yeah, it's great when people do your homework for you isn't it.

                  Comment

                  • Ron Natalie

                    #10
                    Re: Empty Main

                    Harry wrote:[color=blue]
                    > Try the following:
                    >
                    > #include <iostream>
                    > using namespace std;
                    >
                    > class t
                    > {
                    > public:
                    > t(){ std::cout<<"Hel lo world!"<<std::e ndl; }
                    > };
                    >
                    > t _t;
                    >
                    > int main()
                    > {
                    >
                    > }
                    >
                    > This should work.
                    >[/color]
                    _t is an improper symbol name. Leading underscores in
                    the global namespace are reserved to the implementation.


                    Comment

                    • Fred Zwarts

                      #11
                      Re: Empty Main

                      Related to this topic, consider the following program:

                      class t
                      {
                      public:
                      t() { std::cout<<"Hel lo world!"<<std::e ndl; }
                      ~t() { std::cout<<"Goo dbye world!"<<std::e ndl; }
                      };

                      t T;

                      int main()
                      {

                      }

                      I understood from the discussion
                      that the C++ standard defines that the constructor of T is called after the construction of cout.
                      Does the C++ standard also tell that the destructor of T is called before the destruction of cout?

                      Regards,
                      Fred.Zwarts.

                      Comment

                      Working...