Thread: cout vs std::cout

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    cout vs std::cout

    Hi everyone.

    I'm new to C++ so excuse the question. I've seen some source file that uses std::cout insted of just using cout to output text. why is that and what is the difference?

    example

    cout << "C++";

    or

    std::cout << "C++";

    So when should I use the second form insted of the first and what does the second mean actually.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    7
    The std:: is the namespace that cout is in. So for example if you include iostream.h the cout function is not in the namespace std so just use cout<< but if you include iostream (recommended)...then you need to put the std::

    #include <iostream.h>

    cout<<

    #include <iostream>

    std::cout<<

    however, if you include iostream and not iostream.h I would recomment putting:
    using namespace std; right under it. That way you don't have to keep putting std:: before it.

    for example,

    #include <iostream>

    using namespace std;

    void main()
    {
    cout<<"hi";
    }

    Paul Marshall

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    That could've been a nice explanation, if it weren't for a little malplaced four-letter word.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Thanks for the explanation ... it was very nice and clear.

    Now isn't the difference between #include <iostream.h> and #include <iostream> is only about the ANSI standards or am I wrong. I mean using iostream conforms to the standards while using iostream.h does not. If I'm right then is there a way to use #include <iostream> without using any namespaces?

    just wanting to know how things work and not trying to act smart.

    Thanks again.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The statement 'using namespace std;' for all intents and purposes, removes the namespace. What it does is put every included symbol in the std namespace into the global namespace, just as the example in the first post. I would recommend against doing that, however, for exactly that reason: you really pollute the global namespace.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    [The statement 'using namespace std;' for all intents and purposes, removes the namespace]

    I thought this statement indicates that you want to use the std namespace? but you're saying that it removes the namespace?!

    anyway, I think i should read more about namespace in c++ and then things will be clear to me.

    Thanks anyway.

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    No what he is saying is that it puts everything into the std namespace(at least i think so).
    Try doing this instead
    Code:
    using std::cout;
    using std::cin;
    Woop?

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    It doesn't put everything into the std namespace. Instead, it puts everything in the std namespace into the global namespace (that is, the default namespace which is equivalent to no namespace).

    Prog-bman's code is better because it won't pollute the global namespace, with things you don't want or know about. (That is, an identifier may be used only once per namespace, so, for example, if you defined a function with the same name as one in the std namespace and included the appropriate header for that function, you would have an error if you had simply used 'using namespace std;'. If you had not used the all-inclusive using statement and had not explicitly imported that function into the global namespace, there would be no error.)

  9. #9
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Sang-drax
    That could've been a nice explanation, if it weren't for a little malplaced four-letter word.
    to expand on this very good comment by Sang-drax that everyone ignored, DONT USE VOID MAIN, use int main

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Thanks guys ... I can't say I understood everything fully but thats only cos I'm new to C++ ... anyway its very nice to see that people are willing to help when i get stuck on my learning curve ...

    Thanks again for the effort.

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Your welcome keep up the progress and anymore questions feel free to ask it only gets better.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with cout
    By kristentx in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2006, 12:37 PM
  2. Need Help with a Bowling Score Program
    By oobootsy1 in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2005, 10:04 AM
  3. FAQ: Cout and textcolor question...
    By Laniston in forum FAQ Board
    Replies: 2
    Last Post: 12-19-2002, 10:54 AM
  4. changing cout output
    By Yohumbus in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 04:09 AM
  5. Redirecting cout stream
    By Arrow Mk84 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 04:17 PM