Monday, September 25, 2006

Print something outside a main!

hi All!

Came after a long break! Cant make time actually in this boringly busy schedule of my job! Dint read anything technical as such but couldn't leave this blog inactive for this long! Hence posting something interesting for those who dont know about this!

I ask you to print something, say yo name through a program!
you write,

#include < iostream >
#include < iomanip &gt
using std::cout;
using std::endl;

int main()
{
cout << "Sandesh" << endl;
}

Simple right???

Now i ask you to print yo name without writing any output statements in main. You say, Fine! I'll declare a function, write the output statement over there n there you have yo program. i.e.,

#include < iostream >
#include < iomanip &gt

using std::cout;
using std::endl;

void foo()
{
cout << "Sandesh" << endl;
}

int main()
{
foo();
return 0;
}

Haha! I dint say that way! I want neither any output statements in main nor any functions using such things in main!!

Thinking??? Here goes the solution!

#include < iostream >
#include < iomanip &gt

int foo()
{
cout << "Sandesh" << endl;
return 0;
}

static int x = foo();

int main()
{
return 0;
}

Sumthin interestin right???

3 Comments:

Anonymous Anonymous said...

why do u need to declare x as static.. and not just int x.

5:19 AM  
Blogger Unknown said...

This comment has been removed by a blog administrator.

5:25 AM  
Anonymous Anonymous said...

but i tried this on a STL compliant compiler(dono what compiler u use or dono if it is compiler dependent in first place).. and it works fine by just declaring x as a global variable.. so try it out...

5:27 AM  

Post a Comment

<< Home