C语言类函数宏和一般函数的区别
来源:互联网 发布:win10看图软件 编辑:程序博客网 时间:2024/06/07 16:43
类函数宏: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*Xint 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)); return 0;}int squ_fun(int i){ int z = 0; z = i * i; return z;}编译&执行:
[root@rhel6164 test]# gcc test.c[root@rhel6164 test]# ./a.outPlease input one integer(n), and square(n+1) will be shown via two ways(function-like macro and general function). The default is 0.3Evaluating SQUARE(3) is: 7Evaluating squ_fun(3) is: 16类函数宏只会做字符串替换(宏定义都只会做字符串替换),替换后为3+1*3+1,结果就是7;而一般函数就会把参数带进函数进行计算后返回结果,这里其实就是(3+1)*(3+1)。
友情链接:可变宏参数个数和可变函数参数个数
0 0
- C语言类函数宏和一般函数的区别
- C语言中指针变量作为函数参数和一般变量作为函数参数的区别
- 【C语言】宏和函数的区别
- 【C语言】宏和函数的区别
- 【C语言】宏和函数的区别
- C语言函数设计的一般原则和技巧
- C语言函数设计的一般原则和技巧
- 构造函数和一般函数的区别
- 构造函数和一般函数的区别
- C语言内存操作函数和一般字符串操作函数
- 成员函数,友元函数和一般函数的区别
- 由原函数 成员函数 和一般函数的区别
- 成员函数友元函数和一般函数的区别
- 成员函数、友元函数和一般函数的区别
- 成员函数 友元函数 和 一般函数的区别
- 成员函数、友元函数和一般函数的区别
- c 语言 中 宏和函数的区别
- 宏和函数的区别(C语言)
- 二、Linux/UNIX操作命令积累【mkdir、groupadd、groupdel、useradd、userdel】
- zoj3818- Pretty Poem(爆枚型字符串题)
- Cocos2d-x C++调用Android弹出提示框
- cloudstack的manager端自动部署shell脚本
- Inside Geometry Instancing(下)
- C语言类函数宏和一般函数的区别
- 【HDU4991】dp 树状数组优化
- 反射的作用之spring的简单模拟
- 在struts-config.xml配置文件顺序异常的问题
- Oracle的性能优化
- 文件描述符与文件指针等文件操作的几个问题
- load and save cookies
- Spring 3 Validation (jsr-303)
- Word Break