The Preprocessor - PowerPoint PPT Presentation

About This Presentation
Title:

The Preprocessor

Description:

The Preprocessor – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 25
Provided by: thearbe
Category:

less

Transcript and Presenter's Notes

Title: The Preprocessor


1
The Preprocessor
Chapter 8 in ABC
2
define PI 3.14159 define C2 99792.458
/ speed of light / define EOF (-1) define
MAXINT 2147483647 define ITERS 50 /
number of iterations / define SIZE 250
/ array size / define EPS 1.0e-9 / a
numerical limit /
Software 1, TAU - 8.1
3
define EQ define do / blank / while (i
EQ 1) do ...... while (i i)
.....
Software 1, TAU - 8.2
4
define SQ(x) ((x) (x)) SQ(7 w) expands to
((7 w) (7 w)) SQ(SQ(p)) expands to
((((p) (p))) (((p) (p))))
Software 1, TAU - 8.2
5
define SQ(x) x x SQ(a b) expands to a b
a b -----------------------------------------
--------------- define SQ(x) (x) (x) 4 / SQ
(2) expands to 4 / (2) (2) --------------------
------------------------------------- define SQ
(x) ((x) (x)) SQ(7) expands to (x) ((x)
(x)) (7)
Software 1, TAU - 8.3
6
define SQ(x) ((x) (x)) / error
/ because if (x 2) x SQ(y) else
x
Software 1, TAU - 8.3
7
define min(x, y) (((x) lt (y)) ? (x) (y)) m
min(u, v) expands to m (((u) lt (v)) ? (u)
(v)) define min4(a, b, c, d) min(min(a,b),
min(c,d)) ----------------------------------------
------------------------- define SQ(x) ((x)
(x)) define CUBE(x) (SQ(x) (x)) define
F_POW(x) sqrt(sqrt(CUBE(x)))) / fractional
power 3/4 / -----------------------------------
------------------------------ undef
identifier cc -E file.c
Software 1, TAU - 8.4
8
/ cmp.c / int cmp(const void vp, const void
vq) const double p vp const double
q vq double diff p - q
return ((diff gt 0.0) ? ((diff gt 0.0) ? -1 0)
1) / fill.c / void fill_array(double a,
int n) int i srand(time(NULL))
/ seed rand() / for (i 0 i lt
n i) ai (rand() 1001) / 10.0
Software 1, TAU - 8.5
9
/ prn.c / void prn_array(when val, double a,
int n) int i printf("s\nss\n",
"---", ((val before) ? "Before "
"After "), "sorting") for (i 0 i lt n
i) if (i 6 0)
putchar('\n') printf("11.1f", ai)
putchar('\n')
Software 1, TAU - 8.6
10
/ main.c / include ltstdio.hgt include
ltstdlib.hgt include lttime.hgt define N 11
/ size of the array / enum
when before, after typedef enum when
when int cmp(const void vp, const void
vq) / compar. fct / void fill_array(double
a, int n) void prn_array(when val, double a,
int n) int main(void) double aN
fill_array(a, N) prn_array(before, a, N)
qsort(a, N, sizeof(double), cmp)
prn_array(after, a, N) return 0
Software 1, TAU - 8.7
11
Before sorting 1.5 17.0
99.5 45.3 52.6 66.3 3.4
70.2 23.4 57.4
6.4 After sorting 99.5 70.2
66.3 57.4 52.6 45.3
23.4 17.0 6.4 3.4 1.5
Software 1, TAU - 8.8
12
/ compare.c / include "sort.h" int
compare_fractional_part(const void vp, const
void vq) const float p vp, q vq
float x x fractional_part(p) -
fractional_part(q) return ((x lt 0.0) ? -1
(x 0.0) ? 0 1) int lexico(const void
vp, const void vq) const char p vp,
q vq return (p - q)
Software 1, TAU - 8.9
13
Software 1, TAU - 8.10
/ main.c / include "sort.h" int
main(void) char aM float bN
int i srand(time(NULL)) FILL(a, M ,
"char") PRINT(a, M, "-2c") qsort(a, M,
sizeof(char), lexico) PRINT(a, M, "-2c")
printf("---\n") FILL(b, N, "float")
PRINT(b, N, "-6.1f") qsort(b, N,
sizeof(float), compare_fractional_part)
PRINT(b, N, "-6.1f") return
0 --------------------------------------------
------------------- q m z r h l a j o e t b k w
l t z t v i e m h p f y b p s w a j a a b b e e
f h h i j j k l l m m o p p q r s t t t v w w y z
z --- 9.4 0.2 5.1 6.7 5.4 5.3 6.1
9.6 2.8 8.8 8.5 6.1 5.1 0.2 5.3
5.4 9.4 8.5 9.6 6.7 8.8 2.8
14
/ sort.h / include ltstdio.hgt include
ltstdlib.hgt include ltstring.hgt include
lttime.hgt define M 32
/ size of a / define N 11
/ size of b / define
fractional_part(x) (x - (int) x) define
random_char() (rand() 26 'a') define
random_float() (rand() 100 /
10.0) define FILL(array, sz, type) \
if (strcmp(type, "char") 0) \ for (i
0 i lt sz i) \ arrayi
random_char() \ else
\ for (i 0 i lt sz i) \
arrayi random_float() define
PRINT(array, sz, cntrl_string) \ for (i 0
i lt sz i) \
printf(cntrl_string, arrayi) \
putchar('\n') int compare_fractional_part(const
void , const void ) int lexico(const void
, const void )
Software 1, TAU - 8.11
15
  • Macros in stdio.h
  • define getchar() getc(stdin)
  • define putchar(c) putc((c),stdout)

