什么时候用printf / scanf的VS COUT / CIN? [英] When to use printf/scanf vs cout/cin?

查看:175
本文介绍了什么时候用printf / scanf的VS COUT / CIN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了一些片段,我发现了使用G ++从MinGW的网站。这是C ++编译器......那么为什么它正确编译C ....为什么人们相互交织C和C ++。

I'm testing some snippets I found off the web using g++ from MinGW. This is the C++ compiler...why then does it correctly compile C....why do people intertwine C and C++.

具体的问题是:是否有O.K.同时使用C和C ++下摹++编译。如果答案是肯定的,这使我的生活更轻松,因为我没有修改code。

The concrete question is: Is it O.K. to use both C and C++ and compile under g++. If the answer is yes, this makes my life easy as I do not have to modify the code.

奇怪的是......得到一些C ++上班,路过一个字符串到一个ifstream的构造特别是当它需要一个C类型字符串...

Oddly enough...to get some C++ to work, particularly when passing a string to an ifstream constructor it requires a C type string...

我的猜测是,由于C ++取决于C构造有时是是O.K两种语言写在一起。

My guess would be that because C++ depends upon C constructs at times is is O.K to write the two languages together.

但作为一个风格问题,你应该在 COUT / 结算CIN 的printf / scanf函数

However as a matter of style you should settle on cout/cin or printf/scanf.

推荐答案

有在需要的char * 几个古怪。您可以通过桥接使用的std ::字符串 .c_str()的方法来得到一个缺口。

There are a few oddities where char* is needed. You can bridge the gap by using the .c_str() method of a std::string to get one.

有关的大多数情况下,C ++中的C-子集是兼容的。它究竟是如何不兼容不太可能无关紧要大部分:

For the most part, the C subset of C++ is compatible. Exactly how it isn't compatible is not likely to matter for the most part:

<一个href=\"http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B\">http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

如果你是下一个C ++编译器编译C code的片段,一定要改变它使用CLIB格式的包括...例如的#include&LT ; cstdio&GT; 而不是的#include&LT;&stdio.h中GT;

If you're compiling snippets of C code under a C++ compiler, be sure to change it to use the "c" lib format in your includes...for example #include <cstdio> instead of #include <stdio.h>

<一个href=\"http://stackoverflow.com/questions/2649207/is-it-bad-practice-to-use-a-c-header-instead-of-its-c-equivalent-in-c-e-g\">Is它不好​​的做法使用C头,而不是它的C ++相当于C ++(例如stdio.h中,而不是cstdio)?

有关从比亚自己为什么避免scanf函数,看看本文的开头一个相当合理的说法:

For a fairly reasoned argument from Bjarne himself on why to avoid scanf, check out the beginning of this paper:

<一个href=\"http://www2.research.att.com/~bs/new_learning.pdf\">http://www2.research.att.com/~bs/new_learning.pdf

有很多的好处,使用输入输出流代替了printf还有:

There are a lot of benefits to using iostreams instead of printf as well:

的printf VS用C ++的cout

这篇关于什么时候用printf / scanf的VS COUT / CIN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