输入的微小变化会使程序变得疯狂
Slight change in input makes the program go nuts
我有这段代码来检查第二个日期是否早于第一个日期程序正常工作当我输入这些时没有逗号9,9,2021但是当我输入09作为第一个输入它跳过第二个输入并直接跳转到第三个输入我不明白为什么会这样,因为我对 C
还很陌生
#include <stdio.h>
// Array format [DD,MM,YYYY]
int date[3];
int secondDate[3];
// ANSI color codes
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
int main()
{
printf("Enter the day of first date: ");
scanf("%i", &date[0]);
printf("Enter the month of first date: ");
scanf("%i", &date[1]);
printf("Enter the year of first date: ");
scanf("%i", &date[2]);
printf("Enter the day of the second date: ");
scanf("%i", &secondDate[0]);
printf("Enter the month of the second date: ");
scanf("%i", &secondDate[1]);
printf("Enter the year of the second date: ");
scanf("%i", &secondDate[2]);
return 0;
}
%i
格式说明符识别数字基数前缀(即 "0x"
表示十六进制,'0'
表示八进制)。 '0'
前缀用于八进制。 09
是无效的八进制,因为八进制数包含数字 0-7
.
您可以使用 %d
说明符代替 %i
,后者仅解释十进制数。
我有这段代码来检查第二个日期是否早于第一个日期程序正常工作当我输入这些时没有逗号9,9,2021但是当我输入09作为第一个输入它跳过第二个输入并直接跳转到第三个输入我不明白为什么会这样,因为我对 C
还很陌生#include <stdio.h>
// Array format [DD,MM,YYYY]
int date[3];
int secondDate[3];
// ANSI color codes
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
int main()
{
printf("Enter the day of first date: ");
scanf("%i", &date[0]);
printf("Enter the month of first date: ");
scanf("%i", &date[1]);
printf("Enter the year of first date: ");
scanf("%i", &date[2]);
printf("Enter the day of the second date: ");
scanf("%i", &secondDate[0]);
printf("Enter the month of the second date: ");
scanf("%i", &secondDate[1]);
printf("Enter the year of the second date: ");
scanf("%i", &secondDate[2]);
return 0;
}
%i
格式说明符识别数字基数前缀(即 "0x"
表示十六进制,'0'
表示八进制)。 '0'
前缀用于八进制。 09
是无效的八进制,因为八进制数包含数字 0-7
.
您可以使用 %d
说明符代替 %i
,后者仅解释十进制数。