×
单片机 > 单片机程序设计 > 详情

C51编译器中的预处理器指令及使用解析

发布时间:2020-06-04 发布时间:
|

Cx51编译器中的预处理器处理源程序文件中的指令。Cx51支持所有的ANSI C指令。

Directives指令

预处理器指令前面不能有空格,并且必须加前缀‘#’如:

#pragma

#include

#define DEBUG 1

下面列出预处理器指信令和简单描述

指令 描述

Define 定义一个预处理器宏或常量

elif 如果前面的if, ifdef, ifndef或elif分支都不成立的话,初始化if条件的一个分支

else 如果前面的if, ifdef或 ifndef分支都不成立的话,初始化if条件的一个分支

endif 结束一个if, ifdef, ifndef, elif, 或 else 块

error 输出用户定义的一个错误信息。这个指令使用编译器发出一个特定的错误信息

ifdef 为条件编译求表达式的值。要被计算的参数就是一个definition 定义的名字

ifndef 与ifdef相同,只是如果这个名字没有被定义时运算结果为真

if 为条件编译求表达式的值

include 从外部文件中读取源程序文本。

line 指定一个行号和一个可选的文件名,这个指令用于在错误信息中定位错误的位置。Specifies a line number together with an optional filename. These specifications are used in error messages to identify the error position.

pragma 允许你使用可以在C51命令行上使用的控制指令。Allows you to specify control directives that may be included on the C51 command line. Pragmas may contain the same control directives that are specified on the command line.

undef 检查一个宏或常量是否已经定义

Stringize Operator(#)

字符化操作符#

如果操作符出现在宏的参数的前面,参数库就会做为字符串传递到宏里。如

#define stringer(x) printf (#x “\n”)

stringer (text)

预处理器处理的结果为:

printf (“text\n”)

Token-pasting operator(##)

这个操作符在宏定义中连结两个参数。它允许两个分开的标号在宏定义中合并为一个标号

如果在宏定中一个宏参数名字紧随着或紧跟着这操作符,宏参数和这个操作符被参数传递的值取代。邻接着操作符的文本不是宏参数名称的宏是不受影响的。如:

#define paster(n) printf (“token” #n “ = %d”, token##n)

paster (9);

预处理器处理的结果为:

printf (“token9 = %d”, token9);

Predefined Macro Constants预定义宏常量

常量 描述

_ _C51_ _ C51编译器的版本Version number of the Cx51 compiler (for example, 610 for version 6.10)。

_ _DATE_ _ 编译开始的日期Date when the compilation was started.

_ _FILE_ _ 被编译文件的名字Name of the file being compiled.

_ _LINE_ _ 被编译文件当前的行号Current line number in the file being compiled.

_ _MODEL_ _ 选择的存储器模式Memory model selected (0 for small, 1 for compact, 2 for large)。

_ _TIME_ _ 编译开始的时间Time when the compilation was started.__STDC_ _ 定义为1时,表示完全遵守ANSI C标准


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
ARM 汇编的必知必会