why printf works first than cout

Discussion in 'C++' started by pai, Aug 22, 2006.

  1. pai

    pai Guest

    Hi,

    This is a code . the output will be
    why is this so. Can any nody explain me this..


    4444 2222 7777 8888
    aaaa

    *******************************
    #include <stdio.h>
    #include<iostream.h>

    int main(void)
    {

    char* test = "4444 2222 7777 8888";


    cout<<"aaaa";
    printf("New string: %s\n", test);

    return 0;
    }
    *********************************
    thanks

    Pai...
    pai, Aug 22, 2006
    #1
    1. Advertising

  2. pai

    mlimber Guest

    pai wrote:
    > Hi,
    >
    > This is a code . the output will be
    > why is this so. Can any nody explain me this..
    >
    >
    > 4444 2222 7777 8888
    > aaaa
    >
    > *******************************
    > #include <stdio.h>
    > #include<iostream.h>
    >
    > int main(void)
    > {
    >
    > char* test = "4444 2222 7777 8888";
    >
    >
    > cout<<"aaaa";
    > printf("New string: %s\n", test);
    >
    > return 0;
    > }
    > *********************************
    > thanks
    >
    > Pai...


    Cout is buffered, and you didn't sync it with stdio (see, e.g.,
    http://www.cplusplus.com/ref/iostream/ios_base/sync_with_stdio.html).

    Cheers! --M
    mlimber, Aug 22, 2006
    #2
    1. Advertising

  3. pai

    Ron Natalie Guest

    pai wrote:
    > Hi,
    >
    > This is a code . the output will be
    > why is this so. Can any nody explain me this..
    >

    Your standard library implementation is broken.
    cout and stdout are supposed to be sync'd together
    by default.

    Of course, you're not using a standard library
    cout if you are using <iostream.h>.

    Try it with <iostream> and see if it works better.
    Ron Natalie, Aug 22, 2006
    #3
  4. pai

    Ron Natalie Guest

    Ron Natalie, Aug 22, 2006
    #4
  5. pai

    Greg Comeau Guest

    In article <>,
    pai <> wrote:
    >This is a code . the output will be
    >why is this so. Can any nody explain me this..
    >
    >
    >4444 2222 7777 8888
    >aaaa
    >
    >*******************************
    >#include <stdio.h>
    >#include<iostream.h>
    >
    >int main(void)
    >{
    >
    > char* test = "4444 2222 7777 8888";
    >
    >
    > cout<<"aaaa";
    > printf("New string: %s\n", test);
    >
    > return 0;
    >}


    iostreams and stdio are diffeent I/O systems and hance allowed
    to maintain seperate buffers. Solutions range from turning
    buffering off (set*buf()), flushing after each operations (endl or fflush),
    or sync'ing (std::ios_base::sync_with_stdio()). It will probably
    take some playing around with and I forget with aspects are
    guaranteed or not. Of course there is the approach of not mixing
    the two as well.
    --
    Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Aug 22, 2006
    #5
  6. pai schrieb:
    > Hi,
    >
    > This is a code . the output will be
    > why is this so. Can any nody explain me this..
    >
    >
    > 4444 2222 7777 8888
    > aaaa
    >
    > *******************************
    > #include <stdio.h>


    Should be <cstdio>

    > #include<iostream.h>


    Should be <iostream>

    >
    > int main(void)


    Should be

    int main()

    > {
    >
    > char* test = "4444 2222 7777 8888";


    const char* test = "4444 2222 7777 8888";

    >
    >
    > cout<<"aaaa";


    std::cout << "aaaa";

    > printf("New string: %s\n", test);
    >
    > return 0;
    > }


    Usually, both printf and cout are buffered. printf flushes the buffer on a
    newline ('\n'), and cout flushes by calling flush() member function, or by
    using:

    std::cout << std::endl; // and:
    std::cout << std::flush;

    In general, it's a bad idea to mix C and C++ standard library stuff.

    --
    Thomas
    Thomas J. Gritzan, Aug 22, 2006
    #6
  7. pai

    Jack Klein Guest

    On Tue, 22 Aug 2006 14:46:18 +0200, "Thomas J. Gritzan"
    <> wrote in comp.lang.c++:

    > pai schrieb:
    > > Hi,
    > >
    > > This is a code . the output will be
    > > why is this so. Can any nody explain me this..
    > >
    > >
    > > 4444 2222 7777 8888
    > > aaaa
    > >
    > > *******************************
    > > #include <stdio.h>

    >
    > Should be <cstdio>


    Agreed, although the original is not incorrect. And I doubt that the
    C++ standard will actually remove the <cheader.h> usage in any of our
    lifetimes. Or that if they do, compilers will actually remove it.

    > > #include<iostream.h>

    >
    > Should be <iostream>
    >
    > >
    > > int main(void)

    >
    > Should be
    >
    > int main()


    Balderdash. The OP's definition of main() is 100% correct and
    standard conforming. It's bad form to inject your esthetic
    preferences into real corrections without identifying them as such.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Aug 22, 2006
    #7
  8. pai

    Jim Langston Guest

    "pai" <> wrote in message
    news:...
    > Hi,
    >
    > This is a code . the output will be
    > why is this so. Can any nody explain me this..
    >
    >
    > 4444 2222 7777 8888
    > aaaa
    >
    > *******************************
    > #include <stdio.h>
    > #include<iostream.h>
    >
    > int main(void)
    > {
    >
    > char* test = "4444 2222 7777 8888";
    >
    >
    > cout<<"aaaa";
    > printf("New string: %s\n", test);
    >
    > return 0;
    > }
    > *********************************
    > thanks
    >
    > Pai...


    Because you are not flushing cout. Either add std::endl; to the end (which
    adds a newline and then does cout.flush()) or if you don't want to add the
    newline, just do the flush manually.

    std::cout << "aaaa" << std::endl;
    printf("New string: %s\n", test);

    or

    std::cout << "aaaa";
    std::cout.flush();
    printf("New string: %s\n", test);

    Try that and see if you get what you expected.
    Jim Langston, Aug 22, 2006
    #8
  9. Jack Klein wrote:
    > On Tue, 22 Aug 2006 14:46:18 +0200, "Thomas J. Gritzan"
    > <> wrote in comp.lang.c++:
    >
    >> pai schrieb:
    >>> int main(void)

    >> Should be
    >>
    >> int main()

    >
    > Balderdash. The OP's definition of main() is 100% correct and
    > standard conforming. It's bad form to inject your esthetic
    > preferences into real corrections without identifying them as such.


    Err, you are right. Both forms are correct in C++.

    I thought that it is a C'ism to write (void) for empty parameter list,
    since without the void, the parameters are unspecified in C.

    --
    Thomas
    Thomas J. Gritzan, Aug 22, 2006
    #9
  10. pai

    Greg Comeau Guest

    In article <ecfhj7$eua$>,
    Thomas J. Gritzan <> wrote:
    >Jack Klein wrote:
    >> On Tue, 22 Aug 2006 14:46:18 +0200, "Thomas J. Gritzan"
    >> <> wrote in comp.lang.c++:
    >>
    >>> pai schrieb:
    >>>> int main(void)
    >>> Should be
    >>>
    >>> int main()

    >>
    >> Balderdash. The OP's definition of main() is 100% correct and
    >> standard conforming. It's bad form to inject your esthetic
    >> preferences into real corrections without identifying them as such.

    >
    >Err, you are right. Both forms are correct in C++.
    >
    >I thought that it is a C'ism to write (void) for empty parameter list,
    >since without the void, the parameters are unspecified in C.


    It (prototypes) was a C++ism that C adopted but as you
    point out empty params already meant something in C,
    therefore C also has (void), which bounced back into
    C++ as a Cism in order to not have another incompatibility.
    --
    Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Aug 22, 2006
    #10
  11. pai

    Pete Becker Guest

    Thomas J. Gritzan wrote:
    > pai schrieb:
    >
    >>Hi,
    >>
    >>This is a code . the output will be
    >>why is this so. Can any nody explain me this..
    >>
    >>
    >>4444 2222 7777 8888
    >>aaaa
    >>
    >>*******************************
    >>#include <stdio.h>

    >
    >
    > Should be <cstdio>


    <stdio.h> works just fine here. Changing to <cstdio> would require
    adding std:: to the call to printf.

    >
    >>#include<iostream.h>

    >
    >
    > Should be <iostream>


    This is actually the problem, obscured by the gratuitous style advice
    that surrounds it.

    >
    >>int main(void)

    >
    >
    > Should be
    >
    > int main()


    They mean the same thing.

    >
    > Usually, both printf and cout are buffered. printf flushes the buffer on a
    > newline ('\n'), and cout flushes by calling flush() member function, or by
    > using:
    >
    > std::cout << std::endl; // and:
    > std::cout << std::flush;
    >


    And, gasp, printf and insertions to cout are synchronized! The problem
    isn't in mixing them. That works just fine. The problem is the use of an
    old streams library, through <iostream.h>.

    > In general, it's a bad idea to mix C and C++ standard library stuff.
    >


    Not at all.
    Pete Becker, Aug 23, 2006
    #11
  12. pai

    Pete Becker Guest

    Jim Langston wrote:

    >
    > Because you are not flushing cout. Either add std::endl; to the end (which
    > adds a newline and then does cout.flush()) or if you don't want to add the
    > newline, just do the flush manually.
    >


    That might be what's needed to use the non-standard global name cout
    (which is what the original program uses), but the standard stream
    std::cout is synchronized with stdout, and will get the order right. No
    flush gymnastics needed.
    Pete Becker, Aug 23, 2006
    #12
  13. pai

    Gavin Deane Guest

    Thomas J. Gritzan wrote:
    > pai schrieb:
    > > #include <stdio.h>

    >
    > Should be <cstdio>


    Only if

    > > printf("New string: %s\n", test);


    is changed to

    std::printf("New string: %s\n", test);

    or you are using a compiler that implements <cxxx> headers incorrectly,
    putting names in the std and global namespaces, in which case
    preference for <cxxx> gains you nothing.

    Gavin Deane
    Gavin Deane, Aug 24, 2006
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,638
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,396
    Leor Zolman
    Jun 2, 2004
  3. Minti
    Replies:
    12
    Views:
    1,628
    Jack Klein
    Jun 23, 2004
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,763
    Smokey Grindel
    Dec 2, 2006
  5. Replies:
    4
    Views:
    309
    Howard
    Sep 15, 2005
Loading...

Share This Page