473,789 Members | 2,194 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
12 2464
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<iost ream.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("Ne w 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
55594
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
6264
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
8363
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
3975
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
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10386
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
10182
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...
0
9973
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
9003
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...
1
7522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6752
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
5542
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3681
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.