summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SAWTELL/c-lesson.6
blob: 67f8852334f7d9ba15bb67bfd76618b44e5ca2a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331

                                 Lesson 5.

                      The Pre-processor and Header Files.

The pre-processor is activated by a '#' character in column one of the source
code. There are several statements vis:

#include

#define
#undef

#if
#else
#endif

#ifdef
#ifndef

#pragma

  #include.

  In the programming examples presented in the previous lessons you will
probably have noticed that there is this statement:

#include <stdio.h>

right at the start of the program text. This statement tells the pre-processor
to include the named file in the your program text. As far as the compiler is
concerned this text appears just as if you had typed it yourself!

  This is one of the more useful facilities provided by the 'C' language.
The #include statement is frequently combined with the #if construct.
In this program fragment the file "true.h" is included in your program
if the pre-processor symbol FLAG is true, and "false.h" included if FLAG
is false.

#if ( FLAG )
# include "true.h"
#else
# include "false.h"
#endif

This mechanism has many uses, one of which is to provide
portability between all the 57,000 slightly different versions of unix and also
other operating systems. Another use is to be able to alter the way in which
your program behaves according to the preference of the user.

Of course, you will be asking the question "Where is the file stored?".
Well, if the filename is delimited by the "<" and ">" characters as in the
example above the file comes from the /usr/include directory, but if the name
of the file is delimited by quotes then the file is to be found in your current
working directory. (This is not quite the whole truth as 'C' compilers allow
you to extend the search path for the include files using command line option
switches. - See your compiler manual for the whole story. )

So, I would like to suggest that you to have a look around the /usr/include
directory and its /sys sub-directory. You should use either your editor
in 'view' mode or the pg utility. This will ensure that you can't have an
accident and alter one of the files by mistake if you are slightly silly
and just happen to be logged on as the super-user.

A typical file to examine is usr/include/time.h.

It's quite small so here it is.

/*  Copyright (c) 1984 AT&T  */
/*    All Rights Reserved    */

/*  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T  */
/*  The copyright notice above does not evidence any     */
/*  actual or intended publication of such source code.  */

#ident  "@(#)/usr/include/time.h.sl 1.5 4.2 04/20/87 18195 AT&T-SF"
/*  3.0 SID #  1.2  */
struct  tm {  /* see ctime(3) */
  int  tm_sec;
  int  tm_min;
  int  tm_hour;
  int  tm_mday;
  int  tm_mon;
  int  tm_year;
  int  tm_wday;
  int  tm_yday;
  int  tm_isdst;
};
extern struct tm *gmtime(), *localtime();
extern char *ctime(), *asctime();
int  cftime(),  ascftime();
extern void tzset();
extern long timezone, altzone;
extern int daylight;
extern char *tzname[];

  As you can see ( forgetting about the comments and #ident ) there are three
different uses for the file.

  a) The definition of data structures and types.
  b) The declaration of functions which use the data structures.
  c) The declaration of of external data objects.

  These lines of code are all you need in your program in order to be able to
use, in this case, the library routine to access the clock in the computer,
but of course the paradigm applies to all programs which are created by one
programmer and used by another member of the programming team.  Note that, by
proxy, or whatever, the author of the library routines has in effect become
a member of your programming team.

  You might care to write a program or two which use this header file,
and for those who are motivated it might be an idea to re-implement localtime
so that it understands Summer Time in the Southern Hemisphere. (!)

Using another totally trivial example in order to get the idea across please
examine the hello world program printed immediately below.

/* ------------------------------------------------------------ */

#ident "@(#) hw_uc.h UPPER CASE version."

#define HELLO_MESSAGE "HELLO WORLD...\n";

/* ------------------------------------------------------------ */

#ident "@(#) Hello World"

#include <stdio.h>
#include HW_H

#if !defined( HELLO_MESSAGE )
# error "You have forgotten to define the header file name."
#endif

char *format = "%s",
     *hello = HELLO_MESSAGE;

main()
{
  printf ( format, hello );
  }

/* ------------------------------------------------------------ */

You will no doubt notice that the symbol HW_H is used instead of a header file
name. This gives us the ability to force the inclusion of any file we wish by
defining the symbol HW_H to be the desired file name. It can be done like this:

cc -DHW_H="\"hw_uc.h\"" hello.c

The compiler output is placed, by default, in the file a.out, so to execute it
issue the command:

a.out

Which, fairly obviously, produces the output:

HELLO WORLD...

As we are going to generate another version of the program we had better move
the executable image file to another file name:

mv a.out hello_uc

Now to produce the other version issue the command line:

cc -DHW_H="\"hw_lc.h\"" hello.c; mv a.out hello_lc; hello_lc

Which compiles the other version of the hello.c program, using this version of
the include file:

/* ------------------------------------------------------------ */
#ident "@(#) hw_lc.h Lower Case version."

#define HELLO_MESSAGE "Hello World...\n";
/* ------------------------------------------------------------ */

and then moves the executable image to a different file and executes it.
Note that more than one command per line can be issued to the shell by
separating the commands with the ';' delimiting character.
Here - Surprise, Surprise - is the output of the second version.

