与 C++ cin 语句等效的 C 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3723592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:33:45  来源:igfitidea点击:

What is the C equivalent to the C++ cin statement?

c++c

提问by rectangletangle

What is the C equivalent to the C++ cinstatement? Also may I see the syntax on it?

与 C++cin语句等效的 C 是什么?我也可以看看它的语法吗?

回答by paxdiablo

cinis not a statement, it's a variable that refers to the standard input stream. So the closest match in C is actually stdin.

cin不是一个语句,它是一个引用标准输入流的变量。所以 C 中最接近的匹配实际上是stdin.

If you have a C++ statement like:

如果您有一个 C++ 语句,例如:

std::string strvar;
std::cin >> strvar;

a similar thing in C would be the use of any of a wide variety of input functions:

在 C 中类似的事情是使用各种输入函数中的任何一个:

char strvar[100];
fgets (strvar, 100, stdin);


See an earlier answerI gave to a question today on one way to do line input and parsing safely. It's basically inputting a line with fgets(taking advantage of its buffer overflow protection) and then using sscanfto safely scan the line.

请参阅我今天对一个问题的早期回答,以一种安全地进行行输入和解析的方式。它基本上是输入一行fgets(利用其缓冲区溢出保护),然后使用它sscanf来安全地扫描该行。

Things like gets()and certain variationsof scanf()(like unbounded strings) should be avoided like the plague since they're easily susceptible to buffer over-runs. They're fine for playing around with but the sooner you learn to avoid them, the more robust your code will be.

喜欢的东西gets()某些变化scanf()(如无界的字符串)应避免瘟疫一样,因为他们容易受到缓冲区超支。它们很适合玩,但你越早学会避免它们,你的代码就越健壮。

回答by sje397

You probably want scanf.

你可能想要scanf.

An example from the link:

来自链接的一个例子:

int i, n; float x; char name[50];
n = scanf("%d%f%s", &i, &x, name);

Note however (as mentioned in comments) that scanf is prone to buffer overrun, and there are plenty of other input functions you could use (see stdio.h).

但是请注意(如评论中所述) scanf 容易发生缓冲区溢出,并且您可以使用许多其他输入函数(请参阅 参考资料stdio.h)。

回答by jbernadas

There is no close equivalent to cinin C. C++ is an object oriented language and cinuses many of its features (object-orientation, templates, operator overloading) which are not available on C.

cin在 C 中没有接近的等价物。 C++ 是一种面向对象的语言,并cin使用了C 上没有的许多特性(面向对象、模板、运算符重载)。

However, you can read things in C using the C standard library, you can look at the relevant part here (cstdio reference).

但是,您可以使用 C 标准库阅读 C 中的内容,您可以在此处查看相关部分(cstdio 参考)。

回答by Mike Axiak

In c++, cin is the stdin input stream. There is no input stream in C, but there is a file object, called stdin. You can read from it using a lot of ways, but here's an example from cplusplus.com:

在 C++ 中,cin 是 stdin 输入流。C 中没有输入流,但有一个文件对象,称为stdin。您可以使用多种方式阅读它,但这里有一个来自 cplusplus.com 的示例:

/* fgets example */
#include <stdio.h>

int main()
{
  char buffer[256];
  printf ("Insert your full address: ");
  if (fgets(buffer, 256, stdin) != NULL) {
    printf ("Your address is: %s\n", buffer);
  }
  else {
    printf ("Error reading from stdin!\n");
  }
  return 0;
}

UPDATE

更新

I've changed it to use fgets, which is much simpler since you have to worry about how large your buffer is. If you also want to parse the input use scanf, but make sure that you use the width attribute.

我已将其更改为使用 fgets,这要简单得多,因为您必须担心缓冲区有多大。如果您还想解析输入,请使用 scanf,但请确保使用 width 属性