我是一个学习 C 的新手,我尝试使用 if else 制作一个简单的计算器。但是当我执行代码时,即使“if”中的语句为假,它仍然在那里执行 printf 命令?
这是代码
#include <stdio.h>
int main(int argc, char const *argv[])
{
int opt;
int x, y;
float res;
printf("\nSelect an operation (+1, -2, /3, *4): ");
scanf("%d", &opt);
printf("Pick the first number: ");
scanf("%d", &x);
printf("Pick the second number: ");
scanf("%d", &y);
if(opt = 1) {
res = x + y;
printf("\nThe result is: %f", res);
}
else(opt = 2); {
res = x - y;
printf("\nThe result is: %f", res);
}
return 0;
}