2

Possible Duplicate:
mixing cout and printf for faster output

I'm using Microsoft Visual Studio 6.0.

The following program,

#include "stdafx.h"
#include "iostream.h"

int main(int argc, char* argv[])
{
printf("a");
printf("b");
printf("c");
return 0;
}

produces "abc".

While the following program,

#include "stdafx.h"
#include "iostream.h"

int main(int argc, char* argv[])
{
printf("a");
cout<<"b";
printf("c");
return 0;
}

produces "acb".

What's the problem? Can't I mix cout and printf in the same program?

Community
  • 1
  • 1
unix55
  • 141
  • 1
  • 1
  • 4
  • Does [`sync_with_stdio`](http://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio) do the trick? I've never used it. – chris Dec 02 '12 at 07:46
  • @CaptainMurphy: No it is not. The semantics of mixing wide-narrow are unspecified, whereas the semantics of the above code are well defined and it *must* print "abc" unless `sync_with_stdio(false)` was called, which does not happen here. Basically MSVC6 is non standard conforming... – Yakov Galka Dec 02 '12 at 07:53

1 Answers1

5

The standard says that:

When a standard iostream object str is synchronized with a standard stdio stream f, the effect of inserting a character c by

fputc(f, c);

is the same as the effect of

str.rdbuf()->sputc(c);

for any sequences of characters;

By default, unless you invoke sync_with_stdio(false), cout is synchronized with stdout. Therefore your second code snippet is equivalent to:

printf("a");
fputc(stdout, 'b')
printf("c");

Which must produce "abc" even on your implementation.

Bottom line: MSVC6 is not standard conforming, which is not a surprise as it is very old.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • "which is not a surprise as it is very old" - and anyways at Microsoft nobody gives a damn about standards. –  Dec 02 '12 at 08:03
  • 1
    @H2CO3: Not exactly true... they do work on improving their C++ compiler. In particular you cannot say that, e.g., MSVC10 is not an improvement over MSVC6. – Yakov Galka Dec 02 '12 at 08:06
  • it certainly is, but it still cannot possibly be compared with GCC or Clang... –  Dec 02 '12 at 08:13
  • 2
    @H2CO3 this was true for a long time. now MS is at eye level. according to our portability team MS was faster in implementing C++11 – stefan Dec 02 '12 at 10:35
  • @stefan well, it might be the case with C++11, but I can't really care about C++11 until it lacks reasonable support for C99 or even C89 (which it does). –  Dec 02 '12 at 11:04
  • 1
    @H2CO3: You are changing the subject of the discussion. No-one here talks about C, I judge MSVC purely as a C++ compiler. In particular because I do not care for C support. Today it is so close to being a conforming C++ compiler that it is hard to compare it to GCC. Yes both have bugs, but both are good too. – Yakov Galka Dec 02 '12 at 12:19
  • 1
    @ H2CO3: my comment was addressing your comment (i cite) 'and anyways at Microsoft nobody gives a damn about standards.' everyone may jude – stefan Dec 02 '12 at 12:24