一个简单的问题

[复制链接]
3012|14
 楼主| 甄蔡组合 发表于 2007-4-2 10:03 | 显示全部楼层 |阅读模式
在程序里面出现
#ifdef xxxx
。。。
#else
。。。
。。。
#endif
这样的语句是什么意思。和if语句有什么区别!!
那位大虾有相关的资料给小弟发一份。
ayb_ice 发表于 2007-4-2 10:08 | 显示全部楼层

随便说说

  典型的C预处理...
awey 发表于 2007-4-2 10:11 | 显示全部楼层

给编译器看的,告诉编译器编译哪个程序段

xiaoyu9632 发表于 2007-4-2 17:07 | 显示全部楼层

意思

就是如果定义了某些东西就不要再一次的重复定义
sodwell 发表于 2007-4-2 22:45 | 显示全部楼层

:)

楼上的错了,楼上的楼上对头!
平常人 发表于 2007-4-2 22:47 | 显示全部楼层

学名叫“条件编译”,即根据不同的条件编译不同的代码

所以3楼说的正确!
 楼主| 甄蔡组合 发表于 2007-4-3 11:22 | 显示全部楼层

小弟愚钝 能否举个例子

谢谢各位
陈双君 发表于 2007-4-3 11:38 | 显示全部楼层

3楼说的是正确的.

3楼说的是正确的.
ayb_ice 发表于 2007-4-3 11:40 | 显示全部楼层

随便说说

  这都是标准C的一部分,应该懂的...
popmu 发表于 2007-4-3 12:49 | 显示全部楼层

看C语言的书啊

ayb_ice 发表于 2007-4-3 15:02 | 显示全部楼层

KEIL HELP 里东东

#define
The #define directive defines a preprocessor macro.

#define macro-name « (arg« , arg ... ») » replacement-text

You may use #define to create function-like macros with or without arguments. Macros are syntactically similar to function calls. When a defined macro is encountered in the source file, the macro-name and any arguments are replaced by the replacement-text. For example:

#define my_macro(a,b,c) a+b+c

int func (int x, int y, int z)
{
return(my_macro(x,y,z));
}

appears as follows after macro expansion:

int func (int x, int y, int z)
{
return(x+y+z);
}

Refer to Macros for a complete description of how to define and use macros in your C programs.

Copyright (c) Keil Software, Inc. and Keil Elektronik GmbH. All rights reserved.
 楼主| 甄蔡组合 发表于 2007-4-3 20:16 | 显示全部楼层

有中文没

lishuanghua 发表于 2007-4-3 21:30 | 显示全部楼层

举个例子:

如果有一段代码是你想在测试的时候用的,而在正式的产品要用另一段代码:
测试时:
#define  TEST
#if defined TEST
     //测试的代码
#else
    //正式代码
#endif
在这里,编译器只会编译 测试的代码而忽略掉正式代码

正式产品:
//#define  TEST
#if defined TEST
     //测试的代码
#else
    //正式代码
#endif
在这里,编译器只会编译 正式代码而忽略测试的代码
yuanjian79 发表于 2007-4-5 01:09 | 显示全部楼层

#ifdef 是条件编译指令。

#ifdef 是条件编译指令。预编译就过滤掉不符合条件的代码。
if是条件判断关键字,负责程序流程控制。最终生成目标代码(跳转指令)。
#ifdef常见用法有
#ifdef _DEBUG
  printf("output debug information ");
#endif /*_DEBUG*/
对应的还有#ifndef,
常见用法防止头文件重包含
/*头文件开始处*/
#ifndef _FILE_H
....
#endif /*_FILE_H*/
 楼主| 甄蔡组合 发表于 2007-4-9 11:16 | 显示全部楼层

谢谢各位

感谢13 14楼的高人
您需要登录后才可以回帖 登录 | 注册

本版积分规则

33

主题

198

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部