IT training beginning c (5th ed ) horton 2013 02 26

675 334 0
IT training beginning c (5th ed ) horton 2013 02 26

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Download from Wow! eBook For your convenience Apress has placed some of the front matter material after the index Please use the Bookmarks and Contents at a Glance links to access them Contents at a Glance About the Author��������������������������������������������������������������������������������������������������������������� xxi About the Technical Reviewer����������������������������������������������������������������������������������������� xxiii Acknowledgments������������������������������������������������������������������������������������������������������������ xxv Introduction�������������������������������������������������������������������������������������������������������������������� xxvii ■■Chapter 1: Programming in C��������������������������������������������������������������������������������������������1 ■■Chapter 2: First Steps in Programming���������������������������������������������������������������������������25 ■■Chapter 3: Making Decisions�������������������������������������������������������������������������������������������85 ■■Chapter 4: Loops������������������������������������������������������������������������������������������������������������135 ■■Chapter 5: Arrays����������������������������������������������������������������������������������������������������������185 ■■Chapter 6: Applications with Strings and Text��������������������������������������������������������������219 ■■Chapter 7: Pointers��������������������������������������������������������������������������������������������������������263 ■■Chapter 8: Structuring Your Programs��������������������������������������������������������������������������321 ■■Chapter 9: More on Functions���������������������������������������������������������������������������������������349 ■■Chapter 10: Essential Input and Output�������������������������������������������������������������������������395 ■■Chapter 11: Structuring Data�����������������������������������������������������������������������������������������429 ■■Chapter 12: Working with Files�������������������������������������������������������������������������������������489 ■■Chapter 13: The Preprocessor and Debugging��������������������������������������������������������������557 ■■Chapter 14: Advanced and Specialized Topics��������������������������������������������������������������589 v ■ Contents at a Glance ■■Appendix A: Computer Arithmetic���������������������������������������������������������������������������������621 ■■Appendix B: ASCII Character Code Definitions��������������������������������������������������������������629 ■■Appendix C: Reserved Words in C����������������������������������������������������������������������������������635 ■■Appendix D: Input and Output Format Specifications����������������������������������������������������637 ■■Appendix E: Standard Library Header Files�������������������������������������������������������������������643 Index���������������������������������������������������������������������������������������������������������������������������������645 vi Introduction Welcome to Beginning C: Fifth Edition With this book you can become a competent C programmer using the latest version of the C language In many ways, C is an ideal language with which to learn programming It’s very compact, so there isn’t a lot of syntax to learn before you can write real applications In spite of its conciseness, it’s extremely powerful and is used by professionals in many different areas The power of C is such that it can be applied at all levels, from developing device drivers and operating system components to creating large-scale applications A relatively new area for C is in application development for mobile phones C compilers are available for virtually every kind of computer, so when you’ve learned C, you’ll be equipped to program in just about any context Once you know C, you have an excellent base from which you can build an understanding of the object-oriented C++ My objective in this book is to minimize what I think are the three main hurdles the aspiring programmer must face: coming to grips with the jargon that pervades every programming language, understanding how to use the language elements (as opposed to merely knowing what they are), and appreciating how the language is applied in a practical context Jargon is an invaluable and virtually indispensable means of communication for the expert professional as well as the competent amateur, so it can’t be avoided My approach is to ensure that you understand the jargon and get comfortable using it in context In this way, you’ll be able to more effectively use the documentation that comes along with the typical programming product and also feel comfortable reading and learning from the literature that surrounds most programming languages Comprehending the syntax and effects of the language elements is obviously an essential part of learning C, but appreciating how the language features work and how they are used is equally important Rather than just using code fragments, I provide you with practical working examples in each chapter that show how the language features can be applied to specific problems These examples provide a basis for you to experiment and see the effects of changing the code Your understanding of programming in context needs to go beyond the mechanics of applying individual language elements To help you gain this understanding, I conclude most chapters with a more complex program that applies what you’ve learned in the chapter These programs will help you gain the competence and confidence to develop your own applications and provide you with insight into how you can apply language elements in combination and on a larger scale Most important, they’ll give you an idea of what’s involved in designing real programs and managing real code It’s important to realize a few things that are true for learning any programming language First, there is quite a lot to learn, but this means you’ll gain a greater sense of satisfaction when you’ve mastered it Second, it’s great fun, so you really will enjoy it Third, you can only learn programming by doing it, and this book helps you along the way Finally, it’s certain you will make a lot of mistakes and get frustrated from time to time during the learning process When you think you are completely stuck, you just need to be persistent You will eventually experience that eureka moment and realize it wasn’t as difficult as you thought xxvii ■ Introduction How to Use This Book Because I believe in the hands-on approach, you’ll write your first programs almost immediately Every chapter has several complete programs that put theory into practice, and these are key to the book You should type in and run all the examples that appear in the text because the very act of typing them in is a tremendous memory aid You should also attempt all the exercises that appear at the end of each chapter When you get a program to work for the first time—particularly when you’re trying to solve your own problems—you’ll find that the great sense of accomplishment and progress makes it all worthwhile The pace is gentle at the start, but you’ll gain momentum as you get further into the subject Each chapter covers quite a lot of ground, so take your time and make sure you understand everything before moving on Experimenting with the code and trying out your own ideas are important parts of the learning process Try modifying the programs and see what else you can make them do—that’s when it gets really interesting And don’t be afraid to try things out—if you don’t understand how something works, just type in a few variations and see what happens It doesn’t matter if it’s wrong You’ll find you often learn a lot from getting it wrong A good approach is to read each chapter through, get an idea of its scope, and then go back and work through all the examples You might find some of the end-of-chapter programs quite difficult Don’t worry if it’s not all completely clear on the first try There are bound to be bits that you find hard to understand at first because they often apply what you’ve learned to rather complicated problems If you really get stuck, you can skip the end-of-chapter exercises, move on to the next chapter, and come back to them later You can even go through the entire book without worrying about them However, if you can complete the exercises, it shows you are making real progress Who This Book Is For Beginning C: Fifth Edition is designed to teach you how to write useful programs in C as quickly and easily as possible By the end of Beginning C, you’ll have a thorough grounding in programming the C language This is a tutorial for those who’ve done a little bit of programming before, understand the concepts behind it, and want to further your knowledge by learning C However, no previous programming knowledge on your part is assumed, so if you’re a newcomer to programming, the book will still work for you What You Need to Use This Book To use this book, you’ll need a computer with a C compiler and library installed, so you can execute the examples, and a program text editor for preparing your source code files The compiler you use should provide good support for the current International Standard for the C language, ISO/IEC 9899:2011, commonly referred to as C11 You’ll also need an editor for creating and modifying your code You can use any plain text editor such as Notepad or vi to create your source program files However, you’ll get along better if your editor is designed for editing C code I can suggest two sources for a suitable C compiler, both of which are freeware: xxviii • The GNU C compiler, GCC, is available from http://www.gnu.org and supports a variety of operating system environments • The Pelles C compiler for Microsoft Windows is downloadable from http://www.smorgasbordet.com/pellesc/ and includes an excellent IDE ■ Introduction Conventions Used I use a number of different styles of text and layout in the book to help differentiate between the different kinds of information For the most part, their meanings will be obvious Program code will appear like this: int main(void) { printf("Beginning C\n"); return 0; } When a code fragment is a modified version of a previous instance, I occasionally show the lines that have changed in bold type like this: int main(void) { printf("Beginning C by Ivor Horton\n"); return 0; } When code appears in the text, it has a different typestyle that looks like this: double I’ll use different types of “brackets” in the program code They aren’t interchangeable, and their differences are very important I’ll refer to the symbols ( ) as parentheses, the symbols { } as braces, and the symbols [ ] as square brackets Important new words in the text are shown in italic like this xxix Chapter Programming in C C is a powerful and compact computer language that allows you to write programs that specify exactly what you want your computer to You’re in charge: you create a program, which is just a set of instructions, and your computer will follow them Programming in C isn’t difficult, as you’re about to find out I’m going to teach you all the fundamentals of C programming in an enjoyable and easy-to-understand way, and by the end of this chapter you’ll have written your first few C programs It’s as easy as that! In this chapter you’ll learn: • What the C language standard is • What the standard library is • How to create C programs • How C programs are organized • How to write your own program to display text on the screen The C Language C is remarkably flexible It has been used for developing just about everything you can imagine by way of a computer program, from accounting applications to word processing and from games to operating systems It is not only the basis for more advanced languages, such as C++, it is also used currently for developing mobile phone apps in the form of Objective C Objective C is standard C with a thin veneer of object-oriented programming capability added C is easy to learn because of its compactness Thus, C is an ideal first language if you have ambitions to be a programmer You’ll acquire sufficient knowledge for practical application development quickly and easily The C language is defined by an international standard, and the latest is currently defined by the document ISO/IEC 9899:2011 The current standard is commonly referred to as C11, and the language that I describe in this book conforms to C11 You need to be aware that some elements of the language as defined by C11 are optional This implies that a C compiler that conforms to the C11 standard may not implement everything in the standard (A compiler is just a program that converts your program written in terms you understand into a form your computer understands.) I will identify any language feature in the book that is optional so far as C11 is concerned, just so you are aware that it is possible that your compiler may not support it It is also possible that a C11 compiler may not implement all of the language features mandated by the C11 standard It takes time to implement new language capabilities, so compiler developers will often take an incremental approach to implementing them This provides another reason why a program may not work Having said that, I can confirm from my own experience that the most common reason for things not working in a C program, at least 99.9% of the time, is that a mistake has been made Chapter ■ Programming in C The Standard Library The standard library for C is also specified within the C11 standard The standard library defines constants, symbols, and functions that you frequently need when writing a C program It also provides some optional extensions to the basic C language Machine-dependent facilities such as input and output for your computer are implemented by the standard library in a machine-independent form This means that you write data to a disk file in C in the same way on your PC as you would on any other kind of computer, even though the underlying hardware processes are quite different The standard functionality that the library contains includes capabilities that most programmers are likely to need, such as processing text strings or math calculations This saves you an enormous amount of effort that would be required to implement such things yourself The standard library is specified in a set of standard files called header files Header files always have names with the extension h To make a particular set of standard features available in your C program file, you just include the appropriate standard header file in a way that I’ll explain later in this chapter Every program you write will make use of the standard library A summary of the header files that make up the standard library is in Appendix E Learning C If you are completely new to programming, there are some aspects of C that you not need to learn, at least not the first time around These are capabilities that are quite specialized or used relatively infrequently I have put all these together in Chapter 14 so you will learn about them when you are comfortable with the rest Although the code for all the examples is available for download from the Apress web site (http://www.apress.com), I recommend that you type in all the examples in the book, even when they are very simple Keying stuff in makes it less likely that you will forget things later Don’t be afraid to experiment with the code Making mistakes is very educational in programming The more mistakes you make early on, the more you are likely to learn Creating C Programs There are four fundamental stages, or processes, in the creation of any C program: • Editing • Compiling • Linking • Executing You’ll soon know all these processes like the back of your hand because you’ll be carrying them out so often First, I’ll explain what each process is and how it contributes to the development of your C program Editing Editing is the process of creating and modifying C source code—the name given to the program instructions you write Some C compilers come with a specific editor program that provides a lot of assistance in managing your programs In fact, an editor often provides a complete environment for writing, managing, developing, and testing your programs This is sometimes called an integrated development environment (IDE) You can also use a general-purpose text editor to create your source files, but the editor must store the code as plain text without any extra formatting data embedded in it Don’t use a word processor such as Microsoft Word; word processors aren’t suitable for producing program code because of the extra formatting information they store along with the text In general, if you have a compiler system with an editor included, it will provide a lot of features that make it easier to write and organize your source programs There will usually be automatic facilities for laying out the Chapter ■ Programming in C program text appropriately and color highlighting for important language elements, which not only makes your code more readable but also provides a clear indicator when you make errors when keying in such words If you’re working with Linux, the most common text editor is the Vim editor Alternately you might prefer to use the GNU Emacs editor With Microsoft Windows, you could use one of the many freeware and shareware programming editors These will often provide help in ensuring your code is correct, with syntax highlighting and autoindenting There is also a version of Emacs for Microsoft Windows The Vi and VIM editors from the UNIX environment are available for Windows too, and you could even use Notepad++ (http://notepad-plus-plus.org/) Of course, you can also purchase one of the professionally created programming development environments that support C, such as those from Borland or Microsoft, in which case you will have very extensive editing capabilities Before parting with your cash though, it’s a good idea to check that the level of C that is supported conforms to the current C standard, C11 With some of the products out there that are primarily aimed at C++ developers, C has been left behind somewhat Compiling The compiler converts your source code into machine language and detects and reports errors in the compilation process The input to this stage is the file you produce during your editing, which is usually referred to as a source file The compiler can detect a wide range of errors that are due to invalid or unrecognized program code, as well as structural errors where, for example, part of a program can never be executed The output from the compiler is known as object code and it is stored in files called object files, which usually have names with the extension obj in the Microsoft Windows environment, or o in the Linux/UNIX environment The compiler can detect several different kinds of errors during the translation process, and most of these will prevent the object file from being created The result of a successful compilation is a file with the same name as that used for the source file, but with the o or obj extension If you’re working in UNIX, at the command line, the standard command to compile your C programs will be cc (or the GNU’s Not UNIX [GNU] compiler, which is gcc) You can use it like this:   cc -c myprog.c   where myprog.c is the name of the source file that contains the program you want to compile Note that if you omit the -c flag, your program will automatically be linked as well The result of a successful compilation will be an object file Most C compilers will have a standard compile option, whether it’s from the command line (such as cc myprog.c) or a menu option from within an IDE (where you’ll find a Compile menu option) Compiling from within an IDE is generally much easier than using the command line Compilation is a two-stage process The first stage is called the preprocessing phase, during which your code may be modified or added to, and the second stage is the actual compilation that generates the object code Your source file can include preprocessing macros, which you use to add to or modify the C program statements Don’t worry if this doesn’t make complete sense now It will come together for you as the book progresses Linking The linker combines the object modules generated by the compiler from source code files, adds required code modules from the standard library supplied as part of C, and welds everything into an executable whole The linker also detects and reports errors; for example, if part of your program is missing or a nonexistent library component is referenced In practice, a program of any significant size will consist of several source code files, from which the compiler generates object files that need to be linked A large program may be difficult to write in one working session, and it may be impossible to work with as a single file By breaking it up into a number of smaller source files that each provide a coherent part of what the complete program does, you can make the development of the program a lot easier The source files can be compiled separately, which makes eliminating simple typographical errors a bit easier ■ Contents Working with Floating-Point Numbers����������������������������������������������������������������������������������������45 Floating-Point Number Representation��������������������������������������������������������������������������������������������������������������� 46 Floating-Point Variables��������������������������������������������������������������������������������������������������������������������������������������� 47 Division Using Floating-Point Values������������������������������������������������������������������������������������������������������������������� 48 Controlling the Number of Decimal Places in the Output������������������������������������������������������������������������������������ 49 Controlling the Output Field Width����������������������������������������������������������������������������������������������������������������������� 50 More Complicated Expressions���������������������������������������������������������������������������������������������������50 Defining Named Constants����������������������������������������������������������������������������������������������������������53 Knowing Your Limitations������������������������������������������������������������������������������������������������������������56 Introducing the sizeof Operator���������������������������������������������������������������������������������������������������59 Choosing the Correct Type for the Job����������������������������������������������������������������������������������������60 Explicit Type Conversion��������������������������������������������������������������������������������������������������������������63 Automatic Conversions���������������������������������������������������������������������������������������������������������������������������������������� 64 Rules for Implicit Conversions����������������������������������������������������������������������������������������������������������������������������� 64 Implicit Conversions in Assignment Statements������������������������������������������������������������������������������������������������� 65 More Numeric Data Types�����������������������������������������������������������������������������������������������������������66 Character Type����������������������������������������������������������������������������������������������������������������������������������������������������� 66 Character Input and Character Output����������������������������������������������������������������������������������������������������������������� 67 Enumerations������������������������������������������������������������������������������������������������������������������������������������������������������ 71 Choosing Enumerator Values������������������������������������������������������������������������������������������������������������������������������� 72 Unnamed Enumeration Types������������������������������������������������������������������������������������������������������������������������������ 73 Variables That Store Boolean Values������������������������������������������������������������������������������������������������������������������� 73 The op= Form of Assignment������������������������������������������������������������������������������������������������������74 Mathematical Functions��������������������������������������������������������������������������������������������������������������75 Designing a Program�������������������������������������������������������������������������������������������������������������������77 The Problem�������������������������������������������������������������������������������������������������������������������������������������������������������� 77 The Analysis��������������������������������������������������������������������������������������������������������������������������������������������������������� 77 The Solution��������������������������������������������������������������������������������������������������������������������������������������������������������� 79 Summary�������������������������������������������������������������������������������������������������������������������������������������83 ix ■ Contents ■ Chapter 3: Making Decisions �������������������������������������������������������������������������������������������85 The Decision-Making Process �����������������������������������������������������������������������������������������������������85 Arithmetic Comparisons �������������������������������������������������������������������������������������������������������������������������������������� 85 The Basic if Statement ���������������������������������������������������������������������������������������������������������������������������������������� 86 Extending the if statement: if-else ���������������������������������������������������������������������������������������������������������������������� 90 Using Blocks of Code in if Statements����������������������������������������������������������������������������������������������������������������� 93 Nested if Statements ������������������������������������������������������������������������������������������������������������������������������������������� 94 Testing Characters ����������������������������������������������������������������������������������������������������������������������������������������������� 97 Logical Operators ���������������������������������������������������������������������������������������������������������������������������������������������� 100 The Conditional Operator ����������������������������������������������������������������������������������������������������������������������������������� 104 Operator Precedence: Who Goes First? ������������������������������������������������������������������������������������������������������������� 107 Download from Wow! eBook Multiple-Choice Questions ��������������������������������������������������������������������������������������������������������111 Using else-if Statements for Multiple Choices �������������������������������������������������������������������������������������������������� 112 The switch Statement���������������������������������������������������������������������������������������������������������������������������������������� 112 The goto Statement ������������������������������������������������������������������������������������������������������������������������������������������� 121 Bitwise Operators ����������������������������������������������������������������������������������������������������������������������122 The op= Use of Bitwise Operators ��������������������������������������������������������������������������������������������������������������������� 125 Using Bitwise Operators ������������������������������������������������������������������������������������������������������������������������������������ 125 Designing a Program �����������������������������������������������������������������������������������������������������������������128 The Problem ������������������������������������������������������������������������������������������������������������������������������������������������������ 129 The Analysis������������������������������������������������������������������������������������������������������������������������������������������������������� 129 The Solution������������������������������������������������������������������������������������������������������������������������������������������������������� 129 Summary �����������������������������������������������������������������������������������������������������������������������������������133 ■ Chapter 4: Loops������������������������������������������������������������������������������������������������������������135 How Loops Work������������������������������������������������������������������������������������������������������������������������135 Introducing the Increment and Decrement Operators ���������������������������������������������������������������136 The for Loop ������������������������������������������������������������������������������������������������������������������������������137 General Form of the for Loop ����������������������������������������������������������������������������������������������������141 x ■ Contents More on the Increment and Decrement Operators��������������������������������������������������������������������142 The Increment Operator������������������������������������������������������������������������������������������������������������������������������������� 142 The Prefix and Postfix Forms of the Increment Operator���������������������������������������������������������������������������������� 142 The Decrement Operator����������������������������������������������������������������������������������������������������������������������������������� 143 The for Loop Revisited���������������������������������������������������������������������������������������������������������������144 Modifying the for Loop Control Variable������������������������������������������������������������������������������������������������������������ 147 A for Loop with No Parameters�������������������������������������������������������������������������������������������������������������������������� 147 The break Statement in a Loop������������������������������������������������������������������������������������������������������������������������� 148 Limiting Input Using a for Loop�������������������������������������������������������������������������������������������������������������������������� 150 Generating Pseudo-Random Integers���������������������������������������������������������������������������������������������������������������� 153 More for Loop Control Options��������������������������������������������������������������������������������������������������������������������������� 155 Floating-Point Loop Control Variables��������������������������������������������������������������������������������������������������������������� 156 The while Loop��������������������������������������������������������������������������������������������������������������������������156 Nested Loops�����������������������������������������������������������������������������������������������������������������������������159 Nested Loops and the goto Statement��������������������������������������������������������������������������������������165 The do-while Loop���������������������������������������������������������������������������������������������������������������������166 The continue Statement������������������������������������������������������������������������������������������������������������169 Designing a Program�����������������������������������������������������������������������������������������������������������������169 The Problem������������������������������������������������������������������������������������������������������������������������������������������������������ 169 The Analysis������������������������������������������������������������������������������������������������������������������������������������������������������� 169 The Solution������������������������������������������������������������������������������������������������������������������������������������������������������� 170 Summary�����������������������������������������������������������������������������������������������������������������������������������182 ■■Chapter 5: Arrays����������������������������������������������������������������������������������������������������������185 An Introduction to Arrays�����������������������������������������������������������������������������������������������������������185 Programming Without Arrays����������������������������������������������������������������������������������������������������������������������������� 186 What Is an Array?���������������������������������������������������������������������������������������������������������������������������������������������� 187 Using an Array��������������������������������������������������������������������������������������������������������������������������������������������������� 188 The Address of Operator������������������������������������������������������������������������������������������������������������192 Arrays and Addresses����������������������������������������������������������������������������������������������������������������194 Initializing an Array��������������������������������������������������������������������������������������������������������������������195 xi ■ Contents Finding the Size of an Array������������������������������������������������������������������������������������������������������196 Multidimensional Arrays������������������������������������������������������������������������������������������������������������197 Initializing Multidimensional Arrays������������������������������������������������������������������������������������������199 Variable Length Arrays��������������������������������������������������������������������������������������������������������������205 Designing a Program�����������������������������������������������������������������������������������������������������������������208 The Problem������������������������������������������������������������������������������������������������������������������������������������������������������ 208 The Analysis������������������������������������������������������������������������������������������������������������������������������������������������������� 208 The Solution������������������������������������������������������������������������������������������������������������������������������������������������������� 210 Summary�����������������������������������������������������������������������������������������������������������������������������������216 ■■Chapter 6: Applications with Strings and Text��������������������������������������������������������������219 What Is a String?�����������������������������������������������������������������������������������������������������������������������219 Variables That Store Strings������������������������������������������������������������������������������������������������������221 Arrays of Strings������������������������������������������������������������������������������������������������������������������������224 Operations with Strings�������������������������������������������������������������������������������������������������������������227 Checking for C11 Support���������������������������������������������������������������������������������������������������������������������������������� 227 Finding the Length of a String��������������������������������������������������������������������������������������������������������������������������� 228 Copying Strings������������������������������������������������������������������������������������������������������������������������������������������������� 229 Concatenating Strings��������������������������������������������������������������������������������������������������������������������������������������� 229 Comparing Strings��������������������������������������������������������������������������������������������������������������������������������������������� 233 Searching a String��������������������������������������������������������������������������������������������������������������������������������������������� 237 Tokenizing a String�������������������������������������������������������������������������������������������������������������������������������������������� 242 Reading Newline Characters into a String��������������������������������������������������������������������������������������������������������� 247 Analyzing and Transforming Strings������������������������������������������������������������������������������������������248 Converting Character Case�������������������������������������������������������������������������������������������������������������������������������� 250 Converting Strings to Numerical Values������������������������������������������������������������������������������������������������������������ 253 Designing a Program�����������������������������������������������������������������������������������������������������������������255 The Problem������������������������������������������������������������������������������������������������������������������������������������������������������ 255 The Analysis������������������������������������������������������������������������������������������������������������������������������������������������������� 255 The Solution������������������������������������������������������������������������������������������������������������������������������������������������������� 256 Summary�����������������������������������������������������������������������������������������������������������������������������������261 xii ■ Contents ■■Chapter 7: Pointers��������������������������������������������������������������������������������������������������������263 A First Look at Pointers�������������������������������������������������������������������������������������������������������������263 Declaring Pointers��������������������������������������������������������������������������������������������������������������������������������������������� 264 Accessing a Value Through a Pointer���������������������������������������������������������������������������������������������������������������� 265 Using Pointers��������������������������������������������������������������������������������������������������������������������������������������������������� 269 Testing for a NULL Pointer��������������������������������������������������������������������������������������������������������������������������������� 273 Pointers to Constants���������������������������������������������������������������������������������������������������������������������������������������� 273 Constant Pointers���������������������������������������������������������������������������������������������������������������������������������������������� 274 Naming Pointers������������������������������������������������������������������������������������������������������������������������������������������������ 275 Arrays and Pointers�������������������������������������������������������������������������������������������������������������������275 Multidimensional Arrays������������������������������������������������������������������������������������������������������������279 Multidimensional Arrays and Pointers��������������������������������������������������������������������������������������������������������������� 283 Accessing Array Elements��������������������������������������������������������������������������������������������������������������������������������� 284 Using Memory As You Go�����������������������������������������������������������������������������������������������������������287 Dynamic Memory Allocation: The malloc() Function������������������������������������������������������������������������������������������ 288 Releasing Dynamically Allocated Memory��������������������������������������������������������������������������������������������������������� 289 Memory Allocation with the calloc() Function��������������������������������������������������������������������������������������������������� 294 Extending Dynamically Allocated Memory�������������������������������������������������������������������������������������������������������� 294 Handling Strings Using Pointers������������������������������������������������������������������������������������������������298 Using Arrays of Pointers������������������������������������������������������������������������������������������������������������������������������������ 299 Pointers and Array Notation������������������������������������������������������������������������������������������������������������������������������� 305 Designing a Program�����������������������������������������������������������������������������������������������������������������310 The Problem������������������������������������������������������������������������������������������������������������������������������������������������������ 310 The Analysis������������������������������������������������������������������������������������������������������������������������������������������������������� 311 The Solution������������������������������������������������������������������������������������������������������������������������������������������������������� 311 The Complete Program�������������������������������������������������������������������������������������������������������������������������������������� 316 Summary�����������������������������������������������������������������������������������������������������������������������������������319 ■■Chapter 8: Structuring Your Programs��������������������������������������������������������������������������321 Program Structure���������������������������������������������������������������������������������������������������������������������321 Variable Scope and Lifetime������������������������������������������������������������������������������������������������������������������������������ 322 Variable Scope and Functions��������������������������������������������������������������������������������������������������������������������������� 325 xiii ■ Contents Functions�����������������������������������������������������������������������������������������������������������������������������������326 Defining a Function������������������������������������������������������������������������������������������������������������������������������������������� 326 The return Statement���������������������������������������������������������������������������������������������������������������������������������������� 330 The Pass-By-Value Mechanism�������������������������������������������������������������������������������������������������334 Function Prototypes������������������������������������������������������������������������������������������������������������������335 Pointers as Parameters and Return Types��������������������������������������������������������������������������������337 const Parameters���������������������������������������������������������������������������������������������������������������������������������������������� 337 Perils of Returning Pointers������������������������������������������������������������������������������������������������������������������������������� 343 Summary�����������������������������������������������������������������������������������������������������������������������������������346 ■■Chapter 9: More on Functions���������������������������������������������������������������������������������������349 Pointers to Functions����������������������������������������������������������������������������������������������������������������349 Declaring a Pointer to a Function���������������������������������������������������������������������������������������������������������������������� 349 Calling a Function Through a Function Pointer�������������������������������������������������������������������������������������������������� 350 Arrays of Pointers to Functions������������������������������������������������������������������������������������������������������������������������� 353 Pointers to Functions As Arguments������������������������������������������������������������������������������������������������������������������ 355 Variables in Functions���������������������������������������������������������������������������������������������������������������358 Static Variables: Keeping Track Within a Function��������������������������������������������������������������������������������������������� 359 Sharing Variables Between Functions��������������������������������������������������������������������������������������������������������������� 361 Functions That Call Themselves: Recursion������������������������������������������������������������������������������363 Functions with a Variable Number of Arguments����������������������������������������������������������������������366 Copying a va_list����������������������������������������������������������������������������������������������������������������������������������������������� 369 Basic Rules for Variable-Length Argument Lists����������������������������������������������������������������������������������������������� 369 The main() Function������������������������������������������������������������������������������������������������������������������370 Terminating a Program��������������������������������������������������������������������������������������������������������������371 The abort() Function������������������������������������������������������������������������������������������������������������������������������������������ 372 The exit() and atexit() Functions������������������������������������������������������������������������������������������������������������������������ 372 The _Exit() Function������������������������������������������������������������������������������������������������������������������������������������������� 372 The quick_exit() and at_quick_exit() Functions������������������������������������������������������������������������������������������������� 373 xiv ■ Contents Enhancing Performance������������������������������������������������������������������������������������������������������������373 Declaring Functions Inline��������������������������������������������������������������������������������������������������������������������������������� 373 Using the restrict Keyword�������������������������������������������������������������������������������������������������������������������������������� 374 The _Noreturn Function Specifier��������������������������������������������������������������������������������������������������������������������� 374 Designing a Program�����������������������������������������������������������������������������������������������������������������374 The Problem������������������������������������������������������������������������������������������������������������������������������������������������������ 375 The Analysis������������������������������������������������������������������������������������������������������������������������������������������������������� 376 The Solution������������������������������������������������������������������������������������������������������������������������������������������������������� 377 Summary�����������������������������������������������������������������������������������������������������������������������������������392 ■■Chapter 10: Essential Input and Output�������������������������������������������������������������������������395 Input and Output Streams���������������������������������������������������������������������������������������������������������395 Standard Streams���������������������������������������������������������������������������������������������������������������������396 Input from the Keyboard������������������������������������������������������������������������������������������������������������396 Formatted Keyboard Input��������������������������������������������������������������������������������������������������������������������������������� 397 Input Format Control Strings����������������������������������������������������������������������������������������������������������������������������� 397 Characters in the Input Format String��������������������������������������������������������������������������������������������������������������� 403 Variations on Floating-Point Input��������������������������������������������������������������������������������������������������������������������� 404 Reading Hexadecimal and Octal Values������������������������������������������������������������������������������������������������������������ 406 Reading Characters Using scanf_s()����������������������������������������������������������������������������������������������������������������� 408 String Input from the Keyboard������������������������������������������������������������������������������������������������������������������������� 410 Single Character Keyboard Input����������������������������������������������������������������������������������������������������������������������� 411 Output to the Screen�����������������������������������������������������������������������������������������������������������������416 Formatted Output Using printf_s()��������������������������������������������������������������������������������������������������������������������� 416 Escape Sequences�������������������������������������������������������������������������������������������������������������������������������������������� 419 Integer Output���������������������������������������������������������������������������������������������������������������������������������������������������� 419 Outputting Floating-Point Values����������������������������������������������������������������������������������������������������������������������� 422 Character Output����������������������������������������������������������������������������������������������������������������������������������������������� 423 xv ■ Contents Other Output Functions�������������������������������������������������������������������������������������������������������������425 Unformatted Output to the Screen��������������������������������������������������������������������������������������������������������������������� 425 Formatted Output to an Array���������������������������������������������������������������������������������������������������������������������������� 426 Formatted Input from an Array�������������������������������������������������������������������������������������������������������������������������� 426 Summary�����������������������������������������������������������������������������������������������������������������������������������427 ■■Chapter 11: Structuring Data�����������������������������������������������������������������������������������������429 Data Structures: Using struct����������������������������������������������������������������������������������������������������429 Defining Structure Types and Structure Variables��������������������������������������������������������������������������������������������� 431 Accessing Structure Members�������������������������������������������������������������������������������������������������������������������������� 432 Unnamed Structures������������������������������������������������������������������������������������������������������������������������������������������ 435 Arrays of Structures������������������������������������������������������������������������������������������������������������������������������������������� 435 Structure Members in Expressions������������������������������������������������������������������������������������������������������������������� 438 Pointers to Structures���������������������������������������������������������������������������������������������������������������������������������������� 439 Dynamic Memory Allocation for Structures������������������������������������������������������������������������������������������������������� 439 More on Structure Members�����������������������������������������������������������������������������������������������������442 Structures As Members of a Structure�������������������������������������������������������������������������������������������������������������� 442 Declaring a Structure Within a Structure����������������������������������������������������������������������������������������������������������� 444 Pointers to Structures As Structure Members��������������������������������������������������������������������������������������������������� 444 Doubly Linked Lists������������������������������������������������������������������������������������������������������������������������������������������� 449 Bit Fields in a Structure������������������������������������������������������������������������������������������������������������������������������������� 453 Structures and Functions����������������������������������������������������������������������������������������������������������454 Structures As Arguments to Functions�������������������������������������������������������������������������������������������������������������� 454 Pointers to Structures As Function Arguments�������������������������������������������������������������������������������������������������� 455 Structure As a Function Return Value���������������������������������������������������������������������������������������������������������������� 456 Binary Trees������������������������������������������������������������������������������������������������������������������������������������������������������� 461 Sharing Memory������������������������������������������������������������������������������������������������������������������������470 Designing a Program�����������������������������������������������������������������������������������������������������������������474 The Problem������������������������������������������������������������������������������������������������������������������������������������������������������ 474 The Analysis������������������������������������������������������������������������������������������������������������������������������������������������������� 474 The Solution������������������������������������������������������������������������������������������������������������������������������������������������������� 475 Summary�����������������������������������������������������������������������������������������������������������������������������������487 xvi ■ Contents ■■Chapter 12: Working with Files�������������������������������������������������������������������������������������489 The Concept of a File�����������������������������������������������������������������������������������������������������������������489 Positions in a File���������������������������������������������������������������������������������������������������������������������������������������������� 490 File Streams������������������������������������������������������������������������������������������������������������������������������������������������������ 490 Accessing Files�������������������������������������������������������������������������������������������������������������������������490 Opening a File���������������������������������������������������������������������������������������������������������������������������������������������������� 491 Buffering File Operations����������������������������������������������������������������������������������������������������������������������������������� 493 Renaming a File������������������������������������������������������������������������������������������������������������������������������������������������� 494 Closing a File����������������������������������������������������������������������������������������������������������������������������������������������������� 495 Deleting a File���������������������������������������������������������������������������������������������������������������������������������������������������� 496 Writing a Text File����������������������������������������������������������������������������������������������������������������������496 Reading a Text File��������������������������������������������������������������������������������������������������������������������497 Reading and Writing Strings to a Text File���������������������������������������������������������������������������������501 Formatted File Input and Output�����������������������������������������������������������������������������������������������505 Formatted Output to a File��������������������������������������������������������������������������������������������������������������������������������� 506 Formatted Input from a File������������������������������������������������������������������������������������������������������������������������������� 506 Dealing with Errors��������������������������������������������������������������������������������������������������������������������509 More Open Modes for Text Files������������������������������������������������������������������������������������������������510 The freopen_s() Function����������������������������������������������������������������������������������������������������������511 Binary File Input and Output������������������������������������������������������������������������������������������������������511 Opening a File in Binary Mode��������������������������������������������������������������������������������������������������������������������������� 512 Writing a Binary File������������������������������������������������������������������������������������������������������������������������������������������ 513 Reading a Binary File����������������������������������������������������������������������������������������������������������������������������������������� 513 Moving Around in a File�������������������������������������������������������������������������������������������������������������520 File Positioning Operations�������������������������������������������������������������������������������������������������������������������������������� 520 Finding Out Where You Are�������������������������������������������������������������������������������������������������������������������������������� 520 Setting a Position in a File��������������������������������������������������������������������������������������������������������������������������������� 521 Using Temporary Work Files������������������������������������������������������������������������������������������������������528 Creating a Temporary Work File������������������������������������������������������������������������������������������������������������������������� 528 Creating a Unique File Name����������������������������������������������������������������������������������������������������������������������������� 529 xvii ■ Contents Updating Binary Files����������������������������������������������������������������������������������������������������������������530 Changing the Contents of a File������������������������������������������������������������������������������������������������������������������������� 535 Creating a Record from Keyboard Input������������������������������������������������������������������������������������������������������������ 537 Writing a Record to a File���������������������������������������������������������������������������������������������������������������������������������� 538 Reading a Record from a File���������������������������������������������������������������������������������������������������������������������������� 539 Writing a File����������������������������������������������������������������������������������������������������������������������������������������������������� 539 Listing the File Contents������������������������������������������������������������������������������������������������������������������������������������ 540 Updating the Existing File Contents������������������������������������������������������������������������������������������������������������������� 541 File Open Modes Summary�������������������������������������������������������������������������������������������������������548 Designing a Program�����������������������������������������������������������������������������������������������������������������549 The Problem������������������������������������������������������������������������������������������������������������������������������������������������������ 549 The Analysis������������������������������������������������������������������������������������������������������������������������������������������������������� 549 The Solution������������������������������������������������������������������������������������������������������������������������������������������������������� 549 Summary�����������������������������������������������������������������������������������������������������������������������������������555 ■■Chapter 13: The Preprocessor and Debugging��������������������������������������������������������������557 Preprocessing���������������������������������������������������������������������������������������������������������������������������557 Including Header Files��������������������������������������������������������������������������������������������������������������������������������������� 557 Defining Your Own Header Files������������������������������������������������������������������������������������������������������������������������ 558 Managing Multiple Source Files������������������������������������������������������������������������������������������������������������������������ 558 External Variables���������������������������������������������������������������������������������������������������������������������������������������������� 559 Static Functions������������������������������������������������������������������������������������������������������������������������������������������������� 559 Substitutions in Your Program Source Code������������������������������������������������������������������������������������������������������ 560 Macros��������������������������������������������������������������������������������������������������������������������������������������561 Macros That Look Like Functions���������������������������������������������������������������������������������������������������������������������� 561 Strings As Macro Arguments����������������������������������������������������������������������������������������������������������������������������� 563 Joining Two Arguments in a Macro Expansion�������������������������������������������������������������������������������������������������� 565 Preprocessor Directives on Multiple Lines��������������������������������������������������������������������������������565 Logical Preprocessor Directives������������������������������������������������������������������������������������������������������������������������ 565 Conditional Compilation������������������������������������������������������������������������������������������������������������������������������������� 566 Testing for Multiple Conditions�������������������������������������������������������������������������������������������������������������������������� 567 Undefining Identifiers���������������������������������������������������������������������������������������������������������������������������������������� 567 xviii ■ Contents Testing for Specific Values for Identifiers���������������������������������������������������������������������������������������������������������� 567 Multiple-Choice Selections�������������������������������������������������������������������������������������������������������������������������������� 568 Standard Preprocessing Macros����������������������������������������������������������������������������������������������������������������������� 568 Debugging Methods������������������������������������������������������������������������������������������������������������������569 Integrated Debuggers���������������������������������������������������������������������������������������������������������������������������������������� 570 The Preprocessor in Debugging������������������������������������������������������������������������������������������������������������������������ 570 Assertions���������������������������������������������������������������������������������������������������������������������������������������������������������� 575 Date and Time Functions�����������������������������������������������������������������������������������������������������������577 Getting Time Values������������������������������������������������������������������������������������������������������������������������������������������� 577 Getting the Date������������������������������������������������������������������������������������������������������������������������������������������������� 581 Getting the Day for a Date��������������������������������������������������������������������������������������������������������������������������������� 585 Summary�����������������������������������������������������������������������������������������������������������������������������������588 ■■Chapter 14: Advanced and Specialized Topics��������������������������������������������������������������589 Working with International Character Sets�������������������������������������������������������������������������������589 Understanding Unicode������������������������������������������������������������������������������������������������������������������������������������� 589 Setting the Locale���������������������������������������������������������������������������������������������������������������������������������������������� 590 The Wide Character Type wchar_t��������������������������������������������������������������������������������������������������������������������� 591 Operations on Wide Character Strings��������������������������������������������������������������������������������������������������������������� 594 File Stream Operations with Wide Characters��������������������������������������������������������������������������������������������������� 598 Fixed Size Types That Store Unicode Characters����������������������������������������������������������������������������������������������� 598 Specialized Integer Types for Portability�����������������������������������������������������������������������������������602 Fixed Width Integer Types���������������������������������������������������������������������������������������������������������������������������������� 602 Minimum Width Integer Types��������������������������������������������������������������������������������������������������������������������������� 603 Maximum Width Integer Types�������������������������������������������������������������������������������������������������������������������������� 603 The Complex Number Types������������������������������������������������������������������������������������������������������603 Complex Number Basics����������������������������������������������������������������������������������������������������������������������������������� 604 Complex Types and Operations�������������������������������������������������������������������������������������������������������������������������� 605 Programming with Threads�������������������������������������������������������������������������������������������������������607 Creating a Thread���������������������������������������������������������������������������������������������������������������������������������������������� 608 Exiting a Thread������������������������������������������������������������������������������������������������������������������������������������������������� 609 Joining One Thread to Another�������������������������������������������������������������������������������������������������������������������������� 609 xix ■ Contents Suspending a Thread � 613 Managing Thread Access to Data � 613 Summary � 620 ■Appendix A: Computer Arithmetic � �����������������������������������������������������621 Binary Numbers � 621 Hexadecimal Numbers � 622 Negative Binary Numbers � 624 Big-Endian and Little-Endian Systems � 625 Floating-Point Numbers � 626 Download from Wow! eBook ■Appendix B: ASCII Character Code Definitions ��������������������������������������������������������������629 ■Appendix C: Reserved Words in C����������������������������������������������������������������������������������635 ■Appendix D: Input and Output Format Specifications�������637 Output Format Specifications � 637 Input Format Specifications � 639 ■Appendix E: Standard Library Header Files �������������������������������������������������������������������643 Index ���������������������������������������������������������������������������������������������������������������������������������645 xx About the Author Ivor Horton graduated as a mathematician and was lured into information technology by promises of great rewards for very little work In spite of the reality usually being a great deal of work for relatively modest rewards, he has continued to work with computers to the present day He has been engaged at various times in programming, systems design, consultancy, and the management and implementation of projects of considerable complexity Ivor has many years of experience in the design and implementation of computer systems applied to engineering design and manufacturing operations in a variety of industries He has also spent a lot of time developing occasionally useful applications in a wide variety of programming languages and primarily teaching scientists and engineers to likewise He has been writing books on programming for many years, and his currently published works include tutorials on C, C++, and Java At the present time, when he is not writing programming books or providing advice to others, he spends his time fishing, traveling, and enjoying life in general xxi About the Technical Reviewer Marc Gregoire is a software engineer from Belgium He graduated from the Catholic University of Leuven, Belgium, with a degree in “Burgerlijk ingenieur in de computer wetenschappen” (equivalent to master of science in engineering in computer science) The year after, he received the cum laude degree of master in artificial intelligence at the same university After his studies, Marc started working for a software consultancy company called Ordina Belgium As a consultant, he worked for Siemens and Nokia Siemens Networks on critical 2G and 3G software running on Solaris for telecom operators This required working in international teams spanning from South America and the United States to Europe, the Middle East, Africa, and Asia Now, Marc is working for Nikon Metrology on 3D laser scanning software His main expertise is C/C++, specifically Microsoft VC++ and the MFC framework Next to C/C++, Marc also likes C# and uses PHP for creating web pages In addition to his main interest of Windows development, he also has experience in developing C++ programs running 24/7 on Linux platforms (e.g., EIB home automation software) Since April 2007, he has received the yearly Microsoft MVP (Most Valuable Professional) award for his Visual C++ expertise Marc is the founder of the Belgian C++ Users Group (www.becpp.org) and an active member on the CodeGuru forum (as Marc G) He also creates freeware and shareware programs that are distributed through his web site at www.nuonsoft.com, and maintains a blog on www.nuonsoft.com/blog/ xxiii Acknowledgments The author is only one member of the large team of people necessary to get a book into print I thank the entire Apress editorial and production teams for their help and support throughout I would like to acknowledge the efforts of Jonathan Gennick, who initiated this new edition, and Jill Balzano, who has patiently dealt with my questions and problems in the editing process I would also like to thank my technical editor, Marc Gregoire, for doing such a fantastic job of reviewing the text and checking out all the code fragments and examples He has an uncanny knack for finding my errors, and his many constructive comments and thoughtful suggestions have undoubtedly made the book a much better tutorial xxv ... altered the source code With a following wind and a bit of luck you, have now edited your first program You’ve written a program using the editor, edited it, and compiled, linked, and executed it. .. [GNU] compiler, which is gcc) You can use it like this:   cc -c myprog .c   where myprog .c is the name of the source file that contains the program you want to compile Note that if you omit the -c. .. along better if your editor is designed for editing C code I can suggest two sources for a suitable C compiler, both of which are freeware: xxviii • The GNU C compiler, GCC, is available from

