【发布时间】:2021-02-19 23:48:57
【问题描述】:
我正在尝试测试一个数字是偶数还是奇数。
它适用于 8 位数字,但当我超过 9 位时,它看起来很奇怪。我输入的数字发生了变化。
8 位数字示例:
Enter the ID : 20202020
20202020 is even.
Program ended with exit code: 0
但是当使用 10 位数字时,它看起来像这样:
Enter an integer: 2345678915
-1949288381 is odd.
Program ended with exit code: 0
// these nr that are different, what are they?
//Have not found any info about it either...
代码:
#include <stdio.h>
int main()
{
int id;
printf("Enter the Id: ");
scanf("%d", &id);
if(id % 2 == 0)
printf("%d is even.\n", id);
else
printf("%d is odd.\n", id);
return 0;
}
我试着把它改成双倍,没有帮助。
和if语句有关系吗?
if(id % 2 == 0)
【问题讨论】:
-
int为 32 位,因此最大值为2,147,483,647。对较大的数字使用long long,并将格式运算符更改为%lld -
@Barmar 做什么。我们只检查数字是奇数还是偶数。就这样。不需要整数
标签: c if-statement integer