473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

why printf works first than cout

pai
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...

Aug 22 '06 #1
12 2420
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/iostrea...th_stdio.html).

Cheers! --M

Aug 22 '06 #2
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 <iostreamand see if it works better.
Aug 22 '06 #3
mlimber wrote:
Cout is buffered, and you didn't sync it with stdio (see, e.g.,
http://www.cplusplus.com/ref/iostrea...th_stdio.html).
sync_with_stdio is true by defualt.
Aug 22 '06 #4
In article <11**********************@m73g2000cwd.googlegroups .com>,
pai <gr****@yahoo.comwrote:
>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?
Aug 22 '06 #5
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
Aug 22 '06 #6
On Tue, 22 Aug 2006 14:46:18 +0200, "Thomas J. Gritzan"
<Ph*************@gmx.dewrote 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.husage 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/~a...FAQ-acllc.html
Aug 22 '06 #7
"pai" <gr****@yahoo.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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.
Aug 22 '06 #8
Jack Klein wrote:
On Tue, 22 Aug 2006 14:46:18 +0200, "Thomas J. Gritzan"
<Ph*************@gmx.dewrote 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
Aug 22 '06 #9
In article <ec**********@newsreader2.netcologne.de>,
Thomas J. Gritzan <Ph*************@gmx.dewrote:
>Jack Klein wrote:
>On Tue, 22 Aug 2006 14:46:18 +0200, "Thomas J. Gritzan"
<Ph*************@gmx.dewrote 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?
Aug 22 '06 #10
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.hworks just fine here. Changing to <cstdiowould 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.
Aug 23 '06 #11
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.
Aug 23 '06 #12

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 <cxxxheaders incorrectly,
putting names in the std and global namespaces, in which case
preference for <cxxxgains you nothing.

Gavin Deane

Aug 23 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Pontus F | last post by:
Hi I am learning C++ and I'm still trying to get a grip of pointers and other C/C++ concepts. I would appreciate if somebody could explain what's wrong with this code: ---begin code block--- ...
2
by: Tony Murphy | last post by:
I've got an application that sends emails (not spam!). The application reads a template file (html/text) into a string, the string is processed and placeholders filled in as appropiate. I call a...
5
by: uli | last post by:
Hi all! I'm posting to both newsgroups, because it's actually a C++ problem but could be that some of you using Matlab-&-MEX-&-C++ was struggling with the same problem. I'm trying to rewrite...
12
by: Minti | last post by:
Is std::cout slower than printf When we call printf e.g. in printf(20 format conversion specifications, 20 arguments); Is it faster than the std::cout << { 20 redirections to the output...
3
by: Richard Cavell | last post by:
GCC 3.3, XCode: #include <iostream> #include <stdint.h> int main (int argc, char * const argv) { uint64_t a=10123123123LL; printf("%d\n", a);
25
by: Podrzut_z_Laweczki | last post by:
Hello, I have question (or 2 :)). Is that true that for a large data using scanf/printf instead of cin/cout makes that the program runs faster? And if it is, is it legal to mix scanf/printf with...
18
by: Joah Senegal | last post by:
Hello all, I'm trying to print a string on my screen... But the string comes from a variable string... This is the code #include <cstdlib> #include <iostream> #include <string> using...
3
by: IanWright | last post by:
I'm having a little trouble with a progress bar that I've got in C++. This works fine in Windows but is not working correctly in Linux, and I'm wondering if anyone can help? const char DONE =...
10
by: laikon | last post by:
Hello, everyone: this is about overflow in C and C++. int c = 400; printf("%c", c); it print ? on screen, and ascii of '?' is 63.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.