下面的代码有什么问题?

下面的代码有什么问题?

#include
using namespace std;
int main()
{
      int a,b,c;
      scanf("%d%d",&a,&b);
      c=a+b;
      printf("%d",&c);
}

把输出改为printf("%d",c);

如果求输入两个数的和,第8行变量c之前的取址符&不需要,修改如下:

#include<iostream>
using namespace std;
int main()
{
      int a,b,c;
      scanf("%d%d",&a,&b);
      c=a+b;
      printf("%d",c);
}
 

img

该回答引用ChatGPT

这段代码中存在两个问题:

使用了错误的输出函数


printf("%d",&c);
应该改成:


printf("%d", c);
没有包含头文件

应该添加头文件:


#include <cstdio>

修改后的代码如下所示:



#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
      int a, b, c;
      scanf("%d%d", &a, &b);
      c = a + b;
      printf("%d", c);
      return 0;
}

请注意,为了避免程序产生未知的行为,建议在程序开头添加头文件。