我正在编写一个程序来计算矩形的面积。我以前从未用 C 编写过,但我相信我缺少理解函数和如何传递变量的基本方面。这就是我所拥有的:
int computeArea(int length, int width) {
int area;
area = length * width;
return(area);
}
int main() {
printf("Enter the length and width of the rectangle: \n");
scanf("%d %d", &length, &width);
printf("The area of a %d by %d rectangle is %d \n", length, width, area);
}
在 scanf 行上,我在输入的变量上收到“使用未声明的标识符”错误。谁能向我解释我在声明和使用这些变量时出错的地方?
有些东西不见了;首先,您必须在主函数中定义您使用的三个变量(int length = 0, width = 0, area = 0;);然后在打印结果之前,您必须调用计算面积的函数 (area = computeArea (length, width);)
int computeArea(int length, int width) {
int area;
area = length * width;
return(area);
}
int main() {
int length=0, width=0, area=0;
printf("Enter the length and width of the rectangle: \n");
scanf("%d %d", &length, &width);
area = computeArea(length,width);
printf("The area of a %d by %d rectangle is %d \n", length, width, area);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句