why printf works first than cout

P

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

mlimber

pai said:
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
 
R

Ron Natalie

pai said:
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.
 
G

Greg Comeau

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

Thomas J. Gritzan

pai said:
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 said:
#include<iostream.h>

Should be said:
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.
 
J

Jack Klein

Should be <cstdio>

Agreed, although the original is not incorrect. And I doubt that the
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.
 
J

Jim Langston

pai said:
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.
 
T

Thomas J. Gritzan

Jack said:
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.
 
G

Greg Comeau

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

Pete Becker

Thomas said:
Should be <cstdio>



Should be <iostream>

This is actually the problem, obscured by the gratuitous style advice
that surrounds it.
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 said:
In general, it's a bad idea to mix C and C++ standard library stuff.

Not at all.
 
P

Pete Becker

Jim said:
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.
 
G

Gavin Deane

Thomas said:
Should be <cstdio>

Only if

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top