检查从scanf函数的值是多少? [英] Check if a value from scanf is a number?

查看:135
本文介绍了检查从scanf函数的值是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最简单的方法可能,我怎么能检查是否从函数初始化一个整数 scanf函数是多少?

In the simplest way possible, how can I check if an integer initialized from function scanf is a number?

推荐答案

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

在成功[scanf函数]返回
  项目数成功地读取。这个
  计数可以匹配的预期数量
  读数或更少,甚至是零,如果
  匹配故障发生。在这种情况下
  任何数据之前输入故障
  可以成功读取,是EOF
  回来了。

On success, [scanf] returns the number of items succesfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens. In the case of an input failure before any data could be successfully read, EOF is returned.

所以你可以做这样的事情:

So you could do something like this:

#include <stdio.h>

int main()
{
    int v;
    if (scanf("%d", &v) == 1) {
        printf("OK\n");
    } else {
        printf("Not an integer.\n");
    }
    return 0;
}

但它建议您使用与fgets 并的 strtol将来代替。

这篇关于检查从scanf函数的值是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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