何时使用 printf/scanf 与 cout/cin?

发布于 2024-12-10 09:50:21 字数 374 浏览 0 评论 0原文

我正在使用 MinGW 的 g++ 测试从网上找到的一些片段。这就是 C++ 编译器……为什么它能正确编译 C……为什么人们要把 C 和 C++ 交织在一起。

具体问题是:同时使用C和C++并在g++下编译是否可以。如果答案是肯定的,这会让我的生活变得轻松,因为我不必修改代码。

奇怪的是...为了让一些 C++ 工作,特别是当将字符串传递给 ifstream 构造函数时,它需要一个 C 类型字符串...

我的猜测是,因为 C++ 有时依赖于 C 构造,所以可以编写这两个语言在一起。

不过,就风格而言,您应该选择 cout/cinprintf/scanf

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

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.

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黑白记忆 2024-12-17 09:50:21

有一些奇怪的地方需要 char* 。您可以使用 std::string.c_str() 方法来弥补这一差距。

在大多数情况下,C++ 的 C 子集是兼容的。到底如何不兼容在大多数情况下可能并不重要:

http:// /en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

如果您在 C++ 编译器下编译 C 代码片段,请务必将其更改为在包含中使用“c”lib 格式...例如 #include 而不是 #include

这是不好的做法吗使用C 头文件而不是 C++ 中的 C++ 等效项(例如 stdio.h 而不是 cstdio)?

对于 Bjarne 本人关于为什么要避免 scanf 的合理论证,请查看本文的开头:

http://www.stroustrup.com/new_learning.pdf

使用 iostream 有很多好处也代替 printf:

C++ 中的'printf' 与 'cout'

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.

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:

http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

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>

Is it bad practice to use a C header instead of its C++ equivalent in C++ (e.g. stdio.h instead of cstdio)?

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

http://www.stroustrup.com/new_learning.pdf

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

'printf' vs. 'cout' in C++

别再吹冷风 2024-12-17 09:50:21

C++ 语言继承了 C 的大部分核心功能。这是因为 C++ 派生自 C。C++ 标准通过引用包含了 C 标准的大部分内容。因此,您可以使用 C++ 编译器使用 C 结构、惯用语和范例来编写代码。这样做通常被称为使用 C++“作为更好的 C”。

上面的长短是肯定的,你可以在C++代码中使用printf。标准明确允许这样做。

然而,这样做通常会忽略定义 C++ 的许多功能。我将把这个对话留给另一个问题,但足以说明很多人会简单地告诉您“不要这样做”或“那不是 C++”。这就排除了您可能不想在 C++ 程序中使用 printf 的原因,或者实际上您想要使用的原因。但请放心,这在技术上是允许的。

The C++ language inherits much of its core functionality from C. That's because C++ was derived from C. The C++ Standard includes, by reference much of the C Standard. Therefore you can use the C++ compiler to write code using C constructs, idioms and paradigms. Doing so is often referred to as using C++ "as a better C."

The long and the short of the above is yes, you can use printf in C++ code. Doing so is explicitly allowed by the Standard.

Doing this however will often neglect many of the features that define C++. I'll leave that conversation for another question but suffice it to say that many people will tell you simply "don't do that" or "that's not C++." This sets aside the reasons why you might not want to use printf in a C++ program or indeed why you would want to. But rest assured that it is technically allowed.

⊕婉儿 2024-12-17 09:50:21

同时使用 C 和 C++ 并在 g++ 下编译是否可以。

是的,可以混合使用这两种语言。这对于最初为 C 的代码很常见,但后来添加了越来越多的 C++ 功能(显然有人在此过程中更改了编译器)。

通常,C 代码将使用 C++ 编译器进行编译和运行。有许多可能的例外,例如在 C 代码中使用 classvirtual 等关键字作为事物名称,或者 C 的宽松转换规则。

您经常会听到人们说“它们是非常不同的语言”。这是因为您提出的任何编程问题都可能有不同的答案,具体取决于您尝试使用的语言。然而,它们也有很多相似之处和向后兼容性。

Is it O.K. to use both C and C++ and compile under g++.

Yes, it is fine to mix the two languages. This is common with code that started out as C, but then got more and more C++ features added (obviously somebody changed the compiler along the way).

Generally, C code will compile and run with a C++ compiler. There are many possible exceptions, such as use of keywords like class and virtual for names of things in C code, or C's relaxed casting rules.

You will often hear people say "they are very different languages". That's because any programming question you ask probably has a different answer depending on which language you're trying to use. However, there are lots of similarities and backwards compatibility aspects as well.

丑丑阿 2024-12-17 09:50:21

如果你使用C++,那就使用C++。 (cin,cout)
为什么fstream需要c字符串也让我困惑。

If you use C++, then use C++. (cin,cout)
Why fstream takes c string puzzles me too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文