我想知道是否有人可以解释如何通过命令行传递参数?我真的很困惑它是如何工作的。现在我正试图将一个整数传递给主程序。我该怎么做呢?
编辑:继续从指针获取初始化使整数没有强制转换 [-Wint-conversion] 错误?
#include <stdio.h>
#define PI 3.1416
int
main (int argc, char *argv[])
{
double r,area, circ;
char a = argv[1];
int num = a - '0';
printf("You have entered %d",num);
r= num/2;
area = PI * r * r;
circ= 2 * PI * r;
printf ("A circle with a diameter of %d ", num);
printf ("has an area of %5.3lf cm2\n", area);
printf ("and a circumference of %4.2lf cm.\n", circ);
return (0);
}
Run Code Online (Sandbox Code Playgroud)