?
Solved

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

Posted on 2001-08-31
20
Medium Priority
?
694 Views
Last Modified: 2006-11-17
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
Comment
Question by:altaf_techie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +4
20 Comments
 
LVL 2

Expert Comment

by:smitty1276
ID: 6443945
You could globally instanciate a class that prints "Hello world" in its constructor.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6444086
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
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6444182
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:jasonclarke
ID: 6444190
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
 
LVL 22

Expert Comment

by:nietod
ID: 6444234
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
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6444384
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
 
LVL 1

Author Comment

by:altaf_techie
ID: 6445085
sorry but this doesnt work...anyway thanx for the answer
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6445102
> 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
 
LVL 22

Expert Comment

by:nietod
ID: 6445128
I'm sure that will often work.  I'm not sure that will always work.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6445216
> 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
 
LVL 3

Expert Comment

by:kulina
ID: 6445769
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
 
LVL 22

Expert Comment

by:nietod
ID: 6446504
>> 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
 
LVL 2

Expert Comment

by:smitty1276
ID: 6446658
Why would stdout not be available immediately after the inclusion of the appropriate header file?
0
 
LVL 22

Expert Comment

by:nietod
ID: 6446674
because a file must be open in order to use it.
0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6446739
I think that maybe we  display  "Hello world" in destructor. Is it a solution ?
0
 
LVL 3

Expert Comment

by:kulina
ID: 6446741
File as in stdout file descriptor?
0
 
LVL 3

Expert Comment

by:jcgd
ID: 6446843
Make a function printing a "Hello world..." and in Link options put the name of this function in Entry point symbol.
0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6446848
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
 
LVL 2

Accepted Solution

by:
smitty1276 earned 60 total points
ID: 6447117
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
 
LVL 1

Author Comment

by:altaf_techie
ID: 6447196
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month12 days, 10 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question