1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

a tricky one

Discussion in 'C' started by sanghamitra_behera, Dec 3, 2007.

  1. sanghamitra_behera

    sanghamitra_behera New Member

    Joined:
    Dec 3, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Code:
    int main()
    {
    
    }
    int f()
    {
      return printf("Hello world");
    }
    int a = f();
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Moved to C-C++ forum.
     
  4. sanghamitra_behera

    sanghamitra_behera New Member

    Joined:
    Dec 3, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    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");
     
  6. sanghamitra_behera

    sanghamitra_behera New Member

    Joined:
    Dec 3, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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()
    {
    
    }
     
    Last edited by a moderator: Dec 4, 2007
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  8. sanghamitra_behera

    sanghamitra_behera New Member

    Joined:
    Dec 3, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Then can i know what is the role of a static function in c++ and why this pointer is not accessible by static functions.
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    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
     
  10. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    Seems nice bro. But I tried your code,but it is giving error "illegal Initialization".
    Plz explain.....
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Which compiler you are using?
     
  12. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    It is turbo c++ compiler....
     
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    It works for me. Is your file name .c or .cpp . Try to have it as .c file and also remember that some other program compiles correctly and then try to compile the above program so that you know what is the problem.
     
  14. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    i saved the file with .c extension but still it is giving errors. Kindly check I am attaching the screenshot herewith:
     

    Attached Files:

Share This Page