在控制台应用程序中获取 int 的最简单方法是什么?

发布于 2024-07-19 04:52:59 字数 80 浏览 7 评论 0原文

我想将用户输入作为整数处理,但似乎 C 无法从 stdin 获取 int 。 有一个函数可以做到这一点吗? 我将如何从用户那里获取 int ?

I want to process user input as an integer, but it seems as though C has no way to get an int from stdin. Is there a function to do this? How would I go about getting an int from the user?

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

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

发布评论

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

评论(5

乱世争霸 2024-07-26 04:52:59
#include <stdio.h>

int n;
scanf ("%d",&n);

请参阅http://www.cplusplus.com/reference/clibrary/cstdio/scanf/< /a>

#include <stdio.h>

int n;
scanf ("%d",&n);

See http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

说谎友 2024-07-26 04:52:59

scanf() 是答案,但您当然应该检查返回值,因为从外部输入解析数字时,很多很多事情都可能会出错......

int num, nitems;

nitems = scanf("%d", &num);
if (nitems == EOF) {
    /* Handle EOF/Failure */
} else if (nitems == 0) {
    /* Handle no match */
} else {
    printf("Got %d\n", num);
}

scanf() is the answer, but you should certainly check the return value since many, many things can go wrong parsing numbers from external input...

int num, nitems;

nitems = scanf("%d", &num);
if (nitems == EOF) {
    /* Handle EOF/Failure */
} else if (nitems == 0) {
    /* Handle no match */
} else {
    printf("Got %d\n", num);
}
秋凉 2024-07-26 04:52:59

除了其他答案已经充分讨论的 (f)scanf 之外,还有 atoistrtol,适用于您已经已将输入读取到字符串中,但希望将其转换为 intlong

char *line;
scanf("%s", line);

int i = atoi(line);  /* Array of chars TO Integer */

long l = strtol(line, NULL, 10);  /* STRing (base 10) TO Long */
                                  /* base can be between 2 and 36 inclusive */

推荐使用 strtol,因为它可以让你判断一个数字是否被成功读取(与 atoi 不同,后者无法报告任何错误,只会返回如果给出垃圾则为 0)。

char *strs[] = {"not a number", "10 and stuff", "42"};
int i;
for (i = 0; i < sizeof(strs) / sizeof(*strs); i++) {
    char *end;
    long l = strtol(strs[i], &end, 10);
    if (end == line)
        printf("wasn't a number\n");
    else if (end[0] != '\0')
        printf("trailing characters after number %l: %s\n", l, end);
    else
        printf("happy, exact parse of %l\n", l);
}

Aside from (f)scanf, which has been sufficiently discussed by the other answers, there is also atoi and strtol, for cases when you already have read input into a string but want to convert it into an int or long.

char *line;
scanf("%s", line);

int i = atoi(line);  /* Array of chars TO Integer */

long l = strtol(line, NULL, 10);  /* STRing (base 10) TO Long */
                                  /* base can be between 2 and 36 inclusive */

strtol is recommended because it allows you to determine whether a number was successfully read or not (as opposed to atoi, which has no way to report any error, and will simply return 0 if it given garbage).

char *strs[] = {"not a number", "10 and stuff", "42"};
int i;
for (i = 0; i < sizeof(strs) / sizeof(*strs); i++) {
    char *end;
    long l = strtol(strs[i], &end, 10);
    if (end == line)
        printf("wasn't a number\n");
    else if (end[0] != '\0')
        printf("trailing characters after number %l: %s\n", l, end);
    else
        printf("happy, exact parse of %l\n", l);
}
猫九 2024-07-26 04:52:59

标准库函数 scanf 用于格式化输入:
%d int(d 是十进制的缩写)

#include <stdio.h>
int main(void)
{
  int number;
  printf("Enter a number from 1 to 1000: ");

  scanf("%d",&number); 
  printf("Your number is %d\n",number);
  return 0;
} 

The standard library function scanf is used for formatted input:
%d int (the d is short for decimal)

#include <stdio.h>
int main(void)
{
  int number;
  printf("Enter a number from 1 to 1000: ");

  scanf("%d",&number); 
  printf("Your number is %d\n",number);
  return 0;
} 
岁月流歌 2024-07-26 04:52:59
#include <stdio.h>

main() {

    int i = 0;
    int k,j=10;

    i=scanf("%d%d%d",&j,&k,&i);
    printf("total values inputted %d\n",i);
    printf("The input values %d %d\n",j,k);

}

来自此处

#include <stdio.h>

main() {

    int i = 0;
    int k,j=10;

    i=scanf("%d%d%d",&j,&k,&i);
    printf("total values inputted %d\n",i);
    printf("The input values %d %d\n",j,k);

}

from here

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