Thread: C++ Accelerated Questions Chapter 0.

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    62
    This could be a stupid question and already answered before but what exactly do you mean by flushing the stream?

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Forced the data to to be seen at the screen (usually data is accumulated in a buffer and sent to the screen simultaneously because sending data to the screen is very slow).
    I'd write it as:
    Code:
    #include<iostream>
    
    using std::cout;
    using std::endl;
    
    int main()
    {
        cout <<
            "#include<iostream>\n"
            "\n"
            "int main()\n"
            "{\n"
            "\tstd::cout << \"Hello, world!\" << std::endl\n"
            "\n"
            "\treturn 0;\n"
            "}";
        return 0;
    }
    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.

  3. #18
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    Forced the data to to be seen at the screen (usually data is accumulated in a buffer and sent to the screen simultaneously because sending data to the screen is very slow).
    I'd write it as:
    Code:
    #include<iostream>
    
    using std::cout;
    using std::endl;
    
    int main()
    {
        cout <<
            "#include<iostream>\n"
            "\n"
            "int main()\n"
            "{\n"
            "std::cout << \"Hello, world!\" << std::endl\n"
            "\n"
            "return 0;\n"
            "}";
        return 0;
    }
    Sorry for crashing the thread, but I often wondered why you would use endl over \n. So basically endl has greater overhead than \n?

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes it would, since it would flush the output buffer.
    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. #20
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    Yes it would, since it would flush the output buffer.
    So if you just use \n. the output buffer would be flushed in a single go or when the buffer is full?

  6. #21
    Registered User
    Join Date
    May 2010
    Posts
    62
    Not to be rude and interrupt the two of you but do you mean that using "endl" would make a separate call to the standard library therefor making it more slow then using \n?

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Kitt3n View Post
    Not to be rude and interrupt the two of you but do you mean that using "endl" would make a separate call to the standard library therefor making it more slow then using \n?
    I believe so, yes, since std::endl is actually a function. Hence it would flush everytime endl is called. While if you use "\n" if would probably only flush once (after you've printed the whole thing).
    Can't say I know all the details, but this is the principle, at least.
    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. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kitt3n
    Not to be rude and interrupt the two of you but do you mean that using "endl" would make a separate call to the standard library therefor making it more slow then using \n?
    Yes, though the forced flushing of the output buffer when encountering std::endl is more likely to be the main factor in slowing down the overall speed of output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    May 2010
    Posts
    62
    Thanks for the explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc problem
    By Tool in forum C Programming
    Replies: 23
    Last Post: 03-12-2010, 10:54 AM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM