- •Chapter 1. Introduction
- •Support for all 8051 Variants
- •Books About the C Language
- •Chapter 2. Compiling with the Cx51 Compiler
- •Environment Variables
- •Running Cx51 from the Command Prompt
- •ERRORLEVEL
- •Cx51 Output Files
- •Control Directives
- •Directive Categories
- •Reference
- •Chapter 3. Language Extensions
- •Keywords
- •Memory Areas
- •Program Memory
- •Internal Data Memory
- •External Data Memory
- •Far Memory
- •Special Function Register Memory
- •Memory Models
- •Small Model
- •Compact Model
- •Large Model
- •Memory Types
- •Explicitly Declared Memory Types
- •Implicit Memory Types
- •Data Types
- •Bit Types
- •Special Function Registers
- •sbit
- •Absolute Variable Location
- •Pointers
- •Generic Pointers
- •Pointer Conversions
- •Abstract Pointers
- •Function Declarations
- •Function Parameters and the Stack
- •Passing Parameters in Registers
- •Function Return Values
- •Specifying the Memory Model for a Function
- •Specifying the Register Bank for a Function
- •Register Bank Access
- •Interrupt Functions
- •Reentrant Functions
- •Chapter 4. Preprocessor
- •Directives
- •Stringize Operator
- •Predefined Macro Constants
- •Chapter 5. 8051 Derivatives
- •Analog Devices MicroConverter B2 Series
- •Atmel 89x8252 and Variants
- •Dallas 80C320, 420, 520, and 530
- •Arithmetic Accelerator
- •Data Pointers
- •Library Routines
- •Philips 8xC750, 8xC751, and 8xC752
- •Philips 80C51MX Architecture
- •Philips and Atmel WM Dual DPTR
- •Customization Files
- •STARTUP.A51
- •INIT.A51
- •XBANKING.A51
- •Basic I/O Functions
- •Memory Allocation Functions
- •Optimizer
- •General Optimizations
- •Options for Code Generation
- •Segment Naming Conventions
- •Data Objects
- •Program Objects
- •Interfacing C Programs to Assembler
- •Function Parameters
- •Parameter Passing in Registers
- •Parameter Passing in Fixed Memory Locations
- •Function Return Values
- •Using the SRC Directive
- •Register Usage
- •Overlaying Segments
- •Example Routines
- •Small Model Example
- •Compact Model Example
- •Large Model Example
- •Data Storage Formats
- •Bit Variables
- •Signed and Unsigned Long Integers
- •Generic and Far Pointers
- •Floating-point Numbers
- •Accessing Absolute Memory Locations
- •Absolute Memory Access Macros
- •Linker Location Controls
- •The _at_ Keyword
- •Debugging
- •Chapter 7. Error Messages
- •Fatal Errors
- •Actions
- •Errors
- •Syntax and Semantic Errors
- •Warnings
- •Chapter 8. Library Reference
- •Intrinsic Routines
- •Library Files
- •Standard Types
- •va_list
- •Absolute Memory Access Macros
- •CBYTE
- •CWORD
- •DBYTE
- •DWORD
- •FARRAY, FCARRAY
- •FVAR, FCVAR,
- •PBYTE
- •PWORD
- •XBYTE
- •XWORD
- •Routines by Category
- •Buffer Manipulation
- •Character Conversion and Classification
- •Data Conversion
- •Math Routines
- •Memory Allocation Routines
- •Stream Input and Output Routines
- •String Manipulation Routines
- •Miscellaneous Routines
- •Include Files
- •8051 Special Function Register Include Files
- •ABSACC.H
- •ASSERT.H
- •CTYPE.H
- •INTRINS.H
- •MATH.H
- •SETJMP.H
- •STDARG.H
- •STDDEF.H
- •STDIO.H
- •STDLIB.H
- •STRING.H
- •Reference
- •Compiler-related Differences
- •Library-related Differences
- •Appendix B. Version Differences
- •Version 6.0 Differences
- •Version 5 Differences
- •Version 4 Differences
- •Version 3.4 Differences
- •Version 3.2 Differences
- •Version 3.0 Differences
- •Version 2 Differences
- •Appendix C. Writing Optimum Code
- •Memory Model
- •Variable Location
- •Variable Size
- •Unsigned Types
- •Local Variables
- •Other Sources
- •Appendix D. Compiler Limits
- •Appendix E. Byte Ordering
- •Recursive Code Reference Error
- •Problems Using the printf Routines
- •Uncalled Functions
- •Using Monitor-51
- •Trouble with the bdata Memory Type
- •Function Pointers
- •Glossary
- •Index
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. |
|
|
|
|
|
|
|
|
|
|
|
|