Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   a tricky one (http://www.go4expert.com/forums/a-tricky-one-t7683/)

sanghamitra_behera 3Dec2007 12:24

a tricky one
 
hi all,
i am new to this group. but the reason i am here is to explore more on c++.hope i will get good response from other members on this site.

i have a tricky question . can anybody give me some idea on it.

=>how to print "Hello World" without anything inside main function.

for example:

int main()
{

}

Note: here we can do anything outside main , but the result should print hello world.

shabbir 3Dec2007 17:38

Re: a tricky one
 
Code:

int main()
{

}
int f()
{
  return printf("Hello world");
}
int a = f();


shabbir 3Dec2007 17:55

Re: a tricky one
 
Moved to C-C++ forum.

sanghamitra_behera 3Dec2007 18:21

Re: a tricky one
 
Thanks a lot.

i have been trying trying it from yesterday.Doing stuffs like creating static char variable and initializing it with hello world.

as static variables dont need class instance, so i thought it will work.
whats wrong with it..........

plz clear me..........

thanx again.

shabbir 3Dec2007 18:53

Re: a tricky one
 
There is nothing wrong with it and there exist more than one solution. Here is one more.
Code:

int main()
{

}
int a = printf("Hello world");


sanghamitra_behera 4Dec2007 09:44

Re: a tricky one
 
ya, there may be more than one solution.
but this one is not working why?
Code:

class test
{
      static int func();
};

int test::func()
{
      return  printf("hello world\n");
}

int main()
{

}


shabbir 4Dec2007 09:47

Re: a tricky one
 
You are just not calling the function and so its not printing. Also remember to use the code block when you have code in the posts.

sanghamitra_behera 4Dec2007 10:02

Re: a tricky one
 
Then can i know what is the role of a static function in c++ and why this pointer is not accessible by static functions.

shabbir 4Dec2007 11:36

Re: a tricky one
 
A simple answer could be static functions can be called without the class object. You can call the function like this.
int i = test::func();
but if test::func() is not static then you need an object of class test to call the function func

Bhullarz 28Dec2007 10:16

Re: a tricky one
 
Quote:

Originally Posted by shabbir
There is nothing wrong with it and there exist more than one solution. Here is one more.
Code:

int main()
{

}
int a = printf("Hello world");


Seems nice bro. But I tried your code,but it is giving error "illegal Initialization".
Plz explain.....


All times are GMT +5.5. The time now is 07:29.