Thread: Absurd Program Output

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    13

    Unhappy Absurd Program Output

    Hello all
    i tried the following code and got unexpected outputs.
    can anyone provide me with any feedback on any mistake that i have done.


    Code:
    #include<iostream.h>
    #include<stdio.h>
    void stm()
            {
                    cout<<"hello";
            }
    struct a
            {
                    stm();
            }b;
    main()
            {
                  cout<<endl<<b.stm();
                  cout<<endl<<sizeof(b)<<endl;
                  stm();
                  return(0);
              }



    output :

    [

    it do not compiles why ?
    and on commenting the first cout it gives the value 1.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No such thing as iostream.h, although there is a header called "iostream". Similarly, no such thing as stdio.h, although there is a header "cstdio" -- but you don't need it for this program, since you're not using any functions from that header.
    You're missing a namespace std declaration, to use cout and endl without qualification.
    You can't call a function inside a struct, and you can't declare a function without giving a return type, so that stm() inside the struct declaration is just wrong.
    And finally, struct a contains no members, so sizeof b must be at least zero, but it can be larger.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include<iostream>
    #include<cstdio>
    
    void stm()
    {
        std::cout << "hello";
    }
    
    struct a
    {
        void stm();
    }b;
    
    int main()
    {
        std::cout << std::endl << b.stm();
        std::cout << std::endl << sizeof(b) << std::endl;
        stm();
        return(0);
    }
    You're trying to cout the value of b.stm() which is a function call returning a void, you can't do that.

    You can't call a function inside a struct
    Sure you can, ala struct/class member functions. ...not in the way the OP's trying to do it here but it can be done.
    And finally, struct a contains no members, so sizeof b must be at least zero, but it can be larger.
    I think "empty" structs need to be at least 1 byte.
    Last edited by hk_mp5kpdw; 06-25-2008 at 07:26 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by hk_mp5kpdw View Post
    Sure you can.
    I think tabstop is referring to the fact that you can't call a function inside a struct declaration (or definition). It must be done inside a function inside the struct - ie the implementation, not in the definition.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    Tabstop:

    as far as i know if i dont declear any namespace its the default namespace that is taken.
    and please google for "c++ headers" you'll be able to find both of them i used .
    and i am using "cout" so i think iostream.h would be used atleast.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by hk_mp5kpdw View Post
    Sure you can... not in the way the OP's trying to do it here but it can be done.
    Sorry, should have been clearer -- you can't call a function from the definition of a struct -- you can call a method of a struct, I agree.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by C-RaT View Post
    as far as i know if i dont declear any namespace its the default namespace that is taken.
    cout is not located in the default namespace, it's located in std. So are any other standard library functions.

    and please google for "c++ headers" you'll be able to find both of them i used .
    Who do you believe more? Some old web pages or expert programmers?
    Those headers are deprecated and do not exist. They are not standard headers.

    and i am using "cout" so i think iostream.h would be used atleast.
    Make that "iostream".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You probably need a more modern compiler. It seems that you are using something that came out before C++ was standardized (end of 1990s).

    Here's what your program would look like in C++.

    Code:
    //proper header
    #include<iostream>
    using namespace std;
    
    void stm()
        {
            cout<<"hello";
        }
    
    struct a
        {
            //functions require explicit return type
            int stm()
            {
                //if you want to keep the same name
                //you have to indicate that you mean the global stm function
                ::stm();
                //if you declare your functions to return something,
                //you actually have to return something
                return 42;
            }
        }b;
    
    //functions require explicit return type
    int main()
        {
          cout<<endl<<b.stm();
          cout<<endl<<sizeof(b)<<endl;
          stm();
          return(0);
        }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program looping with final output
    By hebali in forum C Programming
    Replies: 24
    Last Post: 02-28-2008, 10:58 AM
  2. Unusual program Output, Help
    By capvirgo in forum C Programming
    Replies: 8
    Last Post: 02-06-2008, 03:13 AM
  3. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  4. Redirecting program output straight to an edit control
    By bennyandthejets in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2004, 08:25 AM
  5. Program Output
    By lavon in forum C Programming
    Replies: 1
    Last Post: 03-19-2002, 10:32 PM