|
Electronics Circuits &
Tutorials |
|
|
|
|
|
|
Home >
Electronics Tutorials >
C Language
Tutorial > Preprocessing Directives - #define, #undef, #ifdef, #ifndef |
C Language Programming Library
Reference Guide
|
Preprocessing Directives
- #define, #undef, #ifdef, #ifndef
|
|
The preprocessing directives #define and
#undef allow the definition of identifiers
which hold a certain value. These identifiers can simply be
constants or a macro function. The directives #ifdef
and #ifndef allow conditional compiling of
certain lines of code based on whether or not an identifier has
been defined.
Syntax:
#define identifier replacement-code
#undef identifier
#ifdef identifier
#else or #elif
#endif
#ifndef identifier
#else or #elif
#endif
#ifdef identifier is the same is
#if defined( identifier) .
#ifndef identifier is the same as
#if !defined( identifier) .
An identifier defined with #define is
available anywhere in the source code until a #undef
is reached.
A function macro can be defined with #define
in the following manner:
#define identifier( parameter-list)
( replacement-text)
The values in the parameter-list are replaced in the
replacement-text.
Examples:
#define PI 3.141
printf("%f",PI);
#define DEBUG
#ifdef DEBUG
printf("This is a debug message.");
#endif
#define QUICK(x) printf("%s\n",x);
QUICK("Hi!")
#define ADD(x, y) x + y
z=3 * ADD(5,6)
This evaluates to 21 due to the fact that multiplication takes
precedence over addition.
#define ADD(x,y) (x + y)
z=3 * ADD(5,6)
This evaluates to 33 due to the fact that the summation is
encapsulated in parenthesis which takes precedence over
multiplication. |
|
|
Note: To report broken links or to submit your projects,
tutorials please email to Webmaster |
|
|
|
|
Copyright ©
1999-2020
www.hobbyprojects.com
(All rights reserved) |
|