#include <stdio.h>
int main()
{
int a, b, c;
/* Input two numbers from user */
printf("Enter two numbers to find maximum: ");
scanf("%d%d", &a, &b);
c = a > b;
switch (c)
{
case 0:
printf("%d is maximum", b);
break;
case 1:
printf("%d is maximum", a);
break;
default:
printf("Invalid Input");
}
return 0;
}
我想通过输入错误的输入(例如 float 或字符 const)来打印此 C 程序中的默认语句。 每当我输入任何 char 类型变量或浮点数时,都会发生这种情况👇
输出示例1:
Enter two numbers to find maximum: 2.5
509 is maximum
输出示例2:
Enter two numbers to find maximum: g
512 is maximum
预期输出应该是:
Enter two numbers to find maximum: g
Invalid Input
预期输出应该是:
Enter two numbers to find maximum: 22.6
Invalid Input
c = a > b;
理论上只能是 1 或 0。
你真正想要的可能是:
c = scanf("%d%d", &a, &b);
switch(c)
{
case 2:
/* expected */
/* additional error handling of inputs here if needed */
break;
default:
/* scanf failed */
}