C ++混合printf和cout [英] C++ mixing printf and cout
问题描述
可能重复:
混合cout和printf以获得更快的输出
我正在使用Microsoft Visual Studio 6.0.
I'm using Microsoft Visual Studio 6.0.
以下程序,
#include "stdafx.h"
#include "iostream.h"
int main(int argc, char* argv[])
{
printf("a");
printf("b");
printf("c");
return 0;
}
产生"abc".
同时使用以下程序,
#include "stdafx.h"
#include "iostream.h"
int main(int argc, char* argv[])
{
printf("a");
cout<<"b";
printf("c");
return 0;
}
产生"acb".
出什么问题了?我不能在同一程序中混合使用cout和printf吗?
What's the problem? Can't I mix cout and printf in the same program?
推荐答案
标准规定:
当标准iostream对象
str
与标准同步时stdio流f
,通过
When a standard iostream object
str
is synchronized with a standard stdio streamf
, the effect of inserting a characterc
by
fputc(f, c);
与
str.rdbuf()->sputc(c);
用于任何字符序列;
默认情况下,除非您调用 sync_with_stdio(false)
,否则 cout
与 stdout
同步.因此,您的第二个代码段等效于:
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");
即使在您的实现中,也必须产生"abc".
Which must produce "abc" even on your implementation.
底线:MSVC6不符合标准,这并不奇怪,因为它很旧.
Bottom line: MSVC6 is not standard conforming, which is not a surprise as it is very old.
这篇关于C ++混合printf和cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!