what does cout return?

This is a discussion on what does cout return? within the C++ Programming forums, part of the General Programming Boards category; what does cout return?In code Code: cout<<cout<<"Hello World"; I am getting a hexadecimal number,probably an address.Does cout return something?Thanking in ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    what does cout return?

    what does cout return?In code
    Code:
    cout<<cout<<"Hello World";
    I am getting a hexadecimal number,probably an address.Does cout return something?Thanking in advance

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    cout doesn't return anything. The '<<' operator returns cout.

    When you write (cout << "x" << 3 << " crazy!" << endl), evaluation happens like this:

    Code:
    ((cout << "x") << 3) << " crazy!") << endl
    ((cout << 3) << " crazy!") << endl
    (cout << " crazy!") << endl
    cout << endl
    cout

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    As written, I'd say you're outputting the address of a function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Which function's address?.I havenot made any function yet.
    Code:
    #include<iostream.h>
    int main()
    {
    cout<<cout<<"Hello world";
    return(0);
    }
    In above code i have not made or called any function.So which function's address are you taking of.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I havenot made any function yet.
    main is a function, so this statement is false.

    >what does cout return?
    cout is an object, it doesn't return anything. The << operator returns a reference to the stream to which it's applied, in this case cout. So the address you're seeing is that of cout.
    My best code is written with the delete key.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    cout is an object, it doesn't return anything. The << operator returns a reference to the stream to which it's applied, in this case cout. So the address you're seeing is that of cout.
    I thought that also but its not :)
    Consider:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
            cout<<cout<<"Hello World"<<endl;
            void* ptr = cout;
            cout<<ptr<<endl;
            cout<<(&cout)<<endl;
    
    }
    The output recieved (obviously the exact addresses will be different by machine)
    0x804de9cHello World
    0x804de9c
    0x804de98
    So it appears that cout<<cout; returns a void pointer address.
    Using gdb I was able to confirm that it does in fact call
    std::basic_ios<char, std::char_traits<char> >::operator void*() const

    or called any function.
    the << operator calls operator<< for whatever datatype you pass it so you did call a function.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Take for example overloading the << operator . Might make it seem a little clearer.
    Code:
    Class complex{};	
    std::ostream& operator<< (std::ostream&, const Complex&);
    this is in main of course
    Code:
    Complex b;
    cout << b;
    //which is equivalent to the call
    cout.operator(b)
    So in fact you are calling a function, in this case the overloaded << function
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by vaibhav
    what does cout return?In code
    Code:
    cout<<cout<<"Hello World";
    I am getting a hexadecimal number,probably an address.Does cout return something?Thanking in advance
    You might try reading the manual (scroll down the page to the "return value" section). The return value of a stream is well documented in every c++ textbook and millions of online references. If you don't know what something does, then look it up in the manual before you hurt yourself.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Ancient Dragon
    You might try reading the manual (scroll down the page to the "return value" section). The return value of a stream is well documented in every c++ textbook and millions of online references. If you don't know what something does, then look it up in the manual before you hurt yourself.
    You might want to try reading the thread. The question wasn't what operator<< returned but what cout<<cout displayed. Yea they used return wrongly but thats pretty obvious if you read the post and the replies. Oh and next time you link something at least make sure its relevent. (you linked operator>> which is istreams and we are talking about operator<< which is ostream)

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
            cout<<cout<<"Hello World"<<endl;
            void* ptr = cout;
            cout<<ptr<<endl;
            cout<<(&cout)<<endl;
    
    }
    I compiled above program.I get output as
    Code:
    0x8f910378Hello World
    0x8f910378
    0x8f910374
    There is one similarity in yours and my output. address returned by cout is exactly 4 bytes greater than address of cout.I think it may help you in finding what cout returns?

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    My teacher replied this as
    Code:
    In general, s<<a where s is a stream returns a reference to s. 
    
    cout<<cout; converts the second "cout" to a printable value using the 
    conversion 
    void* that we use for testing its state (see your C++ textbook). That 
    value is 
    only guaranteed to be zero (meaning the state is bad) or non-zero 
    (meaning the 
    state is good). Nothing more is guaranteed.
    Can anyone explain this to me in simple language?

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    See this

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Ancient Dragon
    See this
    Borland bcc32 gives the following output (different in nature from the others, but compliant with the actual spec):

    00000001Hello World
    00000001
    0042049C
    D

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Thanks all of you ,Now I understands

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  5. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21