iOS 7:漫谈#define 宏定义
- //This defines PI
- #define M_PI 3.14159265358979323846264338327950288
- #define M_PI 3.14159265358979323846264338327950288
- double r = 10.0;
- double circlePerimeter = 2 * M_PI * r;
- // => double circlePerimeter = 2 * 3.14159265358979323846264338327950288 * r;
- printf("Pi is %0.7f",M_PI);
- //Pi is 3.1415927
- //A simple function-like macro
- #define SELF(x) x
- NSString *name = @"Macro Rookie";
- NSLog(@"Hello %@",SELF(name));
- // => NSLog(@"Hello %@",name);
- // => Hello Macro Rookie
- #define PLUS(x,y) x + y
- printf("%d",PLUS(3,2));
- // => printf("%d",3 + 2);
- // => 5
- //Version 1.0
- #define MIN(A,B) A < B ? A : B
- int a = MIN(1,2);
- // => int a = 1 < 2 ? 1 : 2;
- printf("%d",a);
- // => 1
- int a = 2 * MIN(3, 4);
- printf("%d",a);
- // => 4
- int a = 2 * MIN(3, 4);
- // => int a = 2 * 3 < 4 ? 3 : 4;
- // => int a = 6 < 4 ? 3 : 4;
- // => int a = 4;
- //Version 2.0
- #define MIN(A,B) (A < B ? A : B)
- int a = MIN(3, 4 < 5 ? 4 : 5);
- printf("%d",a);
- // => 4
- int a = MIN(3, 4 < 5 ? 4 : 5);
- // => int a = (3 < 4 < 5 ? 4 : 5 ? 3 : 4 < 5 ? 4 : 5); //希望你还记得运算符优先级
- // => int a = ((3 < (4 < 5 ? 4 : 5) ? 3 : 4) < 5 ? 4 : 5); //为了您不太纠结,我给这个式子加上了括号
- // => int a = ((3 < 4 ? 3 : 4) < 5 ? 4 : 5)
- // => int a = (3 < 5 ? 4 : 5)
- // => int a = 4
- //Version 3.0
- #define MIN(A,B) ((A) < (B) ? (A) : (B))
- float a = 1.0f;
- float b = MIN(a++, 1.5f);
- printf("a=%f, b=%f",a,b);
- // => a=3.000000, b=2.000000
- float a = 1.0f;
- float b = MIN(a++, 1.5f);
- // => float b = ((a++) < (1.5f) ? (a++) : (1.5f))
- int a = ({
- int b = 1;
- int c = 2;
- b + c;
- });
- // => a is 3
- //GNUC MIN
- #define MIN(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; })
- //CLANG MIN
- #define __NSX_PASTE__(A,B) A##B
- #define MIN(A,B) __NSMIN_IMPL__(A,B,__COUNTER__)
- #define __NSMIN_IMPL__(A,B,L) ({ __typeof__(A) __NSX_PASTE__(__a,L) = (A); __typeof__(B) __NSX_PASTE__(__b,L) = (B); (__NSX_PASTE__(__a,L) < __NSX_PASTE__(__b,L)) ? __NSX_PASTE__(__a,L) : __NSX_PASTE__(__b,L); })
- #define __NSX_PASTE__(A,B) A##B
- #define MIN(A,B) __NSMIN_IMPL__(A,B,__COUNTER__)
- #define __NSMIN_IMPL__(A,B,L) ({ __typeof__(A) __NSX_PASTE__(__a,L) = (A); \
- __typeof__(B) __NSX_PASTE__(__b,L) = (B); \
- (__NSX_PASTE__(__a,L) < __NSX_PASTE__(__b,L)) ? __NSX_PASTE__(__a,L) : __NSX_PASTE__(__b,L); \
- })
iOS 7:漫谈#define 宏定义的更多相关文章
- iOS 7:漫谈#define 宏定义(转)
iOS :漫谈#define 宏定义 #define宏定义在C系开发中可以说占有举足轻重的作用.底层框架自不必说,为了编译优化和方便,以及跨平台能力,宏被大量使用,可以说底层开发离开define将寸步 ...
- iOS define 宏定义 和 const定义常量区别
const const 是c++中的修饰符. c++中常用来定义常量,修饰左值. #define 宏定义语句, 在预处理阶段直接做文本替换,不做类型检查. 它们之间的最大区别: 1. 对于co ...
- iOS开发经常使用宏定义
iOS开发经常使用宏定义 iOS开发中经常须要获取屏幕宽度高度,为view设置颜色,为imgagView设置图片等,我们都可定义一些宏,随时都可拿来使用,方便开发 <span style=&qu ...
- 面试问题5:const 与 define 宏定义之间的区别
问题描述:const 与 define 宏定义之间的区别 (1) 编译器处理方式不同 define宏是在预处理阶段展开: const常量是编译运行阶段使用: (2) 类型和安全检查不同 ...
- define宏定义中的#,##,@#及\符号
define宏定义中的#,##,@#及\符号 在#define中,标准只定义了#和##两种操作.#用来把参数转换成字符串,##则用来连接两个前后两个参数,把它们变成一个字符串. 1.# (string ...
- iOS开发常见的宏定义(实用)
iOS开发过程中使用一些常用的宏可以提高开发效率,提高代码的重用性:将这些宏放到一个头文件里然后再放到工程中的-Prefix.pch文件中(或者直接放到-Prefix.pch中)直接可以使用,灰常方便 ...
- #define宏定义形式的"函数"导致的bug
定义了一个宏定义形式的"函数": #define SUM8(YY)\ {\ int Y = YY>>2;\ ...\ } 然后使用的时候,传入了一个同名的变量Y: i ...
- define宏定义和const常变量区别
1.define是宏定义,程序在预处理阶段将用define定义的内容进行了替换.因此程序运行时,常量表中并没有用define定义的常量,系统不为它分配内存.const定义的常量,在程序运行时在常量表中 ...
- typedef 类型重命名 和 #define 宏定义(1)
http://www.blogjava.net/jasmine214--love/archive/2010/11/29/339307.html 在现实生活中,信息的概念可能是长度,数量和面积等.在C语 ...
随机推荐
- Redis persistence demystified
https://redis.io/topics/persistence http://oldblog.antirez.com/post/redis-persistence-demystified.ht ...
- Nothing but the key 属性全部依赖于主键 third norm form
全依赖 Designs that Violate 1NF CustomerCustomer ID First Name Surname Telephone Number123 Pooja Singh ...
- [自动化平台系列] - 初次使用 Macaca-前端自动化测试(3)
1. 如果是一个列表页面,当要触发编辑页面是如何做的呢?其实我测试只要点击第一条数据去编辑就好啦!如果页面结构如下 <li class="myatc-li"> < ...
- unity导出android项目
1. 2 . 3 选择Google Android Project(若不选则直接导出Apk) Export,Android项目即可导出成功.
- hadoop 添加,删除节点
http://www.cnblogs.com/tommyli/p/3418273.html
- codeforces 465C.No to Palindromes! 解题报告
题目链接:http://codeforces.com/problemset/problem/464/A 题目意思:给出一个长度为 n 且是 tolerable 的字符串s,需要求出字典序最小的长度也为 ...
- php排序方法之插入排序
//插入排序法 $arr = array(3,55,45,2,67,76,6.7,-65,85,4); function insertSort($arr){ for ( $i=0; $i<cou ...
- [原创]java在线比较两个word文件
一.项目背景 开发文档管理系统或OA办公系统的时候,实现在线处理word文档的功能比较容易,但是也经常会有客户提出文档版本管理的需求,这就需要同时在线打开两个word文件,对比两个不同版本的word文 ...
- linux下syslog使用说明
转自:http://blog.chinaunix.net/uid-25120309-id-3359929.html syslog 系统日志应用 1) 概述 syslog是Linux系统默 ...
- [转载]支付宝钱包手势密码破解实战(root过的手机可直接绕过手势密码)
/* *转自http://blog.csdn.net/hu3167343/article/details/36418063 *本文章由 莫灰灰 编写,转载请注明出处. *作者:莫灰灰 邮箱: m ...