C程序int变量值在读取Char值后发生更改
我是C编程新手,刚刚创建了一个小型计算器应用程序,但我注意到,当我在读取Int值之后读取char值时,下一个立即Int变量会发生变化。发生这种情况的原因是什么?这是我的密码C程序int变量值在读取Char值后发生更改,c,C,我是C编程新手,刚刚创建了一个小型计算器应用程序,但我注意到,当我在读取Int值之后读取char值时,下一个立即Int变量会发生变化。发生这种情况的原因是什么?这是我的密码 #include <stdio.h> int main(){ int num1; int num2; char opr; int ans; printf("Enter the first number : "); scanf("%d", &num1
#include <stdio.h>
int main(){
int num1;
int num2;
char opr;
int ans;
printf("Enter the first number : ");
scanf("%d", &num1);
printf("Enter the second number : ");
scanf("%d", &num2);
printf("Enter the operater : ");
scanf("%s", &opr);
printf("%d \n", num1);
printf("%d \n", num2);
switch(opr) {
case '+':
ans=num1+num2;
printf("The addtion of %d and %d is %d", num1, num2, ans);
printf("\n");
break;
case '-':
ans=num1-num2;
printf("The substractuon of %d from %d is %d", num2, num1, ans);
printf("\n");
break;
case '*':
ans=num1*num2;
printf("The multiplication of %d and %d is %d", num1, num2, ans);
printf("\n");
break;
case '/':
ans=num1/num2;
printf("The substraction of %d from %d is %d", num1, num2, ans);
printf("\n");
break;
}
return 0;
}
#包括
int main(){
int num1;
int num2;
char-opr;
INTANS;
printf(“输入第一个数字:”);
scanf(“%d”&num1);
printf(“输入第二个数字:”);
scanf(“%d”&num2);
printf(“输入运算符:”);
scanf(“%s”、&opr);
printf(“%d\n”,num1);
printf(“%d\n”,num2);
开关(opr){
格“+”:
ans=num1+num2;
printf(“添加%d和%d是%d”,num1,num2,ans);
printf(“\n”);
打破
案例'-':
ans=num1-num2;
printf(“从%d中减去%d是%d”,num2,num1,ans);
printf(“\n”);
打破
案例“*”:
ans=num1*num2;
printf(“%d和%d的乘积是%d”,num1,num2,ans);
printf(“\n”);
打破
案例“/”:
ans=num1/num2;
printf(“从%d中减去%d是%d”,num1,num2,ans);
printf(“\n”);
打破
}
返回0;
}
使用scanf(“%c”和&opr)
读取单个字符
使用%s
将读取以NUL结尾的字符串,但您只有一个字节,这是不够的,导致行为未定义
实际发生的情况是,NUL终止符被写入int
变量顶部的一个字节,该变量紧挨着opr
,如其他人所述,使用%c
读取字符:
但是您使用了%s
,所以发生的是scanf读取一个字符串。假设您的示例的正常输入将读取一个字符和一个换行符。然后,Scanf将字符和一个终止的0字节写入给定的地址,即您的字符
但是一个字符只有一个字节大,而另一个字节溢出到内存中的下一个字节。在您的例子中,这恰好是int变量。这称为缓冲区溢出,也是使用scanf的危险之一。这里您必须使用scanf(“%c”,&opr)代码>而不是scanf(“%s”和&opr)
由于opr是char
您必须使用%c
,因此%s
用于扫描字符串。然后出现未处理的'\n'
问题。因此,在%c
前面添加一个额外的'\n'
代码>
修改代码:-
#include <stdio.h>
int main()
{
int num1;
int num2;
char opr;
int ans;
printf("Enter the first number : ");
scanf("%d", &num1);
printf("Enter the second number : ");
scanf("%d", &num2);
printf("Enter the operater : ");
scanf("\n%c", &opr); // not scanf("%s", &opr);
printf("%d \n", num1);
printf("%d \n", num2);
switch (opr)
{
case '+':
ans = num1 + num2;
printf("The addtion of %d and %d is %d", num1, num2, ans);
printf("\n");
break;
case '-':
ans = num1 - num2;
printf("The substractuon of %d from %d is %d", num2, num1, ans);
printf("\n");
break;
case '*':
ans = num1 * num2;
printf("The multiplication of %d and %d is %d", num1, num2, ans);
printf("\n");
break;
case '/':
ans = num1 / num2;
printf("The substraction of %d from %d is %d", num1, num2, ans);
printf("\n");
break;
}
return 0;
}
scanf(“%s”和&opr)代码>是未定义的行为,任何现代编译器都会发出警告。警告不是为了好玩,而是要认真对待。在要求修理之前。(如果您没有收到警告,请启用它们)。您能解释一下为什么我使用scanf(“%c”、&opr);如果没有,它将无法工作?以及为什么%s给出了错误的数字。最好你能用一些例子来解释。。。非常感谢前面的回答scanf(“%c”,&opr)代码>不工作,因为有一个\n
(输入)您按下进行扫描num2
将被存储到opr
。为了避免\n
,scanf(“\n%c”,&opr)使用了code>。因为\n
也是字符。
Enter the first number : 3
Enter the second number : 4
Enter the operater : *
3
4
The multiplication of 3 and 4 is 12