473,775 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am.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 2463
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<iostre am.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************ **********@m73g 2000cwd.googleg roups.com>,
pai <gr****@yahoo.c omwrote:
>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<iostr eam.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_stdi o()). 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<iostre am.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.husa ge in any of our
lifetimes. Or that if they do, compilers will actually remove it.
#include<iostre am.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 22 '06 #7
"pai" <gr****@yahoo.c omwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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<iostre am.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**********@n ewsreader2.netc ologne.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

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

Similar topics

11
55589
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--- #include "stdio.h" #include "string.h" void printText(char c){
2
9196
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 3rd party api for sending the email, the api is written in c, so i need to convert the string into LPSTR (windows c style string?), i'm from unix world and new to windows mutant. I know that c_str() converts a string to a c string and data()...
5
6263
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 some Matlab routines in C++ for reusing them identically in Matlab and some other simulation tools. For computational algebra I want to use the Matrix Template Library (MTL, http://www.osl.iu.edu/research/mtl/) which is written in C++ and therefore...
12
3568
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 stream }
3
8359
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
9733
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 C++ code? Program should execute below 1 sec and the hint is to use scanf/printf.
18
3775
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 namespace std;
3
10566
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 = '='; const char BLANK = ' '; const unsigned INCREMENT = 3; static char progressB = ""; void progress(unsigned int current, char* msg)
10
3974
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
10272
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10111
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9917
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8941
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6719
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5364
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4021
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2855
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.