SECOND EDITION
THE
BRIAN W KERNIGHAN
DENNIS M. RITCHIE
PRENTICE HALL SOFTWARE SERIES
THE
c
PROGRAMMING
LANGUAGE
Second Edition
THE
c
PROGRAMMING
LANGUAGE
Second Edition
Brian W. Kernighan • Dennis M. Ritchie
AT&T Bell Laboratories
Murray Hill, New Jersey
PRENTICE HALL, Englewood Cliffs, New Jersey 07632
Ubrary of Congress Cataloging-in-Publication Data
Kernighan, Brian W.
The C programming language.
Includes index.
1. C (Computer program language)
II. Title.
005.13'3
Dennis M.
QA76.73.C15K47 1988
ISBN 0-13-110370-9
ISBN 0-13-110362-8(pbk.)
I. Ritchie,
88-5934
Copyright e 1988, 1978 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 the publisher.
Printed in the United States of America. Published simultaneouslyin Canada.
UNIX is a registered trademark of AT&T.
This book was typeset (pic Itbll eqnItroff
-ms) in Times Roman and Courier by
the authors, using an Autologic APS-5 phototypesetter and a DEC VAX 8550 running
the 9th Edition of the UNIX. operating system.
Prentice Hall Software Series
Brian Kernighan, Advisor
Printed in the United States of America
10 9 8 7
ISBN
ISBN
0-13-110362-8
0-l3-110370-9
{PBK}
Prentice-Hall International (UK) Limited, London
Prentice-Hall of Australia Pty. Limited, Sydney
Prentice-Hall Canada Inc., Toronto
Prentice-Hall Hispanoamericana, S.A. , Mexico
Prentice-Hall of India Private Limited, New Delhi
Prentice-Hall of Japan, Inc., Tokyo
Simon & Schuster Asia Pte. Ltd., Singapore
Editora Prentice-Hall do Brasil, Ltda., Rio de Janeiro
Preface
Preface to the First Edition
Introduction
Chapter
I. A Tutorial Introduction
1.1 Getting Started
1.2 Variables and Arithmetic Expressions
1.3 The For Statement
1.4 Symbolic Constants
1.5 Character
1.6 Arrays
1.7 Functions
1.8 Arguments-Call
1.9 Character Arrays
1.10 External Variables and Scope
Input and Output
by Value
Chapter 2. Types, Operators, and Expressions
2.1 Variable Names
2.2 Data Types and Sizes
2.3 Constants
2.4 Declarations
2.5 Arithmetic Operators
2.6 Relational and Logical Operators
2.7 Type Conversions
2.8
2.9 Bitwise Operators
2.10 Assignment Operators and Expressions
2.11 Conditional Expressions
2.12 Precedence and Order of Evaluation
Increment and Decrement Operators
Chapter 3. Control Flow
3.1 Statements and Blocks
3.2
If-Else
,
Contents
ix
xi
I
5
5
8
13
14
15
22
24
27
28
31
35
35
36
37
40
41
41
42
46
48
50
51
52
55
55
55
vi
THE C PROGRAMMING LANGUAGE
CONTENTS
3.3 Else-If
3.4 Switch
3.5 Loops- While and For
3.6 Loops- Do-while
3.7 Break and Continue
3.8 Goto and Labels
Chapter 4. Functionsand Program Structure
4.1 Basicsof Functions
4.2 Functions Returning Non-integers
4.3 External Variables
4.4 Scope Rules
4.5 Header Files
4.6 Static Variables
4.7 Register Variables
4.8 BlockStructure
4.9
4.10 Recursion
4.11 The C Preprocessor
Chapter 5. Pointers and Arrays
Initialization
5.1 Pointers and Addresses
5.2 Pointers and Function Arguments
5.3 Pointers and Arrays
5.4 Address Arithmetic
5.5 Character Pointers and Functions
5.6 Pointer Arrays; Pointers to Pointers
5.7 Multi-dimensionalArrays
5.8
5.9 Pointers vs. Multi-dimensionalArrays
5.10 Command-lineArguments
5.11 Pointers to Functions
5.12 Complicated Declarations
Initialization of Pointer Arrays
Chapter 6. Structures
6.1 Basicsof Structures
6.2 Structures and Functions
6.3 Arrays of Structures
6.4 Pointers to Structures
6.5 Self-referential Structures
6.6 Table Lookup
6.7 Typedef
6.8 Unions
6.9 Bit-fields
Chapter 7. Input and Output
7.1 Standard Input and Output
7.2 Formatted Output-Printf
57
58
60
63
64
65
67
67
71
73
80
81
83
83
84
85
86
88
93
93
95
97
100
104
107
110
113
113
114
118
122
127
127
129
132
136
139
143
146
147
149
151
151
153