C程序返回矩形的周长和面积
我想写一个c程序来打印矩形的面积和周长,但是我得到了一个分割错误C程序返回矩形的周长和面积,c,C,我想写一个c程序来打印矩形的面积和周长,但是我得到了一个分割错误 #include <stdio.h> int rectanglePerimeter(int h, int w) { int p =(2*h)+(2*w); return p; } int rectangleArea(int h, int w) { int a = h * w; return a; } int main() { int h, w, p
#include <stdio.h>
int rectanglePerimeter(int h, int w)
{
int p =(2*h)+(2*w);
return p;
}
int rectangleArea(int h, int w)
{
int a = h * w;
return a;
}
int main()
{
int h, w, perimeter, area;
printf("Tell me the height and width of the rectangle\n");
scanf("%i", h);
scanf("%i", w);
perimeter = rectanglePerimeter(h, w);
area = rectangleArea(h, w);
printf("Perimeter of the rectangle = %i inches", perimeter);
printf("Area of the rectangle = %i square inches", area);
return 0;
}
#包括
整数矩形周长(整数h,整数w)
{
int p=(2*h)+(2*w);
返回p;
}
int矩形区域(int h,int w)
{
int a=h*w;
返回a;
}
int main()
{
内高、西、周长、面积;
printf(“告诉我矩形的高度和宽度\n”);
scanf(“%i”,h);
scanf(“%i”,w);
周长=矩形周长(h,w);
面积=矩形面积(h,w);
printf(“矩形周长=%i英寸”,周长);
printf(“矩形面积=%i平方英寸”,面积);
返回0;
}
有人能解释一下我做错了什么吗?当使用C编程语言时,有些函数需要使用内存地址,这就是使用
scanf
函数的情况。Scanf函数接受参数,第一个参数是期望的数据类型,int、float、char等;第二个参数是保存这个输入的地方,这个地方不是要保存的变量的引用,而是变量的内存地址。这样,scanf
将保存在存储变量中读取的值
scanf(“%i”和&h);
scanf(“%i”和“w”);
scanf(“%i”,h)代码>=>scanf(“%i”和&h)代码>。。。更好的if(scanf(“%i”,&h)!=1)退出(退出失败)代码>Hawk3R,节省时间,允许所有编译器警告快速捕获代码中的问题,如scanf(“%i”,h)代码>。所以我需要访问内存地址。非常感谢。我正在用vim编写,并使用在线编译器。你能为linux推荐一些学习c语言的工具吗?安装gcc
(但很可能它已经安装了),如果你想继续使用vim