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语 ...
随机推荐
- 索引大小 975.45 MB (1,022,836,736)
975.45 MB (1,022,836,736)
- Hadoop实战-Flume之Hdfs Sink(十)
a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe/configure the source a1.sources.r1.type = ...
- UniGui的信息弹出框MessageDlg自定义标题的方法(使用JS动态本地化文本)
UniGui的信息弹出框MessageDlg的原型定义如下: procedure MessageDlg(const Msg: string; DlgType: TMsgDlgType; Buttons ...
- RobotFramework教程使用笔记——web自动化测试弹窗处理
在web自动化测试中会遇到各种弹出框,在selenium中有对这些弹出框的处理. 弹出框一般有这么几类: 1.普通的弹出窗口,如果是可以定位的,直接定位到窗口,然后进行相应的操作. 2.如果是浏览器系 ...
- 基于BASYS2的VHDL程序与仿真——50%占空比8分频器
转帖请注明转自http://www.cnblogs.com/connorzx/p/3547673.html 一.新建工程 1.点击File->New Project, 2.点击Next 注:此处 ...
- H264视频通过RTMP直播
http://blog.csdn.net/firehood_/article/details/8783589 前面的文章中提到了通过RTSP(Real Time Streaming Protocol) ...
- maven中常用命令
1. 更新本地仓库, 首先确认C:\users\pengqiong\ 路径下有相应的pom文件 mvn clean package install:
- ZIP伪加密(deprecated)
ZIP伪加密 经过伪加密的apk,改成zip格式打开会发现里面的文件都经过了加密. APK实际上是Zip压缩文件,但是Android系统在解析APK文件时,和传统的解压压缩软件在解析Zip文件时存在差 ...
- 【CQ18高一暑假前挑战赛3】标程
[A:LCM] #include<bits/stdc++.h> using namespace std; #define ll long long int main() { ll a,b, ...
- mybatis编写流程(老版本的方式,新版本用接口式编程)
1.根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象 有数据源一些运行环境信息2.sql映射文件:配置了每一个sql,以及sql的封装规则等. 3.将sql映射文件注 ...