Connecting Tech Pros Worldwide Forums | Help | Site Map

cout vs Print

Ashish Sharma
Guest
 
Posts: n/a
#1: Jul 23 '05
#include <iostream.h>
#include <stdio.h>
void main()
{
cout<<"hello";
printf("hi");

}


its coming out like

hihello

why so???


babyfrog
Guest
 
Posts: n/a
#2: Jul 23 '05

re: cout vs Print


i have no problem running this program
everything seems nice
what system do you run this on?
maybe ur system's buffer operations are diff from others'

Sharad Kala
Guest
 
Posts: n/a
#3: Jul 23 '05

re: cout vs Print



"Ashish Sharma" <Neo.Scorpio@gmail.com> wrote in message
[color=blue]
> #include <iostream.h>[/color]

Non-standard header. Check the archive and you will see umpteen discussions
on it.
[color=blue]
> #include <stdio.h>
> void main()[/color]

int main. Again check the archive.
[color=blue]
> {
> cout<<"hello";
> printf("hi");
>
> }
>
>
> its coming out like
>
> hihello[/color]

No wonder. Buffering of output stream is causing the surpises for you. C and
C++ standard streams need to be kept in sync when both libraries' facilities
are in use
-- check std::ios::sync_with_stdio.

Sharad


Lionel B
Guest
 
Posts: n/a
#4: Jul 23 '05

re: cout vs Print


Sharad Kala wrote:[color=blue]
> "Ashish Sharma" <Neo.Scorpio@gmail.com> wrote in message
>[color=green]
>> #include <iostream.h>[/color]
>
> Non-standard header. Check the archive and you will see umpteen
> discussions on it.
>[color=green]
>> #include <stdio.h>
>> void main()[/color]
>
> int main. Again check the archive.
>[color=green]
>> {
>> cout<<"hello";
>> printf("hi");
>>
>> }
>>
>>
>> its coming out like
>>
>> hihello[/color]
>
> No wonder. Buffering of output stream is causing the surpises for
> you. C and C++ standard streams need to be kept in sync when both
> libraries' facilities are in use
> -- check std::ios::sync_with_stdio.[/color]

Out of interest, is there (or perhaps should there be) a standard on
whether the C and C++ standard streams are synched by default? Looking
back at some old code, I find I've often used:

std::ios::sync_with_stdio(false);

when using just the C++ standard streams, so as to avoid the I/O
performance hit incurred by synching. I think gcc (as of ver 3.0) does
synch by default; see:

http://gcc.gnu.org/onlinedocs/libstd...o/howto.html#8

--
Lionel B

P.J. Plauger
Guest
 
Posts: n/a
#5: Jul 23 '05

re: cout vs Print


"Lionel B" <me@privacy.net> wrote in message
news:42cce599$0$18643$14726298@news.sunsite.dk...
[color=blue]
> Sharad Kala wrote:[color=green]
>> "Ashish Sharma" <Neo.Scorpio@gmail.com> wrote in message
>>[color=darkred]
>>> #include <iostream.h>[/color]
>>
>> Non-standard header. Check the archive and you will see umpteen
>> discussions on it.
>>[color=darkred]
>>> #include <stdio.h>
>>> void main()[/color]
>>
>> int main. Again check the archive.
>>[color=darkred]
>>> {
>>> cout<<"hello";
>>> printf("hi");
>>>
>>> }
>>>
>>>
>>> its coming out like
>>>
>>> hihello[/color]
>>
>> No wonder. Buffering of output stream is causing the surpises for
>> you. C and C++ standard streams need to be kept in sync when both
>> libraries' facilities are in use
>> -- check std::ios::sync_with_stdio.[/color]
>
> Out of interest, is there (or perhaps should there be) a standard on
> whether the C and C++ standard streams are synched by default? Looking
> back at some old code, I find I've often used:
>
> std::ios::sync_with_stdio(false);
>
> when using just the C++ standard streams, so as to avoid the I/O
> performance hit incurred by synching. I think gcc (as of ver 3.0) does
> synch by default; see:
>
> http://gcc.gnu.org/onlinedocs/libstd...o/howto.html#8[/color]

The C++ Standard requires that the standard streams be synchronized
by default. You might feel moved to turn it off, in the hopes of
improving performance, but you don't have to turn it on anymore
with sync_with_stdio. Our library has no performance penalty for
synchronizing streams.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


Ben Pope
Guest
 
Posts: n/a
#6: Jul 23 '05

re: cout vs Print


Ashish Sharma wrote:[color=blue]
> #include <iostream.h>[/color]

#include <iostream>
[color=blue]
> #include <stdio.h>[/color]

#include <cstdio>
[color=blue]
> void main()[/color]

int main()
[color=blue]
> {
> cout<<"hello";[/color]

std::cout << "hello" << std::flush;
[color=blue]
> printf("hi");
>
> }
>
>
> its coming out like
>
> hihello
>
> why so???
>[/color]

At a guess:

cout is buffered, printf is not.

cout is C++, printf is C, mixing the two is asking for trouble.

Presumably the buffer is flushed when the program returns, which is after the printf (which happens immediately).

Ben
--
I'm not just a number. To many, I'm known as a String...
Closed Thread