C语言代码错误分析及修正 - 计算两个整数的和
#include<stdio.h> int main(void) { int a,b,c; // Please input a,b: // 请输入 a 和 b: scanf('%d%d',&a,&b); c = a + b; printf('%d + %d = %d\n',a,b,c); return 0; }
这段代码有几个错误:
-
在输入提示部分,注释应该是
//Please input a,b:而不是/*********Begin*********/。 -
在
scanf函数中,应该使用%d而不是%d,%d来读取两个整数。 -
在
printf函数中,变量c没有被赋值,所以输出的结果会不正确。
下面是修正后的代码:
#include<stdio.h>
int main(void)
{
int a,b,c;
// Please input a,b:
// 请输入 a 和 b:
scanf('%d%d',&a,&b);
c = a + b;
printf('%d + %d = %d\n',a,b,c);
return 0;
}
修正后的代码将正确地读取两个整数,计算它们的和,并输出结果。
原文地址: http://www.cveoy.top/t/topic/PmF 著作权归作者所有。请勿转载和采集!