The Preprocessor Chapter 8 in ABC

Download Report

Transcript The Preprocessor Chapter 8 in ABC

The Preprocessor
Chapter 8 in ABC
#define PI
#define C2
3.14159
99792.458
/* speed of light */
#define EOF
(-1)
#define MAXINT 2147483647
#define ITERS 50
#define SIZE 250
#define EPS
1.0e-9
/* number of iterations */
/* array size */
/* a numerical limit */
Software 1, TAU - 8.1
#define EQ ==
#define do
/* blank */
while (i EQ 1) do {
......
while (i == i) {
.....
Software 1, TAU - 8.2
#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
#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
#define
SQ(x)
((x) * (x));
/* error */
because:
if (x == 2)
x = SQ(y);
else
++x;
Software 1, TAU - 8.3
#define min(x, y) (((x) < (y)) ? (x) : (y))
m = min(u, v) expands to m = (((u) < (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
/* cmp.c */
int cmp(const void *vp, const void *vq)
{
const double
*p = vp;
const double
*q = vq;
double
diff = *p - *q;
return ((diff >= 0.0) ? ((diff > 0.0) ? -1 : 0) : +1);
}
/* fill.c */
void fill_array(double *a, int n)
{
int
i;
srand(time(NULL));
for (i = 0; i < n; ++i)
a[i] = (rand() % 1001) / 10.0;
/* seed rand() */
}
Software 1, TAU - 8.5
/* prn.c */
void prn_array(when val, double *a, int n)
{
int
i;
printf("%s\n%s%s\n",
"---",
((val == before) ? "Before " : "After "),
"sorting:");
for (i = 0; i < n; ++i) {
if (i % 6 == 0)
putchar('\n');
printf("%11.1f", a[i]);
}
putchar('\n');
}
Software 1, TAU - 8.6
/* main.c */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define
N
11
/* size of the array */
enum when {before, after};
typedef
int
void
void
enum when
when;
cmp(const void *vp, const void *vq); /* compar. fct */
fill_array(double *a, int n);
prn_array(when val, double *a, int n);
int main(void)
{
double
a[N];
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
Before sorting:
1.5
3.4
17.0
70.2
99.5
23.4
45.3
57.4
52.6
6.4
66.3
70.2
17.0
66.3
6.4
57.4
3.4
52.6
1.5
45.3
After sorting:
99.5
23.4
Software 1, TAU - 8.8
/* 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 < 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
/* main.c */
Software 1, TAU - 8.10
#include "sort.h"
int main(void)
{
char
a[M];
float
b[N];
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
/* sort.h */
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<string.h>
<time.h>
#define
#define
M
N
32
11
#define
#define
#define
fractional_part(x)
random_char()
random_float()
/* size of a[] */
/* size of b[] */
(x - (int) x)
(rand() % 26 + 'a')
(rand() % 100 / 10.0)
#define
FILL(array, sz, type)
if (strcmp(type, "char") == 0)
for (i = 0; i < sz; ++i)
array[i] = random_char();
else
for (i = 0; i < sz; ++i)
array[i] = random_float()
\
\
\
\
\
\
#define
PRINT(array, sz, cntrl_string)
for (i = 0; i < sz; ++i)
printf(cntrl_string, array[i]);
putchar('\n')
int
int
\
\
\
compare_fractional_part(const void *, const void *);
lexico(const void *, const void *);
Software 1, TAU - 8.11
Macros in stdio.h :
#define getchar() getc(stdin)
#define putchar(c) putc((c),stdout)
Macros ctype.h (takes int as argument, return int):
isalpha(c)
isupper(c)
islower(c)
isdigit(c)
isalnum(c)
isxdigit(c)
isspace(c)
ispunct(c)
isprint(c)
isgraph(c)
iscntrl(c)
isascii(c)
c
c
c
c
c
c
c
c
c
c
c
c
is
is
is
is
is
is
is
is
is
is
is
is
a letter
an uppercase letter
a lowercase letter
a digit
a letter or a digit
a hexadecimal digit
a white space character
a punctuation character
a printable character
printable, but not a space
a control character
an ASCII code
Also in ctype.h:
Functions in ctype.h (take int, return int):
toupper(c)
tolower(c)
corresponding uppercase value for c
corresponding lowercase value for c
Macro in ctype.h (takes int, returns int) :
toascii(c)
corresponding ASCII value
#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
#define
.......
PIE
PIE "I like apple."
Software 1, TAU - 8.12
statement
#if 0
more statements
#endif
and still more statements
-----------------------------------------#elif constant_interal_expression
#else
#endif
Software 1, TAU - 8.13
Fives predefined macros (can’t be undefined) :
__DATE__
__FILE__
__LINE__
__STDC__
__TIME__
A string containing the current date
A string containing the file name
An integer representing the current
line number
If the implementation follows ANSI C
then the value is a nonzero integer
A string containing the current time
#define message_for(a, b) \
printf(#a " and " #b": We love you!\n")
int main(void)
{
message_for(Carole, Debra);
return 0;
}
After preprocessor:
int main(void)
{
printf("Carole" "and " Debra" ": We love you\n");
return 0;
}
Software 1, TAU - 8.14
#define X(i)x ## i
X(1) = X(2) = X(3); is expanded to x1 = x2 = x3;
---------------------------------------------------
#if A_SIZE < B_SIZE
#error "Incompatible sizes"
#endif
Software 1, TAU - 8.15
#include <assert.h>
Void f(char *p, int n)
{
…
assert (p != NULL);
assert (n > 0 && n < 7);
…
}
/* assert.h */
#include <stdio.h>
#include <stdlib.h>
/* for abort() */
#if defined(NDEBUG)
#define
assert(ignore)
((void) 0) /* ignore it */
#else
#define
assert(expr)
\
if (!(expr)) {
\
printf("\n%s%s\n%s%s\n%s%d\n\n", \
"Assertion failed: ", #expr,
\
"in file ", __FILE__,
\
"at line ", __LINE__);
\
abort();
\
}
#endif
Software 1, TAU - 8.16