检查C中的输入类型 [英] checking the input type in C

查看:48
本文介绍了检查C中的输入类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的输入是否为整数(例如,不是字符或字符串).我偶然发现了这段代码(有效):

  #include< stdio.h>int main(无效){浮动int q;printf("\ nInsert number \ t");scanf(%f",& a);q =(int)a;++ q;if((q-a)!= 1)printf("\ n数字不是整数\ n \ n");别的printf("\ n数字是整数\ n \ n");返回0;} 

有人可以解释一下它为什么起作用吗?

解决方案

我偶然发现了这段代码(有效):...为什么有效?

通常它不起作用.失败案例太多

给出的输入可能是单个字符 Z ,后继代码是未定义行为(UB),因为 scanf()没有分配<代码> a .结果:(int)a 是UB.

  scanf(%f",& a);q =(int)a; 

给出的输入可能是"12345678901234567890",它超出了 int 的范围,然后用(int)a 进行转换也是UB.

如果 a 的值为 INT_MAX ,则 ++ q; 为UB.

可以将输入精确地表示为 int 而不是 float 的值会使OP的代码失败.

四舍五入为整数的输入,例如"0.99999999999999999999999999999" -> 1.0f 未能检测到OP


替代.

scanf(),fgets()不会读取 C字符串 int float 等.相反,最好将它们视为阅读 text 并将其转换为 C字符串 int float ./p>

scanf()中的 f 表示格式化输入,这是此处的未知部分.最好避免使用 scanf().

由于代码需要检测有关用户输入的各种内容,因此请使用 fgets()读取文本的.然后使用 strtol() strtoll() strtoul()等,以查看输入是否适合目标类型.

 //检查输入是否适合`long'char buf [100];if(fgets(buf,sizeof buf,stdin)== NULL){puts(文件尾或输入错误");} 别的 {char * endptr;errno = 0;长ivalue = strtol(buf,& endptr,0);如果(buf == endptr)puts(失败:无转换");否则,如果(* endptr!='\ n'&& * endptr!='\ 0')puts(失败:额外的垃圾");否则,如果(errno)放置(失败:过头");否则printf("Success:%ld \ n",ivalue);} 

I want to check if my input is an integer (and not a char or string for example). I stumbled across this code (that works):

#include <stdio.h>

int main (void)
{
    float a;
    int q;

    printf("\nInsert number\t");
    scanf("%f",&a);

    q=(int)a;
    ++q;

    if((q - a) != 1)
        printf("\nThe number is not an integer\n\n");
    else
        printf("\nThe number is an integer\n\n");

    return 0;
}

Can someone please explain why it works?

解决方案

I stumbled across this code (that works): ... why it works?

It does not work, in general. Too many failing cases

Given input might be a single character Z, the follow code is undefined behavior (UB) as scanf() does not assign a. Result: (int)a is UB.

scanf("%f",&a);
q=(int)a;

Given input might be "12345678901234567890", something outside the range of an int, then converting with (int)a is also UB.

If a takes on the value of INT_MAX, ++q; is UB.

Values where the input can be exactly represented as an int, but not as a float fail OP's code.

Input that is rounded to a whole number like "0.99999999999999999999999999999" --> 1.0f fail OP's detection


Alternative.

scanf(), fgets() do not read C strings, int, float, etc. Instead, better to think of them as reading text and converting them into C strings, int, float.

The f in scanf() implies formatted input and that is the unknown part here. Better to avoid scanf().

Since code needs to detect various things about user input, use fgets() to read a line of text. Then use strtol(), strtoll(), strtoul(), etc. to see if input fits into the target type.

// Check if input fits in a `long`
char buf[100];
if (fgets(buf, sizeof buf, stdin) == NULL) {
  puts("End-of-file or input error");
} else {
  char *endptr;
  errno = 0;
  long ivalue = strtol(buf, &endptr, 0);
  if (buf == endptr) puts("Fail: no conversion");
  else if (*endptr != '\n' && *endptr != '\0') puts("Fail: Extra junk");
  else if (errno) puts("Fail: Overlfow");
  else printf("Success: %ld\n", ivalue);
}

这篇关于检查C中的输入类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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