Link to home
Start Free TrialLog in
Avatar of altaf_techie
altaf_techieFlag for India

asked on

print hello world with empty main() body in C++

Hello freinds,
well i want to print something like "Hello world" or anything else without writing anything in the main() i.e. the main()function should be completely empty.
Thanks in advance
Avatar of smitty1276
smitty1276

You could globally instanciate a class that prints "Hello world" in its constructor.
That is not guaranteed to work.  You cannot be sure that standard output will be available at the time at which the object is constructed.  (It does work on many compilers however.)
I am not entirely sure whether it will definitely work or not.

The standard says only that an object with global (that is global namespace) scope must be initialised before it is used, hence potentially, if unused it could remain uninitialised.  However, I did find this throw away foot note:

'An object defined in namespace scope having initialization with sideeffects must be initialized even if it is not used'

what the exact definition of a sideeffect is, I am not sure.
Actually I looked a little deeper, and found this statement:

' ...calling a library I/O function, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. '

so from this I believe that, since the initialisation of such a constructor would generate a side effect, the object should, in fact, definitely be initialised.

However, this is what the standard says...what a compiler actually does is something else entirely... :-)

But you don't now that standard output is open and ready until main().  I believe there was a little blurb (side note) about this in Exceptional C++, but I can't find it now.

However, there is a section in effective C++ that seems to say that stream objects at least are constructed...  Now I am confussed.   I wish I could find that quote from Sutler.
Oh, I think you maybe right ... order of initialisation is not assured between translation units.... so maybe cout is not initialised.  So I suppose, perversely enough, you maybe better off using printf in this situation, which probably does not rely on any static initialisation?
Avatar of altaf_techie

ASKER

sorry but this doesnt work...anyway thanx for the answer
> sorry but this doesnt work...anyway thanx for the answer

not true, the answer was good, example:

#include <iostream>

using namespace std;

class Hello
{
public:
   Hello() { cout << "Hello, World!" << endl; }
};

Hello myHello;

int main()
{
}
I'm sure that will often work.  I'm not sure that will always work.
> I'm sure that will often work.  
> I'm not sure that will always work.

true, but I suspect that it usually works, it would be interesting to know on which compilers it did fail.  As I mentioned, it is possible that printf is probably slightly more reliable than cout in this case.
Does DllMain make sure that stdout is available? I.e:

// dll.c
#include <stdio.h>
#include <windows.h>

BOOL WINAPI DllMain( HINSTANCE handle,
                     DWORD fdwReason,
                     LPVOID lpvReserved )
{
    if( fdwReason == DLL_PROCESS_ATTACH ){
        printf( "Hello World\n" );
    }
    return TRUE;
}
============

// main.c
#include <windows.h>

HINSTANCE i = LoadLibrary( "dll.dll");

void main()
{
}
>> As I mentioned, it is possible that
>> printf is probably slightly more
>> reliable than cout in this
>> case.
I don't think so.  The effective C++ article shows how you are ensured that the standard stream objects are initalized before you use them.  The problem is, if I remember correctly, you don't know that the standard C streams are open yet, so that might not help you.  Unfortunately, I can't seem to find the reference...

>> Does DllMain make sure that stdout is available? I.e:
According to the C++ standard?  

there is no mention of DllMain.  it depends on what the compiler designers decide to do.  In Vc you can be sure that the C and C++ streams work by the time DllMain is called.
Why would stdout not be available immediately after the inclusion of the appropriate header file?
because a file must be open in order to use it.
I think that maybe we  display  "Hello world" in destructor. Is it a solution ?
File as in stdout file descriptor?
Avatar of Juan Carlos
Make a function printing a "Hello world..." and in Link options put the name of this function in Entry point symbol.
Now, I sure that one solution is to put " printf("Hello, world"); " in destructor of global object.
The ISO/IEC 14882 standard allows to use stdout at the time  the global object is destructor.


#include <stdio.h>


         class Hello
         {
         public:
           ~Hello() { printf("Hello, World!") ; }
         };

         Hello myHello;

         int main()
         {
         }
         
         
Information gets from ISO/IEC 14882


Page 43 :
    In the "3.6.1 Main function " claims
   
    3.6.1.5  after finish "main() function", exit() function will call
   
Page 336 :
    In the "18.3 Start and termination" claims
   
    "18.3.8 The function exit() has additional behavior in this International standard
   
      - First, objects with static storage duration are destroy ....
     
      - Next, all open C stream (...) with unwrite buffered data are flushed, all C
        open streams are close ... "
       
From above information, we can use printf in destructor of "the global object"
       
     
===================================================================
     
More information
   
Page 602 :

    In the " 27.3 standard iostream objects", these is a foot note "256" that claims
   
    "Constructors and destructors for static objects can access these objects
    (I think they are cin, cout, cerr) to read input from stdin or write output
    to stdout or stderr".

From above information, maybe puting "hello world" in constructor is ok. But I am
not sure 100%.
ASKER CERTIFIED SOLUTION
Avatar of smitty1276
smitty1276

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well smitty1276 u were the very first time...but there was some problem with my code.well thanx for the answer.