Ngày đăng: 05/11/2019, 15:53

Từ khóa liên quan

Mục lục

  • Beginning C

    • Contents at a Glance

    • Contents

    • About the Author

    • About the Technical Reviewer

    • Acknowledgments

    • Introduction

    • Chapter 1: Programming in C

      • The C Language

      • The Standard Library

      • Learning C

      • Creating C Programs

        • Editing

        • Compiling

        • Linking

        • Executing

      • Creating Your First Program

      • Editing Your First Program

      • Dealing with Errors

      • Dissecting a Simple Program

        • Comments

        • Preprocessing Directives

        • Defining the main() Function

        • Keywords

        • The Body of a Function

        • Outputting Information

        • Function Arguments

        • Control Characters

        • Trigraph Sequences

      • The Preprocessor

      • Developing Programs in C

        • Understanding the Problem

        • Detailed Design

        • Implementation

        • Testing

      • Functions and Modular Programming

      • Common Mistakes

      • Points to Remember

      • Summary

    • Chapter 2: First Steps in Programming

      • Memory in Your Computer

      • What Is a Variable?

        • Naming Variables

        • Variables That Store Integers

        • Using Variables

        • Initializing Variables

      • Basic Arithmetic Operations

        • More on Division with Integers

        • Unary Operators

        • Unary Minus Operator

      • Variables and Memory

        • Signed Integer Types

        • Unsigned Integer Types

        • Specifying Integer Constants

          • Hexadecimal Constants

          • Octal Constants

          • Default Integer Constant Types

      • Working with Floating-Point Numbers

        • Floating-Point Number Representation

        • Floating-Point Variables

        • Division Using Floating-Point Values

        • Controlling the Number of Decimal Places in the Output

        • Controlling the Output Field Width

      • More Complicated Expressions

      • Defining Named Constants

      • Knowing Your Limitations

      • Introducing the sizeof Operator

      • Choosing the Correct Type for the Job

      • Explicit Type Conversion

        • Automatic Conversions

        • Rules for Implicit Conversions

        • Implicit Conversions in Assignment Statements

      • More Numeric Data Types

        • Character Type

        • Character Input and Character Output

        • Enumerations

        • Choosing Enumerator Values

        • Unnamed Enumeration Types

        • Variables That Store Boolean Values

      • The op= Form of Assignment

      • Mathematical Functions

      • Designing a Program

        • The Problem

        • The Analysis

        • The Solution

          • Step 1

          • Step 2

          • Step 3

      • Summary

    • Chapter 3: Making Decisions

      • The Decision-Making Process

        • Arithmetic Comparisons

        • The Basic if Statement

        • Extending the if statement: if-else

        • Using Blocks of Code in if Statements

        • Nested if Statements

        • Testing Characters

        • Logical Operators

          • The AND Operator &&

          • The OR Operator ||

          • The NOT Operator !

        • The Conditional Operator

        • Operator Precedence : Who Goes First?

      • Multiple-Choice Questions

        • Using else-if Statements for Multiple Choices

        • The switch Statement

        • The goto Statement

      • Bitwise Operators

        • The op= Use of Bitwise Operators

        • Using Bitwise Operators

      • Designing a Program

        • The Problem

        • The Analysis

        • The Solution

          • Step 1

          • Step 2

          • Steps 3 and 4

      • Summary

    • Chapter 4: Loops

      • How Loops Work

      • Introducing the Increment and Decrement Operators

      • The for Loop

      • General Form of the for Loop

      • More on the Increment and Decrement Operators

        • The Increment Operator

        • The Prefix and Postfix Forms of the Increment Operator

        • The Decrement Operator

      • The for Loop Revisited

        • Modifying the for Loop Control Variable

        • A for Loop with No Parameters

        • The break Statement in a Loop

        • Limiting Input Using a for Loop

        • Generating Pseudo-Random Integers

        • More for Loop Control Options

        • Floating-Point Loop Control Variables

      • The while Loop

      • Nested Loops

      • Nested Loops and the goto Statement

      • The do-while Loop

      • The continue Statement

      • Designing a Program

        • The Problem

        • The Analysis

        • The Solution

          • Step 1

          • Step 2

          • Step 3

          • Step 4

          • Step 5

      • Summary

    • Chapter 5: Arrays

      • An Introduction to Arrays

        • Programming Without Arrays

        • What Is an Array?

        • Using an Array

      • The Address of Operator

      • Arrays and Addresses

      • Initializing an Array

      • Finding the Size of an Array

      • Multidimensional Arrays

      • Initializing Multidimensional Arrays

      • Variable Length Arrays

      • Designing a Program

        • The Problem

        • The Analysis

        • The Solution

          • Step 1

          • Step 2

          • Step 3

          • Step 4

      • Summary

    • Chapter 6: Applications with Strings and Text

      • What Is a String?

      • Variables That Store Strings

      • Arrays of Strings

      • Operations with Strings

        • Checking for C11 Support

        • Finding the Length of a String

        • Copying Strings

        • Concatenating Strings

        • Comparing Strings

        • Searching a String

          • The Idea of a Pointer

          • Searching a String for a Character

          • Searching a String for a Substring

        • Tokenizing a String

        • Reading Newline Characters into a String

      • Analyzing and Transforming Strings

        • Converting Character Case

        • Converting Strings to Numerical Values

      • Designing a Program

        • The Problem

        • The Analysis

        • The Solution

          • Step 1

          • Step 2

          • Step 3

      • Summary

    • Chapter 7: Pointers

      • A First Look at Pointers

        • Declaring Pointers

        • Accessing a Value Through a Pointer

        • Using Pointers

        • Testing for a NULL Pointer

        • Pointers to Constants

        • Constant Pointers

        • Naming Pointers

      • Arrays and Pointers

      • Multidimensional Arrays

        • Multidimensional Arrays and Pointers

        • Accessing Array Elements

      • Using Memory As You Go

        • Dynamic Memory Allocation: The malloc() Function

        • Releasing Dynamically Allocated Memory

        • Memory Allocation with the calloc() Function

        • Extending Dynamically Allocated Memory

      • Handling Strings Using Pointers

        • Using Arrays of Pointers

        • Pointers and Array Notation

      • Designing a Program

        • The Problem

        • The Analysis

        • The Solution

          • Step 1

          • Step 2

          • Step 3

          • Step 4

          • Step 5

          • Step 6

          • Step 7

          • Step 8

        • The Complete Program

      • Summary

    • Chapter 8: Structuring Your Programs

      • Program Structure

        • Variable Scope and Lifetime

        • Variable Scope and Functions

      • Functions

        • Defining a Function

          • Naming a Function

          • Function Parameters

          • Specifying the Return Value Type

        • The return Statement

      • The Pass-By-Value Mechanism

      • Function Prototypes

      • Pointers as Parameters and Return Types

        • const Parameters

        • Perils of Returning Pointers

      • Summary

    • Chapter 9: More on Functions

      • Pointers to Functions

        • Declaring a Pointer to a Function

        • Calling a Function Through a Function Pointer

        • Arrays of Pointers to Functions

        • Pointers to Functions As Arguments

      • Variables in Functions

        • Static Variables: Keeping Track Within a Function

        • Sharing Variables Between Functions

      • Functions That Call Themselves: Recursion

      • Functions with a Variable Number of Arguments

        • Copying a va_list

        • Basic Rules for Variable-Length Argument Lists

      • The main() Function

      • Terminating a Program

        • The abort() Function

        • The exit() and atexit() Functions

        • The _Exit() Function

        • The quick_exit() and at_quick_exit() Functions

      • Enhancing Performance

        • Declaring Functions Inline

        • Using the restrict Keyword

        • The _Noreturn Function Specifier

      • Designing a Program

        • The Problem

        • The Analysis

        • The Solution

          • Step 1

          • Step 2

          • Step 3

          • Step 4

      • Summary

    • Chapter 10: Essential Input and Output

      • Input and Output Streams

      • Standard Streams

      • Input from the Keyboard

        • Formatted Keyboard Input

        • Input Format Control Strings

        • Characters in the Input Format String

        • Variations on Floating-Point Input

        • Reading Hexadecimal and Octal Values

        • Reading Characters Using scanf_s()

        • String Input from the Keyboard

        • Single Character Keyboard Input

      • Output to the Screen

        • Formatted Output Using printf_s()

        • Escape Sequences

        • Integer Output

        • Outputting Floating-Point Values

        • Character Output

      • Other Output Functions

        • Unformatted Output to the Screen

        • Formatted Output to an Array

        • Formatted Input from an Array

      • Summary

    • Chapter 11: Structuring Data

      • Data Structures: Using struct

        • Defining Structure Types and Structure Variables

        • Accessing Structure Members

        • Unnamed Structures

        • Arrays of Structures

        • Structure Members in Expressions

        • Pointers to Structures

        • Dynamic Memory Allocation for Structures

      • More on Structure Members

        • Structures As Members of a Structure

        • Declaring a Structure Within a Structure

        • Pointers to Structures As Structure Members

        • Doubly Linked Lists

        • Bit Fields in a Structure

      • Structures and Functions

        • Structures As Arguments to Functions

        • Pointers to Structures As Function Arguments

        • Structure As a Function Return Value

        • Binary Trees

          • Ordering Data in a Binary Tree

          • Constructing a Binary Tree

          • Traversing a Binary Tree

      • Sharing Memory

      • Designing a Program

        • The Problem

        • The Analysis

        • The Solution

          • Step 1

          • Step 2

          • Step 3

          • Step 4

      • Summary

    • Chapter 12: Working with Files

      • The Concept of a File

        • Positions in a File

        • File Streams

      • Accessing Files

        • Opening a File

          • Write Mode

          • Append Mode

          • Read Mode

        • Buffering File Operations

        • Renaming a File

        • Closing a File

        • Deleting a File

      • Writing a Text File

      • Reading a Text File

      • Reading and Writing Strings to a Text File

      • Formatted File Input and Output

        • Formatted Output to a File

        • Formatted Input from a File

      • Dealing with Errors

      • More Open Modes for Text Files

      • The freopen_s() Function

      • Binary File Input and Output

        • Opening a File in Binary Mode

        • Writing a Binary File

        • Reading a Binary File

      • Moving Around in a File

        • File Positioning Operations

        • Finding Out Where You Are

        • Setting a Position in a File

      • Using Temporary Work Files

        • Creating a Temporary Work File

        • Creating a Unique File Name

      • Updating Binary Files

        • Changing the Contents of a File

        • Creating a Record from Keyboard Input

        • Writing a Record to a File

        • Reading a Record from a File

        • Writing a File

        • Listing the File Contents

        • Updating the Existing File Contents

      • File Open Modes Summary

      • Designing a Program

        • The Problem

        • The Analysis

        • The Solution

          • Step 1

          • Step 2

          • Step 3

      • Summary

    • Chapter 13: The Preprocessor and Debugging

      • Preprocessing

        • Including Header Files

        • Defining Your Own Header Files

        • Managing Multiple Source Files

        • External Variables

        • Static Functions

        • Substitutions in Your Program Source Code

      • Macros

        • Macros That Look Like Functions

        • Strings As Macro Arguments

        • Joining Two Arguments in a Macro Expansion

      • Preprocessor Directives on Multiple Lines

        • Logical Preprocessor Directives

        • Conditional Compilation

        • Testing for Multiple Conditions

        • Undefining Identifiers

        • Testing for Specific Values for Identifiers

        • Multiple-Choice Selections

        • Standard Preprocessing Macros

      • Debugging Methods

        • Integrated Debuggers

        • The Preprocessor in Debugging

        • Assertions

          • Runtime Assertions

          • Switching Off Assertions

          • Compile-Time Assertions

      • Date and Time Functions

        • Getting Time Values

        • Getting the Date

        • Getting the Day for a Date

      • Summary

    • Chapter 14: Advanced and Specialized Topics

      • Working with International Character Sets

        • Understanding Unicode

        • Setting the Locale

        • The Wide Character Type wchar_t

          • Storing Wide Characters

          • Operations on Wide Characters

          • Working with Wide Character Strings

        • Operations on Wide Character Strings

        • File Stream Operations with Wide Characters

        • Fixed Size Types That Store Unicode Characters

      • Specialized Integer Types for Portability

        • Fixed Width Integer Types

        • Minimum Width Integer Types

        • Maximum Width Integer Types

      • The Complex Number Types

        • Complex Number Basics

        • Complex Types and Operations

      • Programming with Threads

        • Creating a Thread

        • Exiting a Thread

        • Joining One Thread to Another

        • Suspending a Thread

        • Managing Thread Access to Data

          • Creating a Mutex

          • Acquiring a Mutex

          • Releasing a Mutex

      • Summary

    • Appendix A: Computer Arithmetic

      • Binary Numbers

      • Hexadecimal Numbers

      • Negative Binary Numbers

      • Big-Endian and Little-Endian Systems

      • Floating-Point Numbers

    • Appendix B: ASCII Character Code Definitions

    • Appendix C: Reserved Words in C

    • Appendix D: Input and Output Format Specifications

      • Output Format Specifications

      • Input Format Specifications

    • Appendix E: Standard Library Header Files

    • Index

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan