gpt4 book ai didi

c - 每当我执行代码时都会得到 "-317629566"

转载 作者:行者123 更新时间:2023-11-30 20:42:07 24 4
gpt4 key购买 nike

我正在使用 dev++ 进行 C 编程,并且我已经编写了简单的加、减、多和 div 程序。我的代码中没有错误,但我在输出屏幕中收到不同类型的 9 位数字 (-317629566) 消息以及输出。

#include<stdio.h>
#include<conio.h>

main()
{
int a, b;
char op;

printf("select a operator(+, -, *, /) :: ");
scanf("%c", &op);
printf("enter first number : ");
scanf("%d", &a);
printf("enter second number : ");
scanf("%d", &b);

switch (op)
{
case '+':
printf("addition : %d%d", a + b);
break;
case '-':
printf("subtraction : %d%d", a - b);
break;
case '*':
printf("multiplication : %d%d", a *b);
break;
case '/':
printf("divison : %d%d", a / b);
break;
default:
printf("not a operator");
}
getch();
}

这是我执行代码后得到的输出。

select a operator( + , - , * , / ) :: +
enter first number : 12
enter second number : 13
addition : 25-317629566

为什么我得到这个-317629566结果。

最佳答案

printf("addition : %d%d", a + b);

您要求 printf 打印两个 个整数,但您只传递了一个。这会导致未定义的行为。在这种情况下,它只是打印任意值,但可能会发生更糟糕的事情。

关于c - 每当我执行代码时都会得到 "-317629566",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54041836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com