C++ primer pdf download






















Second Edition. Brian W. Kernighan 0 Dennis M. Murray Hill, New Jersey. Copyright issues : The pdf version of this book is made available here for download for academic purpose only, i. Both these books can be used by beginners as course books or for reference and tutorials. Thursday, December 16, Code with C. Please enter your comment! Although you do not need to know C in order to understand this book, we assume you know enough about programming to write, compile, and run a program in at least one modern block-structured language.

Some of these capabilities are of great import for large project teams but might not be necessary for smaller efforts. As a result, not every programmer needs to know every detail of every feature. The topics covered in sections marked this way form the core part of the language.

Everyone should read and understand these sections. These sections can be skipped or skimmed on a first reading. It is probably a good idea to skim such sections so you know that the capability exists.

However, there is no reason to spend time studying these topics until you actually need to use the feature in your own programs.

We hope that readers will take the time to understand thoroughly the material presented in the sections so marked.

Another aid to reading this book, is our extensive use of cross-references. We hope these references will make it easier for readers to dip into the middle of the book, yet easily jump back to the earlier material on which later examples rely.

These parts cover enough material to let you, the reader, write significant programs. In Part III, we cover issues of copy control, along with other techniques to make classes that are as easy to use as the built-in types. Classes are the foundation for object-oriented and generic programming, which we also cover in Part III. We also summarize the library algorithms in Appendix A.

Readers should use these sections as a personal checklist: If you do not understand a term, restudy the corresponding part of the chapter.

To that end, the Primer provides extensive examples throughout the text. The compiler we use most frequently is the GNU compiler, version 4. There are only a few features used in this book that this compiler does not yet implement: inheriting constructors, reference qualifiers for member functions, and the regular-expression library. Acknowledgments In preparing this edition we are very grateful for the help of several current and former members of the standardization committee: Dave Abrahams, Andy Koenig, Stephan T.

They provided invaluable assistance to us in understanding some of the more subtle parts of the new standard. We extend our deep-felt thanks to our reviewers, whose helpful comments led us to make improvements great and small throughout the book: Marshall Clow, Jon Kalb, Nevin Liber, Dr. Tondo, Daveed Vandevoorde, and Steve Vinoski. Our well-justified thanks go to the members of the LATEX community, who have made available such powerful typesetting tools.

Chapter 1. After having read this chapter and worked through the exercises, you should be able to write, compile, and execute simple programs. Later chapters will assume that you can use the features introduced in this chapter, and will explain these features in more detail. The way to learn a new programming language is to write programs. Our store keeps a file of transactions, each of which records the sale of one or more copies of a single book. Each transaction contains three data elements: X 4 From time to time, the bookstore owner reads this file and for each book computes the number of copies sold, the total revenue from that book, and the average sales price.

Although main is special in some ways, we define main the same way we define any other function. In this example, main has an empty list of parameters shown by the with nothing inside. The main function is required to have a return type of int, which is a type that represents integers. The int type is a built-in type, which means that it is one of the types the language defines. When a return statement includes a value, the value returned must have a type that is compatible with the return type of the function.

In this case, the return type of main is int and the return value is 0, which is an int. Note Note the semicolon at the end of the return statement.

They are easy to overlook but, when forgotten, can lead to mysterious compiler error messages. On most systems, the value returned from main is a status indicator. A return value of 0 indicates success. A nonzero return has a meaning that is defined by the system.

Key Concept: Types Types are one of the most fundamental concepts in programming and a concept that we will come back to over and over in this Primer. A type defines both the contents of a data element and the operations that are possible on those data. The data our programs manipulate are stored in variables and every variable has a type. Compiling and Executing Our Program Having written the program, we need to compile it. How you compile a program depends on your operating system and compiler.

For details on how your particular compiler works, check the reference manual or ask a knowledgeable colleague. Many PC-based compilers are run from an integrated development environment IDE that bundles the compiler with build and analysis tools. These environments can be a great asset in developing large programs but require a fair bit of time to learn how to use effectively. Learning how to use such environments is well beyond the scope of this book. Most compilers, including those that come with an IDE, provide a command-line interface.

Unless you already know the IDE, you may find it easier to start with the command-line interface. Moreover, once you understand the language, the IDE is likely to be easier to learn. Program Source File Naming Convention Whether you use a command-line interface or an IDE, most compilers expect program source code to be stored in one or more files. Program files are normally referred to as a source files. On most systems, the name of a source file ends with a suffix, which is a period followed by one or more characters.

Different compilers use different suffix conventions; the most common include. Running the Compiler from the Command Line If we are using a command-line interface, we will typically compile a program in a console window such as a shell window on a UNIX system or a Command Prompt window on Windows. Assuming that our main program is in a file named prog1. The compiler generates an executable file. On a Windows system, that executable file is named prog1.

UNIX compilers tend to put their executables in files named a. To run an executable on Windows, we supply the executable file name and can omit the. On both UNIX and Windows systems, after executing the program, you must issue an appropriate echo command. This command generates an executable file named prog1 or prog1.

If the -o prog1 is omitted, the compiler generates an executable named a. The Microsoft compiler automatically generates an executable with a name that corresponds to the first source file name.

The executable has the suffix. In this case, the executable is named prog1. Compilers usually include options to generate warnings about problematic constructs. It is usually a good idea to use these options. Exercises Section 1. Compile and run the main program from page 2. Jan 24, Update Jan 28, Add soultions to second half of exercises in chapter Feb 11, Add range check for subscript operator in ex Mar 3, Mar 1, Add soultions to exercises 17 to 27 in chapter C is a very portable language, but you may find differences between how a program works on your system and how it works on ours.

Experiment with changing part of a program to see what the effect is. Modify a program to do something slightly different. See if you can develop an alternative approach.



0コメント

  • 1000 / 1000