为什么c语言 do while下宏函数do

C语言用宏定义函数
& 1 #include
& 3 #define transit(a, b, c, type) {
type t = & &
& 5 & & a =
& 6 & & b =
& 7 & & c =
&10 int main(int argc, char **argv)
&12 & & int
&13 & & int
&14 & & int
printf("a=%d, b=%d, c=%d\n", a, b, c);
transit(a, b, c, int);
printf("a=%d, b=%d, c=%d\n", a, b ,c);
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。C/C++(25)
类函数宏:function-like-macro 用#define使用参数来定义宏,宏的参数用圆括号括起来,可以使一个参数或者多个参数,然后在使用的过程中这些参数将会被替换。
#define SQUARE(X) X*X //定义类函数宏
z = SQUARE(3); //使用类函数宏源代码:
[root@rhel6164 test]# cat test.c
#include &stdio.h&
#define SQUARE(X) X*X
int squ_fun(int);
int main(void)
int x = 0;
printf(&Please input one integer(n), and square(n+1) will be shown via two \
ways(function-like macro and general function). The default is 0.\n&);
scanf(&%d&, &x);
printf(&Evaluating SQUARE(3) is: %d \n&, SQUARE(x+1));
printf(&Evaluating squ_fun(3) is: %d \n&, squ_fun(x+1));
int squ_fun(int i)
int z = 0;
}编译&执行:[root@rhel6164 test]# gcc test.c
[root@rhel6164 test]# ./a.out
Please input one integer(n), and square(n+1) will be shown via two ways(function-like macro and general function). The default is 0.
Evaluating SQUARE(3) is: 7
Evaluating squ_fun(3) is: 16类函数宏只会做字符串替换(宏定义都只会做字符串替换),替换后为3+1*3+1,结果就是7;而一般函数就会把参数带进函数进行计算后返回结果,这里其实就是(3+1)*(3+1)。
友情链接:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:59304次
积分:1782
积分:1782
排名:第17070名
原创:120篇
转载:35篇
(1)(1)(2)(2)(1)(3)(2)(5)(1)(2)(9)(9)(1)(5)(17)(15)(1)(8)(11)(23)(22)(16)

我要回帖

更多关于 c语言do while语句 的文章

 

随机推荐