[英]How to check if input is an integer in C?
我有一个问题,如何检查输入是否真的是 integer。 如果输入是 1 或 2,我下面的简单程序会写引号。在所有其他情况下,它应该打印“luj”。
我的代码:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int cislo;
printf("ml' nob:\n");
scanf("%d", &cislo);
if (cislo == 1) {
printf("Qapla'\n");
printf("noH QapmeH wo' Qaw'lu'chugh yay chavbe'lu' 'ej wo' choqmeH may' "
"DoHlu'chugh lujbe'lu'.\n");
} else if (cislo == 2) {
printf("Qapla'\n");
printf("Qu' buSHa'chugh SuvwI', batlhHa' vangchugh, qoj matlhHa'chugh, "
"pagh ghaH SuvwI''e'.\n");
} else {
printf("luj\n");
}
}
当输入为 1.5 时,我的程序将其读取为 1。但它应该打印“luj”。 在 python 我会做类似isinstance(cislo, int)
的事情。 请问如何在 C 中做到这一点?
这个问题对我没有帮助: Checking if input is an integer in C
请注意,C 不像 Python:您想将一个带小数点的数字输入到一个类型为integer的变量中。 整数类型变量只能容纳整数,所以……这个数字会自动变成整数,点后面的数字不会被计算在内。
因此,当您尝试将值 1.5 插入cislo
该值会自动转换为int
值,即为 1。
解决方案:
#include <stdio.h>
int main(int argc, char** argv) {
float cislo;
printf("ml' nob:\n");
if (scanf("%f", &cislo) != 1)
// Problem (Handle it)
if (cislo == 1) {
printf("Qapla'\n");
printf("noH QapmeH wo' Qaw'lu'chugh yay chavbe'lu' 'ej wo' choqmeH may' DoHlu'chugh lujbe'lu'.\n");
} else if (cislo == 2) {
printf("Qapla'\n");
printf("Qu' buSHa'chugh SuvwI', batlhHa' vangchugh, qoj matlhHa'chugh, pagh ghaH SuvwI''e'.\n");
} else {
printf("luj\n");
}
}
带有%d
scanf
只会读取整数 - 任何不是整数的东西都不会被读取。
不幸的是,这意味着您可以获得部分匹配,正如您所经历的那样 - "1.5"
不是一个整数,但scanf
会读取并将1
分配给cislo
并返回成功。
解决这个问题的方法是将下一个输入读取为文本(不要尝试将其读取为整数或浮点数),并使用strtol
等库函数或通过手动进行自己的转换来单独进行转换。 这是一个使用strtol
的示例:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define BUFFER_SIZE 11 // up to 10 digits for a 32-bit int plus sign
/**
* We don't want to use a naked "%s" in our scanf call, since
* if the user types in more characters than the buffer is sized
* to hold, we'll write past the end of the buffer. You can specify
* the maximum number of characters to read as part of the "%s"
* conversion like "%11s", but unlike with printf you can't specify
* it with a runtime argument - it has to be hardcoded.
*
* We're going to create a format string based on the value of BUFFER_SIZE;
* when we're done, FMT will expand to "%" "11" "s", which will
* be interpreted as "%11s".
*/
#define EXPAND(x) #x
#define STR(x) EXPAND(x)
#define FMT "%" STR(BUFFER_SIZE) "s" // Make sure we don't read more characters than the buffer can hold
int main( void )
{
int cislo = 0;
char buffer[BUFFER_SIZE+1]; // +1 for string terminator
if ( scanf ( FMT, buffer ) == 1 )
{
/**
* strtol will take a string representation of an integer
* like "123" and convert it to the corresponding integer
* value. chk will point to the first character in the
* string that *isn't* part of an integer constant. If that
* character isn't whitespace or 0 (the string terminator),
* then the input is not a properly formed integer.
*/
char *chk;
int tmp = strtol( buffer, &chk, 10 );
if ( !isspace( *chk ) && *chk != 0 )
{
fprintf( stderr, "%s is not a valid integer input!\n", buffer );
return -1;
}
cislo = tmp;
}
printf( "cislo = %d\n", cislo );
return 0;
}
例子:
$ ./converter
123
cislo = 123
$ ./converter
abc
abc is not a valid integer input!
$ ./converter
123c
123c is not a valid integer input!
$ ./converter
12.3
12.3 is not a valid integer input!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.