如何限制用户输入值<;以C为单位的整数最大值 #包括 #包括 #包括 int getInt(){ int n,检查; char ch; 做{ fflush(stdin); printf(“输入正整数n=”); check=scanf(“%d%c”,&n,&ch); 如果((检查==2)&&(检查=='\n')){ 如果(n>INT_MAX){ printf(“无效数字\n”); }else if(n0”
我的代码接受范围为0到如何限制用户输入值<;以C为单位的整数最大值 #包括 #包括 #包括 int getInt(){ int n,检查; char ch; 做{ fflush(stdin); printf(“输入正整数n=”); check=scanf(“%d%c”,&n,&ch); 如果((检查==2)&&(检查=='\n')){ 如果(n>INT_MAX){ printf(“无效数字\n”); }else if(n0”,c,int,C,Int,我的代码接受范围为0到INT\u MAX的用户输入号码 当我输入-1时,程序显示“n必须>0” 但当我输入“7777”(>INT_MAX)时,程序仍然显示“n must>0”,而不是“Invalid number”,首先,您需要了解变量如何存储数据 在64位体系结构中,int类型有4个字节(C long类型中的任意一个,不考虑体系结构),因此可以存储以下值: 00000000 00000000 00000000 00000000=0(十进制值) 0111111111111111111111111
INT\u MAX
的用户输入号码
当我输入-1时,程序显示“n必须>0”
但当我输入“7777”(>INT_MAX)时,程序仍然显示
“n must>0”
,而不是“Invalid number”
,首先,您需要了解变量如何存储数据
在64位体系结构中,int类型有4个字节(C long类型中的任意一个,不考虑体系结构),因此可以存储以下值:
00000000 00000000 00000000 00000000=0(十进制值)
01111111111111111111111111111=2147483647(十进制值)
11111111111111111111111111111111111=4294967294(无符号十进制值)
11111111111111111111111111111111111111=-1(带符号的十进制值)
请注意,整数类型可以使用最高有效位(MSB)来表示使用模块化算术运算的信号(0到正,1到负)
有关整数信号的更多详细信息:
因此,要存储高于INT_MAX的十进制数据,需要比使用INT type更多的字节。与64位体系结构兼容的一个好方法是使用long-long类型
Long-Long类型使用8个字节,因此可以存储高于INT_MAX的值
您必须声明:
long代码>
然后像这样使用scanf():
scanf(“%lld%c”,&n,&ch)代码>
您的fflush(stdin)必须在scanf()之后,因为如果您的应用程序在scanf()之后和到达fflush()指令之前中断循环,则在进一步的输入处理中可能会出现问题。像这样:
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
int getInt() {
int n, check;
char ch;
do {
fflush(stdin);
printf("Enter positive integer n = ");
check = scanf("%d%c", &n, &ch);
if ((check == 2)&&(ch == '\n')) {
if (n > INT_MAX) {
printf("Invalid number\n");
} else if (n < 0) {
printf("n must > 0");
} else {
break;
}
} else printf("Invalid number\n");
} while (1);
return n;
}
int main(int argc, char** argv) {
int n;
n = getInt();
}
但是,一些开发人员不赞成在stdin中使用fflush(),因此这是另一种选择(有点复杂),使用getch()只接受数字,并使用strtoll()将char*转换为long:
charc=0;
字符*输入_编号=malloc(32);
int接受字符=0;
memset(输入_编号,0,32);
而(c!='\r'){//循环,直到用户按ENTER键
c=getch();
//按接收号码键
如果(c>='0'&&c0){//如果没有要清除的内容,请不要执行任何操作
已接受的字符--;
*(输入字符数+接受字符)=0;
printf(“\b”);
printf(“”);
printf(“\b”);
}
}
}
printf(“\n”);
char*endptr;
n=strtoll(输入编号&endptr,10)//转换以10为基数(十进制)的长型字符串
正如评论部分所指出的,测试n>INT\u MAX
没有意义,因为n
属于INT
类型,因此根据定义,无法表示任何大于INT\u MAX
的值
检查作为用户输入提供的数字大小的一种简单、灵活的方法是将其作为字符串读入,并使用该函数计算位数,还可能计算实际位数
但是,当处理可能较大的数值作为用户输入时,通常最好先将此输入读入大于int
的数据类型,然后使用此较大的数据类型进行范围检查。在您确信数字在所需范围内后,可以将其转换为较小的int
数据类型。尽管ISO C标准不能保证long
大于int
,但这是一个合理的假设,我所知道的所有编译器都是如此。但是,在某些平台上(包括64位窗口),数据类型long
与int
的大小相同,因此long
不能在所有平台上可靠地用于此
解决问题的最简单方法可能是使用函数或strtoll
。使用这些函数的优点是,您不需要更大的数据类型,通过将errno
设置为ERANGE
,它可以明确地告诉您数字是否超出范围。但是,这些函数仅支持数据类型long
和long
,而不支持int
。因此,如果您想使用int
,则必须在转换之前手动进行范围检查。在scanf(“%d%c”、&n、&ch”)中输入超出范围代码>,行为未定义
相反,使用fgets()
读取一行输入,然后使用strtol()
(;;)的{
char-buf[100];
if(fgets(buf,sizeof buf,stdin)=NULL){
printf(“无更多输入\n”);
返回-1;
}
errno=0;
char*endptr;
long val=strtol(buf和endptr,0);
//根本没有进行数字转换?
//数字输入超出长范围?
//数字文本后的垃圾?
如果(buf==endptr | | errno==ERANGE | |*endptr!='\n'){
printf(“无效数字\n”);
继续;
}
//超出整数范围?
如果(valINT_MAX){
printf(“无效数字\n”);
继续;
}
if(val<0){
printf(“n必须大于0”);
继续;
}
n=(int)val;
}
我建议使用一个可重用的intget\u int(int*val)
helper函数。只需检查一下即可
for (;;) {
char buf[100];
if (fgets(buf, sizeof buf, stdin) == NULL) {
printf("No more input\n");
return -1;
}
errno = 0;
char *endptr;
long val = strtol(buf, &endptr, 0);
// No numeric conversion done at all?
// Numeric input outside long range?
// Junk after the numeric text?
if (buf == endptr || errno == ERANGE || *endptr != '\n') {
printf("Invalid number\n");
continue;
}
// Outside int range?
if (val < INT_MIN || val > INT_MAX) {
printf("Invalid number\n");
continue;
}
if (val < 0) {
printf("n must > 0");
continue;
}
n = (int) val;
}
这是一种永远不会起作用的东西。如果int
值的值不能高于IN
char c = 0;
char* input_number = malloc(32);
int accepted_chars = 0;
memset(input_number, 0, 32);
while(c != '\r'){ //loop until user press ENTER
c = getch();
//receive numbers keys pressed
if(c >= '0' && c <= '9'){
*(input_number + accepted_chars) = c;
accepted_chars ++;
printf("%c", c);
}
//receive backspace key pressed
if(c == 8){
if(accepted_chars > 0){ //don't do nothing if there is nothing to clear
accepted_chars --;
*(input_number + accepted_chars) = 0;
printf("\b");
printf(" ");
printf("\b");
}
}
}
printf("\n");
char* endptr;
n = strtoll(input_number, &endptr, 10); //convert string in base 10 (decimal) long long type
for (;;) {
char buf[100];
if (fgets(buf, sizeof buf, stdin) == NULL) {
printf("No more input\n");
return -1;
}
errno = 0;
char *endptr;
long val = strtol(buf, &endptr, 0);
// No numeric conversion done at all?
// Numeric input outside long range?
// Junk after the numeric text?
if (buf == endptr || errno == ERANGE || *endptr != '\n') {
printf("Invalid number\n");
continue;
}
// Outside int range?
if (val < INT_MIN || val > INT_MAX) {
printf("Invalid number\n");
continue;
}
if (val < 0) {
printf("n must > 0");
continue;
}
n = (int) val;
}
int x;
...
if (x > INT_MAX) { ...
int digit;
int the_integer = 0;
while ( (digit = fgetchar(stdin)) != EOF
&& isdigit(digit)
&& the_integer <= (INT_MAX - (digit - '0')))
{
the_integer *= 10;
the_integer += digit;
} /* while */
if (digit != EOF && isdigit(digit)) {
/* x > (INT_MAX - (digit - '0')) */
/* this is necessary, as if you don't do it, you'll lose
* the next character to read. */
unput(digit, stdin);
}