[英]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.