`
`The complete
`introduction
`in the
`standardized
`
`C language
`
`Sicp-by-step guide to
`complhng and Wl’lhllg
`programs
`
`Covers program looping, decision
`making, arrays, structures, pointers,
`E and more
`
`r:
`
`r
`,
`I?
`.n
`i‘ §AM§..
`I PuauswwarA
`
`E:
`
`.
`
`SAMSUNG-1007
`
`Page 1 of 559
`
`SAMSUNG-1007
`Page 1 of 559
`
`
`
`359%
`
`AUSTE‘J
`
`THE LIBRARY
`OF
`THE UNIVERSITY
`OF TEMS
`AT
`
`SAMSUNG-1007
`
`Page 2 of 559
`
`SAMSUNG-1007
`Page 2 of 559
`
`
`
`g.”fim‘xwflww”WWWWmmama-rantummmm‘mWWA-wm
`
`ANSI C
`
`4}
`é
`
`‘mnu-qmrumwmvdrwr‘wr
`
`Reference Card
`
`,mmflwmmn.w-n.w-ufiaum mmmvummmmmumammwrmwwmm:mm:”MW-u:
`
`_' 1 Variables/Identifiers
`l
`
`I
`
`l
`
`- Must be <= 31 characters in length.
`
`- Only the first six characters are
`Tecognlze‘i-
`
`May consist of uppercase/ lowercase let~
`t
`,
`‘c d' “ts, oranunderscore.
`ers numen
`1g1
`- The first letter must be a letter or under-
`SCOI‘E‘.
`
`-'
`
`7
`
`l Standard Program Template
`
`#include <stdio.h>
`
`1111: main (void)
`{
`I" program body t],
`return l2);
`
`a character string
`
`'
`
`. Sequences
`
`Is
`
`Equivalent
`To:
`
`In")IH-Jrnni—u—Iit
`
`.
`
`I
`
`an integer
`followed by u or U
`
`Indicates:
`
`an unsigned
`integer
`
`an integer followed by 1 or L
`
`a long integer
`
`an integer followed by
`u or U and 1 or L
`
`a floating point constant
`_ £0110wed by f or F
`
`I
`
`I
`
`a floating point constant
`followed by ‘f or F and 1 or L
`
`\ followed by three octal
`digits (0-7)
`
`\x followed by three
`hexadecimal digits (0—9, A—F)
`
`a string of characters
`enclosed in quotation marks
`
`an unsigned
`long integer
`float
`
`a long double
`
`an octal number
`
`a hexadecimal
`number
`
`SAMSUNG-1007
`
`Page 3 of 559
`
`SAMSUNG-1007
`Page 3 of 559
`
`
`
` And the result will be
`
`long double
`the other operand is
`_ either operand is a long double
`converted to long double
`
`double
`the other operand is
`either operand is a double
`converted to double
`
`either operand is
`the other operand is
`float
`a float
`converted to float
`
`
`either operand is a char‘, short int,
`it is converted to int
`int or unsigned int
`
` int bit field or enumerated or unsigned int
`
`
`either operand is
`the other operand is converted
`unsigned long int
`
`an unsigned long int
`to unsigned long int
`
`One operand is a long int and
`the other is an unsigned int
`and a long int can contain the
`value of the unsigned int
`
`the unsigned int is
`converted to long int
`
`long int
`
`both operands are
`one operand is a long int and
`converted to unsigned
`the other is an unsigned int
`and a long int can't contain
`long int
`
`the value of the unsigned int
`'
`
`unsigned long int
`
`one operand is a long int
`_ the other'operand is
`long int
`
`converted to long int
`
`one operand is an
`the other operand is
`unsigned int
`
`unsigned int
`converted to unsigned int
`
`both operands are
`of type int
`
`,
`
`int
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`Order of
`Precedence C Opera tor(s)
`
`I
`‘
`
`
`i
`Order of
`'. Precedence C Operatofis)
`
`
`
`.9
`
`1'0
`:
`
`
`11
`12
`
`13
`
`14
`
`15
`
`8
`
`& (bitwiseAND)
`
`E
`
`{
`'
`
`5
`
`
`
`SAMSUNG-1007
`
`Page 4 of 559
`
`1. o n ->-
`2
`! ~ ++ -- *(indirection)
`&(address of) (type) sizeo‘l=
`+(unary)
`-(unary)
`
`
`* (multiplication) I
`
`+-
`<<
`
`
`95
`
`>>
`
`3 4
`
`5 6
`
`
`
`SAMSUNG-1007
`Page 4 of 559
`
`
`
`Revised Edition
`
`SAMSUNG-1007
`
`Page 5 of 559
`
`
`
`..i‘1i.s.«.9.ar.(.1552.viz...
`
`.5
`‘.‘
`
`SAMSUNG-1007
`Page 5 of 559
`
`
`
`
`
`
`THE UNIVERSITY OF TEXAS AT AUSTIN
`
`UNIVERSITY OFTEXAS LIBRARIES
`
`L
`
`DATE RETURNED
`
`
`PCL/RES
`I
`
`
`
`
`
`I‘IIW 3.? J]
`
`
`
`SAMSUNG-1007
`
`Page 6 of 559
`
`SAMSUNG-1007
`Page 6 of 559
`
`
`
`31SNV‘319W
`
`Stephen G. Kacha'n
`
`'
`
`'
`
`SAMS
`PUBLISHING
`
`A Division of Prentice Hall Computer Publishing
`201 West 103rd Street, Indianapolis, Indian-[1.46290
`
`‘#-.=4
`W.
`Z
`
`.
`
`% g
`
`3
`.- é.
`19'
`PEG!
`
`gm.-
`'2
`<33:
`
`m.
`.2_
`7%”
`
`‘
`
`Qrm
`m
`
`”ANSIE
`
`Revised Editififi”"" ,
`
`-~
`
`
`
`3ESN‘V315m
`
`
`
`
`
`9:SN???31‘3“?
`
`SAMSUNG-1007
`
`Page 7 of 559
`
`SAMSUNG-1007
`Page 7 of 559
`
`
`
`To my mother and father
`
`Copyright © 1994 by Stephen G. Kochan
`REVISED EDITION
`
`All rights reserved. No part of this book shall be reproduced,
`stored in a retrieval system, or transmitted by any means, elec—
`tronic, mechanical, photocopying, recording, or otherwise, Without
`written permission from the publisher. No patent liability is
`assumed with respect to the use of the information contained
`herein. Although every preCaut-ion has been taken in the prepara—
`tion of this book, the publisher and author assume no responsibil-
`ity for errors or omissions. Neither is any liability assumed for
`damages resulting fromthe use of the information contained
`herein. For information, address Sams Publishing, a division of
`Prentice Hall Computer Publishing, 201 W. 103rd St., Indianapolis,
`IN 46290.
`
`International Standard Book Number: 0-672-30339-6
`
`Library of Congress Catalog Card Number: 93-83485
`98
`97
`'7
`
`Composed in Palatine and MCPdigz’tal
`by Prentice Hall Computer Publishing
`
`Printed in the United States of America
`
`Trademarks
`
`All terms mentioned in this book that are known to be trademarks
`
`or service marks have been appropriately capitalized. Sams
`Publishing cannot attest to the accuracy of this information. Use of
`a term in this book should not be regarded as affecting the validity
`of any trademark or service mark.
`
`DEC is a registered trademark of Digital Equipment Corp. IBM is
`a registered trademark of International Business Machines Corp.
`Macintosh is a registered trademark of Apple Computer, Inc.
`MS—DOS is a registered trademark of Microsoft Corporation.
`Turbo C is a registered trademark of Borland International, Inc.
`UNIX is a registered trademark of AT&T.
`
`
`
`
`
`
`
`
`
`.yavfimmbllnm—a‘r-finiaihwifif_:...._..;;a......;_.lemma-maneemafimnwmunm,__..__;.._._.er“.:.;;4_~;:;;;.{.4use;1,,
`
`
`
`
`
`...,n..Ezra-la.-:.:.:a\m‘
`
`SAMSUNG-1007
`
`Page 8 of 559
`
`SAMSUNG-1007
`Page 8 of 559
`
`
`
`
`
`Publisher
`
`Richard K. Swadley
`
`Associate Publisher
`
`Iordan Gold
`
`Acquisitions Manager
`Stacy Hiquet
`
`Managing Editor
`Cindy Morrow
`
`Acquisitions Editor
`Stacy Hiquet
`
`Development Editor
`Phillip W. Paxton
`
`Production Editor
`
`Carolyn Linn
`Copy Editor
`Mitzi Foster Gianakos
`
`Editorial and Graphics
`Coordinator
`Bill Whitmer
`
`Editorial Assistants
`Sharon Cox
`
`, Lynette Quinn
`
`Technical Reviewer
`
`Greg Guntle
`
`Marketing Manager
`Greg Wiegand
`
`Cover Designer
`Tim Amrheim
`
`Book Designer
`Michele Laseau
`
`Director of Production
`
`and Manufacturing
`Iefi' Valler
`
`Imprint Manager
`Kelli Widdifield
`
`Manufacturing Coordinator
`Barry Pruett
`
`Production Analyst
`Mary Beth Wakefield
`
`Proofreading/Indexing
`Coordinator
`
`Ioelynn Gifiord
`
`Graphics .Image Specialists
`Sue VandeWalle
`Dennis Sheehan
`
`Tim Montgomery
`
`Production
`Nick Anderson
`
`Ayrilca Bryant
`Lisa Daugherty
`Steph Davis
`Rich Evers
`
`Dennis Clay Hager
`Kim Hannel
`
`Stephanie McComb
`Ian N01ler
`Shelly Palma
`Linda Quigley
`Ryan Rader
`Kim'Scott
`
`Tonya Simpson
`Susan Springer
`Dennis Wesner
`
`.
`
`r"
`
`'-
`
`Alyssa Yesh
`
`‘ Indexer
`john Sleeva
`
`
`
`SAMSUNG-1007
`
`Page 9 of 559
`
`SAMSUNG-1007
`Page 9 of 559
`
`
`
`
`
`Preface XVll
`
`Introduction 1
`
`Some Fundamentals 5
`
`Writing a Program in ANSI C 13
`
`Variables, Data Types,
`and Arithmetic Expressions 23
`
`Program Looping 4:5
`
`Making Decisions 71
`
`Arrays 103
`
`~
`
`Functions 127
`
`Structures 175
`
`Character Strings 203
`
`Pointers 245
`
`Operations on Bits 291
`
`The Preprocessor 313
`
`More on Data Types 337
`
`Working with Larger Programs 351
`
`kiput and Output 3 61
`
`Miscellaneous and Advanced Features 391
`
`10
`
`11
`
`12
`
`13
`
`14
`
`15
`
`16
`
`1'7
`
`SAMSUNG-1007
`
`Page 10 of 559
`
`SAMSUNG-1007
`Page 10 of 559
`
`
`
`
`
`
`
`ANSI C Language Summary 409
`
`The Standard c Library 451
`
`Major Differences in ANSI C 471
`
`Common Programmmg Mistakes 477.
`
`The ASCII Character Set 483 -
`
`'
`
`Answers to Odd-Numbered Exercises 485
`
`Index 513
`
`Appendices
`
`A U
`
`! D U.
`
`
`
`
`
`
`
`SAMSUNG-1007
`
`Page 11 of 559
`
`SAMSUNG-1007
`Page 11 of 559
`
`
`
`
`
`Preface XVii
`
`Introduction 1
`
`Some Fundamentals 5
`
`..... 5
`Programming ..........
`Higher—Level Languages6
`Operating Systems7
`Compiling Programs ..............................8
`
`Writing a Program in ANSI C 13
`Exerc15es .......
`
`....... ..21
`
`Data Types and Constants ..
`Type int
`Typeflont
`Type double
`
`Variables, Data Types, and Arithmetic Expressions 2.3
`Variables
`
`
`
`
`
`
`Qualifiers long, short, unsigned, and signed.
`......30
`Arithmetic Expressions
`.......................... 33
`Integer Arithmetic and the Unary Minus Operator ...36
`The Modulus Operator
`................... .......38
`Integer and Floating—Point Conversions
`40
`Exercises ......................................................................
`42
`
`Program Looping 4:5
`
`Thefor Statement ..........................................46
`Program input
`............................ 54
`Nestedfor Loops .................................... 56
`for Loop Variants58
`The while Statement59
`The do Statement ...........
`............................................. 64
`The break Statement66
`The continue Statement
`..............66
`Exercises
`........ .67
`
`
`
`SAMSUNG-1007
`
`Page 12 of 559
`
`SAMSUNG-1007
`Page 12 of 559
`
`
`
`
`
`Making Decisions 71
`
`........................... ............71
`The ifStatement
`........76
`The if—else Construct
`Compound Relational Tests79 j
`Nested IfStatements
`.......................... .............82
`i
`
`The else ifConstruct
`........... .84
`The switch Statement ..............................92
`Flags ..................
`95
`The Conditional Operator
`...................... .99
`
`Exercises
`100
`.
`
`
`
`Arrays 103
`
`115
`Initializing Array Elements............. ..............
`Character Arrays117
`Multidimensional Arrays .........
`........
`121
`Exercises .............
`....... 123
`
`Functions 127
`
`A
`
`.
`
`
`
`'
`
`
`
`
`
`
`
`
`
`
`
`131
`Arguments and Local Variables
`......
`134
`Returning Function Results ... ..
`
`.140
`Functions Calling Functions Calling ... ..
`
`143
`Declaring Return Types and Argument Types
`145
`Checking Function Arguments
`Top-Down Programming..................................... ............. 147
`Functions and Arrays
`........ 148
`Assignment Operators ........
`152
`MultidimensiOnal Arrays
`158
`Global Variables162
`Automatic and Static Variables .. ..
`. .. .
`
`Recursive Functions
`Exercises
`
`......................
`
`Structures 175
`
`Functions and Structures
`
`...........................................
`
`A Structure for Storing the Time
`
`Initializing Structures
`
`Arrays of Structures
`-
`
`Structures Within Structures
`
`
`Structures Containing Arrays;........ .
`
`
`SAMSUNG-1007
`
`Page 13 of 559
`
`SAMSUNG-1007
`Page 13 of 559
`
`
`
`
`
`Structure Variants .............................................................. 199
`Exercises200
`
`10
`
`11
`
`12
`
`Character Strings 203
`Variable Length Character Strings...
`...
`...207
`Initializing and Displaying CharacterStringsm"...........209
`Testing Two Character Strings for Equality .............. 212
`lnputting Character Strings ......................................... 214
`Single—Character Input;................................................. 217'
`The Null String .............................................................. 222
`Escape Characters .............................................................. 225'
`More on Constant Strings ................................................. 227
`Character Strings, Structures, and Arrays ...................... 228
`A Better Search Method ................................................ 232
`
`Character Operations237
`
`Pointers 245
`
`Pointers and Structures ..................................................... 251'
`Structures Containing Pointers ................................... 253
`Linked Lists .................................................................... 255
`Pointers and Functions264
`Pointers and Arrays ........................................................... 270
`A Slight Digression About Program Optimization.274
`Is It an Array or Is It a Pointer? ................................... 275
`Pointers to Character Strings ....................................... 276
`Constant Character Strings and Pointers ................... 278
`The Increment and Decrement
`
`Operators Revisited .................................................... 280
`Operations on Pointers ...................................................... 283
`Pointers to Functions284
`Pointers and Memory Addresses286
`Exercises .............................................................................. 287
`
`Operations on Bits 291
`Bit Operators ....................................................................... 293
`The Bitwise AND Operator............................ 293
`The Bitwise Inclusive-OR Operator ............................ 296
`The Bitwise Exclusive-OR Operator ........................... 297
`The Ones Complement Operator298
`The Left Shift Operator ................................................. 300
`The Right Shift Operator .............................................. 301
`Bit Fields .......... . ................................................................... 306
`Exercises .............................................................................. 311
`
`Exercises240
`
`
`g
`2
`
`SAMSUNG-1007
`
`Page 14 of 559
`
`SAMSUNG-1007
`Page 14 of 559
`
`
`
`
`
`13
`
`The Preprocessor 313
`
`......... ......314
`The #define Statement .....
`.318
`...
`Program Extendability
`........319
`Program Portability ...............
`
`
`More Advanced Types of Definitions
`..............321
`
`The # Operator
`........................
`..326
`The ## Operator ...........
`........327
`The #inciude Statement ..................
`.....328
`System Include F11e5331 ‘
`
`Conditional COmpilation
`........,..331
`The #ifdefi #endzf, #else, and #ifndef Statements ..........332
`The #if and #elif Preprocessor Statements 334
`The #undef Statement
`.................... 335
`Exercises
`335
`
`
`
`14
`
`More on Data Types 337
`
`15
`
`16
`
`............. 337
`Enumerated Data Types ......
`....... 341
`.....
`The typedefStatement ......
`
`Data Type Conversions344
`Sign Extension.............................. ...........
`........ 346
`.......
`Argument Conversion .....
`..... 347
`Exercises............. ...........
`......348
`
`Working with Larger Programs 351
`Separate Compilations ....................... 351
`Communication Between Modules .....353
`an..."
`
`External Var1ab1es
`...
`...
`....354
`
`Static versus Extern Variables and Functions ..... ,......357
`Include Files
`... ... 36D
`
`Input and Output 361
`Character I/O: getchar and putchar 362
`Formatted I/O: printfand scanf362
`The printhunction363
`
`The scaanunction
`......... 369
`
`File I/O
`......
`........ ..........374
`Redirection of I/O to a File .......
`........................... 374
`
`End of File..... ....
`...
`.................... ..376
`
`Special Functions for Handling Files .........
`..378.
`
`Thefopen FunctiOn ........
`.........
`”378?
`.. .................
`The getc and puts Functions
`
`The close Function ......................................... .
`
`
`
`ThefeofFuncfion
`
`SAMSUNG-1007
`
`Page 15 of 559
`
`SAMSUNG-1007
`Page 15 of 559
`
`
`
`
`
`Thefprintf andfscaanunctions 383
`
`The fgets and fputs Functions ................
`stdin, stdout, and stderr385
`The exit Function
`........
`.............. 386
`
`Renaming and Removing F11e5387
`Exercises
`.......... 388
`
`17
`
`Miscellaneous and Advanced Features 391
`
`Miscellaneous Language Statements391
`The gate Statement..................................... ........... .. ..... ..391
`
`Unions393
`
`........... ............396
`The Comma Operator ...............
`Variable Attributes397
`
`
`
`volatile ..........398
`
`Command-Line Arguments398
`Dynamic Memory Allocation403
`The caller and malloc Functions 403
`
`The sizeofOperator404
`Thefree Function407
`
`A
`
`ANSI C Language Summary 409
`1.0 Identifiers .....................................409
`2.0 Comments
`...................... 410
`3.0 Constants .......410
`
`............410
`3.1 Integer Constants ................
`3.2 Floatinngoint Constants411
`3.3 Character Constants411
`
`3.3.1 Escape Sequences412
`3.3.2 Wide Character Constants
`...... 412
`
`
`. 412
`3.4 Character String Constants
`.......... .413
`3.4.1 Character String Concatenation
`3.4.2 Wide Character String Constants .. ..................... 413
`3.5 Enumeration Constants413
`
`...413
`4.0 Data Types and Declarations ..
`4.1 Declarations .....
`........................... 413
`
`.............. 414
`4.2 Basic Data Types .........
`4.3 Derived Data Types ............ .. ..........415
`
`4.3.1 Arrays 415
`4.3.2 Structures ...... .. .................... 417
`4.3.3Unions ............ 419
`
`
`
`
`
`SAMSUNG-1007
`
`Page 16 of 559
`
`SAMSUNG-1007
`Page 16 of 559
`
`
`
`
`
`4.3.4 Pointers .................................................................. 419
`
`
`
`4.4 Enumerated Data Types ......................................... 420
`4.5 typedef ......................................................1.................. 421
`4.6 Type Modifiers const and volatile ........................... 421
`5.0 Expressions .................................................................... 422
`5.1 Summary of C Operators ....................................... 423
`5.2 Constant Expressions .............................................. 426
`5.3 Arithmetic Operators ........................................ 427
`5.4 Logical Operators ............................................. 427
`5.5 Relational Operators428
`5.6 Bitwise Operators .................................................... 428
`5.7 Increment and Decrement Operators ................... 429
`5.8 Assignment Operators429
`5.9 Conditional Operator ......-........................................ 430
`5.10 Type Cast Operator ............................................... 430
`5.11 sizeof Operator ........................................................ 430
`5.12 Comma Operator ................................................... 431
`5.13 Basic Operations with Arrays ............................... 431
`5.14 Basic Operations with Structures431
`5.15 Basic Operations with Pointers ........................... 432
`Pointers to Arrays .............
`....................................... 433
`Pointersto Structures .................................................... 434
`5.16 Conversion of Basic Data Types .......................... 435
`6.0 Storage Classes and Scepe .......................................... 436
`6.1 Functions ................
`................................................ 437
`6.2 Variables ................................................................... 438
`7.0 Functions ....................................................................... 438
`7.1 Function Definition438
`7.2 Function Call ............................................................ 439
`7.3 Function Pointers ..................................................... 440
`8.0 Statements441
`
`8.1 Compound Statements ........................................... 441
`8.2 The break Statement ................................................. 441
`8.3 The continue-Statement ........................................... 441
`8.4 The do Statement ...................................................... 441
`
`8.5 Thefor Statement ..........................................-.- .......... 442
`8.6 The gato Statement .................................................... 442
`8.7 The If Statement .._. .................................................... 442
`8.8 The null Statement ................................................... 443
`8.9 The return Statement ............................................... 443
`8.10 The switch Statement ............................................. 443
`8.11 The while Statement444
`
`
`
`SAMSUNG-1007
`
`Page 17 of 559
`
`SAMSUNG-1007
`Page 17 of 559
`
`
`
`
`
`.................................................... 444
`9.0 The Preprocessor
`9.1 Trigraph Sequences445
`9.2 Preprocessor Directives .......................................... 445
`9.2.1 The #define Directive ............................................ 445
`9.2.2 The #arror Directive .............................................. 447
`9.2.3 The #zfDirective .................................................... 447
`9.2.4 The #ifdefDirective ............... . ............................... 448
`9.2.5 The #tfndqf Directive ............................................. 448
`9.2.6 The #include Directive .......................................... 449
`9.2.7 The #line Directive ................................................ 449
`9.2.8 The #pmgma Directive .......................................... 449
`9.2.9 The #undef Directive ............................................. 450
`9.2.10 The # Directive .................................................... 450
`9.3 Predefined Identifiers ...... . ...................................... 450
`
`B
`
`C
`
`
`
`The Standard C Library 451
`Standard Header Files ....................................................... 451
`<stddef.h> 452
`<1imit5.h> ........................................................................ 452
`<float.h> .........................................................
`...453
`
`String Functions
`454
`Memory Functions ............................................................. 455
`Character Functions ........................................................... 456
`I/ 0 Functions ..................................................................... 457
`III-Memory Format Conversion Functions ..................... 462
`String~to—Nurnber Conversion ..................................'....... 463
`Dynamic Memory Allocation Functions ......................... 465
`Math Functions ................................................................... 465
`E
`General Utility Functions .................................................. 467
`Major Differences in ANSI c: 471E
`1.0 Identifiers ....................................................................... 4.71
`f:
`3.0 Constants
`........................................................472
`E
`3.1 Integer Constants ..................................................... 472
`fig
`3.2 Floating-Point Constants ........................................ 472
`E
`3.3 Character Constants472
`‘E
`3.3.1 Escape Sequences ................................................. 472
`E
`3.3.2 Wide Character COnstants .................................. 472
`E
`3.4 Character String Constants ....................................472
`{E
`3.4.1 Character String Concatenation 472
`5;
`3.4.2 Wide Character String Constants.................... 472
`E
`
`
`
`SAMSUNG-1007
`
`Page 18 of 559
`
`SAMSUNG-1007
`Page 18 of 559
`
`
`
`
`
`4.0 Data Types and Declarations........-..............................472
`4.2 Basic Data Types ......................472
`. 4.3 Derived Data Types 473
`"nun-u ”- ”nu.-
`
`
`
`4.3.1 Arrays........
`473
`
`4.3.2 Structures
`....473
`4.3.3 Unions473
`4.3.4 Pointers473
`
`473
`4.6 Type Modifiers const and volatile... ............ . .......
`5.0 Expressions....................................................................473
`
`5.3 Arithmetic Operators
`'
`
`5.6 Bitwise Operators
`
`5.11 sizeofOperator ............474
`5.16 Conversion of Basic Data Types .......................... 474
`7.0 FunctiOns ...........474
`7.1 Function Definition474
`7.3 Function Pomters ...........474
`
`
`........475
`9.0 The Preprocessor
`9.1 Trigraph Sequences ................................................ .475
`9.2.1 The #define Directive 475
`9.2.2 The #error Directive475
`
`9.2.8 The #pragma Directive .......................................... 475
`
`9.2.10 The # Directive
`................4’75
`
`Common Programming Mistakes 477
`
`E
`
`The ASCII Character Set 483
`
`Answars To Odd-Numbered Exercises 485
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`Chapter3485
`Chapter4486
`Chapter5486
`Chapter 6 ................490
`Chapter 7 ......................................................................... 492
`
`“eunu-
`
`Chapter 8......
`..........493
`Chapter 9496
`Chapter 10500
`Chapter11505 _
`
`Chapter12
`Chapter 13.......... .......................... . ...................
` - ”nun-noun”.nae-nun”“Hue—unann-
`Chapter 14
`Chapter 16......... ...................... . .......
`
`'
`
`
`
`
`
`
`'
`
`
`Index ‘ 513
`
`SAMSUNG-1007
`
`Page 19 of 559
`
`SAMSUNG-1007
`Page 19 of 559
`
`
`
`
`
`
`
`SAMSUNG-1007
`
`Page 20 of 559
`
`SAMSUNG-1007
`Page 20 of 559
`
`
`
`A
`
`a”
`
`Preface
`
`When the original edition of Programming in ANSI C was written, the ANSI C
`specification still existed in draft form. Since that time, not only has the stan—
`dard been adopted, but the ANSI C form of the C programming language has
`become ubiquitous as well.
`
`Over the years, I have had many requests to offer answers to the exercises at
`the end of the chapters. I have been reluctant to do so since that would make
`the book less suitable for use as a classroom textbook. As a compromise of sorts,
`Ihave decided to include answers to the odd-numbered exercises.
`
`The other significant change made to this edition of the book has been the
`addition of ”Quick Tips” throughout the text. These were added to reinforce
`some key ideas and to help you avoid pitfalls commonly made by C program—
`mers.
`.
`
`This year marks 10 years since the originial Programming in C book was pub—
`lished. I am grateful to anyone who has used my book. I hope it has achieved
`its objective of trying to make learning to program in C easier and more enjoy—
`able.
`'
`
`
`
`SAMSUNG-1007
`
`Page 21 of 559
`
`SAMSUNG-1007
`Page 21 of 559
`
`
`
`gafiufifiwgmv,
`
`gsfiiaiuafiixaivfiifi‘i
`
`519/4
`
`SAMSUNG-1007
`
`Page 22 of 559
`
`SAMSUNG-1007
`Page 22 of 559
`
`
`
`
`.
`
`in!
`
`.m'
`
`Up.44
`
`.
`
`_ o- t==i
`
`
`'0?)
`.2
`at?
`
`(a).
`m.
`
`1
`
`.
`
`Introduction
`
`The original version of the ”C” programming language was pioneered
`by Dennis Ritchie at AT&T Bell Laboratories in the early 19705. It was
`not until the late 19705, however, that this programing language
`began to gain widespread popularity and support. This was because
`until that time C compilers were not readily available for conunercial
`use outside of Bell Laboratories. Initially, C’s growth in popularity was
`also spurred on in part by the equal,“ if not faster, growth in popular-
`ity of the UNIX operating system. This operating system, which was
`i also developed at Bell Laboratories, has C as its ”standard” program—
`ming language. In fact, well over 90 percent of the operating system
`itself is written in the C language!
`
`The enormous success of the IBM ‘PC and its look~alikes soon made
`'MS-DOS the most popular environment for the C language. As C grew
`in p0pu1arity across different operating systems, more and more ven-
`dors hopped on the bandwagon and started marketing their mm C
`compilers. For the most part, their version of the C language was based
`on an appendix found in the first Cprogramming text—The C Pro—
`gramming Language—by Brian Kerru'ghan and Dennis Ritchie (Pren-
`tice-Hall, 1978). Unfortunately, this appendix did not provide a com-
`plete and unambiguous definition of C, meaning that vendors were
`
`__
`
`left to interpret some as ects of the language on their own. éisnv;
`
`JIM'
`318Warsurom
`
`SAMSUNG-1007
`
`Page 23 of 559
`
`SAMSUNG-1007
`Page 23 of 559
`
`
`
`
`
`In the early 19805 a need was seen to standardize the definition of the_C lan-
`guage. The American National Standards Institute (ANSI) is the organization
`that handles such things, so in 1983 an ANSI committee (called X3111).was
`formed to standardize C.
`
`With an ANSI definition for C, you are assured that anyone who sells a true
`ANSI C compiler has incorporated all of the features of the language specified
`by the standard. It also means that programmers who are careful can truly
`Write C programs that will run without modification on any system that has
`an ANSI C compiler.
`
`C is a ”higher-level language,” yet it provides capabilities that enable the usar
`to ”get in close” with the hardware and deal with the computer on a much lower
`level. This is because, although C is a general-purpose structured programming
`language, it was originally designed with systems prograrruning applications
`in mind and, as such, provides the user with an enormous amount of power
`and flexibility. In fact, programming applications exist that could be easily
`handled by the C language, but that would be difficult to develop in other lan-
`guages such as Pascal, FORTRAN, or BASIC.
`
`This book pr0poses to teach you how to program in ANSI C. It assumes no
`previous exposure to the language and was designed to appeal to novice and
`experienced programmers alike. If you have previous programming experi-
`ence, you will find that C has a unique way of doing things that probably dif—
`fers significantly from any language you have used. Even if y0u are coming
`from a Pascalbackground—a language that C superficially resembles—you will
`quickly discover many features that are unique to this language, such as point-
`ers, character strings, and bit operations.
`
`Every feature of the C language is treated in this text. As each new feature is
`presented, a small complete program example is usually provided to illustrate
`the feature. This reflects the overriding philosophy that has been used in writ«
`ing this book: to teach by example. Just as a picture is worth a thousand words,
`so is a properly chosen program example. If yOu have access to a computer
`facility that supports the ANSI C programming language, you-are strongly
`encouraged to enter and run each program presented in this book and to com-
`pare the results obtained on your system to thOse shown'in the text. By doing
`so, not only will you learn the language and its syntax, but you will also be-
`come familiar with the process of typing in, compiling, andrunning C
`programs.
`1
`1
`1
`1
`-
`The style used for teaching ANSI C is ohe of posing a particular problem for
`
`
`
`SAMSUNG-1007
`
`Page 24 of 559
`
`
`
`
`
`
`
`solution on a computer and then proceeding to develop p ogramin C to solve
`the problem. In this manner, new language constr
`1n
`uced as they
`
`
`
`SAMSUNG-1007
`Page 24 of 559
`
`
`
`
`
`
`You will find that program readability has been stressed throughout the'book.
`This is because I strongly believe that programs should be written so that they
`
`may be easily read—either by the author or by somebody else. Through expe—
`rience and common sens e, you will find that such programs are almost always
`easier to write, debug, and modify. Furthermore, deve10ping programs that
`are readable is a natural result of a true adherence to a structured program- -
`mjng discipline.
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`Because this book was written as a tutorial, the material covered in each chap—
`ter is based on previously presented material. Therefore, maximumbenefit will
`be derived from this book by reading each chapter in succeSsion, and you are
`highly discouraged from ”skipping around.” You should also work through
`the exercises that are presented at the end of each chapter before proceeding
`on to the next chapter.
`
`Chapter 2, which covers some fundamental terminology about higher-level
`programming languages and the process of compiling programs, has been
`included to make sure that we are speaking the same language throughout the
`remainder of the text. From Chapter 3 on, you will be slowly introduced to the
`ANSI C language. By the time Chapter 16 rolls around, all the essential fea-
`tures of the language will have been covered. Chapter 16 goes into more depth
`about 1/0 operations in C. Finally, Chapter 17 includes those features of the
`language that are of a more advanced or esoteric nature.
`
`Appendix A provides a complete summary of the language and is provided
`for reference purposes. Appendix B provides a summary of most of the stan-
`dard library routines that you will find on all systems that support ANSI C.
`Appendix C summarizes the major differences between the ANSI C language
`described in this text and the “older” C. In Appendix D you’ll find a list of
`common programming mistakes. Appendix E is a single—page ASCII chart.
`Finally, Appendix F contains the answers to the odd-numbered exercises found
`at the end of each chapter.
`
`This book makes no assumptions about a particular computer system or oper-
`ating system on which the ANSI C language is implemented. The text makes
`brief mention of how to compile and execute programs under MS-DOS and
`UNIX. The best reference for more detailed information is the documentation
`that came with your compiler.
`:
`
`I wish to thank the following people for their help in the preparation of th.
`original text, Programming in C: Douglas McCormick, Maureen Connelly, Ihit
`Scharf, Henry Tabickman, and, above all, Ken Brown. I also wish to thank
`
`
`
`
`
`
`
`
`
`
`
`SAMSUNG-1007
`
`Page 25 of 559
`
`SAMSUNG-1007
`Page 25 of 559
`
`
`
` Henry Mullish of New York University for teaching me so much about writ-
`
`ing and for getting me started in the publishing business. I’d also ‘like to thank
`Dick Fritz, who has generously given comments for many books Ihave writ-
`ten, including the original and revised editions of this text.
`
`Finally, I wish to thank my wife Leela, who provided her patience and under—
`standing through three versions of this text.
`
`
`
`SAMSUNG-1007
`
`Page 26 of 559
`
`SAMSUNG-1007
`Page 26 of 559
`
`
`
`
`
`ANSECANS-ICC‘ANSEC
`
`ANSIC
`
`_-U".)
`.2;
`a.
`
`aU)
`
`Fundamentals
`
`This chapter describes some fundamental terms that you must under-
`stand before you can learn how to program in C. A general overv