您的位置:首页 > 移动开发 > Objective-C

Objective-C 学习笔记5 预处理程序

2013-01-22 10:49 323 查看
预处理程序类似于简单替换,没有逻辑运算

1、预处理程序以#号开头,结尾没有任何符号,不换行,换行使用换行符号

2、预处理程序只是替换,没有计算、逻辑等运算

3、不能定义同一个常量多次,否则会报错

如下,OC中没有TRUE FALSE 可以定义一个

#define TRUE 1 //定义遇到TRUE就替换为1
#define FALSE 0 //定义遇到FALSE 就替换为0

if(MAX==TRUE)//等于 if(MAX==1)


下面一个简单的示例(Code Blocks )代码

#import <Foundation/Foundation.h>
#define P_MAX 99
#define P_MIN 9
#define PI 3.1415926
#define VERSION 1
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello World!");
[pool drain];
//复制
int n=9;
n=n*PI;
if(n>P_MAX){
NSLog(@"oh no this max max");
}
return 0;
}


加入计算X2(y) 公式,计算一个数的平方

#import <Foundation/Foundation.h>
#define P_MAX 99
#define P_MIN 9
#define PI 3.1415926
#define X2(y) y*y
#define VERSION 1
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello World!");
[pool drain];
//复制
int n=9;
n=n*PI;
if(n>P_MAX){
NSLog(@"oh no this max max");
}
n=X2(100);
NSLog(@"%d",n);
return 0;
}


计算的优先级

#import <Foundation/Foundation.h>
#define P_MAX 99
#define P_MIN 9
#define PI 3.1415926
#define X2(y) y*y
#define ADDXY(x,y) x+y
#define VERSION 1
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello World!");
[pool drain];
//复制
int n=9;
n=n*PI;
if(n>P_MAX){
NSLog(@"oh no this max max");
}
//一个数的平方
n=X2(100);
NSLog(@"%d",n);
//优先级
n=ADDXY(1,5)*ADDXY(1,6);//等于x+y *x+y
n=(ADDXY(1,5))*(ADDXY(1,6));//等于(x+y) *(x+y)
//以上两个结果完全不一样
return 0;
}


条件编译

#import <Foundation/Foundation.h>
#define P_MIN 9
#define PI 3.1415926
#define X2(y) y*y
#define ADDXY(x,y) x+y
#define VERSION 1
#define DEBUG YES
#ifdef DEBUG==YES
#define P_MAX 999
#else
#define P_MAX 99
#endif
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello World!");
[pool drain];
//复制
int n=9;
n=n*PI;
if(n>P_MAX){
NSLog(@"oh no this max max");
}
//一个数的平方
n=X2(100);
NSLog(@"%d",n);
//优先级
n=ADDXY(1,5)*ADDXY(1,6);//等于x+y *x+y
n=(ADDXY(1,5))*(ADDXY(1,6));//等于(x+y) *(x+y)
//以上两个结果完全不一样

//条件编译
if(DEBUG==YES)
{
NSLog(@"yes print");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: