macros in embedded c

15
Macros Perhaps the most useful aspect of the C preprocessor is the ability to create and use macros. Macros enable you to assign short names to source code blocks. When you use the macro name in your source file, the preprocessor replaces it with the source code block specified in the macro definition.

Upload: sandesh-kumar

Post on 27-Apr-2015

448 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Macros in Embedded c

MacrosPerhaps the most useful aspect of the C

preprocessor is the ability to create and use macros.

Macros enable you to assign short names to source code blocks.

When you use the macro name in your source file, the preprocessor replaces it with the source code block specified in the macro definition.

Page 2: Macros in Embedded c

Types of macrosA macro definition includes the name of the

macro, the macro body, and may include macro arguments.

Simple Macros require no arguments and are the simplest macros to define.

Complex Macros accept one or more arguments and may be used like functions.

Macro Operators lists special macro operators that may be used in macro definitions.

Predefined Macros lists macros that are defined by the compiler at compile-time.

Page 3: Macros in Embedded c

Simple MacrosA simple macro is merely an abbreviation

for a fragment of code. It is often called a manifest constant

because it defines a name for a constant value.

Macros must be defined using the #define directive before they can be used. For example:

Page 4: Macros in Embedded c

Example #define LEN 128 defines a macro named

LEN. When LEN is used in your program (or in preprocessor directives) it is replaced with the text 128.

So, a C statement like char buffer[LEN];is expanded by the preprocessor into char buffer[128]; and is subsequently compiled by the compiler.

Page 5: Macros in Embedded c

Complex macrosA complex macro accepts arguments and

generates a fragment of code using the values of those arguments.

Macros that accept arguments appear to be functions. However, arguments are not typed as in a C function. They are merely replaced by the text passed to the macro when expanded.

Macros with arguments must be defined using the #define directive before they can be used.

Page 6: Macros in Embedded c

The argument list is enclosed in parentheses and must immediately follow the macro name. Spaces are not allowed between and macro name and open parenthesis.

Example:-#define MAX(x,y) ((x) > (y) ? (x) : (y))defines a macro named MAX that takes two

arguments (x and y). When MAX is used in your program it is replaced with the text ((x) > (y) ? (x) : (y)). If x and y are numeric constants, the preprocessor can determine the result of the macro and substitute the greater value.

Page 7: Macros in Embedded c

The number of arguments passed to a macro must match the number of arguments specified in the macro definition.

It is common practice to surround arguments used in a macro definition with parentheses. This is done so that compound expressions, when passed to a macro, do not cause unwanted side-effects.

Page 8: Macros in Embedded c

Macros may be defined with a null or empty argument list. For example: #define MYMACRO() (func();)

To call such a macro, you must specify the macro name along with an empty argument list. For example:MYMACRO()

To pass an empty argument to a macro, you must include at least one whitespace character in the place of that argument.

Page 9: Macros in Embedded c

Macro OperatorsThree preprocessor operators may be

used in #define or #if and #elif preprocessor directives.

Stringize Operator (#)Token-Pasting Operator (##)Defined Operator

Page 10: Macros in Embedded c

Stringize (#)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 stringizer(x) printf (#x "\n") stringizer(text)

Page 11: Macros in Embedded c

Token Pasting (##)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.

Page 12: Macros in Embedded c

For example:#define tokenpaster(n) printf ("token" #n " =

%d",token##n)tokenpaster(34);

Page 13: Macros in Embedded c

DefinedThe preprocessor defined operator is used in

constant expressions to determine if an identifier has been defined (by the #define preprocessor directive).

If the specified identifieris defined, the value is true (non-zero). If the symbol is not defined, the value is false (zero). The defined operator is specified as follows:defined (identifier)ordefined identifier

The defined operator may be used in #if or #elif directives only.

Page 14: Macros in Embedded c

Predefined MacrosThe C Compiler provides the following predefined

constants you may use in preprocessor directives and C code to create portable programs.

There are two (2) leading and trailing underscore characters ('_') used for the predefined constants.

Page 15: Macros in Embedded c

Constant Description

__CA__ Version number of the compiler (for example, 101 for version 1.01).

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

__FILE__ Name of the file being compiled.

__FLOAT64__ Defined to 1 to indicate that the FLOAT64 directive is active.

__KEIL__ Defined to 1 to indicate the Keil CARM Compiler is used.

__LINE__ Current line number in the file being compiled.

__THUMB__ CPU mode selected: 0 for ARM mode,

1 for THUMB mode.

__TIME__ Time when the compilation was started.

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