Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 346 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
346
Dung lượng
9,83 MB
Nội dung
c: An Advanced Introduction Narain Gehani AT & T Bell Laboratories Murray Hill, New Jersey COMPUTER SCIENCE PRESS Copyright @ 1985 Bell Telephone Laboratories, Inc Printed in the United States of America All rights reserved No part of this book may be reproduced in any form including photostat, microfilm, and xerography, and not in information storage and retrieval systems, without permission in writing from the publisher, except by a reviewer who may quote brief passages in a review or as provided in the Copyright Act of 1976 Computer Science Press 11 Taft Court Rockville, Maryland 20850 Printing Year 8988878685 Library of Congress Cataloging in Publication Data Gehani, Narain, 1947C : an advanced introduction Bibliography: p Includes index C (Computer program language) I Title QA76.73.CL5G44 1984 001.64'24 ISBN 0-88175-053-0 84-12145 To my friends and colleagues at AT&T Bell Labs Contents Preface Xl Acknowledgment xv Chapter I Introduction and Basics I A Sample C Program Basic~ Constants , H Probl!rms I I 14 15 Chapter Types and Variables Fundamental Types Derived Types Type Declarations Definitions and Declarations Type Conversions Problems 17 18 23 43 45 58 62 Chapter Operators and Expressions Operators Expressions Problems 65 65 78 79 Chapter Control Flow Expressions and'Statements Null Stat~ment Compound or Block Statement Assignment Statement If Statement Switch Statement Loops The Break Statement The Continue Statement 81 81 81 82 82 83 85 87 89 89 vii Contents Vlll 10 11 12 13 14 The Function Call Statement The Return Statement The Goto Statement Statement Labels Problems 90 91 91 91 91 Chapter Functions and Complete Programs Functions Lexical Scope of Identifiers Input/Output Redirection of Input and Output Main Programs Examples Problems 93 93 104 106 110 112 114 143 Chapter Independent Compilation and pata Abstraction Scope of External Defi~itions and Declarations Independent Compilation Abstract Data Types and Information Hiding Classes Examples Problems 147 149 153 159 169 Exceptions The Different Signals Setting Up Signal/Exception Generating/Sending Signals Examples Problems 171 172 173 178 179 187 Chapter Chapter Handlers Concurrent Programming Concurrent Programming in C under the UNIX Operating System Creating a Process using the fork Library Function The execl Library Function for Overlaying Processes Pipes-A Synchronous Communication Mechanism Examples Problems 148 149 189 190 190 192 193 198 206 Contents IX Chapter The Chapter 10 One final Example A Simple Query Database Problems Appendix A 209 209 214 214 218 218 219 C Preprocessor Macro Definition' and Invocation File Inclusion Conditional Compilation Concluding Remarks Example Problems 223 223 230 Example Some Library Functions UNIX System Calls and The Standard Library i be, Math Library libm 233 C 233 282 Some C Tools 1in t: The C Program Checker c c: The C Compiler db: The C Beautifier make: Prograrrl Group Maintainer 283 283 284 Appendix C ANSI Standard 295 Appendix D ASCII Table Appendix E Implementation-Dependent Appendix Annotated Index B Bibliography C 285 287 297 Characteristics 299 301 311 Preface Introduction The C programming language was designed and implemented by Dennis Ritchie in 1972 at AT&T Bell Laboratories Despite a late start, the popularity of C has been increasing rapidly C compilers are now available for many machines and the list of available C compilers is growing fast [Halfant 1983, Kern 1983, Phraner 1983] Two important reasons for this increasing popularity are the Flexibility of the C Language: It can be used for a wide variety of application domains with relative ease Popularity of the UNIX™ System: Most of the software in the UNIX System is written in C and C is the primary language supported by the UNIX system Ever since its design, C has been evolving, particularly in the areas of type checking and mechanisms to improve program portability For example, a project to transport the UNIX operating system to an Interdata 8/32 computer led to several additions to C, notably, unions, casts and type definitions [Bourne 82] More recently, an effort has been under way to incorporate data abstraction facilities in C [Stroustrup 1983]; data abstraction is an area in which the current version of C has only limited facilities C is currently in the process of being adopted as an ANSI standard; it is likely that this process will result in further changes to C, several of which are under consideration ANSI standardization of C is scheduled for late 1985 I would have liked to base this book on the ANSI version of C, which is currently in preliminary form However, I decided against doing this because the ANSI version of C is likely to undergo many changes before it is adopted as a standard and because existing compilers not implement this version I will discuss C as it is described in The C Programming Language-Reference Manual [Ritchie 1980], which is the latest version of the C reference manual The anticipated differences between this version of C and the preliminary n< UNIX is a trademark of AT&T Bell Laboratories Xl xu Preface ANSI version are summarized in Appendix C C is a flexible programming language that gives a great deal of freedom to the programmer This freedom is the source of much expressive power and one of the main strengths of C, making it powerful, versatile and easy to use in a variety of application areas However, undisciplined use of this freedom can lead to errors Consequently, in this book I shall, without loss of generality, restrict discussion to a disciplined use of C For example, I shall discuss only a restricted version of the switch statement in which the code for the different alternatives does not overlap Although overlapping alternatives can sometimes be used to advantage, such code can be hard to read, modify and maintain, and can be a potential source of errors Likewise, I shall not rely on default initial values for variables because only a subset of variables is initialized by default; I will explicitly initialize all variables in the example programs No C compiler will check and warn of all violations of the disciplined use of C advocated in this book However, many undisciplined uses will be detected by the C program checker lint [AT&T UNIX