16
  • Macros ctype.h (takes int as argument, return
    int)
  • isalpha(c) c is a letter
  • isupper(c) c is an uppercase letter
  • islower(c) c is a lowercase letter
  • isdigit(c) c is a digit
  • isalnum(c) c is a letter or a digit
  • isxdigit(c) c is a hexadecimal digit
  • isspace(c) c is a white space character
  • ispunct(c) c is a punctuation character
  • isprint(c) c is a printable character
  • isgraph(c) c is printable, but not a space
  • iscntrl(c) c is a control character
  • isascii(c) c is an ASCII code

17
  • Also in ctype.h
  • Functions in ctype.h (take int, return int)
  • toupper(c) corresponding uppercase value for c
  • tolower(c) corresponding lowercase value for c
  • Macro in ctype.h (takes int, returns int)
  • toascii(c) corresponding ASCII value

18
CONDITIONAL COMPILATION
if constant_integral_expression ifdef
indentifier ifndef indentifier endif ---------
--------------------------------------------------
------ if defined(HP9000) defined(SUN4)
!defined(VAX) ...... / machine_dependent code
/ endif ---------------------------------------
-------------------------- define DEBUG 1 if
DEBUG printf("debug a d\n",
a) endif --------------------------------------
--------------------------- include
"everything.h" undef PIE define PIE "I
like apple." .......
Software 1, TAU - 8.12
19
statement if 0 more statements endif and
still more statements ---------------------------
--------------- elif constant_interal_expression
else endif
Software 1, TAU - 8.13
20
  • Fives predefined macros (cant be undefined)
  • __DATE__ A string containing the current date
  • __FILE__ A string containing the file name
  • __LINE__ An integer representing the current
  • line number
  • __STDC__ If the implementation follows ANSI C
  • then the value is a nonzero integer
  • __TIME__ A string containing the current time

21
define message_for(a, b) \ printf(a " and "
b" We love you!\n") int main(void) message_fo
r(Carole, Debra) return 0 After
preprocessor int main(void) printf("Carole"
"and " Debra" " We love you\n") return 0
Software 1, TAU - 8.14
22
define X(i)x i X(1) X(2) X(3) is
expanded to x1 x2 x3 ----------------------
----------------------------- if A_SIZE lt
B_SIZE error "Incompatible sizes" endif
Software 1, TAU - 8.15
23
  • include ltassert.hgt
  • Void f(char p, int n)
  • assert (p ! NULL)
  • assert (n gt 0 n lt 7)

24
/ assert.h / include ltstdio.hgt include
ltstdlib.hgt / for abort()
/ if defined(NDEBUG) define
assert(ignore) ((void) 0) / ignore it
/ else define assert(expr)
\ if (!(expr)) \
printf("\nss\nss\nsd\n\n", \
"Assertion failed ", expr, \
"in file ", __FILE__, \ "at
line ", __LINE__) \ abort()
\ endif
Software 1, TAU - 8.16
Write a Comment
User Comments (0)
About PowerShow.com