The C Preprocessor is not part of the compiler, but is a separate step in the compilation process. In simplistic terms, a C Preprocessor is just a text substitution tool. We'll refer to the C Preprocessor as the CPP. All preprocessor lines begin with #
Pre-Processors Examples:Analyze following examples to understand various directives.
Tells the CPP to replace instances of MAX_ARRAY_LENGTH with 20. Use #definefor constants to increase readability.
Tells the CPP to get stdio.h from System Libraries and add the text to this file. The next line tells CPP to get myheader.h from the local directory and add the text to the file.
Tells the CPP to undefine FILE_SIZE and define it for 42.
Tells the CPP to define MESSAGE only if MESSAGE isn't defined already.
Tells the CPP to do the following statements if DEBUG is defined. This is useful if you pass the -DDEBUG flag to gcc. This will define DEBUG, so you can turn debugging on and off on the fly! 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:
This will produce following result using stringization macro message_for
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. For example:
This example results in the following actual output from the preprocessor:
This example shows the concatenation of token##n into token34. Both the stringize and the token-pasting operators are used in this example. Parameterized Macros:One of the powerful functions of the CPP is the ability to simulate functions using parameterized macros. For example, we might have some code to square a number:
We can instead rewrite this using a macro:
Macros with arguments must be defined using the #define directive before they can be used. 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. For example:
Macro Caveats:
|
Pre-Processors
Subscribe to:
Post Comments (Atom)
C Tutorials
- C - Programming HOME
- C - Basic Introduction
- C - Program Structure
- C - Reserved Keywords
- C - Basic Datatypes
- C - Variable Types
- C - Storage Classes
- C - Using Constants
- C - Operator Types
- C - Control Statements
- C - Input and Output
- C - Pointing to Data
- C - Using Functions
- C - Play with Strings
- C - Str Datatypes
- C - Working with Files
- C - Bits Manipulation
- C - Pre-Processors
- C - Useful Concepts
0 comments:
Post a Comment