• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 705
  • Last Modified:

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
0
altaf_techie
Asked:
altaf_techie
  • 5
  • 5
  • 3
  • +4
1 Solution
 
smitty1276Commented:
You could globally instanciate a class that prints "Hello world" in its constructor.
0
 
nietodCommented:
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.)
0
 
jasonclarkeCommented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jasonclarkeCommented:
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... :-)

0
 
nietodCommented:
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.
0
 
jasonclarkeCommented:
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?
0
 
altaf_techieAuthor Commented:
sorry but this doesnt work...anyway thanx for the answer
0
 
jasonclarkeCommented:
> 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()
{
}
0
 
nietodCommented:
I'm sure that will often work.  I'm not sure that will always work.
0
 
jasonclarkeCommented:
> 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.
0
 
kulinaCommented:
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()
{
}
0
 
nietodCommented:
>> 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.
0
 
smitty1276Commented:
Why would stdout not be available immediately after the inclusion of the appropriate header file?
0
 
nietodCommented:
because a file must be open in order to use it.
0
 
thienpnguyenCommented:
I think that maybe we  display  "Hello world" in destructor. Is it a solution ?
0
 
kulinaCommented:
File as in stdout file descriptor?
0
 
jcgdCommented:
Make a function printing a "Hello world..." and in Link options put the name of this function in Entry point symbol.
0
 
thienpnguyenCommented:
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%.
0
 
smitty1276Commented:
I'm sorry guys... but I'm having a hard time believing that ANY compiler wouldn't allow you to access stdout at anytime after including the appropriate header files, which define the functions needed to access it.   If the functions included were dependent upon some stream being initialized, but don't initialize the stream themselves, then they are VERY poorly written... I find that to be VERY unlikely.  I would suggest that if an operating system is already running, which it certainly will be, everything needed will already be in place, because the OS handles that.

nietod...

>because a file must be open in order to use it.

What file?

altaf_techie... copy and paste this code and compile and run it.
--------------------------------------------

#include <iostream.h>

class Test
{
public:
  Test() { cout << "Testing..." << endl; };
};

Test test;

void main( void )
{
}


0
 
altaf_techieAuthor Commented:
well smitty1276 u were the very first time...but there was some problem with my code.well thanx for the answer.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now