We Want to Hear from You As the reader of this book, you are our most important critic and commentator.We value your opinion and want to know what we re doing right, what we could do better, what areas you d like to see us publish in, and any other words of wisdom you re willing to pass our way. As an associate publisher for Sams Publishing, I welcome your comments.You can email or write me directly to let me know what you did or didn t like about this book as well as what we can do to make our books better. Please note that I cannot help you with technical problems related to the topic of this book.We do have a User Services group, however, where I will forward specific technical questions related to the book. When you write, please be sure to include this book s title and author as well as your name, email address, and phone number. I will carefully review your comments and share them with the author and editors who worked on the book. Email: feedback@samspublishing.com Mail: Michael Stephens Associate Publisher Sams Publishing 800 East 96th Street Indianapolis, IN 46240 USA For more information about this book or another Sams Publishing title, visit our Web site at www.samspublishing.com.Type the ISBN (excluding hyphens) or the title of a book in the Search field to find the page you re looking for. –
We Want to Hear from You As the reader of this book, you are our most important critic and commentator.We value your opinion and want to know what we re doing right, what we could do better, what areas you d like to see us publish in, and any other words of wisdom you re willing to pass our way. As an associate publisher for Sams Publishing, I welcome your comments.You can email or write me directly to let me know what you did or didn t like about this book as well as what we can do to make our books better. Please note that I cannot help you with technical problems related to the topic of this book.We do have a User Services group, however, where I will forward specific technical questions related to the book. When you write, please be sure to include this book s title and author as well as your name, email address, and phone number. I will carefully review your comments and share them with the author and editors who worked on the book. Email: feedback@samspublishing.com Mail: Michael Stephens Associate Publisher Sams Publishing 800 East 96th Street Indianapolis, IN 46240 USA For more information about this book or another Sams Publishing title, visit our Web site at www.samspublishing.com.Type the ISBN (excluding hyphens) or the title of a book in the Search field to find the page you re looking for.
Acknowledgements I wish to thank the following people
Acknowledgements I wish to thank the following people for their help in the preparation of various versions of this text: Douglas McCormick, Jim Scharf, Henry Tabickman, Dick Fritz, Steve Levy, Tony Ianinno, and Ken Brown. I also want to thank Henry Mullish of New York University for teaching me so much about writing and for getting me started in the publishing business. From Sams Publishing, I d like to thank my development editor Mark Renfrow and my project editor Dan Knott.Thanks also to my copy editor, Karen Annett, and my technical editor, Bradley Jones. Finally, I d like to thank all the other people from Sams who were involved on this project, even if I did not work with them directly. –
Acknowledgements I wish to thank the following people for their help in the preparation of various versions of this text: Douglas McCormick, Jim Scharf, Henry Tabickman, Dick Fritz, Steve Levy, Tony Ianinno, and Ken Brown. I also want to thank Henry Mullish of New York University for teaching me so much about writing and for getting me started in the publishing business. From Sams Publishing, I d like to thank my development editor Mark Renfrow and my project editor Dan Knott.Thanks also to my copy editor, Karen Annett, and my technical editor, Bradley Jones. Finally, I d like to thank all the other people from Sams who were involved on this project, even if I did not work with them directly.
About the Author Stephen Kochan has been developing
About the Author Stephen Kochan has been developing software with the C programming language for over 20 years. He is the author and coauthor of several bestselling titles on the C language, including Programming in C, Programming in ANSI C, andTopics in C Programming, and several Unix titles, including Exploring the Unix System, Unix Shell Programming, and Unix System Security. Mr. Kochan s most recent title, Programming in Objective-C, is a tutorial on an object-oriented programming language that is based on C. –
About the Author Stephen Kochan has been developing software with the C programming language for over 20 years. He is the author and coauthor of several bestselling titles on the C language, including Programming in C, Programming in ANSI C, andTopics in C Programming, and several Unix titles, including Exploring the Unix System, Unix Shell Programming, and Unix System Security. Mr. Kochan s most recent title, Programming in Objective-C, is a tutorial on an object-oriented programming language that is based on C.
Preface It s hard to believe that 20 years
Preface It s hard to believe that 20 years have passed since I first wrote Programming in C. At that time the Kernighan & Ritchie book The C Programming Language was the only other book on the market. How times have changed! When talk about an ANSI C standard emerged in the early 1980s, this book was split into two titles:The original was still called Programming in C, and the title that covered ANSI C was called Programming in ANSI C.This was done because it took several years for the compiler vendors to release their ANSI C compilers and for them to become ubiquitous. I felt it was too confusing to try to cover both ANSI and non-ANSI C in the same tutorial text, thus the reason for the split. The ANSI C standard has changed several times since the first standard was published in 1989.The latest version, called C99, is the major reason for this edition.This edition addresses the changes made to the language as a result of that standard. In addition to covering C99 features, this book also includes two new chapters. The first discusses debugging C programs.The second offers a brief overview of the pervasive field of object-oriented programming, or OOP.This chapter was added because several popular OOP languages are based on C: C++, C#, Java, and Objective-C. For those who have stayed with this text through the years, I am sincerely grateful. The feedback I have received has been enormously gratifying. It remains my main motivation for continuing to write today. For newcomers, I welcome your input and hope that this book satisfies your expectations. Stephen Kochan June 2004 steve@kochan-wood.com –
Preface It s hard to believe that 20 years have passed since I first wrote Programming in C. At that time the Kernighan & Ritchie book The C Programming Language was the only other book on the market. How times have changed! When talk about an ANSI C standard emerged in the early 1980s, this book was split into two titles:The original was still called Programming in C, and the title that covered ANSI C was called Programming in ANSI C.This was done because it took several years for the compiler vendors to release their ANSI C compilers and for them to become ubiquitous. I felt it was too confusing to try to cover both ANSI and non-ANSI C in the same tutorial text, thus the reason for the split. The ANSI C standard has changed several times since the first standard was published in 1989.The latest version, called C99, is the major reason for this edition.This edition addresses the changes made to the language as a result of that standard. In addition to covering C99 features, this book also includes two new chapters. The first discusses debugging C programs.The second offers a brief overview of the pervasive field of object-oriented programming, or OOP.This chapter was added because several popular OOP languages are based on C: C++, C#, Java, and Objective-C. For those who have stayed with this text through the years, I am sincerely grateful. The feedback I have received has been enormously gratifying. It remains my main motivation for continuing to write today. For newcomers, I welcome your input and hope that this book satisfies your expectations. Stephen Kochan June 2004 steve@kochan-wood.com
xvi Contents Dynamic Memory Allocation Functions 481 Math Functions
xvi Contents Dynamic Memory Allocation Functions 481 Math Functions 482 Complex Arithmetic 488 General Utility Functions 490 C Compiling Programs with gcc 493 General Command Format 493 Command-Line Options 494 D Common Programming Mistakes 497 E Resources 501 Answers to Exercises, Errata, etc. 501 The C Programming Language 501 Books 501 Web Sites 502 Newsgroups 502 C Compilers and Integrated Development Environments 502 gcc 502 MinGW 502 CygWin 502 Visual Studio 503 CodeWarrior 503 Kylix 503 Miscellaneous 503 Object-Oriented Programming 503 The C++ Language 503 The C# Language 503 The Objective-C Language 503 Development Tools 504 Index 505 –
xvi Contents Dynamic Memory Allocation Functions 481 Math Functions 482 Complex Arithmetic 488 General Utility Functions 490 C Compiling Programs with gcc 493 General Command Format 493 Command-Line Options 494 D Common Programming Mistakes 497 E Resources 501 Answers to Exercises, Errata, etc. 501 The C Programming Language 501 Books 501 Web Sites 502 Newsgroups 502 C Compilers and Integrated Development Environments 502 gcc 502 MinGW 502 CygWin 502 Visual Studio 503 CodeWarrior 503 Kylix 503 Miscellaneous 503 Object-Oriented Programming 503 The C++ Language 503 The C# Language 503 The Objective-C Language 503 Development Tools 504 Index 505
Contents xv 6.0 Storage Classes and Scope 452
Contents xv 6.0 Storage Classes and Scope 452 6.1 Functions 452 6.2 Variables 452 7.0 Functions 454 7.1 Function Definition 454 7.2 Function Call 455 7.3 Function Pointers 456 8.0 Statements 456 8.1 Compound Statements 456 8.2 The break Statement 456 8.3 The continue Statement 457 8.4 The do Statement 457 8.5 The for Statement 457 8.6 The goto Statement 458 8.7 The if Statement 458 8.8 The null Statement 458 8.9 The return Statement 459 8.10 The switch Statement 459 8.11 The while Statement 460 9.0 The Preprocessor 460 9.1 Trigraph Sequences 460 9.2 Preprocessor Directives 461 9.3 Predefined Identifiers 466 B The Standard C Library 467 Standard Header Files 467
Contents xv 6.0 Storage Classes and Scope 452 6.1 Functions 452 6.2 Variables 452 7.0 Functions 454 7.1 Function Definition 454 7.2 Function Call 455 7.3 Function Pointers 456 8.0 Statements 456 8.1 Compound Statements 456 8.2 The break Statement 456 8.3 The continue Statement 457 8.4 The do Statement 457 8.5 The for Statement 457 8.6 The goto Statement 458 8.7 The if Statement 458 8.8 The null Statement 458 8.9 The return Statement 459 8.10 The switch Statement 459 8.11 The while Statement 460 9.0 The Preprocessor 460 9.1 Trigraph Sequences 460 9.2 Preprocessor Directives 461 9.3 Predefined Identifiers 466 B The Standard C Library 467 Standard Header Files 467
xiv Contents A C Language Summary 425 1.0
xiv Contents A C Language Summary 425 1.0 Digraphs and Identifiers 425 1.1 Digraph Characters 425 1.2 Identifiers 425 2.0 Comments 426 3.0 Constants 427 3.1 Integer Constants 427 3.2 Floating-Point Constants 427 3.3 Character Constants 428 3.4 Character String Constants 429 3.5 Enumeration Constants 430 4.0 Data Types and Declarations 430 4.1 Declarations 430 4.2 Basic Data Types 430 4.3 Derived Data Types 432 4.4 Enumerated Data Types 438 4.5 The typedef Statement 438 4.6 Type Modifiers const, volatile, and restrict 439 5.0 Expressions 439 5.1 Summary of C Operators 440 5.2 Constant Expressions 442 5.3 Arithmetic Operators 443 5.4 Logical Operators 444 5.5 Relational Operators 444 5.6 Bitwise Operators 445 5.7 Increment and Decrement Operators 445 5.8 Assignment Operators 446 5.9 Conditional Operators 446 5.10 Type Cast Operator 446 5.11 sizeof Operator 447 5.12 Comma Operator 447 5.13 Basic Operations with Arrays 447 5.14 Basic Operations with Structures 448 5.15 Basic Operations with Pointers 448 5.16 Compound Literals 450 5.17 Conversion of Basic Data Types 451 –
xiv Contents A C Language Summary 425 1.0 Digraphs and Identifiers 425 1.1 Digraph Characters 425 1.2 Identifiers 425 2.0 Comments 426 3.0 Constants 427 3.1 Integer Constants 427 3.2 Floating-Point Constants 427 3.3 Character Constants 428 3.4 Character String Constants 429 3.5 Enumeration Constants 430 4.0 Data Types and Declarations 430 4.1 Declarations 430 4.2 Basic Data Types 430 4.3 Derived Data Types 432 4.4 Enumerated Data Types 438 4.5 The typedef Statement 438 4.6 Type Modifiers const, volatile, and restrict 439 5.0 Expressions 439 5.1 Summary of C Operators 440 5.2 Constant Expressions 442 5.3 Arithmetic Operators 443 5.4 Logical Operators 444 5.5 Relational Operators 444 5.6 Bitwise Operators 445 5.7 Increment and Decrement Operators 445 5.8 Assignment Operators 446 5.9 Conditional Operators 446 5.10 Type Cast Operator 446 5.11 sizeof Operator 447 5.12 Comma Operator 447 5.13 Basic Operations with Arrays 447 5.14 Basic Operations with Structures 448 5.15 Basic Operations with Pointers 448 5.16 Compound Literals 450 5.17 Conversion of Basic Data Types 451
Contents xiii 17 Miscellaneous and Advanced Features 373
Contents xiii 17 Miscellaneous and Advanced Features 373 Miscellaneous Language Statements 373 The goto Statement 373 The null Statement 374 Working with Unions 375 The Comma Operator 378 Type Qualifiers 378 The register Qualifier 378 The volatile Qualifier 379 The restrict Qualifier 379 Command-Line Arguments 380 Dynamic Memory Allocation 383 The calloc and malloc Functions 384 The sizeof Operator 385 The free Function 387 18 Debugging Programs 389 Debugging with the Preprocessor 389 Debugging Programs with gdb 395 Working with Variables 398 Source File Display 399 Controlling Program Execution 400 Getting a Stack Trace 405 Calling Functions and Setting Arrays and Structures 405 Getting Help with gdb Commands 406 Odds and Ends 408 19 Object-Oriented Programming 411 What Is an Object Anyway? 411 Instances and Methods 412 Writing a C Program to Work with Fractions 413 Defining an Objective-C Class to Work with Fractions 414 Defining a C++ Class to Work with Fractions 419 Defining a C# Class to Work with Fractions 422 –
Contents xiii 17 Miscellaneous and Advanced Features 373 Miscellaneous Language Statements 373 The goto Statement 373 The null Statement 374 Working with Unions 375 The Comma Operator 378 Type Qualifiers 378 The register Qualifier 378 The volatile Qualifier 379 The restrict Qualifier 379 Command-Line Arguments 380 Dynamic Memory Allocation 383 The calloc and malloc Functions 384 The sizeof Operator 385 The free Function 387 18 Debugging Programs 389 Debugging with the Preprocessor 389 Debugging Programs with gdb 395 Working with Variables 398 Source File Display 399 Controlling Program Execution 400 Getting a Stack Trace 405 Calling Functions and Setting Arrays and Structures 405 Getting Help with gdb Commands 406 Odds and Ends 408 19 Object-Oriented Programming 411 What Is an Object Anyway? 411 Instances and Methods 412 Writing a C Program to Work with Fractions 413 Defining an Objective-C Class to Work with Fractions 414 Defining a C++ Class to Work with Fractions 419 Defining a C# Class to Work with Fractions 422
xii Contents Data Type Conversions 327 Sign Extension 329
xii Contents Data Type Conversions 327 Sign Extension 329 Argument Conversion 329 Exercises 330 15 Working with Larger Programs 333 Dividing Your Program into Multiple Files 333 Compiling Multiple Source Files from the Command Line 334 Communication Between Modules 336 External Variables 336 StaticVersus ExternVariables and Functions 339 Using Header Files Effectively 341 Other Utilities for Working with Larger Programs 342 The make Utility 343 The cvs Utility 344 Unix Utilities: ar, grep, sed, and so on 345 16 Input and Output Operations in C 347 Character I/O: getchar and putchar 348 Formatted I/O: printf and scanf 348 The printf Function 348 The scanf Function 355 Input and Output Operations with Files 359 Redirecting I/O to a File 359 End of File 361 Special Functions for Working with Files 363 The fopen Function 363 The getc and putc Functions 365 The fclose Function 365 The feof Function 367 The fprintf and fscanf Functions 368 The fgets and fputs Functions 368 stdin, stdout, and stderr 369 The exit Function 370 Renaming and Removing Files 371 Exercises 371 –
xii Contents Data Type Conversions 327 Sign Extension 329 Argument Conversion 329 Exercises 330 15 Working with Larger Programs 333 Dividing Your Program into Multiple Files 333 Compiling Multiple Source Files from the Command Line 334 Communication Between Modules 336 External Variables 336 StaticVersus ExternVariables and Functions 339 Using Header Files Effectively 341 Other Utilities for Working with Larger Programs 342 The make Utility 343 The cvs Utility 344 Unix Utilities: ar, grep, sed, and so on 345 16 Input and Output Operations in C 347 Character I/O: getchar and putchar 348 Formatted I/O: printf and scanf 348 The printf Function 348 The scanf Function 355 Input and Output Operations with Files 359 Redirecting I/O to a File 359 End of File 361 Special Functions for Working with Files 363 The fopen Function 363 The getc and putc Functions 365 The fclose Function 365 The feof Function 367 The fprintf and fscanf Functions 368 The fgets and fputs Functions 368 stdin, stdout, and stderr 369 The exit Function 370 Renaming and Removing Files 371 Exercises 371
Contents xi Pointers to Functions 273 Pointers and
Contents xi Pointers to Functions 273 Pointers and Memory Addresses 274 Exercises 276 12 Operations on Bits 279 Bit Operators 280 The Bitwise AND Operator 281 The Bitwise Inclusive-OR Operator 283 The Bitwise Exclusive-OR Operator 284 The Ones Complement Operator 285 The Left Shift Operator 287 The Right Shift Operator 287 A Shift Function 288 Rotating Bits 290 Bit Fields 292 Exercises 297 13 The Preprocessor 299 The #define Statement 299 Program Extendability 303 Program Portability 305 More Advanced Types of Definitions 306 The # Operator 312 The ## Operator 313 The #include Statement 313 System Include Files 316 Conditional Compilation 316 The #ifdef, #endif, #else, and #ifndef Statements 316 The #if and #elif Preprocessor Statements 318 The #undef Statement 319 Exercises 320 14 More on Data Types 321 Enumerated Data Types 321 The typedef Statement 325 –
Contents xi Pointers to Functions 273 Pointers and Memory Addresses 274 Exercises 276 12 Operations on Bits 279 Bit Operators 280 The Bitwise AND Operator 281 The Bitwise Inclusive-OR Operator 283 The Bitwise Exclusive-OR Operator 284 The Ones Complement Operator 285 The Left Shift Operator 287 The Right Shift Operator 287 A Shift Function 288 Rotating Bits 290 Bit Fields 292 Exercises 297 13 The Preprocessor 299 The #define Statement 299 Program Extendability 303 Program Portability 305 More Advanced Types of Definitions 306 The # Operator 312 The ## Operator 313 The #include Statement 313 System Include Files 316 Conditional Compilation 316 The #ifdef, #endif, #else, and #ifndef Statements 316 The #if and #elif Preprocessor Statements 318 The #undef Statement 319 Exercises 320 14 More on Data Types 321 Enumerated Data Types 321 The typedef Statement 325