How to program/test for portability?

Doug Gwyn gwyn at smoke.BRL.MIL
Thu Apr 6 07:29:55 AEST 1989


In article <439 at warlock.UUCP> gregb at dowjone.UUCP (Gregory S. Baber) writes:
>Given that portable C code is a desire, how does someone who has only
>had exposure to one "flavor" of C compiler (cc, gcc, Microsoft C 5.1, etc.)
>decide whether his or her code is portable, aside from actually trying to
>compile it on the target machine? How do you recognize code as being
>portable or not? Are there tests that you can perform on your code to test
>for portablility? Books on portable C? Gurus willing to look at your code
>a pass judgement as to its degree of portability? (1/2 :-)  Thanks, gregb

The most important step when writing the code is to use a standard C
language specification, not your particular vendor's C manual, as your
reference.  Originally that meant K&R (1st Edition) Appendix A, now it
is starting to mean the proposed ANSI C standard, for which K&R 2nd
Edition is a pretty good guide (so long as you don't have to deal with
"internationalization" issues).  The idea is to pay attention to what
the language reference guarantees about C, and not to use additional
knowledge about the particular way C is implemented in your environment.

There are several books about portable C programming.  Plum Hall Inc.
(1 Spruce Av., Cardiff NJ) specializes in this, and Lapin et al. of
Rabbit Software wrote another such book for which I've heard favorable
reviews.

UNIX systems generally provide a "lint" utility that examines your C
code for possible portability problems (among other things).  If you
have access to "lint" you really should learn how to use it.



More information about the Comp.lang.c mailing list