常量作业
#include <stdio.h>#define r 5
#define S(r) "2*3.14*r*r"
#define C(r) "2*3.14*r"
int main()
{
const char* NL="\n";
printf("Line1%s",NL);
printf("Line2%s",NL);
printf("半径为%d的圆\n",r);
printf("面积是:%4.2f\n",S(r));
printf("周长是:%4.2f\n",C(r));
return 0;
}
输出
常量-作业.c:26:34: warning: format specifies type 'double' but the argument has type 'char *' [-Wformat]
printf("面积是:%4.2f\n",S(r));
~~~~~ ^~~~
%4.2s
常量-作业.c:16:14: note: expanded from macro 'S'
#define S(r) "2*3.14*r*r"
^~~~~~~~~~~~
常量-作业.c:27:34: warning: format specifies type 'double' but the argument has type 'char *' [-Wformat]
printf("周长是:%4.2f\n",C(r));
~~~~~ ^~~~
%4.2s
常量-作业.c:17:14: note: expanded from macro 'C'
#define C(r) "2*3.14*r"
^~~~~~~~~~
2 warnings generated.
该怎么改
#include <stdio.h>
#define r 5
#define S(r) 2*3.14*r*r // 把双引号去掉
#define C(r) 2*3.14*r // 把双引号去掉
int main()
{
const char* NL="\n";
printf("Line1%s",NL);
printf("Line2%s",NL);
printf("半径为%d的圆\n",r);
printf("面积是:%4.2f\n",S(r));
printf("周长是:%4.2f\n",C(r));
return 0;
} #include <stdio.h>
#define r 5
// 去掉字符串
#define S(r) 2 * 3.14 * r *r
#define C(r) 2 * 3.14 * r
int main()
{
const char *NL = "\n";
printf("Line1%s", NL);
printf("Line2%s", NL);
printf("半径为%d的圆\n", r);
printf("面积是:%4.2f\n", S(r));
printf("周长是:%4.2f\n", C(r));
return 0;
} ckblt 发表于 2022-3-19 15:07
楼市要快一点,也谢谢你呀 isdkz 发表于 2022-3-19 15:06
#include <stdio.h>
#define S(r) 3.14159*r*r
int main()
{
const char* NL="\n";
const double C(r)=2*3.1415*r;
int r=5;
printf("Line1%s",NL);
printf("Line2%s",NL);
printf("半径为%d的圆\n",r);
printf("面积是:%4.2f\n",S(r));
printf("周长是:%4.2f\n",C(r));
return 0;
}
输出
常量-作业.c:22:20: error: a parameter list without types is only allowed in a function definition
const double C(r)=2*3.1415*r;
^
常量-作业.c:22:32: error: use of undeclared identifier 'r'
const double C(r)=2*3.1415*r;
^
2 errors generated.
问:想用const该怎么定义 Weabu 发表于 2022-3-19 15:14
#include
首先,你就算是声明函数也不能把返回值声明为常量,
然后,你这个是定义宏,只能用 #define isdkz 发表于 2022-3-19 15:16
首先,你就算是声明函数也不能把返回值声明为常量,
然后,你这个是定义宏,只能用 #define
那如何用const定义函数呢 Weabu 发表于 2022-3-19 15:21
那如何用const定义函数呢
const 是定义常量的,不能用来定义函数,
因为函数的返回值不能为常量 isdkz 发表于 2022-3-19 15:23
const 是定义常量的,不能用来定义函数,
因为函数的返回值不能为常量
好的
谢谢
页:
[1]