View and vote on the article here: C Lesson 5- The Pre-processor and Header Files
C Lesson 5- The Pre-processor and Header Files| Category | | | Summary | | | Body |
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
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. <br.
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
#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
#define min(a, b) ((a
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 )
+----------------------------------------------------------------------+
|
|
This article was imported from zZine. (original author: hellbound)
There are no replies to this post yet.
|