Preprocessors and Macros in C
Pre-processors in C are micro-processor that
is basically used by compilers. It converts the code and replaces it at the
line where the pre-processor used in your code before the actual compilation of your program.
This is the reason the process is known as pre-processing.
It is said to be a micro-pre-processor as it
allows you to ass macros in the program.
All the commands used in
Pre-processor are called the pre-processor directives. Preprocessor directives
should always begin with the “#” symbol and should begin in the first column.
Types of Pre-Processors Used in C
|
Preprocessor |
Syntax/Description |
|
Macro |
Syntax: #define |
|
Header file inclusion |
Syntax: #include <file_name> |
|
Conditional
compilation |
Syntax: #ifdef,
#endif, #if, #else, #ifndef |
|
Other directives |
Syntax: #undef, #pragma |
List of Few Pre-Processor Directives and Its Short
Description
|
Sr.No. |
Directive
& Description |
|
1 |
#define Substitutes a preprocessor macro. |
|
2 |
#include Inserts a particular the header from another file. |
|
3 |
#undef Undefines
a preprocessor macro. |
|
4 |
#ifdef Returns true if this
macro is defined. |
|
5 |
#ifndef Returns true if this macro is not
defined. |
|
6 |
#if Tests if a compile
time condition is true. |
|
7 |
#else The alternative for #if. |
|
8 |
#elif #else and #if in
one statement. |
|
9 |
#endif Ends preprocessor conditional. |
|
10 |
#error Prints error
message on stderr. |
Macros in C:
Macros are generally used to
define constant values that are being used repeatedly in programs.
Macros can even accept arguments
and such macros are known as function-like macros.
It can be useful if tokens are
concatenated into code to simplify some complex declarations.
Macros provide text replacement
functionality at pre-processing time.
A macro is a name given to a
block of C statements as a pre-processor directive.
Being a pre-processor, the block
of code is communicated to the compiler before entering into the actual coding
(main () function).
A macro is defined with the
preprocessor directive, #define
Types of Macros in C
A macro is a segment of code that is replaced
by the value of the macro. Macro is defined by #define directive. There are two
types of macros:
1.
Object-like Macros
2.
Function-like Macros
Object-Like Macro:
The object-like macro is an identifier that is
replaced by value. It is widely used to represent numeric constants. For
example:
#define PI 3.14
Here, PI is the macro name that will be
replaced by the value 3.14.
#Define Macro:
In the C Programming Language, the #define
directive allows the definition of macros within your source code.
These macro definitions allow constant values
to be declared for use throughout your code.
Macro definitions are not variables and cannot
be changed by your program code like variables.
You generally use this syntax when creating
constants that represent numbers, strings, or expressions.
Constant
#define CNAME value
OR
#define CNAME (expression)
CNAME
The name of the constant. Most C programmers
define their constant names in uppercase, but it is not a requirement of the C Language.
value
The value of the constant.
expression
An expression whose value is assigned to the
constant. The expression must be enclosed in parentheses if it contains
operators.
Note: Do NOT put a semicolon character at the end of
#define statements. This is a common mistake.
Number
#define directive to define a numeric constant:
Example: The constant named AGE would contain
the value of 10.
#define AGE 10
String
#define directive to define a string constant.
Example: The constant called NAME would contain
the value of "C Programming".
#define NAME "C Programming"
Expression
Example: The #define directive to define a
constant using an expression.
#define GRADE (20 / 2)
Example 1:
Example 2:
Example 3:
Output:
Example 4:
Output:
Example 5:
Output:
Function Like Macro:
The function-like macro looks like the function
call. For example:
#define MIN(a,b) ((a)<(b)?(a):(b))
Here, MIN is the macro name.
Example :
Output:
Predefined Macros in C:
|
Sr.No. |
Macro
& Description |
|
1 |
__DATE__ The current date as a character literal
in "MMM DD YYYY" format. |
|
2 |
__TIME__ The current time as a a character literal in "HH:MM: SS" format. |
|
3 |
__FILE__ This contains the current filename as a
string literal. |
|
4 |
__LINE__ This contains the
current line number as a decimal constant. |
|
5 |
__STDC__ Defined as 1 when the compiler complies
with the ANSI standard. |
Dr. Arpana Chaturvedi
Asst Prof (JIMS-BCA)
Department of Information Technology and Software Development










👍Informative
ReplyDeleteNice
ReplyDelete