Scanf赢得';t将用户输入的值分配给变量| C

Scanf赢得';t将用户输入的值分配给变量| C,c,C,当使用scanf获取计算的第二个整数时,它将0指定给秒,而不是用户输入的值。请帮忙 // This program will act as a basic calculator`enter code here` // The user will input two numbers and an operand. The program will print // the result of the operation #include<stdio.h> int main() {

当使用scanf获取计算的第二个整数时,它将0指定给秒,而不是用户输入的值。请帮忙

// This program will act as a basic calculator`enter code here`
// The user will input two numbers and an operand. The program will print
// the result of the operation

#include<stdio.h>
int main()
{
    int first; //Variable for the first input of the user
    int sec;   //Variable for the second input of the user
    char operand[1]; //Variable for the operand inputed by the user
    int op, answer; //Dummy Variables for temp use
    printf("This is a simple calculator. Make sure you don't divide by 0!!\n");
    printf("Enter your first integer: ");
    scanf("%d", &first); //Ask the user to input the first number for the     calculation
    printf("\nEnter your second integer: ");
    scanf("%d", &sec); //Ask the user to input the second number for the calculation
    printf("Enter the operand.\n");
    scanf("%s", operand); //Ask the user to input the operand of the     calculation (+,-,*,/)
    printf("%i %s %i\n", first, operand, sec); //Display the calculaiton to be made
    op = (int) operand[0]; // converts the operand to an ASCII value
}
//此程序将用作基本计算器`在此处输入代码`
//用户将输入两个数字和一个操作数。程序将打印
//手术结果
#包括
int main()
{
int first;//用户第一次输入的变量
int sec;//用户第二次输入的变量
char操作数[1];//用户输入的操作数的变量
int op,answer;//临时使用的伪变量
printf(“这是一个简单的计算器。请确保不被0除!!\n”);
printf(“输入第一个整数:”);
scanf(“%d”,&first);//要求用户输入计算的第一个数字
printf(“\n输入第二个整数:”;
scanf(“%d”,&sec);//要求用户输入第二个数字进行计算
printf(“输入操作数。\n”);
scanf(“%s”,操作数);//要求用户输入计算的操作数(+、-、*、/)
printf(“%i%s%i\n”,第一个,操作数,秒);//显示要进行的计算
op=(int)操作数[0];//将操作数转换为ASCII值
}
C使用以null结尾的字符串。这意味着最小的字符串(“”)将只包含1个字符:
0x00

当您试图从输入中读取类似“+”的内容时,这是一个由两个字节组成的字符串:
0x2B 0x00
。由于内存仅分配给单个字符(
操作数[1]
),
0x2B
进入此处,终止的null
0x00
意外进入
sec
变量

这就是所谓的。在您的情况下,解决方案是为字符串分配更多空间,或者只使用一个字符

C使用以null结尾的字符串。这意味着最小的字符串(“”)将只包含1个字符:
0x00

当您试图从输入中读取类似“+”的内容时,这是一个由两个字节组成的字符串:
0x2B 0x00
。由于内存仅分配给单个字符(
操作数[1]
),
0x2B
进入此处,终止的null
0x00
意外进入
sec
变量

这就是所谓的。在您的情况下,解决方案是为字符串分配更多空间,或者只使用一个字符

C使用以null结尾的字符串。这意味着最小的字符串(“”)将只包含1个字符:
0x00

当您试图从输入中读取类似“+”的内容时,这是一个由两个字节组成的字符串:
0x2B 0x00
。由于内存仅分配给单个字符(
操作数[1]
),
0x2B
进入此处,终止的null
0x00
意外进入
sec
变量

这就是所谓的。在您的情况下,解决方案是为字符串分配更多空间,或者只使用一个字符

C使用以null结尾的字符串。这意味着最小的字符串(“”)将只包含1个字符:
0x00

当您试图从输入中读取类似“+”的内容时,这是一个由两个字节组成的字符串:
0x2B 0x00
。由于内存仅分配给单个字符(
操作数[1]
),
0x2B
进入此处,终止的null
0x00
意外进入
sec
变量

这就是所谓的。在您的情况下,解决方案是为字符串分配更多空间,或者只使用一个字符

在解决实际问题的意义上添加正确的字符,您可能希望指示
scanf()
只扫描单个字符,如下所示

scanf(" %c", operand);
/*     ^ this is needed with the %c specifier in order to skip spaces */
printf("%i %s %i\n", first, operand, sec);
但是在使用
操作数时必须小心,因为它不是一个字符串,而是一个只有一个字符的数组,并且是这样使用的

scanf(" %c", operand);
/*     ^ this is needed with the %c specifier in order to skip spaces */
printf("%i %s %i\n", first, operand, sec);
将调用未定义的行为,因为
printf()
将尝试读取超过
操作数
数组末尾的内容

你也可以这样做

char operand;

scanf(" %c", &operand);
/*     ^ this is needed with the %c specifier in order to skip spaces */
在这种情况下,
printf()
将更改为

printf("%d %c %d\n", first, operand, sec);
/*       ^ is normally used instead of %i, though %i is correct */
再加上它解决实际问题的正确性,您可能希望指示
scanf()
只扫描单个字符,如下所示

scanf(" %c", operand);
/*     ^ this is needed with the %c specifier in order to skip spaces */
printf("%i %s %i\n", first, operand, sec);
但是在使用
操作数时必须小心,因为它不是一个字符串,而是一个只有一个字符的数组,并且是这样使用的

scanf(" %c", operand);
/*     ^ this is needed with the %c specifier in order to skip spaces */
printf("%i %s %i\n", first, operand, sec);
将调用未定义的行为,因为
printf()
将尝试读取超过
操作数
数组末尾的内容

你也可以这样做

char operand;

scanf(" %c", &operand);
/*     ^ this is needed with the %c specifier in order to skip spaces */
在这种情况下,
printf()
将更改为

printf("%d %c %d\n", first, operand, sec);
/*       ^ is normally used instead of %i, though %i is correct */
再加上它解决实际问题的正确性,您可能希望指示
scanf()
只扫描单个字符,如下所示

scanf(" %c", operand);
/*     ^ this is needed with the %c specifier in order to skip spaces */
printf("%i %s %i\n", first, operand, sec);
但是在使用
操作数时必须小心,因为它不是一个字符串,而是一个只有一个字符的数组,并且是这样使用的

scanf(" %c", operand);
/*     ^ this is needed with the %c specifier in order to skip spaces */
printf("%i %s %i\n", first, operand, sec);
将调用未定义的行为,因为
printf()
将尝试读取超过
操作数
数组末尾的内容

你也可以这样做

char operand;

scanf(" %c", &operand);
/*     ^ this is needed with the %c specifier in order to skip spaces */
在这种情况下,
printf()
将更改为

printf("%d %c %d\n", first, operand, sec);
/*       ^ is normally used instead of %i, though %i is correct */
再加上它解决实际问题的正确性,您可能希望指示
scanf()
只扫描单个字符,如下所示

scanf(" %c", operand);
/*     ^ this is needed with the %c specifier in order to skip spaces */
printf("%i %s %i\n", first, operand, sec);
但是在使用
操作数时必须小心,因为它不是一个字符串,而是一个只有一个字符的数组,并且是这样使用的

scanf(" %c", operand);
/*     ^ this is needed with the %c specifier in order to skip spaces */
printf("%i %s %i\n", first, operand, sec);
将调用未定义的行为,因为
printf()
将尝试读取超过
操作数
数组末尾的内容

你也可以这样做

char operand;

scanf(" %c", &operand);
/*     ^ this is needed with the %c specifier in order to skip spaces */
在这种情况下,
printf()
将更改为

printf("%d %c %d\n", first, operand, sec);
/*       ^ is normally used instead of %i, though %i is correct */

我将操作数改为单个c