Elements of Programming Style
Total Page:16
File Type:pdf, Size:1020Kb
THE ELEMENTS OF PROGRAMMING STYLE SECOND EDITION Kernighan and Plauger THE ELEMENTS OF PROGRAMMING STYLE Second Edition Brian W. Kernighan Bell laboratories Murray Hill, New Jersey P. J. Plauger Yourdon, Inc. New York, New York McGRAW-HILL BOOK COMPANY New York St. Lou1; San Franc1;co Auckland Bogota Du;seldorf London Madnd Mexico Montreal New Delhi Panama Pan' Sao Paulo Singapore Sydney Tokyo Toronto Library of Congress Cataloging in Publication Data Kernighan, Brian W. The elements of programming style. Bibliography: p. Includes index. I. Electronic digital computers-Programming. I. Plauger, P.J., date joint author. II. Title. QA 76.6.K47 1978 001.6'42 78-3498 ISBN 0-07-034207-5 The Elements of Programming Style Copyright © 1978, 1974 by Bell Telephone Laboratories, Incorporated. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopy ing, recording, or otherwise, without the prior written permission of Bell Laboratories. Printed in the United States of America. 12 13 14 15 DODO 8 9 This book was set in Times Roman and Courier 12 by the authors, using a Graphic Sys tems phototypesetter driven by a PDP-11/70 running under the UNIX operating system. UNIX is a Trademark of Bell Laboratories. We are deeply indebted to the following authors and publishers for their kind permission to reproduce excerpts from the following copyrighted material: R V Andree, J P Andree, and D D Andree, Computer Programmmg Techniques, A nalys1s, and Mathematics. Copyright © 1973 by R V Andree By permission of Prentice-Hall, Inc F Bates and M L Douglas, Programmmg Language/One with Structured Programming (Thtrd Edmon) Copyright © 1975 by Prentice-Hall, Inc Reprinted by permission C R Bauer and A P Peluso, Basic Fortran IV with Waifor & Wa(/iv. Copyright © 1974 by Addison-Wesley Publishing Com pany, Inc By permission C R Bauer, A P Peluso, and D A Gomberg, Baste PL/I Programming. Copyright © 1968 by Addison-Wesley Publishing Company, Inc By permission M Bohl and A Walter, Introduction to PL/I Programming and PL/C. Copyright © 1973 by Science Research Associates, Inc Reprinted by permission of the publisher V J Calderbank, A Course on Programming m Fortran IV. Copyright © 1969 by Chapman and Hall, Ltd By permission Paul M Chirlian, lntroductt0n to Fortran IV Copyright © 1973 by Academic Press By permission Frank J Clark, lntroduc11on to PL/I Programming. Copyright© 1971 by Allyn and Bacon, Inc By permission Computerworld Copyright© 1972 by Computerworld, Newton, Mass 02160 By permission Datoma11on9 Copyright© 1972, 1973 by Technical Publishing Company, Greenwich, Connecticut 06830 Reprinted with per mission D F DeTar, Prmetples of Fortran Programming. Copyright © 1972 by W A Benjamin, Inc, Menlo Park, California By per mission of the publisher H Dinter, lntroductt0n to Computing. Copyright © 1973, Heinz Dinter By permission of The Macmillan Company, New York D Dmitry and T Mott, Jr, lntroduc11on to Fortran IV Programming Copyright © Holt, Rinehart and Winston, Inc, 1966 By permission V T Dock, Fortran IV Programming, Copyright © 1972 by Reston Publishing Company, Inc By permission W S Dorn, G G Bitter, and D L Hector, Computer Appltca11ons for Calculus Copyright © Prindle, Weber & Schmidt, Inc, 1972 By permission W S Dorn and D D McCracken, Numerical Methods with Fortran IV Case Studtes. Copyright © 1972 by John Wiley & Sons, Inc By permission L E Edwards, PL// for Busmess Applica11ons. Copyright© 1973 by Reston Publishing Company, Inc By permission M V Farina, Fortran IV Seif-Taught. Copyright© Prentice-Hall, Inc, 1966 By permission B S Gottfried, Programming with Fortran IV Copyright© 1972 by Quantum Publishers, Inc By permission Gabriel F Groner, PL// Programming 1n Technological Applteatt0ns. Copyright © 1971 by John Wiley and Sons, Inc Reprinted by permission of the publisher N Haag, Comprehensive Standard Fortran Programmmg. Copyright© Hayden Book Company, Inc, 1969 By permission K Hughes, PL/I Programming. Copyright © 1973 by John Wiley & Sons, Inc By permission K Hughes and J I Michtom, A Structured Approach to Programming Copyright © 1977 by Prentice-Hall, Inc Reprinted by permission R J Kochenburger and C J Turcio, Introduction to PL/I and PL/C Programming - Instructor's Guide Copyright © 1974 by John Wiley & Sons, Inc By permission C B Kreitzberg and B Shneiderman, The Elements of Fortran Style. Copyright © 1972 by Harcourt Brace Jovanovich, Inc By permission J L Kuester and J H Mize, Op11m1za11on Techniques with Fortran Copyright © 1973 by McGraw-Hill, Inc By permission S S Kuo, Computer Applica11ons of Numer1cal Methods. Copyright ©Addison-Wesley Publishing Company, 1972 By permis sion H L Ledgard, Programming Proverbs Copyright © 1975 by Hayden Book Company By permission R S Ledley, Fortran IV Programming. Copyright© McGraw-Hill, Inc, 1966 By permission G 0 Manifold, Ca/cu/atmg W1th Fortran Copyright © 1972 by Charles E Merrill Publishing Co , Inc By permission W A Manning and R S Gamero, A Fortran IV Problem Solver. Copyright © McGraw-Hill, Inc, 1970 By permission E Marxer and D Hartford, Elements of Computer Programming: Fortran. Copyright© 1973 Published by Delmar Publishers, a division of Litton Educational Publishing, Inc By permission D D McCracken, A Guide to Fortran IV Programming Copyright © 1965 by John Wiley and Sons, Inc Reprinted by permis sion of the publisher v vi THE ELEMENTS OF PROGRAMMING STYLE D D McCracken, A Guide to Fortran IV Programming, Second Edmon Copyright © 1972 by John Wiley and Sons, Inc Re printed by permission of the publisher C L McGowan and J R Kelly, Top-Down Structured Programming Techniques Copyright © 1975 by Litton Educational Pub lishing, Inc Reprinted by permission of Van Nostrand Reinhold Company, a division of Litton Educational Publishing, Inc L P Meissner, The Science ofCompullng. Copyright© 1974 by Wadsworth Publishing Company, Inc By permission H Mullish, Modern Programming: Fortran IV Copyright © 1968 by John Wiley & Sons, Inc By permission Paul W Murrill and Cecil L Smith, Fortran IV Programming for Engineers and Scientists, Second Edmon Ontext) Copyright © 1973 by Harper & Row, Publishers, Inc Used by permission of Harper & Row, Publishers Paul W Murrill and Cecil L Smith, PL/I Programming Ontext) Copyright© 1973 by Harper & Row, Publishers, Inc Used by permission of Harper & Row, Publishers R L Nolan, Fortran IV Compullng and App/1ca11ons Copyright © Addison-Wesley Publishing Company, 1971 By permission E I Organick and L P Meissner, For1ran IV (Second Edmon) Copyright © 1974 by Addison-Wesley Publishing Company, Inc By permission S V Pollack, A Guide to Fortran IV. Copyright © Columbia University Press, 1965 By permission Seymour V Pollack and Theodor D Sterling, A Guide to PL/I Copyright © 1969 by Holt, Rinehart and Winston Reprinted by permission of Holt, Rinehart and Winston Seymour V Pollack and Theodor D Sterling, A Guide 10 Pl// (Second Ed1t1onJ Copyright © 1976 by Holt, Rinehart and Win•• ton Reprinted by permission of Holt, Rinehart and Winston Seymour V Pollack and Theodor D Sterling, Essen11als of PL/I Copyright © 1974 by Holt, Rinehart and Winston Reprinted by permission of Holt, Rinehart and Winston A Ralston, Fortran IV Programming, A Concise Expos111on Copyright© McGraw-Hill, Inc, 1971 By permission J K Rice and J R Rice, lntroduc11on to Computer Science Copyright © 1969 by Holt, Rinehart and Winston, Inc Reprinted by permission of Holt, Rinehart and Winston, Inc G L Richardson and S J Birkin, Program Solving Using Pl/C Copyright © 1975 by John Wiley ~· Sons, Inc By permission J S Roper, Pl/I in Easy Stages Copyright© 1973 by Paul Elek (Scientific Books) Ltd By permis-.;m W P Rule, Fortran IV Programming Copyright © 1968 by W P Rule Prindle, Weber & Schmidt, Inc By permission School Mathematics Study Group, Algortthms, Computa11on and Mathema11cs, Fortran Supplement. Studen1 Tex1 (Revised Edt110n) Copyright ©Stanford University, 1966 By permission No endorsement by SMSG is implied G L Scott and J Scott, Pl/I. A Se!f-lnstruc11onal Manual Copyright © 1969 by Dickenson Publishing Company By permis sion R C Scott and N E Sondak, Pl// for Programmers Copyright ©Addison-Wesley Publishing Company, 1970 By permission Donald D Spencer, Programming with USA S1andard Fortran and Fortran IV Copyright© 1969 by Xerox Corporation Used by permission of Ginn and Company (Xerox Corporation) Donald D Spencer, Computers and Programming Guide For Engineers Copyright © 1973 by Howard W Sams & Co. Inc By permission R C Sprowls, lntroduc11on to Pl/I Programming Copyright © 1969 by Harper & Row, Publishers. Inc By permission R A Stern and N B Stern. Principles of Data Processing Copyright © 1973 by John Wiley & Sons. Inc By permission F Stuart, Fortran Programming Copyright © 1969 by Fredric Stuart Reprinted by permission of John Wiley and Sons, Inc A Vazsonyi, Problem Solving by D1g1tal Computers wl/h Pl/I Programming Copyright © Prentice-Hall, Inc. 1970 By permis sion T M Walker and W W Cotterman, An Introduction to Computer Science and A/gortthm1c Processes Copyright© 1971 by Allyn and Bacon. Inc Used by permission G M Weinberg, Pl/I Programming· A Manual of Style Copyright© McGraw-Hill, Inc. 1970 By permission CONTENTS Preface to the Second Edition ix Preface to the First Edition xi 1. Introduction 1 2. Expression 9 3. Control Structure 31 4. Program Structure 59 5. Input and Output 83 6. Common Blunders 101 7. Efficiency and Instrumentation 123 8. Documentation 141 Epilogue 155 Supplementary Reading 157 Summary of Rules 159 Index 163 vii PREFACE to the Second Edition The practice of computer programming has changed since The Elements of Pro gramming Style first appeared. Programming style has become a legitimate topic of discussion. After years of producing "write-only code," students, teachers, and computing professionals now recognize the importance of readable programs. There has also been a widespread acceptance of structured programming as a valuable cod ing discipline, and a growing recognition that program design is an important phase, too often neglected in the past.