Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
50
Добавлен:
14.04.2015
Размер:
1.37 Mб
Скачать

Keil Software — Cx51 Compiler User’s Guide

135

 

 

Chapter 4. Preprocessor

The preprocessor built into the Cx51 compiler handles directives found in the source file. The Cx51 compiler supports all of the ANSI Standard C directives. This chapter gives a brief overview of the preprocessor.

Directives

Preprocessor directives must be the first non-whitespace text specified on a line. All directives are prefixed with the pound or number-sign character (‘#’). For example:

#pragma

#include <stdio.h> #define DEBUG 1

The following table lists the preprocessor directives and gives a brief description

4

of each.

 

Directive

Description

 

define

Defines a preprocessor macro or constant.

 

elif

Initiates an alternative branch of the if condition, when the previous if, ifdef, ifndef,

 

 

or elif branch was not taken.

 

else

Initiates an alternative branch when the previous if, ifdef, or ifndef branch was not

 

 

taken.

 

endif

Ends an if, ifdef, ifndef, elif, or else block.

 

error

Outputs an error message defined by the user. This directive instructs the

 

 

compiler to emit the specified error message.

 

ifdef

Evaluates an expression for conditional compilation. The argument to be evaluated

 

 

is the name of a definition.

 

ifndef

Same as ifdef but the evaluation succeeds if the definition is not defined.

 

if

Evaluates an expression for conditional compilation.

 

include

Reads source text from an external file. The notation sequence determines the

 

 

search sequence of the included files. Cx51 searches for include files specified

 

 

with less-than/greater-than symbols (‘<’ ‘>’) in the include file directory. Cx51

 

 

searches for include files specified with double-quotes (“ “) in the current directory.

 

line

Specifies a line number together with an optional filename. These specifications

 

 

are used in error messages to identify the error position.

 

pragma

Allows you to specify directives that may be included on the C51 command line.

 

 

Pragmas may contain the same directives that are specified on the command line.

 

undef

Deletes a preprocessor macro or constant definition.

 

136 Chapter 4. Preprocessor

Stringize Operator

The stringize or number-sign operator (‘#’), when used within a macro definition, converts a macro parameter into a string constant. This operator may be used only in a macro that has a specified argument or parameter list.

When the stringize operator immediately precedes the name of one of the macro parameters, the parameter passed to the macro is enclosed within quotation marks and is treated as a string literal. For example:

#define stringer(x) printf (#x "\n")

stringer (text)

This example results in the following actual output from the preprocessor:

printf ("text\n")

4

 

The expansion shows that the parameter is converted literally as if it were a

 

 

 

 

string. When the preprocessor stringizes the x parameter, the resulting line is:

 

 

 

 

 

printf ("text" "\n")

 

 

 

Because strings separated by whitespace are concatenated at compile time, these

 

 

two strings are combined into "text\n".

 

 

If the string passed as a parameter contains characters that should normally be

 

 

literalized or escaped (for example, " and \), the required \ character is

 

 

automatically added.

Keil Software — Cx51 Compiler User’s Guide

137

 

 

Token-pasting operator

The token-pasting operator (##) within a macro definition combines two arguments. It permits two separate tokens in the macro definition to be joined into a single token.

If the name of a macro parameter used in the macro definition is immediately preceded or followed by the token-pasting operator, the macro parameter and the token-pasting operator are replaced by the value of the passed parameter. Text that is adjacent to the token-pasting operator that is not the name of a macro parameter is not affected. For example:

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

paster (9);

This example results in the following actual output from the preprocessor:

 

4

printf ("token9 = %d", token9);

 

This example shows the concatenation of token##n into token9. Both the stringize and the token-pasting operators are used in this example.

138

Chapter 4. Preprocessor

 

 

Predefined Macro Constants

The Cx51 compiler provides you with predefined constants to use in preprocessor directives and C code for more portable programs. The following table lists and describes each one.

 

 

Constant

Description

 

 

 

_ _C51_ _

Version number of the C51 compiler (for example, 610 for version 6.10).

 

 

 

_ _CX51_ _

Version number of the CX51 compiler (for example, 610 for version 6.10).

 

 

 

_ _DATE_ _

Date when the compilation was started in ANSI format (month dd yyyy).

 

 

 

_ _DATE2_ _

Date when the compilation in short form (mm/dd/yy).

 

 

 

_ _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.

 

4

 

 

__STDC_ _

Defined to 1 to indicate full conformance with the ANSI C Standard.

 

 

 

 

 

 

 

 

 

 

Соседние файлы в папке HLP