Hello World...

I'd like to suggest that you use your editor to cut these example programs
and the shell file below out of the mail file and have a play with them.

/* ----------------------------------------- */

# @(#) Shell file to do the compilations.

cc -o hello_uc -DHW_H="\"hw_uc.h\"" hello.c
cc -o hello_lc -DHW_H="\"hw_lc.h\"" hello.c

/* ----------------------------------------- */


#define

  This statement allows you to set up macro definitions. The word immediately
after the #define, together with its arguments, is expanded in the program
text to the whole of the rest of the line.

#define min(a, b) ((a<b) ? a : b )

  Some things to note:

  1) There isn't a space between the last character of the symbol being defined
     and the opening parenthesis enclosing the arguments, and there MUST NOT BE
     one.

  2) The code into which the macro is expanded MUST always be enclosed in
     parentheses and for safety always use parentheses to get the arithmetic
     right.

  3) Never EVER define a macro, and use it with a side effect. e.g.

  c = min ( a++, b);                              /* DON'T _EVER_ DO THIS!!! */

     Do you think that the value of 'a' will get advanced after the
     macro is used? Well it WON'T. It gets incremented after the less
     than test and before the values get assigned! I have written a tiny
     program which uses the min macro above. Have a look at the output
     from the pre-processor. Lesson One told you how to do this.
     Now execute it and get an educative surprise!

/* ----------------------------------------- */

#include <stdio.h>
#define min(a, b) ((a<b) ? a : b )

main()
{ int a,b,c;

  a = 1;
  b = 2;
  c = min ( a++, b);                              /* DON'T _EVER_ DO THIS!!! */
  printf ( "a: %d, b: %d, c: %d\n", a, b, c );
  }

/* ----------------------------------------- */

  4) You can continue a macro on the next line by putting a \ ( back-slash )
     as THE VERY LAST character on the line. NOTHING, not even a space may
     follow, as your compiler just can't handle it. I spent far too long trying
     to find one of those really difficult bugs, and it turned out that this
     was the problem - spaces are transparent aren't they?

  5) Using macros is fast and convenient, but they do take up a lot of memory
     because the code is expanded and inserted into the output stream for
     every occurrence of the macro in your code. There is a trade-off between
     using a macro and a function.

  The symbol does not have to be the handle for a macro expansion, but can just
be equated to a single constant. This is done many times over in the header
files provided by the operating system vendor. Have a look in
/usr/include/sys/file.h for an example of this.

#undef

  Not surprisingly this preprocessor command removes a symbol WHICH IS BEING
  USED BY THE PRE-PROCESSOR - don't confuse it with compiler proper symbols.

  Note that the symbol can be a macro name, in which case the space
  used for the code expansion is made available for re-use.

#if ( FLAG )

      /* Code in here is sent on to the compiler if FLAG is true. */

#else

      /* Code in here is sent on to the compiler if FLAG is false. */

#endif

  When the pre-processor encounters one of these, the lines of code between the
#if and the corresponding #else or #endif are either skipped over or allowed to
proceed to the compilation phase depending on the truth or falsity of the
logical expression ( FLAG ). All the logical and boolean expressions available
as part of the 'C' language are available here. You are also allowed to say:

#if defined( FLAG )  or,
#if !defined( FLAG )


  The symbol FLAG may be an expression which reduces to a boolean value.

  A convention which is adhered to quite well is that all pre-processor
symbols are in UPPER_CASE so as to make them obvious.

#ifdef FLAG  or,
#ifndef FLAG

  These two statements are the old fashioned way of testing whether a symbol is
defined or not. They are absolutely the same as the previous example.

  There are two more pre-processor statements, namely the #pragma and
the "stringizing" operator. The #pragma is used to alter the way in which
the compiler works on a block of code, but it is completely implementation
dependant and you must refer to your compiler manual. I can't help as
they are all different. The "stringizing" operator is quite an advanced
technique and will be dealt with later on.

Copyright notice:-

(c) 1993 Christopher Sawtell.

I assert the right to be known as the author, and owner of the
intellectual property rights of all the files in this material,
except for the quoted examples which have their individual
copyright notices. Permission is granted for onward copying,
but not modification, of this course and its use for personal
study only, provided all the copyright notices are left in the
text and are printed in full on any subsequent paper reproduction.

--
 +----------------------------------------------------------------------+
 | NAME   Christopher Sawtell                                           |
 | SMAIL  215 Ollivier's Road, Linwood, Christchurch, 8001. New Zealand.|
 | EMAIL  chris@gerty.equinox.gen.nz                                    |
 | PHONE  +64-3-389-3200   ( gmt +13 - your discretion is requested )   |
 +----------------------------------------------------------------------+

--
 +----------------------------------------------------------------------+
 | NAME   Christopher Sawtell                                           |
 | SMAIL  215 Ollivier's Road, Linwood, Christchurch, 8001. New Zealand.|
 | EMAIL  chris@gerty.equinox.gen.nz                                    |
 | PHONE  +64-3-389-3200   ( gmt +13 - your discretion is requested )   |
 +----------------------------------------------------------------------+