CS157 Preprocessor
#define
directive allows for defining constants.
#define NAME value
NAME
following the
#define
will be replaced with value.
#define SIZE 20 #define BAD 10**10 int main() { int list[SIZE]; // this is ok num = BAD; // not ok, strange error about * SIZE = 10; // not ok, SIZE is replaced with 20, error BIG_INT = 10000000; // ok, but bad practice #define BIG_INT long int BIG_INT big_num; // ok list[0] = BIG_INT; // not ok, BIG_INT is replaced with long int return 0; }
#define
to declare constants like π;
that’s what const
is for.
#define
to replace elements of the C language,
like the long int example; that’s what typedef
is for.
#define
can be useful for controlling
conditional compilation of code.
The const keyword tells the compiler that the following variable declaration/initialization will remain constant throughout the program.
const double PI = 3.1419; PI = 3.0; // This won't work: it's const!
I realize that the concept of a constant variable is a bit of a contradiction. Oh, well!
#define
can also be used to define parameterized macros. For example,
#define SQR(x) ((x)*(x))
#define
preprocessor directive is
used to equate constants and operators to symbolic names
#define SALESTAX 0.05
#define
statement
#define
#define PI 3.14159 #define CIRCLE_AREA(x) (PI * (x) * (x)) area = CIRCLE_AREA(4);
Use parentheses. Without them, the macro
#define CIRCLE_AREA(x) PI * (x) * (x)
would cause
area = CIRCLE_AREA(c + 2);
to become
area = 3.14159 * c + 2 * c + 2;
Multiple arguments:
#define RECTANGLE_AREA(x, y) ((x) * (y)) rectArea = RECTANGLE_AREA(a+4, b+7);
becomes:
rectArea = ((a+4) * (b+7));
Making decisions at compile-time.
#define
can be used to create flags
#define DEBUG
DEBUG
. It’s defined, or it isn’t.
-D
option:
c11 -DDEBUG my_prog.c
if
statement:
#if defined(HP) #define compare stricmp #else #define compare strcasecmp #endif
compare(a,b)
as if it’s a real function.
#if
must end with #endif
#else
is optional
#ifdef
means #if defined(name)
,
#ifndef
means #if !defined(name)
The usual way to do it:
#ifdef HP #define compare stricmp #else #define compare strcasecmp #endif
#else
— equivalent of else
in an if
structure
#elif
— equivalent of else if
in an if
structure
/*
… */
around /* */
#if 0 the code that you don’t like #endif
#include <stdio.h> int main() { float friction, number; unsigned int zip_code; #ifndef DEBUG zip_code = 13285; #else zip_code = 00001; #endif friction = 0.04; number = (zip_code * friction) - 3.2; #ifdef DEBUG printf("friction: %f number %f\n",friction,number); #endif printf("The final number was %f\n", number); return 0; }
#include
previously for getting
access to standard library functions.
#include <file> #include "file"
#include
directive tells the preprocessor to
copy a file into the current file at the location of the #include
.
<file>
looks in official directories, often /usr/include
.
"file"
looks in the current directory
#undef
to undefine a macro name
declared in another file you may have included:
#include "everything.h" #undef PIE #define PIE "I like apple."
#undef
directive has no effect.
Modified: 2016-07-26T18:36 User: Guest Check: HTML CSSEdit History Source |
Apply to CSU |
Contact CSU |
Disclaimer |
Equal Opportunity Colorado State University, Fort Collins, CO 80523 USA © 2015 Colorado State University |